-
Notifications
You must be signed in to change notification settings - Fork 0
/
flare.sty
147 lines (120 loc) · 3.4 KB
/
flare.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
%%
%% Copyright 2021-2023 Andreas MATTHIAS
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%% http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2008 or later.
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% The Current Maintainer of this work is Andreas MATTHIAS.
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{flare}[2023/02/19 v0.1 flare (AM)]%
\RequirePackage{l3keys2e}
\RequirePackage{luatexbase}
\RequirePackage{graphicx}
\RequirePackage{kvsetkeys}
\RequirePackage{etoolbox}
%\tracingpatches
\ExplSyntaxOn
\bool_new:N \l__FLR_show_dirty_cache_bool
\keys_define:nn { flare }
{
showDirtyCache .bool_set:N = \l__FLR_show_dirty_cache_bool,
}
\ProcessKeysOptions { flare }
\directlua{
flare = {}
flare.options = {}
}
\bool_if:NT \l__FLR_show_dirty_cache_bool {
\directlua{flare.options.showDirtyCache = true}
}
\directlua{
flare = require('flare')
}
\clist_new:N \l_FLR_Gin_kvlist
\clist_new:N\l_FLR_kv_clist
\cs_new:Npn \FLR_kv_one:nn #1 {
\clist_put_right:Nn \l_FLR_kv_clist {{{ key = '#1', val = true }}}
}
\cs_new:Npn \FLR_kv_two:nn #1#2 {
\clist_put_right:Nn \l_FLR_kv_clist {{{ key = '#1', val = '#2' }}}
}
\cs_generate_variant:Nn \FLR_kv_two:nn { nx }
\cs_new:Npn \FLR_setkeys:nn #1#2 {
% perpare keyvals for Flare
\clist_clear:N \l_FLR_kv_clist
\keyval_parse:NNn \FLR_kv_one:nn \FLR_kv_two:nn { #1 }
\FLR_kv_two:nx { filename }{ #2 }
% prepare keyvals for Gin
\clist_map_inline:nn { #1 }{
\regex_match:nnF { ^flare }{ ##1 }{
\clist_put_right:Nn \l_FLR_Gin_kvlist { ##1 }
}
}
\exp_args:NnV \setkeys { Gin }{ \l_FLR_Gin_kvlist }
}
\AddToHook{begindocument}{
\directlua{
doc = flare.Doc:new()
doc:loadCache()
}
}
\AddToHook{enddocument/afterlastpage}{
\directlua{
doc:saveCache()
doc:warnIfCacheDirty()
}
}
\cs_new:Nn \FLR_CopyAnnotations: {
\directlua{
page = flare.Page:new(doc)
page:processKeyvals("{\clist_use:Nnnn \l_FLR_kv_clist {,} {,} {,}}")
page:openFile()
page:copyAnnots()
page:copyDests()
tex.print("\string\\latelua{doc:cacheData(" .. page.pictureId ..")}")
}
}
\msg_new:nnn { Flare }{ missing-pdfmanagement }
{Please~ load~ package~ pdfmanagement.~ Widget~ annotations~
will~ not~ work~ correctly~ without~ pdfmanagement.}
\cs_new:Nn \FLR_AddFieldToAcroForm:n {
\cs_if_exist_use:NTF\pdfmanagement_add:nnn
{{Catalog/AcroForm}{Fields}{#1~ 0~ R}}
{\msg_error:nn { Flare }{missing-pdfmanagement}}
}
\msg_new:nnn { Flare }{ patching }{ Patching~ #1.}{}
\msg_new:nnn { Flare }{ patchingfailed }{ Paching~ of~ #1 failed.}{}
%
% Turning off ExplSyntax because different catcodes would
% confuse /patchcmd. But first we rename some commands needed
% for /patchcmd.
%
\let\FLR@msginfo\msg_info:nnn
\let\FLR@msgerror\msg_error:nnn
\let\FLR@setkeys\FLR_setkeys:nn
\let\FLR@CopyAnnotations\FLR_CopyAnnotations:
\ExplSyntaxOff
\pretocmd{\Gread@pdftex}{%
\FLR@CopyAnnotations
}{%
\FLR@msginfo{Flare}{patching}{\Gread@pdftex}%
}{%
\FLR@msgerror{Flare}{patchingfailed}{\Gread@pdftex}%
}
\patchcmd{\Gin@ii}{%
\setkeys{Gin}{#1}%
}{%
\FLR@setkeys{#1}{#2}%
}{%
\FLR@msginfo{Flare}{patching}{\Gin@ii}%
}{%
\FLR@msgerror{Flare}{patchingfailed}{\Gin@ii}%
}