Skip to content

Commit

Permalink
Adding external_encoding configuration. Resolve some encoding issues …
Browse files Browse the repository at this point in the history
…with Ruby 1.9 like UndefinedConversionError ... from ASCII-8BIT to UTF-8
  • Loading branch information
cncardoso committed May 17, 2011
1 parent 9ab8016 commit 2622ab2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 62 deletions.
70 changes: 39 additions & 31 deletions lib/rghost/ruby_ghost_config.rb
Expand Up @@ -3,12 +3,12 @@
require 'rghost/font_map'

#Rghost setup with Ghostscript.
#Ghostscript runs on a variety of platforms, this is why we recommend the non coupled install for non *nix environments.
#Ghostscript runs on a variety of platforms, this is why we recommend the non coupled install for non *nix environments.
#The gem already comes with a set of defaults but you can change the settings using the Hash RGhost::Config::GS before each use of the API. Listed below are the keys of the said hash.
#===RGhost::Config::GS[:mode]
#Operating mode
#* <tt>:gsparams</tt> In this mode RGhost just pass parameters to the Ghostscript framework.
#* <tt>:gsapi</tt> based on the exchange of data between Ruby and Ghostscript via rgengine.so using gslib.so.8 or gslib-esp.so.8.
#* <tt>:gsapi</tt> based on the exchange of data between Ruby and Ghostscript via rgengine.so using gslib.so.8 or gslib-esp.so.8.
#
#===RGhost::Config::GS[:path]
#Path to the ghostscript executable.
Expand All @@ -33,7 +33,7 @@
#RGhost::Config::GS[:stack_elements]=5000
#
#===RGhost::Config::GS[:unit]
#Set the measure units. See Units::Unit for available units.
#Set the measure units. See Units::Unit for available units.
#====Example
#RGhost::Config::GS[:unit]=Units::Cm
#
Expand All @@ -48,11 +48,17 @@
#====Example
#RGhost::Config::GS[:font_encoding]= :IsoLatin
#
#===RGhost::Config::GS[:external_encoding]
#Sets the file-in external encoding (Ruby 1.9). Affects how data will be written and
#could help when dealing with encoding conversion errors. Default: nil
#====Example
#RGhost::Config::GS[:external_encoding]= 'ascii-8bit'
#
module RGhost::Config

DEFAULT_PORTRAIT_TEMPLATE = File.join(File.dirname(__FILE__),"ps","rghost_default_template.eps")


GS={
:mode => :gsparams,
:plugin => nil,
Expand All @@ -66,18 +72,19 @@ module RGhost::Config
:font_encoding => :IsoLatin,
:charset_convert => lambda {|text| Iconv::iconv('latin1','utf-8', text).join },
#:charset_convert => nil,
:external_encoding => nil,
:fontsize => 8,
:unit => RGhost::Units::Cm
}





def self.config_platform #:nodoc:

const= 'PLATFORM'
const = "RUBY_"+const if RUBY_VERSION =~ /^1.9/
const = "RUBY_"+const if RUBY_VERSION =~ /^1.9/
GS[:path]=case Object.const_get(const)
when /linux/ then "/usr/bin/gs"
when /darwin/ then "/opt/local/bin/gs"
Expand Down Expand Up @@ -108,21 +115,21 @@ def self.is_ok?
d.text_in :x => 10, :y=> 13, :write => "RGhost Version " + RGhost::VERSION::STRING
d.text_in :x => 10, :y=> 12, :write => "Created at " + Time.at(RGhost::VERSION::DATE).to_s
d.text_in :x => 10, :y=> 11, :write => "Now " + Time.now.to_s

d.benchmark(:stop)
d.done
d



end
#This method is a helper to gets the best encoding.
#
#link:images/encode_test.png
#
#You can generate this page with the code.
# RGhost::Config.encode_teste("Fiancé").render :pdf, :filename => "/tmp/mytest.pdf"
#
#
#The encode will use on Document class.
# doc=Document.new :font_encoding => 'IsoLatin'
#
Expand All @@ -137,7 +144,7 @@ def self.encode_test(value)
d.show "Encode Name", :with => :b
d.horizontal_line :bottom
d.next_row

Dir.glob(exp).sort.each do |f|
name=File.basename(f)
name.gsub!(/\.enc/,'')
Expand All @@ -146,12 +153,12 @@ def self.encode_test(value)
d.set RGhost::FontMap.new {
new(:font_test, :name => "Helvetica",:size => 8,:color => "#FF0000", :encoding => true)
}

