What is the unit of font size in view#text_bounds method?

In the Sketchup::View class, there is an instance method called text_bounds which the third parameter(options) is a hash. This parameter has a key ‘size’, I think it should be the size of the text font.

In the entity info board, I can see the font size is 12 points, so I set options[:size] = 12, and in the current active view, this text_bounds method returns a bound with the width of 63 and the height of 14. But this bound is much bigger than the bounds of the real text.

So I think it must be something wrong about the font size parameter. Does anyone know how to set the size?

Official example:

TEXT_OPTIONS = {
    :font => "Arial",
    :size => 20,
    :bold => true,
    :align => TextAlignRight,
    :align => TextVerticalAlignBaseline
  }

The unit (according to the documentation) is DeskTop Publishing points.

The DTP point is defined as 1⁄72 of an international inch (1/72 × 25.4 mm ≈ 0.353 mm) and, as with earlier American point sizes, is considered to be 1⁄12 of a pica.

What the size actually is in pixels on the screen depends upon the user’s display scaling and the dot pitch of their display.


The View.html#text_bounds documentation says:

The bounds are not a tight fit around the top and bottom as they include varying amount of line spacing depending on the font used.

It also depends whether there are characters in the text used that descend below the baseline or above the capline.


REF:

I’m not very understanding. If this method depends on the display, and Sketchup Ruby API does not have such a method to get the display’s attributes, what is this method used for?

The API does have a UI.scale_factor method, bit it is currently bugged at least on Windows. It always returns the display scaling of the Windows “Main” display, regardless of which display SketchUp is running on.

It gets the result size of the 2D bounds in screen space, but again, the documentation says it is not a tight fit.
It also says that the result text drawn with View#draw_text is scaled by UI.scale_factor.

There is also a note for MacOS that …

some fonts on Mac might not align as expected due to the system reporting incorrect font metrics.

So, your example of the 2D bounds being 14 instead of 12 is only 2 pt larger. This is not much larger in actuality.
If your display displayed exactly 72pt == 1 inch, then the difference between 12pt and 14pt would only be 0.028 of an inch.

So, as to what you can use the method for? Look at the example for the View#text_bounds method. It shows how to draw a box around the text on the screen.

1 Like

I just tested this on my external UHD display which is at 150% display scaling.

The example sets the text height to 20pt. But the View#text_bounds correctly reports the scaled height of 30pt.

So although UI.scale_factor incorrectly returns 1.25 instead of 1.50 for my external display, the text is correctly scaled.


If you did not wish to honor the user’s display scaling setting, then you will have to find the scaling by dividing the height result from View#text_bounds by the desired text height, … and then reduce the actual text height by that factor.

So, if I still wanted text at 20pt on my display, I would set the text to 13.333 (20 / 1.5)

Ok, I understand now.
I have to make an explanation in case someone also has such doubt:
the result of the method View#text_bounds is a Geom::Bounds2d, and the unit of this result is pt, I thought the unit should be inch or something else, this really confused me for a long time.

Yes, I need to open an API Issue about this as it is confusing.

Basically, the API writers got lazy and did not create a classes for 2D screen space rectangles and coordinates in pixels.
Instead, they used the existing Geom::Bounds2d class, which is for paper space rectangles in inches.

Also, notice how they wrote the two View text methods needing the screen space position arguments using Geom::Point3d with a z value of 0. This was also lazy of them.

Also, weirdly the Geom::Bounds2d#width and Geom::Bounds2d#height methods should return Length class in inches but instead return Float.

You cannot change display scaling whilst SketchUp is open, SketchUp uses Qt which cannot adapt to the change.

You must close SketchUp, change the display scaling, log off your Windows account, log back in and then restart SketchUp.