SkFont::getWidths measure units and DPI

308 views
Skip to first unread message

Андрей Гаврюшенко

unread,
May 23, 2023, 5:51:32 PM5/23/23
to skia-discuss
I use skia to measure text's width and to build own pixel independent text layout. As I understood  method SkFont::getWidths returns the advanced width for each glyph in pixels.

I have investigated that skia uses free type library under hood to fetch information about font metrics for font face . Skia creates inside SkScalerContext_FreeType  and calls FT_Set_Char_Size for new scaled font face and set default DPI equal 72:

if (FT_IS_SCALABLE(fFaceRec->fFace)) {
err = FT_Set_Char_Size(fFaceRec->fFace.get(), scaleX, scaleY, 72, 72);
if (err != 0) {
SK_TRACEFTR(err, "FT_Set_CharSize(%s, %f, %f) failed.", fFaceRec->fFace->family_name, fScale.fX, fScale.fY);
return;
}
}

As I understood when I called SkFont::getWidths  in this case 1 px = 1 pt?
I wonder how I get advanced width of each glyph in points (1/72 inch) to build pixel independent text layout for different DPI(for example I have logical dpi equal 96 on my OS now )? Is there any way to get glyph width information in pixel independent units?

I know that free type allows to pass special configuration flag FT_LOAD_NO_SCALE in function FT_Load_Glyph don't scale the outline glyph loaded  and t keep it in font units. Does it possible do via Skia?

bungeman

unread,
May 24, 2023, 10:54:17 AM5/24/23
to skia-discuss
Don't read anything interesting into this "72", it is just a FreeType constant that needs to be used to get sane output due to "72" being hard coded in several places in FreeType. FreeType itself has no real idea about units like inches. SkFont::getWidths returns the advances of the glyphs if they are drawn by Skia through an identity matrix. Skia itself generally (outside of PDF) doesn't know anything about pixels per inch either. That is up to the application to know that and set the appropriate transformation matrix on the SkCanvas.

Андрей Гаврюшенко

unread,
May 29, 2023, 11:57:48 AM5/29/23
to skia-discuss
For example, I recieved 100 pt as the available width for text. How can I calculate how many text symbols fit in this width? Should I convert SkFont::getWidths returned value into pt? I little bit confused what is advanced width and in what unit of measure it measures.
середа, 24 травня 2023 р. о 17:54:17 UTC+3 bungeman пише:
Reply all
Reply to author
Forward
0 new messages