pdf-writer 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/ChangeLog +44 -0
  2. data/LICENCE +118 -0
  3. data/README +32 -0
  4. data/bin/loader +54 -0
  5. data/bin/manual +22 -0
  6. data/bin/manual.bat +2 -0
  7. data/demo/chunkybacon.rb +28 -0
  8. data/demo/code.rb +63 -0
  9. data/demo/colornames.rb +843 -0
  10. data/demo/demo.rb +65 -0
  11. data/demo/gettysburg.rb +58 -0
  12. data/demo/hello.rb +18 -0
  13. data/demo/individual-i.rb +81 -0
  14. data/demo/pac.rb +62 -0
  15. data/demo/pagenumber.rb +67 -0
  16. data/demo/qr-language.rb +573 -0
  17. data/demo/qr-library.rb +371 -0
  18. data/images/chunkybacon.jpg +0 -0
  19. data/images/chunkybacon.png +0 -0
  20. data/images/pdfwriter-icon.jpg +0 -0
  21. data/images/pdfwriter-small.jpg +0 -0
  22. data/lib/pdf/charts.rb +13 -0
  23. data/lib/pdf/charts/stddev.rb +431 -0
  24. data/lib/pdf/grid.rb +135 -0
  25. data/lib/pdf/math.rb +108 -0
  26. data/lib/pdf/quickref.rb +330 -0
  27. data/lib/pdf/simpletable.rb +946 -0
  28. data/lib/pdf/techbook.rb +890 -0
  29. data/lib/pdf/writer.rb +2661 -0
  30. data/lib/pdf/writer/arc4.rb +63 -0
  31. data/lib/pdf/writer/fontmetrics.rb +201 -0
  32. data/lib/pdf/writer/fonts/Courier-Bold.afm +342 -0
  33. data/lib/pdf/writer/fonts/Courier-BoldOblique.afm +342 -0
  34. data/lib/pdf/writer/fonts/Courier-Oblique.afm +342 -0
  35. data/lib/pdf/writer/fonts/Courier.afm +342 -0
  36. data/lib/pdf/writer/fonts/Helvetica-Bold.afm +2827 -0
  37. data/lib/pdf/writer/fonts/Helvetica-BoldOblique.afm +2827 -0
  38. data/lib/pdf/writer/fonts/Helvetica-Oblique.afm +3051 -0
  39. data/lib/pdf/writer/fonts/Helvetica.afm +3051 -0
  40. data/lib/pdf/writer/fonts/MustRead.html +1 -0
  41. data/lib/pdf/writer/fonts/Symbol.afm +213 -0
  42. data/lib/pdf/writer/fonts/Times-Bold.afm +2588 -0
  43. data/lib/pdf/writer/fonts/Times-BoldItalic.afm +2384 -0
  44. data/lib/pdf/writer/fonts/Times-Italic.afm +2667 -0
  45. data/lib/pdf/writer/fonts/Times-Roman.afm +2419 -0
  46. data/lib/pdf/writer/fonts/ZapfDingbats.afm +225 -0
  47. data/lib/pdf/writer/graphics.rb +727 -0
  48. data/lib/pdf/writer/graphics/imageinfo.rb +365 -0
  49. data/lib/pdf/writer/lang.rb +43 -0
  50. data/lib/pdf/writer/lang/en.rb +77 -0
  51. data/lib/pdf/writer/object.rb +23 -0
  52. data/lib/pdf/writer/object/action.rb +40 -0
  53. data/lib/pdf/writer/object/annotation.rb +42 -0
  54. data/lib/pdf/writer/object/catalog.rb +39 -0
  55. data/lib/pdf/writer/object/contents.rb +68 -0
  56. data/lib/pdf/writer/object/destination.rb +40 -0
  57. data/lib/pdf/writer/object/encryption.rb +53 -0
  58. data/lib/pdf/writer/object/font.rb +76 -0
  59. data/lib/pdf/writer/object/fontdescriptor.rb +34 -0
  60. data/lib/pdf/writer/object/fontencoding.rb +39 -0
  61. data/lib/pdf/writer/object/image.rb +168 -0
  62. data/lib/pdf/writer/object/info.rb +55 -0
  63. data/lib/pdf/writer/object/outline.rb +30 -0
  64. data/lib/pdf/writer/object/outlines.rb +30 -0
  65. data/lib/pdf/writer/object/page.rb +195 -0
  66. data/lib/pdf/writer/object/pages.rb +115 -0
  67. data/lib/pdf/writer/object/procset.rb +46 -0
  68. data/lib/pdf/writer/object/viewerpreferences.rb +74 -0
  69. data/lib/pdf/writer/ohash.rb +58 -0
  70. data/lib/pdf/writer/oreader.rb +25 -0
  71. data/lib/pdf/writer/state.rb +48 -0
  72. data/lib/pdf/writer/strokestyle.rb +138 -0
  73. data/manual.pwd +5151 -0
  74. metadata +147 -0
