Text vertical align based on descenders
Hello everyone. I know this is not specifically inkscape related, but i thought maybe someone here may help.
Please consider this simple code:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg id="svg_root" width="26in" height="16in" viewBox="0 0 26 16" xmlns="http://www.w3.org/2000/svg" xml:space="preserve" shape-rendering="geometricPrecision" text-rendering="geometricPrecision" image-rendering="optimizeQuality" fill-rule="evenodd" clip-rule="evenodd" xmlns:xlink="http://www.w3.org/1999/xlink">
<line id="line_obj" x1="0" y1="8" x2="26" y2="8" stroke-width="0.1" stroke="#000000"/> <text id="text_obj" transform="translate(13,8)" style="font-size: 4; font-family: 'Times New Roman'; font-style: italic; font-weight: bold; fill: #FF0000; stroke: none;">for</text>
</svg>
It simply draws a line, and a text "sitting" on it. Obviously the "f" letter, which is a descender, goes below the line.
Actually the text location (13,8) determines the placement related to the baseline of the font: i'd like to place at (13,8) the REAL bottom-left corner of the font. So, simply valign to bottom instead of baseline, or move up the text item by descender's amount.
I have to do this in scripting, as this is part of a PHP script which invokes inkscape to manage the SVG file. I was only able to imagine 2 solutions, but was not able to apply any of them. So here they are:
1) is there any SVG option, recognized by inkscape as well, that permits this? In SVG specs i saw the "dominant-baseline" and other baseline related params, but none seems to have effect in inkscape
2) can you think at any way to calc the descender offset via scripting? Something like a function: get_descent(fontfile,is_italic,is_bold,textline) Which , i thins case, would look like get_descent("/pathtofonts/times.ttf",1,1,"for")
Hope someone can help in some way, every suggestion will be really appreciated.
Alex
Hello again! I'm here to say help no more required. I solved the issue! I wanted to write here my solution, in case someone else will have some similar issue to solve.
It was simple enough: i used the PHP function imagettfbbox().
Evenif i always thought it returns the real bounding box of a truetype text, i found that the values of it's array had some "gaps" inside. If you do something like:
$lineinfo=imagettfbbox(90,0,$fontfilename,$textline);
You can generally find out text width and height this way:
$line_h=abs($lineinfo[1]-$lineinfo[7]); $line_w=abs($lineinfo[2]-$lineinfo[0]);
But indexes 1 and 7 are not only zero and textheight: if there's a descender or an ascender, you will see also negative values there. So, $lineinfo[1] is simply... the descender offset!
My solution then was easy: i simply made a proportion.
$svg_descender/$calculated_descender=$svg_height/$calculated_height
So:
$svg_descender=($svg_height/$line_h)*$lineinfo[1];
If you do know $svg_height, which is the svg offset you have to "sit" your text to, this helps a lot.
Cya!
participants (1)
-
Alexander Bonivento