summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/l3kernel/l3drivers.dtx
blob: cfbcb9bdce37a1751e27a86488dada00e32d7741 (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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
% \iffalse meta-comment
%
%% File: l3drivers.dtx Copyright(C) 2011-2016 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 "l3kernel bundle" (The Work in LPPL)
%% and all files in that bundle must be distributed together.
%%
%% The released version of this bundle is available from CTAN.
%%
%% -----------------------------------------------------------------------
%%
%% The development version of the bundle can be found at
%%
%%    http://www.latex-project.org/svnroot/experimental/trunk/
%%
%% for those people who are interested.
%%
%%%%%%%%%%%
%% NOTE: %%
%%%%%%%%%%%
%%
%%   Snapshots taken from the repository represent work in progress and may
%%   not work or may contain conflicting material!  We therefore ask
%%   people _not_ to put them into distributions, archives, etc. without
%%   prior consultation with the LaTeX Project Team.
%%
%% -----------------------------------------------------------------------
%%
%
%<*driver>
\documentclass[full]{l3doc}
%</driver>
%<*driver|package>
\GetIdInfo$Id: l3drivers.dtx 6511 2016-05-17 20:20:23Z joseph $
  {L3 Experimental drivers}
%</driver|package>
%<*driver>
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{^^A
%   The \textsf{l3drivers} package\\ Drivers^^A
%   \thanks{This file describes v\ExplFileVersion,
%     last revised \ExplFileDate.}^^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 \ExplFileDate}
%
% \maketitle
%
% \begin{documentation}
%
% \TeX{} relies on drivers in order to carry out a number of tasks, such
% as using color, including graphics and setting up hyper-links. The nature
% of the code required depends on the exact driver in use. Currently,
% \LaTeX3 is aware of the following drivers:
% \begin{itemize}
%   \item \texttt{pdfmode}:  The \enquote{driver} for direct PDF output by
%     \emph{both} \pdfTeX{} and \LuaTeX{} (no separate driver is used in this
%     case: the engine deals with PDF creation itself).
%   \item \texttt{dvips}: The \texttt{dvips} program, which works in
%     conjugation with \pdfTeX{} or \LuaTeX{} in DVI mode.
%   \item \texttt{dvipdfmx}: The \texttt{dvipdfmx} program, which works in
%     conjugation with \pdfTeX{} or \LuaTeX{} in DVI mode.
%   \item \texttt{dvisvgm}:  The \texttt{dvisvgm} program, which works in
%     conjugation with \pdfTeX{} or \LuaTeX{} in DVI mode to create SVG
%     output.
%   \item \texttt{xdvipdfmx}: The driver used by \XeTeX{}.
% \end{itemize}
%
% The code here is all very low-level, and should not in general be used
% outside of the kernel. It is also important to note that many of the
% functions here are closely tied to the immediate level \enquote{up},
% and they must be used in the correct contexts.d
%
% \section{Box clipping}
%
% \begin{function}[added = 2011-11-11]{\__driver_box_use_clip:N}
%   \begin{syntax}
%     \cs{__driver_box_use_clip:N} \meta{box}
%   \end{syntax}
%   Inserts the content of the \meta{box} at the current insertion point
%   such that any material outside of the bounding box will not be displayed
%   by the driver. The material in the \meta{box} is still placed in the
%   output stream: the clipping takes place at a driver level.
%
%   This function should only be used within a surrounding horizontal
%   box construct.
% \end{function}
%
% \section{Box rotation and scaling}
%
% \begin{function}[added = 2016-05-12]{\__driver_box_use_rotate:Nn}
%   \begin{syntax}
%     \cs{__driver_box_use_rotate:Nn} \meta{box} \Arg{angle}
%   \end{syntax}
%   Inserts the content of the \meta{box} at the current insertion point
%   rotated by the \meta{angle} (expressed in degrees). The material is
%   inserted with no apparent height or width, and is rotated such the
%   the \TeX{} reference point of the box is the center of rotation and
%   remains the reference point after rotation. It is the responsibly of
%   the code using this function to adjust the apparent size of the box to
%   be correct at the \TeX{} side.
%
%   This function should only be used within a surrounding horizontal
%   box construct.
% \end{function}
%
% \begin{function}[added = 2016-05-12]{\__driver_box_use_scale:Nnn}
%   \begin{syntax}
%     \cs{__driver_box_use_scale:Nnn} \meta{box} \Arg{x-scale} \Arg{y-scale}
%   \end{syntax}
%   Inserts the content of the \meta{box} at the current insertion point
%   scale by the \meta{x-scale} and \meta{y-scale}. The material is
%   inserted with no apparent height or width. It is the responsibly of
%   the code using this function to adjust the apparent size of the box to
%   be correct at the \TeX{} side.
%
%   This function should only be used within a surrounding horizontal
%   box construct.
% \end{function}
%
% \section{Color support}
%
% \begin{function}[added = 2011-09-03, updated = 2012-05-18]
%   {\__driver_color_ensure_current:}
%   \begin{syntax}
%     \cs{__driver_color_ensure_current:}
%   \end{syntax}
%   Ensures that the color used to typeset material is that which was
%   set when the material was placed in a box. This function is therefore
%   required inside any \enquote{color safe} box to ensure that the box may
%   be inserted in a location where the foreground color has been altered,
%   while preserving the color used in the box.
% \end{function}
%
% \section{Drawing}
%
% The drawing functions provided here are \emph{highly} experimental. They
% are inspired heavily by the system layer of \pkg{pgf} (most have the
% same interface as the same functions in the latter's \cs{pgfsys@\ldots}
% namespace). They are intended to form the basis for higher level drawing
% interfaces, which themselves are likely to be further abstracted for user
% access. Again, this model is heavily inspired by \pkg{pgf} and
% Ti\textit{k}z.
%
% These low level drawing interfaces abstract from the driver raw requirements
% but still require an appreciation of the concepts of PostScript/PDF/SVG
% graphic creation.
%
% At present \emph{not all drivers are covered} by the following.
%
% \begin{function}
%   { \__driver_draw_begin:,  \__driver_draw_end:}
%   \begin{syntax}
%     \cs{__driver_draw_begin:}
%     \meta{content}
%     \cs{__driver_draw_end:}
%   \end{syntax}
%   Defines a drawing environment. This will be a scope for the purposes of
%   the graphics state. Depending on the driver, other set up may or may not
%   take place here. The natural size of the \meta{content} should be zero
%   from the \TeX{} perspective: allowance for the size of the content must
%   be made at a higher level (or indeed this can be skipped if the content is
%   to overlap other material).
% \end{function}
%
% \begin{function}
%   {\__driver_draw_scope_begin:,  \__driver_draw_scope_end:}
%   \begin{syntax}
%     \cs{__driver_draw_scope_begin:}
%     \meta{content}
%     \cs{__driver_draw_scope_end:}
%   \end{syntax}
%   Defines a scope for drawing settings and so on. Changes to the graphic
%   state and concepts such as color or linewidth are localised to a scope.
%   This function pair must never be used if an partial path is under
%   construction: such paths must be entirely contained at one unbroken
%   scope level.
% \end{function}
%
% \subsection{Path construction}
%
% \begin{function}{\__driver_draw_moveto:nn}
%   \begin{syntax}
%     \cs{__driver_draw_move:nn} \Arg{x} \Arg{y}
%   \end{syntax}
%   Moves the current drawing reference point to (\meta{x}, \meta{y});
%   any active transformation matrix will apply.
% \end{function}
%
% \begin{function}{\__driver_draw_lineto:nn}
%   \begin{syntax}
%     \cs{__driver_draw_lineto:nn} \Arg{x} \Arg{y}
%   \end{syntax}
%   Adds a path from the current drawing reference point to
%   (\meta{x}, \meta{y}); any active transformation matrix will apply. Note
%   that nothing is drawn until a fill or stroke operation is applied, and that
%   the path may be discarded or used as a clip without appearing itself.
% \end{function}
%
% \begin{function}{\__driver_draw_curveto:nnnnnn}
%   \begin{syntax}
%     \cs{__driver_draw_curveto:nnnnnn} \Arg{x_1} \Arg{y_1}
%       \Arg{x_2} \Arg{y_2} \Arg{x_3} \Arg{y_3}
%   \end{syntax}
%   Adds a Bezier curve path from the current drawing reference point to
%   (\meta{x_3}, \meta{y_3}), using (\meta{x_1}, \meta{y_1}) and
%   (\meta{x_2}, \meta{y_2}) as control points; any active transformation
%   matrix will apply.  Note that nothing is drawn until a fill or stroke
%   operation is applied, and that the path may be discarded or used as a clip
%   without appearing itself.
% \end{function}
%
% \begin{function}{\__driver_draw_closepath:}
%   \begin{syntax}
%     \cs{__driver_draw_closepath:}
%   \end{syntax}
%   Closes an existing path, adding a line from the current point to the
%   start of path. Note that nothing is drawn until a fill or stroke
%   operation is applied, and that the path may be discarded or used as a clip
%   without appearing itself.
% \end{function}
%
% \subsection{Stroking and filling}
%
% \begin{function}{\__driver_draw_stroke:, \__driver_draw_closestroke:}
%   \begin{syntax}
%     \meta{path construction}
%     \cs{__driver_draw_stroke:}
%   \end{syntax}
%   Draws a line alone the current path, which will also be closed when
%   \cs{__driver_draw_closestroke:} is used. The nature of the line drawn
%   is influenced by settings for
%   \begin{itemize}
%     \item Line thickness
%     \item Stroke color (or the current color if no specific stroke color
%       is set)
%     \item Line capping (how non-closed line ends should look)
%     \item Join style (how a bend in the path should be rendered)
%     \item Dash pattern
%   \end{itemize}
%   The path may also be used for clipping.
% \end{function}
%
% \begin{function}{\__driver_draw_fill:, \__driver_draw_fillstroke:}
%   \begin{syntax}
%     \meta{path construction}
%     \cs{__driver_draw_fill:}
%   \end{syntax}
%   Fills the area surrounded by the current path: this will be closed prior
%   to filling if it is not already. The \texttt{fillstroke} version will also
%   stroke the path as described for \cs{__driver_draw_stroke:}. The fill is
%   influenced by the setting for fill color (or the current color if no
%   specific stroke color is set). The path may also be used for clipping.
%   For paths which are self-intersecting or comprising multiple parts, the
%   determination of which areas are inside the path is made using the nonzero
%   winding rule number unless \cs{__driver_draw_eor_bool} is \texttt{true},
%   in which case the even-odd rule is used.
% \end{function}
%
% \begin{function}{\__driver_draw_clip:}
%   \begin{syntax}
%     \meta{path construction}
%     \cs{__driver_draw_clip:}
%   \end{syntax}
%   Indicates that the current path should be used for clipping, such that
%   any subsequent material outside of the path (but within the current
%   scope) will not be shown. This command should be given once a path is
%   complete but before it is stroked or filled (if appropriate).
% \end{function}
%
% \begin{function}{\__driver_draw_discardpath:}
%   \begin{syntax}
%     \meta{path construction}
%     \cs{__driver_draw_discardpath:}
%   \end{syntax}
%   Discards the current path without stroking or filling. This is primarily
%   useful for paths constructed purely for clipping, as this alone does not
%   end the paths existence.
% \end{function}
%
% \subsection{Stroke options}
%
% \begin{function}{\__driver_draw_linewidth:n}
%   \begin{syntax}
%     \cs{__driver_draw_linewidth:n} \Arg{dimexpr}
%   \end{syntax}
%   Sets the width to be used for stroking to \meta{dimexpr}.
% \end{function}
%
% \begin{function}{\__driver_draw_dash:nn}
%   \begin{syntax}
%     \cs{__driver_draw_dash:nn} \Arg{dash pattern} \Arg{phase}
%   \end{syntax}
%   Sets the pattern of dashing to be used when stroking a line. The
%   \meta{dash pattern} should be a comma-separated list of dimension
%   expressions. This is then interpreted as a series of pairs of line-on
%   and line-off lengths. For example \texttt{3pt, 4pt} means that $3$\,pt on,
%   $4$\,pt off, $3$\,pt on, and so on. A more complex pattern will also
%   repeat: \texttt{3pt, 4pt, 1pt, 2pt} results in $3$\,pt on, $4$\,pt off,
%   $1$\,pt on, $2$\,pt off, $3$\,pt on, and so on. An odd number of entries
%   means that the last is repeated, for example \texttt{3pt} is equal to
%   \texttt{3pt, 3pt}. An empty pattern yields a solid line.
%   
%   The \meta{phase} specifies an offset at the start of the cycle. For
%   example, with a pattern \texttt{3pt} a phase of \texttt{1pt} will mean
%   that the output is $2$\,pt on, $3$\,pt off, $3$\,pt on, $3$\,pt on,
%   \emph{etc.}
% \end{function}
%
% \begin{function}
%   {
%     \__driver_draw_cap_butt:      ,
%     \__driver_draw_cap_rectangle: ,
%     \__driver_draw_cap_round:
%   }
%   \begin{syntax}
%     \cs{__driver_draw_cap_butt:}
%   \end{syntax}
%   Sets the style of terminal stroke position to one of butt, rectangle or
%   round.
% \end{function}
%
% \begin{function}
%   {
%     \__driver_draw_join_bevel:      ,
%     \__driver_draw_join_miter: ,
%     \__driver_draw_join_round:
%   }
%   \begin{syntax}
%     \cs{__driver_draw_cap_butt:}
%   \end{syntax}
%   Sets the style of stroke joins to one of bevel, miter or round.
% \end{function}
%
% \begin{function}{\__driver_draw_miterlimit:n}
%   \begin{syntax}
%     \cs{__driver_draw_miterlimit:n} \Arg{dimexpr}
%   \end{syntax}
%   Sets the miter limit of lines joined as a miter, as described in the
%   PDF and PostScript manuals.
% \end{function}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3drivers} Implementation}
%
%    \begin{macrocode}
%<*initex|package>
%<@@=driver>
%    \end{macrocode}
%
% Whilst there is a reasonable amount of code overlap between drivers,
% it is much clearer to have the blocks more-or-less separated than run
% in together and DocStripped out in parts. As such, most of the following
% is set up on a per-driver basis, though there is some common code (again
% given in blocks not interspersed with other material).
%
% All the file identifiers are up-front so that they come out in the right
% place in the files.
%    \begin{macrocode}
%<*package>
\ProvidesExplFile
%<*dvipdfmx>
  {l3dvidpfmx.def}{\ExplFileDate}{\ExplFileVersion}
  {L3 Experimental driver: dvipdfmx}
