I wonder what's the best way of getting a text bounding box with FreeType 2?
我想知道使用FreeType 2获取文本边界框的最佳方法是什么?
To get the linespace bounding box width, I iterate over all the characters of the text and get it's advance and kearning:
为了获得行间距边框宽度,我迭代文本的所有字符并获得它的前进和学习:
FT_Face face = ...;
text_bbox_width = 0;
while (*p_text)
{
...
FT_Get_Kerning(...);
text_bbox_width += (face->glyph->advance.x + kerning.x) >> 6;
}
How to get linespace bounding box height? Is it necessary to iterate or can it be obtained using font face data? I.e:
如何获得线性空间边界框的高度?是否需要迭代或使用字体数据获取?即:
text_bbox_height = (face->ascender - face->descender) >> 6
1 个解决方案
#1
3
Good news: you do not need to iterate over the characters in each of your strings. You can use face->size->metrics->height
, as described in 3. Global glyph metrics of http://www.freetype.org/freetype2/docs/tutorial/step2.html. Note the warnings on using ascender
and descender
.
好消息:您不需要遍历每个字符串中的字符。您可以使用face-> size-> metrics-> height,如http://www.freetype.org/freetype2/docs/tutorial/step2.html中的全局字形指标所述。请注意使用上升器和下降器的警告。
Do not mistake this height for the actual pixel bounding box. Individual glyphs may stick out of this box. You can use this line height to get an even spacing over multiple lines in the same text block. To get 'larger' or 'smaller' spacing, you can multiply this value with a constant, such as 1.5 or 2.0 for "double line spacing".
不要将此高度误认为是实际的像素边界框。单个字形可能会突然出现在这个框中。您可以使用此线高在同一文本块中的多行上获得均匀间距。要获得“更大”或“更小”的间距,可以将此值乘以常量,例如1.5或2.0,以表示“双行间距”。
I'm guessing that the value of height
that Freetype calculates is the "normal" or "optimal" line spacing for a certain font.
我猜测Freetype计算的高度值是某种字体的“正常”或“最佳”行间距。
#1
3
Good news: you do not need to iterate over the characters in each of your strings. You can use face->size->metrics->height
, as described in 3. Global glyph metrics of http://www.freetype.org/freetype2/docs/tutorial/step2.html. Note the warnings on using ascender
and descender
.
好消息:您不需要遍历每个字符串中的字符。您可以使用face-> size-> metrics-> height,如http://www.freetype.org/freetype2/docs/tutorial/step2.html中的全局字形指标所述。请注意使用上升器和下降器的警告。
Do not mistake this height for the actual pixel bounding box. Individual glyphs may stick out of this box. You can use this line height to get an even spacing over multiple lines in the same text block. To get 'larger' or 'smaller' spacing, you can multiply this value with a constant, such as 1.5 or 2.0 for "double line spacing".
不要将此高度误认为是实际的像素边界框。单个字形可能会突然出现在这个框中。您可以使用此线高在同一文本块中的多行上获得均匀间距。要获得“更大”或“更小”的间距,可以将此值乘以常量,例如1.5或2.0,以表示“双行间距”。
I'm guessing that the value of height
that Freetype calculates is the "normal" or "optimal" line spacing for a certain font.
我猜测Freetype计算的高度值是某种字体的“正常”或“最佳”行间距。