meta-txt.mkii / last modification: 2020-01-30 14:15
%D \module
%D   [       file=meta-txt,
%D        version=2000.07.06,
%D          title=\METAPOST\ Graphics,
%D       subtitle=Text Tricks,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D In this library some handy text manipulations are
%D defined. Some can and will be improved as soon as the
%D \TEX||\METAPOST\ interface is stable. Some of the
%D solutions may look weird, which is entirely my fault,
%D since I implemented them in the process of getting grip
%D on this kind of manipulations. Undoubtly better
%D \METAPOST\ code is possible, but my way of learning
%D this kind of trickery happens to be by \quote {trial
%D and error} and \quote {look and feel} (as well as
%D identifying tricks in Hobby's code).

% textext ipv btex ... etex

% we need a proper prefix here


  if unknown context_text: input mp-text; fi;


% \def\newchar#1{\chardef#1=0 }

\ifdefined\MPtoks   \else \newtoks\MPtoks        \fi
\ifdefined\MPbox    \else \newbox \MPbox         \fi

\ifdefined\parwidth     \else \newdimen\parwidth     \fi
\ifdefined\parheight    \else \newdimen\parheight    \fi
\ifdefined\parvoffset   \else \newdimen\parvoffset   \fi
\ifdefined\parhoffset   \else \newdimen\parhoffset   \fi
\ifdefined\parlines     \else \newcount\parlines     \fi
\ifdefined\partoks      \else \newtoks \partoks      \fi
\ifdefined\shapetextbox \else \newbox  \shapetextbox \fi
                              \newif   \ifparseries
\ifdefined\parfirst     \else \chardef \parfirst=0   \fi

        \getMPdata % \readlocfile{\MPdatafile}{}{}%
        \advance\!!counta by \parlines
    %\ifx\partoks\emptytoks\else % safeguard
       \expanded{\parshape \the\!!counta \the\!!toksa}%
     \setshapecharacteristics % extra dummy


        \expandafter\appendtoks\space##1 \to\!!toksa
        \advance\!!counta \minusone
   \multiply\!!counta \plustwo
  %\ifx\partoks\emptytoks\else % safeguard

     {\global\parlines         \getvalue{parlines:\currentshapetext}%
      \global\chardef\parfirst \getvalue{parfirst:\currentshapetext}%
      \global\parvoffset       \getvalue{parvoffset:\currentshapetext}%
      \global\parhoffset       \getvalue{parhoffset:\currentshapetext}%
      \global\parwidth         \getvalue{parwidth:\currentshapetext}%
      \global\parheight        \getvalue{parheight:\currentshapetext}}
     {\global\parlines         \plusone
      \global\chardef\parfirst \zerocount
      \global\parvoffset       \zeropoint
      \global\parhoffset       \zeropoint
      \global\parwidth         \hsize
      \global\parheight        \vsize}}

   \setxvalue{parlines:\currentshapetext  }{\the\parlines}%
   \setxvalue{parfirst:\currentshapetext  }{\the\parfirst}%
   \setxvalue{parwidth:\currentshapetext  }{\the\parwidth}%
   \setxvalue{parheight:\currentshapetext }{\the\parheight}}

\def\getshapetext% option: unvbox
   \setbox\scratchbox\vbox to \parheight
     {\expanded{\switchtobodyfont[\@@shbodyfont]}% evt strutheight en
      \splittopskip\strutheight               % lineheight opslaan
      \vskip\parvoffset                       % scheelt switch en
      \ifcase\parfirst\or\vskip\lineheight\fi % is ook veiliger
      \hbox{\vsplit\shapetextbox to \parlines\lineheight}}%



%%%%%%% rotfont nog definieren


