[metapost] Stroke Font

Laurent Méhats laurent.mehats at gmail.com
Sun May 31 15:45:18 CEST 2015


Le 16/05/2015 15:37, Troy Henderson a écrit :
> Is there a preferred method for converting a
>
> btex Here is my sentence etex
>
> from a picture to an array of closed paths that can be filled and stroked
> different colors?  Thus, I would like to convert the btex ... etex
> sentence picture into an array of outlines for each letter (in the current
> font) used in the sentence so that I can fill the outlines one color and
> draw them with another color.
>
> Thanks in advance,
>
> Troy Henderson


Hello,

Given a stroked token tkn within a btex/etex picture,
1. is it always the case that tkn is stroked with linecap set to butt ?
2. If not, is there a way to get a 'linecappart' from tkn ?
3. Then, if linecap is set correctly, can I always expect 'fill envelope 
(makepen makepath penpart tkn) of pathpart tkn' to look like tkn ?

Regards,
Laurent Méhats


--%<-- oln-test.mp
verbatimtex%&latex
\documentclass{article}
\usepackage[T1]{fontenc}
%\usepackage{cmbright}
%\usepackage{fourier}
%\usepackage{arev}
\begin{document}
etex;

vardef oln_init (suffix glp_num, pth_num, glp_pth, stk_num, stk_vlp) expr pct=
%  pct            btex .. etex material
%  glp_num        number of glyphs within pct
%  pth_num[i]     number of paths defining the rank i glyph
%                   (i ranges from 0 to glp_num-1)
%  glp_pth[i][j]  rank j path of the rank i glyph
%                   (j ranges from 0 pth_num[i]-1)
%  stk_num        number of stroked lines within pct
%  stk_vlp[i]     rank i envelope
%                   (i ranges from 0 to stk_num-1)
interim linecap:=butt;
glp_num:=0;
stk_num:=0;
for tkn within pct:
   if textual tkn:
     string fnt_str, txt_str, sub_str;
     transform trn;
     numeric txt_wd, pth_idx;
     picture sub_pct;
     fnt_str:=fontpart tkn;
     txt_str:=textpart tkn;
     (0, 0) transformed trn=(xpart tkn, ypart tkn);
     (0, 1) transformed trn=(xpart tkn+xypart tkn, ypart tkn+yypart tkn);
     (1, 0) transformed trn=(xpart tkn+xxpart tkn, ypart tkn+yxpart tkn);
     txt_wd:=0;
     for glp_idx=0 upto (length txt_str-1):
       sub_str:=substring (glp_idx, glp_idx+1) of txt_str;
       sub_pct:=sub_str infont fnt_str;
       pth_idx:=0;
       for sub_tkn within glyph ASCII sub_str of fnt_str
           scaled (fontsize fnt_str/1000)
           shifted (txt_wd, 0)
           transformed trn:
         glp_pth[glp_num][pth_idx]:=pathpart sub_tkn;
         pth_idx:=pth_idx+1;
       endfor
       txt_wd:=txt_wd+xpart (urcorner sub_pct-llcorner sub_pct);
       pth_num[glp_num]:=pth_idx;
       glp_num:=glp_num+1;
     endfor
   elseif stroked tkn:
     stk_vlp[stk_num]:=envelope (makepen makepath penpart tkn) of pathpart 
tkn;
     stk_num:=stk_num+1;
   fi
endfor
enddef;

vardef oln_draw (expr pct) text opt=
numeric glp_num, pth_num[], stk_num;
path glp_pth[][], stk_vlp[];
oln_init (glp_num, pth_num, glp_pth, stk_num, stk_vlp) pct;
for glp_idx=0 upto glp_num-1:
   for pth_idx=0 upto pth_num[glp_idx]-1:
     draw glp_pth[glp_idx][pth_idx] opt;
   endfor
endfor
for stk_idx=0 upto stk_num-1:
   draw stk_vlp[stk_idx] opt;
endfor
enddef;

picture pct;
pct:=btex$\displaystyle\left(\sqrt{\frac{\sqrt{123}}{\sqrt{a}}}\right\}$etex
   rotated 15 scaled 10;

beginfig(0)
   oln_draw (pct);
endfig;

beginfig(1)
   oln_draw (pct) withpen pencircle scaled 2;
   draw pct withcolor (0.7, 0.7, 0.7);
endfig;

beginfig(2)
   oln_draw (pct) withpen pencircle scaled 2 dashed evenly;
   draw pct withcolor (0.7, 0.7, 0.7);
endfig;

beginfig(3)
   draw pct withcolor (0.7, 0.7, 0.7);
   oln_draw (pct) withpen pencircle dashed evenly;
endfig;

end
--%<-- oln-test.mp



More information about the metapost mailing list