sawine@11: % pgfsubpic.tex sawine@11: % Version 1.1, 25 Dec 2009 sawine@11: sawine@11: % Copyright 2009 by David Chiang sawine@11: sawine@11: % This program is free software; you can redistribute it and/or modify sawine@11: % it under the terms of the GNU General Public License as published by sawine@11: % the Free Software Foundation; either version 2 of the License, or sawine@11: % (at your option) any later version. sawine@11: sawine@11: % This program is distributed in the hope that it will be useful, sawine@11: % but WITHOUT ANY WARRANTY; without even the implied warranty of sawine@11: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the sawine@11: % GNU General Public License for more details. sawine@11: sawine@11: % You should have received a copy of the GNU General Public License along sawine@11: % with this program; if not, write to the Free Software Foundation, Inc., sawine@11: % 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. sawine@11: sawine@11: % New in version 1.1: sawine@11: % - the ability to save a subpicture in local variables sawine@11: % - nodes in subpictures are tracked if the subpicture is placed with arbitrary transforms sawine@11: % - new \pgffitsubpicture macro to transform a subpicture (preserving aspect) to fit in a desired box sawine@11: sawine@11: \newdimen\pgf@subpicminx sawine@11: \newdimen\pgf@subpicminy sawine@11: \newdimen\pgf@subpicmaxx sawine@11: \newdimen\pgf@subpicmaxy sawine@11: sawine@11: % Special virtual node for current subpicture's bounding box sawine@11: \expandafter\def\csname pgf@sh@ns@current subpicture\endcsname{rectangle} sawine@11: \expandafter\def\csname pgf@sh@np@current subpicture\endcsname{% sawine@11: \def\southwest{\pgfqpoint{\pgf@subpicminx}{\pgf@subpicminy}}% sawine@11: \def\northeast{\pgfqpoint{\pgf@subpicmaxx}{\pgf@subpicmaxy}}% sawine@11: } sawine@11: \expandafter\def\csname pgf@sh@nt@current subpicture\endcsname{{\pgf@pt@aa}{\pgf@pt@ab}{\pgf@pt@ba}{\pgf@pt@bb}{\the\pgf@pt@x}{\the\pgf@pt@y}} % the transformation at invocation time sawine@11: \expandafter\def\csname pgf@sh@pi@current subpicture\endcsname{\pgfpictureid} sawine@11: sawine@11: % Create a pgfpicture inside an hbox for delayed placement sawine@11: \def\pgfsubpicture{% sawine@11: \expandafter\global\expandafter\setbox\pgf@hbox=\hbox\bgroup sawine@11: \pgfinterruptpicture sawine@11: \pgfpicture sawine@11: \relax % not sure why. otherwise a curly brace immediately after causes an error sawine@11: } sawine@11: sawine@11: \def\endpgfsubpicture{ sawine@11: \global\pgf@subpicminx=\pgf@picminx sawine@11: \global\pgf@subpicminy=\pgf@picminy sawine@11: \global\pgf@subpicmaxx=\pgf@picmaxx sawine@11: \global\pgf@subpicmaxy=\pgf@picmaxy sawine@11: \global\edef\subpictureid{\pgfpictureid}% sawine@11: \pgfsetbaseline{\pgf@picminy}% sawine@11: \endpgfpicture% sawine@11: \endpgfinterruptpicture% sawine@11: \egroup sawine@11: } sawine@11: sawine@11: % Allocate registers for saving a subpicture. #1 is text, not a control sequence. sawine@11: \def\pgfnewsubpicture#1{% sawine@11: \expandafter\newbox\csname pgf@subpic@hbox@#1\endcsname sawine@11: \expandafter\newdimen\csname pgf@subpic@minx@#1\endcsname sawine@11: \expandafter\newdimen\csname pgf@subpic@miny@#1\endcsname sawine@11: \expandafter\newdimen\csname pgf@subpic@maxx@#1\endcsname sawine@11: \expandafter\newdimen\csname pgf@subpic@maxy@#1\endcsname sawine@11: } sawine@11: sawine@11: % saved subpictures are local to the current group sawine@11: \def\pgfsavesubpicture#1{% sawine@11: \expandafter\setbox\csname pgf@subpic@hbox@#1\endcsname\box\pgf@hbox sawine@11: \csname pgf@subpic@minx@#1\endcsname\pgf@subpicminx sawine@11: \csname pgf@subpic@miny@#1\endcsname\pgf@subpicminy sawine@11: \csname pgf@subpic@maxx@#1\endcsname\pgf@subpicmaxx sawine@11: \csname pgf@subpic@maxy@#1\endcsname\pgf@subpicmaxy sawine@11: \expandafter\edef\csname pgf@subpic@id@#1\endcsname{\subpictureid}% sawine@11: } sawine@11: sawine@11: % place current subpicture into named subpicture sawine@11: \def\pgfmergesubpicture#1{% sawine@11: \begin{pgfsubpicture} sawine@11: % place current subpicture sawine@11: \pgfplacesubpicture sawine@11: % override containing picture sawine@11: \expandafter\xdef\csname pgf@sh@pi@\subpictureid\endcsname{\csname pgf@subpic@id@#1\endcsname}% sawine@11: % copy contents of #1 sawine@11: \pgfrestoresubpicture{#1} sawine@11: \pgflowlevelobj{\pgftransformshift{\pgfqpoint{\the\pgf@subpicminx}{\the\pgf@subpicminy}}}{\pgfqbox\pgf@hbox} sawine@11: \pgfpathrectanglecorners{\pgfqpoint{\the\pgf@subpicminx}{\the\pgf@subpicminy}}{\pgfqpoint{\the\pgf@subpicmaxx}{\the\pgf@subpicmaxy}}% sawine@11: \pgfusepath{use as bounding box}% sawine@11: % sawine@11: \end{pgfsubpicture} sawine@11: \expandafter\setbox\csname pgf@subpic@hbox@#1\endcsname\box\pgf@hbox sawine@11: \csname pgf@subpic@minx@#1\endcsname\pgf@subpicminx sawine@11: \csname pgf@subpic@miny@#1\endcsname\pgf@subpicminy sawine@11: \csname pgf@subpic@maxx@#1\endcsname\pgf@subpicmaxx sawine@11: \csname pgf@subpic@maxy@#1\endcsname\pgf@subpicmaxy sawine@11: % but don't save the new picture id, keep the existing one sawine@11: } sawine@11: sawine@11: \def\pgfrestoresubpicture#1{% sawine@11: \edef\act{\global\noexpand\setbox\pgf@hbox\noexpand\box\csname pgf@subpic@hbox@#1\endcsname}\act sawine@11: \expandafter\global\expandafter\pgf@subpicminx\csname pgf@subpic@minx@#1\endcsname sawine@11: \expandafter\global\expandafter\pgf@subpicminy\csname pgf@subpic@miny@#1\endcsname sawine@11: \expandafter\global\expandafter\pgf@subpicmaxx\csname pgf@subpic@maxx@#1\endcsname sawine@11: \expandafter\global\expandafter\pgf@subpicmaxy\csname pgf@subpic@maxy@#1\endcsname sawine@11: \xdef\subpictureid{\csname pgf@subpic@id@#1\endcsname}% sawine@11: } sawine@11: sawine@11: % Place a previously-created subpicture, lining up its origin with the current origin sawine@11: \def\pgfplacesubpicture{ sawine@11: \pgfscope sawine@11: % expand current bounding box to accommodate subpicture sawine@11: \pgfpathrectanglecorners{\pgfqpoint{\the\pgf@subpicminx}{\the\pgf@subpicminy}}{\pgfqpoint{\the\pgf@subpicmaxx}{\the\pgf@subpicmaxy}}% sawine@11: \pgfusepath{use as bounding box}% sawine@11: % sawine@11: % make the subpicture a node in the containing picture sawine@11: \expandafter\gdef\csname pgf@sh@ns@\subpictureid\endcsname{rectangle}% sawine@11: \expandafter\xdef\csname pgf@sh@np@\subpictureid\endcsname{% sawine@11: \noexpand\def\noexpand\southwest{\noexpand\pgfqpoint{\the\pgf@subpicminx}{\the\pgf@subpicminy}}% sawine@11: \noexpand\def\noexpand\northeast{\noexpand\pgfqpoint{\the\pgf@subpicmaxx}{\the\pgf@subpicmaxy}}% sawine@11: }% sawine@11: \pgfgettransform\pgf@temp sawine@11: \expandafter\xdef\csname pgf@sh@nt@\subpictureid\endcsname{\pgf@temp}% sawine@11: \expandafter\xdef\csname pgf@sh@pi@\subpictureid\endcsname{\pgfpictureid}% sawine@11: % sawine@11: % align origin of subpicture with origin sawine@11: \pgftransformshift{\pgfqpoint{\the\pgf@subpicminx}{\the\pgf@subpicminy}}% sawine@11: \pgfqboxsynced{\pgf@hbox}% sawine@11: \endpgfscope sawine@11: } sawine@11: sawine@11: % Hook onto existing macro \pgf@shape@interpictureshift. sawine@11: % This is called whenever we look up an anchor of a node. sawine@11: % This hook recursively checks to see if the node's picture sawine@11: % is a subpicture of another, and if so, adjusts its position accordingly. sawine@11: sawine@11: % This is slow. It makes drawing trees O(n^2) in the depth of the tree. sawine@11: % The alternative is to store, for each picture, a list of the nodes sawine@11: % inside it. But this way doesn't require us to hijack \pgfnode, and sawine@11: % is robust to re-placement of a subpicture. A compromise would be sawine@11: % to store, for each picture, a list of the *subpictures* inside it. sawine@11: sawine@11: \let\orig@pgf@shape@interpictureshift\pgf@shape@interpictureshift sawine@11: \def\unwind@subpic#1{% sawine@11: % is #1 the current picture? sawine@11: \edef\subpicid{#1}% sawine@11: \ifx\subpicid\pgfpictureid sawine@11: % yes, we're done sawine@11: \else sawine@11: % does #1 have a parent picture? sawine@11: \expandafter\ifx\csname pgf@sh@pi@#1\endcsname\relax sawine@11: % no, the original node was not inside the current picture sawine@11: \fallback sawine@11: \else sawine@11: % yes, apply transform and move up to parent picture sawine@11: {% sawine@11: \pgfsettransform{\csname pgf@sh@nt@#1\endcsname}% sawine@11: \pgf@pos@transform{\pgf@x}{\pgf@y}% sawine@11: \global\pgf@x=\pgf@x sawine@11: \global\pgf@y=\pgf@y sawine@11: }% sawine@11: \unwind@subpic{\csname pgf@sh@pi@#1\endcsname}% sawine@11: \fi sawine@11: \fi sawine@11: } sawine@11: \def\pgf@shape@interpictureshift#1{% sawine@11: \edef\fallback{\pgf@x=\the\pgf@x\pgf@y=\the\pgf@y\noexpand\orig@pgf@shape@interpictureshift{#1}}% sawine@11: \unwind@subpic{\csname pgf@sh@pi@#1\endcsname}% sawine@11: } sawine@11: sawine@11: % \pgffitsubpicture{sw}{ne} sawine@11: % Make the subpicture fit in the rectangle from sw to ne, preserving its aspect ratio. sawine@11: \def\pgffitsubpicture#1#2{% sawine@11: % current size sawine@11: \pgfpointdiff{\pgfpointanchor{current subpicture}{south west}}{\pgfpointanchor{current subpicture}{north east}}% sawine@11: \pgf@xa=\pgf@x \pgf@ya=\pgf@y sawine@11: % desired size sawine@11: \pgf@process{\pgfpointdiff{#1}{#2}}% sawine@11: \pgf@xb=\pgf@x \pgf@yb=\pgf@y sawine@11: \pgfmathparse{min(\pgf@xb/\pgf@xa,\pgf@yb/\pgf@ya)}% sawine@11: \pgfmathparse{min(1,\pgfmathresult)}% sawine@11: \pgftransformscale{\pgfmathresult}% sawine@11: % sawine@11: % current position sawine@11: \pgfpointanchor{current subpicture}{center}% sawine@11: \pgf@xa=\pgf@x \pgf@ya=\pgf@y sawine@11: % desired position sawine@11: % we scaled transform, so apply reverse scaling to argument sawine@11: \pgfmathparse{1/\pgfmathresult}% sawine@11: \pgf@process{\pgfpointscale{\pgfmathresult}{\pgfpointlineattime{0.5}{#1}{#2}}}% sawine@11: \pgf@xb=\pgf@x \pgf@yb=\pgf@y sawine@11: \pgfpointdiff{\pgfpoint{\pgf@xa}{\pgf@ya}}{\pgfpoint{\pgf@xb}{\pgf@yb}}% sawine@11: \pgftransformshift{\pgfpoint{\pgf@x}{\pgf@y}}% sawine@11: } sawine@11: sawine@11: % utility functions -- not currently used sawine@11: sawine@11: \def\pgfnodedelete#1{% sawine@11: \expandafter\global\expandafter\let\csname pgf@sh@ns@#1\endcsname\relax sawine@11: \expandafter\global\expandafter\let\csname pgf@sh@np@#1\endcsname\relax sawine@11: \expandafter\global\expandafter\let\csname pgf@sh@nt@#1\endcsname\relax sawine@11: \expandafter\global\expandafter\let\csname pgf@sh@pi@#1\endcsname\relax sawine@11: \expandafter\global\expandafter\let\csname pgf@sh@ma@#1\endcsname\relax sawine@11: } sawine@11: sawine@11: \def\pgfnodeifexists#1#2#3{% sawine@11: \expandafter\ifx\csname pgf@sh@ns@#1\endcsname\relax#3\else#2\fi sawine@11: } sawine@11: