Hi, everyone,
I don't know if this question is specific to OpenSCAD (Nightly
2025.07.11) or more about the way computer text layout works in general
(maybe kerning or one of the many other strange text layout things I
don't understand.)
If I translate the text "ABC" to the left by half the x size give by
textmetrics, its position is not quite the same as the I get by
centering it using halign. Why is there a difference?
And, if I change the text string to be a single character "A" the two
approaches exactly overlap, but using "B" they do not. What's going on here?
Cheers,
Steve
s = "ABC";
color("red") text(s, halign = "center");
color("lime") translate([textmetrics(s).size.x/-2, 0, 0]) text(s);
On 7/14/2025 10:30 AM, Steve Lelievre via Discuss wrote:
I don't know if this question is specific to OpenSCAD (Nightly
2025.07.11) or more about the way computer text layout works in
general (maybe kerning or one of the many other strange text layout
things I don't understand.)
Somewhere in-between.
If I translate the text "ABC" to the left by half the x size give by
textmetrics, its position is not quite the same as the I get by
centering it using halign. Why is there a difference?
Because the bounding box is not the same as the advance value, and is
also not necessarily rooted at the origin.
I think halign centers over the total advance for the string, while the
"size" value returned by textmetrics is the actual extent of the ink.
Somewhere I have a demo that draws text with all of the various metrics
marked. I'll try to remember to find it, but I really ought to pretend
to do some real work.
And, if I change the text string to be a single character "A" the two
approaches exactly overlap, but using "B" they do not. What's going on
here?
Different characters have different shapes and different spacing. In
particular, compare the "position" value for "A" and "B". The "A" is
positioned further left, because it's OK if its pointy left corner is
closer to the previous character, where a "B" would look crowded.
Kerning: Kerning considers pairs of letters and how they fit together.
Here's an example that I noticed as a kid (and the sign is still there):
Note how it looks like there's too much space between the L and the V.
That's because the L has a bunch of empty space in its top right, and
the V has a bunch of empty space in its bottom left, and the people who
made the sign didn't understand kerning. Properly kerned, those two
would be pulled together, perhaps even to the point of having their
bounding boxes overlap.