SkFont::measureText in CanvasKit?

194 views
Skip to first unread message

Micha

unread,
Jan 15, 2024, 10:50:35 AM1/15/24
to skia-discuss
Hi all, 
is there an equivalent to SkFont::measureText in CanvasKit?
We need this for individual layouting and text positioning before rendering. We are implementing a cross-platform drawing interface using Skia/Win32 on Windows and CanvasKit/emscripten on Web. Part of the interface is something like "measureText(myFont, myText)". 

It is easy to implement for Skia/Win32 environment using SkFont::measureText. However, we found no counterpart in CanvasKit, which we could call through an emscripten C-API Interface. 

Thanks you any ideas and replies.
Michael.

Micha

unread,
Jan 15, 2024, 10:56:31 AM1/15/24
to skia-discuss
Is Font.getGlyphIDs => Font.getGlyphBounds => Sum up all the bounds a possible way to go?

Ben Wagner

unread,
Jan 16, 2024, 10:58:18 AM1/16/24
to skia-d...@googlegroups.com
Note that Canvas.drawText internally calls SkCanvas::drawSimpleText.
Note that this does not do any text shaping, so will only work well
with languages that don't require anything more than simple code point
to glyph conversions (for complex shaping you'll need to use something
like CanvasKit.Paragraph).

That being said, Canvas.drawText internally does something like
calling Font.getGlyphIDs and Font.getGlyphWidths, creates the position
of each glyph by accumulating the appropriate widths, then passes this
information to Canvas.drawGlyphs. Then one can use Font.getGlyphBounds
and join the bounds of the glyphs where they are positioned to get a
bounding rectangle. If using CanvasKit.Paragraph then that takes care
of shaping and providing the positioned glyph ids
(Paragraph.getShapedLines), and bounds can then be had through
Font.getGlyphBounds in a similar way.
> --
> You received this message because you are subscribed to the Google Groups "skia-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/bc7fb0a4-f8b1-49aa-9a49-23f677d77a95n%40googlegroups.com.

Micha

unread,
Jan 17, 2024, 7:59:25 AM1/17/24
to skia-discuss
Thanks for answering. I finally came up with calling getGlyphIDs for my text to measure, then getGlyphBounds on the resulting glyph IDs, then summing up the width of all the returned rectangles and calculating the maximum height of them. The result seems to be quite reasonable for me. 
That being said, it would anyway be nice to have a shortcut like Skia provides it (e.g. a simple measureText method on a CanvasKit font). 
Michael.

Reply all
Reply to author
Forward
0 new messages