Debian Codegraph @

texmf-dist/source/latex/l3experimental/l3draw/l3draw-scopes.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3draw-scopes.dtx
4 %
5 % Copyright (C) 2018-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % http://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3experimental bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \RequirePackage{expl3}
27 \documentclass[full]{l3doc}
28 \begin{document}
29 \DocInput{\jobname.dtx}
30 \end{document}
31 %</driver>
32 % \fi
33 %
34 % \title{^^A
35 % The \pkg{l3draw} package\\ Drawing scopes^^A
36 % }
37 %
38 % \author{^^A
39 % The \LaTeX{} Project\thanks
40 % {^^A
41 % E-mail:
42 % \href{mailto:[email protected]}
43 % {[email protected]}^^A
44 % }^^A
45 % }
46 %
47 % \date{Released 2026-05-26}
48 %
49 % \maketitle
50 %
51 % \begin{implementation}
52 %
53 % \section{\pkg{l3draw-scopes} implementation}
54 %
55 % \begin{macrocode}
56 %<*package>
57 % \end{macrocode}
58 %
59 % \begin{macrocode}
60 %<@@=draw>
61 % \end{macrocode}
62 %
63 % This sub-module covers more-or-less the same ideas as
64 % \texttt{pgfcorescopes.code.tex}. At present, equivalents of the
65 % following are currently absent:
66 % \begin{itemize}
67 % \item \cs{pgftext}: This is covered at this level by the coffin-based
68 % interface \cs{draw_coffin_use:Nnn}
69 % \end{itemize}
70 %
71 % \subsection{Drawing environment}
72 %
73 % \begin{variable}
74 % {\g_draw_bb_xmax_dim, \g_draw_bb_xmin_dim, \g_draw_bb_ymax_dim, \g_draw_bb_ymin_dim}
75 % Used to track the overall (official) size of the image created: may
76 % not actually be the natural size of the content.
77 % \begin{macrocode}
78 \dim_new:N \g_draw_bb_xmax_dim
79 \dim_new:N \g_draw_bb_xmin_dim
80 \dim_new:N \g_draw_bb_ymax_dim
81 \dim_new:N \g_draw_bb_ymin_dim
82 % \end{macrocode}
83 % \end{variable}
84 %
85 % \begin{variable}{\l_draw_bb_update_bool}
86 % Flag to indicate that a path (or similar) should update the bounding box
87 % of the drawing.
88 % \begin{macrocode}
89 \bool_new:N \l_draw_bb_update_bool
90 % \end{macrocode}
91 % \end{variable}
92 %
93 % \begin{variable}{\l_@@_layer_main_box, \l_@@_layer_main_box}
94 % Box for setting the drawing itself and the top-level layer.
95 % \begin{macrocode}
96 \box_new:N \l_@@_main_box
97 \box_new:N \l_@@_layer_main_box
98 % \end{macrocode}
99 % \end{variable}
100 %
101 % \begin{variable}{\g_draw_id_int}
102 % The drawing number.
103 % \begin{macrocode}
104 \int_new:N \g_draw_id_int
105 % \end{macrocode}
106 % \end{variable}
107 %
108 % \begin{macro}{\@@_reset_bb:}
109 % A simple auxiliary.
110 % \begin{macrocode}
111 \cs_new_protected:Npn \@@_reset_bb:
112 {
113 \dim_gset:Nn \g_draw_bb_xmax_dim { -\c_max_dim }
114 \dim_gset:Nn \g_draw_bb_xmin_dim { \c_max_dim }
115 \dim_gset:Nn \g_draw_bb_ymax_dim { -\c_max_dim }
116 \dim_gset:Nn \g_draw_bb_ymin_dim { \c_max_dim }
117 }
118 % \end{macrocode}
119 % \end{macro}
120 %
121 % \begin{macro}{\draw_begin:, \draw_end:}
122 % Drawings are created by setting them into a box, then adjusting the box
123 % before inserting into the surroundings. Color is set here using the drawing
124 % mechanism largely as it then sets up the internal data structures. It may be
125 % that a coffin construct is better here in the longer term: that may become
126 % clearer as the code is completed. As we need to avoid any insertion of
127 % baseline skips, the outer box here has to be an |hbox|. To allow for
128 % layers, there is some box nesting: notice that we
129 % \begin{macrocode}
130 \cs_new_protected:Npn \draw_begin:
131 {
132 \group_begin:
133 \int_gincr:N \g_draw_id_int
134 \hbox_set:Nw \l_@@_main_box
135 \@@_backend_begin:
136 \@@_reset_bb:
137 \@@_path_reset_limits:
138 \bool_set_true:N \l_draw_bb_update_bool
139 \draw_transform_matrix_reset:
140 \draw_transform_shift_reset:
141 \@@_softpath_clear:
142 \draw_set_linewidth:n { \l_draw_default_linewidth_dim }
143 \color_ensure_current:
144 \draw_set_nonzero_rule:
145 \draw_set_cap_butt:
146 \draw_set_join_miter:
147 \draw_set_miterlimit:n { 10 }
148 \draw_set_dash_pattern:nn { } { 0cm }
149 \hbox_set:Nw \l_@@_layer_main_box
150 \@@_record_origin:
151 }
152 \cs_new_protected:Npn \draw_end:
153 {
154 \@@_baseline_finalize:w
155 \exp_args:NNNV \hbox_set_end:
156 \clist_set:Nn \l_draw_layers_clist \l_draw_layers_clist
157 \@@_layers_insert:
158 \@@_backend_end:
159 \hbox_set_end:
160 \dim_compare:nNnT \g_draw_bb_xmin_dim = \c_max_dim
161 {
162 \dim_gzero:N \g_draw_bb_xmax_dim
163 \dim_gzero:N \g_draw_bb_xmin_dim
164 \dim_gzero:N \g_draw_bb_ymax_dim
165 \dim_gzero:N \g_draw_bb_ymin_dim
166 }
167 \@@_finalize:
168 \box_set_wd:Nn \l_@@_main_box
169 { \g_draw_bb_xmax_dim - \g_draw_bb_xmin_dim }
170 \mode_leave_vertical:
171 \box_use_drop:N \l_@@_main_box
172 \group_end:
173 }
174 % \end{macrocode}
175 % \end{macro}
176 %
177 % \begin{macro}{\@@_record_origin:}
178 % Used to log the absolute location of a drawing. Ideally this would not
179 % need two \tn{savepos}: we need to sort an \enquote{always left-to-right}
180 % box. At present, this functionality is only available in \LaTeX{}.
181 % \begin{macrocode}
182 \cs_new_protected:Npe \@@_record_origin:
183 {
184 \hbox_to_wd:nn { 0pt }
185 {
186 \tex_savepos:D
187 \cs_if_exist:NT \@expl@finalise@setup@@@@
188 {
189 \exp_not:N \property_record:en
190 { draw . \exp_not:N \int_use:N \exp_not:N \g_draw_id_int }
191 { xpos , ypos , abspage }
192 }
193 \tex_savepos:D
194 }
195 }
196 \cs_generate_variant:Nn \property_record:nn { e }
197 % \end{macrocode}
198 % \end{macro}
199 %
200 % \begin{macro}{\@@_finalize:}
201 % \begin{macro}{\@@_finalize_baseline:n}
202 % Finalizing the (vertical) size of the output depends on whether we have
203 % an explicit baseline or not. To allow for that, we have two functions, and
204 % the one that's used depends on whether the user has set a baseline. Notice
205 % that in contrast to \pkg{pgf} we \emph{do} allow for a non-zero depth if
206 % the explicit baseline is above the lowest edge of the initial bounding box.
207 % \begin{macrocode}
208 \cs_new_protected:Npn \@@_finalize:
209 {
210 \hbox_set:Nn \l_@@_main_box
211 {
212 \skip_horizontal:n { -\g_draw_bb_xmin_dim }
213 \box_move_down:nn
214 { \g_draw_bb_ymin_dim }
215 { \box_use_drop:N \l_@@_main_box }
216 }
217 \box_set_dp:Nn \l_@@_main_box { 0pt }
218 \box_set_ht:Nn \l_@@_main_box
219 { \g_draw_bb_ymax_dim - \g_draw_bb_ymin_dim }
220 }
221 \cs_new_protected:Npn \@@_finalize_baseline:n #1
222 {
223 \hbox_set:Nn \l_@@_main_box
224 {
225 \skip_horizontal:n { -\g_draw_bb_xmin_dim }
226 \box_move_down:nn
227 {#1}
228 { \box_use_drop:N \l_@@_main_box }
229 }
230 \box_set_dp:Nn \l_@@_main_box
231 {
232 \dim_max:nn
233 { #1 - \g_draw_bb_ymin_dim }
234 { 0pt }
235 }
236 \box_set_ht:Nn \l_@@_main_box
237 { \g_draw_bb_ymax_dim - #1 }
238 }
239 % \end{macrocode}
240 % \end{macro}
241 % \end{macro}
242 %
243 % \subsection{Baseline position}
244 %
245 % \begin{variable}{\l_@@_baseline_bool, \l_@@_baseline_dim}
246 % For tracking the explicit baseline and whether it is active.
247 % \begin{macrocode}
248 \bool_new:N \l_@@_baseline_bool
249 \dim_new:N \l_@@_baseline_dim
250 % \end{macrocode}
251 % \end{variable}
252 %
253 % \begin{macro}{\draw_set_baseline:n}
254 % A simple setting of the baseline along with the flag we need to know that
255 % it is active.
256 % \begin{macrocode}
257 \cs_new_protected:Npn \draw_set_baseline:n #1
258 {
259 \bool_set_true:N \l_@@_baseline_bool
260 \dim_set:Nn \l_@@_baseline_dim { \fp_to_dim:n {#1} }
261 }
262 % \end{macrocode}
263 % \end{macro}
264 %
265 % \begin{macro}{\@@_baseline_finalize:w}
266 % Rather than use a global data structure, we can arrange to put the baseline
267 % value at the right group level with a small amount of shuffling. That happens
268 % here.
269 % \begin{macrocode}
270 \cs_new_protected:Npn \@@_baseline_finalize:w #1 \@@_finalize:
271 {
272 \bool_if:NTF \l_@@_baseline_bool
273 {
274 \use:e
275 {
276 \exp_not:n {#1}
277 \@@_finalize_baseline:n { \dim_use:N \l_@@_baseline_dim }
278 }
279 }
280 { #1 \@@_finalize: }
281 }
282 % \end{macrocode}
283 % \end{macro}
284 %
285 % \subsection{Scopes}
286 %
287 % \begin{variable}{\l_@@_linewidth_dim}
288 % \begin{variable}{\l_@@_fill_color_tl, \l_@@_stroke_color_tl}
289 % Storage for local variables.
290 % \begin{macrocode}
291 \dim_new:N \l_@@_linewidth_dim
292 \tl_new:N \l_@@_fill_color_tl
293 \tl_new:N \l_@@_stroke_color_tl
294 % \end{macrocode}
295 % \end{variable}
296 % \end{variable}
297 %
298 % \begin{macro}{\draw_scope_begin:, \draw_scope_begin:}
299 % As well as the graphics (and \TeX{}) scope, also deal with global
300 % data structures.
301 % \begin{macrocode}
302 \cs_new_protected:Npn \draw_scope_begin:
303 {
304 \@@_backend_scope_begin:
305 \group_begin:
306 \dim_set_eq:NN \l_@@_linewidth_dim \g_@@_linewidth_dim
307 \draw_path_scope_begin:
308 }
309 \cs_new_protected:Npn \draw_scope_end:
310 {
311 \draw_path_scope_end:
312 \dim_gset_eq:NN \g_@@_linewidth_dim \l_@@_linewidth_dim
313 \group_end:
314 \@@_backend_scope_end:
315 }
316 % \end{macrocode}
317 % \end{macro}
318 %
319 % \begin{variable}
320 % {\l_@@_xmax_dim, \l_@@_xmin_dim, \l_@@_ymax_dim, \l_@@_ymin_dim}
321 % Storage for the bounding box.
322 % \begin{macrocode}
323 \dim_new:N \l_@@_xmax_dim
324 \dim_new:N \l_@@_xmin_dim
325 \dim_new:N \l_@@_ymax_dim
326 \dim_new:N \l_@@_ymin_dim
327 % \end{macrocode}
328 % \end{variable}
329 %
330 % \begin{macro}{\@@_scope_bb_begin:, \@@_scope_bb_end:}
331 % The bounding box is simple: a straight group-based save and restore
332 % approach.
333 % \begin{macrocode}
334 \cs_new_protected:Npn \@@_scope_bb_begin:
335 {
336 \group_begin:
337 \dim_set_eq:NN \l_@@_xmax_dim \g_draw_bb_xmax_dim
338 \dim_set_eq:NN \l_@@_xmin_dim \g_draw_bb_xmin_dim
339 \dim_set_eq:NN \l_@@_ymax_dim \g_draw_bb_ymax_dim
340 \dim_set_eq:NN \l_@@_ymin_dim \g_draw_bb_ymin_dim
341 \@@_reset_bb:
342 }
343 \cs_new_protected:Npn \@@_scope_bb_end:
344 {
345 \dim_gset_eq:NN \g_draw_bb_xmax_dim \l_@@_xmax_dim
346 \dim_gset_eq:NN \g_draw_bb_xmin_dim \l_@@_xmin_dim
347 \dim_gset_eq:NN \g_draw_bb_ymax_dim \l_@@_ymax_dim
348 \dim_gset_eq:NN \g_draw_bb_ymin_dim \l_@@_ymin_dim
349 \group_end:
350 }
351 % \end{macrocode}
352 % \end{macro}
353 %
354 % \begin{macro}{\draw_suspend_begin:, \draw_suspend_end:}
355 % Suspend all parts of a drawing.
356 % \begin{macrocode}
357 \cs_new_protected:Npn \draw_suspend_begin:
358 {
359 \@@_scope_bb_begin:
360 \draw_path_scope_begin:
361 \draw_transform_matrix_reset:
362 \draw_transform_shift_reset:
363 \@@_layers_save:
364 }
365 \cs_new_protected:Npn \draw_suspend_end:
366 {
367 \@@_layers_restore:
368 \draw_path_scope_end:
369 \@@_scope_bb_end:
370 }
371 % \end{macrocode}
372 % \end{macro}
373 %
374 % \begin{macrocode}
375 %</package>
376 % \end{macrocode}
377 %
378 % \end{implementation}
379 %
380 % \PrintIndex