%</dvipdfmx>
%<*dvips>
  {l3dvips.def}{\ExplFileDate}{\ExplFileVersion}
  {L3 Experimental driver: dvips}
%</dvips>
%<*dvisvgm>
  {l3dvisvgm.def}{\ExplFileDate}{\ExplFileVersion}
  {L3 Experimental driver: dvisvgm}
%</dvisvgm>
%<*pdfmode>
  {l3pdfmode.def}{\ExplFileDate}{\ExplFileVersion}
  {L3 Experimental driver: PDF mode}
%</pdfmode>
%<*xdvipdfmx>
  {l3xdvidpfmx.def}{\ExplFileDate}{\ExplFileVersion}
  {L3 Experimental driver: xdvipdfmx}
%</xdvipdfmx>
%</package>
%    \end{macrocode}
%
% \subsection{\texttt{pdfmode} driver}
%
%    \begin{macrocode}
%<*pdfmode>
%    \end{macrocode}
%
% The direct PDF driver covers both \pdfTeX{} and \LuaTeX{}. The latter
% renames/restructures the driver primitives but this can be handled
% at one level of abstraction. As such, we avoid using two separate drivers
% for this material at the cost of some \texttt{x}-type definitions to get
% everything expanded up-front.
%
% \subsubsection{Basics}
%
% \begin{macro}[int]{\@@_literal:n}
%   This is equivalent to \verb|\special{pdf:}| but the engine can
%   track it. Without the \texttt{direct} keyword everything is kept in
%   sync: the transformation matrix is set to the current point automatically.
%   Note that this is still inside the text (\texttt{BT} \dots \texttt{ET}
%   block).
%    \begin{macrocode}
\cs_new_protected:Npx \@@_literal:n #1
  {
    \cs_if_exist:NTF \luatex_pdfextension:D
      { \luatex_pdfextension:D literal }
      { \pdftex_pdfliteral:D }
        {#1}
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_scope_begin:, \@@_scope_end:}
%  Higher-level interfaces for saving and restoring the graphic state.
%    \begin{macrocode}
\cs_new_protected_nopar:Npx \@@_scope_begin:
  {
    \cs_if_exist:NTF \luatex_pdfextension:D
      { \luatex_pdfextension:D save \scan_stop: }
      { \pdftex_pdfsave:D }
  }
\cs_new_protected_nopar:Npx \@@_scope_end:
  {
    \cs_if_exist:NTF \luatex_pdfextension:D
      { \luatex_pdfextension:D restore \scan_stop: }
      { \pdftex_pdfrestore:D }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_matrix:n}
%   Here the appropriate function is set up to insert an affine matrix
%   into the PDF. With \pdfTeX{} and \LuaTeX{} in direct PDF output mode there
%   is a primitive for this, which only needs the rotation/scaling/skew part.
%    \begin{macrocode}
\cs_new_protected:Npx \@@_matrix:n #1
  {
    \cs_if_exist:NTF \luatex_pdfextension:D
      { \luatex_pdfextension:D setmatrix }
      { \pdftex_pdfsetmatrix:D }
        {#1}
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Color}
%
% \begin{variable}{\l_@@_current_color_tl}
%   The current color in driver-dependent format: pick up the package-mode
%   data if available.
%    \begin{macrocode}
\tl_new:N \l_@@_current_color_tl
\tl_set:Nn \l_@@_current_color_tl { 0~g~0~G }
%<*package>
\AtBeginDocument
  {
    \@ifpackageloaded { color }
      { \tl_set:Nn \l_@@_current_color_tl { \current@color } }
      { }
  }
%</package>
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_color_stack_int}
%   \pdfTeX{} and \LuaTeX{} have multiple stacks available, and to track
%   which one is in use a variable is required.
%    \begin{macrocode}
\int_new:N \l_@@_color_stack_int
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}[int]{\@@_color_ensure_current:}
% \begin{macro}[aux]{\@@_color_reset:}
%   There is a dedicated primitive/primitive interface for setting colors.
%   As with scoping, this approach is not suitable for cached operations.
%    \begin{macrocode}
\cs_new_protected_nopar:Npx \@@_color_ensure_current:
  {
    \cs_if_exist:NTF \luatex_pdfextension:D
      { \luatex_pdfextension:D colorstack }
      { \pdftex_pdfcolorstack:D }
        \exp_not:N \l_@@_color_stack_int push
          { \exp_not:N \l_@@_current_color_tl }
    \group_insert_after:N \exp_not:N \@@_color_reset:
  }
