Debian Codegraph @

texmf-dist/source/latex/wargame/hex/split.dtx

raw
1 % \iffalse
2 % --------------------------------------------------------------------
3 %<*hex>
4 % \fi
5 %
6 % \subsubsection{Board splitting}
7 % \label{sec:impl:hex:split}
8 %
9 % \begin{Macro}{\splitboard}
10 %
11 % Calculates how to split a board into sheets of paper.
12 %
13 % \begin{Syntax}
14 % \cs{splitboard}\oarg{options}
15 % \end{Syntax}
16 %
17 % where options are
18 %
19 % \begin{itemize}
20 % \item \spec{paper}=\meta{format}: Specifies the paper format. One
21 % of \spec{a4}, \spec{a3}, \spec{letter}, \spec{tabloid}. Default
22 % is \spec{a4}.
23 % \item \spec{landscape}: Sets the paper format to be in landscape
24 % mode (default is portrait).
25 % \item \spec{margin}=\meta{size in centimetres}: Size of margins on
26 % each sheet in centimetres \emph{without} unit. That is put
27 % \spec{0.6} for 6mm, \emph{not} \spec{6mm}. Default is \spec{0.6}.
28 % This should be \emph{slightly} larger (by roughly 5\%) than the
29 % \emph{least} margin required by the printer used. \emph{Must} be
30 % given \emph{before} \spec{paper} to have any effect.
31 % \item \spec{ncol}=\meta{number of columns}: Sets the number of
32 % columns of sheets.
33 % \item \spec{nrow}=\meta{number of rows}: Set the number of rows of
34 % sheets.
35 % \item \spec{overlap}=\meta{size in centimetres}: Sets the size of
36 % the overlap between sheets in centimetres \emph{without} unit.
37 % That is put \spec{2} for 2cm, \emph{not} \spec{2cm}. Default is
38 % \spec{2}.
39 % \item \spec{image}=\meta{file name}: File name of the board image
40 % (a PDF). Default is \spec{board}
41 % \item \spec{output}=\meta{file name}: File name (without
42 % \spec{.tex} ending) to write calculated split to.
43 % \item \spec{standalone}: Boolean flag. If true, then output file
44 % will be a standalone document (i.e., has a \cs{documentclass}).
45 % \item \spec{scale}=\meta{scale}: Set scale of board.
46 % \end{itemize}
47 %
48 % The macro will produce a file named \cs{jobname}\spec{\_out.tex}
49 % which can be included in another document to generate the split
50 % board PDF.
51 %
52 % To use, make, for example, the file \spec{calcsplit.tex} with the
53 % content
54 %
55 % \begin{verbatim}
56 % \documentclass[11pt]{standalone}
57 % \usepackage{wargame}
58 % \usepackage{mystyle}
59 % \begin{document}
60 % \splitboard{paper=letter,margin=.7,ncol=2,nrow=2,overlap=1}
61 % \end{document}
62 % \end{verbatim}
63 %
64 % to calculate the split of \spec{board.pdf} over $2\times2$ letter
65 % paper sheets, with a non-printable margin of 7mm, and an overlap
66 % between the segments of 1cm.
67 %
68 % The final split document can then be
69 %
70 % \begin{verbatim}
71 % \documentclass[11pt]{article}
72 % \usepackage[letterpaper,margin=7mm]{geometry}
73 % \begin{document}
74 % \input{calcsplit_out}
75 % \end{document}
76 % \end{verbatim}
77 %
78 %
79 % If you need to scale down the board, define the style \spec{board
80 % scale}. E.g.,
81 %
82 % \begin{verbatim}
83 % \tikzset{board scale/.style={scale=.9}}
84 % \end{verbatim}
85 %
86 % \end{Macro}
87 %
88 %
89 % Styles used for drawing things.
90 %
91 % \begin{macrocode}
92 \tikzset{%
93 % Margin must be <1cm
94 split/paper outline/.style={
95 shape=rectangle,
96 draw=red!50!black,
97 line width=.5mm},
98 split/effective outline/.style={
99 shape=rectangle,
100 draw=green!50!black,
101 dashed,
102 line width=.5mm},
103 split/board outline/.style={%
104 draw=magenta,
105 line width=.5mm,
106 dotted},
107 }
108 % \end{macrocode}
109 %
110 % A scratch dimension used
111 %
112 % \begin{macrocode}
113 \newdimen\split@tmp
114 % \end{macrocode}
115 %
116 % Get upper right and lower left corners of node. Argument is node name.
117 %
118 % \begin{macrocode}
119 \def\split@getem#1{%
120 \draw (#1.north east);%
121 \pgfgetlastxy{\split@ulx}{\split@uly}%
122 \xdef\split@ulx{\split@ulx}%
123 \xdef\split@ulx{\split@ulx}%
124 \draw (#1.south west);%
125 \pgfgetlastxy{\split@lrx}{\split@lry}%
126 \xdef\split@lrx{\split@lrx}%
127 \xdef\split@lry{\split@lry}%
128 }
129 % \end{macrocode}
130 %
131 % Get board dimensions. Argument is node name.
132 %
133 % \begin{macrocode}
134 \def\split@getboard#1{%
135 \split@getem{#1}%
136 \xdef\split@bulx{\split@ulx}%
137 \xdef\split@buly{\split@uly}%
138 \xdef\split@blrx{\split@lrx}%
139 \xdef\split@blry{\split@lry}%
140 \split@w{\@percentchar\space Board:
141 (\split@bulx,\split@buly)(\split@blrx,\split@blry)}}
142 % \end{macrocode}
143 %
144 % Adjust placement of markers and cut lines.
145 % \begin{enumerate}
146 % \item Dimension to adjust
147 % \item Overlap dimension (with units)
148 % \end{enumerate}
149 %
150 % \begin{macrocode}
151 \def\split@adj#1#2{%
152 \split@tmp=#2%
153 \divide\split@tmp by 2%
154 \advance\split@tmp by #1%
155 \edef\t{\the\split@tmp}}
156 % \end{macrocode}
157 %
158 % Get initial offset in a direction.
159 %
160 % \begin{enumerate}
161 % \item Number of segments in direction
162 % \item Overlap in centimetres (without unit)
163 % \item Effective size, in centimetres (without unit), of sheets in
164 % direction
165 % \item Full size, in centimetres (without unit), of board in
166 % direction.
167 % \end{enumerate}
168 %
169 % \begin{macrocode}
170 \def\split@get@init#1#2#3#4{%
171 \pgfmathparse{((#1 * #3 - (#1 - 1) * #2) - #4)/2}%
172 \xdef\split@off{\pgfmathresult}%
173 \hex@dbg{2}{((#1 * #3 - (#1 - 1) * #2) - #4)/2 -> `\split@off'}}
174 % \end{macrocode}
175 %
176 % Get initial offset of first segment.
177 %
178 % \begin{enumerate}
179 % \item Number of rows
180 % \item Number of columns
181 % \item Overlap in centimetres (without unit)
182 % \item Effective height, in centimetres (without unit), of sheets
183 % \item Effective width, in centimetres (without unit), of sheets
184 % \item Full height, in centimetres (without unit), of board
185 % \item Full width, in centimetres (without unit), of board
186 % \end{enumerate}
187 %
188 % \begin{macrocode}
189 \def\split@getinit#1#2#3#4#5#6#7{%
190 \split@get@init{#1}{#3}{#4}{#6}\xdef\dy{\split@off cm}
191 \split@get@init{#2}{#3}{#5}{#7}\xdef\dx{\split@off cm}}
192 % \end{macrocode}
193 %
194 % Get coordinates of a segment
195 %
196 % \begin{enumerate}
197 % \item Column number
198 % \item Row number
199 % \item Overlap, in centimetres (without unit)
200 % \end{enumerate}
201 %
202 % \begin{macrocode}
203 \def\split@getcoords#1#2#3{%
204 \hex@dbg{2}{Getting coords `c#1r#2'}%
205 \split@getem{c#1r#2}%
206 \edef\sulx{\split@ulx}%
207 \edef\suly{\split@uly}%
208 \edef\slrx{\split@lrx}%
209 \edef\slry{\split@lry}%
210 \edef\mlx{\split@blrx}%
211 \edef\mrx{\split@bulx}%
212 \edef\mty{\split@buly}%
213 \edef\mby{\split@blry}%
214 \pgfmathparse{int(#1-1)}\edef\pc{\pgfmathresult}%
215 \pgfmathparse{int(#2-1)}\edef\pr{\pgfmathresult}%
216 \pgfmathparse{int(#1+1)}\edef\nc{\pgfmathresult}%
217 \pgfmathparse{int(#2+1)}\edef\nr{\pgfmathresult}%
218 \pgfutil@ifundefined{pgf@sh@ns@c\pc r#2}{}{% Left
219 \hex@dbg{3}{\space Getting left `c\pc r#2'}%
220 \split@getem{c\pc r#2}\split@adj{\split@ulx}{-#3}\edef\mlx{\t}}%
221 \pgfutil@ifundefined{pgf@sh@ns@c\nc r#2}{}{% Right
222 \hex@dbg{3}{\space Getting right `c\nc r#2'}%
223 \split@getem{c\nc r#2}\split@adj{\split@lrx}{#3}\edef\mrx{\t}}%
224 \pgfutil@ifundefined{pgf@sh@ns@c#1r\pr}{}{% Above
225 \hex@dbg{3}{\space Getting above `c#1 r\pr'}%
226 \split@getem{c#1r\pr}\split@adj{\split@lry}{#3} \edef\mty{\t}}%
227 \pgfutil@ifundefined{pgf@sh@ns@c#1r\nr}{}{% Below
228 \hex@dbg{3}{\space Getting below `c#1 r\nr'}%
229 \split@getem{c#1r\nr}\split@adj{\split@uly}{-#3}\edef\mby{\t}}%
230 \draw[fill=red] (\mlx,\mty) circle(.2);%
231 \draw[fill=green](\mrx,\mty) circle(.4);%
232 \draw[fill=blue] (\mlx,\mby) circle(.6);%
233 \draw[fill=cyan] (\mrx,\mby) circle(.8);%
234 \split@w{%
235 \@percentchar^^J%
236 \string\segment(\sulx,\suly)(\slrx,\slry){\mlx}{\mrx}{\mby}{\mty}
237 \@percentchar\space c#1r#2}
238 }
239 % \end{macrocode}
240 %
241 % Stream to write to
242 %
243 % \begin{macrocode}
244 \newwrite\split@calcout
245 % \end{macrocode}
246 %
247 % Short-hand for write outs.
248 %
249 % \begin{macrocode}
250 \def\split@w{\immediate\write\split@calcout}
251 % \end{macrocode}
252 %
253 % Open stream and set-up
254 %
255 % \begin{macrocode}
256 \def\split@header#1{%
257 \immediate\openout\split@calcout=#1.tex
258 \ifsplit@standalone
259 \pgfmathparse{\split@margin*.95}\edef\tmp{\pgfmathresult}
260 \split@w{\@percentchar\@percentchar\space These are made with
261 `calcsplit' with `-jobname \jobname'}
262 \split@w{
263 ^^J\string\documentclass[twoside]{article}
264 ^^J\string\usepackage{geometry}
265 ^^J\string\geometry{papersize={\the\paperwidth,\the\paperheight},margin=\tmp cm}
266 ^^J\string\usepackage{wargame}
267 ^^J\string\setlength{\string\parindent}{0pt}
268 ^^J\string\setlength{\string\parskip}{0pt}
269 ^^J\string\begin{document}
270 ^^J\string\ignorespaces\@percentchar}
271 \fi
272 \split@w{\string\def\string\boardfile{\split@img}\@percentchar}
273 \split@w{\string\def\string\boardscale{\split@scale}\@percentchar}
274 }
275 % \end{macrocode}
276 %
277 % Write final stuff and close stream
278 %
279 % \begin{macrocode}
280 \def\split@footer{%
281 \ifsplit@standalone
282 \split@w{^^J\string\end{document}}
283 \fi
284 \split@w{^^J\@percentchar\@percentchar End of `\jobname'^^J}
285 \immediate\closeout\split@calcout
286 }
287 % \end{macrocode}
288 %
289 % Initial calculations. This draws the board and then extracts the
290 % dimensions of the board. It also defines some styles for drawing
291 % the board segments.
292 %
293 % \begin{macrocode}
294 \def\split@init#1{%
295 \node[scale=\split@scale,
296 inner sep=0pt,
297 outer sep=0pt,
298 anchor=north west,
299 transform shape](b){\includegraphics{#1}};
300 \split@getboard{b}
301 %x
302 \split@tmp=\split@blrx cm\advance\split@tmp by -\split@bulx%
303 \wg@pt@to@cm{\split@tmp}\edef\split@bw{\pgfmathresult}%
304 \pgfmathparse{abs(\split@bw)}\edef\split@bw{\pgfmathresult}%
305 %
306 \split@tmp=\split@buly cm\advance\split@tmp by -\split@blry%
307 \wg@pt@to@cm{\split@tmp}\edef\split@bh{\pgfmathresult}%
308 \pgfmathparse{abs(\split@bh)}\edef\split@bh{\pgfmathresult}%
309 %
310 \wg@pt@to@cm{\paperwidth}\edef\split@pw{\pgfmathresult}%
311 \wg@pt@to@cm{\paperheight}\edef\split@ph{\pgfmathresult}%
312 %
313 \wg@pt@to@cm{\textwidth}\edef\split@ew{\pgfmathresult}%
314 \wg@pt@to@cm{\textheight}\edef\split@eh{\pgfmathresult}%
315 %
316 \hex@dbg{1}{Board:
317 (\split@bulx,\split@buly)(\split@blrx,\split@blry) \split@bw x\split@bh
318 ^^JPaper: \split@pw x\split@ph
319 ^^JEffective: \split@ew x\split@eh
320 }
321 \tikzset{
322 split/paper size/.style={
323 shape=rectangle,
324 minimum width=\paperwidth,
325 minimum height=\paperheight,
326 split/paper outline,
327 },
328 split/effective size/.style={
329 shape=rectangle,
330 minimum width=\textwidth,
331 minimum height=\textheight,
332 split/effective outline},
333 split/board size/.style={
334 shape=rectangle,
335 minimum width=\split@bw cm,
336 minimum height=\split@bh cm,
337 split/board outline}}
338 \node[board/.try,split/board size,anchor=north west] {};
339 }
340 % \end{macrocode}
341 %
342 % Calculate effective sheet sizes from sheet dimensions and the
343 % defined margin.
344 %
345 % \begin{macrocode}
346 \def\split@text@dim#1{%
347 \textwidth=\paperwidth%
348 \textheight=\paperheight%
349 \advance\textwidth by -#1cm%
350 \advance\textwidth by -#1cm%
351 \advance\textheight by -#1cm%
352 \advance\textheight by -#1cm%
353 \global\textwidth=\textwidth%
354 \global\textheight=\textheight%
355 }
356 % \end{macrocode}
357 %
358 % Options for the \cs{splitboard} macro.
359 %
360 % \begin{macrocode}
361 \newif\ifsplit@standalone\split@standalonetrue
362 \tikzset{%
363 split/.search also={/tikz},%
364 split/.cd,%
365 margin/.store in=\split@margin,
366 paper/.is choice,%
367 paper/a4/.code={%
368 \hex@dbg{3}{A4 paper for split}%
369 \global\paperwidth=21cm%
370 \global\paperheight=29.7cm%
371 \split@text@dim{\split@margin}},
372 paper/a3/.code={%
373 \hex@dbg{3}{A3 paper for split}%
374 \global\paperheight=42cm%
375 \global\paperwidth=29.7cm%
376 \split@text@dim{\split@margin}},
377 paper/letter/.code={%
378 \hex@dbg{3}{Letter paper for split}
379 \paperheight=27.9cm,%
380 \paperwidth=21.6cm,%
381 \split@text@dim{\split@margin}},%
382 paper/tabloid/.code={%
383 \hex@dbg{3}{Tabloid paper for split}%
384 \paperheight=43.2cm,%
385 \paperwidth=27.9cm,%
386 \split@text@dim{\split@margin}},
387 landscape/.code={%
388 \hex@dbg{3}{Landscape option for split}
389 \split@tmp=\paperheight
390 \global\paperheight=\paperwidth
391 \global\paperwidth=\split@tmp
392 \split@tmp=\textheight
393 \global\textheight=\textwidth
394 \global\textwidth=\split@tmp},
395 standalone/.is if=split@standalone,
396 scale/.store in=\split@scale,
397 output/.store in=\split@out,
398 ncol/.store in=\split@ncol,
399 nrow/.store in=\split@nrow,
400 overlap/.store in=\split@ov, % Centimeter, no unit
401 image/.store in=\split@img,
402 paper/.default=a4, paper/.initial=a4,
403 margin/.default=.6, margin/.initial=.6,
404 ncol/.default=0, ncol/.initial=0,
405 nrow/.default=0, nrow/.initial=0,
406 overlap/.default=2, overlap/.initial=2,
407 image/.default=board, image/.initial=board,
408 output/.default=\jobname_out,
409 standalone/.default=true,
410 scale/.default=1,
411 }
412 % \end{macrocode}
413 %
414 % The actual macro. The argument is key-value pairs of options.
415 %
416 % \begin{macrocode}
417 \def\splitboard#1{%
418 \pgfkeys{/tikz/split/.cd,%
419 standalone,%
420 output,%
421 margin,%
422 paper,%
423 image,%
424 overlap,%
425 scale,%
426 ncol,%
427 nrow,%
428 #1}
429 \hex@dbg{1}{%
430 Paper: `\the\paperwidth'x`\the\paperheight'
431 ^^JEffective: `\the\textwidth'x`\the\textheight'
432 ^^JNcols: `\split@ncol'
433 ^^JNrows: `\split@nrow'
434 ^^JOverlap: `\split@ov' cm}
435 \split@header{\split@out}
436 \begin{tikzpicture}
437 \split@init{\split@img}
438 \split@getinit{%
439 \split@nrow}{%
440 \split@ncol}{%
441 \split@ov}{\split@eh}{\split@ew}{\split@bh}{\split@bw}
442 \node[split/effective size,
443 above left=\dy and \dx of b.north west,
444 anchor=north west] (c1r1) {};
445 \node[split/paper size] at (c1r1) {};
446 %
447 \foreach \r [remember=\r as \pr (initially 0)] in {1,...,\split@nrow}{%
448 \ifnum\r>1
449 \hex@dbg{3}{Placing first column of row `\r'}
450 \node[split/effective size,
451 below=-\split@ov cm of c1r\pr.south west,anchor=north west] (c1r\r){};
452 \node[split/paper size] at (c1r\r) {};
453 \fi
454 \foreach \c [remember=\c as \pc (initially 1)] in {2,...,\split@ncol}{%
455 \ifnum\c>\split@ncol\else%
456 \ifnum\c>\pc
457 \hex@dbg{3}{Placing column `\c' (`\pc') of row `\r'}
458 \node[split/effective size,
459 right=-\split@ov cm of c\pc r\r.north east,anchor=north west]
460 (c\c r\r) {};
461 \node[split/paper size] at (c\c r\r) {};
462 \fi
463 \fi
464 }
465 }
466 \foreach \r [remember=\r as \pr (initially 0)] in {1,...,\split@nrow}{%
467 \foreach \c [remember=\c as \pc (initially 0)] in {1,...,\split@ncol}{%
468 \split@getcoords{\c}{\r}{\split@ov cm}}}
469 \end{tikzpicture}
470 \split@footer
471 }
472 % \end{macrocode}
473 %
474 % Macro used by the written file.
475 %
476 %
477 % \begin{enumerate}
478 % \item first coordinate (e.g., \spec{(hex ak:c=C,r=17)})
479 % \item second coordinate (e.g., \spec{(hex ak:c=M,r=33)})
480 % \item Crop mark left
481 % \item Crop mark right
482 % \item Crop mark bottom
483 % \item Crop mark top
484 % \end{enumerate}
485 %
486 % \begin{macrocode}
487 \def\segment(#1)(#2)#3#4#5#6{%
488 \begin{tikzpicture}%
489 \begin{scope}
490 \clip (#1) rectangle (#2);
491 \node[scale=\boardscale,
492 inner sep=0pt,
493 outer sep=0pt,
494 anchor=north west,
495 transform shape]{\includegraphics{\boardfile}};
496 \end{scope}
497 \pgfinterruptboundingbox
498 \draw(#3,#6)--++( 0.0, 0.3);
499 \draw(#3,#6)--++(-0.3, 0.0);
500 \draw(#3,#5)--++( 0.0,-0.3);
501 \draw(#3,#5)--++(-0.3, 0.0);
502 \draw(#4,#6)--++( 0.0, 0.3);
503 \draw(#4,#6)--++( 0.3, 0.0);
504 \draw(#4,#5)--++( 0.0,-0.3);
505 \draw(#4,#5)--++( 0.3, 0.0);
506 \endpgfinterruptboundingbox
507 \end{tikzpicture}%
508 \cleardoublepage}%
509 % \end{macrocode}
510 %
511 %
512 %
513 %
514 % \iffalse
515 %</hex>
516 % --------------------------------------------------------------------
517 % \fi