eventioz-pdf-writer 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +1 -0
  3. data/ChangeLog +113 -0
  4. data/Demo-README +43 -0
  5. data/Gemfile +4 -0
  6. data/LICENCE +131 -0
  7. data/README +33 -0
  8. data/Rakefile +1 -0
  9. data/Release-Announcement +87 -0
  10. data/bin/techbook +24 -0
  11. data/bugs/first_page_margins.rb +28 -0
  12. data/bugs/page_numbering_simple.rb +14 -0
  13. data/bugs/page_numbering_stop_and_start.rb +21 -0
  14. data/demo/chunkybacon.rb +36 -0
  15. data/demo/code.rb +71 -0
  16. data/demo/colornames.rb +47 -0
  17. data/demo/demo.rb +73 -0
  18. data/demo/gettysburg.rb +66 -0
  19. data/demo/hello.rb +26 -0
  20. data/demo/individual-i.rb +89 -0
  21. data/demo/pac.rb +70 -0
  22. data/demo/qr-language.rb +580 -0
  23. data/demo/qr-library.rb +380 -0
  24. data/images/bluesmoke.jpg +0 -0
  25. data/images/chunkybacon.jpg +0 -0
  26. data/images/chunkybacon.png +0 -0
  27. data/lib/pdf/charts.rb +13 -0
  28. data/lib/pdf/charts/stddev.rb +431 -0
  29. data/lib/pdf/core_ext/mutex.rb +11 -0
  30. data/lib/pdf/math.rb +108 -0
  31. data/lib/pdf/quickref.rb +333 -0
  32. data/lib/pdf/simpletable.rb +948 -0
  33. data/lib/pdf/techbook.rb +905 -0
  34. data/lib/pdf/writer.rb +2734 -0
  35. data/lib/pdf/writer/arc4.rb +63 -0
  36. data/lib/pdf/writer/fontmetrics.rb +203 -0
  37. data/lib/pdf/writer/fonts/Courier-Bold.afm +342 -0
  38. data/lib/pdf/writer/fonts/Courier-BoldOblique.afm +342 -0
  39. data/lib/pdf/writer/fonts/Courier-Oblique.afm +342 -0
  40. data/lib/pdf/writer/fonts/Courier.afm +342 -0
  41. data/lib/pdf/writer/fonts/Helvetica-Bold.afm +2827 -0
  42. data/lib/pdf/writer/fonts/Helvetica-BoldOblique.afm +2827 -0
  43. data/lib/pdf/writer/fonts/Helvetica-Oblique.afm +3051 -0
  44. data/lib/pdf/writer/fonts/Helvetica.afm +3051 -0
  45. data/lib/pdf/writer/fonts/MustRead.html +19 -0
  46. data/lib/pdf/writer/fonts/Symbol.afm +213 -0
  47. data/lib/pdf/writer/fonts/Times-Bold.afm +2588 -0
  48. data/lib/pdf/writer/fonts/Times-BoldItalic.afm +2384 -0
  49. data/lib/pdf/writer/fonts/Times-Italic.afm +2667 -0
  50. data/lib/pdf/writer/fonts/Times-Roman.afm +2419 -0
  51. data/lib/pdf/writer/fonts/ZapfDingbats.afm +225 -0
  52. data/lib/pdf/writer/graphics.rb +817 -0
  53. data/lib/pdf/writer/graphics/imageinfo.rb +365 -0
  54. data/lib/pdf/writer/lang.rb +43 -0
  55. data/lib/pdf/writer/lang/en.rb +99 -0
  56. data/lib/pdf/writer/object.rb +23 -0
  57. data/lib/pdf/writer/object/action.rb +35 -0
  58. data/lib/pdf/writer/object/annotation.rb +42 -0
  59. data/lib/pdf/writer/object/catalog.rb +39 -0
  60. data/lib/pdf/writer/object/contents.rb +70 -0
  61. data/lib/pdf/writer/object/destination.rb +40 -0
  62. data/lib/pdf/writer/object/encryption.rb +53 -0
  63. data/lib/pdf/writer/object/font.rb +72 -0
  64. data/lib/pdf/writer/object/fontdescriptor.rb +34 -0
  65. data/lib/pdf/writer/object/fontencoding.rb +40 -0
  66. data/lib/pdf/writer/object/image.rb +304 -0
  67. data/lib/pdf/writer/object/info.rb +51 -0
  68. data/lib/pdf/writer/object/outline.rb +30 -0
  69. data/lib/pdf/writer/object/outlines.rb +30 -0
  70. data/lib/pdf/writer/object/page.rb +195 -0
  71. data/lib/pdf/writer/object/pages.rb +115 -0
  72. data/lib/pdf/writer/object/procset.rb +46 -0
  73. data/lib/pdf/writer/object/viewerpreferences.rb +74 -0
  74. data/lib/pdf/writer/ohash.rb +58 -0
  75. data/lib/pdf/writer/oreader.rb +25 -0
  76. data/lib/pdf/writer/state.rb +48 -0
  77. data/lib/pdf/writer/strokestyle.rb +138 -0
  78. data/manual.pwd +5965 -0
  79. data/metaconfig +13 -0
  80. data/pdf-writer.gemspec +28 -0
  81. data/pre-setup.rb +56 -0
  82. data/setup.rb +1366 -0
  83. data/test/helper.rb +4 -0
  84. data/test/test_image_info.rb +16 -0
  85. data/test/test_page_numbering.rb +13 -0
  86. metadata +196 -0