d.show "#{value}" , :with => :font_test
d.moveto :x => 16
d.show "#{name}", :with => :i
d.next_row

end
d
end
Expand All @@ -160,7 +167,7 @@ def self.encode_test(value)
#link:images/environment_fonts.png
#
def self.environment_fonts(text="The quick brown fox jumps over the lazy dog")

d=RGhost::Document.new :margin_left => 2.3, :margin_bottom => 2.3
d.before_page_create do |b|
b.image RGhost::Config::DEFAULT_PORTRAIT_TEMPLATE
Expand All @@ -171,36 +178,36 @@ def self.environment_fonts(text="The quick brown fox jumps over the lazy dog")
d.raw :default_font
d.raw %Q{
LIBPATH{
limit_left current_row moveto show
limit_left current_row moveto show
nrdp
} forall
}
d.next_row
d.show "Example"
d.moveto :x=> 13
d.show "Font Name"
d.horizontal_line :bottom
d.next_row
#=begin
#=begin
d.raw %Q{
Fontmap{
Fontmap{
50 string cvs pop dup findfont 10 scalefont setfont
limit_left current_row moveto (#{text}) show
limit_left current_row moveto (#{text}) show
13 cm current_row moveto default_font 0 setgray
50 string cvs show
nrdp
nrdp
} forall
}
#=end
#=end
d.done
d
end

# .
#
#
#Preseted tags
#Preseted tags
#
#
#
Expand Down Expand Up @@ -239,5 +246,6 @@ def self.environment_fonts(text="The quick brown fox jumps over the lazy dog")
new :title, :name => "Helvetica", :size => 20
new :pre, :name => "Courier"
end

end

63 changes: 32 additions & 31 deletions lib/rghost/ruby_ghost_engine.rb
Expand Up @@ -4,18 +4,18 @@
class RGhost::Engine
attr_reader :errors, :error, :output
DEFAULT_OPTIONS={:device => :pdf }

include RGhost::Constants::Devices

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

end
end


def render(device=nil)
Expand All @@ -28,60 +28,62 @@ def render(device=nil)
else
file_out=tmp_filename
end
external_encoding = RGhost::Config::GS[:external_encoding]

file_err="#{tmp_filename}.rgerr"
multipage=@options[:multipage]

multipage=@options[:multipage]
file_out.gsub!(/\./,"_%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')}" if RGhost::Config::GS[:extensions]
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.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=#{file_err}"
params << @options[:raw] if @options[:raw]
params << @options[:raw] if @options[:raw]
params << "-sOutputFile=#{file_out}"
case @document


case @document
when RGhost::Document
file_in="#{tmp_filename}.rgin"
file_in="#{tmp_filename}.rgin"
params.concat @document.gs_paper
fi=File.open(file_in,'w')
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
file_in=@document.path
#@delete_input=false unless @options[:debug]
when String
file_in=@document
file_in=@document
#@delete_input=false unless @options[:debug]
end

params << file_in

#puts params.inspect

if RGhost::Config::GS[:mode] == :gslib
require "rghost/rgengine"
gs=RGEngine.new
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)
else
Expand All @@ -96,7 +98,7 @@ def render(device=nil)
File.delete(file_err)
File.delete(file_in) unless (@options[:debug] || @options[:convert])
rescue
end
end
log(params) if @options[:logfile]
return @output
end
Expand All @@ -106,8 +108,8 @@ def clear_output
when File
@output.close
File.delete(@output.path)
when Array
@output.each do |f|
when Array
@output.each do |f|
f.close
File.delete(f.path)
end
Expand All @@ -125,13 +127,13 @@ def log(gp)
flog.puts "[#{id}] GS OPTIONS: #{gp.join(" ")}"
if @error
flog.puts "[#{id}] EXIT STATUS: ERROR"
flog.puts @errors.uniq.map {|m| "[#{id}] #{m}" }.to_s;
flog.puts @errors.uniq.map {|m| "[#{id}] #{m}" }.to_s;
else
flog.puts "[#{id}] EXIT STATUS: OK"
flog.puts "[#{id}] EXIT STATUS: OK"
end
end


def format_params(v,pre="-d")
r=[]
case v
Expand All @@ -143,14 +145,13 @@ def format_params(v,pre="-d")
end
when Hash
v.each do |k,v|
r << "#{pre}#{k}=#{v.to_s.gsub(/ /,'')}"
r << "#{pre}#{k}=#{v.to_s.gsub(/ /,'')}"
end
else
return ""
return ""
end
return r;
return r;
end

end


0 comments on commit 2622ab2

Please sign in to comment.