summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/context/ruby/graphics/inkscape.rb
blob: 4495c3070f42c1f416edddec79622d949dae8432 (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
# 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)

        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

        report("converting #{inpfilename} to #{tmpfilename}")

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

        resultpipe = "--without-gui --print=\">#{tmpfilename}\" 2>#{logfile}"

        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:
            ok = system("inkscape #{arguments}")
            # and 0.41 fails with everything
        rescue
            report("aborted due to error")
            return false
        else
            return false unless ok
        end

        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