rghost-ruby1.9-beta 0.8.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (132) hide show
  1. data/lib/rghost.rb +18 -0
  2. data/lib/rghost/border.rb +44 -0
  3. data/lib/rghost/callback.rb +58 -0
  4. data/lib/rghost/circle.rb +102 -0
  5. data/lib/rghost/color.rb +175 -0
  6. data/lib/rghost/constants.rb +626 -0
  7. data/lib/rghost/convert.rb +43 -0
  8. data/lib/rghost/cursor.rb +124 -0
  9. data/lib/rghost/dash.rb +59 -0
  10. data/lib/rghost/document.rb +546 -0
  11. data/lib/rghost/document_callback_facade.rb +64 -0
  12. data/lib/rghost/dsc_entry.rb +20 -0
  13. data/lib/rghost/dynamic_document_callback.rb +15 -0
  14. data/lib/rghost/eps.rb +31 -0
  15. data/lib/rghost/font.rb +48 -0
  16. data/lib/rghost/font_map.rb +54 -0
  17. data/lib/rghost/frame.rb +105 -0
  18. data/lib/rghost/function.rb +38 -0
  19. data/lib/rghost/gif.rb +41 -0
  20. data/lib/rghost/graphic.rb +26 -0
  21. data/lib/rghost/grid/base_grid.rb +228 -0
  22. data/lib/rghost/grid/callback_facade.rb +136 -0
  23. data/lib/rghost/grid/csv_grid.rb +51 -0
  24. data/lib/rghost/grid/dynamic_callback.rb +12 -0
  25. data/lib/rghost/grid/field_format.rb +43 -0
  26. data/lib/rghost/grid/grid.rb +15 -0
  27. data/lib/rghost/grid/header.rb +120 -0
  28. data/lib/rghost/grid/matrix.rb +10 -0
  29. data/lib/rghost/grid/rails_grid.rb +74 -0
  30. data/lib/rghost/grid/static_callback.rb +13 -0
  31. data/lib/rghost/grid/style/border_lines.rb +44 -0
  32. data/lib/rghost/grid/style/bottom_lines.rb +28 -0
  33. data/lib/rghost/grid/style/old_forms.rb +28 -0
  34. data/lib/rghost/grid/style/style.rb +8 -0
  35. data/lib/rghost/gs_alone.rb +25 -0
  36. data/lib/rghost/helpers.rb +13 -0
  37. data/lib/rghost/horizontal_line.rb +45 -0
  38. data/lib/rghost/how_to.rb +75 -0
  39. data/lib/rghost/image.rb +26 -0
  40. data/lib/rghost/jpeg.rb +39 -0
  41. data/lib/rghost/line.rb +89 -0
  42. data/lib/rghost/line_width.rb +28 -0
  43. data/lib/rghost/load.rb +43 -0
  44. data/lib/rghost/newpath.rb +19 -0
  45. data/lib/rghost/paper.rb +147 -0
  46. data/lib/rghost/parse_text.rb +53 -0
  47. data/lib/rghost/pdf_security.rb +36 -0
  48. data/lib/rghost/point.rb +23 -0
  49. data/lib/rghost/point_with_command.rb +17 -0
  50. data/lib/rghost/polygon.rb +75 -0
  51. data/lib/rghost/ps/AdobeExpert.enc +258 -0
  52. data/lib/rghost/ps/AdobeLatinEncoding.enc +258 -0
  53. data/lib/rghost/ps/Bengali.enc +386 -0
  54. data/lib/rghost/ps/CodePage1250.enc +258 -0
  55. data/lib/rghost/ps/CodePage1251.enc +258 -0
  56. data/lib/rghost/ps/CodePage1252.enc +258 -0
  57. data/lib/rghost/ps/CodePage1253.enc +258 -0
  58. data/lib/rghost/ps/CodePage1254.enc +258 -0
  59. data/lib/rghost/ps/CodePage1256.enc +258 -0
  60. data/lib/rghost/ps/CodePage1257.enc +258 -0
  61. data/lib/rghost/ps/CodePage1258.enc +258 -0
  62. data/lib/rghost/ps/CodePage874.enc +258 -0
  63. data/lib/rghost/ps/Fontmap +5 -0
  64. data/lib/rghost/ps/IsoLatin.enc +352 -0
  65. data/lib/rghost/ps/MacCentralEuropean.enc +258 -0
  66. data/lib/rghost/ps/MacCyrillice.desnc +258 -0
  67. data/lib/rghost/ps/MacGreek.enc +258 -0
  68. data/lib/rghost/ps/MacHebrew.enc +258 -0
  69. data/lib/rghost/ps/TeX-CorkEncoding.enc +258 -0
  70. data/lib/rghost/ps/TeX-LGR-Greek.enc +258 -0
  71. data/lib/rghost/ps/TeX-T2AModified2Encoding.enc +258 -0
  72. data/lib/rghost/ps/TeX-T2BAdobeEncoding.enc +258 -0
  73. data/lib/rghost/ps/TeX-T2CAdobeEncoding.enc +258 -0
  74. data/lib/rghost/ps/TeX-X2AdobeEncoding.enc +258 -0
  75. data/lib/rghost/ps/TeX-XL2encoding.enc +258 -0
  76. data/lib/rghost/ps/TeXMathExtensionEncoding.enc +258 -0
  77. data/lib/rghost/ps/TeXMathItalicEncoding.enc +258 -0
  78. data/lib/rghost/ps/TeXMathSymbolEncoding.enc +258 -0
  79. data/lib/rghost/ps/US-ASCII.enc +258 -0
  80. data/lib/rghost/ps/UTF-8.enc +3 -0
  81. data/lib/rghost/ps/_cusor.ps +46 -0
  82. data/lib/rghost/ps/basic.ps +25 -0
  83. data/lib/rghost/ps/begin_document.ps +8 -0
  84. data/lib/rghost/ps/callbacks.ps +175 -0
  85. data/lib/rghost/ps/code128.font +344 -0
  86. data/lib/rghost/ps/code39.font +195 -0
  87. data/lib/rghost/ps/cursor.ps +106 -0
  88. data/lib/rghost/ps/datagrid.ps +55 -0
  89. data/lib/rghost/ps/developer.ps +237 -0
  90. data/lib/rghost/ps/ean.font +150 -0
  91. data/lib/rghost/ps/eps.ps +42 -0
  92. data/lib/rghost/ps/font.ps +38 -0
  93. data/lib/rghost/ps/frame.ps +16 -0
  94. data/lib/rghost/ps/gif.ps +150 -0
  95. data/lib/rghost/ps/horizontal_line.ps +4 -0
  96. data/lib/rghost/ps/i25.font +103 -0
  97. data/lib/rghost/ps/jpeg.ps +122 -0
  98. data/lib/rghost/ps/link.ps +22 -0
  99. data/lib/rghost/ps/paper.ps +8 -0
  100. data/lib/rghost/ps/rect_link.ps +17 -0
  101. data/lib/rghost/ps/rectangle.ps +5 -0
  102. data/lib/rghost/ps/rghost_default_template.eps +1532 -0
  103. data/lib/rghost/ps/row.ps +4 -0
  104. data/lib/rghost/ps/show.ps +21 -0
  105. data/lib/rghost/ps/table_callbacks.ps +96 -0
  106. data/lib/rghost/ps/text.ps +63 -0
  107. data/lib/rghost/ps/textarea.ps +11 -0
  108. data/lib/rghost/ps/type.ps +1 -0
  109. data/lib/rghost/ps/unit.ps +3 -0
  110. data/lib/rghost/ps/vertical_line.ps +12 -0
  111. data/lib/rghost/ps/virtual_pages.ps +55 -0
  112. data/lib/rghost/ps_facade.rb +253 -0
  113. data/lib/rghost/ps_object.rb +55 -0
  114. data/lib/rghost/rectangle_link.rb +65 -0
  115. data/lib/rghost/rgengine.so +0 -0
  116. data/lib/rghost/ruby_ghost_config.rb +243 -0
  117. data/lib/rghost/ruby_ghost_engine.rb +156 -0
  118. data/lib/rghost/ruby_ghost_version.rb +8 -0
  119. data/lib/rghost/ruby_to_ps.rb +78 -0
  120. data/lib/rghost/scale.rb +29 -0
  121. data/lib/rghost/shape_content.rb +23 -0
  122. data/lib/rghost/show.rb +88 -0
  123. data/lib/rghost/static_document_callback.rb +18 -0
  124. data/lib/rghost/text.rb +44 -0
  125. data/lib/rghost/text_in.rb +51 -0
  126. data/lib/rghost/text_link_in.rb +42 -0
  127. data/lib/rghost/textarea.rb +88 -0
  128. data/lib/rghost/units.rb +82 -0
  129. data/lib/rghost/variable.rb +12 -0
  130. data/lib/rghost/vertical_line.rb +37 -0
  131. data/lib/rghost/virtual_pages.rb +42 -0
  132. metadata +185 -0