\def\processfollowingtoken#1% strut toegevoegd
     n := n + 1 ; len[n] := \the\wd\MPbox ;
   % pic[n] := textext{\RotFont\setstrut\strut#1} ; % btex \RotFont\setstrut\strut#1 etex ;
     pic[n] := btex \RotFont\setstrut\strut#1 etex ;
     pic[n] := pic[n] shifted - llcorner pic[n] ;

  % we default to nothing

     \includeMPgraphic{followtokens} ;
     picture pic[] ; numeric len[], n ; n := 0 ;
     if unknown RotPath  : path    RotPath  ; RotPath  := origin ; fi ;
     if unknown RotColor : color   RotColor ; RotColor := black  ; fi ;
     if unknown TraceRot : boolean TraceRot ; TraceRot := false  ; fi ;
     if unknown ExtraRot : numeric ExtraRot ; ExtraRot := 0      ; fi ;
     numeric al, at, pl, wid, pos ; pair ap, ad ;
     al := arclength RotPath ;
     if al=0 :
       al := len[n] + ExtraRot ;
       RotPath := origin -- (al,0) ;
     fi ;
     if al<len[n]:
       RotPath := RotPath scaled ((len[n]+ExtraRot)/al) ;
       al := arclength RotPath ;
     fi ;
     pl := (al-len[n])/(if n>1 : (n-1) else : 1 fi) ;
     if TraceRot :
       draw RotPath withpen pencircle scaled 1pt withcolor blue ;
     fi ;
     for i=1 upto n :
       wid := abs(xpart urcorner pic[i] - xpart llcorner pic[i]) ;
       pos := len[i]-wid/2 + (i-1)*pl ;
       at := arctime   pos of RotPath ;
       ap := point     at  of RotPath ;
       ad := direction at  of RotPath ;
       draw pic[i] shifted (-wid/2,0) rotated(angle(ad)) shifted ap
         withcolor RotColor ;
       if TraceRot :
         draw boundingbox
            pic[i] shifted (-wid/2,0) rotated(angle(ad)) shifted ap
            withpen pencircle scaled .25pt withcolor red ;
         draw ap
            withpen pencircle scaled .50pt withcolor green ;
       fi ;
     endfor ;

% \followtokens
%   {This is just a dummy text, kerned by T{\kern
%    -.1667em\lower .5ex\hbox {E}}{\kern -.125emX} and typeset
%    in a circle using {\setMFPfont M}{\setMFPfont
%    E}{\setMFPfont T}{\setMFPfont A}{\setMFPfont
%    P}{\setMFPfont O}{\setMFPfont S}{\setMFPfont T}.\quad}

   save height, span, drift, d, cp ;
   height := 3/ 5 * \baselinedistance ;
   span   := 1/ 3 * height ;
   drift  := 1/10 * height ;
   pickup pencircle scaled (1/12 * height) ;
   def d = (uniformdeviate drift) enddef ;
   for i := 1 upto \MPvar{n} :
       if (i mod 5)=0 : ((-d-4.5span,d)--(+d-0.5span,height-d))
       else           : ((-d,+d)--(+d,height-d)) fi
       shifted (span*i,d-drift) ;
   picture cp ; cp := currentpicture ; % for readability
   setbounds currentpicture to
     (llcorner cp shifted (0,-ypart llcorner cp) --
      lrcorner cp shifted (0,-ypart lrcorner cp) --
      urcorner cp -- ulcorner cp -- cycle) ;
   endgroup ;





   width=\the\localhsize, % without \the, problems in non e-tex


    height = \MPvar{height} ;
    x1 = 0 ; x3 = \MPvar{width} ; x2 = x4 = .5x3 ;
    y1 = y3 = 0 ; y2 = -y4 = height/2 ;
    fill z1..z2..z3 & z3..z4..z1 & cycle withcolor \MPvar{color} ;

   \setlocalhsize \noindent \reuseMPgraphic{EnglishRule}

%D The following macro returns a tight bound character
%D sequence.
%D \useMPlibrary[txt]
%D \startlinecorrection
%D \TightText{\ss\bf 123}{0cm}{3cm}{red}
%D \stoplinecorrection

  {\hbox % \ruledhbox
        picture p ; p := image (graphictext "#1" withfillcolor red) ;
        draw p xsized #2 ysized #3 withcolor \MPcolor{#4} ;

\protect \endinput