-
Notifications
You must be signed in to change notification settings - Fork 0
/
flare-keyval.lua
355 lines (309 loc) · 9.49 KB
/
flare-keyval.lua
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
--
-- 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.
--
---
-- @classmod Page
local Page = {}
local pkg = require('flare-pkg')
local types = require('flare-types')
local pdfarray = types.pdfarray
local pdfdictionary = types.pdfdictionary
local luatex = require('flare-luatex')
--- Processing Keyvals
-- @section processing_keyvals
--
-- The following functions process a keyval string representing optional
-- arguments of `\includegraphics`. The string originates from LaTeX and
-- should have the following form:
--
-- {{key = 'scale', val = '0.5'}, {key = 'replaceC', val = '[1 1 0]'}, ...}
--
-- After processing the string these keyvals are stored in two tables:
--
-- * `Page.FlareKV`: options of Flare
-- * `Page.GinKV`: options of graphicx
--- Processes string `kv_str` and creates tables `Page.FlareKV` and `Page.GinKV`.
-- String `kv_str` represents optional arguments of `\includegraphics`.
-- @string kv_str Keyval string
function Page:processKeyvals(kv_str)
local status, kv = pcall(
function ()
return loadstring('return' .. kv_str)()
end)
if not status then
-- internal error
pkg.error("Argument 'kv_str' must be a string representing a Lua table")
end
-- processs keyvals in reversed order
if kv then
for idx = #kv, 1, -1 do
local key = kv[idx].key
local val = kv[idx].val
if self:isFlareKey(key) then
local key, page, id, op = self:splitFlareKey(key)
self:setFlareKV(page, id, key, op, val)
else
self:setGinKV(key, val)
end
end
end
self:setDefaultGinKVs()
end
--- Non-strict setter function for `Page.FlareKV`.
-- A key is added only if it is not mutually exclusive to already
-- existing keys in table `self.FlareKV`. The truth table below
-- reflects whether or not to add a new key.
--
-- Note that keys are processed in reverse order. See @{Page:processKeyvals}.
--
-- ─────────────────────────────────────
-- new_key existing_key
-- ───────────────────────────
-- repl rmT rmF scale 0
-- ─────────────────────────────────────
-- repl ✗ ✗ ✓ ✗ ✓
-- ref ✗ ✗ ✓ ✗ ✓
-- scale ✗ ✗ ✓ ✗ ✓
-- rmT ✗ ✗ ✗ ✗ ✓
-- rmF ✗ ✗ ✗ ✗ ✓
-- ─────────────────────────────────────
--
-- repl ... replace<key>
-- scale ... scale<key>
-- rmT ... remove<key> = true
-- rmF ... remove<key> = false
-- 0 ... none of the keys exists
--
-- @number page Page.
-- @number annotId Annotation ID
-- @string key Key.
-- @string op Operation
-- @strbool val Value.
function Page:setFlareKV(page, annotId, key, op, val)
local val_old, op_old = self:getFlareKV(page, annotId, key)
if op_old == nil then
self:setFlareKVStrict(page, annotId, key, op, val)
elseif (op == 'replace' or
op == 'ref' or
op == 'scale') and
op_old == 'remove' and val_old == false then
self:setFlareKVStrict(page, annotId, key, op, val)
end
end
--- Setter function for `Page.FlareKV`.
-- @numstr page Page
-- @numstr annotId Annotation Id
-- @string key Key
-- @string op Operation
-- @param val Value
function Page:setFlareKVStrict(page, annotId, key, op, val)
if key == '' then
key = '@@@'
end
self.FlareKV[page] = self.FlareKV[page] or {}
self.FlareKV[page][annotId] = self.FlareKV[page][annotId] or {}
self.FlareKV[page][annotId][key] = {op = op,
val = self:makeBoolean(self:trim(val))}
end
--- Non-strict getter function for `Page.FlareKV`.
-- Does a non-strict search. Replaces `page` and `annotId` with `'all'` if
-- a strict search retrieved `nil`.
-- @number page Page
-- @numstr annotId Annotation Id
-- @string key Key
-- @return Value
-- @return Operation
function Page:getFlareKV(page, annotId, key)
local op, val
val, op = self:getFlareKVStrict(page, annotId, key)
if val ~= nil then
return val, op
else
val, op = self:getFlareKVStrict(page, 'all', key)
if val ~= nil then
return val, op
else
val, op = self:getFlareKVStrict('all', 'all', key)
return val, op
end
end
end
--- Getter function for `Page.FlareKV`.
-- @number page Page
-- @string annotId Annotation Id
-- @string key Key
-- @return Value
-- @return Operation
function Page:getFlareKVStrict(page, annotId, key)
if self.FlareKV[page] ~= nil and
self.FlareKV[page][annotId] ~= nil and
self.FlareKV[page][annotId][key] ~= nil then
return self.FlareKV[page][annotId][key]['val'],
self.FlareKV[page][annotId][key]['op']
else
return nil, nil
end
end
--- Checks if `key` is a Flare-key.
-- @string key Key
-- @return Boolean
function Page:isFlareKey(key)
if key:find('^flare') then
return true
else
return false
end
end
--- Splits a compound key name as deliverd by LaTeX into separate parts.
--
-- A compound key consists of a prefix, an operation string, a raw key
-- name, a page number, and an annotation ID.
-- For example the compound key name `annotReplaceFoo@3!1`
-- is split into:
--
-- * prefix: `annot`
-- * operation: `replace`
-- * key: `Foo`
-- * page number: `3`
-- * annot id: `1`
--
-- @string key Key
-- @return Raw key name
-- @return Page number or `all`
-- @return Annotation ID or `all`
-- @return Operation
function Page:splitFlareKey(key)
key = self:removeFlareKeyPrefix(key)
key = key:sub(1, 1):lower() .. key:sub(2)
local op, key = self:getFlareKeyOperation(key)
local page = self:getFlareKeyPage(key)
local id = self:getFlareKeyId(key)
key = self:removeFlareKeyPageId(key)
return key, page, id, op
end
--- Returns operation of compound key name.
-- @string key Key
-- @return Operation
function Page:getFlareKeyOperation(key)
for _, op in pairs({'replace', 'ref', 'remove', 'scale'}) do
local _, idx = key:find('^' .. op)
if idx then
return op, key:sub(#op + 1)
end
end
return nil, key
end
--- Returns page of compound key name.
-- @string key Key
-- @return Page
function Page:getFlareKeyPage(key)
local _, _, page = key:find('.*(@%d+)')
if page then
return tonumber(page:sub(2))
else
return 'all'
end
end
--- Returns annotation ID of coumpound key name.
-- @string key Key
-- @return Annot id.
function Page:getFlareKeyId(key)
local _, _, id = key:find('.*(!%d+)')
if id then
return tonumber(id:sub(2))
else
return 'all'
end
end
--- Removes prefix `annot` of compound key name.
-- @string key Key
function Page:removeFlareKeyPrefix(key)
return key:sub(6)
end
--- Removes prefixes for page and annotation id.
-- @string key Key
-- @return Raw key name
function Page:removeFlareKeyPageId(key)
idx1 = key:find('@%d+')
idx2 = key:find('!%d+')
if idx1 and idx2 then
if idx1 < idx2 then
return key:sub(0, idx1 - 1)
else
return key:sub(0, idx2 - 1)
end
elseif idx1 then
return key:sub(0, idx1 - 1)
elseif idx2 then
return key:sub(0, idx2 - 1)
else
return key
end
end
--- Setter function for `Page.GinKV`.
-- This function is called once for each non-Flare keyval of `\includegraphics`.
-- If it is called with the same `key` several times, it depdends on `key`
-- whether the first or the last keyval is set.
-- @string key Key
-- @string val Value
function Page:setGinKV(key, val)
-- Note that processing of keyvals is still in reverse order,
-- see processKeyvals().
-- last one wins
if key == 'filename' and not self.GinKV.filename then
self.GinKV.filename = val
elseif key == 'page' and not self.GinKV.page then
self.GinKV.page = self:checkNumber('page', val)
elseif key == 'userpassword' and not self.GinKV.userpassword then
self.GinKV.userpassword = val
end
-- first one wins
if key == 'scale' then
self.GinKV.scale = self:checkNumber('scale', val)
end
end
--- Sets default keyvals of `\includegraphics` in table `Page.GinKV`.
function Page:setDefaultGinKVs()
if not self.GinKV.page then
self.GinKV.page = 1
end
end
--- Auxiliary
-- @section auxiliary
--- Converts strings `true` and `false` into boolean values.
-- @string str String
-- @return String
function Page:makeBoolean(str)
if str == 'true' then
return true
elseif str == 'false' then
return false
end
return str
end
--- Checks, if string `val` can be converted into a number.
-- Returns the number or throws an error.
-- @string key Key
-- @string val Value
-- @return Number
function Page:checkNumber(key, val)
num = tonumber(val)
if num then
return num
else
pkg.error("Invalid number '%s' for parameter '%s'", val, key)
end
end
return Page