summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/l3experimental/l3draw/l3draw-softpath.dtx
blob: f10d792808217af31bdec6f8d36f9b767c8dae70 (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
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
% \iffalse meta-comment
%
%% File: l3draw-softpath.dtx Copyright(C) 2018 The LaTeX3 Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    http://www.latex-project.org/lppl.txt
%
% This file is part of the "l3experimental bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/latex3/latex3
%
% for those people who are interested.
%
%<*driver>
\RequirePackage{expl3}
\documentclass[full]{l3doc}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{^^A
%   The \pkg{l3draw-softpath} package\\ Soft paths^^A
% }
%
% \author{^^A
%  The \LaTeX3 Project\thanks
%    {^^A
%      E-mail:
%        \href{mailto:latex-team@latex-project.org}
%          {latex-team@latex-project.org}^^A
%    }^^A
% }
%
% \date{Released 2018/02/21}
%
% \maketitle
%
% \begin{implementation}
%
% \section{\pkg{l3draw-softpath} implementation}
%
%    \begin{macrocode}
%<*initex|package>
%    \end{macrocode}
%
%    \begin{macrocode}
%<@@=draw>
%    \end{macrocode}
%
% There are two linked aims in the code here. The most significant is to
% provide a way to modify paths, for example to shorten the ends or round
% the corners.  This means that the path cannot be written piecemeal as
% specials, but rather needs to be held in macros. The second aspect that
% follows from this is performance: simply adding to a single macro a piece
% at a time will have poor performance as the list gets long. Paths need to
% be global (as specials are), so we cannot use \pkg{l3tl-build} or a similar
% approach. Instead, we use the same idea as \pkg{pgf}: use a series of buffer
% macros such that in most cases we don't add tokens to the main list. This
% will get slow only for \emph{enormous} paths.
%
% Each marker (operation) token takes two arguments, which makes processing
% more straight-forward. As such, some operations have dummy arguments, whilst
% others have to be split over several tokens. As the code here is at a low
% level, all dimension arguments are assumed to be explicit and fully-expanded.
%
% \begin{variable}
%   {
%     \g_@@_softpath_main_tl     ,
%     \g_@@_softpath_buffer_a_tl ,
%     \g_@@_softpath_buffer_b_tl
%   }
%   The soft path itself.
%    \begin{macrocode}
\tl_new:N \g_@@_softpath_main_tl
\tl_new:N \g_@@_softpath_buffer_a_tl
\tl_new:N \g_@@_softpath_buffer_b_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}
%   {
%     \g_@@_softpath_buffer_a_int ,
%     \g_@@_softpath_buffer_b_int
%   }
%   Tracking data.
%    \begin{macrocode}
\int_new:N \g_@@_softpath_buffer_a_int
\int_new:N \g_@@_softpath_buffer_b_int
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_softpath_add:n, \@@_softpath_add:x}
% \begin{macro}{\@@_softpath_concat:n}
% \begin{macro}{\@@_softpath_reset_buffers:}
%   The softpath itself is quite simple. We use three token lists to hold the
%   data: two buffers of limited length, and the main list of arbitrary size.
%   Most of the time this will mean that we don't add to the full list, so
%   performance will be acceptable.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_softpath_add:n #1
  {
    \int_compare:nNnTF \g_@@_softpath_buffer_a_int < { 40 }
      {
        \int_gincr:N \g_@@_softpath_buffer_a_int
        \tl_gput_right:Nn \g_@@_softpath_buffer_a_tl {#1}
      }
      {
        \int_compare:nNnTF \g_@@_softpath_buffer_b_int < { 40 }
          {
            \int_gincr:N \g_@@_softpath_buffer_b_int
            \tl_gset:Nx \g_@@_softpath_buffer_b_tl
              {
                \exp_not:V \g_@@_softpath_buffer_b_tl
                \exp_not:V \g_@@_softpath_buffer_a_tl
                \exp_not:n {#1}
              }
            \int_gzero:N \g_@@_softpath_buffer_a_int
            \tl_gclear:N \g_@@_softpath_buffer_a_tl
          }
          { \@@_softpath_concat:n {#1} }
      }
  }
\cs_generate_variant:Nn \@@_softpath_add:n { x }
\cs_new_protected:Npn \@@_softpath_concat:n #1
  {
    \tl_gset:Nx \g_@@_softpath_main_tl
      {
        \exp_not:V \g_@@_softpath_main_tl
        \exp_not:V \g_@@_softpath_buffer_b_tl
        \exp_not:V \g_@@_softpath_buffer_a_tl
        \exp_not:n {#1}
      }
    \@@_softpath_reset_buffers:
  }
\cs_new_protected:Npn \@@_softpath_reset_buffers:
  {
    \int_gzero:N \g_@@_softpath_buffer_a_int
    \tl_gclear:N \g_@@_softpath_buffer_a_tl
    \int_gzero:N \g_@@_softpath_buffer_b_int
    \tl_gclear:N \g_@@_softpath_buffer_b_tl
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_softpath_get:N, \@@_softpath_set_eq:N}
%   Save and restore functions.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_softpath_get:N #1
  {
    \@@_softpath_concat:n { }
    \tl_set_eq:NN #1 \g_@@_softpath_main_tl
  }
\cs_new_protected:Npn \@@_softpath_set_eq:N #1
  {
    \tl_gset_eq:NN \g_@@_softpath_main_tl #1
    \@@_softpath_reset_buffers:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}
%   {\@@_softpath_use:, \@@_softpath_clear:, \@@_softpath_use_clear:}
%   Using and clearing is trivial.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_softpath_use:
  {
    \g_@@_softpath_main_tl
    \g_@@_softpath_buffer_b_tl
    \g_@@_softpath_buffer_a_tl
  }
\cs_new_protected:Npn \@@_softpath_clear:
  {
    \tl_gclear:N \g_@@_softpath_main_tl
    \tl_gclear:N \g_@@_softpath_buffer_a_tl
    \tl_gclear:N \g_@@_softpath_buffer_b_tl
  }
\cs_new_protected:Npn \@@_softpath_use_clear:
  {
    \@@_softpath_use:
    \@@_softpath_clear:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{variable}{\g_@@_softpath_lastx_dim, \g_@@_softpath_lasty_dim}
%   For tracking the end of the path (to close it).
%    \begin{macrocode}
\dim_new:N \g_@@_softpath_lastx_dim
\dim_new:N \g_@@_softpath_lasty_dim
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_softpath_move_bool}
%   Track if moving a point should update the close position.
%    \begin{macrocode}
\bool_new:N \g_@@_softpath_move_bool
\bool_gset_true:N \g_@@_softpath_move_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\@@_softpath_curveto:nnnnnn}
% \begin{macro}
%   {
%     \@@_softpath_lineto:nn,
%     \@@_softpath_moveto:nn
%   }
% \begin{macro}{\@@_softpath_rectangle:nnnn}
% \begin{macro}{\@@_softpath_roundpoint:nn, \@@_softpath_roundpoint:VV}
%   The various parts of a path expressed as the appropriate soft path
%   functions.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_softpath_closepath:
  {
    \@@_softpath_add:x
      {
        \@@_softpath_close_op:nn
          { \dim_use:N \g_@@_softpath_lastx_dim }
          { \dim_use:N \g_@@_softpath_lasty_dim }
      }
  }
\cs_new_protected:Npn \@@_softpath_curveto:nnnnnn #1#2#3#4#5#6
  {
    \@@_softpath_add:n
      {
        \@@_softpath_curveto_opi:nn {#1} {#2}
        \@@_softpath_curveto_opii:nn {#3} {#4}
        \@@_softpath_curveto_opiii:nn {#5} {#6}
      }
  }
\cs_new_protected:Npn \@@_softpath_lineto:nn #1#2
  {
    \@@_softpath_add:n
      { \@@_softpath_lineto_op:nn {#1} {#2} }
  }
\cs_new_protected:Npn \@@_softpath_moveto:nn #1#2
  {
    \@@_softpath_add:n
      { \@@_softpath_moveto_op:nn {#1} {#2} }
    \bool_if:NT \g_@@_softpath_move_bool
      {
        \dim_gset:Nn \g_@@_softpath_lastx_dim {#1}
        \dim_gset:Nn \g_@@_softpath_lasty_dim {#2}
      }
  }
\cs_new_protected:Npn \@@_softpath_rectangle:nnnn #1#2#3#4
  {
    \@@_softpath_add:n
      {
        \@@_softpath_rectangle_opi:nn {#1} {#2}
        \@@_softpath_rectangle_opii:nn {#3} {#4}
      }
  }
\cs_new_protected:Npn \@@_softpath_roundpoint:nn #1#2
  {
    \@@_softpath_add:n
      { \@@_softpath_roundpoint_op:nn {#1} {#2} }
  }
\cs_generate_variant:Nn \@@_softpath_roundpoint:nn { VV }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}
%   {
%     \@@_softpath_close_op:nn      ,
%     \@@_softpath_curveto_opi:nn   ,
%     \@@_softpath_curveto_opii:nn  ,
%     \@@_softpath_curveto_opiii:nn ,
%     \@@_softpath_lineto_op:nn     ,
%     \@@_softpath_moveto_op:nn     ,
%     \@@_softpath_roundpoint_op:nn ,
%     \@@_softpath_rectangle_opi:nn ,
%     \@@_softpath_rectangle_opii:nn
%   }
% \begin{macro}{\@@_softpath_curveto_opi:nnNnnNnn}
% \begin{macro}{\@@_softpath_rectangle_opi:nnNnn}
%   The markers for operations: all the top-level ones take two arguments.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_softpath_close_op:nn #1#2
  { \driver_draw_closepath: }
\cs_new_protected:Npn \@@_softpath_curveto_opi:nn #1#2 
  { \@@_softpath_curveto_opi:nnNnnNnn {#1} {#2} }
\cs_new_protected:Npn \@@_softpath_curveto_opi:nnNnnNnn #1#2#3#4#5#6#7#8
  { \driver_draw_curveto:nnnnnn {#1} {#2} {#4} {#5} {#7} {#8} }
\cs_new_protected:Npn \@@_softpath_curveto_opii:nn #1#2 { }
\cs_new_protected:Npn \@@_softpath_curveto_opiii:nn #1#2 { }
\cs_new_protected:Npn \@@_softpath_lineto_op:nn #1#2
  { \driver_draw_lineto:nn {#1} {#2} }
\cs_new_protected:Npn \@@_softpath_moveto_op:nn #1#2
  { \driver_draw_moveto:nn {#1} {#2} }
\cs_new_protected:Npn \@@_softpath_roundpoint_op:nn #1#2 { }
\cs_new_protected:Npn \@@_softpath_rectangle_opi:nn #1#2 
  { \@@_softpath_rectangle_opi:nnNnn {#1} {#2} }
\cs_new_protected:Npn \@@_softpath_rectangle_opi:nnNnn #1#2#3#4#5
  { \driver_draw_rectangle:nnnn {#1} {#2} {#4} {#5} }
  \cs_new_protected:Npn \@@_softpath_rectangle_opii:nn #1#2 { }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
%</initex|package>
%    \end{macrocode}
%
% \end{implementation}
%
% \PrintIndex