summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/wargame/source/util/bb.dtx
blob: 83e72da7a9622beb8b204f1d6f2abe0f726c6c30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
% \iffalse
% --------------------------------------------------------------------
%<*utils>
% \fi
% 
% \subsubsection{Bounding boxes}
% 
%  Bounding box dimensions
%
%
%    \begin{macrocode}
\newdimen\wg@bb@minx
\newdimen\wg@bb@miny
\newdimen\wg@bb@maxx
\newdimen\wg@bb@maxy
%    \end{macrocode}
%
%
%
% Enable or disable bounding box tracking 
%
%    \begin{macrocode}
\newif\ifwg@notrelevantforpathsize\wg@notrelevantforpathsizefalse
%    \end{macrocode}
% 
%
% \begin{Macro}{wg@resetbb}
% Reset the bounding box tracking dimensions
%
% \begin{macrocode}
\def\wg@resetbb{%
  \global\wg@bb@minx=16000pt\relax%
  \global\wg@bb@miny=16000pt\relax%
  \global\wg@bb@maxx=-16000pt\relax%
  \global\wg@bb@maxy=-16000pt\relax%
}
%    \end{macrocode}
% \end{Macro}
%
% \begin{Macro}{\old@pgf@protocolsize}
%   Save PGF's bounding box algorithm
%   
%    \begin{macrocode}
\let\old@pgf@protocolsize\pgf@protocolsizes
%    \end{macrocode}
% \end{Macro}
%
% \begin{Macro}{\wg@protocolsizes}
%   Our bounding box algorithm
%
%    \begin{macrocode}
\def\wg@protocolsizes#1#2{%
  \old@pgf@protocolsize{#1}{#2}
  \ifwg@notrelevantforpathsize\else%
  \ifdim#1<\wg@bb@minx\global\wg@bb@minx#1\fi%
  \ifdim#1>\wg@bb@maxx\global\wg@bb@maxx#1\fi%
  \ifdim#2<\wg@bb@miny\global\wg@bb@miny#2\fi%
  \ifdim#2>\wg@bb@maxy\global\wg@bb@maxy#2\fi%
  \fi
}
%    \end{macrocode}
% \end{Macro}
%
% % \begin{environment}{getbbl}
%   Environment that tracks the local bounding box
%
%    \begin{macrocode}
\newenvironment{getbbl}{%
  \wg@resetbb%
  \wg@notrelevantforpathsizefalse%
  \global\let\pgf@protocolsizes\wg@protocolsizes}{%
  \gdef\pgf@sh@ns@L{rectangle}
  \gdef\pgf@sh@np@L{%
    \def\southwest{\pgfqpoint{\the\wg@bb@minx}{\the\wg@bb@miny}}%
    \def\northeast{\pgfqpoint{\the\wg@bb@maxx}{\the\wg@bb@maxy}}%
  }
  \gdef\pgf@sh@nt@L{{1}{0}{0}{1}{0pt}{0pt}}
  \gdef\pgf@sh@pi@L{\pgfpictureid}
  \global\let\pgf@protocolsizes\old@pgf@protocolsize
}
%    \end{macrocode}
% \end{environment}
% 
% \begin{environment}{getbb}
%   Environment to track global bounding box
%   
%    \begin{macrocode}
\newenvironment{getbb}{%
  \wg@resetbb%
  \wg@notrelevantforpathsizefalse%
  \global\let\pgf@protocolsizes\wg@protocolsizes}{%
  \gdef\pgf@sh@ns@M{rectangle}
  \gdef\pgf@sh@np@M{%
    \def\southwest{\pgfqpoint{\the\wg@bb@minx}{\the\wg@bb@miny}}%
    \def\northeast{\pgfqpoint{\the\wg@bb@maxx}{\the\wg@bb@maxy}}%
  }
  \gdef\pgf@sh@nt@M{{1}{0}{0}{1}{0pt}{0pt}}
  % \pgfgettransform\pgf@temp%
  % \xdef\pgf@sh@nt@M{\pgf@temp}
  % \pgfgettransformentries{\wg@tmp@a}{\wg@tmp@b}{\wg@tmp@c}{\wg@tmp@d}{\pgf@temp}{\pgf@temp}
  % \message{^^JTransform of M: \meaning\pgf@temp}
  % \xdef\pgf@sh@nt@M{{\wg@tmp@a}{\wg@tmp@b}{\wg@tmp@c}{\wg@tmp@d}{0pt}{0pt}}%
  % \message{^^JTransform of M: \meaning\pgf@sh@nt@M}
  \gdef\pgf@sh@pi@M{\pgfpictureid}
  \global\let\pgf@protocolsizes\old@pgf@protocolsize
}
%    \end{macrocode}
% \end{environment}
% 
% \iffalse
% --------------------------------------------------------------------
% \fi
% 
% \subsubsection{Some utilities to get bounding boxes and the like}
%
%
% All coordinates, and such are recorded in centimetres.  It is worth
% remembering that the Tikz coordinate system has the $y$ axis point
% upward, while typical image software has the $y$ axis point down.
% \texttt{pdftocairo} typically assumes a 150 PPI (pixels-per-inch)
% resolution.
%
% That means that scaling factor becomes
%
% $$
% \frac{150\mathrm{pixel}}{2.54\mathrm{cm}} =
% 59.055\frac{\mathrm{pixel}}{\mathrm{cm}}
% $$
% 
% \iffalse Using definition in terms of printers feet - the one to
% use!
% 
% PNG: 1674 x 1101
% PDF:   "lower left": [-0.02107,-0.02107],
%        "upper right": [28.31705,18.60843]
% Width: 28.31705+0.02107 = 28.33812
% Height: 18.60843+0.02107 = 18.62950
% Pixel / cm: 1674 / 28.33812 = 59.07237318495369488166
%             1101 / 18.62950 = 59.09981480984460130438
%             Average         = 59.08609399739914809302
%          
% Calculated = 150 / 2.54 = 59.05511811023622047244
%
% Using 1/72.27
% 
% "lower left": [-0.02109,-0.02109],
%  "upper right": [28.321,18.61102]
%  Width: 28.321+0.02109=28.34209
%  Height: 18.61102+0.02109=18.63211
% Pixel / cm: 1674 / 28.34209 = 59.06409866033168337267
%             1101 / 18.63211 = 59.09153606328000425072
%             Average         = 59.07781736180584381169
% \fi
% 
% Since we want to write all dimensions in centimetres, we need to be
% able to convert \texttt{pt} dimensions to centimetres.  We make two
% macros to do that for us.
%
% The exact definition of 1pt is
%
% $$1\,\mathrm{pt} = \frac{249}{250}12"\frac{1}{864}=\frac{83}{6000}1"
% = 0.03513\overline{6}$$ 
% 
%    \begin{macrocode}
% 2.54 / 72.27 = .03514598035145980351
% \def\wg@pt@to@cm#1{\pgfmathparse{#1 * 0.0351460}}
\def\wg@pt@to@cm#1{\pgfmathparse{#1 * 0.0351367}}
\def\ptpoint@to@cm#1#2{%
  \wg@pt@to@cm{#1}\edef\x{\pgfmathresult}%
  \wg@pt@to@cm{#2}\edef\y{\pgfmathresult}}
%    \end{macrocode}
%
% The next macro gets an anchors coordinates and stores them (in units
% of centimetres) in \cs{tmp@x} and \cs{tmp@y}
%
%    \begin{macrocode}
\def\wg@get@nchor#1#2{%
  \wg@dbg{2}{Get anchor coordinates #1.#2}
  \pgfpointanchor{#1}{#2}%
  \wg@dbg{2}{ `\the\pgf@x',`\the\pgf@y'}
  \pgfgetlastxy\tmp@x\tmp@y%
  \wg@dbg{2}{ `\tmp@x',`\tmp@y'}
  \wg@pt@to@cm{\tmp@x}\edef\tmp@x{\pgfmathresult}
  \wg@pt@to@cm{\tmp@y}\edef\tmp@y{\pgfmathresult}
}
%    \end{macrocode}
%
% This does the same as above, but transform to the global coordinate
% system.
% 
%    \begin{macrocode}
\def\wg@get@global@nchor#1#2{%
  \pgfpointanchor{#1}{#2}%
  \pgfgetlastxy\tmp@x\tmp@y%
  \pgfpointtransformed{\pgfpoint{\tmp@x}{\tmp@y}}
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  %% \message{^^JAnchor #1.#2 @ (\the\pgf@xa,\the\pgf@ya)}
  \wg@pt@to@cm{\the\pgf@xa}\edef\tmp@x{\pgfmathresult}
  \wg@pt@to@cm{\the\pgf@ya}\edef\tmp@y{\pgfmathresult}
}
%    \end{macrocode}
%
% This records the bounding box given by a named node.  The result is
% stored in the macros \cs{llx}, \cs{lly}, \cs{urx}, and \cs{ury}. 
% 
%    \begin{macrocode} 
\def\wg@get@bb#1{%
  \wg@get@nchor{#1}{south west}
  \edef\llx{\tmp@x}
  \edef\lly{\tmp@y}
  \wg@get@nchor{#1}{north east}
  \edef\urx{\tmp@x}
  \edef\ury{\tmp@y}
}
\def\wglogbb#1{%
  \wg@get@bb{#1}%
  \message{^^J`#1' BB: (\llx,\lly) x (\urx,\ury)^^J}} 
%    \end{macrocode}
%
%
% 
% \iffalse
% --------------------------------------------------------------------
%</utils>
%\fi