Re: Metapost - text alignment



On 1/29/07, Lars Clausen <lars raeder dk> wrote:
As I see it, there's two possible situations:  Either the text is plain
text or it's a LaTeX formula (of some kind).  The formulas we have no
chance to fit correctly until a separate LaTeXObject is introduced (which
could start out as a clone of TextObject) which the MetaPost renderer can
handle specially.  Plain text we actually have a chance to not just
position right, but also have a reasonable width of -- which is used in
the diagram, for sizing boxes etc.  If we always leave the size to
LaTeX/MP, the output is 99% guaranteed to be different from what you see
in Dia.  If we give LaTeX/MP the size (width!), any plain text will be
just as in Dia, but formulas will need fixing.

For the plain text case:

Your plan works if you are perfectly exact in calculating the width of
the text.  However, if you get a slightly different result from the
text rendered in LaTeX, you will wreak havoc.  Lets say you can
predict the text width within 1%.  If you use my MetaPost macro to
force the text to fit your estimated width, you will end up with
inconsistent results.  Some text, you underpredict its width, so the
macro shrinks it slightly.  Some text, you overpredict its width, so
the macro grows it slightly.  What you end up with is subtle (random)
variations in the font size throughout the document.

On the other hand, if you leave things the way they are...  You can
use your 1% error estimate of text width within DIA to calculate
position, box size, etc.  You can output these diagrams and let
MP/LaTeX handle the text sizing.  All text will have exactly the same
(deterministic) font size.  However, things like the margins in a box
drawn around text will be subtlety wrong.  When I do a flowchart, I
usually pick a box size big enough to surround all of my text.  I then
use a consistent box size, even if sometimes it means the margins are
larger than others.  So, for me, slight variations in the margins are
unimportant.

Now lets say you are able to estimate the text width _exactly_ for
plain text.  Now your results match exactly, but they would've the
other way too (you gained nothing).  Use the estimate internally, and
let LaTeX handle its own size.  If your estimate is exact, then you
still end up with the same result out.  However, it requires no code
change, and no debugging.

I do not like the idea of introducing small random variations in text
size.   When I say I want a certain text size (0.35mm or 10pt), it is
more important to me to get the exact same text size everywhere than
it is exactly what that size is.

Of course, for truly complex LaTeX text, we don't have any hope of
calculating the size inside of DIA until we integrate a LaTeX renderer
into DIA in some way.

I'm very tempted to break the feature freeze and introduce LaTeXObject in
the Misc sheet, just so that all other text can be treated The Right Way
[tm].  Then it can later be updated to do LaTeX interaction in order to
render nicely and have the right bounding box.  I introduced render_object
in the renderer system exactly so that this kind of thing could be
handled.

Either way you go, I don't think you need to break the feature freeze
now.  Let the current version go out the door and lets get this really
right for the next version.

Thanks, I'll give it a try.  Do you have a quick-and-dirty way to see if
metapost output is correct (e.g. convert MP files directly to PNG or
something)?  I'm not a metaposter myself.

Unfortunately, you can't compile these MP files without LaTeX, so
you'll need to be set up with both.  Here is a complete MP macro I've
been using.  I'll include the LaTeX file I embed it into next.

             Rob


beginfig(1);
verbatimtex
\documentclass{minimal}
\begin{document}
etex

x = 1.0cm; y = -1.0cm;

%% Left justified text positioned with label.
%% Note that the space between the lines is not what you would want.
label.rt(btex {\usefont{OT1}{cmr}{m}{n} aaaaaaa} etex scaled
1.0,(3.0000x,4.650000y));
label.rt(btex {\usefont{OT1}{cmr}{m}{n} aa} etex scaled
1.0,(3.0000x,5.000000y));
label.rt(btex {\usefont{OT1}{cmr}{m}{n} aaab} etex scaled
1.0,(3.0000x,5.350000y));
label.rt(btex {\usefont{OT1}{cmr}{m}{n} aaaaa} etex scaled
1.0,(3.0000x,5.700000y));
label.rt(btex {\usefont{OT1}{cmr}{m}{n} aaay} etex scaled
1.0,(3.0000x,6.050000y));
label.rt(btex {\usefont{OT1}{cmr}{m}{n} aaaaaaa} etex scaled
1.0,(3.0000x,6.400000y));

