forked from leanprover/tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrimclip.sty
433 lines (433 loc) · 11.6 KB
/
trimclip.sty
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
% \begin{macrocode}
%<!COPYRIGHT>
\ProvidesPackage{trimclip}[%
%<!DATE>
%<!VERSION>
%<*DRIVER>
2099/01/01 develop
%</DRIVER>
Trim and clip general TeX material]
% \end{macrocode}
%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \subsection{Options}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{macrocode}
\def\tc@driver{tc-\Gin@driver}
\DeclareOption{pgf}{\def\tc@driver{tc-pgf.def}\AtEndOfPackage{\RequirePackage{pgf}}}
\DeclareOption{pdftex}{\def\tc@driver{tc-pdftex.def}\PassOptionsToPackage{pdftex}{graphicx}}
\DeclareOption{xetex}{\def\tc@driver{tc-xetex.def}\PassOptionsToPackage{xetex}{graphicx}}
\DeclareOption{dvips}{\def\tc@driver{tc-dvips.def}\PassOptionsToPackage{dvips}{graphicx}}
\DeclareOption{dvipdfm}{\def\tc@driver{tc-xetex.def}\PassOptionsToPackage{xetex}{graphicx}}
\DeclareOption{dvipdf}{\def\tc@driver{tc-xetex.def}\PassOptionsToPackage{xetex}{graphicx}}
\DeclareOption*{%
\@ifpackageloaded{graphics}{%
\edef\tc@driver{tc-\CurrentOption.def}%
\begingroup
\edef\@tempa{\CurrentOption.def}%
\ifx\@tempa\Gin@driver\else
\let\on@line\@gobble
\PackageWarning{trimclip}{%
A different clipping driver was requested than the\MessageBreak
one used for 'graphics/x'! This is not recommended\MessageBreak
and can lead to defect output files.%
}%
\fi
\endgroup
}{%
\def\tc@driver{tc-\Gin@driver}%
\PassOptionsToPackage\CurrentOption{graphicx}%
}%
}
\ProcessOptions*\relax
% \end{macrocode}
%
% \begin{macrocode}
\RequirePackage{graphicx}[1999/02/16]
\RequirePackage{collectbox}[2011/08/22]
\RequirePackage{adjcalc}
% \end{macrocode}
%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \subsection{User level and auxiliary macros}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \begin{macro}{\tc@readvalues}
% Reads the four values for trim, viewport and clip.
% Valid inputs are:
% 4 values directly,
% 1 value which is taken four times,
% 2 values which is taken for left/right and top/bottom.
% \begin{macrocode}
\def\tc@readvalues#1{%
\tc@@readvalues#1 {} {} {} \\%
}
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\tc@@readvalues}
% \begin{macrocode}
\def\tc@@readvalues#1 #2 #3 #4 #5\\{%
\adjsetlengthdefault\tc@llx{#1}%
\ifx\@nnil#2\@nnil
\tc@lly\tc@llx
\tc@urx\tc@llx
\tc@ury\tc@llx
\else
\adjsetlengthdefault\tc@lly{#2}%
\ifx\@nnil#3\@nnil
\tc@urx\tc@llx
\tc@ury\tc@lly
\else
\adjsetlengthdefault\tc@urx{#3}%
\adjsetlengthdefault\tc@ury{#4}%
\fi
\fi
}%
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\tc@llx}
% \begin{macro}{\tc@lly}
% \begin{macro}{\tc@urx}
% \begin{macro}{\tc@ury}
% Dimension registers for the four trim/viewport values.
% Legend: ll = lower left, ur = upper right.
% \begin{macrocode}
\newdimen\tc@llx
\newdimen\tc@lly
\newdimen\tc@urx
\newdimen\tc@ury
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\trimbox}
% \begin{macrocode}
\newcommand\trimbox{%
\collectboxcheckenv{trimbox}%
\@ifstar
\trimbox@s
\trimbox@
}
\def\trimbox@#1{%
\collectbox{\@trimclip\@trimbox{#1}}%
}
\def\trimbox@s#1{%
\collectbox{\@trimclip\@viewportbox{#1}}%
}
% \end{macrocode}
% \end{macro}
%
%
% \begin{environment}{trimbox*}
% \begin{macrocode}
\expandafter\newcommand\expandafter*\csname trimbox*\endcsname{%
\@collectboxisenv{trimbox*}%
\trimbox@s
}
% \end{macrocode}
% \end{environment}
%
%
% \begin{macro}{\clipbox}
% \begin{macrocode}
\newcommand\clipbox{%
\collectboxcheckenv{clipbox}%
\@ifstar
\clipbox@s
\clipbox@
}
\def\clipbox@#1{%
\collectbox{\@trimclip\@clipbox{#1}}%
}
\def\clipbox@s#1{%
\collectbox{\@trimclip\@clipvpbox{#1}}%
}
% \end{macrocode}
% \end{macro}
%
%
% \begin{environment}{clipbox*}
% \begin{macrocode}
\expandafter\newcommand\expandafter*\csname clipbox*\endcsname{%
\@collectboxisenv{clipbox*}%
\clipbox@s
}
% \end{macrocode}
% \end{environment}
%
%
% \begin{macro}{\marginbox}[1]{Margins as space separated values (like for 'trim')}
% Collect box first.
% \begin{macrocode}
\newcommand\marginbox{%
\collectboxcheckenv{marginbox}%
\@ifstar
\marginbox@s
\marginbox@
}
\def\marginbox@#1{%
\@collectbox{\@trimclip\@marginbox{#1}}%
}
\def\marginbox@s#1{%
\@collectbox{\@trimclip\@marginraisebox{#1}}%
}
% \end{macrocode}
% \end{macro}
%
%
% \begin{environment}{marginbox*}[1]{Margins as space separated values (like for 'trim')}
% \begin{macrocode}
\expandafter\newcommand\expandafter*\csname marginbox*\endcsname{%
\@collectboxisenv{marginbox*}%
\marginbox@s
}
% \end{macrocode}
% \end{environment}
%
%
% \begin{macro}{\@trimclip}[2]{<trim/viewport/clip macro>}{<values>}
% General macro which parses the values and feeds it to the given lower-level macro.
% Finally the box is typeset. This macro will always be used inside a group
% created by \Macro\@collectbox.
% \begin{macrocode}
\def\@trimclip#1#2{%
\tc@readvalues{#2}%
#1%
\collectedbox
\tc@llx
\tc@lly
\tc@urx
\tc@ury
\usebox\collectedbox
}
% \end{macrocode}
% \end{macro}
%
%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \subsection{Low-level commands}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% \begin{macro}{\tc@correctbaseline}
% Adjust baseline if required by negative depth or height.
% \begin{macrocode}
\def\tc@correctbaseline#1{%
% \end{macrocode}
% If depth is negative lower the box to get zero depth
% \begin{macrocode}
\ifdim\dp#1<\z@
\raise\dp#1%
\else
% \end{macrocode}
% Else if height is negative lower the box to get zero height
% \begin{macrocode}
\ifdim\ht#1<\z@
\lower\ht#1%
\fi\fi
\box#1%
}%
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\tc@correctdims}
% Ensure that all dimensions are non-negative.
% \begin{macrocode}
\def\tc@correctdims#1{%
\ifdim\dp#1<\z@ \dp#1=\z@ \fi
\ifdim\wd#1<\z@ \wd#1=\z@ \fi
\ifdim\ht#1<\z@ \ht#1=\z@ \fi
}
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@trimbox}[5]{<box register>}{<tllx>}{<tlly>}{<turx>}{<tury>}
% Removes the four length for the left, bottom, right and top from the official size of the box register.
% \begin{macrocode}
\def\@trimbox#1#2#3#4#5{%
\setbox#1=\hbox{%
%
\tc@llx=#2\relax
\tc@lly=#3\relax
\advance\tc@lly-\dp#1%
\tc@urx=#4\relax
\advance\tc@urx-\wd#1%
\tc@ury=#5\relax
\advance\tc@ury-\ht#1%
%
% Set dimensions now.
% This allows that the arguments can refer
% to the original dimensions without issues.
\hskip-\tc@llx
\dp#1-\tc@lly
\wd#1-\tc@urx
\ht#1-\tc@ury
%
\tc@correctbaseline{#1}%
}%
\tc@correctdims{#1}%
}
% \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\@marginbox}
% Adds the given margins to the depth, width and height.
% The left margin is created by an horizontal skip.
% This implementation assumes that the margins are positive and no special checks are added.
% While negative margins will trim some margin off, this will not lead to correct results if this amounts
% are larger than the existing dimensions. For this the \Macro\@trimbox macro should be used.
% \begin{macrocode}
\def\@marginbox#1#2#3#4#5{%
\setbox#1=\hbox{%
%
\tc@llx=#2\relax
\tc@lly=#3\relax
\advance\tc@lly\dp#1%
\tc@urx=#4\relax
\advance\tc@urx\wd#1%
\tc@ury=#5\relax
\advance\tc@ury\ht#1%
%
% Set dimensions now.
% This allows that the arguments can refer
% to the original dimensions without issues.
\hskip\tc@llx
\dp#1\tc@lly
\wd#1\tc@urx
\ht#1\tc@ury
%
\box#1%
}%
\tc@correctdims{#1}%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@marginraisebox}
% Like \Macro\@marginbox but raises the box accordant to the bottom margin,
% so that the original depth is kept.
% \begin{macrocode}
\def\@marginraisebox#1#2#3#4#5{%
\setbox#1=\hbox{%
%
\tc@llx=#2\relax
\tc@lly=#3\relax
\tc@urx=#4\relax
\advance\tc@urx\wd#1%
\tc@ury=#5\relax
\advance\tc@ury\ht#1%
%
% Set dimensions now.
% This allows that the arguments can refer
% to the original dimensions without issues.
\hskip\tc@llx
\wd#1\tc@urx
\ht#1\tc@ury
% Copy original tty values (ury is taken as temp dimension)
\tc@ury=\tc@lly
\advance\tc@lly\dp#1%
\dp#1\tc@lly
% Raise bu original tty value (now in ury)
\raise\tc@ury\box#1%
}%
\tc@correctdims{#1}%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@trimbox}[5]{<box register>}{<tllx>}{<tlly>}{<turx>}{<tury>}
% Removes the four length for the left, bottom, right and top from the official size of the box register.
% \begin{macrocode}
\def\@viewportbox#1#2#3#4#5{%
\setbox#1=\hbox{%
%
% Assign values
\tc@llx=#2\relax
\tc@lly=#3\relax
\tc@urx=#4\relax
\tc@ury=#5\relax
%
% Set dimensions now.
% This allows that the arguments can refer
% to the original dimensions without issues.
\hskip-\tc@llx
\dp#1-\tc@lly
\wd#1\tc@urx
\ht#1\tc@ury
%
\tc@correctbaseline{#1}%
}%
\tc@correctdims{#1}%
}
% \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@clipbox}
% Clips the box using the given trim amounts.
% For this the box is first trimmed and then clipped to its official size using a driver dependent macro.
% \begin{macrocode}
\def\@clipbox#1#2#3#4#5{%
\@trimbox{#1}{#2}{#3}{#4}{#5}%
\@cliptoboxdim{#1}%
}
% \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\@clipvpbox}
% Clips the box using the given trim amounts.
% For this the box is first trimmed and then clipped to its official size using a driver dependent macro.
% \begin{macrocode}
\def\@clipvpbox#1#2#3#4#5{%
\@viewportbox{#1}{#2}{#3}{#4}{#5}%
\@cliptoboxdim{#1}%
}
% \end{macrocode}
% \end{macro}
%
%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \subsection{Driver loading}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The clipping support is output driver dependent. The driver selected by \pkg{graphics} is used and a definition file
% is used if its exists. Otherwise either the default \texttt{pdftex} implementation or the \pkg{pgf} fall-back driver
% is used.
%
%
%
% \begin{macro}{\tc@bezfacn}
% The Bezier factor required to draw rounded corners in same drivers.
% \begin{macrocode}
\def\tc@bezfacn{0.44771525}%
% \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
\InputIfFileExists{\tc@driver}{%
{\let\on@line\@gobble
\PackageInfo{trimclip}{Using driver '\tc@driver'.}}%
}{%
\input{tc-pgf.def}%
{\let\on@line\@gobble
\PackageInfo{trimclip}{No clipping driver '\tc@driver' available.\MessageBreak Using fall-back PGF driver.}}%
}
% \end{macrocode}
%
%