| 1 |
% Copyright 1994 Michael John Downes |
| 2 |
% Copyright 2013 TeX Users Group |
| 3 |
% This file is part of the dialogl package, released under the LPPL; |
| 4 |
% see dialogl.ins for details. |
| 5 |
|
| 6 |
\newdimen\zdim \newdimen\tempdim \newdimen\tempdima |
| 7 |
|
| 8 |
% Conversion factors: |
| 9 |
% |
| 10 |
% According Scaled Rational Prime |
| 11 |
% Unit to TeX Points Form Factorization |
| 12 |
%----------------------------------------------------------------- |
| 13 |
% 1 sp 0.00002 pt 1 1/65536 pt 1 / 2^16 |
| 14 |
% 1 mm 2.84526 pt 186467 7227/2540 pt 3*3*11*73 / 2*2*5*127 |
| 15 |
% 1 cm 28.45274 pt 1864679 7227/254 pt 3*3*11*73 / 2*127 |
| 16 |
% 1 pt 1.0 pt 65536 100/7227 in 2*2*5*5 / 3*3*11*73 |
| 17 |
% 1 pc 12.0 pt 786432 12/1 pt |
| 18 |
% 1 dd 1.07 pt 70124 1238/1157 pt 2*619 / 13*89 |
| 19 |
% 1 cc 12.8401 pt 841489 14856/1157 pt 2*2*2*3*619 / 13*89 |
| 20 |
% 1 bp 1.00374 pt 65781 803/800 pt 11*73 / 2*2*2*2*2*5*5 |
| 21 |
% [1/72 in] |
| 22 |
% 1 in 72.2699 pt 4736286 7227/100 pt 3*3*11*73 / 2*2*5*5 |
| 23 |
% [2.54 cm] [254/100 cm] |
| 24 |
|
| 25 |
\def\points#1#2#3{\tempdim#2\relax |
| 26 |
\edef#3{\csname cnvunits#1\expandafter\endcsname\the\tempdim}% |
| 27 |
} |
| 28 |
|
| 29 |
\def\inches#1#2#3{% |
| 30 |
\tempdim=#2\relax |
| 31 |
\tempdima=\ifdim\tempdim<\zdim -\fi\tempdim % absolute value |
| 32 |
\roundup\tempdima{#1}{in}% |
| 33 |
% In the interest of maximum accuracy we push \tempdima as near |
| 34 |
% to \maxdimen as possible before dividing, using the prime |
| 35 |
% factorization of the fraction 7227/100 which is the |
| 36 |
% points/inches conversion factor. |
| 37 |
\ifdim\tempdima<.01\maxdimen |
| 38 |
\multiply\tempdima 100 \divide\tempdima 7227 |
| 39 |
\else |
| 40 |
\ifdim\tempdima<.1\maxdimen |
| 41 |
\multiply\tempdima 10 \divide\tempdima 11 |
| 42 |
\multiply\tempdima 10 \divide\tempdima 657 |
| 43 |
\else |
| 44 |
\divide\tempdima 9 \multiply\tempdima 5 |
| 45 |
\divide\tempdima 803 \multiply\tempdima 20 |
| 46 |
\fi |
| 47 |
\fi |
| 48 |
\tempdim=\ifdim\tempdim<\zdim -\fi \tempdima |
| 49 |
\edef#3{% |
| 50 |
\csname cnvunits#1\expandafter\endcsname\the\tempdim}% |
| 51 |
} |
| 52 |
|
| 53 |
% Function \roundup for rounding upward. #1 must be a dimension |
| 54 |
% register. If it holds a negative value it will be rounded |
| 55 |
% `outward' away from zero rather than `upward' toward zero. #3 |
| 56 |
% is a TeX units string such as "pt" or "in". If #2 = 0 then |
| 57 |
% this will round up to the nearest tenth; if #2 = 00, nearest |
| 58 |
% hundredth; and so forth (up to 5 zeros). If #2 is empty then |
| 59 |
% full accuracy up to TeX's limits will be used. |
| 60 |
% |
| 61 |
% The rounded result will be returned in the dimension register |
| 62 |
% #1. |
| 63 |
|
| 64 |
\def\roundup#1#2#3{% |
| 65 |
\if .#2.\else |
| 66 |
\begingroup |
| 67 |
\ifdim#1>\zdim |
| 68 |
\advance#1-\maxdimen \advance#1.#25#3\relax |
| 69 |
\fi |
| 70 |
\ifdim#1<\zdim |
| 71 |
\endgroup |
| 72 |
\advance#1.#25#3 |
| 73 |
\else |
| 74 |
\endgroup |
| 75 |
\fi |
| 76 |
\fi |
| 77 |
} |
| 78 |
|
| 79 |
\begingroup |
| 80 |
\catcode`\P=12 \catcode`\T=12 |
| 81 |
\lowercase{% |
| 82 |
\expandafter\gdef\csname cnvunits\endcsname#1PT{#1} |
| 83 |
\expandafter\gdef\csname cnvunits0\endcsname#1.#2PT{% |
| 84 |
#1.\takeone#20\takeone} |
| 85 |
\expandafter\gdef\csname cnvunits00\endcsname#1.#2PT{% |
| 86 |
#1.\taketwo#200\taketwo} |
| 87 |
\expandafter\gdef\csname cnvunits000\endcsname#1.#2PT{% |
| 88 |
#1.\takethree#2000\takethree} |
| 89 |
\expandafter\gdef\csname cnvunits0000\endcsname#1.#2PT{% |
| 90 |
#1.\takefour#20000\takefour} |
| 91 |
\expandafter\gdef\csname cnvunits00000\endcsname#1.#2PT{% |
| 92 |
#1.\takefive#200000\takefive} |
| 93 |
}% |
| 94 |
\endgroup |
| 95 |
|
| 96 |
\def\takeone#1#2\takeone{#1} |
| 97 |
\def\taketwo#1#2#3\taketwo{#1#2} |
| 98 |
\def\takethree#1#2#3#4\takethree{#1#2#3} |
| 99 |
\def\takefour#1#2#3#4#5\takefour{#1#2#3#4} |
| 100 |
\def\takefive#1#2#3#4#5#6\takefive{#1#2#3#4#5} |
| 101 |
|
| 102 |
\def\showinches#1{\inches{00}{#1}\converted |
| 103 |
\immediate\write16{% |
| 104 |
#1 = (after conversion) \converted\space inches}} |
| 105 |
|
| 106 |
\showinches{0in} \showinches{1in} \showinches{2.0in} |
| 107 |
\showinches{2.2in} \showinches{8.5in} \showinches{1pc} |
| 108 |
\showinches{6pc} \showinches{1cm} \showinches{1mm} |
| 109 |
\showinches{1bp} \showinches{72bp} \showinches{1cc} |
| 110 |
\showinches{1dd} \showinches{72dd} \showinches{5000pt} |
| 111 |
\showinches{-5000pt} \showinches{\maxdimen} |
| 112 |
\showinches{-\maxdimen} \showinches{.999\maxdimen} |
| 113 |
\showinches{1pt} \showinches{.01pt} |
| 114 |
|
| 115 |
\endinput |
| 116 |
|
| 117 |
% From the TeX log: |
| 118 |
|
| 119 |
0in = (after conversion) 0.00 inches |
| 120 |
1in = (after conversion) 1.00 inches |
| 121 |
2.0in = (after conversion) 2.00 inches |
| 122 |
2.2in = (after conversion) 2.20 inches |
| 123 |
8.5in = (after conversion) 8.50 inches |
| 124 |
1pc = (after conversion) 0.17 inches |
| 125 |
6pc = (after conversion) 1.00 inches |
| 126 |
1cm = (after conversion) 0.39 inches |
| 127 |
1mm = (after conversion) 0.04 inches |
| 128 |
1bp = (after conversion) 0.01 inches |
| 129 |
72bp = (after conversion) 1.00 inches |
| 130 |
1cc = (after conversion) 0.18 inches |
| 131 |
1dd = (after conversion) 0.01 inches |
| 132 |
72dd = (after conversion) 1.07 inches |
| 133 |
5000pt = (after conversion) 69.18 inches |
| 134 |
-5000pt = (after conversion) -69.18 inches |
| 135 |
\maxdimen = (after conversion) 226.70 inches |
| 136 |
-\maxdimen = (after conversion) -226.70 inches |
| 137 |
.999\maxdimen = (after conversion) 226.48 inches |
| 138 |
1pt = (after conversion) 0.01 inches |
| 139 |
.01pt = (after conversion) 0.00 inches |