4

I want to perform a loop (type within) on all the paths of a text. I tried to build a picture with btex ... etex but it seems that this kind of picture has only one element (length gives 1). I want something like the glyph example of mpman.pdf, but for a whole word. Is there a way?


picture mots[];

mots[1]:= btex coucou etex shifted (2cm,0);

beginfig(0);
show length(mots[1]);
for i within mots[1]:   
    show i;   
endfor;
draw mots[1];
endfig;

end.

3 Answers 3

4

Yes, but you need to go down a level. Like this:

prologues := 3;
outputtemplate := "%j%c.eps";
picture word;  word = btex Cuckoo etex;
beginfig(1);
    x = 0;
    for p within word:
        if textual p:
            string w; w = textpart p;
            for i = 1 upto length w:
                string c; picture g;
                c = substring (i-1, i) of w;
                g = glyph c of fontpart p scaled 1/4;
                for thing within g:
                    drawarrow pathpart thing shifted (x,0);
                endfor
                x := x + 160;
            endfor
        fi
    endfor
endfig;
end.

Compiling this with mpost should produce an EPS file that looks like this:

enter image description here

There may be more efficient ways of doing this, but I have not discovered any.

Notice that textpart returns a string of one or more characters, and that there may be more than one pathpart in the <picture> returned by glyph (for example in the "o" characters).

2
  • Tnak you, but it does not work if I use btex ff etex. Do you know why the ligature is different? May 16, 2023 at 20:08
  • 1
    sigh... because it is a different character in the font table. It is hard to make this sort of thing completely general. Did you see an error like this? Warning: Glyph interpreter failed (missing glyph '^^O'?)
    – Thruston
    May 16, 2023 at 21:11
3

First a somewhat different version of the other answer:

prologues := 3;
outputtemplate := "%j%c.eps";
beginfig(1);
    save g, x, w ;
    picture g;
    x := 0 ;
    string w; w := "Cuckoo" ;
    for i = 1 upto length w:
        g := glyph (substring (i-1, i) of w) of "cmr10" scaled 1/10 ;
        for thing within g:
            drawarrow pathpart thing shifted (x,0) withpen pencircle scaled 1pt ;
        endfor
        x := x + xpart urcorner g + 2pt ;
    endfor
endfig;
end.

cuckoo

(I don't know what that little thingy on the left is, it almost looks like a small piece of the arrow to the right, but I hope not.)

A bit more fun (well, MetaFun), compiled with context:

\startMPpage[offset=1ts]

picture word ;  

word = image(
    draw outlinetext.d ("Cuckoo")(scaled 5)
  ) ;

for i within word :
  drawarrow  pathpart i randomizedcontrols 1 withcolor (uniformdeviate(1),uniformdeviate(1),uniformdeviate(1));
endfor ;

\stopMPpage

more fun cuckoo

The output if the text is changed to "Too baffled". Note the kerning between the T and the o, and the ffl ligature.

too baffled

4
  • Thank you. However, I would like to have the word composed by TeX (ligature, kerning, etc.) May 16, 2023 at 20:08
  • 1
    In the "fun" solution, you get all that. I updated the answer with such an example.
    – mickep
    May 16, 2023 at 20:16
  • I tried to compile that with input metafun but it seems that is not working anymore... I tried with lualatex with \mplibformat{metafun} but no PDF is created and I don’t understand why. Any idea? May 16, 2023 at 20:29
  • Sorry, no idea, I only use ConTeXt for this.
    – mickep
    May 16, 2023 at 20:37
0

I’ve remembered of the Syracuse animation: https://melusine.eu.org/syracuse/metapost/animations/mehats/?swf=anim.swf#simple_source

Here is the code:


verbatimtex%&latex
\documentclass{article}
\usepackage{fourier}
\begin{document}
etex;
 
% text material and glyphs contours
 
picture tex_pct, glp_pct;
numeric glp_num, pth_num[];
path glp_pth[][];
% tex_pct       btex .. etex material
% glp_num       number of glyphs within tex_pct
% pth_num[i]    number of paths defining the rank i (0 .. glp_num-1) glyph
% glp_pth[i][j] rank j (0 .. pth_num[i]-1) path of the rank i glyph
% glp_pct       glyphs picture
 
tex_pct:=btex Too baffled etex scaled 3;
 
glp_pct:=nullpicture;
string fnt_str, txt_str, sub_str;
numeric txt_wd;
glp_num:=0;
for tkn within tex_pct:
  if textual tkn:
    fnt_str:=fontpart tkn;
    txt_str:=textpart tkn;
    txt_wd:=0;
    for glp_idx=0 upto (length txt_str-1):
      sub_str:=substring (glp_idx, glp_idx+1) of txt_str;
      pth_num[glp_num]:=0;
      for sub_tkn within glyph ASCII sub_str of fnt_str
          scaled (fontsize fnt_str/1000)
          xscaled xxpart tkn
          yscaled yypart tkn
          shifted (txt_wd+xpart tkn, ypart tkn):
        glp_pth[glp_num][pth_num[glp_num]]:=pathpart sub_tkn;
        addto glp_pct doublepath glp_pth[glp_num][pth_num[glp_num]];
        pth_num[glp_num]:=pth_num[glp_num]+1;
      endfor
      glp_num:=glp_num+1;
      txt_wd:=txt_wd+
        (xxpart tkn)*xpart urcorner (sub_str infont fnt_str);
    endfor
  fi
endfor

beginfig(0);
for i:= 0 upto glp_num-1:
    for j:= 0 upto pth_num[i]-1:
        draw glp_pth[i][j]; 
    endfor
endfor

endfig;

end

enter image description here

Unfortunately, I do not understand everything.

  • why the ligatures are ok here? (the ASCII parameter?)
  • if i am right, the kernings come with the bounding box of the glyph?
  • i do not understand why there is a factor 1/1000 for the fontsize.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .