grph-con.lua /size: 14 Kb    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
grph-con
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to grph-inc.mkiv
"
,
4
author
=
"
Hans Hagen, PRAGMA-ADE, Hasselt NL
"
,
5
copyright
=
"
PRAGMA ADE / ConTeXt Development Team
"
,
6
license
=
"
see context related readme files
"
7
}
8 9
local
P
,
R
,
S
,
Cc
,
C
,
Cs
,
Ct
,
lpegmatch
=
lpeg
.
P
,
lpeg
.
R
,
lpeg
.
S
,
lpeg
.
Cc
,
lpeg
.
C
,
lpeg
.
Cs
,
lpeg
.
Ct
,
lpeg
.
match
10 11
local
tonumber
=
tonumber
12
local
find
=
string
.
find
13
local
longtostring
=
string
.
longtostring
14
local
formatters
=
string
.
formatters
15
local
expandfilename
=
dir
.
expandname
16
local
isfile
=
lfs
.
isfile
17 18
local
settings_to_array
=
utilities
.
parsers
.
settings_to_array
19
local
settings_to_hash
=
utilities
.
parsers
.
settings_to_hash
20
local
allocate
=
utilities
.
storage
.
allocate
21
local
setmetatableindex
=
table
.
setmetatableindex
22 23
local
codeinjections
=
backends
.
codeinjections
24
local
nodeinjections
=
backends
.
nodeinjections
25 26
local
report_figures
=
logs
.
reporter
(
"
system
"
,
"
graphics
"
)
27 28
local
variables
=
interfaces
.
variables
29
local
v_high
=
variables
.
high
30
local
v_low
=
variables
.
low
31
local
v_medium
=
variables
.
medium
32 33
local
figures
=
figures
34 35
local
converters
=
figures
.
converters
36
local
programs
=
figures
.
programs
37 38
local
runprogram
=
programs
.
run
39 40
do
-- eps | ps
41 42
-- \externalfigure[cow.eps]
43
-- \externalfigure[cow.pdf][conversion=stripped]
44 45
-- todo: colorspace
46
-- todo: lowres
47 48
local
epsconverter
=
converters
.
eps
49
converters
.
ps
=
epsconverter
50 51
local
resolutions
=
{
52
[
v_low
]
=
"
screen
"
,
53
[
v_medium
]
=
"
ebook
"
,
54
[
v_high
]
=
"
prepress
"
,
55
}
56 57
local
runner
=
sandbox
.
registerrunner
{
58
name
=
"
eps to pdf
"
,
59
program
=
{
60
windows
=
os
.
platform
=
=
"
win64
"
and
"
gswin64c
"
or
"
gswin32c
"
,
61
unix
=
"
gs
"
,
62
}
,
63
template
=
longtostring
[[
64 -q 65 -sDEVICE=pdfwrite 66 -dNOPAUSE 67 -dNOCACHE 68 -dBATCH 69 -dAutoRotatePages=/None 70 -dPDFSETTINGS=/%presets% 71 -dEPSCrop 72 -dCompatibilityLevel=%level% 73 -sOutputFile=%newname% 74 %colorspace% 75 %oldname% 76 -c quit 77
]]
,
78
checkers
=
{
79
oldname
=
"
readable
"
,
80
newname
=
"
writable
"
,
81
presets
=
"
string
"
,
82
level
=
"
string
"
,
83
colorspace
=
"
string
"
,
84
}
,
85
}
86 87
programs
.
epstopdf
=
{
resolutions
=
epstopdf
,
runner
=
runner
}
88
programs
.
gs
=
programs
.
epstopdf
89 90
local
cleanups
=
{
}
91
local
cleaners
=
{
}
92 93
local
whitespace
=
lpeg
.
patterns
.
whitespace
94
local
quadruple
=
Ct
(
(
whitespace
^
0
*
lpeg
.
patterns
.
number
/
tonumber
*
whitespace
^
0
)
^
4
)
95
local
betterbox
=
P
(
"
%%BoundingBox:
"
)
*
quadruple
96
*
P
(
"
%%HiResBoundingBox:
"
)
*
quadruple
97
*
P
(
"
%AI3_Cropmarks:
"
)
*
quadruple
98
*
P
(
"
%%CropBox:
"
)
*
quadruple
99
/
function
(
b
,
h
,
m
,
c
)
100
return
formatters
[
"
%%%%BoundingBox: %r %r %r %r\n%%%%HiResBoundingBox: %F %F %F %F\n%%%%CropBox: %F %F %F %F\n
"
]
(
101
m
[
1
]
,
m
[
2
]
,
m
[
3
]
,
m
[
4
]
,
-- rounded integer
102
m
[
1
]
,
m
[
2
]
,
m
[
3
]
,
m
[
4
]
,
-- real number
103
m
[
1
]
,
m
[
2
]
,
m
[
3
]
,
m
[
4
]
104
)
105
end
106
local
nocrap
=
P
(
"
%
"
)
/
"
"
*
(
107
(
P
(
"
AI9_PrivateDataBegin
"
)
*
P
(
1
)
^
0
)
/
"
%%%%EOF
"
108
+
(
P
(
"
%EOF
"
)
*
whitespace
^
0
*
P
(
"
%AI9_PrintingDataEnd
"
)
*
P
(
1
)
^
0
)
/
"
%%%%EOF
"
109
+
(
P
(
"
AI7_Thumbnail
"
)
*
(
1
-
P
(
"
%%EndData
"
)
)
^
0
*
P
(
"
%%EndData
"
)
)
/
"
"
110
)
111
local
whatever
=
nocrap
+
P
(
1
)
112
local
pattern
=
Cs
(
(
betterbox
*
whatever
^
1
+
whatever
)
^
1
)
113 114
directives
.
register
(
"
graphics.conversion.eps.cleanup.ai
"
,
function
(
v
)
cleanups
.
ai
=
v
end
)
115 116
cleaners
.
ai
=
function
(
name
)
117
local
tmpname
=
name
.
.
"
.tmp
"
118
io
.
savedata
(
tmpname
,
lpegmatch
(
pattern
,
io
.
loaddata
(
name
)
or
"
"
)
)
119
return
tmpname
120
end
121 122
function
epsconverter
.
pdf
(
oldname
,
newname
,
resolution
,
colorspace
)
-- the resolution interface might change
123
local
presets
=
resolutions
[
resolution
or
"
high
"
]
or
resolutions
.
high
124
local
level
=
codeinjections
.
getformatoption
(
"
pdf_level
"
)
or
"
1.3
"
125
local
tmpname
=
oldname
126
if
not
tmpname
or
tmpname
=
=
"
"
or
not
isfile
(
tmpname
)
then
127
return
128
end
129
if
cleanups
.
ai
then
130
tmpname
=
cleaners
.
ai
(
oldname
)
131
end
132
if
colorspace
=
=
"
gray
"
then
133
colorspace
=
"
-sColorConversionStrategy=Gray -sProcessColorModel=DeviceGray
"
134
-- colorspace = "-sColorConversionStrategy=Gray"
135
else
136
colorspace
=
nil
137
end
138
runner
{
139
newname
=
newname
,
140
oldname
=
tmpname
,
141
presets
=
presets
,
142
level
=
tostring
(
level
)
,
143
colorspace
=
colorspace
,
144
}
145
if
tmpname
~
=
oldname
then
146
os
.
remove
(
tmpname
)
147
end
148
end
149 150
epsconverter
[
"
gray.pdf
"
]
=
function
(
oldname
,
newname
,
resolution
)
-- the resolution interface might change
151
epsconverter
.
pdf
(
oldname
,
newname
,
resolution
,
"
gray
"
)
152
end
153 154
epsconverter
.
default
=
epsconverter
.
pdf
155 156
end
157 158
-- do -- pdf
159
--
160
-- local pdfconverter = converters.pdf
161
--
162
-- programs.pdftoeps = {
163
-- runner = sandbox.registerrunner {
164
-- name = "pdf to ps",
165
-- command = "pdftops",
166
-- template = [[-eps "%oldname%" "%newname%"]],
167
-- checkers = {
168
-- oldname = "readable",
169
-- newname = "writable",
170
-- }
171
-- }
172
-- }
173
--
174
-- pdfconverter.stripped = function(oldname,newname)
175
-- local pdftoeps = programs.pdftoeps -- can be changed
176
-- local epstopdf = programs.epstopdf -- can be changed
177
-- local presets = epstopdf.resolutions[resolution or ""] or epstopdf.resolutions.high
178
-- local level = codeinjections.getformatoption("pdf_level") or "1.3"
179
-- local tmpname = newname .. ".tmp"
180
-- pdftoeps.runner { oldname = oldname, newname = tmpname, presets = presets, level = level }
181
-- epstopdf.runner { oldname = tmpname, newname = newname, presets = presets, level = level }
182
-- os.remove(tmpname)
183
-- end
184
--
185
-- figures.registersuffix("stripped","pdf")
186
--
187
-- end
188 189
do
-- svg
190 191
local
svgconverter
=
converters
.
svg
192
converters
.
svgz
=
svgconverter
193 194
-- inkscape on windows only works with complete paths .. did the command line
195
-- arguments change again? Ok, it's weirder, with -A then it's a name only when
196
-- not . (current)
197 198
-- Beware: the order of printed output lines is a bit random depending on the
199
-- method of calling (bin or pipe) because part of the message prints to stdout
200
-- and part to stderr. Also, on Windows, a second call to the old binaries
201
-- doesn't return anything at all, so that is also a signal of it being old.
202
-- This test will be dropped in 2021 anyway.
203 204
local
new
=
nil
205 206
local
function
inkscapeformat
(
suffix
)
207
if
new
=
=
nil
then
208
new
=
os
.
resultof
(
"
inkscape --version
"
)
or
"
"
209
new
=
new
=
=
"
"
or
not
find
(
new
,
"
Inkscape%s*0
"
)
210
end
211
return
new
and
"
filename
"
or
suffix
212
end
213 214
local
runner
=
sandbox
.
registerrunner
{
215
name
=
"
svg to something
"
,
216
program
=
"
inkscape
"
,
217
template
=
longtostring
[[
218 %oldname% 219 --export-dpi=%resolution% 220 --export-%format%=%newname% 221
]]
,
222
checkers
=
{
223
oldname
=
"
readable
"
,
224
newname
=
"
writable
"
,
225
format
=
"
string
"
,
226
resolution
=
"
string
"
,
227
}
,
228
defaults
=
{
229
format
=
format
,
230
resolution
=
"
600
"
,
231
}
232
}
233 234
programs
.
inkscape
=
{
235
runner
=
runner
,
236
}
237 238
function
svgconverter
.
pdf
(
oldname
,
newname
)
239
runner
{
240
format
=
inkscapeformat
(
"
pdf
"
)
,
241
resolution
=
"
600
"
,
242
newname
=
expandfilename
(
newname
)
,
243
oldname
=
expandfilename
(
oldname
)
,
244
}
245
end
246 247
function
svgconverter
.
png
(
oldname
,
newname
)
248
runner
{
249
format
=
inkscapeformat
(
"
png
"
)
,
250
resolution
=
"
600
"
,
251
newname
=
expandfilename
(
newname
)
,
252
oldname
=
expandfilename
(
oldname
)
,
253
}
254
end
255 256
svgconverter
.
default
=
svgconverter
.
pdf
257 258
end
259 260
do
-- gif | tif
261 262
local
gifconverter
=
converters
.
gif
263
local
tifconverter
=
converters
.
tif
264
local
bmpconverter
=
converters
.
bmp
265 266
programs
.
convert
=
{
267
command
=
"
gm
"
,
-- graphicmagick
268
argument
=
[[
convert "%oldname%" "%newname%"
]]
,
269
}
270 271
local
function
converter
(
oldname
,
newname
)
272
local
convert
=
programs
.
convert
273
runprogram
(
convert
.
command
,
convert
.
argument
,
{
274
newname
=
newname
,
275
oldname
=
oldname
,
276
}
)
277
end
278 279
tifconverter
.
pdf
=
converter
280
gifconverter
.
pdf
=
converter
281
bmpconverter
.
pdf
=
converter
282 283
gifconverter
.
default
=
converter
284
tifconverter
.
default
=
converter
285
bmpconverter
.
default
=
converter
286 287
end
288 289
do
-- png | jpg | profiles
290 291
-- ecirgb_v2.icc
292
-- ecirgb_v2_iccv4.icc
293
-- isocoated_v2_300_eci.icc
294
-- isocoated_v2_eci.icc
295
-- srgb.icc
296
-- srgb_v4_icc_preference.icc
297 298
-- [[convert %?colorspace: -colorspace "%colorspace%" ?%]]
299 300
local
rgbprofile
=
"
srgb_v4_icc_preference.icc
"
-- srgb.icc
301
local
cmykprofile
=
"
isocoated_v2_300_eci.icc
"
-- isocoated_v2_eci.icc
302 303
directives
.
register
(
"
graphics.conversion.rgbprofile
"
,
function
(
v
)
rgbprofile
=
type
(
v
)
=
=
"
string
"
and
v
or
rgbprofile
end
)
304
directives
.
register
(
"
graphics.conversion.cmykprofile
"
,
function
(
v
)
cmykprofile
=
type
(
v
)
=
=
"
string
"
and
v
or
cmykprofile
end
)
305 306
local
jpgconverters
=
converters
.
jpg
307
local
pngconverters
=
converters
.
png
308 309
local
function
profiles
(
)
310
if
not
isfile
(
rgbprofile
)
then
311
local
found
=
resolvers
.
findfile
(
rgbprofile
)
312
if
found
and
found
~
=
"
"
then
313
rgbprofile
=
found
314
else
315
report_figures
(
"
unknown profile %a
"
,
rgbprofile
)
316
end
317
end
318
if
not
isfile
(
cmykprofile
)
then
319
local
found
=
resolvers
.
findfile
(
cmykprofile
)
320
if
found
and
found
~
=
"
"
then
321
cmykprofile
=
found
322
else
323
report_figures
(
"
unknown profile %a
"
,
cmykprofile
)
324
end
325
end
326
return
rgbprofile
,
cmykprofile
327
end
328 329
local
checkers
=
{
330
oldname
=
"
readable
"
,
331
newname
=
"
writable
"
,
332
rgbprofile
=
"
string
"
,
333
cmykprofile
=
"
string
"
,
334
resolution
=
"
string
"
,
335
color
=
"
string
"
,
336
}
337 338
local
defaults
=
{
339
resolution
=
"
600
"
,
340
}
341 342
local
pngtocmykpdf
=
sandbox
.
registerrunner
{
343
name
=
"
png to cmyk pdf
"
,
344
program
=
"
gm
"
,
345
template
=
[[
convert -compress Zip -strip +profile "*" -profile %rgbprofile% -profile %cmykprofile% -sampling-factor 1x1 %oldname% %newname%
]]
,
346
checkers
=
checkers
,
347
defaults
=
defaults
,
348
}
349 350
local
jpgtocmykpdf
=
sandbox
.
registerrunner
{
351
name
=
"
jpg to cmyk pdf
"
,
352
program
=
"
gm
"
,
353
template
=
[[
convert -compress JPEG -strip +profile "*" -profile %rgbprofile% -profile %cmykprofile% -sampling-factor 1x1 %oldname% %newname%
]]
,
354
checkers
=
checkers
,
355
defaults
=
defaults
,
356
}
357 358
local
pngtograypdf
=
sandbox
.
registerrunner
{
359
name
=
"
png to gray pdf
"
,
360
program
=
"
gm
"
,
361
template
=
[[
convert -colorspace gray -compress Zip -sampling-factor 1x1 %oldname% %newname%
]]
,
362
checkers
=
checkers
,
363
defaults
=
defaults
,
364
}
365 366
local
jpgtograypdf
=
sandbox
.
registerrunner
{
367
name
=
"
jpg to gray pdf
"
,
368
program
=
"
gm
"
,
369
template
=
[[
convert -colorspace gray -compress Zip -sampling-factor 1x1 %oldname% %newname%
]]
,
370
checkers
=
checkers
,
371
defaults
=
defaults
,
372
}
373 374
programs
.
pngtocmykpdf
=
{
runner
=
pngtocmykpdf
}
375
programs
.
jpgtocmykpdf
=
{
runner
=
jpgtocmykpdf
}
376
programs
.
pngtograypdf
=
{
runner
=
pngtograypdf
}
377
programs
.
jpgtograypdf
=
{
runner
=
jpgtograypdf
}
378 379
pngconverters
[
"
cmyk.pdf
"
]
=
function
(
oldname
,
newname
,
resolution
)
380
local
rgbprofile
,
cmykprofile
=
profiles
(
)
381
pngtocmykpdf
{
382
oldname
=
oldname
,
383
newname
=
newname
,
384
rgbprofile
=
rgbprofile
,
385
cmykprofile
=
cmykprofile
,
386
resolution
=
resolution
,
387
}
388
end
389 390
pngconverters
[
"
gray.pdf
"
]
=
function
(
oldname
,
newname
,
resolution
)
391
pngtograypdf
{
392
oldname
=
oldname
,
393
newname
=
newname
,
394
resolution
=
resolution
,
395
}
396
end
397 398
jpgconverters
[
"
cmyk.pdf
"
]
=
function
(
oldname
,
newname
,
resolution
)
399
local
rgbprofile
,
cmykprofile
=
profiles
(
)
400
jpgtocmykpdf
{
401
oldname
=
oldname
,
402
newname
=
newname
,
403
rgbprofile
=
rgbprofile
,
404
cmykprofile
=
cmykprofile
,
405
resolution
=
resolution
,
406
}
407
end
408 409
jpgconverters
[
"
gray.pdf
"
]
=
function
(
oldname
,
newname
,
resolution
)
410
jpgtograypdf
{
411
oldname
=
oldname
,
412
newname
=
newname
,
413
resolution
=
resolution
,
414
}
415
end
416 417
-- recolor
418 419
local
recolorpng
=
sandbox
.
registerrunner
{
420
name
=
"
recolor png
"
,
421
program
=
"
gm
"
,
422
template
=
[[
convert -recolor %color% %oldname% %newname%
]]
,
423
checkers
=
checkers
,
424
defaults
=
defaults
,
425
}
426 427
-- this is now built in so not really needed any more
428 429
programs
.
recolor
=
{
runner
=
recolorpng
}
430 431
pngconverters
[
"
recolor.png
"
]
=
function
(
oldname
,
newname
,
resolution
,
arguments
)
432
recolorpng
{
433
oldname
=
oldname
,
434
newname
=
newname
,
435
resolution
=
resolution
,
436
color
=
arguments
or
"
.5 0 0 .7 0 0 .9 0 0
"
,
437
}
438
end
439 440
end
441 442
if
CONTEXTLMTXMODE
>
0
then
443 444
-- This might also work ok in mkiv but is yet untested. Anyway, it's experimental as we
445
-- go through TeX which is is inefficient. I'll improve the buffer trick.
446 447
local
function
remap
(
specification
)
448
local
fullname
=
specification
.
fullname
449
if
fullname
then
450
local
only
=
file
.
nameonly
(
fullname
)
451
local
name
=
formatters
[
"
svg-%s-inclusion
"
]
(
only
)
452
local
code
=
formatters
[
"
\\includesvgfile[%s]\\resetbuffer[%s]
"
]
(
fullname
,
name
)
453
buffers
.
assign
(
name
,
code
)
454
specification
.
format
=
"
buffer
"
455
specification
.
fullname
=
name
456
end
457
return
specification
458
end
459 460
figures
.
remappers
.
svg
=
{
mp
=
remap
}
461 462
end
463