GNOME Bugzilla – Bug 332554
More metapost text positioning
Last modified: 2008-05-18 14:13:46 UTC
Please describe the problem: The metapost command label uses a bouding box as a reference point for positioning text. This makes consistent positioning impossible. Steps to reproduce: 1. Create a UML class diagram 2. Enter a list of properties, some with tall letters T,h,I, some with normal letters e,o,u,w, and some with descending letters p,y,j. Say This, puny, car, with some other words thrown in for good measure. 3. Export MetaPost, compile, and observe the uneven spacing of the words. Actual results: The words aren't spaced right. Expected results: Have them be spaced right Does this happen every time? Yes Other information: > It turns out that MetaPost positions text based on the bounding box of that > particular text element. So, the left, right, top, bottom, etc. commands > are difficult to apply correctly. I.E. if you position the words: dog cat > sea This puny. And you try to align them by their tops, bottoms, or > middles, you'll get a zigzag of words. Some poke down, some poke up, some > do both, and some do neither.... As the label command (and all its variants) are based on positioning text by the coordinates of its bounding box, it is impossible to get precisely controlled text layout (using that command) in MetaPost. Instead, you have to use the (less powerful) draw command... It is less powerful because draw won't take care of left, right, center positioning for you. You'll have to calculate that yourself... For an example of a list of attributes in a UML diagram. So, instead of this: label.urt(btex {\usefont{OT1}{cmr}{m}{n} dog} etex scaled 1.0, (0.0x,0.00y)); label.urt(btex {\usefont{OT1}{cmr}{m}{n} cat} etex scaled 1.0, (0.0x,0.35y)); label.urt(btex {\usefont{OT1}{cmr}{m}{n} sea} etex scaled 1.0, (0.0x,0.70y)); label.urt(btex {\usefont{OT1}{cmr}{m}{n} This} etex scaled 1.0, (0.0x,1.05y)); label.urt(btex {\usefont{OT1}{cmr}{m}{n} puny} etex scaled 1.0, (0.0x,1.40y)); We need to do this... draw btex {\usefont{OT1}{cmr}{m}{n} dog} etex scaled 1.0 shifted (5.0x,0.00y); draw btex {\usefont{OT1}{cmr}{m}{n} cat} etex scaled 1.0 shifted (5.0x, 0.35y); draw btex {\usefont{OT1}{cmr}{m}{n} sea} etex scaled 1.0 shifted (5.0x,0.70y); draw btex {\usefont{OT1}{cmr}{m}{n} This} etex scaled 1.0 shifted (5.0x,1.05y); draw btex {\usefont{OT1}{cmr}{m}{n} puny} etex scaled 1.0 shifted (5.0x,1.40y); The 'x' coordinate difference is just to separate the two columns of text. The 'y' values are separated by 0.35cm (the font size). Using the draw command with the shifted transformation, you position text by the left end of the baseline. Using the label.??? command, you position text such that its bounding box is positioned ??? relative to the coordinate point. In this case, label.urt means the bounding box is up and to the right of the specified coordinates. While left-right-center will take some special handling (although Dia must do this someplace already), this should be an answer once and for all... There were some hackish fixes suggested where you insert an invisible character that reaches to the top of the tallest and the bottom of the lowest character.
pushing bugs forward to a still relevant version, 0.95-pre2 is not
I've looked at this, and have written a couple of test files and some code. I've used the macro suggestions Rob McDonald made on the mailing list: % Define macro for horizontal centering. vardef hcentered primary P = P shifted -(xpart center P, 0) enddef; % Define macro for right justification. vardef rjust primary P = P shifted -(xpart (lrcorner P - llcorner P), 0) enddef; and plain draw for left justification.
Created attachment 81488 [details] Test cases for MetaPost Two test cases: Diagram1 -> .dia, .mp and .pdf. This is a UML class as per Comment #1. Created using patch and then mptopdf to create pdf. TextTestCase -> .dia, .mp and .pdf. This has two different text sizes and shows problems with left, centred and right aligned text which are fixed by the patch.
Created attachment 81489 [details] [review] Patch referenced in comment #2 and #3 Note that text scaling has been changed (from 3.0 to 2.05). THis may be the WRONG thing to do, but it made the test cases look pretty similar when comparing the MetaPost and Dia files.
Created attachment 81558 [details] Metapost Test Cases Added extra test cases - serif, monospace and cmr10.
Created attachment 81559 [details] [review] Improved font size matching Changed font scaling for each font type (sans, serif, monospace, cmr).
Created attachment 81569 [details] [review] Patch with text_line Experimental patch using text_line, something's wrong with the makebox, though, complains about missing $.
The 'experimental' patch was applied before dia-0.96 was released, so I think this is obsolete (or broken with 0.96)? Revision: 3609 Author: lclausen Date: 17:52:32, Samstag, 3. Februar 2007 Message: VDX v. 0.9 ---- Modified : /trunk/ChangeLog Modified : /trunk/plug-ins/metapost/render_metapost.c Modified : /trunk/plug-ins/vdx/vdx-import.c Modified : /trunk/plug-ins/vdx/vdx-xml.c Modified : /trunk/plug-ins/vdx/vdx.h Modified : /trunk/plug-ins/vdx/visio-types.h