1

I try to transform some text rendering code from WinForm to Skia.

Winform: graphics.MeasureString(text, font)

Skia: var rect = new SKRect(pos.X, pos.Y, 0, 0) paint.MeasureText(text, ref rect)

enter image description here enter image description here

If you compare the results you will realize that Skia actually calculates the size and position of the rendered text very precise. In contrast Winforms returns a much rougher approximation of size and position including an generous margin. (Please ignore the vertical offset.)

In order to replace WinForms with Skia I need to convert Skia to Winform results.

Has anybody an idea? Or am I on my own here?

Matze
  • 1,402
  • 2
  • 17
  • 24
  • Drawing text in WinForms (GDI+ or GDI), is as precise as you make it. Both the Graphics and TextRenderer classes provide format flags that allow to precisely specify how the text should be rendered. See the notes, e.g., [here](https://stackoverflow.com/a/54383828/7444103), [here](https://stackoverflow.com/a/53074638/7444103) and [here](https://stackoverflow.com/a/54772134/7444103). All generate default padding, if not otherwise specified, with a small difference, since TextRenderer has not [Grid-Fitting](https://stackoverflow.com/a/49953353/7444103). – Jimi Jun 17 '21 at 08:26
  • Using a rasterizer, you have no padding. If you need to replicate the same behavior, take these features into consideration. You didn't specify what of type canvas you're using for rendering, but, in any case, specify the precise Region. When measuring the text, set the rectangle to the actual size of the region (not just a point) and add *padding* if needed to the resulting SKRect. In this case, it means moving the text 2-3 pixels to the left and 2 down from the top. – Jimi Jun 17 '21 at 08:26

0 Answers0