Class: RGhost::Engine

Inherits:
Object
  • Object
show all
Includes:
Constants::Devices
Defined in:
lib/rghost/ruby_ghost_engine.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{:device => :pdf }

Constants included from Constants::Devices

Constants::Devices::DEVICES_ALIAS, Constants::Devices::STANDARD

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Constants::Devices

#device_for

Constructor Details

#initialize(document, options = {}) ⇒ Engine


10
11
12
13
14
15
16
17
18
# File 'lib/rghost/ruby_ghost_engine.rb', line 10

def initialize(document,options={})
  @document=document
  @options=DEFAULT_OPTIONS.merge(options)
  @errors=[]
  @error=false
  @output=nil
  @delete_input=true

end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error


5
6
7
# File 'lib/rghost/ruby_ghost_engine.rb', line 5

def error
  @error
end

#errorsObject (readonly)

Returns the value of attribute errors


5
6
7
# File 'lib/rghost/ruby_ghost_engine.rb', line 5

def errors
  @errors
end

#outputObject (readonly)

Returns the value of attribute output


5
6
7
# File 'lib/rghost/ruby_ghost_engine.rb', line 5

def output
  @output
end

Instance Method Details

#clear_outputObject


104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rghost/ruby_ghost_engine.rb', line 104

def clear_output
  case @output
  when File
      @output.close
    File.delete(@output.path)
  when Array
      @output.each do |f|
      f.close
      File.delete(f.path)
    end
  end
end

#error?Boolean


116
117
118
# File 'lib/rghost/ruby_ghost_engine.rb', line 116

def error?
  @error
end

#render(device = nil) ⇒ Object


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
# File 'lib/rghost/ruby_ghost_engine.rb', line 21

def render(device=nil)

  device||=@options[:device]
  tmp_filename=@options[:filename]
  unless tmp_filename
    tmp_filename||= File.join(RGhost::Config::GS[:tmpdir], "#{self.object_id.abs}-#{rand(999999)}-#{Time.now.to_i}")
    file_out="#{tmp_filename}.#{device}"
  else
    file_out=tmp_filename
  end
  external_encoding = RGhost::Config::GS[:external_encoding]

  file_err="#{tmp_filename}.rgerr"

  multipage=@options[:multipage]
  file_out.gsub!(/^(.*)\./,'\1_%04d.') if multipage

  params=RGhost::Config::GS[:default_params].dup #default parameters gs engine
  params << @document.additional_params.join(" ") unless @options[:convert]
  params << "-I#{RGhost::Config::GS[:pslibdir]}"
  params << "-dPDFSETTINGS=/#{@options[:quality]}" if (@options[:device] ==:pdf && @options[:quality])
  params << "-I#{RGhost::Config::GS[:extensions].join(' -I')}" unless RGhost::Config::GS[:extensions].empty?
  params << "-sDEVICE=#{device_for(device)}"
  params.concat format_params(@options[:d],"-d") if  @options[:d]
  params.concat format_params(@options[:s],"-s") if  @options[:s]
  params << "-r#{@options[:resolution]}" if @options[:resolution]
  params << "-g#{@options[:size]}" if @options[:size]
  if @options[:range]
    params << "-dFirstPage=#{@options[:range].first}"
    params << "-dLastPage=#{@options[:range].last}"
  end
  params << "-sstdout=#{shellescape(file_err)}"
  params << "-sOutputFile=#{shellescape(file_out)}"
  params << @options[:raw] if @options[:raw]


  case @document
  when RGhost::Document
    file_in="#{tmp_filename}.rgin"
    params.concat @document.gs_paper
    mode=external_encoding.nil? ? 'w' : "w:#{external_encoding}"
    fi=File.open(file_in,mode)
    fi.puts @document.ps
    fi.close
  when File
    file_in=@document.path
    #@delete_input=false unless @options[:debug]
  when String
    file_in=@document
  end

  params << shellescape(file_in)

  if RGhost::Config::GS[:mode] == :gslib
    require "rghost/rgengine"
    gs=RGEngine.new
    @error=!gs.render(params,params.size)
  else
    require 'rghost/gs_alone'
    gs=RGhost::GSAlone.new(params,@options[:debug])
    @error=!gs.run
  end

  if @error # if error
    @errors=File.open(file_err).readlines if File.exists?(file_err)
    raise RGhost::RenderException.new(@errors.join(" ")) if RGhost::Config::GS[:raise_on_error]
  else
    if multipage
      file_out.gsub!(/_%04d/,"_*")
      @output=Dir.glob(file_out).map { |f| f }
    else
      @output=File.open(file_out)
    end
  end
  begin
    File.delete(file_err)
    File.delete(file_in) unless (@options[:debug] || @options[:convert])
  rescue
  end
  log(params) if @options[:logfile]
  return @output
end