lunks_rghost 0.8.7.6

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/border.rb +44 -0
  2. data/lib/rghost/callback.rb +58 -0
  3. data/lib/rghost/circle.rb +102 -0
  4. data/lib/rghost/color.rb +175 -0
  5. data/lib/rghost/constants.rb +626 -0
  6. data/lib/rghost/convert.rb +43 -0
  7. data/lib/rghost/cursor.rb +124 -0
  8. data/lib/rghost/dash.rb +59 -0
  9. data/lib/rghost/document.rb +546 -0
  10. data/lib/rghost/document_callback_facade.rb +64 -0
  11. data/lib/rghost/dsc_entry.rb +20 -0
  12. data/lib/rghost/dynamic_document_callback.rb +15 -0
  13. data/lib/rghost/eps.rb +31 -0
  14. data/lib/rghost/font.rb +48 -0
  15. data/lib/rghost/font_map.rb +54 -0
  16. data/lib/rghost/frame.rb +105 -0
  17. data/lib/rghost/function.rb +38 -0
  18. data/lib/rghost/gif.rb +41 -0
  19. data/lib/rghost/graphic.rb +26 -0
  20. data/lib/rghost/grid/base_grid.rb +228 -0
  21. data/lib/rghost/grid/callback_facade.rb +136 -0
  22. data/lib/rghost/grid/csv_grid.rb +51 -0
  23. data/lib/rghost/grid/dynamic_callback.rb +12 -0
  24. data/lib/rghost/grid/field_format.rb +43 -0
  25. data/lib/rghost/grid/grid.rb +15 -0
  26. data/lib/rghost/grid/header.rb +120 -0
  27. data/lib/rghost/grid/matrix.rb +10 -0
  28. data/lib/rghost/grid/rails_grid.rb +74 -0
  29. data/lib/rghost/grid/static_callback.rb +13 -0
  30. data/lib/rghost/grid/style/border_lines.rb +44 -0
  31. data/lib/rghost/grid/style/bottom_lines.rb +28 -0
  32. data/lib/rghost/grid/style/old_forms.rb +28 -0
  33. data/lib/rghost/grid/style/style.rb +8 -0
  34. data/lib/rghost/gs_alone.rb +25 -0
  35. data/lib/rghost/helpers.rb +13 -0
  36. data/lib/rghost/horizontal_line.rb +45 -0
  37. data/lib/rghost/how_to.rb +75 -0
  38. data/lib/rghost/image.rb +29 -0
  39. data/lib/rghost/jpeg.rb +39 -0
  40. data/lib/rghost/line.rb +89 -0
  41. data/lib/rghost/line_width.rb +28 -0
  42. data/lib/rghost/load.rb +43 -0
  43. data/lib/rghost/newpath.rb +19 -0
  44. data/lib/rghost/paper.rb +147 -0
  45. data/lib/rghost/parse_text.rb +53 -0
  46. data/lib/rghost/pdf_security.rb +36 -0
  47. data/lib/rghost/point.rb +23 -0
  48. data/lib/rghost/point_with_command.rb +17 -0
  49. data/lib/rghost/polygon.rb +75 -0
  50. data/lib/rghost/ps/AdobeExpert.enc +258 -0
  51. data/lib/rghost/ps/AdobeLatinEncoding.enc +258 -0
  52. data/lib/rghost/ps/Bengali.enc +386 -0
  53. data/lib/rghost/ps/CodePage1250.enc +258 -0
  54. data/lib/rghost/ps/CodePage1251.enc +258 -0
  55. data/lib/rghost/ps/CodePage1252.enc +258 -0
  56. data/lib/rghost/ps/CodePage1253.enc +258 -0
  57. data/lib/rghost/ps/CodePage1254.enc +258 -0
  58. data/lib/rghost/ps/CodePage1256.enc +258 -0
  59. data/lib/rghost/ps/CodePage1257.enc +258 -0
  60. data/lib/rghost/ps/CodePage1258.enc +258 -0
  61. data/lib/rghost/ps/CodePage874.enc +258 -0
  62. data/lib/rghost/ps/Fontmap +5 -0
  63. data/lib/rghost/ps/IsoLatin.enc +352 -0
  64. data/lib/rghost/ps/MacCentralEuropean.enc +258 -0
  65. data/lib/rghost/ps/MacCyrillice.desnc +258 -0
  66. data/lib/rghost/ps/MacGreek.enc +258 -0
  67. data/lib/rghost/ps/MacHebrew.enc +258 -0
  68. data/lib/rghost/ps/TeX-CorkEncoding.enc +258 -0
  69. data/lib/rghost/ps/TeX-LGR-Greek.enc +258 -0
  70. data/lib/rghost/ps/TeX-T2AModified2Encoding.enc +258 -0
  71. data/lib/rghost/ps/TeX-T2BAdobeEncoding.enc +258 -0
  72. data/lib/rghost/ps/TeX-T2CAdobeEncoding.enc +258 -0
  73. data/lib/rghost/ps/TeX-X2AdobeEncoding.enc +258 -0
  74. data/lib/rghost/ps/TeX-XL2encoding.enc +258 -0
  75. data/lib/rghost/ps/TeXMathExtensionEncoding.enc +258 -0
  76. data/lib/rghost/ps/TeXMathItalicEncoding.enc +258 -0
  77. data/lib/rghost/ps/TeXMathSymbolEncoding.enc +258 -0
  78. data/lib/rghost/ps/US-ASCII.enc +258 -0
  79. data/lib/rghost/ps/UTF-8.enc +3 -0
  80. data/lib/rghost/ps/_cusor.ps +46 -0
  81. data/lib/rghost/ps/basic.ps +25 -0
  82. data/lib/rghost/ps/begin_document.ps +8 -0
  83. data/lib/rghost/ps/callbacks.ps +175 -0
  84. data/lib/rghost/ps/code128.font +344 -0
  85. data/lib/rghost/ps/code39.font +195 -0
  86. data/lib/rghost/ps/cursor.ps +106 -0
  87. data/lib/rghost/ps/datagrid.ps +55 -0
  88. data/lib/rghost/ps/developer.ps +237 -0
  89. data/lib/rghost/ps/ean.font +150 -0
  90. data/lib/rghost/ps/eps.ps +42 -0
  91. data/lib/rghost/ps/font.ps +38 -0
  92. data/lib/rghost/ps/frame.ps +16 -0
  93. data/lib/rghost/ps/gif.ps +150 -0
  94. data/lib/rghost/ps/horizontal_line.ps +4 -0
  95. data/lib/rghost/ps/i25.font +103 -0
  96. data/lib/rghost/ps/jpeg.ps +122 -0
  97. data/lib/rghost/ps/link.ps +22 -0
  98. data/lib/rghost/ps/paper.ps +8 -0
  99. data/lib/rghost/ps/rect_link.ps +17 -0
  100. data/lib/rghost/ps/rectangle.ps +5 -0
  101. data/lib/rghost/ps/rghost_default_template.eps +1532 -0
  102. data/lib/rghost/ps/row.ps +4 -0
  103. data/lib/rghost/ps/show.ps +21 -0
  104. data/lib/rghost/ps/table_callbacks.ps +96 -0
  105. data/lib/rghost/ps/text.ps +63 -0
  106. data/lib/rghost/ps/textarea.ps +11 -0
  107. data/lib/rghost/ps/type.ps +1 -0
  108. data/lib/rghost/ps/unit.ps +3 -0
  109. data/lib/rghost/ps/vertical_line.ps +12 -0
  110. data/lib/rghost/ps/virtual_pages.ps +55 -0
  111. data/lib/rghost/ps_facade.rb +253 -0
  112. data/lib/rghost/ps_object.rb +55 -0
  113. data/lib/rghost/rectangle_link.rb +65 -0
  114. data/lib/rghost/rgengine.so +0 -0
  115. data/lib/rghost/ruby_ghost_config.rb +252 -0
  116. data/lib/rghost/ruby_ghost_engine.rb +174 -0
  117. data/lib/rghost/ruby_ghost_version.rb +8 -0
  118. data/lib/rghost/ruby_to_ps.rb +78 -0
  119. data/lib/rghost/scale.rb +29 -0
  120. data/lib/rghost/shape_content.rb +23 -0
  121. data/lib/rghost/show.rb +88 -0
  122. data/lib/rghost/static_document_callback.rb +18 -0
  123. data/lib/rghost/text.rb +44 -0
  124. data/lib/rghost/text_in.rb +51 -0
  125. data/lib/rghost/text_link_in.rb +42 -0
  126. data/lib/rghost/textarea.rb +88 -0
  127. data/lib/rghost/units.rb +82 -0
  128. data/lib/rghost/variable.rb +12 -0
  129. data/lib/rghost/vertical_line.rb +37 -0
  130. data/lib/rghost/virtual_pages.rb +42 -0
  131. data/lib/rghost.rb +18 -0
  132. metadata +181 -0
