Debian Codegraph @

texmf-dist/tex/plain/ifis-macros/ifisdimension.tex

raw
1 % This file is part of the CTAN package named ifis-macros.
2 %
3 % ifisdimension.tex: provide macro \ifisdim to check if a given
4 % input is a valid dimension for \TeX.
5 % Usage: \ifisdim <argument>\Boolend <true branch>\else <false branch>\fi
6 %
7 % Copyright (C) 2024,2025 Udo Wermuth (author)
8 %
9 % This program is free software: you can redistribute it and/or modify
10 % it under the terms of the GNU General Public License as published by
11 % the Free Software Foundation, either version 3 of the License, or
12 % (at your option) any later version.
13 %
14 % This program is distributed in the hope that it will be useful,
15 % but WITHOUT ANY WARRANTY; without even the implied warranty of
16 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 % GNU General Public License for more details.
18 %
19 % You should have received a copy of the GNU General Public License
20 % along with this program. If not, see <http://www.gnu.org/licenses/>.
21 %
22 \catcode`\@=11
23 \newif\ifII@itis %% reused from \ifisint %%
24 \def\II@rmsign #1{\ifx#1+\else\ifx#1-\else %%
25 \II@endrm#1\fi\fi\II@rmsign}% remove signs: %%
26 \def\II@endrm #1\fi\fi#2{\fi\fi#1}% `+' & `-' %%
27 \let\Boolend=\iffalse \font\II@font=cmr10 % %%
28 \let\IIcurrentmode=\errorstopmode % CONFIGURE %%
29 \def\II@W{W}\def\II@octW{'W}\def\II@hexW{"W}% %%
30 %% declarations
31 \newdimen\II@frac
32 \countdef\II@cnt=255 \dimendef\II@dim=255
33 %% helper macros; some use \ifisint's sentinel W
34 \def\II@bad #1#2#3#4#5#6\II@end{% numeric part
35 \def\II@id{#1W}% is missing but maybe with unit
36 \edef\II@X{#6}\ifx\II@X\empty
37 \edef\II@X{#5}\ifx\II@X\empty\else\II@Bad\fi
38 \else \edef\II@X{\II@mklc#2#3#4W}%
39 \ifx\II@X\II@rueW
40 \else\ifx\II@X\II@truW\II@Bad
41 \else \II@itistrue
42 \fi\fi\fi}
43 \def\II@rueW{rueW}\def\II@truW{truW}
44 \def\II@Bad{\ifx\II@id\II@W
45 \else\ifx\II@id\II@octW
46 \else\ifx\II@id\II@hexW
47 \else \II@itistrue
48 \fi\fi\fi}
49 \def\II@mklc #1{\if#1pp\else\if#1Pp\else
50 \if#1tt\else\if#1Tt\else
51 \if#1bb\else\if#1Bb\else
52 \if#1ss\else\if#1Ss\else
53 \if#1rr\else\if#1Rr\else
54 \if#1uu\else\if#1Uu\else
55 \if#1ee\else\if#1Ee\else
56 \II@endlc#1\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
57 \fi\fi\fi \II@mklc}% `W' and `m' stop \II@mklc
58 \def\II@endlc #1\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
59 \fi\fi\fi\fi#2{\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
60 \fi\fi\fi\fi#1}
61 \def\II@getfrac #1mm\II@end{\global\II@frac=0#1}
62 \def\II@getcalc{%\II@calc=coerced\II@int\II@frac
63 \ifdim\II@unit=26.11119pt % \II@unit is ``pt''
64 \II@dim=\ifnum\II@int<16384
65 \II@int\else 0\fi pt
66 \else\ifdim\II@unit=27.77786pt % it is ``bp''
67 \II@dim=\ifnum\II@int<16323
68 \II@int\else 0\fi bp
69 \else\ifdim\II@unit=26.16673pt % it is ``sp''
70 \II@dim=\ifnum\II@int<1073741824
71 \II@int\else 0\fi sp
72 \else \II@dim=0pt \fi\fi\fi
73 \II@cnt=\II@dim \advance\II@cnt by \II@frac
74 \edef\II@calc{\number\II@cnt}}
75 \def\II@point #1#2\II@end{% assign digits of the
76 \afterassignment\II@mklc % fraction to \II@cnt
77 \ifx#1.\II@cnt=0#2%
78 \else\ifx#1,\II@cnt=0#2%
79 \else \II@cnt=0#1#2%
80 \fi\fi}
81 \def\II@getunit #1{\afterassignment\II@hdlfrac
82 \II@cnt=#1\relax}
83 \def\II@rmtrue{\ifdim\wd0>40pt \the\II@dim
84 \else \the\wd0 \fi}
85 %% main macro
86 \def\ifisdim #1\Boolend{\II@itisfalse % S1.3
87 \edef\II@dist{\II@rmsign#1mm}% S1.1
88 \edef\II@dist{\expandafter\II@rmsign\II@dist}%
89 \expandafter\II@bad
90 \II@dist\empty\empty\empty\empty\II@end % S1.2
91 \ifII@itis % S4.1, S4.4
92 \wlog{=== start ignore}\batchmode\begingroup
93 \setbox0=\hbox{\II@font
94 \afterassignment\II@getfrac
95 \II@cnt=\II@dist\II@end % S2.2
96 \xdef\II@int{\the\II@cnt}}% S2.1
97 \setbox0=\hbox{\II@font
98 \afterassignment\II@point
99 \II@cnt=\II@dist\II@end}\II@dim=\wd0
100 \advance\II@dim by -17.80559pt % width `true'
101 \xdef\II@unit{\II@rmtrue}% S2.3
102 \setbox0=\hbox{\II@font\II@dim=#1mm% S3.1
103 \xdef\II@val{\ifdim\II@dim<0pt-\fi
104 \the\II@dim}}%
105 \xdef\II@wd{\the\wd0}%
106 \endgroup\IIcurrentmode\wlog{=== stop ignore}%
107 \ifdim\II@wd=16.66672pt % width ``mm'' S3.2
108 \ifdim\II@val=\maxdimen \II@getcalc
109 \ifnum\II@calc=1073741823 % S4.2
110 \else \II@itisfalse % S4.3
111 \fi\fi
112 \else \II@itisfalse % S3.3
113 \fi\fi \ifII@itis}
114 \catcode`\@=12