% bxnewfont.sty %% package declaration \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{bxnewfont}[2016/03/27 v0.2] %% preparation \def\bxnf@pkgname{bxnewfont} \def\bxnf@error{\PackageError\bxnf@pkgname} \providecommand\bxDebug[1]{} %--------------------------------------- options %% variables \newif\ifbxnf@newfont %% options \DeclareOption{newfont}{% \bxnf@newfonttrue } \ProcessOptions\relax %--------------------------------------- general %% load packages \RequirePackage{etoolbox} \ifx\newrobustcmd\@undefined % non-e-TeX \expandafter\endinput\fi\relax %% unique tokens \def\bxnf@mk{\bxnf@mk@} \def\bxnf@end{\bxnf@end@} %% variables \newbool{bxnf@ok} %% \bxnf@cond\if...\fi{}{} \@gobbletwo\if\if \def\bxnf@cond#1\fi{% #1\expandafter\@firstoftwo \else \expandafter\@secondoftwo \fi } %% \ifbxnf@ptex : engine is pTeX? \newbool{bxnf@ptex} \begingroup \edef\bxnf@tmpa{\string\tfont}\edef\bxnf@tmpb{\meaning\tfont} \ifx\bxnf@tmpa\bxnf@tmpb \global\bxnf@ptextrue \fi \endgroup %--------------------------------------- main %% variables \newbool{bxnf@fixed} % fixed size? \let\bxnf@font\relax % font-spec string \let\bxnf@tfm\relax % tfm name \let\bxnf@atcl\relax % at-clause \let\bxnf@enc\relax % encoding \let\bxnf@fam\relax % family \let\bxnf@type\relax % 1=normal,2=pTeX-yoko,3=pTeX-tate \let\bxnf@size\relax %%<*> \newfontx*[]{} \newcommand\newfontx{% \@ifstar{% \bxnf@fixedfalse \bxnf@newfontx@a }{%else \bxnf@fixedtrue \bxnf@newfontx@a }% } \def\bxnf@newfontx@a#1{% \@ifnextchar[{% \bxnf@newfontx@b{#1}% }{%else \bxnf@newfontx@b{#1}[]% }%] } \def\bxnf@newfontx@b#1[#2]#3{% \edef\bxnf@enc{#2}% \edef\bxnf@font{#3}% \bxnf@split@name \bxnf@check@param \ifbxnf@ok \bxnf@declare \fi \ifbxnf@ok \bxnf@make@cmd#1\fi } %% \bxnf@split@name % Parses \bxnf@font and sets \bxnf@tfm and \bxnf@atcl. \def\bxnf@next#1{% \def\bxnf@split@name{% \expandafter\bxnf@split@name@a\bxnf@font\bxnf@mk#1\bxnf@end }% \def\bxnf@split@name@a##1#1##2\bxnf@end{% \ifstrempty{##2}{% \let\bxnf@tfm\bxnf@font \let\bxnf@atcl\relax }{%else \def\bxnf@tfm{##1}% \bxnf@split@name@b##2\bxnf@end }% }% \def\bxnf@split@name@b##1\bxnf@mk##2\bxnf@end{% \def\bxnf@atcl{##1}% }% } \bxnf@next{ } %% \bxnf@check@param % Sets bxnf@ok. \def\bxnf@check@param{% \bxnf@oktrue \bxnf@get@tfm@info \unless\ifdim\bxnf@size>\z@ \chardef\bxnf@type\z@ \fi \ifnum\bxnf@type=\z@ \bxnf@error{Failed in getting TFM info}\@eha \bxnf@okfalse \else \ifx\bxnf@enc\@empty % resolve encoding when unspecified \ifcase\bxnf@type\or \let\bxnf@enc\cf@encoding \or % pTeX-yoko \let\bxnf@enc\cy@encoding \or % pTeX-tate \let\bxnf@enc\ct@encoding \fi \fi \fi \ifcsundef{T@\bxnf@enc}{% \bxnf@error{Unknown encoding '\bxnf@enc'}\@ehc \bxnf@okfalse }{}% \ifbxnf@ok \bxDebug{type=\number\bxnf@type; enc=\bxnf@enc}% \fi \unless\ifbxnf@fixed \unless\ifx\bxnf@atcl\relax \bxnf@error{You cannot use size spec here}{% The invalid size spec (\bxnf@atcl) is ignored.% \MeesageBreak\@ehc}% \fi\fi } %% \bxnf@declare \def\bxnf@declare{% \bxnf@get@family@name \unless\ifbxnf@ok \bxnf@set@family@param \bxnf@declare@family \fi \bxnf@oktrue } %% \bxnf@make@cmd\CS % Defines \CS to be a protected macro that selects the family % which name equals (the current value of) \bxnf@fam. % A wierdly-named control sequence is used in the macro body % so that \show'ing \CS will display something like: % > \CS=\protected macro: % \select font FAMILY(ID) . \def\bxnf@make@cmd#1{% \expandafter\bxnf@make@cmd@a\bxnf@fam\bxnf@mk#1% } \begingroup \catcode`\ =11\relax\catcode`\_=10\relax \gdef\bxnf@make@cmd@a#1\bxnf@mk#2{% __\newrobustcmd*#2{\select font#1 }}% \endgroup %% \select_font_ % (Here '_' means a space with catcode 11.) \begingroup \catcode`\ =11\relax\catcode`\_=10\relax \gdef\select font#1 {% __\bxnf@select@family{#1}}% \endgroup %--------------------------------------- Family name %% variables %\[bxnf@varid/] % maximum used id number %\[bxnf@prm/] % font parameter ({}{}) %\[bxnf@pc/] % cache %% \bxnf@get@family@name % Generates a family name and returns to \bxnf@fam. % The name is of the form "()". \def\bxnf@get@family@name{% \def\bxnf@tmpa{bxnf@pc/\bxnf@enc:\bxnf@tfm:% \ifbxnf@fixed \the\dimexpr\bxnf@size\relax \fi}% \letcs\bxnf@fam{\bxnf@tmpa}% \ifdef\bxnf@fam{% \bxDebug{\bxnf@tmpa==\bxnf@fam}% \bxnf@oktrue }{%else \csnumdef{bxnf@varid/\bxnf@tfm}{\csuse{bxnf@varid/\bxnf@tfm}+1}% \edef\bxnf@fam{\bxnf@tfm(\csuse{bxnf@varid/\bxnf@tfm})}% \cslet{\bxnf@tmpa}\bxnf@fam \bxDebug{\bxnf@tmpa:=\bxnf@fam}% \bxnf@okfalse }% } %% \def\bxnf@set@family@param % Sets \[bxnf@prm/*]. \def\bxnf@set@family@param{% \csedef{bxnf@prm/\bxnf@fam}{{\bxnf@enc}% {\ifbxnf@fixed \expandafter\rem@pt\bxnf@size \fi}}% \bxDebug{bxnf@prm/\bxnf@fam:=\csuse{bxnf@prm/\bxnf@fam}}% } %% \bxnf@declare@family \def\bxnf@declare@family{% \DeclareFontFamily{\bxnf@enc}{\bxnf@fam}{}% \DeclareFontShape{\bxnf@enc}{\bxnf@fam}{m}{n}% {<->\bxnf@tfm}{}% % In pTeX, a dummy entry must be declared for the % encoding counterpart. \let\bxnf@tmpb\relax \ifcase\bxnf@type\or % no-op for normal \or \letcs\bxnf@tmpb{t@enc@\bxnf@enc}% yoko->tate \or \letcs\bxnf@tmpb{y@enc@\bxnf@enc}% tate->yoko \fi \unless\ifx\bxnf@tmpb\relax \DeclareFontFamily{\bxnf@tmpb}{\bxnf@fam}{}% \DeclareFontShape{\bxnf@tmpb}{\bxnf@fam}{m}{n}% {<->ssub*\kanjifamilydefault/m/n}{}% \fi } %% \bxnf@select@family{} \def\bxnf@select@family#1{% \edef\bxnf@next{\noexpand\bxnf@select@family@a {#1}\csuse{bxnf@prm/#1}}% \bxnf@next } \def\bxnf@select@family@a#1#2#3{% \bxDebug{select=#2/#1/m/n/#3}% \usefont{#2}{#1}{m}{n}\relax \ifstrempty{#3}{}{%else \fontsize{#3}{#3}\selectfont }% } %--------------------------------------- Inquery on TFM %% variables \let\bxnf@type\relax \let\bxnf@size\relax %% \bxnf@get@tfm@info \def\bxnf@get@tfm@info{% \begingroup \chardef\bxnf@type=0 \let\bxnf@size\@empty \font\bxnf@tmpa=\bxnf@tfm\space scaled 2000\relax \ifx\bxnf@tmpa\nullfont\else \bxnf@get@tfm@info@a \bxnf@get@tfm@info@b \fi \xdef\bxnf@g@tmpa{% \chardef\bxnf@type=\number\bxnf@type\relax \def\noexpand\bxnf@size{\bxnf@size}}% \endgroup \bxnf@g@tmpa \bxDebug{tfm=\number\bxnf@type/\bxnf@size}% } \def\bxnf@get@tfm@info@a{% \bxnf@tmpa \bxnf@curr@font\bxnf@g@tmpa\font \chardef\bxnf@type=1 \let\bxnf@xfont\font } \def\bxnf@get@tfm@info@b{% \unless\ifnum\bxnf@type=0 \expandafter\bxnf@read@at\bxnf@g@tmpa\bxnf@end \ifx\bxnf@size\relax \let\bxnf@size\z@ \fi \dimdef\bxnf@size{\bxnf@size/2}% \bxDebug{tfm1=\number\bxnf@type/\bxnf@size}% \unless\ifx\bxnf@atcl\relax \let\bxnf@tmpb\bxnf@size \bxnf@xfont\bxnf@tmpa=\bxnf@tfm\space\bxnf@atcl\relax \bxnf@tmpa \bxnf@curr@font\bxnf@g@tmpa\bxnf@xfont \expandafter\bxnf@read@at\bxnf@g@tmpa\bxnf@end \ifx\bxnf@size\relax \let\bxnf@size\bxnf@tmpb \fi \fi \fi } %% \bxnf@read@at\bxnf@end \begingroup \catcode`\A=12 \catcode`\T=12 \lowercase{% \gdef\bxnf@read@at#1\bxnf@end{% \bxnf@read@at@a#1\bxnf@mk AT \bxnf@mk\bxnf@end }% \gdef\bxnf@read@at@a#1AT #2\bxnf@mk#3\bxnf@end{% \ifstrempty{#2}{% \let\bxnf@size\relax }{%else \def\bxnf@size{#2}% }% }% } \endgroup %% \bxnf@curr@font\CS\Xfont \def\bxnf@curr@font#1#2{% \xdef#1{\expandafter\meaning\the#2}% } \ifbxnf@ptex %----<*pTeX> %% revision to \bxnf@get@tfm@info \def\bxnf@get@tfm@info@a{% \bxnf@get@tfm@info@init \nullfont \bxnf@jnullfont \bxnf@tnullfont \bxnf@tmpa \bxnf@curr@font\bxnf@g@tmpa\font \unless\ifx\bxnf@g@tmpa\bxnf@dsf@null \chardef\bxnf@type=1 \let\bxnf@xfont\font \else \bxnf@curr@font\bxnf@g@tmpa\jfont \unless\ifx\bxnf@g@tmpa\bxnf@dsf@jnull \chardef\bxnf@type=2 \let\bxnf@xfont\jfont \else \bxnf@curr@font\bxnf@g@tmpa\tfont \unless\ifx\bxnf@g@tmpa\bxnf@dsf@tnull \chardef\bxnf@type=3 \let\bxnf@xfont\tfont \fi \fi \fi } \def\bxnf@get@tfm@info@init{% \bxDebug{\string\bxnf@get@tfm@info@init}% \begingroup \global\jfont\bxnf@jnullfont=rml\relax \global\tfont\bxnf@tnullfont=rmlv\relax \nullfont \bxnf@jnullfont \bxnf@tnullfont \bxnf@curr@font\bxnf@dsf@null\font \bxnf@curr@font\bxnf@dsf@jnull\jfont \bxnf@curr@font\bxnf@dsf@tnull\tfont \endgroup \bxDebug{null=\bxnf@dsf@null}% \bxDebug{jnull=\bxnf@dsf@jnull}% \bxDebug{tnull=\bxnf@dsf@tnull}% \global\let\bxnf@get@tfm@info@init\relax } \fi %---- %--------------------------------------- Switching of \newfont %% \bxnf@ltx@newfont % The original. \let\bxnf@ltx@newfont\newfont %%<*>\enhancenewfont \newcommand*\enhancenewfont{% \let\newfont\newfontx} %%<*>\noenhancenewfont \newcommand*\noenhancenewfont{% \let\newfont\bxnf@ltx@newfont} %% initial \ifbxnf@newfont \enhancenewfont \fi %--------------------------------------- all done \endinput %% EOF