@@ -0,0 +1,546 @@
1
+ ##
2
+ #Ruby Ghost Engine - Document Builder.
3
+ #http://rghost.rubyforge.org
4
+ #Author Shairon Toledo <shairon.toledo at gmail.com> http://www.hashcode.eti.br
5
+ #Brazil Jun/2007
6
+ #
7
+ #Copyright (c) 2007-2008 Shairon Toledo
8
+ #
9
+ #Permission is hereby granted, free of charge, to any person
10
+ #obtaining a copy of this software and associated documentation
11
+ #files (the "Software"), to deal in the Software without
12
+ #restriction, including without limitation the rights to use,
13
+ #copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ #copies of the Software, and to permit persons to whom the
15
+ #Software is furnished to do so, subject to the following
16
+ #conditions:
17
+ #
18
+ #The above copyright notice and this permission notice shall be
19
+ #included in all copies or substantial portions of the Software.
20
+ #
21
+ #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22
+ #EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23
+ #OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24
+ #NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25
+ #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26
+ #WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27
+ #FROM, OUT OF OR IN CONNECTION WITH
28
+
29
+ require 'rghost/ps_object'
30
+ require 'rghost/ps_facade'
31
+ require 'rghost/document_callback_facade'
32
+ require 'rghost/virtual_pages'
33
+ require 'rghost/variable'
34
+ require 'rghost/pdf_security'
35
+ #The Document class child of PsFacade is used to join postscript objects to generate output file.
36
+
37
+ class RGhost::Document < RGhost::PsFacade
38
+ attr_reader :additional_params
39
+ include RGhost::DocumentCallbackFacade
40
+ include RGhost::RubyToPs
41
+ DISABLE_VIRTUAL_PAGE=RGhost::Variable.new(:has_vp?, false)
42
+ ENABLE_VIRTUAL_PAGE=RGhost::Variable.new(:has_vp?, true)
43
+ DEFAULT_OPTIONS={
44
+ :rows_per_page => 80 ,
45
+ :count_pages => 10,
46
+ :row_height => 0.4,
47
+ :row_padding => 0.1,
48
+ :font_encoding=> RGhost::Config::GS[:font_encoding],
49
+ :paper => RGhost::Paper::DEFAULT_OPTIONS
50
+ }
51
+
52
+ #===Examples
53
+ #Creating document with 80 rows per page and custom paper
54
+ # doc=Document.new :rows_per_page => 80, paper => [30,5]
55
+ #Document A4 with row height 0.5 and font encoding CodePage1252
56
+ # doc=Document.new :row_height => 0.5, :font_encoding => 'CodePage1252'
57
+ #Defining all margins
58
+ # doc=Document.new :margin => 0.5
59
+ #
60
+ #
61
+ #==Parameters
62
+ #* <tt>:paper</tt> - Facade to +paper+ defined on the construction of Paper class
63
+ #* <tt>:margin, :duplex and :tuble</tt> - Facade to +options+ defined on the construction of Paper class
64
+ #* <tt>:rows_per_page</tt> - Specifies count of rows per pages.
65
+ #* <tt>:landscape</tt> - Whether true invert de size(width per height)
66
+ #* <tt>:count_pages</tt> - Defines postscript internal variable to display with class TextIn. Example:
67
+ #
68
+ # doc=Document.new :count_pages => 10
69
+ # doc.before_page_create :except => 1 do |b|
70
+ # b.text_in :x => 15, :y => 5, :write => "Page %current_page% of %count_pages%"
71
+ # end
72
+ #
73
+ #The value above %count_pages% will be evaluated inside of document for all pages except page one.
74
+ #* <tt>:fontsize</tt> - Defines the size of tag :default_font.
75
+ #* <tt>:row_height and row_padding</tt> - Its names say by itself :)
76
+ #* <tt>:font_encoding</tt> - Specifies encoding of data input. You can look for supported encoding using the method RGhost::Config.encode_test
77
+ def initialize(options={},&block)
78
+
79
+
80
+ @head,@callbacks=RGhost::PsObject.new,RGhost::PsObject.new
81
+ @head.set RGhost::Load.library(:type)
82
+ @head.set RGhost::Load.library(:unit)
83
+ #super()
84
+ @variables=DEFAULT_OPTIONS.dup.merge(options)
85
+ default_encoding
86
+ @paper=RGhost::Paper.new(options[:paper] || :A4, options)
87
+ @head.set @paper
88
+ @done=false
89
+ @docinfo={:Producer => "Ruby Ghostscript - RGhost v#{RGhost::VERSION::STRING}" }
90
+ @defines=[]
91
+ @additional_params=[]
92
+
93
+ default_variables
94
+ super()
95
+ #block.call(self) if block
96
+ #yield self if block
97
+ end
98
+
99
+ def gs_paper #:nodoc:
100
+ @paper.gs_paper
101
+ end
102
+ #
103
+
104
+ #Creates map of tags if will be use in 'writable' classes(Show, Text, TextIn and TextArea).
105
+ #The font names file catalog can be generated by code below
106
+ # RGhost::Config.enviroment_fonts.render :pdf, :filename => "mycatalog.pdf"
107
+ #this can take while. If waiting for much time you has any font with problem, remove some fonts mostly international fonts not used often.
108
+ #Below little piece of catalog
109
+ #link:images/font_catalog.png
110
+ #
111
+ #After genereted catalog you can map your tags.
112
+ #
113
+ #
114
+ #Tags has +name+ of tag(as Symbol) and its options. The options are
115
+ #
116
+ #* <tt>:name</tt> - Font name from catalog.
117
+ #* <tt>:size</tt> - Font size.
118
+ #* <tt>:color</tt> - Color.create facade
119
+ #* <tt>:encoding</tt> - If true the font will be encoding using de pattern :font_encoding of the document.
120
+ #* <tt>:from</tt> - Load True Type or Type1 font from file.
121
+ #===Examples
122
+ # d=Document.new :encoding => 'IsoLatin'
123
+ # d.define_tags do
124
+ # tag :my_italic, :name => 'Hershey-Gothic-Italian-Oblique', :size => 10
125
+ # tag :myfont, :name => 'Hershey-Plain'
126
+ # tag :font_encoded, :name => 'NimbusMonL-Regu', :size => 8, :color => 0.5, :encoding => true
127
+ # tag :other_font, :name => 'NimbusMonL-Regu', :size => 10
128
+ # tag :arial, :name => 'Arial-ItalicMT', :color => '#ADAD66'
129
+ # tag :arial_bold, :name => 'NimbusSanL-BoldItal',:size => 12, :color => '#ADAD66'
130
+ # tag :monaco, :name => 'Monaco', :from => "/path/to/myfont.ttf", :size => 12
131
+ # end
132
+ #You can use :default_font tag for custom the default font.
133
+ #===Using tags
134
+ #====With Show class
135
+ # doc.show 'My Text on this row', :with => :my_italic, :align => :page_center
136
+ #====With Show class overrinding tag's color.
137
+ # doc.show 'My Text on this row', :with => :my_italic, :align => :page_center, :color => :red
138
+ #====With TextIn class.
139
+ # doc.text_in :x=> 3, :y=> 10, :tag => :arial_bold , :write => "Here's point(3,10)"
140
+ #====With Text
141
+ # doc.text '<myfont>My Text</myfont>on this row.<arial>Other text</arial><my_italic>Italic font</my_italic>'
142
+ #====With TextArea
143
+ # txt='<myfont>My Text</myfont>on this row.<arial>Other text</arial><my_italic>Italic font</my_italic>'
144
+ # doc.text_area txt, :text_align => :center, :width => 5, :x => 3, :y => 10
145
+ #====Using tag
146
+ # doc.use_tag :myfont
147
+ # doc.show "Simple Text", :tag => nil # it will use :myfont
148
+ # doc.show "Simple Text2", :tag => nil # it will use :myfont too
149
+ def define_tags(&block)
150
+ RGhost::Config::FONTMAP.instance_eval(&block)
151
+ end
152
+
153
+ def ps #:nodoc:
154
+ done
155
+
156
+
157
+
158
+ out=RGhost::PsObject.new
159
+ out.set @head
160
+ out.raw formated_docinfo
161
+ out.set @default_variables
162
+ out.set RGhost::Load.rg_enviroment
163
+ out.raw @defines.join
164
+ out.set RGhost::Cursor.moveto
165
+ out.set RGhost::Config::FONTMAP
166
+ out.set @callbacks
167
+ out.set RGhost::Load.library(:begin_document)
168
+ RGhost::Config::GS[:preload].uniq.each{|v| out.set RGhost::Load.library(v) }
169
+ out.set RGhost::Cursor.moveto
170
+ out.raw super
171
+ out.raw "\n\n"
172
+
173
+ "#{out} "
174
+
175
+ #"#{@head} \n%%endhead\n#{@default_variables}\n\n #{Load.rg_enviroment} #{@defines.join} #{@callbacks} #{Load.library(:begin_document)}\n #{Cursor.moveto}#{super}"
176
+
177
+
178
+ end
179
+ # def link(label,options={:to => 'http://rghost.rubyforge.net'})
180
+ # raw "/:link_str #{to_string(label)} def /:link_uri #{to_string(options[:to])} def :link_make"
181
+ #
182
+ # end
183
+ #Facade to RubyGhostEngine.render
184
+ #Converts a document to an output format, such as :pdf, :png, :ps, :jpeg, :tiff etc
185
+ #The paramter device can be found at RGhost::Constants::Devices or at http://pages.cs.wisc.edu/~ghost/doc/cvs/Devices.htm
186
+ #===Options
187
+ #Method render have the following options available.
188
+ #* <tt>:filename</tt> - File path.
189
+ #* <tt>:logfile</tt> - Writes the converter's process into a file.
190
+ #* <tt>:multipage</tt> - Whether true the output will be one page per file posfixed by _0001.ext, for example, for one file name 'test.png' with two pages will create test_001.png and test_002.png
191
+ #* <tt>:resolution</tt> - Integer value to output resolution.
192
+ #* <tt>:quality</tt> - Presets the "distiller parameters" to one of four predefined settings:
193
+ # :screen - selects low-resolution output similar to the Acrobat Distiller "Screen Optimized" setting.
194
+ # :ebook - selects medium-resolution output similar to the Acrobat Distiller "eBook" setting.
195
+ # :printer - selects output similar to the Acrobat Distiller "Print Optimized" setting.
196
+ # :prepress - selects output similar to Acrobat Distiller "Prepress Optimized" setting.
197
+ # :default - selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.
198
+ #* <tt>:size</tt> - Crops a single page using a string of dimension, example, '200x180', '140x90'.
199
+ #* <tt>:range</tt> - Specifies range of pages(PDF only)
200
+ #====Ghostscript interpreter options
201
+ #
202
+ #Array of Hashes for Ghostscript interpreter look at http://pages.cs.wisc.edu/~ghost/doc/cvs/Use.htm#Parameter_switches for more details.
203
+ #You can use two parameter :s and :d, examples
204
+ # :s => [{:GenericResourceDir => /dir, :DEFAULTPAPERSIZE=> "a3"}]
205
+ # :d => [ {:TextAlphaBits => 2} ]
206
+ #Or one string using the parameter :raw, as below
207
+ # :raw => "-sGenericResourceDir=/test -dTextAlphaBits=2"
208
+ #
209
+ #===Examples
210
+ # doc=Document.new
211
+ # #do something
212
+ #
213
+ # doc.render :pdf, :filename => 'foo.pdf # PDF output
214
+ # doc.render :pdf, :filename => 'foo.pdf, :quality => :ebook # PDF output
215
+ # doc.render :jpeg, :filename => 'foo.jpg' # JPEG output
216
+ # doc.render :png, :filename => 'foo.png', :multipage => true # PNG output one page per file
217
+ # doc.render :tiff, :filename => 'foo.tiff', :resolution => 300 # TIFF with 300dpi
218
+ # doc.render :ps, :raw => '-sFONTMAP=/var/myoptional/font/map', :filename => 'test.ps'
219
+ #
220
+ #===Testing if has errors
221
+ # doc=Document.new
222
+ # doc.raw "hahahah!" #it produce error in ps stack
223
+ # doc.render :jpeg, :filename => 'with_error.jpg'
224
+ # puts r.errors if r.error? #=> GPL Ghostscript 8.61: Unrecoverable error, exit code 1.\ Error: /undefined in hahahah!
225
+ #
226
+ #===Printing
227
+ #====Using printing system
228
+ # doc=Document.new
229
+ # #do something
230
+ # f="myjob.prn"
231
+ # doc.render :laserjet, :filename => f
232
+ # `lpr #{f}`
233
+ #====Windows shared printer
234
+ # doc.render :eps9mid, :filename => "//machine/printer"
235
+ def render(device,options={})
236
+ rg=RGhost::Engine.new(self,options)
237
+ rg.render(device)
238
+ rg
239
+ end
240
+ #Behavior as render but returns content file after convertion.
241
+ #===Example with Rails
242
+ # def my_action
243
+ # doc=RGhost::Document.new
244
+ # #do something
245
+ # send_data doc.render_stream(:pdf), :filename => "/tmp/myReport.pdf"
246
+ # end
247
+ #
248
+ #===TCP/IP direct printer
249
+ # require 'socket'
250
+ #
251
+ # doc=Document.new
252
+ # #do something
253
+ # printer = TCPSocket.open('192.168.1.70', 9100)
254
+ # printer.write doc.render_stream(:ps)
255
+ # printer.close
256
+ def render_stream(device,options={})
257
+ rg=render(device,options)
258
+ out=rg.output.readlines.join
259
+ rg.clear_output
260
+ out
261
+ end
262
+ #Facade to Function.new
263
+ #Defines low level function to optimize repetitive piece of code.
264
+ #===Example
265
+ # doc=Document.new
266
+ # doc.define :piece do
267
+ # set Show.new("Hello")
268
+ # set Cursor.next_row
269
+ # set HorizontalLine.new(:middle)
270
+ # set Cursor.next_row
271
+ # end
272
+ # #Repeting ten times the same code
273
+ # 10.times{ doc.call :piece }
274
+ def define(name,&block)
275
+ @defines << RGhost::Function.new("_#{name}",&block)
276
+ end
277
+ def define_variable(name,value)
278
+ set RGhost::Variable.new(name,value)
279
+ end
280
+ #Defines a function using the method define after that call de function one time.
281
+ def define_and_call(name,&block)
282
+ define(name,&block)
283
+ call(name)
284
+ end
285
+
286
+ #Prints the text file using the predefined tag +:pre+
287
+ #===Example
288
+ # doc=Document.new :paper => :A4, :landscape => true
289
+ # doc.print_file "/etc/passwd"
290
+ # doc.render :pdf, :filename => "/tmp/passwd.pdf
291
+ def print_file(file)
292
+ s=File.open(file).readlines.join.gsub(/</,'&lt').gsub(/>/,'&gt').gsub(/\n/,'<br/>')
293
+
294
+ use_tag :pre
295
+ set RGhost::Text.new(s,true)
296
+
297
+ end
298
+ #With method virtual_pages you can define any virtual pages per physical page.
299
+ #The cursor into virtual page jumps in column for each virtual page and run primitives next_page when ends columns. Look the example below.
300
+ #Example for a document without virtual pages we will has
301
+ # doc=Document.new
302
+ # doc.text File.readlines("/tmp/mytext.txt")
303
+ #will generate
304
+ #
305
+ #link:images/virtual_page1.png
306
+ #
307
+ #Now for a document with 3 virtual pages
308
+ # doc=Document.new
309
+ # doc.virtual_pages do
310
+ # new_page :width => 4
311
+ # new_page :width => 7, :margin_left => 1
312
+ # new_page :width => 4, :margin_left => 1
313
+ # end
314
+ # doc.text File.readlines("/tmp/mytext.txt")
315
+ #will generate
316
+ #
317
+ #link:images/virtual_page2.png
318
+ #
319
+ #PS: The parameter margin left of first virtual page won't be used because it's will use page's margin left.
320
+ def virtual_pages(&block)
321
+ set RGhost::VirtualPages.new(&block)
322
+ end
323
+ {
324
+ :base => -4,
325
+ :print => -4,
326
+ :modify => -8,
327
+ :copy => -16,
328
+ :annotate => -32,
329
+ :interactive => -256,
330
+ :copy_access => -512,
331
+ :assemble => -1024,
332
+ :high_quality_print => -2048,
333
+ :all => -3904}
334
+
335
+ #Security disable the permissions and define passwords to PDF documents.
336
+ #The password just support set of \w .
337
+ #Always that use the block security should set owner and user password. By default the encryption is 3.
338
+ #Document Security can be set with the permissions flags
339
+ #===Disable options
340
+ #* <tt>:base or :print</tt> Print document (possibly not at the highest quality level).
341
+ #* <tt>:modify</tt>Modify contents of document, except as controlled by :annotate, :interective and :assemble.
342
+ #* <tt>:copy</tt>Copy text and graphics from document other than that controlled by :copy_access
343
+ #* <tt>:annotate</tt>Add or modify text annotations, fill in interactive form fields, and if :interective is set, create or modify interactive form fields
344
+ #* <tt>:interactive</tt>Fill in existing interacive form fields, even if :annotate is clear
345
+ #* <tt>:copy_access</tt>Extract text and graphics (in support of accessibility to disabled users or for other purposes).
346
+ #* <tt>:assemble</tt>Assemble the document (insert, rotate, or delete pages and create bookmarks or thumbnail images), even when :base is clear
347
+ #* <tt>:high_quality_print</tt>Add or modify text annotations
348
+ #* <tt>:all</tt>Disable all permissions.
349
+ #===Example 1
350
+ # doc.security do |sec|
351
+ # sec.owner_password ="owner" #password without space!
352
+ # sec.user_password ="user" #password without space!
353
+ # sec.key_length = 128
354
+ # sec.disable :print, :copy, :high_quality
355
+ # end
356
+ #===Example 2
357
+ #Disable all
358
+ # doc.security do |sec|
359
+ # sec.owner_password ="owner" #password without space!
360
+ # sec.user_password ="user" #password without space!
361
+ # sec.disable :all
362
+ # end
363
+ #
364
+ def security
365
+ sec=RGhost::PdfSecurity.new
366
+ yield sec
367
+ @additional_params << sec.gs_params
368
+ end
369
+
370
+ #Starts and Ends internal benckmark will write in bottom of page.
371
+ #===Example
372
+ # doc=Document.new
373
+ # doc.benchmark :start
374
+ # doc.... #do something
375
+ # doc.benchmarck :stop
376
+ # doc.render ...
377
+ def benchmark(state=:start)
378
+ case state
379
+ when :stop
380
+ moveto(:x => "20", :y => "20")
381
+ raw %Q{
382
+ default_font (RGhost::Ghostscript benchmark: ) show
383
+ realtime benchmark sub 1000 div 20 string cvs show ( seconds ) show
384
+ }
385
+ when :start
386
+ set RGhost::Variable.new(:benchmark,"realtime")
387
+ end
388
+ end
389
+
390
+ #Rghost can make use of Encapsulated Postscript files to act as templates(EPS).
391
+ #This way you can create the visual layout of the page using a graphics tool and just paint the dynamic pieces over using Rghost.
392
+ #
393
+ #link:images/templates_demo.jpg
394
+ #
395
+ #Above we have mytemplate.eps that was generated by a graphic app, my_ruby_program.rb that takes care of the positioning and at last the generated output.
396
+ #
397
+ #
398
+ #A Template use example
399
+ #Let's say that the files first.eps and content.eps already exist. Now we shall see how to create a document that uses the template first.eps for the cover and the rest of the document uses content.eps.
400
+ #
401
+ # d = Document.new :margin_top => 5, :margin_bottom => 2
402
+ #
403
+ #Just for the first page
404
+ #
405
+ # d.first_page do
406
+ # image "/my/dir/first.eps" #loads the template
407
+ # text_in :x=> 5, :y=> 17, :text => "My Report", :with => :big
408
+ # next_page #go to the next page using cursors
409
+ # end
410
+ #Callback for all other pages.
411
+ #
412
+ # d.before_page_create :except => 1 do
413
+ # image "/my/dir/content.eps"
414
+ # text_in :text => "Page %current_page% of %count_pages%", :x => 18, :y => 27, :with => :normal
415
+ # end
416
+ #
417
+ #1500 rows
418
+ #
419
+ # 1500.times do |n|
420
+ # d.show "Value #{n}"
421
+ # d.next_row
422
+ # end
423
+ #We have a cover page and 1500 rows, judging by the margins each page supports 46 rows, so we have 1500/46 = 32.60 pages plus the cover. Rounding it up totals 34 pages for the :count_pages
424
+ #
425
+ # d.define_variable(:count_pages, 34)
426
+ # d.showpage
427
+ # d.render :pdf, :filename => "/tmp/test.pdf"
428
+ #
429
+ #If we knew the amount of pages beforehand we could state it on the creation of the document, i.e.
430
+ #
431
+ # :current_pages => 34
432
+ #
433
+ #The example uses one template per page, but this is not a limit in RGhost. You can have multiple images and templates on per page. Just have to define the template:
434
+ #
435
+ # d=Document.new :margin_top => 5, :margin_bottom => 2
436
+ # d.define_template(:myform, '/local/template/form1.eps', :x=> 3, :y => 5)
437
+ #
438
+ #and call it on the document.
439
+ # d.use_template :myform
440
+ #
441
+ #===Arguments
442
+ #* <tt>:name</tt> - Template's name.
443
+ #* <tt>:file_path</tt> - Path to file.
444
+ #* <tt>:options</tt> - Options facade to Image.for(or image)
445
+ def define_template(name,file_path,options={})
446
+
447
+ @defines << RGhost::Function.new("_#{name}",RGhost::Image.for(file_path,options))
448
+ end
449
+
450
+
451
+ #Informs is ready to converts/prints
452
+ def done
453
+
454
+ unless @done
455
+ @done=true
456
+ raw "\n\n"
457
+ call :after_page_create
458
+ call :callback
459
+ call :after_document_create
460
+
461
+ showpage
462
+ raw "\n%%EOF"
463
+ end
464
+ self
465
+ end
466
+ def enable_virtual_pages
467
+ set RGhost::Variable.new(:has_vp?, true)
468
+
469
+ end
470
+ def disable_virtual_pages
471
+ set RGhost::Variable.new(:has_vp?, false)
472
+ set RGhost::Variable.new(:limit_left, 'source_limit_left')
473
+ set RGhost::Variable.new(:limit_right, 'source_limit_right')
474
+
475
+ end
476
+
477
+ #Configures properties about your document.
478
+ #The keys are supported :Creator, :Title, :Author, :Subject and :Keywords, or downcase as :title etc.
479
+ #Example:
480
+ # doc.properties :Autor => "Shairon Toledo", :Title => "Learning RGhost"
481
+ def info(docinfo={})
482
+ #puts docinfo.inspect
483
+ @docinfo.merge!(docinfo)
484
+ #puts @docinfo.inspect
485
+ end
486
+
487
+ #Creates Grid::Rails inside of the document. Facade to RGhost::Grid::Rails
488
+ def rails_grid(default_columns_options={})
489
+
490
+ grid=RGhost::Grid::Rails.new(default_columns_options)
491
+ yield grid
492
+ grid.style(default_columns_options[:style]) if default_columns_options[:style]
493
+ grid.data(default_columns_options[:data]) if default_columns_options[:data]
494
+ set grid
495
+
496
+ end
497
+ #Creates Grid::CSV inside of the document. Facade to RGhost::Grid::CSV
498
+ def csv_grid(default_columns_options={})
499
+ grid=RGhost::Grid::CSV.new(default_columns_options)
500
+ yield grid
501
+ grid.style(default_columns_options[:style]) if default_columns_options[:style]
502
+ grid.data(default_columns_options[:data]) if default_columns_options[:data]
503
+ set grid
504
+ end
505
+ #Creates Grid::Matrix inside of the document. Facade to RGhost::Grid::Matrix
506
+ def matrix_grid(default_columns_options={})
507
+ grid=RGhost::Grid::Matrix.new(default_columns_options)
508
+ yield grid
509
+ grid.style(default_columns_options[:style]) if default_columns_options[:style]
510
+ set grid
511
+ end
512
+
513
+
514
+ private
515
+
516
+ def default_variables
517
+ ps=RGhost::PsObject.new
518
+ ps.set RGhost::Variable.new(:rows_per_page,@variables[:rows_per_page])
519
+ ps.set RGhost::Variable.new(:count_pages,@variables[:count_pages])
520
+ ps.set RGhost::Variable.new(:row_height,RGhost::Units::parse(@variables[:row_height]))
521
+ ps.set RGhost::Variable.new(:row_padding,RGhost::Units::parse(@variables[:row_padding]))
522
+
523
+ @default_variables=ps
524
+
525
+ end
526
+
527
+ def default_encoding
528
+ @head.set RGhost::Load.library(@variables[:font_encoding],:enc)
529
+ @head.set RGhost::Variable.new(:default_encoding,@variables[:font_encoding])
530
+
531
+ end
532
+ def formated_docinfo
533
+ d=["["]
534
+
535
+ @docinfo.each do |k,v|
536
+ d << "/#{k.to_s.capitalize}"
537
+ d << to_string(v)
538
+ end
539
+ d << "/DOCINFO"
540
+ d << "pdfmark "
541
+ d.join(" ")
542
+ end
543
+
544
+ end
545
+
546
+
@@ -0,0 +1,64 @@
1
+ #This module is included inside of Document class. It will creates methods to easy the use.
2
+ #===Callback execution order
3
+ #Example for a document with 4 pages, looking at the picture you can see the order in which the callbacks are executed
4
+ #
5
+ #link:images/pages_rghost.png
6
+
7
+ module RGhost::DocumentCallbackFacade
8
+
9
+ #Executes before page create. Respond to :only and :except options
10
+ def before_page_create(options={},&block)
11
+ new_dynamic_callback(:before_page_create,options,&block)
12
+ end
13
+
14
+ #Executes after page create. Respond to :only and :except options
15
+ def after_page_create(options={},&block)
16
+ new_dynamic_callback(:after_page_create,options,&block)
17
+ end
18
+
19
+ #Executes on odd pages create. Respond to :only and :except options
20
+ def odd_pages(options={},&block)
21
+ new_dynamic_callback(:odd_pages,options,&block)
22
+ end
23
+
24
+ #Executes on even pages create. Respond to :only and :except options
25
+ def even_pages(options={},&block)
26
+ new_dynamic_callback(:even_pages,options,&block)
27
+ end
28
+
29
+ #Executes one time before document create.
30
+ def before_document_create(&block)
31
+ new_static_callback(:before_document_create,&block)
32
+ end
33
+
34
+ #Executes one time on first page.
35
+ def first_page(&block)
36
+ new_static_callback(:first_page,&block)
37
+ end
38
+ #Executes one time on last page(end of document).
39
+ def last_page(&block)
40
+ new_static_callback(:last_page,&block)
41
+ end
42
+
43
+ #Executes before virtual page create. Respond to :only and :except options
44
+ def before_virtual_page_create(options={},&block)
45
+ new_dynamic_callback(:before_virtual_page_create,options,&block)
46
+ end
47
+ #Executes after virtual page create. Respond to :only and :except options
48
+ def after_virtual_page_create(options={},&block)
49
+ new_dynamic_callback(:after_virtual_page_create,options,&block)
50
+ end
51
+
52
+ private
53
+
54
+ def new_dynamic_callback(name,options={},&block)
55
+ @callbacks.set RGhost::Callback.new(name,options,&block)
56
+ end
57
+ def new_static_callback(name,&block)
58
+
59
+ callback_body= RGhost::PsFacade.new(&block)
60
+ @callbacks.set RGhost::Function.new(name,callback_body)
61
+ end
62
+
63
+
64
+ end
@@ -0,0 +1,20 @@
1
+ require "rghost/ps_object"
2
+ #Specifiesnew DSC (Document Structuring Conventions)
3
+ class RGhost::DSCEntry < RGhost::PsObject
4
+ #===Example
5
+ # DSCEntry.new do |entry|
6
+ # entry << "BoundingBox: 0 0 612 792"
7
+ # entry << "Pages: 45"
8
+ # entry << "BeginSetup"
9
+ # end
10
+ def initialize
11
+
12
+ yield @entries=[]
13
+ end
14
+
15
+ def ps
16
+ @entries.map{|e| "%%#{e}\n"}.to_s
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,15 @@
1
+ require "rghost/callback"
2
+
3
+ class RGhost::DynamicDocumentCallback < RGhost::Callback #:nodoc:
4
+ ACCEPT=[:before_page_create,
5
+ :after_page_create,
6
+ :odd_pages,
7
+ :even_pages
8
+ ]
9
+
10
+ def initialize(name,options={},&block)
11
+ raise NameError.new("#{name} no accept in #{self.class}") unless ACCEPT.include? name
12
+ super(name,options,&block)
13
+ end
14
+
15
+ end
data/lib/rghost/eps.rb ADDED
@@ -0,0 +1,31 @@
1
+ require "rghost/ps_object"
2
+ #Load new EPS file.
3
+ class RGhost::Eps < RGhost::PsObject
4
+ #===Examples
5
+ # doc=Document.new
6
+ # doc.set Eps.new "/local/templates/myform.eps", :x => 10, :y => 3
7
+ #Using Image.for facade
8
+ # doc.set Image.for "/local/templates/myform.eps", :x => 10, :y => 3
9
+ #Using PsFacade or Document
10
+ # doc.image "/local/templates/myform.eps", :x => 10, :y => 3
11
+ #===Options
12
+ #
13
+ #* <tt>:x and :y</tt> - Coordinates to position.
14
+ #* <tt>:rotate</tt> - Angle to image rotation if there is one.
15
+ def initialize(eps_path,options={:x=>0, :y=> 0, :rotate => 0})
16
+ super("")
17
+ @options=options
18
+ @path=eps_path
19
+ end
20
+
21
+
22
+ def ps
23
+
24
+ "BeginEPSF \n #{RGhost::Cursor.translate(@options)} \n\t(#{@path}) run \nEndEPSF\n ";
25
+
26
+ end
27
+
28
+
29
+
30
+
31
+ end