summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/tagpdf/tagpdf-luatex.def
blob: 5cb16c5216c276908aa9f563e4c4a0e6a575e239 (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
\ProvidesExplFile {tagpdf-luatex.def} {2019/07/02} {0.61}
  {tagpdf~driver~for~luatex}

%\newattribute \g__uftag_mc_type_attr     %the value represent the type
%\newattribute \g__uftag_mc_cnt_attr      %will hold the \c@g__uftag_MCID_abs_int value

\newattribute \l__uftag_mc_type_attr     %the value represent the type
\newattribute \l__uftag_mc_cnt_attr      %will hold the \c@g__uftag_MCID_abs_int value

\newattribute \g__uftag_interwordspace_attr
\newattribute \g__uftag_interwordfont_attr

% The lua code
% ensure that 10pt font has been loaded:
{
 \fontencoding{TU}\fontfamily{lmr}\fontseries{m}\fontshape{n}\fontsize{10pt}{10pt}\selectfont
}
\directlua { tagpdf=require('tagpdf.lua') }

%%%% driver (lualatex) commands
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% these should be in expl3!!!
\cs_new:Nn \__uftag_pdf_startlink:{ \pdfextension startlink }
\cs_new:Nn \__uftag_pdf_endlink:  { \pdfextension endlink }
\cs_new_protected:Nn \__uftag_pdf_lastlink: { \numexpr\pdffeedback lastlink\relax }
\cs_new:Nn \__uftag_pdf_catalog:  { \pdfextension catalog }

% needed for \str_set_convert:Nnon
\tl_new:N   \g__uftag_inputencoding_tl
\tl_gset:Nn \g__uftag_inputencoding_tl {}


% literals
\cs_new:Nn \__uftag_pdfliteral_page:n {\__uftag_tex_pdfextension:D literal~page {#1}}
\cs_new:Nn \__uftag_pdfcatalog:n      {\__uftag_tex_pdfextension:D catalog {#1}}

%reserve an object num
\cs_new:Nn \__uftag_pdfreserveobjnum:N
 {
  % #1 = macro name to be populated with object number
  \__uftag_tex_pdfextension:D~obj~reserveobjnum
  \tl_set:Nx #1 { \__uftag_tex_pdffeedback:D lastobj }%
 }

% use an object num
\cs_new:Nn \__uftag_pdfuseobjnum:Nn
 {
 % #1 = macro with object number to be populated
 % #2 = object contents, as valid PDF
 % should #2 be filtered through  \pdfstringdef  ???
  \__uftag_tex_immediate:D \__uftag_tex_pdfextension:D~obj~useobjnum~#1~{ #2 }%
 }

\cs_new:Nn \__uftag_pdfuseobjnum:nn
 {
 % #1 = a number
 % #2 = object contents, as valid PDF
 % should #2 be filtered through  \pdfstringdef  ???
  \__uftag_tex_immediate:D \__uftag_tex_pdfextension:D~obj~useobjnum~#1~{ #2 }%
 }

% obj num of the dictionary for a page:
% the page count starts by 1
% pages can referenced before they are actually created
% no error if the page later doesn't exist

\cs_new:Nn \__uftag_store_pdfpageref:Nn
 {
 % #1 = macro name to be populated with current page object number
 % #2 = number or counter identifying the required page
  \tl_set:Nx #1 { \__uftag_tex_pdffeedback:D pageref~#2 }%
 }

% a global version of the command
\cs_new:Nn \__uftag_gstore_pdfpageref:Nn
 {
  \tl_gset:Nx #1 { \__uftag_tex_pdffeedback:D pageref~#2 }%
 }

\cs_new:Nn \__uftag_pdfobj:Nn
 {
 % #1 = macro name to refer to this object
 % #2 = object contents, as valid PDF
 % should #2 be filtered through  \pdfstringdef  ???
  \__uftag_tex_immediate:D \__uftag_tex_pdfextension:D~obj~{ #2 }
  \tl_set:Nx #1 { \__uftag_tex_pdffeedback:D~lastobj }%
 }

%  pdfpage**s**attr: for all pages
%% is global needed? Yes. Without it the setting is lost if issued in a group
%
\cs_new:Nn \__uftag_gset_pdfpagesattr:n
 {
  \__uftag_tex_global:D \__uftag_tex_pdfvariable:D~pagesattr { #1 }
 }

\cs_new:Nn \__uftag_gadd_pdfpagesattr:n
 {
  \exp_args:No \__uftag_gset_pdfpagesattr:n { \__uftag_tex_the:D \__uftag_tex_pdfvariable:D~pagesattr #1}
 }

% pdfpageattr for one page
% do we need an immediate version??
\cs_new:Nn \__uftag_gset_pdfpageattr:n
 {
  \__uftag_tex_global:D \__uftag_tex_pdfvariable:D~pageattr  { #1 }
 }

\cs_new:Nn \__uftag_gadd_pdfpageattr:n
 {
  \exp_args:No \__uftag_gset_pdfpageattr:n { \__uftag_tex_the:D \__uftag_tex_pdfvariable:D~pageattr #1}
 }

\cs_new:Nn \__uftag_get_pdfpageattr:N
 {
  \tl_set:No #1 { \__uftag_tex_the:D \__uftag_tex_pdfvariable:D~pageattr }
 }


% I probably want also lua tables
% I put them in the uftag.tables namespaces
% The tables will be named like the variables but without backslash
% To access such a table with a dynamical name create a string and then use
% uftag.tables[string]
% Old code, I'm not quite sure if this was a good idea. Now I have mix of table in
% utftag.tables and uftag.mc/struct. And a lot is probably not needed.

\cs_new:Nn \__uftag_luatex_get_table_name:Nn
 {
  \tl_set_rescan:Nnn #1  { \char_set_catcode_ignore:N \\ } { #2 }
 }


\cs_new:Nn \__uftag_prop_new:N
 {
  \prop_new:N #1
  \__uftag_luatex_get_table_name:Nn \l_tmpa_tl { #1 }
  \directlua { uftag.tables.\l_tmpa_tl = {} }
 }


\cs_new:Nn \__uftag_seq_new:N
 {
  \seq_new:N #1
  \__uftag_luatex_get_table_name:Nn \l_tmpa_tl { #1 }
  \directlua { uftag.tables.\l_tmpa_tl = {} }
 }


\cs_new:Nn \__uftag_prop_gput:Nnn
 {
  \prop_gput:Nnn #1 { #2 } { #3 }
  \__uftag_luatex_get_table_name:Nn \l_tmpa_tl { #1 }
  \directlua { uftag.tables.\l_tmpa_tl["#2"] = "#3" }
 }


\cs_new:Nn \__uftag_seq_gput_right:Nn
 {
  \seq_gput_right:Nn #1 { #2 }
  \__uftag_luatex_get_table_name:Nn \l_tmpa_tl { #1 }
  \directlua { table.insert(uftag.tables.\l_tmpa_tl, "#2") }
 }

%Hm not quite sure about the naming

\cs_new:Npn \__uftag_seq_item:cn #1 #2
 {
  \directlua { tex.print(uftag.tables.#1[#2]) }
 }

\cs_new:Npn \__uftag_prop_item:cn #1 #2
 {
  \directlua { tex.print(uftag.tables.#1["#2"]) }
 }

%for debugging commands that show both the seq/prop and the lua tables
\cs_new:Nn \__uftag_seq_show:N
 {
   \seq_show:N #1
   \__uftag_luatex_get_table_name:Nn \l_tmpa_tl { #1 }
   \directlua { uftag.trace.log ("lua~sequence~array~\l_tmpa_tl",1) }
   \directlua { uftag.trace.show_seq (uftag.tables.\l_tmpa_tl) }
 }

\cs_new:Nn \__uftag_prop_show:N
 {
   \prop_show:N #1
   \__uftag_luatex_get_table_name:Nn \l_tmpa_tl { #1 }
   \directlua {uftag.trace.log  ("lua~property~table~\l_tmpa_tl",1) }
   \directlua {uftag.trace.show_prop (uftag.tables.\l_tmpa_tl) }
 }


\endinput