sch-rnd-aux knowledge pool
Text alignment
text_align by Tibor 'Igor2' Palinkas on 2022-08-01 | Tags: howto, test, alignment, left, right, justify |
Abstract: n/a
Terms: origin and bbox
Each text object has an origin, which is normally its bottom left corner. A bbox (bounding box) is an invisible rectangular box around the text.
There are two kind of text objects: height-specified and bbox-specified.
For a height-specified text the stroke pen used for the text object specifies text height. Height includes font's descender and ascender; in other words, it's the distance between the highest point of the tallest glyph in the font and the lowest point of the most ascending glyph in the font. Text width is calculated depending on the text string. Bbox is also calculated, using pen's text hight and the calculated text width.
In a bbox-specified text object the origin corner of the bbox and the diagonal corner are specified, so that the width and height of the bbox are known. Text string is rendered with a scale so that it fits into the box. Since bbox's aspect rarely matches the aspect of the text, and text scaling will always be the same in x and y direction, this means the text will be smaller than the box in either height or width (but not both).
Single line text alignment
Note: mirroring a text object will mirror the bounding box, affecting the place of the origin within the bbox, but does not mirror the actual pixels of the rendered text (so the text is always readable, regardless of mirroring).
In case of height-specified text , either the start or the end of the rendered text is always the origin. If text string is edited to grow longer, new characters are appended, text width and the bbox width grows.
Since the bbox is always calculated to fit the rendered text, there can be no extra room around the text, thus the hailgn property doesn't play a role. Whether such text grows left or right depends on the x-mirror state of the text:
- when not mirrored, the origin is anchoring the bottom left corner and new characters appended to the string will grow the text to the right (existing characters stay in place)
- when mirrored, the origin is anchoring the bottom right corner and new characters appended to the string will grow the text to the left (shifting existing characters to the left)
Below is an example of a text "Hello" expanded to "Hello world!". The origin is marked with two arrows, showing positive x and y direction for text growth (in case of single line text only the horizontal direction is of interest). On the left side is the non-mirrored case, on the right side is the x-mirrored case.
In case of bbox-specified text , if text scaling bumps first into height constraint of the bbox, there will be empty space left between text width and bbox width. The allocation of this empty space is controlled by the halign property of the text object:
- start means the text is aligned near the origin (all extra space accumulates at the opposite side from the origin)
- center means the text is centered within the bbox (half of extra space placed on each side)
- end means the text is aligned away from the origin (all extra space accumulates at the side of the origin)
- word_justify means both ends of the rendered text should reach the side of the box and extra space should be evenly distributed among the whitespace characters of the text; if there's no whitespace, act as center
- justify means both ends of the rendered text should reach the side of the box and a portion of extra space should be evenly distributed among the whitespace characters and the other portion of extra space should be evenly distributed in between every two glyphs.
Below is an example of a bbox specified text with the box wider than the text. The origin is marked with two arrows, showing positive x and y direction for text growth and the opposite corner of the bbox is marked with two lines. On the left side is the non-mirrored case, on the right side is the x-mirrored case. Copies of the quote from top to bottom uses different halign values, following the order of the list above.
Multiple line text alignment
Not yet implemented.