[metapost] accessing letters in picture (for...within loop)

Stephan Hennig mailing_list at arcor.de
Thu Nov 23 01:18:37 CET 2006

Dan Luecking schrieb:

> If you tried to do this is TeX, it would require the same
> calculations. Neither MetaPost nor TeX gives direct user
> access to individual letters in an already formed box/picture.

That's the fundamental problem.  I wished there were low level support
for text following a curve in either TeX or MetaPost.

> MetaPost is even a little better than TeX in that one can tell from
> the individual strings returned by for...within where kerning has
> taken place. This could even be used to help determine where a
> character is located: within each separate string there should be no
> kerning, so one can tell from its length (and from the length of each
> character) where the character occurs.

Thanks!  I have to play with the idea of storing character widths in an
array.  The problem with this approach is that fonts have to be
supported explicitly.  Unfortunately, fontpart doesn't provide an NFSS
specification so that one can calculate character width tables for
unexpected fonts on the fly.

In my current implementation---where character locations are calculated
by removing characters from textpart one by one---I'm already facing the
problem of reconstructing proper TeX input from the information MetaPost
makes available.  Ligatures have to be replaced by multiple letters,
certain 8 bit characters have to be remapped, some characters have to
be replaced by their TeX command, such as \textborn et al. and fontpart
has to be translated into a matching NFSS font selection command.
Unfortunately, all those actions are font specific and all in all these
approaches require very much manual work and support.

I'm currently working on an alternative approach.  In the intermediate
DVI step strings are stored character by character if there is
horizontal skip between them.  The idea is to insert such space in the
TeX run and compensate for that space later on the MetaPost side.
However, kerning has to be preserved, since otherwise one could refrain
from using TeX at all.  I've found out that the soul package is able to
do exactly what I want---adding inter word space while preserving
kerning information.  With a proper \sodef definition one can letter
space TeX input which MetaPost then accesses characterwise.  In the
\sodef definition inter word space has to match normal inter word space
plus the artificially inserted inter letter space.  On the MetaPost side
one can then correct the wrong character position by counting the number
of characters seen in the current for...within loop and subtracting n
times the inter letter space.

This approach has two advantages:

(i) characters are accessed one by one on the MetaPost side and
(ii) one does not need to reTeX the current character at the desired

The last item is very good news, since that way translation from
MetaPost to TeX input isn't required anymore.  Just clipping the
character from the picture should do as well.  This is only possible if
inter letter space is rather large, since otherwise kerning could lead
to artifacts due to overlapping character rectangles.

The soul part of this approach works quite well, the clipping idea has
to be tested still.  But since with my current solution---the naive
approach described before---I hit the maximum number of TeX strings
latexmp can handle this idea sounds promising.  I'll report back on the

But please, anyone, make low level support available for a text on path
feature in MetaPost or TeX!  Pretty please!

Best regards,
Stephan Hennig

More information about the metapost mailing list