Module: Deplate::External

Defined in:
lib/deplate/mod/pstoedit.rb,
lib/deplate/external.rb

Overview

pstoedit.rb @Author: Thomas Link (micathom AT gmail com) @Website: deplate.sf.net/ @License: GPL (see www.gnu.org/licenses/gpl.txt) @Created: 13-Apr-2005. @Revision: 0.78

Description

Use pstoedit for converting postscript to images.

TODO

  • Run pstoedit only if the file has changed

  • Output from latex is sometimes ugly

Constant Summary collapse

@@apps =
{}

Class Method Summary collapse

Class Method Details

.def_app(name, filename) ⇒ Object



16
17
18
# File 'lib/deplate/external.rb', line 16

def def_app(name, filename)
    @@apps[name] = filename
end

.dot(instance, device, dotfile, outfile, command_line_args = []) ⇒ Object



123
124
125
126
# File 'lib/deplate/external.rb', line 123

def dot(instance, device, dotfile, outfile, command_line_args=[])
    c = command_line_args.join(' ')
    log_popen(instance, "#{get_app('dot')} -T#{device} -o#{outfile} #{c} #{dotfile}")
end

.dvi2png(instance, dvifile, outfile, other_options = nil) ⇒ Object



80
81
82
# File 'lib/deplate/external.rb', line 80

def dvi2png(instance, dvifile, outfile, other_options=nil)
    log_popen(instance, "#{get_app('dvipng')} -T tight -bg Transparent -D 120  #{other_options} -o #{outfile} #{dvifile}")
end

.dvi2ps(instance, dvifile, psfile, other_options = nil) ⇒ Object



76
77
78
# File 'lib/deplate/external.rb', line 76

def dvi2ps(instance, dvifile, psfile, other_options=nil)
    log_popen(instance, "#{get_app('dvips')} -E -Z -D 300 #{other_options} -o #{psfile} #{dvifile}")
end