@@ -0,0 +1,135 @@
1
+ #! /usr/bin/env ruby
2
+ #--
3
+ # PDF::Writer for Ruby.
4
+ # http://rubyforge.org/projects/ruby-pdf/
5
+ # Copyright 2003 - 2005 Austin Ziegler.
6
+ #
7
+ # Licensed under a MIT-style licence. See LICENCE in the main distribution
8
+ # for full licensing information.
9
+ #
10
+ # $Id: grid.rb,v 1.1 2005/06/07 04:19:58 austin Exp $
11
+ #++
12
+ require 'pdf/writer'
13
+
14
+ class PDF::Grid
15
+ # The scale of the grid lines in one direction. The scale always starts
16
+ # from the top or left of the page, depending on whether this is the X
17
+ # axis or Y axis. Minor lines are drawn before major lines.
18
+ class Scale
19
+ def initialize
20
+ @initial_gap = 0
21
+
22
+ yield self if block_given?
23
+ end
24
+
25
+ # The initial gap between the top or left of the page and the first
26
+ # grid line.
27
+ attr_accessor :initial_gap
28
+ # Major grid line style. The default is unset, which uses the current
29
+ # line style.
30
+ attr_accessor :major_style
31
+ # Major grid line colour. The default is unset, which uses the current
32
+ # line colour.
33
+ attr_accessor :major_color
34
+ # The number of units between each major line.
35
+ attr_accessor :major_step
36
+ # Minor grid line style. The default is unset, which uses the current
37
+ # line style.
38
+ attr_accessor :minor_style
39
+ # Minor grid line colour. The default is unset, which uses the current
40
+ # line colour.
41
+ attr_accessor :minor_color
42
+ # The number of units between each minor line.
43
+ attr_accessor :minor_step
44
+ end
45
+
46
+ def initialize
47
+ yield self if block_given?
48
+ end
49
+
50
+ # The X axis scale of the grid. X axis lines are drawn first.
51
+ attr_accessor :x_scale
52
+ # The Y axis scale of the grid. X axis lines are drawn first.
53
+ attr_accessor :y_scale
54
+
55
+ # Renders the grid on the document.
56
+ def render_on(pdf)
57
+ pdf.save_state
58
+
59
+ if @x_scale.minor_step and @x_scale.minor_step > 0
60
+ pdf.stroke_color! @x_scale.minor_color if @x_scale.minor_color
61
+ pdf.stroke_style! @x_scale.minor_style if @x_scale.minor_style
62
+
63
+ start = @x_scale.initial_gap
64
+ step = @x_scale.minor_step
65
+
66
+ start.step(pdf.page_width, step) do |x|
67
+ line(x, 0, x, pdf.page_height).stroke
68
+ end
69
+ end
70
+
71
+ if @y_scale.minor_step and @y_scale.minor_step > 0
72
+ pdf.stroke_color! @y_scale.minor_color if @y_scale.minor_color
73
+ pdf.stroke_style! @y_scale.minor_style if @y_scale.minor_style
74
+
75
+ start = pdf.page_height - @y_scale.initial_gap
76
+ step = -@y_scale.minor_step
77
+
78
+ start.step(0, step) do |y|
79
+ line(0, y, pdf.page_width, y).stroke
80
+ end
81
+ end
82
+
83
+ if @x_scale.major_step and @x_scale.major_step > 0
84
+ pdf.stroke_color! @x_scale.major_color if @x_scale.major_color
85
+ pdf.stroke_style! @x_scale.major_style if @x_scale.major_style
86
+
87
+ start = @x_scale.initial_gap
88
+ step = @x_scale.major_step
89
+
90
+ start.step(pdf.page_width, step) do |x|
91
+ line(x, 0, x, pdf.page_height).stroke
92
+ end
93
+ end
94
+
95
+ if @y_scale.major_step and @y_scale.major_step > 0
96
+ pdf.stroke_color! @y_scale.major_color if @y_scale.major_color
97
+ pdf.stroke_style! @y_scale.major_style if @y_scale.major_style
98
+
99
+ start = pdf.page_height - @y_scale.initial_gap
100
+ step = -@y_scale.major_step
101
+
102
+ start.step(0, step) do |y|
103
+ line(0, y, pdf.page_width, y).stroke
104
+ end
105
+ end
106
+
107
+ # stroke_color(Color::Grey60)
108
+ # y = absolute_top_margin
109
+ # line(0, y, @page_width, y).stroke
110
+ # line(0, @bottom_margin, @page_width, @bottom_margin).stroke
111
+ # line(@left_margin, 0, @left_margin, @page_height).stroke
112
+ # x = absolute_right_margin
113
+ # line(x, 0, x, @page_height).stroke
114
+ # y = @page_height / 2.0
115
+ # line(0, y, @left_margin, y).stroke
116
+ # x = absolute_right_margin
117
+ # line(x, y, @page_width, y).stroke
118
+ # x = @page_width / 2.0
119
+ # line(x, 0, x, @bottom_margin).stroke
120
+ # y = absolute_top_margin
121
+ # line(x, y, x, @page_height).stroke
122
+
123
+ # 0.step(@page_width, 10) do |x|
124
+ # add_text(x, 0, 3, x.to_s)
125
+ # add_text(x, @page_height - 3, 3, x.to_s)
126
+ # end
127
+
128
+ # 0.step(@page_height, 10) do |y|
129
+ # add_text(0, y, 3, y.to_s)
130
+ # add_text(@page_width - 5, y, 3, y.to_s)
131
+ # end
132
+
133
+ restore_state
134
+ end
135
+ end
@@ -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: math.rb,v 1.3 2005/05/16 20:44:34 austin Exp $
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,330 @@
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: quickref.rb,v 1.7 2005/06/08 12:16:11 austin Exp $
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.0.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)
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
+ # Put lines between the columns.
111
+ all = @pdf.open_object
112
+ @pdf.save_state
113
+ @pdf.stroke_color! Color::Black
114
+ @pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT
115
+ (1 .. (columns - 1)).each do |ii|
116
+ x = @pdf.left_margin + (@pdf.column_width * ii)
117
+ x += (@pdf.column_gutter * (ii - 0.5))
118
+ @pdf.line(x, @pdf.page_height - @pdf.top_margin, x, @pdf.bottom_margin)
119
+ @pdf.stroke
120
+ end
121
+ @pdf.restore_state
122
+ @pdf.close_object
123
+ @pdf.add_object(all, :all_pages)
124
+ end
125
+
126
+ # Access to the raw PDF canvas for normal PDF::Writer configuration.
127
+ attr_reader :pdf
128
+
129
+ # The name of the font that will be used for #title text. The default
130
+ # font is Times-Roman.
131
+ attr_accessor :title_font
132
+ # The font encoding for #title text.
133
+ attr_accessor :title_font_encoding
134
+ # The size #title text. The default is 14 points.
135
+ attr_accessor :title_font_size
136
+
137
+ # The name of the font that will be used for #h1, #h2, #h3, and #h4
138
+ # text. The default is Times-Roman.
139
+ attr_accessor :heading_font
140
+ # The font encoding for #h1, #h2, #h3, and #h4 text.
141
+ attr_accessor :heading_font_encoding
142
+ # The size #h1 text. The default is 11 points.
143
+ attr_accessor :h1_font_size
144
+ # The size #h2 text. The default is 9 points.
145
+ attr_accessor :h2_font_size
146
+ # The size #h3 text. The default is 8 points.
147
+ attr_accessor :h3_font_size
148
+ # The size #h4 text. The default is 7 points.
149
+ attr_accessor :h4_font_size
150
+
151
+ # The name of the font that will be used for #body, #lines, and #pairs
152
+ # text. The default is 'Times-Roman'.
153
+ attr_accessor :body_font
154
+ # The font encoding for #body, #lines, and #pairs text.
155
+ attr_accessor :body_font_encoding
156
+ # The name of the font that will be used for #code, #codelines, and
157
+ # #codepairs text; this is generally a fixed-pitch font. The default is
158
+ # 'Courier'.
159
+ attr_accessor :code_font
160
+ # The font encoding for #code, #codelines, and #codepairs text.
161
+ attr_accessor :code_font_encoding
162
+ # The size #body and #code text. The default is 7 points.
163
+ attr_accessor :body_font_size
164
+
165
+ # Creates a two-column zebra-striped table using the #body font. Each
166
+ # line of the text is a separate row; the two columns are separated by
167
+ # tab characters.
168
+ def pairs(text)
169
+ data = text.split($/).map do |line|
170
+ one, two = line.split(/\t/)
171
+ { 'one' => one, 'two' => two }
172
+ end
173
+ @ptab.data.replace data
174
+ @ptab.render_on(@pdf)
175
+ @pdf.text "\n", :font_size => @body_font_size
176
+ end
177
+ # Creates a two-column zebra-striped table using the #code font. Each
178
+ # line of the text is a separate row; the two columns are separated by
179
+ # tab characters.
180
+ def codepairs(text)
181
+ data = text.split($/).map do |line|
182
+ one, two = line.split(/\t/)
183
+ { 'one' => one, 'two' => two }
184
+ end
185
+ @ptab.data.replace data
186
+ use_code_font
187
+ @ptab.render_on(@pdf)
188
+ use_body_font
189
+ @pdf.text "\n", :font_size => @body_font_size
190
+ end
191
+ # Creates a one-column zebra-striped table using the #body font. Each
192
+ # line of the text is a separate row.
193
+ def lines(text)
194
+ data = text.split($/).map { |line| { "line" => line } }
195
+ @ltab.data.replace data
196
+ @ltab.render_on(@pdf)
197
+ @pdf.text "\n", :font_size => @body_font_size
198
+ end
199
+ # Creates a one-column zebra-striped table using the #code font. Each
200
+ # line of the text is a separate row.
201
+ def codelines(text)
202
+ data = text.split($/).map { |line| { "line" => line } }
203
+ @ltab.data.replace data
204
+ use_code_font
205
+ @ltab.render_on(@pdf)
206
+ use_body_font
207
+ @pdf.text "\n", :font_size => @body_font_size
208
+ end
209
+
210
+ # Change the current font to the #title font.
211
+ def use_title_font
212
+ @pdf.select_font @title_font, @title_font_encoding
213
+ end
214
+ # Change the current font to the heading font (used normally by #h1,
215
+ # #h2, #h3, and #h4|).
216
+ def use_heading_font
217
+ @pdf.select_font @heading_font, @heading_font_encoding
218
+ end
219
+ # Change the current font to the #body font.
220
+ def use_body_font
221
+ @pdf.select_font @body_font, @body_font_encoding
222
+ end
223
+ # Change the current font to the #code font.
224
+ def use_code_font
225
+ @pdf.select_font @code_font, @code_font_encoding
226
+ end
227
+
228
+ # Writes the +text+ with the #title_font and #title_font_size centered
229
+ # in the column. After the title has been written, an #hline will be
230
+ # drawn under the title. The font is set to #body_font after the title
231
+ # is drawn.
232
+ def title(text)
233
+ use_title_font
234
+ @pdf.text text, :font_size => @title_font_size, :justification => :center
235
+ use_body_font
236
+ hline
237
+ end
238
+ # Writes the +text+ with the #heading_font and #h1_font_size left
239
+ # justified in the column. The font is set to #body_font after the
240
+ # heading is drawn.
241
+ def h1(text)
242
+ use_heading_font
243
+ @pdf.text text, :font_size => @h1_font_size
244
+ use_body_font
245
+ end
246
+ # Writes the +text+ with the #heading_font and #h2_font_size left
247
+ # justified in the column. The font is set to #body_font after the
248
+ # heading is drawn.
249
+ def h2(text)
250
+ use_heading_font
251
+ @pdf.text "<i>#{text}</i>", :font_size => @h2_font_size
252
+ use_body_font
253
+ end
254
+ # Writes the +text+ with the #heading_font and #h3_font_size left
255
+ # justified in the column. The font is set to #body_font after the
256
+ # heading is drawn.
257
+ def h3(text)
258
+ use_heading_font
259
+ @pdf.text "<i>#{text}</i>", :font_size => @h3_font_size
260
+ use_body_font
261
+ end
262
+ # Writes the +text+ with the #heading_font and #h4_font_size left
263
+ # justified in the column. The font is set to #body_font after the
264
+ # heading is drawn.
265
+ def h4(text)
266
+ use_heading_font
267
+ @pdf.text "<b><i>#{text}</i></b>", :font_size => @h4_font_size
268
+ use_body_font
269
+ end
270
+ # Writes body text. Paragraphs will be reflowed for optimal placement of
271
+ # text. Text separated by two line separators (e.g., \n\n, although the
272
+ # separator is platform dependent). The text will be placed with full
273
+ # justification.
274
+ def body(text)
275
+ # Transform the text a little.
276
+ paras = text.split(%r(#{$/}{2}))
277
+ paras.map! { |para| para.split($/).join(" ").squeeze(" ") }
278
+ text = paras.join("\n\n")
279
+
280
+ @pdf.text "#{text}\n", :font_size => @body_font_size, :justification => :full
281
+ end
282
+ # Writes code text. Newlines and spaces will be preserved.
283
+ def pre(text)
284
+ use_code_font
285
+ @pdf.text "#{text}\n", :font_size => @body_font_size
286
+ use_body_font
287
+ end
288
+
289
+ # Draws a horizontal line with the specified style and colour.
290
+ def hline(style = PDF::Writer::StrokeStyle::DEFAULT, color = Color::Black)
291
+ @pdf.y -= 2.5
292
+ @pdf.save_state
293
+ @pdf.stroke_style style
294
+ @pdf.stroke_color! color
295
+ x0 = @pdf.left_margin
296
+ x1 = @pdf.left_margin + pdf.column_width
297
+ @pdf.line(x0, @pdf.y, x1, @pdf.y)
298
+ @pdf.stroke
299
+ @pdf.restore_state
300
+ @pdf.y -= 2.5
301
+ end
302
+
303
+ # Writes the Quick Reference to disk.
304
+ def save_as(filename, compressed = true)
305
+ @pdf.save_as(filename, compressed)
306
+ end
307
+
308
+ # Generates the PDF document as a string.
309
+ def render
310
+ @pdf.render
311
+ end
312
+
313
+ alias to_s render
314
+
315
+ # Creates a QuickRef document and then calls #instance_eval on the
316
+ # document. This allows for a more natural use of the QuickRef class as
317
+ # a DSL for creating these documents.
318
+ #
319
+ # === Using #make
320
+ # PDF::QuickRef.make do # 3-column LETTER
321
+ # title "My QuickRef"
322
+ # h1 "H1 Text"
323
+ # lines "Text to put after the header."
324
+ # save_as "MyQuickRef.pdf"
325
+ # end
326
+ def self.make(*args, &block)
327
+ qr = PDF::QuickRef.new(*args)
328
+ qr.__send__(:instance_eval, &block)
329
+ end
330
+ end