[Tex/LaTex] How to scale/resize a box in plain TeX

plain-texscaling

How can I resize (anamorphically) the contents of a box in plain TeX, that is, give it new width, height and depth?

\setbox0=\hbox{Hello!}
\copy0 % output at orig. size

%
% code which resizes box 0
% ... 
%

\box0  % output with altered dimensions
\bye

Best Answer

Just to get a hand on the problem, here's what one has to do with pdftex.

You can change the “current matrix” that tells pdftex how to resize the material it's going to print. Just save the current settings, change the matrix and restore.

\setbox0=\hbox{Abc}

X\copy0

\bigskip

X\pdfsave\pdfsetmatrix{0.87 -0.5 0.5 0.87}\rlap{\smash{\copy0}}\pdfrestore

\bigskip

X\box0

\bye

enter image description here

Do you see the problem? Between \pdfsave and \pdfrestore there should be no horizontal movement, so you need to compute the apparent dimension of the text you're transforming. Some trigonometry is needed in case of rotations, while scaling in either direction is easier. You'd use something like

\pdfsetmatrix{2 0 0 3}

for doubling in the x-direction and tripling in the y-direction and typeset an empty box with the apparent dimensions:

\setbox0=\hbox{Abc}

X\copy0 X

\bigskip

X\pdfsave\pdfsetmatrix{2 0 0 3}\rlap{\smash{\copy0}}\pdfrestore
{\setbox2=\hbox{}\wd2=2\wd0 \ht2=3\ht0 \dp2=3\dp0 \box2}X

\bigskip

X\box0 X

\bye

enter image description here

Note that proper space is reserved for the entire construction.

If you want to support also XeTeX, you'd have to use a completely different method, issuing suitable \special commands. Different \special commands would be needed for latex+dvips.