.get_app(name, default = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/deplate/external.rb', line 20

def get_app(name, default=nil)
    app = @@apps[name]
    unless app
        app = default || name
        if RUBY_PLATFORM =~ /mswin/ and app !~ /\.\w+$/
            app += '.exe'
        end
    end
    app
end

.image_dimension(filename) ⇒ Object

return the bounding box as [bw, bh, bx, by]



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/deplate/external.rb', line 139

def image_dimension(filename)
    # `identify "#{filename}"`.scan(/(\d+)x(\d+)\+(\d+)\+(\d+)/).flatten
    rv = {}
    begin
        unless filename =~ /\.(pdf)$/
            for line in `#{get_app('identify')} -verbose "#{filename}"`
                if line =~ /^\s*Geometry: /
                    bw = line.scan(/(\d+)x(\d+)(\+(\d+)\+(\d+))?/).flatten
                    bw.delete_at(2)
                    rv[:bw] = bw.collect {|x| x ? x.to_i : nil}
                elsif line =~ /^\s*Resolution:/
                    res = line.scan(/(\d+)x(\d+)/).flatten
                    rv[:res] = res[0].to_i
                end
            end
        end
        return rv
    rescue Exception => e
        Deplate::Core.log(["Running identify failed", filename, e], :error)
    end
    return nil
end

.jave(instance, imgfile, args) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/deplate/external.rb', line 98

def jave(instance, imgfile, args)
    variables = args[:deplate].variables
    cmd   = ["#{get_app('jave')} image2ascii #{imgfile}"]
    p cmd
    alg   = args['ascii_algorithm'] || args['algorithm'] || 
        variables['ascii_algorithm'] || 'edge_detection'
    cmd << "algorithm=#{alg}"
    width = args['ascii_width'] || args['width'] || variables['ascii_width']
    if width =~ /^(\d+)%$/
        width = 80 * $1.to_i / 100
    elsif width =~ /^(\d+)cm$/
        width = $1.to_i / 2
    elsif width =~ /^(\d+)mm$/
        width = $1.to_i / 20
    elsif width =~ /^(\d+)px$/ or width.to_i > 120
        width = $1.to_i / 8
    elsif width =~ /^(\d+)pt$/
        width = $1.to_i / 8
    elsif width !~ /^(\d+)$/
        width = nil
    end
    cmd << "width=#{width}" if width
    log_popen(instance, cmd.join(' '))
end

.kpsewhich(instance, bibfile) ⇒ Object



71
72
73
74
# File 'lib/deplate/external.rb', line 71

def kpsewhich(instance, bibfile)
    bibfile = File.basename(bibfile)
    log_popen(instance, "#{get_app('kpsewhich')} #{bibfile}")
end

.latex(instance, texfile) ⇒ Object



67
68
69
# File 'lib/deplate/external.rb', line 67

def latex(instance, texfile)
    log_popen(instance, "#{get_app('latex')} -interaction=nonstopmode #{texfile}")
end

.log_popen(container, cmd) ⇒ Object



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
# File 'lib/deplate/external.rb', line 31

def log_popen(container, cmd)
    rv = []
    if container.deplate.allow_external
        container.log(["CWD", Dir.pwd])
        container.log(["Exec", cmd])
        begin
            IO.popen(cmd, "w+") do |io|
                if block_given?
                    yield(io)
                else
                    until io.eof
                        l = io.gets
                        rv << l.chomp
                        puts l unless Deplate::Core.quiet?
                    end
                end
            end
        rescue StandardError => e
            container.log(["Error when running command", cmd, e], :error)
        end
    else
        container.log(["Disabled", cmd], :error)
        container.log("Use -X command line option to enable external commands", :newbie)
    end
    return rv.join("\n")
end

.neato(instance, device, dotfile, outfile, command_line_args = []) ⇒ Object



128
129
130
131
# File 'lib/deplate/external.rb', line 128

def neato(instance, device, dotfile, outfile, command_line_args=[])
    c = command_line_args.join(" ")
    log_popen(instance, "#{get_app('neato')} -T#{device} -o#{outfile} #{c} #{dotfile}")
end

.ps2img(instance, device, psfile, outfile, args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/deplate/external.rb', line 84

def ps2img(instance, device, psfile, outfile, args)
    # r = args["rx"] || instance.deplate.variables["ps2imgRes"] || 96
    r = args["rx"] || instance.deplate.variables["ps2imgRes"] || 120
    # r = args["rx"] || 140
    # case device
    # when "pdf"
    #     log_popen(instance, "#{get_app('ps2pdf'} #{psfile} #{outfile}")
    # else
    #     log_popen(instance, "#{get_app('ps2ppm'} -o -r #{r} -g -t -f #{device} #{psfile}")
        # log_popen(instance, "#{get_app('convert')} -antialias -density #{r}x#{r} #{psfile} #{outfile}")
        Ps2ppm.run(psfile, "o" => true, "r" => r, "g" => true, "t" => true, "f" => device)
    # end
end

.pstoedit(invoker, fps, fout, args) ⇒ Object



19
20
21
22
# File 'lib/deplate/mod/pstoedit.rb', line 19

def pstoedit(invoker, fps, fout, args)
    cmd = [get_app('pstoedit'), args, '-mergetext -adt -pti', fps, fout].join(' ')
    log_popen(invoker, cmd)
end

.r(instance, rfile, outfile) ⇒ Object



133
134
135
136
# File 'lib/deplate/external.rb', line 133

def r(instance, rfile, outfile)
    c = "#{get_app('R')} CMD BATCH --slave --restore --no-save #{rfile} #{outfile}"
    log_popen(instance, c)
end

.write_file(container, filename, &block) ⇒ Object

The method assumes that the file should be created in the current directory, i.e., that the proper working directory was previously

set



61
62
63
64
65
# File 'lib/deplate/external.rb', line 61

def write_file(container, filename, &block)
    if container.deplate.allow_external
        File.open(filename, "w") {|io| block.call(io)}
    end
end