%% Left justified text positioned with draw.
draw btex {\usefont{OT1}{cmr}{m}{n} aaaaaaa} etex scaled 1.0 shifted
(4.50000x,4.650000y);
draw btex {\usefont{OT1}{cmr}{m}{n} aa} etex scaled 1.0 shifted
(4.50000x,5.000000y);
draw btex {\usefont{OT1}{cmr}{m}{n} aaab} etex scaled 1.0 shifted
(4.50000x,5.350000y);
draw btex {\usefont{OT1}{cmr}{m}{n} aaaaa} etex scaled 1.0 shifted
(4.50000x,5.700000y);
draw btex {\usefont{OT1}{cmr}{m}{n} aaay} etex scaled 1.0 shifted
(4.50000x,6.050000y);
draw btex {\usefont{OT1}{cmr}{m}{n} aaaaaaa} etex scaled 1.0 shifted
(4.50000x,6.400000y);

% Define macro for horizontal centering.
vardef hcentered primary P =
 P shifted -(xpart center P, 0)
enddef;

%% Horizontally centered text positioned with draw
draw hcentered btex {\usefont{OT1}{cmr}{m}{n} aaaaaaa} etex scaled 1.0
shifted (7.00000x,4.650000y);
draw hcentered btex {\usefont{OT1}{cmr}{m}{n} aa} etex scaled 1.0
shifted (7.00000x,5.000000y);
draw hcentered btex {\usefont{OT1}{cmr}{m}{n} aaab} etex scaled 1.0
shifted (7.00000x,5.350000y);
draw hcentered btex {\usefont{OT1}{cmr}{m}{n} aaaaa} etex scaled 1.0
shifted (7.00000x,5.700000y);
draw hcentered btex {\usefont{OT1}{cmr}{m}{n} aaay} etex scaled 1.0
shifted (7.00000x,6.050000y);
draw hcentered btex {\usefont{OT1}{cmr}{m}{n} aaaaaaa} etex scaled 1.0
shifted (7.00000x,6.400000y);

% Define macro for right justification.
vardef rjust primary P =
 P shifted -(xpart (lrcorner P - llcorner P), 0)
enddef;

draw rjust btex {\usefont{OT1}{cmr}{m}{n} aaaaaaa} etex scaled 1.0
shifted (9.50000x,4.650000y);
draw rjust btex {\usefont{OT1}{cmr}{m}{n} aa} etex scaled 1.0 shifted
(9.50000x,5.000000y);
draw rjust btex {\usefont{OT1}{cmr}{m}{n} aaab} etex scaled 1.0
shifted (9.50000x,5.350000y);
draw rjust btex {\usefont{OT1}{cmr}{m}{n} aaaaa} etex scaled 1.0
shifted (9.50000x,5.700000y);
draw rjust btex {\usefont{OT1}{cmr}{m}{n} aaay} etex scaled 1.0
shifted (9.50000x,6.050000y);
draw rjust btex {\usefont{OT1}{cmr}{m}{n} aaaaaaa} etex scaled 1.0
shifted (9.50000x,6.400000y);

% Define macro to scale to a desired width.
primarydef P scaledw W =
 P scaled (W/(xpart (lrcorner P - llcorner P)))
enddef;

draw btex {\usefont{OT1}{cmr}{m}{n} $c^2=a^2+b^2$} etex scaled 1.0
shifted (11.00000x,4.650000y);
draw btex {\usefont{OT1}{cmr}{m}{n} Mary had a little lamb.} etex
scaled 1.0 shifted (11.00000x,5.000000y);

draw btex {\usefont{OT1}{cmr}{m}{n} $c^2=a^2+b^2$} etex scaledw 4.0cm
shifted (11.00000x,7.650000y);
draw btex {\usefont{OT1}{cmr}{m}{n} Mary had a little lamb.} etex
scaledw 4.0cm shifted (11.00000x,8.000000y);

endfig;
end;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]