gpt4 book ai didi

c++ - FreeType 中的新线像素距离?

转载 作者:太空狗 更新时间:2023-10-29 23:35:51 25 4
gpt4 key购买 nike

我尝试在 OpenGL 和 FreeType2 中渲染文本,但我不知道如何在渲染字体时获取每行之间的空间(以像素为单位)。当我解析文本时,我希望能够做类似的事情

const char *text = "some text\n here";

for(char *p = text; *p; p++)
{
if(*p == '\n')
{
y -= newLineDistance;
continue;
}

...//render text here
}

一开始我用

newLineDistance = face->glyph->metrics.vertAdvance >> 6;

但它不适用于某些字体,因为文档还说它对非垂直字体(中文等)不可靠

最佳答案

不需要计算行距,因为FreeType已经提供了。自己计算也可能会产生错误的结果,因为它是艺术家定义的。例如,小写字母“g”的最低部分可能会在下一行延伸到大写字母“G”的最高部分之下。

来自文档: http://www.freetype.org/freetype2/docs/tutorial/step2.html

height

This field represents a default line spacing (i.e., the baseline-to-baseline distance) when writing text with this font. Note that it usually is larger than the sum of the ascender and descender taken as absolute values. There is also no guarantee that no glyphs extend above or below subsequent baselines when using this distance – think of it as a value the designer of the font finds appropriate.

使用方法:

// initialise freetype
FT_Library ft;
FT_Init_FreeType(&ft);
// load a font
FT_Face face;
FT_New_Face(ft, "path_to_font.ttf", 0, &face);
// set the font size, e.g. 48 pixels
FT_Set_Pixel_Sizes(face, 0, 48);
// get the default line spacing, note that it is measured in 64ths of a pixel
int line_spacing = face->height;
// get the scaled line spacing (for 48px), also measured in 64ths of a pixel
int scaled_line_spacing = face->size->metrics.height;

关于c++ - FreeType 中的新线像素距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28009564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com