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

%D This module replaces the by now rather old supp-fun module.

\writestatus{loading}{ConTeXt Typesetting Macros / Text Hacks}

\unprotect

% registerctxluafile{typo-txt}{}

%D \macros
%D   {normalizefontheight,normalizefontwidth,normalizedfontsize}
%D
%D Next we introduce some font manipulation macros. When we
%D want to typeset some text spread in a well defined area, it
%D can be considered bad practice to manipulate character and
%D word spacing. In such situations the next few macros can be
%D of help:
%D
%D \starttyping
%D \normalizefontheight \name {sample text} {height} {font}
%D \normalizefontwidth  \name {sample text} {width}  {font}
%D \stoptyping
%D
%D Consider for instance:
%D
%D \startbuffer[a]
%D \normalizefontheight \TempFont {X} {2\baselineskip} {Serif}
%D \stopbuffer
%D
%D \startbuffer[b]
%D \ruledhbox{\TempFont To Be Or Not To Be}
%D \stopbuffer
%D
%D \typebuffer[a,b] \getbuffer[a]
%D
%D This shows up as:
%D
%D \startlinecorrection
%D \ruledhbox{\getbuffer[b]}
%D \stoplinecorrection
%D
%D The horizontal counterpart is:
%D
%D \startbuffer[a]
%D \normalizefontwidth \TempFont {This Line Fits} {\hsize} {Serif}
%D \stopbuffer
%D
%D \startbuffer[b]
%D \ruledhbox{\TempFont This Line Fits}
%D \stopbuffer
%D
%D \typebuffer[a,b] \getbuffer[a]
%D
%D This gives:
%D
%D \startlinecorrection
%D \ruledhbox{\getbuffer[b]}
%D \stoplinecorrection
%D
%D The calculated font scale is avaliable in \type {\normalizedfontsize}.

\newbox\b_typo_normalizers

