util-lib-imp-gs.lua /size: 5621 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['util-lib-imp-gs'] = {
2    version   = 1.001,
3    comment   = "a mkiv swiglib module",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files",
7}
8
9if true then
10    logs.report("warning","swiglib is no longer supported")
11end
12
13local insert = table.insert
14local formatters = string.formatters
15
16local ghostscript     = utilities.ghostscript or { }
17utilities.ghostscript = ghostscript
18
19local report_gs = logs.reporter("swiglib ghostscript")
20
21local gs      = swiglib("ghostscript.core")
22local helpers = swiglib("helpers.core")
23
24if gs then
25    report_gs("library loaded")
26    -- inspect(table.sortedkeys(gs))
27else
28    return
29end
30
31-- these could be generic helpers
32
33local function luatable_to_c_array(t)
34    local gsargv = helpers.new_char_p_array(#t)
35    for i=1,#t do
36        helpers.char_p_array_setitem(gsargv,i-1,t[i])
37    end
38    return gsargv
39end
40
41-- the more abstract interface
42
43local interface       = { }
44ghostscript.interface = interface
45
46function interface.new()
47
48    local instance = helpers.new_void_p_p()
49    local result   = gs.gsapi_new_instance(instance,nil)
50    local buffer   = nil
51    local object   = { }
52
53    local function reader(instance,str,len)
54        return 0
55    end
56
57    local function writer(instance,str,len)
58        if buffer then
59            str = buffer .. str
60            buffer = nil
61        end
62        if not string.find(str,"[\n\r]$") then
63            str, buffer = string.match(str,"(.-)([^\n\r]+)$")
64        end
65        local log = object.log
66        for s in string.gmatch(str,"[^\n\r]+") do
67            insert(log,s)
68            report_gs(s)
69        end
70        return len
71    end
72
73    if result < 0 then
74        return nil
75    else
76        local job = helpers.void_p_p_value(instance)
77        gs.gsapi_set_stdio_callback(job,reader,writer,writer) -- could be option
78        object.instance = instance
79        object.job      = job
80        object.result   = 0
81        object.log      = { }
82        return object
83    end
84end
85
86function interface.dispose(run)
87    if run.job then
88        gs.gsapi_delete_instance(run.job)
89        run.job = nil
90    end
91    if run.instance then
92        helpers.delete_void_p_p(run.instance)
93        run.instance = nil
94    end
95end
96
97function interface.init(run,options)
98    if run.job then
99        if not options then
100            options = { "ps2pdf" }
101        else
102          insert(options,1,"ps2pdf") -- a dummy
103        end
104        run.log = { }
105        local ct = luatable_to_c_array(options)
106        local result = gs.gsapi_init_with_args(run.job,#options,ct)
107        helpers.delete_char_p_array(ct)
108        run.initresult = result
109        return result >= 0
110    end
111end
112
113function interface.exit(run)
114    if run.job then
115        local result = gs.gsapi_exit(run.job)
116        if run.initresult == 0 or run.initresult == gs.e_Quit then
117            run.result = result
118        end
119        run.exitresult = result
120        return run.result >= 0
121    end
122end
123
124function interface.process(run,options)
125    interface.init(run,options)
126    return interface.exit(run)
127end
128
129-- end of more abstract interface
130
131local nofruns = 0
132
133function ghostscript.convert(specification)
134    --
135    nofruns = nofruns + 1
136    statistics.starttiming(ghostscript)
137    --
138    local inputname = specification.inputname
139    if not inputname or inputname == "" then
140        report_gs("invalid run %s, no inputname specified",nofruns)
141        statistics.stoptiming(ghostscript)
142        return false
143    end
144    local outputname = specification.outputname
145    if not outputname or outputname == "" then
146        outputname = file.replacesuffix(inputname,"pdf")
147    end
148    --
149    if not lfs.isfile(inputname) then
150        report_gs("invalid run %s, input file %a is not found",nofruns,inputname)
151        statistics.stoptiming(ghostscript)
152        return false
153    end
154    --
155    local device = specification.device
156    if not device or device == "" then
157        device = "pdfwrite"
158    end
159    --
160    local code = specification.code
161    if not code or code == "" then
162        code = ".setpdfwrite"
163    end
164    --
165    local run = interface.new()
166    if gsinstance then
167        report_gs("invalid run %s, initialization error",nofruns)
168        statistics.stoptiming(ghostscript)
169        return false
170    end
171    --
172    local options = specification.options or { }
173    --
174    insert(options,"-dNOPAUSE")
175    insert(options,"-dBATCH")
176    insert(options,"-dSAFER")
177    insert(options,formatters["-sDEVICE=%s"](device))
178    insert(options,formatters["-sOutputFile=%s"](outputname))
179    insert(options,"-c")
180    insert(options,code)
181    insert(options,"-f")
182    insert(options,inputname)
183    --
184    report_gs("run %s, input file %a, outputfile %a",nofruns,inputname,outputname)
185    report_gs("")
186    local okay = interface.process(run,options)
187    report_gs("")
188    --
189    interface.dispose(run)
190    --
191    statistics.stoptiming(ghostscript)
192    if okay then
193        return outputname
194    else
195        report_gs("run %s quit with errors",nofruns)
196        return false
197    end
198end
199
200function ghostscript.statistics(report)
201    local runtime = statistics.elapsedtime(ghostscript)
202    if report then
203        report_gs("nofruns %s, runtime %s",nofruns,runtime)
204    else
205        return {
206            runtime = runtime,
207            nofruns = nofruns,
208        }
209    end
210end
211
212-- for i=1,100 do
213--     ghostscript.convert { inputname = "temp.eps" }
214--     ghostscript.convert { inputname = "t:/escrito/tiger.eps" }
215-- end
216-- ghostscript.statistics(true)
217