@@ -0,0 +1,55 @@
1
+ #Postscript wrapper class
2
+ class RGhost::PsObject
3
+
4
+ #Creates new instance of PsObject. Example
5
+ # PsObject.new("/my_proc { 33 45 2 10 sub mul div} bind def")
6
+ #Or by block code
7
+ # PsObject.new do
8
+ # set PsObject.new("(A test) show")
9
+ # raw "(Other Test in raw) show "
10
+ # end
11
+ def initialize(body="",&block)
12
+ @body=if body.is_a? RGhost::PsObject
13
+ body.ps
14
+ else
15
+ "#{body} "
16
+ end
17
+
18
+ instance_eval(&block) if block
19
+
20
+ end
21
+ #Alias for ps.
22
+ def to_s
23
+ ps
24
+ end
25
+ #Gets postscript code.
26
+ def ps
27
+ @body.to_s
28
+ end
29
+ #Pushes elements on the stack.
30
+ def raw(*value)
31
+ @body << "#{value.join(' ')} "
32
+ end
33
+ #Freezes graphic state
34
+ def graphic_scope
35
+ "beging #{self} endg "
36
+ end
37
+ #Sets PsObject into this object.
38
+ def set(value)
39
+
40
+ raise TypeError.new("can't convert #{value.class} into PsObject") unless value.is_a? RGhost::PsObject
41
+ @body << "#{value.ps} "
42
+ value
43
+ end
44
+ #Alias for set
45
+ def <<(value)
46
+ set value
47
+
48
+ end
49
+
50
+ #Call operator in the stack
51
+ def call(name)
52
+ set RGhost::PsObject.new(name)
53
+ end
54
+
55
+ end
@@ -0,0 +1,65 @@
1
+ #Creates one rectangle or one shape with rounded corners.
2
+ #===Options
3
+ #
4
+ #* <tt>:x and :y</tt> - Coordinates to position.
5
+ #* <tt>:corners</tt> - Value for rounded corners. Use 0 to straight angle.
6
+ #* <tt>:width and :height</tt> - Size of frame
7
+ #* <tt>:content</tt> - facade to ShapeContent with same parameters.
8
+ #* <tt>:border</tt> - facade to Border with same parameters.
9
+ #===Examples using facade frame method inside of Document
10
+ # d=Document.new
11
+ # d.frame :x => 3, :width => 7, :height => 5, :content => {:fill => false}
12
+ #
13
+ #link:images/frame01.png
14
+ # d=Document.new
15
+ # d.frame :x => 3, :width => 7, :height => 5, :content => {:color => '#35F6A3' }
16
+ #
17
+ #link:images/frame02.png
18
+ #
19
+ # d=Document.new
20
+ # d.frame :x => 3, :width => 7, :height => 5, :content => {:color => '#35F6A3' }, :border =>{:width => 5, :dash => [1,3,10]}
21
+ #
22
+ #link:images/frame03.png
23
+ #
24
+ # d=Document.new
25
+ # d.frame :x => 3, :width => 7, :height => 5, :content => {:color => '#35F6A3' }, :corners => 20
26
+ #
27
+ #link:images/frame04.png
28
+ #
29
+ # d=Document.new
30
+ # d.frame :x => 3, :width => 7, :height => 5, :content => {:color => :yellow }, :border => {:color => :red, :width => 4}, :corners => 20
31
+ #
32
+ #link:images/frame05.png
33
+ class RGhost::RectangleLink < RGhost::PsObject
34
+ include RGhost::RubyToPs
35
+ DEFAULT_OPTIONS={
36
+ :x => :limit_left,
37
+ :y => :current_row,
38
+ :width => 5,
39
+ :height => 3.5,
40
+ :url => "http://rghost.rubyforge.org",
41
+ :border_color => RGhost::Border::DEFAULT_OPTIONS[:color]
42
+
43
+
44
+ }
45
+ def initialize(options={})
46
+
47
+ @options = DEFAULT_OPTIONS.dup.merge(options)
48
+
49
+ end
50
+
51
+
52
+ def ps
53
+ x=RGhost::Units::parse(@options[:x])
54
+ y=RGhost::Units::parse(@options[:y])
55
+
56
+ h=RGhost::Units::parse(@options[:height])
57
+ w=RGhost::Units::parse(@options[:width])
58
+ url=@options[:url]
59
+ scolor=RGhost::Color.create(@options[:border_color]).ps.gsub(/setrgbcolor/,'')
60
+ "/:rect_color [#{scolor}] def /:rect_x #{x} def /:rect_y #{y} def /:rect_w #{w} def /:rect_h #{h} def /:rect_uri #{to_string(url)} def :rect_link"
61
+
62
+
63
+ end
64
+
65
+ end
Binary file
@@ -0,0 +1,243 @@
1
+ require "rghost/ps_object"
2
+ require 'iconv'
3
+ require 'rghost/font_map'
4
+
5
+ #Rghost setup with Ghostscript.
6
+ #Ghostscript runs on a variety of platforms, this is why we recommend the non coupled install for non *nix environments.
7
+ #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.
8
+ #===RGhost::Config::GS[:mode]
9
+ #Operating mode
10
+ #* <tt>:gsparams</tt> In this mode RGhost just pass parameters to the Ghostscript framework.
11
+ #* <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.
12
+ #
13
+ #===RGhost::Config::GS[:path]
14
+ #Path to the ghostscript executable.
15
+ #====Example on windows
16
+ #RGhost::Config::GS[:path] = "C:\\gs\\bin\\gswin32c.exe"
17
+ #====Example on Linux
18
+ #RGhost::Config::GS[:path] = "/usr/bin/gs"
19
+ #
20
+ #===RGhost::Config::GS[:tmpdir]
21
+ #Temporary directory
22
+ #====Example
23
+ #RGhost::Config::GS[:tmpdir] = "/tmp"
24
+ #
25
+ #===RGhost::Config::GS[:default_params]
26
+ #Allows you to add/remove options. (use with caution!)
27
+ #====Example
28
+ #RGhost::Config::GS[:default_params] << "-dSAFER"
29
+ #
30
+ #===RGhost::Config::GS[:stack_elements]
31
+ #Defines the maximum number of elements for each matrix inside postscript's internal stack, avoiding a stack overflow error..
32
+ #====Example
33
+ #RGhost::Config::GS[:stack_elements]=5000
34
+ #
35
+ #===RGhost::Config::GS[:unit]
36
+ #Set the measure units. See Units::Unit for available units.
37
+ #====Example
38
+ #RGhost::Config::GS[:unit]=Units::Cm
39
+ #
40
+ #===RGhost::Config::GS[:charset_convert]
41
+ #Ruby to PS character conversion proxy. Necessary when the source code isn't on the same encoding of the document.
42
+ #Params is a block that returns a String. The default setting is nil.
43
+ #====Example
44
+ #RGhost::Config::GS[:charset_convert]= lambda {|text| Iconv::iconv("latin1","utf8", text).to_s}
45
+ #
46
+ #===RGhost::Config::GS[:font_encoding]
47
+ #Sets the Postscript font encoding. Default: :IsoLatin
48
+ #====Example
49
+ #RGhost::Config::GS[:font_encoding]= :IsoLatin
50
+ #
51
+ module RGhost::Config
52
+
53
+ DEFAULT_PORTRAIT_TEMPLATE = File.join(File.dirname(__FILE__),"ps","rghost_default_template.eps")
54
+
55
+
56
+ GS={
57
+ :mode => :gsparams,
58
+ :plugin => nil,
59
+ :path => nil,
60
+ :tmpdir => ENV["TMP"] || ENV["TMPDIR"] || ENV["TEMPDIR"] || ENV["TEMP"] || "/tmp",
61
+ :pslibdir => File.join( File.dirname(__FILE__),"ps"),
62
+ :extensions => [],
63
+ :preload => [],
64
+ :default_params=> %w(gs -dNOPAUSE -dBATCH -dQUIET -dNOPAGEPROMPT),
65
+ :stack_elements => 5000,
66
+ :font_encoding => :IsoLatin,
67
+ :charset_convert => lambda {|text| Iconv::iconv('latin1','utf-8', text).join },
68
+ #:charset_convert => nil,
69
+ :fontsize => 8,
70
+ :unit => RGhost::Units::Cm
71
+ }
72
+
73
+
74
+
75
+
76
+
77
+ def self.config_platform #:nodoc:
78
+
79
+ const= 'PLATFORM'
80
+ const = "RUBY_"+const if RUBY_VERSION =~ /^1.9/
81
+ GS[:path]=case Object.const_get(const)
82
+ when /linux/ then "/usr/bin/gs"
83
+ when /darwin/ then "/opt/local/bin/gs"
84
+ when /freebsd|bsd/ then "/usr/local/bin/gs"
85
+ when /mswin/ then "C:\\gs\\bin\\gswin32\\gswin32c.exe"
86
+ end
87
+ not_found_msg="\nGhostscript not found in your environment.\nInstall it and set the variable RGhost::Config::GS[:path] with the executable.\nExample: RGhost::Config::GS[:path]='/path/to/my/gs' #unix-style\n RGhost::Config::GS[:path]=\"C:\\\\gs\\\\bin\\\\gswin32c.exe\" #windows-style\n"
88
+ raise not_found_msg unless (File.exists? GS[:path])
89
+ end
90
+ #Test if your environment is ready to works. If yes the page below will show.
91
+ #
92
+ #link:images/is_ok.png
93
+ #
94
+ #You can generate this page with the code.
95
+ # RGhost::Config.is_ok?.render :pdf, :filename => "/tmp/mytest.pdf"
96
+ #
97
+ def self.is_ok?
98
+ d=RGhost::Document.new :margin_left => 2.3, :margin_bottom => 2.3
99
+ d.benchmark(:start)
100
+ d.before_page_create do |b|
101
+ b.image RGhost::Config::DEFAULT_PORTRAIT_TEMPLATE
102
+ end
103
+ d.define_tags do
104
+ new :bigger, :size => 150, :color => "#AAFF33"
105
+ end
106
+ d.text_in :x => 6, :y=> 15, :write => "Yes!", :with => :bigger
107
+ d.text_in :x => 10, :y=> 14, :write => "Your environment is ready!"
108
+ d.text_in :x => 10, :y=> 13, :write => "RGhost Version " + RGhost::VERSION::STRING
109
+ d.text_in :x => 10, :y=> 12, :write => "Created at " + Time.at(RGhost::VERSION::DATE).to_s
110
+ d.text_in :x => 10, :y=> 11, :write => "Now " + Time.now.to_s
111
+
112
+ d.benchmark(:stop)
113
+ d.done
114
+ d
115
+
116
+
117
+
118
+ end
119
+ #This method is a helper to gets the best encoding.
120
+ #
121
+ #link:images/encode_test.png
122
+ #
123
+ #You can generate this page with the code.
124
+ # RGhost::Config.encode_teste("Fiancé").render :pdf, :filename => "/tmp/mytest.pdf"
125
+ #
126
+ #The encode will use on Document class.
127
+ # doc=Document.new :font_encoding => 'IsoLatin'
128
+ #
129
+ def self.encode_test(value)
130
+ d=RGhost::Document.new :paper => :A4, :margin_left => 2 #, :landscape => true
131
+ d.before_page_create do |b|
132
+ b.image RGhost::Config::DEFAULT_PORTRAIT_TEMPLATE
133
+ end
134
+ exp=File.join(File.dirname(__FILE__),"ps","*.enc")
135
+ d.show "String (Using Helvetica Font)", :with => :b
136
+ d.moveto :x => 16
137
+ d.show "Encode Name", :with => :b
138
+ d.horizontal_line :bottom
139
+ d.next_row
140
+
141
+ Dir.glob(exp).sort.each do |f|
142
+ name=File.basename(f)
143
+ name.gsub!(/\.enc/,'')
144
+ d.set RGhost::Load.library(name,:enc)
145
+ d.set RGhost::Variable.new(:default_encoding,name)
146
+ d.set RGhost::FontMap.new {
147
+ new(:font_test, :name => "Helvetica",:size => 8,:color => "#FF0000", :encoding => true)
148
+ }
149
+
150
+ d.show "#{value}" , :with => :font_test
151
+ d.moveto :x => 16
152
+ d.show "#{name}", :with => :i
153
+ d.next_row
154
+
155
+ end
156
+ d
157
+ end
158
+ #Generates font catalog to use into method define_tags on Document
159
+ # RGhost::Config.enviroment_fonts.render :pdf, :filename => "mycatalog.pdf"
160
+ #link:images/environment_fonts.png
161
+ #
162
+ def self.environment_fonts(text="The quick brown fox jumps over the lazy dog")
163
+
164
+ d=RGhost::Document.new :margin_left => 2.3, :margin_bottom => 2.3
165
+ d.before_page_create do |b|
166
+ b.image RGhost::Config::DEFAULT_PORTRAIT_TEMPLATE
167
+ end
168
+ d.show "Search Path"
169
+ d.horizontal_line :bottom
170
+ d.next_row
171
+ d.raw :default_font
172
+ d.raw %Q{
173
+ LIBPATH{
174
+ limit_left current_row moveto show
175
+ nrdp
176
+ } forall
177
+
178
+ }
179
+ d.next_row
180
+ d.show "Example"
181
+ d.moveto :x=> 13
182
+ d.show "Font Name"
183
+ d.horizontal_line :bottom
184
+ d.next_row
185
+ #=begin
186
+ d.raw %Q{
187
+ Fontmap{
188
+ 50 string cvs pop dup findfont 10 scalefont setfont
189
+ limit_left current_row moveto (#{text}) show
190
+ 13 cm current_row moveto default_font 0 setgray
191
+ 50 string cvs show
192
+ nrdp
193
+ } forall
194
+ }
195
+ #=end
196
+ d.done
197
+ d
198
+ end
199
+
200
+ # .
201
+ #
202
+ #
203
+ #Preseted tags
204
+ #
205
+ #
206
+ #
207
+ #FONTMAP=RGhost::FontMap.new :name => "Helvetica", :size => 8, :encoding => false do
208
+ # new :span
209
+ # new :b, :name => "Helvetica-Bold"
210
+ # new :bold, :name => "Helvetica-Bold"
211
+ # new :normal, :name => "Helvetica"
212
+ # new :i, :name => "Helvetica-Oblique", :size => 8
213
+ # new :bi, :name => "Helvetica-BoldOblique"
214
+ # new :big, :size => 10
215
+ # new :small, :size => 7
216
+ # new :h1, :name => "Helvetica", :size => 14
217
+ # new :h2, :name => "Helvetica", :size => 13
218
+ # new :h3, :name => "Helvetica", :size => 12
219
+ # new :h4, :name => "Helvetica", :size => 11
220
+ # new :h5, :name => "Helvetica", :size => 10
221
+ # new :title, :name => "Helvetica", :size => 20
222
+ # new :pre, :name => "Courier"
223
+ # end
224
+ #
225
+ FONTMAP=RGhost::FontMap.new :name => "Helvetica", :size => 8, :encoding => true do
226
+ new :span
227
+ new :b, :name => "Helvetica-Bold"
228
+ new :bold, :name => "Helvetica-Bold"
229
+ new :normal, :name => "Helvetica"
230
+ new :i, :name => "Helvetica-Oblique", :size => 8
231
+ new :bi, :name => "Helvetica-BoldOblique"
232
+ new :big, :size => 10
233
+ new :small, :size => 7
234
+ new :h1, :name => "Helvetica", :size => 14
235
+ new :h2, :name => "Helvetica", :size => 13
236
+ new :h3, :name => "Helvetica", :size => 12
237
+ new :h4, :name => "Helvetica", :size => 11
238
+ new :h5, :name => "Helvetica", :size => 10
239
+ new :title, :name => "Helvetica", :size => 20
240
+ new :pre, :name => "Courier"
241
+ end
242
+
243
+ end
@@ -0,0 +1,156 @@
1
+ require 'rghost/ruby_ghost_config'
2
+ require "rghost/constants"
3
+
4
+ class RGhost::Engine
5
+ attr_reader :errors, :error, :output
6
+ DEFAULT_OPTIONS={:device => :pdf }
7
+
8
+ include RGhost::Constants::Devices
9
+
10
+ def initialize(document,options={})
11
+ @document=document
12
+ @options=DEFAULT_OPTIONS.merge(options)
13
+ @errors=[]
14
+ @error=false
15
+ @output=nil
16
+ @delete_input=true
17
+
18
+ end
19
+
20
+
21
+ def render(device=nil)
22
+
23
+ device||=@options[:device]
24
+ tmp_filename=@options[:filename]
25
+ unless tmp_filename
26
+ tmp_filename||="#{RGhost::Config::GS[:tmpdir]}#{File::SEPARATOR}#{self.object_id.abs}"
27
+ file_out="#{tmp_filename}.#{device}"
28
+ else
29
+ file_out=tmp_filename
30
+ end
31
+
32
+ file_err="#{tmp_filename}.rgerr"
33
+
34
+ multipage=@options[:multipage]
35
+ file_out.gsub!(/\./,"_%04d.") if multipage
36
+
37
+ params=RGhost::Config::GS[:default_params].dup #default parameters gs engine
38
+ params << @document.additional_params.join(" ") unless @options[:convert]
39
+ params << "-I#{RGhost::Config::GS[:pslibdir]}"
40
+ params << "-dPDFSETTINGS=/#{@options[:quality]}" if (@options[:device] ==:pdf && @options[:quality])
41
+ params << "-I#{RGhost::Config::GS[:extensions].join(' -I')}" if RGhost::Config::GS[:extensions]
42
+ params << "-sDEVICE=#{device_for(device)}"
43
+ params.concat format_params(@options[:d],"-d") if @options[:d]
44
+ params.concat format_params(@options[:s],"-s") if @options[:s]
45
+ params << "-r#{@options[:resolution]}" if @options[:resolution]
46
+ params << "-g#{@options[:size]}" if @options[:size]
47
+ if @options[:range]
48
+ params << "-dFirstPage=#{@options[:range].first}"
49
+ params << "-dLastPage=#{@options[:range].last}"
50
+ end
51
+ params << "-sstdout=#{file_err}"
52
+ params << @options[:raw] if @options[:raw]
53
+ params << "-sOutputFile=#{file_out}"
54
+
55
+
56
+ case @document
57
+ when RGhost::Document
58
+ file_in="#{tmp_filename}.rgin"
59
+ params.concat @document.gs_paper
60
+ fi=File.open(file_in,'w')
61
+ fi.puts @document.ps
62
+ fi.close
63
+ when File
64
+ file_in=@document.path
65
+ #@delete_input=false unless @options[:debug]
66
+ when String
67
+ file_in=@document
68
+ #@delete_input=false unless @options[:debug]
69
+ end
70
+
71
+ params << file_in
72
+
73
+ #puts params.inspect
74
+
75
+ if RGhost::Config::GS[:mode] == :gslib
76
+ require "rghost/rgengine"
77
+ gs=RGEngine.new
78
+ @error=!gs.render(params,params.size)
79
+ else
80
+ require 'rghost/gs_alone'
81
+ gs=RGhost::GSAlone.new(params,@options[:debug])
82
+ @error=!gs.run
83
+ end
84
+
85
+ if @error # if error
86
+ @errors=File.open(file_err).readlines if File.exists?(file_err)
87
+ else
88
+ if multipage
89
+ file_out.gsub!(/_%04d/,"_*")
90
+ @output=Dir.glob(file_out).map { |f| f }
91
+ else
92
+ @output=File.open(file_out)
93
+ end
94
+ end
95
+ begin
96
+ File.delete(file_err)
97
+ File.delete(file_in) unless (@options[:debug] || @options[:convert])
98
+ rescue
99
+ end
100
+ log(params) if @options[:logfile]
101
+ return @output
102
+ end
103
+
104
+ def clear_output
105
+ case @output
106
+ when File
107
+ @output.close
108
+ File.delete(@output.path)
109
+ when Array
110
+ @output.each do |f|
111
+ f.close
112
+ File.delete(f.path)
113
+ end
114
+ end
115
+ end
116
+ def error?
117
+ @error
118
+ end
119
+ private
120
+ def log(gp)
121
+ id=self.object_id.abs
122
+ flog=File.open(@options[:logfile], File::WRONLY|File::APPEND|File::CREAT)
123
+ flog.puts "[ID #{id}] created on #{Time.now}"
124
+ flog.puts "[#{id}] RUBY GS OPTIONS: #{@options.inspect}"
125
+ flog.puts "[#{id}] GS OPTIONS: #{gp.join(" ")}"
126
+ if @error
127
+ flog.puts "[#{id}] EXIT STATUS: ERROR"
128
+ flog.puts @errors.uniq.map {|m| "[#{id}] #{m}" }.to_s;
129
+ else
130
+ flog.puts "[#{id}] EXIT STATUS: OK"
131
+ end
132
+ end
133
+
134
+
135
+ def format_params(v,pre="-d")
136
+ r=[]
137
+ case v
138
+ when Symbol
139
+ r << "#{pre}#{v}"
140
+ when Array
141
+ v.each do |av|
142
+ r << format_params(av,pre).to_s
143
+ end
144
+ when Hash
145
+ v.each do |k,v|
146
+ r << "#{pre}#{k}=#{v.to_s.gsub(/ /,'')}"
147
+ end
148
+ else
149
+ return ""
150
+ end
151
+ return r;
152
+ end
153
+
154
+ end
155
+
156
+