Get bounds of text drawn on a path

20 views
Skip to first unread message

Peter Nelson

unread,
Jun 6, 2024, 11:35:07 PMJun 6
to skia-discuss
Hello,

Is there a method or algorithm to get the bounds of just the text - not the path - of text drawn on a path using the canvas.DrawTextOnPath method? I have an app I am working on that draws text on a path. I want to be able to select the drawn text on the screen, then move or rotate it. That's easy to do when drawing text at a location using canvas.DrawText, because I specify the point, and then use MeasureText to get the width of the text as rendered. But I can't figure out how to get the bounding rectangle of just the rendered text (not the bounding rectangle of the path) when using DrawTextOnPath. If someone can tell me how to do that, I'd appreciate it!

Peter Nelson

unread,
Jun 7, 2024, 1:52:13 AMJun 7
to skia-discuss
Never mind - I figured it out, more or less:

                        using SKTextBlob sKTextBlob = SKTextBlob.CreatePathPositioned(LabelText, font, LabelPath, LabelPaint.TextAlign, new SKPoint(0, 0));
                        SKRect boundsRect = sKTextBlob.Bounds;

The only slightly tricky part was creating a SKFont from a Windows system.drawing.Font.

The resulting bounding box is somewhat larger than the text (it's a conservative bounding box, according to the documentation), but it works for my purposes. I don't need a tight bounding box for what I need to do.

Thanks to anyone that looked at my original post.
Reply all
Reply to author
Forward
0 new messages