\def\typo_normalizers_size#axis#size%
  {\dimexpr\ifdim#1\b_typo_normalizers=\zeropoint
     \bodyfontsize
   \else
     \luaexpr{\number\dimexpr10pt\relax*\number\dimexpr#size\relax/\number#axis\b_typo_normalizers}\scaledpoint
   \fi\relax}

\def\typo_normalizers_font_at_size#axis#cs#text#size#font% avoid overflow by using lua
  {\begingroup
   \setbox\b_typo_normalizers\hbox{\definedfont[#font at 10pt]\settrialtypesetting#text}%
   \normalexpanded{\endgroup\edef\noexpand\normalizedfontsize{\the\typo_normalizers_size{#axis}{#size}}}%
   \definefont[\strippedcsname#cs][#font at \normalizedfontsize]}

\unexpanded\def\typo_normalizers_text_at_size#axis#font#size#text%
  {\dontleavehmode
   \begingroup
   \setbox\b_typo_normalizers\hbox{\definedfont[#font at 10pt]\settrialtypesetting#text}%
   \definedfont[#font at \the\typo_normalizers_size{#axis}{#size}]#text%
   \endgroup}

\def\normalizedfontsize{\bodyfontsize}

\unexpanded\def\normalizetextwidth {\typo_normalizers_text_at_size\wd}
\unexpanded\def\normalizetextheight{\typo_normalizers_text_at_size\ht}
\unexpanded\def\normalizetextdepth {\typo_normalizers_text_at_size\dp}
\unexpanded\def\normalizetextline  {\typo_normalizers_text_at_size\htdp}

\unexpanded\def\normalizefontwidth {\typo_normalizers_font_at_size\wd}
\unexpanded\def\normalizefontheight{\typo_normalizers_font_at_size\ht}
\unexpanded\def\normalizefontdepth {\typo_normalizers_font_at_size\dp}
\unexpanded\def\normalizefontline  {\typo_normalizers_font_at_size\htdp}

\unexpanded\def\widthspanningtext #text#size#specification{\hbox{\normalizefontwidth \temp{#text}{#size}{#specification}\temp#text}}
\unexpanded\def\heightspanningtext#text#size#specification{\hbox{\normalizefontheight\temp{#text}{#size}{#specification}\temp#text}}
\unexpanded\def\depthspanningtext #text#size#specification{\hbox{\normalizefontdepth \temp{#text}{#size}{#specification}\temp#text}}
\unexpanded\def\linespanningtext  #text#size#specification{\hbox{\normalizefontline  \temp{#text}{#size}{#specification}\temp#text}}

%D Traditionally we use UpperCasedNames for this kind of functionality.

\let\NormalizeFontHeight   \normalizefontheight
\let\NormalizeFontWidth    \normalizefontwidth
\let\NormalizeTextHeight   \normalizetextheight
\let\NormalizeTextWidth    \normalizetextwidth

\let\WidthSpanningText     \widthspanningtext

\def\TheNormalizedFontSize{\normalizedfontsize}

%D \macros
%D   {vulgarfraction}
%D
%D This code is moved from \type {cor-mis.mkiv}. We show three versions. First
%D the simple one using \type {\low} and \type {high}:
%D
%D \startbuffer
%D \def\vfrac#1#2%
%D   {\hbox{\high{\tx#1\kern-.25em}/\low{\kern-.25em\tx#2}}}
%D
%D test \vfrac{1}{2} test \vfrac{123}{456} test
%D \stopbuffer
%D
%D \typebuffer {\showmakeup\getbuffer}
%D
%D A better way to handle the kerning is the following, here we kind of assume
%D that tye slash is symmetrical and has nearly zero width.
%D
%D \startbuffer
%D \def\vfract#1#2%
%D   {\hbox{\high{\tx#1}\hbox to \zeropoint{\hss/\hss}\low{\tx#2}}}
%D \stopbuffer
%D
%D \typebuffer {\showmakeup\getbuffer}
%D
%D The third and best alternative is the following:
%D
%D {\showmakeup\getbuffer}\crlf\getbuffer
%D
%D This time we measure the height of the \type {/} and shift over the maximum
%D height and depths of this character and the fractional digits (we use 57 as
%D sample). Here we combine all methods in one macros.

\setnewconstant\vulgarfractionmethod\plusthree

\definehspace[\v!vulgarfraction][.25em] % [.15em]
\definesymbol[\v!vulgarfraction][/]     % [\raise.2ex\hbox{/}]

\unexpanded\def\vulgarfraction#1#2%
  {\dontleavehmode
   \hbox
     {\def\vulgarfraction{vulgarfraction}%
      \ifcase\vulgarfractionmethod
        #1\symbol[\v!vulgarfraction]#2%
      \or
        \high{\tx#1\kern-\hspaceamount\empty\v!vulgarfraction}%
        \symbol[\vulgarfraction]%
        \low {\kern-\hspaceamount\empty\v!vulgarfraction\tx#2}%
      \or
        \high{\tx#1}%
        \hbox to \zeropoint{\hss\symbol[\v!vulgarfraction]\hss}%
        \low{\tx#2}%
      \or
        \setbox0\hbox{\symbol[\vulgarfraction]}%
        \setbox2\hbox{\txx57}%
        \raise\ht0\hbox{\lower\ht2\hbox{\txx#1}}%
        \hbox to \zeropoint{\hss\symbol[\v!vulgarfraction]\hss}%
        \lower\dp0\hbox{\raise\dp2\hbox{\txx#2}}%
      \fi}}

\ifdefined\vfrac \else \let\vfrac\vulgarfraction \fi

%D \starttabulate[|l|l|]
%D \HL
%D \NC \bf method \NC \bf visualization \NC\NR
%D \HL
%D \NC 0 \NC \vulgarfractionmethod0 \vulgarfraction{1}{2} \NC\NR
%D \NC 1 \NC \vulgarfractionmethod1 \vulgarfraction{1}{2} \NC\NR
%D \NC 2 \NC \vulgarfractionmethod2 \vulgarfraction{1}{2} \NC\NR
%D \NC 3 \NC \vulgarfractionmethod3 \vulgarfraction{1}{2} \NC\NR
%D \HL
%D \stoptabulate

%D This is used in the beginners manual. One needs to set the font size to an
%D acceptable value for this to work.

\unexpanded\def\startnicelyfilledbox
  {\vbox\bgroup
   \forgetall
   \dosingleempty\dostartnicelyfilledbox}

\def\dostartnicelyfilledbox[#1]%
  {\letdummyparameter\c!width \hsize
   \letdummyparameter\c!height\vsize
   \letdummyparameter\c!offset\exheight % we obey to the outer exheight
   \letdummyparameter\c!strut \v!yes    % we obey to the inner strut !
   \getdummyparameters[#1]%
   \scratchoffset\dummyparameter\c!offset\relax
   \setbox\scratchbox\vbox to \dummyparameter\c!height \bgroup
   \hsize\dummyparameter\c!width\relax
   \emergencystretch10\scratchoffset
   \parfillskip\zeropoint
   \baselineskip\zeropoint plus \onepoint minus \onepoint
   \beginofshapebox
   \leftskip \scratchoffset
   \rightskip\scratchoffset}

\unexpanded\def\stopnicelyfilledbox
  {\doifelse{\dummyparameter\c!strut}\v!yes
     {\xdef\doflushnicelyfilledbox
        {\ht\shapebox\the\strutht
         \dp\shapebox\the\strutdp
         \box\shapebox}}%
     {\gdef\doflushnicelyfilledbox
        {\box\shapebox}}%
   \endofshapebox
   \doreshapebox
     {\doflushnicelyfilledbox}
     {\penalty\shapepenalty}
     {\kern\shapekern}
     {\vfil}%
   \kern\scratchoffset
   \vfilneg
   \flushshapebox
   \vfilneg
   \kern\scratchoffset
   \egroup
   \box\scratchbox
   \egroup}

\protect \endinput