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?
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.
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_boundscorrectly 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.