summaryrefslogtreecommitdiff
path: root/scripts/context/ruby/graphics/inkscape.rb
blob: 8f22d9b7954b130d1485aa8cfcb2645eda50201b (plain)
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
# module    : graphics/inkscape
# copyright : PRAGMA Advanced Document Engineering
# version   : 2002-2005
# author    : Hans Hagen
#
# project   : ConTeXt / eXaMpLe
# concept   : Hans Hagen
# info      : j.hagen@xs4all.nl
# www       : www.pragma-ade.com

# ['base/variables','variables'].each do |r| begin require r ; rescue Exception ; else break ; end ; end
# ['graphics/gs','gs'].each   do |r| begin require r ; rescue Exception ; else break ; end ; end

require 'base/variables'
require 'base/system'
require 'graphics/gs'

class InkScape

    include Variables

    def initialize(logger=nil)

        unless logger then
            puts('inkscape class needs a logger')
            exit
        end

        @variables = Hash.new
        @logger    = logger

        reset

    end

    def reset
        # nothing yet
    end

    def supported?(filename)
        filename =~ /.*\.(svg|svgz)/io
    end

    def convert(logfile=System.null)

        directpdf = false

        logfile = logfile.gsub(/\/+$/,"")

        inpfilename = getvariable('inputfile').dup
        outfilename = getvariable('outputfile').dup
        outfilename = inpfilename.dup if outfilename.empty?
        outfilename.gsub!(/(\.[^\.]*?)$/, ".pdf")
        tmpfilename = outfilename.gsub(/(\.[^\.]*?)$/, ".ps")

        if inpfilename.empty? || outfilename.empty? then
            report("no filenames given")
            return false
        end
        if inpfilename == outfilename then
            report("filenames must differ (#{inpfilename} #{outfilename})")
            return false
        end
        unless FileTest.file?(inpfilename) then
            report("unknown file #{inpfilename}")
            return false
        end

        # we need to redirect the error info else we get a pop up console

        if directpdf then
            report("converting #{inpfilename} to #{outfilename}")
          # resultpipe = "--without-gui --export-pdf=\"#{outfilename}\" 2>#{logfile}"
            resultpipe = "--without-gui --export-filename=\"#{outfilename}\" 2>#{logfile}"
        else
            report("converting #{inpfilename} to #{tmpfilename}")
            resultpipe = "--without-gui --print=\">#{tmpfilename}\" 2>#{logfile}"
        end

        arguments = [resultpipe,inpfilename].join(' ').gsub(/\s+/,' ')

        ok = true
        begin
            debug("inkscape: #{arguments}")
            # should work
            # ok = System.run('inkscape',arguments) # does not work here
            # but 0.40 only works with this:
            command = "inkscape #{arguments}"
            report(command)
            ok = system(command)
            # and 0.41 fails with everything
            # and 0.45 is better
        rescue
            report("aborted due to error")
            return false
        else
            return false unless ok
        end

        if not directpdf then
            ghostscript = GhostScript.new(@logger)
            ghostscript.setvariable('inputfile',tmpfilename)
            ghostscript.setvariable('outputfile',outfilename)
            report("converting #{tmpfilename} to #{outfilename}")
            ghostscript.convert
            begin
                File.delete(tmpfilename)
            rescue
            end
        end
    end

end