\cs_new_protected_nopar:Npx \@@_color_reset:
  {
    \cs_if_exist:NTF \luatex_pdfextension:D
      { \luatex_pdfextension:D colorstack }
      { \pdftex_pdfcolorstack:D }
        \exp_not:N \l_@@_color_stack_int pop \scan_stop:
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
%</pdfmode>
%    \end{macrocode}
%
% \subsection{\texttt{dvipdfmx} driver}
%
%    \begin{macrocode}
%<*dvipdfmx|xdvipdfmx>
%    \end{macrocode}
%
% The \texttt{dvipdfmx} shares code with the PDF mode one (using the common
% section to this file) but also with \texttt{xdvipdfmx}. The latter is close
% to identical to \texttt{dvipdfmx} and so all of the code here is extracted
% for both drivers, with some \texttt{clean up} for \texttt{xdvipdfmx} as
% required.
%
% \subsubsection{Basics}
%
% \begin{macro}[int]{\@@_literal:n}
%   Equivalent to \texttt{pdf:content} but favoured as the link to
%   the \pdfTeX{} primitive approach is clearer.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_literal:n #1
  { \tex_special:D { pdf:literal~ #1 } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_scope_begin:, \@@_scope_end:}
%   Scoping is done using direct PDF operations here.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_scope_begin:
  { \@@_literal:n { q } }
\cs_new_protected_nopar:Npn \@@_scope_end:
  { \@@_literal:n { Q } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_matrix:n}
%   With \texttt{(x)dvipdfmx} the matrix has to include a translation
%   part: that is always zero and so is built in here so that the same
%   internal interface works for all PDF-related drivers.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_matrix:n #1
  { \@@_literal:n { #1 \c_space_tl 0~0~cm } }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Color}
%
% \begin{variable}{\l_@@_current_color_tl}
%   The current color in driver-dependent format.
%    \begin{macrocode}
\tl_new:N \l_@@_current_color_tl
\tl_set:Nn \l_@@_current_color_tl { [ 0 ] }
%<*package>
\AtBeginDocument
  {
    \@ifpackageloaded { color }
      { \tl_set:Nn \l_@@_current_color_tl { \current@color } }
      { }
  }
%</package>
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}[int]{\@@_color_ensure_current:}
% \begin{macro}[aux]{\@@_color_reset:}
%   Directly set the color using the specials with optimisation support.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_color_ensure_current:
  {
    \tex_special:D { pdf:bcolor~\l_@@_current_color_tl }
    \group_insert_after:N \@@_color_reset:
  }
\cs_new_protected_nopar:Npn \@@_color_reset:
  { \tex_special:D { pdf:ecolor } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
%</dvipdfmx|xdvipdfmx>
%    \end{macrocode}
%
% \subsection{\texttt{xdvipdfmx} driver}
%
%    \begin{macrocode}
%<*xdvipdfmx>
%    \end{macrocode}
%
% \subsubsection{Color}
%
% \begin{macro}[int]{\@@_color_ensure_current:}
% \begin{macro}[aux]{\@@_color_reset:}
%   The \LaTeXe{} driver uses \texttt{dvips}-like specials so there has to
%   be a change of set up if \pkg{color} is loaded.
%    \begin{macrocode}
%<*package>
\AtBeginDocument
  {
    \@ifpackageloaded { color }
      {
        \cs_set_protected_nopar:Npn \@@_color_ensure_current:
          {
            \tex_special:D { color~push~\l_@@_current_color_tl }
            \group_insert_after:N \@@_color_reset:
          }
        \cs_set_protected_nopar:Npn \@@_color_reset:
          { \tex_special:D { color~pop } }
      }
      { }
  }
%</package>
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
%</xdvipdfmx>
%    \end{macrocode}
%
% \subsection{Common code for PDF production}
%
% As all of the drivers which understand PDF-targeted specials act in much
% the same way there is a lot of shared code. Rather than try to DocStrip it
% interspersed with the above, we collect all of it here.
%
%    \begin{macrocode}
%<*dvipdfmx|pdfmode|xdvipdfmx>
%    \end{macrocode}
%
% \subsubsection{Box operations}
%
% \begin{macro}{\@@_box_use_clip:N}
%   The general method is to save the current location, define a clipping path
%   equivalent to the bounding box, then insert the content at the current
%   position and in a zero width box. The \enquote{real} width is then made up
%   using a horizontal skip before tidying up. There are other approaches that
%   can be taken (for example using XForm objects), but the logic here shares
%   as much code as possible and uses the same conversions (and so same
%   rounding errors) in all cases.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_box_use_clip:N #1
  {
    \@@_scope_begin:
    \@@_literal:n
      {
        0~
        \dim_to_decimal_in_bp:n { -\box_dp:N #1 } ~
        \dim_to_decimal_in_bp:n { \box_wd:N #1 } ~
        \dim_to_decimal_in_bp:n { \box_ht:N #1 + \box_dp:N #1 } ~
        re~W~n
      }
    \hbox_overlap_right:n { \box_use:N #1 }
    \@@_scope_end:
    \skip_horizontal:n { \box_wd:N #1 }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_box_use_rotate:Nn}
% \begin{variable}{\l_@@_cos_fp, \l_@@_sin_fp}
%   Rotations are set using an affine transformation matrix which therefore
%   requires sine/cosine values not the angle itself. We store the rounded
%   values to avoid rounding twice. There are also a couple of comparisons to
%   ensure that |-0| is not written to the output, as this avoids any issues
%   with problematic display programs.  Note that numbers are compared to~$0$
%   after rounding.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_box_use_rotate:Nn #1#2
  {
    \@@_scope_begin:
    \box_set_wd:Nn #1 \c_zero_dim
    \fp_set:Nn \l_@@_cos_fp { round ( cosd ( #2 ) , 5 ) }
    \fp_compare:nNnT \l_@@_cos_fp = \c_zero_fp
      { \fp_zero:N \l_@@_cos_fp }
    \fp_set:Nn \l_@@_sin_fp { round ( sind ( #2 ) , 5 ) }
    \@@_matrix:n
      {
        \fp_use:N \l_@@_cos_fp \c_space_tl
        \fp_compare:nNnTF \l_@@_sin_fp = \c_zero_fp
          { 0~0 }
          {
            \fp_use:N \l_@@_sin_fp
            \c_space_tl
            \fp_eval:n { -\l_@@_sin_fp }
          }
        \c_space_tl
        \fp_use:N \l_@@_cos_fp
      }
   \box_use:N #1
   \@@_scope_end:
  }
\fp_new:N \l_@@_cos_fp
\fp_new:N \l_@@_sin_fp
%    \end{macrocode}
% \end{variable}
% \end{macro}
%
% \begin{macro}{\@@_box_use_scale:Nnn}
%   The same idea as for rotation but without the complexity of signs and
%   cosines.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_box_use_scale:Nnn #1#2#3
  {
    \@@_scope_begin:
    \@@_matrix:n
      {
        \fp_eval:n { round ( #2 , 5 ) } ~
        0~0~
        \fp_eval:n { round ( #3 , 5 ) }
      }
    \hbox_overlap_right:n { \box_use:N #1 }
    \@@_scope_end:
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Drawing}
%
% \begin{macro}[aux]{\@@_draw_literal:n, \@@_draw_literal:x}
%   Pass data through using a dedicated interface.
%     \begin{macrocode}
\cs_new_eq:NN \@@_draw_literal:n \@@_literal:n
\cs_generate_variant:Nn \@@_draw_literal:n { x }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_draw_begin:, \@@_draw_end:}
%   No special requirements here, so simply set up a drawing scope.
%     \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_draw_begin:
  { \@@_draw_scope_begin: }
\cs_new_protected_nopar:Npn \@@_draw_end:
  { \@@_draw_scope_end: }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_draw_scope_begin:, \@@_draw_scope_end:}
%   In contrast to a general scope, a drawing scope is always done using
%   the PDF operators so is the same for all relevant drivers.
%     \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_draw_scope_begin:
  { \@@_draw_literal:n { q } }
\cs_new_protected_nopar:Npn \@@_draw_scope_end:
  { \@@_draw_literal:n { Q } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_draw_moveto:nn, \@@_draw_lineto:nn}
% \begin{macro}[int]{\@@_draw_curveto:nnnnnn}
%   Path creation operations all resolve directly to PDF primitive steps, with
%   only the need to convert to \texttt{bp}. Notice that \texttt{x}-type
%   expansion is included here to ensure that any variable values are
%   forced to literals before any possible caching.
%     \begin{macrocode}
\cs_new_protected:Npn \@@_draw_moveto:nn #1#2
  {
    \@@_draw_literal:x
      { \dim_to_decimal_in_bp:n {#1} ~ \dim_to_decimal_in_bp:n {#2} ~ m }
  }
\cs_new_protected:Npn \@@_draw_lineto:nn #1#2
  {
    \@@_draw_literal:x
      { \dim_to_decimal_in_bp:n {#1} ~ \dim_to_decimal_in_bp:n {#2} ~ l }
  }
\cs_new_protected:Npn \@@_draw_curveto:nnnnnn #1#2#3#4#5#6
  {
    \@@_draw_literal:x
      {
        \dim_to_decimal_in_bp:n {#1} ~ \dim_to_decimal_in_bp:n {#2} ~
        \dim_to_decimal_in_bp:n {#3} ~ \dim_to_decimal_in_bp:n {#4} ~
        \dim_to_decimal_in_bp:n {#5} ~ \dim_to_decimal_in_bp:n {#6} ~
        c
      }
 }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{variable}[int]{\@@_draw_eor_bool}
%    The even-odd rule here can be implemented as a simply switch.
%     \begin{macrocode}
\bool_new:N \@@_draw_eor_bool
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}[int]
%   {
%     \@@_draw_closepath:   ,
%     \@@_draw_stroke:      ,
%     \@@_draw_closestroke: ,
%     \@@_draw_fill:        ,
%     \@@_draw_fillstroke:  ,
%     \@@_draw_clip:        ,
%     \@@_draw_discardpath:
%   }
%   Converting paths to output is again a case of mapping directly to
%   PDF operations.
%     \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_draw_closepath:
  { \@@_draw_literal:n { h } }
\cs_new_protected_nopar:Npn \@@_draw_stroke:
  { \@@_draw_literal:n { S } }
\cs_new_protected_nopar:Npn \@@_draw_closestroke:
  { \@@_draw_literal:n { s } }
\cs_new_protected_nopar:Npn \@@_draw_fill:
  {
    \@@_draw_literal:x
      { f \bool_if:NT \@@_draw_eor_bool * }
  }
\cs_new_protected_nopar:Npn \@@_draw_fillstroke:
  {
    \@@_draw_literal:x
      { B \bool_if:NT \@@_draw_eor_bool * }
  }
\cs_new_protected_nopar:Npn \@@_draw_clip:
  {
    \@@_draw_literal:x
      { W \bool_if:NT \@@_draw_eor_bool * }
  }
\cs_new_protected_nopar:Npn \@@_draw_discardpath:
  { \@@_draw_literal:n { n } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_draw_dash:nn}
% \begin{macro}[aux]{\@@_draw_dash:n}
% \begin{macro}[int]{\@@_draw_linewidth:n}
% \begin{macro}[int]{\@@_draw_miterlimit:n}
% \begin{macro}[int]^^A
%   {
%     \@@_draw_cap_butt:, \@@_draw_cap_round:, \@@_draw_cap_rectangle:,
%     \@@_draw_join_miter:, \@@_draw_join_round:, \@@_draw_join_bevel:
%   }
%   Converting paths to output is again a case of mapping directly to
%   PDF operations.
%     \begin{macrocode}
\cs_new_protected:Npn \@@_draw_dash:nn #1#2
  {
    \@@_draw_literal:x
      {
        [ ~
          \clist_map_function:nN {#1} \@@_draw_dash:n
        ] ~
        \dim_to_decimal_in_bp:n {#2} ~ d
      }
  }
\cs_new:Npn \@@_draw_dash:n #1
  { \dim_to_decimal_in_bp:n {#1} ~ }
\cs_new_protected:Npn \@@_draw_linewidth:n #1
  {
    \@@_draw_literal:x
      { \dim_to_decimal_in_bp:n {#1} ~ w }
  }
\cs_new_protected:Npn \@@_draw_miterlimit:n #1
  { \@@_draw_literal:x { \fp_eval:n {#1} ~ M } }
\cs_new_protected_nopar:Npn \@@_draw_cap_butt:
  { \@@_draw_literal:n { 0 ~ J } }
\cs_new_protected_nopar:Npn \@@_draw_cap_round:
  { \@@_draw_literal:n { 1 ~ J } }
\cs_new_protected_nopar:Npn \@@_draw_cap_rectangle:
  { \@@_draw_literal:n { 2 ~ J } }
\cs_new_protected_nopar:Npn \@@_draw_join_miter:
  { \@@_draw_literal:n { 0 ~ j } }
\cs_new_protected_nopar:Npn \@@_draw_join_round:
  { \@@_draw_literal:n { 1 ~ j } }
\cs_new_protected_nopar:Npn \@@_draw_join_bevel:
  { \@@_draw_literal:n { 2 ~ j } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[int]
%   {\@@_draw_color_cmyk_fill:nnnn, \@@_draw_color_cmyk_stroke:nnnn}
% \begin{macro}[int]
%   {\@@_draw_color_gray_fill:n, \@@_draw_color_gray_stroke:n}
% \begin{macro}[int]
%   {\@@_draw_color_rgb_fill:nnn, \@@_draw_color_rgb_stroke:nnn}
%   Yet more fast conversion, all using the FPU to allow for expressions
%   in numerical input.
%     \begin{macrocode}
\cs_new_protected:Npn \@@_draw_color_cmyk_fill:nnnn #1#2#3#4
  {
    \@@_draw_literal:x
      {
        \fp_eval:n {#1} ~ \fp_eval:n {#2} ~
        \fp_eval:n {#3} ~ \fp_eval:n {#4} ~
        k
      }
  }
\cs_new_protected:Npn \@@_draw_color_cmyk_stroke:nnnn #1#2#3#4
  {
    \@@_draw_literal:x
      {
        \fp_eval:n {#1} ~ \fp_eval:n {#2} ~
        \fp_eval:n {#3} ~ \fp_eval:n {#4} ~
        K
      }
  }
\cs_new_protected:Npn \@@_draw_color_gray_fill:n #1
  { \@@_draw_literal:x { \fp_eval:n {#1} ~ g } }
\cs_new_protected:Npn \@@_draw_color_gray_stroke:n #1
  { \@@_draw_literal:x { \fp_eval:n {#1} ~ G } }
\cs_new_protected:Npn \@@_draw_color_rgb_fill:nnn #1#2#3
  {
    \@@_draw_literal:x
      { \fp_eval:n {#1} ~ \fp_eval:n {#2} ~ \fp_eval:n {#3} ~ rg }
  }
\cs_new_protected:Npn \@@_draw_color_rgb_stroke:nnn #1#2#3
  {
    \@@_draw_literal:x
      { \fp_eval:n {#1} ~ \fp_eval:n {#2} ~ \fp_eval:n {#3} ~ RG }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[int]{\@@_draw_transformcm:nnnnnn}
%   The first four arguments here are floats (the affine matrix), the last
%   two are a displacement vector. Once again, force evaluation to allow for
%   caching.
%     \begin{macrocode}
\cs_new_protected:Npn \@@_draw_transformcm:nnnnnn #1#2#3#4#5#6
  {
    \@@_draw_literal:x
      {
        \fp_eval:n {#1} ~ \fp_eval:n {#2} ~
        \fp_eval:n {#3} ~ \fp_eval:n {#4} ~
        \dim_to_decimal_in_bp:n {#5} ~ \dim_to_decimal_in_bp:n {#6} ~
        cm
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_draw_hbox:Nnnnnnn}
% \begin{variable}[aux]{\l_@@_tmp_box}
%   Inserting a \TeX{} box transformed to the requested position and using
%   the current matrix is done using a mixture of \TeX{} and low-level
%   manipulation. The offset can be handled by \TeX{}, so only any rotation/^^A
%   skew/scaling component needs to be done using the matrix operation. As this
%   operation can never be cached, the scope is set directly not using the
%   \texttt{draw} version.
%     \begin{macrocode}
\cs_new_protected:Npn \@@_draw_hbox:Nnnnnnn #1#2#3#4#5#6#7
  {
    \hbox_set:Nn \l_@@_tmp_box
      {
        \tex_kern:D \__dim_eval:w #6 \__dim_eval_end:
        \@@_scope_begin:
        \@@_draw_transformcm:nnnnnn {#2} {#3} {#4} {#5}
          { 0pt } { 0pt }
        \box_move_up:nn {#7} { \box_use:N #1 }
        \@@_scope_end:
      }
    \box_set_wd:Nn \l_@@_tmp_box { 0pt }
    \box_set_ht:Nn \l_@@_tmp_box { 0pt }
    \box_set_dp:Nn \l_@@_tmp_box { 0pt }
    \box_use:N \l_@@_tmp_box
  }
\box_new:N \l_@@_tmp_box
%    \end{macrocode}
% \end{variable}
% \end{macro}
%
%    \begin{macrocode}
%</dvipdfmx|pdfmode|xdvipdfmx>
%    \end{macrocode}
%
% \subsection{\texttt{dvips} driver}
%
%    \begin{macrocode}
%<*dvips>
%    \end{macrocode}
%
% \subsubsection{Basics}
%
% \begin{macro}[int]{\@@_literal:n}
%   In the case of \texttt{dvips} there is no build-in saving of the current
%   position, and so some additional PostScript is required to set up the
%   transformation matrix and also to restore it afterwards. Notice the use
%   of the stack to save the current position \enquote{up front} and to
%   move back to it at the end of the process.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_literal:n #1
  {
    \tex_special:D
      {
        ps:
          currentpoint~
          currentpoint~translate~
          #1 ~
          neg~exch~neg~exch~translate
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_scope_begin:, \@@_scope_end:}
%   Scope saving/restoring is done directly with no need to worry about the
%   transformation matrix.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_scope_begin:
  { \tex_special:D { ps:gsave } }
\cs_new_protected_nopar:Npn \@@_scope_end:
  { \tex_special:D { ps:grestore } }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Driver-specific auxiliaries}
%
% \begin{macro}[int, EXP]{\@@_absolute_lengths:n}
%   The \texttt{dvips} driver scales all absolute dimensions based
%   on the output resolution selected and any \TeX{} magnification. Thus
%   for any operation involving absolute lengths there is a correction to
%   make. This is based on \texttt{normalscale} from \texttt{special.pro}
%   but using the stack rather than a definition to save the current matrix.
%    \begin{macrocode}
\cs_new:Npn \@@_absolute_lengths:n #1
  {
     matrix~currentmatrix~
     Resolution~72~div~VResolution~72~div~scale~
     DVImag~dup~scale~
     #1 ~
     setmatrix
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Box operations}
%
% \begin{macro}{\@@_box_use_clip:N}
%   Much the same idea as for the PDF mode version but with a slightly
%   different syntax for creating the clip path. To avoid any scaling
%   issues we need the absolute length auxiliary here.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_box_use_clip:N #1
  {
    \@@_scope_begin:
    \@@_literal:n
      {
        \@@_absolute_lengths:n
          {
            0 ~
            \dim_to_decimal_in_bp:n { \box_dp:N #1 } ~
            \dim_to_decimal_in_bp:n { \box_wd:N #1 } ~
            \dim_to_decimal_in_bp:n { -\box_ht:N #1 - \box_dp:N #1 } ~
            rectclip
          }
      }
    \hbox_overlap_right:n { \box_use:N #1 }
    \@@_scope_end:
    \skip_horizontal:n { \box_wd:N #1 }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_box_use_rotate:Nn}
%   Rotating using \texttt{dvips} does not require that the box dimensions
%   are altered and has a very convenient built-in operation. Zero rotation
%   must be written as |0| not |-0| so there is a quick test.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_box_use_rotate:Nn #1#2
  {
    \@@_scope_begin:
    \@@_literal:n
      {
        \fp_compare:nNnTF {#2} = \c_zero_fp
          { 0 }
          { \fp_eval:n { round ( -#2 , 5 ) } } ~
        rotate
      }
   \box_use:N #1
   \@@_scope_end:
  }
% \end{macro}
%
% \begin{macro}{\@@_box_use_scale:Nnn}
%   The \texttt{dvips} driver once again has a dedicated operation we can
%   use here.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_box_use_scale:Nnn #1#2#3
  {
    \@@_scope_begin:
    \@@_literal:n
      {
        \fp_eval:n { round ( #2 , 5 ) } ~
        \fp_eval:n { round ( #3 , 5 ) } ~
        scale
      }
    \hbox_overlap_right:n { \box_use:N #1 }
    \@@_scope_end:
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Color}
%
% \begin{variable}{\l_@@_current_color_tl}
%   The current color in driver-dependent format.
%    \begin{macrocode}
\tl_new:N \l_@@_current_color_tl
\tl_set:Nn \l_@@_current_color_tl { gray~0 }
%<*package>
\AtBeginDocument
  {
    \@ifpackageloaded { color }
      { \tl_set:Nn \l_@@_current_color_tl { \current@color } }
      { }
  }
%</package>
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}[int]{\@@_color_ensure_current:}
% \begin{macro}[aux]{\@@_color_reset:}
%   Directly set the color using the specials: no optimisation here.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_color_ensure_current:
  {
    \tex_special:D { color~push~\l_@@_current_color_tl }
    \group_insert_after:N \@@_color_reset:
  }
\cs_new_protected_nopar:Npn \@@_color_reset:
  { \tex_special:D { color~pop } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
%</dvips>
%    \end{macrocode}
%
% \subsection{\texttt{dvisvgm} driver}
%
%    \begin{macrocode}
%<*dvisvgm>
%    \end{macrocode}
%
% \subsubsection{Basics}
%
% \begin{macro}[int]{\@@_literal:n}
%   Unlike the other drivers, the requirements for making SVG files mean
%   that we can't conveniently transform all operations to the current point.
%   That makes life a bit more tricky later as that needs to be accounted for.
%   A new line is added after each call to help to keep the output readable
%   for debugging.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_literal:n #1
  { \tex_special:D { dvisvgm:raw~ #1 { ?nl } } }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[int]{\@@_scope_begin:, \@@_scope_end:}
%   A scope in SVG terms is slightly different to the other drivers as
%   operations have to be \enquote{tied} to these not simply inside them.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_scope_begin:
  { \@@_literal:n { <g> } }
\cs_new_protected_nopar:Npn \@@_scope_end:
  { \@@_literal:n { </g> } }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Driver-specific auxiliaries}
%
% \begin{macro}[int]{\@@_scope_begin:n}
%   In SVG transformations, clips and so on are attached directly to scopes so
%   we need a way or allowing for that. This is rather more useful that
%   \cs{@@_scope_begin:} as a result. No assumptions are made about the nature
%   of the scoped operation(s).
%    \begin{macrocode}
\cs_new_protected:Npn \@@_scope_begin:n #1
  { \@@_literal:n { <g~ #1 > } }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Box operations}
%
% \begin{macro}[int]{\@@_box_use_clip:N}
% \begin{variable}[aux]{\g_@@_clip_path_int}
%   Clipping in SVG is more involved than with other drivers. The first issue
%   is that the clipping path must be defined separately from where it is used,
%   so we need to track how many paths have applied. The naming here uses
%   \texttt{l3cp} as the namespace with a number following. Rather than use
%   a rectangular operation, we define the path manually as this allows it to
%   have a depth: easier than the alternative approach of shifting content
%   up and down using scopes to allow for the depth of the \TeX{} box and
%   keep the reference point the same!
%    \begin{macrocode}
\cs_new_protected:Npn \@@_box_use_clip:N #1
  {
    \int_gincr:N \g_@@_clip_path_int
    \@@_literal:n
      { < clipPath~id = " l3cp \int_use:N \g_@@_clip_path_int " > }
    \@@_literal:n
      {
        <
          path ~ d =
            "
              M ~ 0 ~
                  \dim_to_decimal:n { -\box_dp:N #1 } ~
              L ~ \dim_to_decimal:n { \box_wd:N #1 } ~
                  \dim_to_decimal:n { -\box_dp:N #1 } ~
              L ~ \dim_to_decimal:n { \box_wd:N #1 }  ~
                  \dim_to_decimal:n { \box_ht:N #1 + \box_dp:N #1 } ~
              L ~ 0 ~
                  \dim_to_decimal:n { \box_ht:N #1 + \box_dp:N #1 } ~
              Z
            "
        />
      }
    \@@_literal:n
      { < /clipPath > }
%    \end{macrocode}
%   In general the SVG set up does not try to transform coordinates to the
%   current point. For clipping we need to do that, so have a transformation
%   here to get us to the right place, and a matching one just before the
%   \TeX{} box is inserted to get things back on track. The clip path needs to
%   come between those two such that if lines up with the current point, as
%   does the \TeX{} box.
%    \begin{macrocode}
    \@@_scope_begin:n
      {
        transform =
          "
            translate ( { ?x } , { ?y } ) ~
            scale ( 1 , -1 )
          "
      }
    \@@_scope_begin:n
      {
        clip-path = "url ( \c_hash_str l3cp \int_use:N \g_@@_clip_path_int ) "
      }
    \@@_scope_begin:n
      {
        transform =
          "
            scale ( -1 , 1 ) ~
            translate ( { ?x } , { ?y } ) ~
            scale ( -1 , -1 )
          "
      }
    \box_use:N #1
    \@@_scope_end:
    \@@_scope_end:
    \@@_scope_end:
%    \skip_horizontal:n { \box_wd:N #1 }
  }
\int_new:N \g_@@_clip_path_int
%    \end{macrocode}
% \end{variable}
% \end{macro}
%
% \begin{macro}[int]{\@@_box_use_rotate:Nn}
%   Rotation has a dedicated operation which includes a centre-of-rotation
%   optional pair. That can be picked up from the driver syntax, so there is
%   no need to worry about the transformation matrix.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_box_use_rotate:Nn #1#2
  {
    \@@_scope_begin:n
      {
        transform =
          "
            rotate
            ( \fp_eval:n { round ( -#2 , 5 ) } , ~ { ?x } , ~ { ?y } )
          "
      }
    \box_use:N #1
    \@@_scope_end:
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_box_use_scale:Nnn}
%   In contrast to rotation, we have to account for the current position in this
%   case. That is done using a couple of translations in addition to the scaling
%   (which is therefore done backward with a flip).
%    \begin{macrocode}
\cs_new_protected:Npn \@@_box_use_scale:Nnn #1#2#3
  {
    \@@_scope_begin:n
      {
        transform =
          "
            translate ( { ?x } , { ?y } ) ~
            scale
              (
                \fp_eval:n { round ( -#2 , 5 ) } ,
                \fp_eval:n { round ( -#3 , 5 ) }
              ) ~
            translate ( { ?x } , { ?y } ) ~
            scale ( -1 )
          "
      }
    \hbox_overlap_right:n { \box_use:N #1 }
    \@@_scope_end:
  }
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{Color}
%
% \begin{variable}{\l_@@_current_color_tl}
%   The current color in driver-dependent format: the same as for
%   \texttt{dvips}.
%    \begin{macrocode}
\tl_new:N \l_@@_current_color_tl
\tl_set:Nn \l_@@_current_color_tl { gray~0 }
%<*package>
\AtBeginDocument
  {
    \@ifpackageloaded { color }
      { \tl_set:Nn \l_@@_current_color_tl { \current@color } }
      { }
  }
%</package>
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}[int]{\@@_color_ensure_current:}
% \begin{macro}[aux]{\@@_color_reset:}
%   Directly set the color: same as \texttt{dvips}.
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \@@_color_ensure_current:
  {
    \tex_special:D { color~push~\l_@@_current_color_tl }
    \group_insert_after:N \@@_color_reset:
  }
\cs_new_protected_nopar:Npn \@@_color_reset:
  { \tex_special:D { color~pop } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
%</dvisvgm>
%    \end{macrocode}
%
%    \begin{macrocode}
%</initex|package>
%    \end{macrocode}
%
% \end{implementation}
%
% \PrintIndex