@@ -0,0 +1,11 @@
1
+
2
+ unless RUBY_VERSION < '1.9'
3
+ class ::Mutex
4
+ def marshal_dump
5
+ end unless method_defined?(:marshal_dump)
6
+ def marshal_load(data)
7
+ end unless method_defined?(:marshal_load)
8
+ end
9
+ end
10
+
11
+
@@ -0,0 +1,108 @@
1
+ #--
2
+ # PDF::Writer for Ruby.
3
+ # http://rubyforge.org/projects/ruby-pdf/
4
+ # Copyright 2003 - 2005 Austin Ziegler.
5
+ #
6
+ # Licensed under a MIT-style licence. See LICENCE in the main distribution
7
+ # for full licensing information.
8
+ #
9
+ # $Id$
10
+ #++
11
+ # Encapsulate some of the mathematical calculations that need to be
12
+ # performed when working with PDF documents. All angles in PDF::Writer are
13
+ # measured in degrees, but all angles in PDF documents are in radians. The
14
+ # standard conversions between radians, degrees, and gradians are
15
+ # provided.
16
+ #
17
+ # As with the Perl implementations of these conversions, they will be
18
+ # wrapped in the range of the target measurement (0..PI2 for radians,
19
+ # 0..360 for degrees, and 0..400 for gradians). To prevent this wrapping,
20
+ # provide a false value for the +wrap+ parameter.
21
+ #
22
+ # To wrap these values manually, use #rad2rad, #deg2deg, or #grad2grad.
23
+ module PDF::Math
24
+ class << self
25
+ PI2 = ::Math::PI * 2.0
26
+
27
+ # One degree of arc measured in terms of radians.
28
+ DR = PI2 / 360.0
29
+ # One radian of arc, measured in terms of degrees.
30
+ RD = 360 / PI2
31
+ # One degree of arc, measured in terms of gradians.
32
+ DG = 400 / 360.0
33
+ # One gradian of arc, measured in terms of degrees.
34
+ GD = 360 / 400.0
35
+ # One radian of arc, measured in terms of gradians.
36
+ RG = 400 / PI2
37
+ # One gradian of arc, measured in terms of radians.
38
+ GR = PI2 / 400.0
39
+
40
+ # Truncate the remainder.
41
+ def remt(num, den)
42
+ num - den * (num / den.to_f).to_i
43
+ end
44
+
45
+ # Wrap radian values within the range of radians (0..PI2).
46
+ def rad2rad(rad)
47
+ remt(rad, PI2)
48
+ end
49
+
50
+ # Wrap degree values within the range of degrees (0..360).
51
+ def deg2deg(deg)
52
+ remt(deg, 360)
53
+ end
54
+
55
+ # Wrap gradian values within the range of gradians (0..400).
56
+ def grad2grad(grad)
57
+ remt(grad, 400)
58
+ end
59
+
60
+ # Convert degrees to radians. The value will be constrained to the
61
+ # range of radians (0..PI2) unless +wrap+ is false.
62
+ def deg2rad(deg, wrap = true)
63
+ rad = DR * deg
64
+ rad = rad2rad(rad) if wrap
65
+ rad
66
+ end
67
+
68
+ # Convert degrees to gradians. The value will be constrained to the
69
+ # range of gradians (0..400) unless +wrap+ is false.
70
+ def deg2grad(deg, wrap = true)
71
+ grad = DG * deg
72
+ grad = grad2grad(grad) if wrap
73
+ grad
74
+ end
75
+
76
+ # Convert radians to degrees. The value will be constrained to the
77
+ # range of degrees (0..360) unless +wrap+ is false.
78
+ def rad2deg(rad, wrap = true)
79
+ deg = RD * rad
80
+ deg = deg2deg(deg) if wrap
81
+ deg
82
+ end
83
+
84
+ # Convert radians to gradians. The value will be constrained to the
85
+ # range of gradians (0..400) unless +wrap+ is false.
86
+ def rad2grad(rad, wrap = true)
87
+ grad = RG * rad
88
+ grad = grad2grad(grad) if wrap
89
+ grad
90
+ end
91
+
92
+ # Convert gradians to degrees. The value will be constrained to the
93
+ # range of degrees (0..360) unless +wrap+ is false.
94
+ def grad2deg(grad, wrap = true)
95
+ deg = GD * grad
96
+ deg = deg2deg(deg) if wrap
97
+ deg
98
+ end
99
+
100
+ # Convert gradians to radians. The value will be constrained to the
101
+ # range of radians (0..PI2) unless +wrap+ is false.
102
+ def grad2rad(grad, wrap = true)
103
+ rad = GR * grad
104
+ rad = rad2rad(rad) if wrap
105
+ rad
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,333 @@
1
+ #--
2
+ # PDF::Writer for Ruby.
3
+ # http://rubyforge.org/projects/ruby-pdf/
4
+ # Copyright 2003 - 2005 Austin Ziegler.
5
+ #
6
+ # Licensed under a MIT-style licence. See LICENCE in the main distribution
7
+ # for full licensing information.
8
+ #
9
+ # $Id$
10
+ #++
11
+ require 'pdf/simpletable'
12
+
13
+ # = QuickRef
14
+ # A formatting language to create a quick reference sheet. This is a
15
+ # multi-column page in landscape mode that generally has three or four
16
+ # columns. This format may also be used for brochures, but brochure
17
+ # creation requires a bit of management to create properly.
18
+ #
19
+ # == Reference Sheets
20
+ # A three-column reference sheet is generally in the form of:
21
+ #
22
+ # Page 1:
23
+ # column 1 | column 2 | column 3
24
+ # Page 2:
25
+ # column 4 | column 5 | column 6
26
+ #
27
+ # The formatting language provided in QuickRef is based around this text
28
+ # flow. The title of the quick reference sheet is in column 1. The two
29
+ # pages are intended to be printed on both sides of pieces of paper so
30
+ # that columns 1 and 6 are matched. This will use a Z-fold that places
31
+ # columns 5 and 6 face to face and columns 2 and 3 face to face. In the
32
+ # folded reference sheet, columns 1 and 4 will be facing out.
33
+ #
34
+ # == Brochures
35
+ # In contrast, brochures differ vastly in their design, although the
36
+ # common brochure is also three columns and either follows the same layout
37
+ # as a reference sheet or uses an overlapping fold.
38
+ #
39
+ # When an overlapping fold is used, the title is typically on column 6
40
+ # (assuming a left-to-right reading order). A short summary will appear on
41
+ # column 4. Contact information about the maker of the brochure is
42
+ # typically in column 5. Columns 1, 2, and 3 will contain the main body of
43
+ # the brochure. The brochure will be folded so that columns 2 and 3 are
44
+ # face to face. After this, column 1 will face column 4 (exposed by the
45
+ # first fold). In the folded brochure, columns 5 and 6 are facing out.
46
+ #
47
+ # == Usage
48
+ # qr = PDF::QuickRef.new # 3-column LETTER
49
+ # qr.title "My QuickRef"
50
+ # qr.h1 "H1 Text"
51
+ # qr.lines "Text to put after the header."
52
+ # qr.save_as "MyQuickRef.pdf"
53
+ class PDF::QuickRef
54
+ VERSION = '1.2.0'
55
+
56
+ # Create the quick reference document. +paper+ is passed unchanged to
57
+ # the PDF::Writer.new; the page is always created landscape. Margins
58
+ # are initialized to 18 points. After some additional initialization is
59
+ # performed, the quick reference document is yielded to an optional
60
+ # block for further configuration. All of this is done before the
61
+ # columns are started.
62
+ #
63
+ # After the columns are started, lines will be drawn between column
64
+ # positions.
65
+ def initialize(paper = "LETTER", columns = 3, column_separators_visible = true)
66
+ @pdf = PDF::Writer.new(:paper => paper, :orientation => :landscape)
67
+ @pdf.margins_pt 18
68
+ @pdf.y = @pdf.absolute_top_margin
69
+
70
+ @title_font = "Times-Roman"
71
+ @heading_font = "Times-Roman"
72
+ @body_font = "Times-Roman"
73
+ @code_font = "Courier"
74
+ @title_font_size = 14
75
+ @h1_font_size = 11
76
+ @h2_font_size = 9
77
+ @h3_font_size = 8
78
+ @h4_font_size = 7
79
+ @body_font_size = 6
80
+
81
+ @ptab = PDF::SimpleTable.new do |tab|
82
+ tab.column_order.replace %w(one two)
83
+
84
+ tab.font_size = @body_font_size
85
+ tab.show_lines = :none
86
+ tab.show_headings = false
87
+ tab.orientation = :center
88
+ tab.position = :center
89
+ end
90
+ @ltab = PDF::SimpleTable.new do |tab|
91
+ tab.column_order.replace %w(line)
92
+
93
+ tab.font_size = @body_font_size
94
+ tab.show_lines = :none
95
+ tab.show_headings = false
96
+ tab.orientation = :center
97
+ tab.position = :center
98
+ end
99
+
100
+ yield self if block_given?
101
+
102
+ @pdf.start_columns columns
103
+
104
+ @ptab.font_size = @body_font_size
105
+ @ltab.font_size = @body_font_size
106
+
107
+ @ptab.maximum_width = @pdf.column_width - 10
108
+ @ltab.maximum_width = @pdf.column_width - 10
109
+
110
+ if column_separators_visible
111
+ # Put lines between the columns.
112
+ all = @pdf.open_object
113
+ @pdf.save_state
114
+ @pdf.stroke_color! Color::RGB::Black
115
+ @pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT
116
+ (1 .. (columns - 1)).each do |ii|
117
+ x = @pdf.left_margin + (@pdf.column_width * ii)
118
+ x += (@pdf.column_gutter * (ii - 0.5))
119
+ @pdf.line(x, @pdf.page_height - @pdf.top_margin, x, @pdf.bottom_margin)
120
+ @pdf.stroke
121
+ end
122
+ @pdf.restore_state
123
+ @pdf.close_object
124
+ @pdf.add_object(all, :all_pages)
125
+ end
126
+ end
127
+
128
+ # Access to the raw PDF canvas for normal PDF::Writer configuration.
129
+ attr_reader :pdf
130
+
131
+ # The name of the font that will be used for #title text. The default
132
+ # font is Times-Roman.
133
+ attr_accessor :title_font
134
+ # The font encoding for #title text.
135
+ attr_accessor :title_font_encoding
136
+ # The size #title text. The default is 14 points.
137
+ attr_accessor :title_font_size
138
+
139
+ # The name of the font that will be used for #h1, #h2, #h3, and #h4
140
+ # text. The default is Times-Roman.
141
+ attr_accessor :heading_font
142
+ # The font encoding for #h1, #h2, #h3, and #h4 text.
143
+ attr_accessor :heading_font_encoding
144
+ # The size #h1 text. The default is 11 points.
145
+ attr_accessor :h1_font_size
146
+ # The size #h2 text. The default is 9 points.
147
+ attr_accessor :h2_font_size
148
+ # The size #h3 text. The default is 8 points.
149
+ attr_accessor :h3_font_size
150
+ # The size #h4 text. The default is 7 points.
151
+ attr_accessor :h4_font_size
152
+
153
+ # The name of the font that will be used for #body, #lines, and #pairs
154
+ # text. The default is 'Times-Roman'.
155
+ attr_accessor :body_font
156
+ # The font encoding for #body, #lines, and #pairs text.
157
+ attr_accessor :body_font_encoding
158
+ # The name of the font that will be used for #code, #codelines, and
159
+ # #codepairs text; this is generally a fixed-pitch font. The default is
160
+ # 'Courier'.
161
+ attr_accessor :code_font
162
+ # The font encoding for #code, #codelines, and #codepairs text.
163
+ attr_accessor :code_font_encoding
164
+ # The size #body and #code text. The default is 7 points.
165
+ attr_accessor :body_font_size
166
+
167
+ # Creates a two-column zebra-striped table using the #body font. Each
168
+ # line of the text is a separate row; the two columns are separated by
169
+ # tab characters.
170
+ def pairs(text)
171
+ data = text.split($/).map do |line|
172
+ one, two = line.split(/\t/)
173
+ { 'one' => one, 'two' => two }
174
+ end
175
+ @ptab.data.replace data
176
+ @ptab.render_on(@pdf)
177
+ @pdf.text "\n", :font_size => @body_font_size
178
+ end
179
+ # Creates a two-column zebra-striped table using the #code font. Each
180
+ # line of the text is a separate row; the two columns are separated by
181
+ # tab characters.
182
+ def codepairs(text)
183
+ data = text.split($/).map do |line|
184
+ one, two = line.split(/\t/)
185
+ { 'one' => one, 'two' => two }
186
+ end
187
+ @ptab.data.replace data
188
+ use_code_font
189
+ @ptab.render_on(@pdf)
190
+ use_body_font
191
+ @pdf.text "\n", :font_size => @body_font_size
192
+ end
193
+ # Creates a one-column zebra-striped table using the #body font. Each
194
+ # line of the text is a separate row.
195
+ def lines(text)
196
+ data = text.split($/).map { |line| { "line" => line } }
197
+ @ltab.data.replace data
198
+ @ltab.render_on(@pdf)
199
+ @pdf.text "\n", :font_size => @body_font_size
200
+ end
201
+ # Creates a one-column zebra-striped table using the #code font. Each
202
+ # line of the text is a separate row.
203
+ def codelines(text)
204
+ data = text.split($/).map { |line| { "line" => line } }
205
+ @ltab.data.replace data
206
+ use_code_font
207
+ @ltab.render_on(@pdf)
208
+ use_body_font
209
+ @pdf.text "\n", :font_size => @body_font_size
210
+ end
211
+
212
+ # Change the current font to the #title font.
213
+ def use_title_font
214
+ @pdf.select_font @title_font, @title_font_encoding
215
+ end
216
+ # Change the current font to the heading font (used normally by #h1,
217
+ # #h2, #h3, and #h4|).
218
+ def use_heading_font
219
+ @pdf.select_font @heading_font, @heading_font_encoding
220
+ end
221
+ # Change the current font to the #body font.
222
+ def use_body_font
223
+ @pdf.select_font @body_font, @body_font_encoding
224
+ end
225
+ # Change the current font to the #code font.
226
+ def use_code_font
227
+ @pdf.select_font @code_font, @code_font_encoding
228
+ end
229
+
230
+ # Writes the +text+ with the #title_font and #title_font_size centered
231
+ # in the column. After the title has been written, an #hline will be
232
+ # drawn under the title. The font is set to #body_font after the title
233
+ # is drawn.
234
+ def title(text)
235
+ use_title_font
236
+ @pdf.text text, :font_size => @title_font_size, :justification => :center
237
+ use_body_font
238
+ hline
239
+ end
240
+ # Writes the +text+ with the #heading_font and #h1_font_size left
241
+ # justified in the column. The font is set to #body_font after the
242
+ # heading is drawn.
243
+ def h1(text)
244
+ use_heading_font
245
+ @pdf.text text, :font_size => @h1_font_size
246
+ use_body_font
247
+ end
248
+ # Writes the +text+ with the #heading_font and #h2_font_size left
249
+ # justified in the column. The font is set to #body_font after the
250
+ # heading is drawn.
251
+ def h2(text)
252
+ use_heading_font
253
+ @pdf.text "<i>#{text}</i>", :font_size => @h2_font_size
254
+ use_body_font
255
+ end
256
+ # Writes the +text+ with the #heading_font and #h3_font_size left
257
+ # justified in the column. The font is set to #body_font after the
258
+ # heading is drawn.
259
+ def h3(text)
260
+ use_heading_font
261
+ @pdf.text "<i>#{text}</i>", :font_size => @h3_font_size
262
+ use_body_font
263
+ end
264
+ # Writes the +text+ with the #heading_font and #h4_font_size left
265
+ # justified in the column. The font is set to #body_font after the
266
+ # heading is drawn.
267
+ def h4(text)
268
+ use_heading_font
269
+ @pdf.text "<b><i>#{text}</i></b>", :font_size => @h4_font_size
270
+ use_body_font
271
+ end
272
+ # Writes body text. Paragraphs will be reflowed for optimal placement of
273
+ # text. Text separated by two line separators (e.g., \n\n, although the
274
+ # separator is platform dependent). The text will be placed with full
275
+ # justification.
276
+ def body(text)
277
+ # Transform the text a little.
278
+ paras = text.split(%r(#{$/}{2}))
279
+ paras.map! { |para| para.split($/).join(" ").squeeze(" ") }
280
+ text = paras.join("\n\n")
281
+
282
+ @pdf.text "#{text}\n", :font_size => @body_font_size, :justification => :full
283
+ end
284
+ # Writes code text. Newlines and spaces will be preserved.
285
+ def pre(text)
286
+ use_code_font
287
+ @pdf.text "#{text}\n", :font_size => @body_font_size
288
+ use_body_font
289
+ end
290
+
291
+ # Draws a horizontal line with the specified style and colour.
292
+ def hline(style = PDF::Writer::StrokeStyle::DEFAULT,
293
+ color = Color::RGB::Black)
294
+ @pdf.y -= 2.5
295
+ @pdf.save_state
296
+ @pdf.stroke_style style
297
+ @pdf.stroke_color! color
298
+ x0 = @pdf.left_margin
299
+ x1 = @pdf.left_margin + pdf.column_width
300
+ @pdf.line(x0, @pdf.y, x1, @pdf.y)
301
+ @pdf.stroke
302
+ @pdf.restore_state
303
+ @pdf.y -= 2.5
304
+ end
305
+
306
+ # Writes the Quick Reference to disk.
307
+ def save_as(filename)
308
+ @pdf.save_as(filename)
309
+ end
310
+
311
+ # Generates the PDF document as a string.
312
+ def render
313
+ @pdf.render
314
+ end
315
+
316
+ alias to_s render
317
+
318
+ # Creates a QuickRef document and then calls #instance_eval on the
319
+ # document. This allows for a more natural use of the QuickRef class as
320
+ # a DSL for creating these documents.
321
+ #
322
+ # === Using #make
323
+ # PDF::QuickRef.make do # 3-column LETTER
324
+ # title "My QuickRef"
325
+ # h1 "H1 Text"
326
+ # lines "Text to put after the header."
327
+ # save_as "MyQuickRef.pdf"
328
+ # end
329
+ def self.make(*args, &block)
330
+ qr = PDF::QuickRef.new(*args)
331
+ qr.__send__(:instance_eval, &block)
332
+ end
333
+ end