[Solved] Centering Substring Text

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
chrisb
Veteran
Posts: 53920
Joined: Tue Mar 17, 2015 9:14 am

Re: Centering Substring Text

Post by chrisb »

wandererfan wrote: Wed Aug 05, 2020 1:18 am

Code: Select all

>>> s = App.ActiveDocument.ShapeString
>>> ss = s.Shape
>>> bearingX = ss.BoundBox.XMax - ss.BoundBox.XLength
I would have indeed expected that the bounding box would have the width of advance from your image, which includes some white space before the character - it is called bearing here - and some white space after the character - not named in your image.
Instead it seems that the bound box is just width wide.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Centering Substring Text

Post by openBrain »

My trick to center a ShapeString is to use an intermediate compound and some expressions.
As you use PartDesign, a ShapeBinder is needed.
I attached your file modified that way. You can change anything (cube width/length/height, shapestring text) and it will keep the text centered on the top face. ;)
Attachments
d6_OB.FCStd
(97.63 KiB) Downloaded 65 times
User avatar
wandererfan
Veteran
Posts: 6268
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Centering Substring Text

Post by wandererfan »

chrisb wrote: Wed Aug 05, 2020 5:47 am I would have indeed expected that the bounding box would have the width of advance from your image, which includes some white space before the character - it is called bearing here - and soome white space after the character - not named in your image.
Instead it seems that the bound box is just width wide.
This is standard FreeCAD behaviour. The bound box only includes the actual shape. The distance from the reference point/origin is never included.

Advance is used to determine the position of subsequent characters.
User avatar
Vincent B
Veteran
Posts: 4713
Joined: Sun Apr 05, 2015 9:02 am
Location: La Rochelle, France

Re: Centering Substring Text

Post by Vincent B »

easy with string feature of lattice2 wb:
select the face, then attach a datumpoint
call the string feature, then select datumpoint as link.
and choose middle for alignment.
Attachments
Capture.JPG
Capture.JPG (69.17 KiB) Viewed 1368 times
the6.FCStd
(11.24 KiB) Downloaded 26 times
Kirasuna
Posts: 4
Joined: Tue Aug 04, 2020 2:44 am

Re: Centering Substring Text

Post by Kirasuna »

wandererfan wrote: Wed Aug 05, 2020 1:18 am You need to also allow for "bearingX" (the empty space in the "em square" before the actual shape):
glyphMetrics.png

EDIT: this calculation is rubbish. Don't know what I was thinking. WF

Code: Select all

>>> s = App.ActiveDocument.ShapeString
>>> ss = s.Shape
>>> bearingX = ss.BoundBox.XMax - ss.BoundBox.XLength
The bearingX value is determined by the font designer, and will be different for every glyph in every font.
Using this to find bearingX made it so I could account for it, thank you so much.
User avatar
wandererfan
Veteran
Posts: 6268
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Centering Substring Text

Post by wandererfan »

Kirasuna wrote: Thu Aug 06, 2020 1:24 am Using this to find bearingX made it so I could account for it, thank you so much.
Glad it worked for you. What I wrote is still rubbish. :oops:

The formula should of course be simplified to just use BoundBox.XMin, and it only works if the Placement.Base of the ShapeString is the origin.

I'll have a look to see if there is a way to align the reference point with the BoundBox.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Centering Substring Text

Post by openBrain »

wandererfan wrote: Thu Aug 06, 2020 2:39 pm I'll have a look to see if there is a way to align the reference point with the BoundBox.
Because of cyclic dependency, I know no more efficient way than using a Compound as I did in my above post. ;)
notSure
Posts: 1
Joined: Sun Sep 17, 2023 3:00 pm

Re: Centering Substring Text

Post by notSure »

Vincent B wrote: Wed Aug 05, 2020 7:00 pm easy with string feature of lattice2 wb:
select the face, then attach a datumpoint
call the string feature, then select datumpoint as link.
and choose middle for alignment.
Thanks for this. It took me some fiddling to get this right, but now that I understand it, it is an easy solution.
Post Reply