[metapost] Stroke Font

Laurent Méhats laurent.mehats at gmail.com
Mon May 18 13:09:34 CEST 2015


Le 18/05/2015 05:23, Troy Henderson a écrit :
>     Here is an attempt that goes the way you describe.
>
>     Regards,
>     Laurent Méhats
>
>
> I've attached a sample, and I'm having a little trouble applying your
> technique to math.  Attached is the source file(s) and resulting output
> PDF.  As you can see, the fraction line is the only part of the formula
> that is not "traced".  I would appreciate any help on fixing this.
>
> Troy

Here is how you may proceed. It works fine with fractions, not that much 
with square roots. However stroked lines are too long; but if we use 
'pencircle' rather than 'pensquare' with 'linecap' set to 'butt', outlines 
are not complete.

Laurent

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

vardef init (suffix glp_num, pth_num, glp_pth, stk_num, stk_pth, stk_thk) 
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_pth[i]     rank i stroked line
%  stk_thk[i]     thickness of the rank i stroked line
%                   (i ranges from 0 to stk_num-1)
string fnt_str, txt_str, sub_str;
numeric txt_wd;
glp_num:=0;
stk_num:=0;
for tkn within 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;
         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
   elseif stroked tkn:
     stk_pth[stk_num]:=pathpart tkn;
     picture pen_pct;
     pen_pct:=nullpicture;
     addto pen_pct doublepath makepath penpart tkn;
     stk_thk[stk_num]:=ypart(urcorner pen_pct-llcorner pen_pct);
     stk_num:=stk_num+1;
   fi
endfor
enddef;

picture pct;     % btex .. etex material
color fll_clr,   % fill color
       bkg_clr,   % background color
       oln_clr;   % outlines color
numeric oln_thk; % outlines thickness

pct:=btex $\displaystyle\int_a^b \!f(x)\,dx \approx 
\frac{b-a}{N}\sum_{n=1}^N 
f\left(a+n\cdot\frac{b-a}{N}\right)\times\frac{\sqrt{123}}{\sqrt{123}}$ 
etex scaled 3;
fll_clr:=green;
bkg_clr:=white;
oln_clr:=red;
oln_thk:=.5;

beginfig(0)
numeric glp_num, pth_num[], stk_num, stk_thk[];
path glp_pth[][], stk_pth[];
init (glp_num, pth_num, glp_pth, stk_num, stk_pth, stk_thk) pct;
% glyphs
for i=0 upto glp_num-1:
   for j=0 upto pth_num[i]-1:
     fill glp_pth[i][j] withcolor
       if turningnumber glp_pth[i][j]=1: fll_clr else: bkg_clr fi;
     draw glp_pth[i][j]
       withpen pencircle scaled oln_thk
       withcolor oln_clr;
   endfor
endfor
% stroked lines
for i=0 upto stk_num-1:
   draw stk_pth[i]
     withpen pensquare scaled (stk_thk[i]+oln_thk)
     withcolor oln_clr;
   draw stk_pth[i]
     withpen pensquare scaled (stk_thk[i]-oln_thk)
     withcolor fll_clr;
endfor
endfig;

end
--%<-- test.mp


More information about the metapost mailing list