libs-imp-ghostscript.lmt /size: 3304 b    last modification: 2021-10-28 13:51
1if not modules then modules = { } end modules ['libs-imp-ghostscript'] = {
2    version   = 1.001,
3    comment   = "companion to luat-lib.mkxl",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files",
7}
8
9local libname = "ghostscript"
10local libfile = "gsdll64" -- what on unix?
11
12local gslib = resolvers.libraries.validoptional(libname)
13
14if not gslib then return end
15
16local function okay()
17    if resolvers.libraries.optionalloaded(libname,libfile) then
18        okay = function() return true end
19    else
20        okay = function() return false end
21    end
22    return okay()
23end
24
25local insert = table.insert
26local formatters = string.formatters
27
28local ghostscript     = utilities.ghostscript or { }
29utilities.ghostscript = ghostscript
30
31local gs_execute = gslib.execute
32local nofruns    = 0
33local report     = logs.reporter(libname)
34
35function ghostscript.convert(specification)
36    if okay() then
37        --
38        nofruns = nofruns + 1
39        statistics.starttiming(ghostscript)
40        --
41        local inputname = specification.inputname
42        if not inputname or inputname == "" then
43            report("invalid run %s, no inputname specified",nofruns)
44            statistics.stoptiming(ghostscript)
45            return false
46        end
47        local outputname = specification.outputname
48        if not outputname or outputname == "" then
49            outputname = file.replacesuffix(inputname,"pdf")
50        end
51        --
52        if not lfs.isfile(inputname) then
53            report("invalid run %s, input file %a is not found",nofruns,inputname)
54            statistics.stoptiming(ghostscript)
55            return false
56        end
57        --
58        local device = specification.device
59        if not device or device == "" then
60            device = "pdfwrite"
61        end
62        --
63        local code = specification.code
64        if not code or code == "" then
65            code = ".setpdfwrite"
66        end
67        --
68        local options = specification.options or { }
69        --
70        insert(options,"-dNOPAUSE")
71        insert(options,"-dBATCH")
72        insert(options,"-dSAFER")
73        insert(options,formatters["-sDEVICE=%s"](device))
74        insert(options,formatters["-sOutputFile=%s"](outputname))
75        insert(options,"-c")
76        insert(options,code)
77        insert(options,"-f")
78        insert(options,inputname)
79        --
80        report("run %s, input file %a, outputfile %a",nofruns,inputname,outputname)
81        report("")
82        local done = gslib_execute(options)
83        report("")
84        --
85        statistics.stoptiming(ghostscript)
86        if done then
87            return outputname
88        else
89            report("run %s quit with errors",nofruns)
90            return false
91        end
92    end
93end
94
95function ghostscript.statistics(report)
96    local runtime = statistics.elapsedtime(ghostscript)
97    if report then
98        report("nofruns %s, runtime %s",nofruns,runtime)
99    else
100        return {
101            runtime = runtime,
102            nofruns = nofruns,
103        }
104    end
105end
106
107-- for i=1,100 do
108--     ghostscript.convert { inputname = "temp.eps" }
109--     ghostscript.convert { inputname = "t:/escrito/tiger.eps" }
110-- end
111-- ghostscript.statistics(true)
112