prawn 0.14.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +2 -1
  3. data/Rakefile +12 -0
  4. data/lib/prawn.rb +9 -21
  5. data/lib/prawn/document.rb +95 -68
  6. data/lib/prawn/document/bounding_box.rb +22 -4
  7. data/lib/prawn/document/column_box.rb +2 -0
  8. data/lib/prawn/document/graphics_state.rb +1 -1
  9. data/lib/prawn/document/internals.rb +2 -2
  10. data/lib/prawn/document/snapshot.rb +2 -1
  11. data/lib/prawn/document/span.rb +2 -0
  12. data/lib/prawn/encoding.rb +1 -1
  13. data/lib/prawn/font.rb +89 -75
  14. data/lib/prawn/font/afm.rb +3 -0
  15. data/lib/prawn/font/dfont.rb +1 -0
  16. data/lib/prawn/font/ttf.rb +2 -0
  17. data/lib/prawn/font_metric_cache.rb +3 -1
  18. data/lib/prawn/graphics.rb +2 -14
  19. data/lib/prawn/graphics/cap_style.rb +1 -0
  20. data/lib/prawn/graphics/color.rb +1 -0
  21. data/lib/prawn/graphics/dash.rb +3 -2
  22. data/lib/prawn/graphics/join_style.rb +2 -0
  23. data/lib/prawn/graphics/patterns.rb +1 -0
  24. data/lib/prawn/graphics/transformation.rb +1 -0
  25. data/lib/prawn/graphics/transparency.rb +2 -0
  26. data/lib/prawn/image_handler.rb +2 -0
  27. data/lib/prawn/images.rb +5 -0
  28. data/lib/prawn/images/image.rb +1 -0
  29. data/lib/prawn/images/jpg.rb +3 -0
  30. data/lib/prawn/images/png.rb +2 -0
  31. data/lib/prawn/layout.rb +8 -13
  32. data/lib/prawn/layout/grid.rb +15 -3
  33. data/lib/prawn/measurement_extensions.rb +4 -0
  34. data/lib/prawn/measurements.rb +2 -0
  35. data/lib/prawn/outline.rb +3 -1
  36. data/lib/prawn/repeater.rb +3 -1
  37. data/lib/prawn/security.rb +15 -7
  38. data/lib/prawn/security/arcfour.rb +52 -0
  39. data/lib/prawn/soft_mask.rb +3 -1
  40. data/lib/prawn/stamp.rb +2 -0
  41. data/lib/prawn/table.rb +2 -0
  42. data/lib/prawn/table/cell.rb +4 -1
  43. data/lib/prawn/table/cell/image.rb +1 -2
  44. data/lib/prawn/table/cell/in_table.rb +2 -0
  45. data/lib/prawn/table/cell/span_dummy.rb +1 -0
  46. data/lib/prawn/table/cells.rb +7 -2
  47. data/lib/prawn/table/column_width_calculator.rb +8 -2
  48. data/lib/prawn/text.rb +4 -2
  49. data/lib/prawn/text/box.rb +3 -0
  50. data/lib/prawn/text/formatted/arranger.rb +2 -0
  51. data/lib/prawn/text/formatted/box.rb +55 -50
  52. data/lib/prawn/text/formatted/fragment.rb +2 -0
  53. data/lib/prawn/text/formatted/line_wrap.rb +1 -0
  54. data/lib/prawn/text/formatted/parser.rb +2 -0
  55. data/lib/prawn/text/formatted/wrap.rb +2 -0
  56. data/lib/prawn/utilities.rb +5 -3
  57. data/manual/graphics/common_lines.rb +2 -0
  58. data/manual/text/group.rb +2 -0
  59. data/manual/text/text.rb +1 -1
  60. data/prawn.gemspec +4 -3
  61. data/spec/grid_spec.rb +11 -0
  62. data/spec/object_store_spec.rb +1 -96
  63. data/spec/reference_spec.rb +0 -57
  64. data/spec/spec_helper.rb +7 -0
  65. data/spec/table_spec.rb +26 -0
  66. metadata +172 -185
  67. data/lib/pdf/core.rb +0 -35
  68. data/lib/pdf/core/annotations.rb +0 -60
  69. data/lib/pdf/core/byte_string.rb +0 -9
  70. data/lib/pdf/core/destinations.rb +0 -90
  71. data/lib/pdf/core/document_state.rb +0 -79
  72. data/lib/pdf/core/filter_list.rb +0 -51
  73. data/lib/pdf/core/filters.rb +0 -36
  74. data/lib/pdf/core/graphics_state.rb +0 -89
  75. data/lib/pdf/core/literal_string.rb +0 -16
  76. data/lib/pdf/core/name_tree.rb +0 -177
  77. data/lib/pdf/core/object_store.rb +0 -311
  78. data/lib/pdf/core/outline.rb +0 -315
  79. data/lib/pdf/core/page.rb +0 -212
  80. data/lib/pdf/core/page_geometry.rb +0 -126
  81. data/lib/pdf/core/pdf_object.rb +0 -99
  82. data/lib/pdf/core/reference.rb +0 -103
  83. data/lib/pdf/core/stream.rb +0 -98
  84. data/lib/pdf/core/text.rb +0 -275
  85. data/lib/prawn/templates.rb +0 -75
  86. data/spec/filters_spec.rb +0 -34
  87. data/spec/name_tree_spec.rb +0 -112
  88. data/spec/pdf_object_spec.rb +0 -172
  89. data/spec/stream_spec.rb +0 -58
  90. data/spec/template_spec_obsolete.rb +0 -352
@@ -9,6 +9,8 @@ require_relative "bounding_box"
9
9
  module Prawn
10
10
  class Document
11
11
 
12
+ # @group Experimental API
13
+
12
14
  # A column box is a bounding box with the additional property that when
13
15
  # text flows past the bottom, it will wrap first to another column on the
14
16
  # same page, and only flow to the next page when all the columns are
@@ -9,7 +9,7 @@
9
9
 
10
10
  module Prawn
11
11
  class Document
12
- module GraphicsState
12
+ module GraphicsState # @private
13
13
 
14
14
  # Pushes the current graphics state on to the graphics state stack so we
15
15
  # can restore it when finished with a change we want to isolate (such as
@@ -14,8 +14,8 @@ module Prawn
14
14
  # low level PDF functionality as defined by Adobe's specification, chances
15
15
  # are you won't need anything you find here.
16
16
  #
17
- module Internals
18
-
17
+ # @private
18
+ module Internals
19
19
  # Creates a new Prawn::Reference and adds it to the Document's object
20
20
  # list. The +data+ argument is anything that Prawn::PdfObject() can convert.
21
21
  #
@@ -10,8 +10,9 @@ require 'delegate'
10
10
 
11
11
  module Prawn
12
12
  class Document
13
+ # @private
14
+
13
15
  module Snapshot
14
-
15
16
  RollbackTransaction = Class.new(StandardError)
16
17
 
17
18
  # Call this within a +transaction+ block to roll back the transaction and
@@ -8,6 +8,8 @@
8
8
 
9
9
  module Prawn
10
10
  class Document
11
+ # @group Stable API
12
+
11
13
  # A span is a special purpose bounding box that allows a column of
12
14
  # elements to be positioned relative to the margin_box.
13
15
  #
@@ -5,7 +5,7 @@
5
5
  # This is free software. Please see the LICENSE and COPYING files for details.
6
6
  #
7
7
  module Prawn
8
- module Encoding
8
+ module Encoding # @private
9
9
  # Map between unicode and WinAnsiEnoding
10
10
  #
11
11
  class WinAnsi #:nodoc:
@@ -14,6 +14,8 @@ require_relative "font_metric_cache"
14
14
  module Prawn
15
15
 
16
16
  class Document
17
+ # @group Stable API
18
+
17
19
  # Without arguments, this returns the currently selected font. Otherwise,
18
20
  # it sets the current font. When a block is used, the font is applied
19
21
  # transactionally and is rolled back when the block exits.
@@ -67,7 +69,10 @@ module Prawn
67
69
  @font
68
70
  end
69
71
 
72
+ # @method font_size(points=nil)
73
+ #
70
74
  # When called with no argument, returns the current font size.
75
+ #
71
76
  # When called with a single argument but no block, sets the current font
72
77
  # size. When a block is used, the font size is applied transactionally and
73
78
  # is rolled back when the block exits. You may still change the font size
@@ -98,59 +103,44 @@ module Prawn
98
103
  @font_size = size_before_yield
99
104
  end
100
105
 
101
- # Sets the font directly, given an actual Font object
102
- # and size.
103
- #
104
- def set_font(font, size=nil) # :nodoc:
105
- @font = font
106
- @font_size = size if size
106
+ # Sets the font size
107
+ def font_size=(size)
108
+ font_size(size)
107
109
  end
108
110
 
109
- # Saves the current font, and then yields. When the block
110
- # finishes, the original font is restored.
111
+ # Returns the width of the given string using the given font. If :size is not
112
+ # specified as one of the options, the string is measured using the current
113
+ # font size. You can also pass :kerning as an option to indicate whether
114
+ # kerning should be used when measuring the width (defaults to +false+).
111
115
  #
112
- def save_font
113
- @font ||= find_font("Helvetica")
114
- original_font = @font
115
- original_size = @font_size
116
-
117
- yield
118
- ensure
119
- set_font(original_font, original_size) if original_font
120
- end
121
-
122
- # Looks up the given font using the given criteria. Once a font has been
123
- # found by that matches the criteria, it will be cached to subsequent lookups
124
- # for that font will return the same object.
116
+ # Note that the string _must_ be encoded properly for the font being used.
117
+ # For AFM fonts, this is WinAnsi. For TTF, make sure the font is encoded as
118
+ # UTF-8. You can use the Font#normalize_encoding method to make sure strings
119
+ # are in an encoding appropriate for the current font.
125
120
  #--
126
- # Challenges involved: the name alone is not sufficient to uniquely identify
127
- # a font (think dfont suitcases that can hold multiple different fonts in a
128
- # single file). Thus, the :name key is included in the cache key.
121
+ # For the record, this method used to be a method of Font (and still delegates
122
+ # to width computations on Font). However, having the primary interface for
123
+ # calculating string widths exist on Font made it tricky to write extensions
124
+ # for Prawn in which widths are computed differently (e.g., taking formatting
125
+ # tags into account, or the like).
129
126
  #
130
- # It is further complicated, however, since fonts in some formats (like the
131
- # dfont suitcases) can be identified either by numeric index, OR by their
132
- # name within the suitcase, and both should hash to the same font object
133
- # (to avoid the font being embedded multiple times). This is not yet implemented,
134
- # which means if someone selects a font both by name, and by index, the
135
- # font will be embedded twice. Since we do font subsetting, this double
136
- # embedding won't be catastrophic, just annoying.
137
- # ++
138
- def find_font(name, options={}) #:nodoc:
139
- if font_families.key?(name)
140
- family, name = name, font_families[name][options[:style] || :normal]
141
- if name.is_a?(::Hash)
142
- options = options.merge(name)
143
- name = options[:file]
144
- end
145
- end
146
- key = "#{name}:#{options[:font] || 0}"
147
- font_registry[key] ||= Font.load(self, name, options.merge(:family => family))
148
- end
127
+ # By putting width_of here, on Document itself, extensions may easily override
128
+ # it and redefine the width calculation behavior.
129
+ #++
130
+ def width_of(string, options={})
131
+ if p = options[:inline_format]
132
+ p = [] unless p.is_a?(Array)
149
133
 
150
- # Hash of Font objects keyed by names
151
- #
152
- def font_registry #:nodoc:
153
- @font_registry ||= {}
134
+ # Build up an Arranger with the entire string on one line, finalize it,
135
+ # and find its width.
136
+ arranger = Prawn::Text::Formatted::Arranger.new(self, options)
137
+ arranger.consumed = self.text_formatter.format(string, *p)
138
+ arranger.finalize_line
139
+
140
+ arranger.line_width
141
+ else
142
+ width_of_string(string, options)
143
+ end
154
144
  end
155
145
 
156
146
  # Hash that maps font family names to their styled individual font names.
@@ -197,39 +187,63 @@ module Prawn
197
187
  })
198
188
  end
199
189
 
200
- # Returns the width of the given string using the given font. If :size is not
201
- # specified as one of the options, the string is measured using the current
202
- # font size. You can also pass :kerning as an option to indicate whether
203
- # kerning should be used when measuring the width (defaults to +false+).
190
+ # @group Experimental API
191
+
192
+ # Sets the font directly, given an actual Font object
193
+ # and size.
204
194
  #
205
- # Note that the string _must_ be encoded properly for the font being used.
206
- # For AFM fonts, this is WinAnsi. For TTF, make sure the font is encoded as
207
- # UTF-8. You can use the Font#normalize_encoding method to make sure strings
208
- # are in an encoding appropriate for the current font.
209
- #--
210
- # For the record, this method used to be a method of Font (and still delegates
211
- # to width computations on Font). However, having the primary interface for
212
- # calculating string widths exist on Font made it tricky to write extensions
213
- # for Prawn in which widths are computed differently (e.g., taking formatting
214
- # tags into account, or the like).
195
+ def set_font(font, size=nil) # :nodoc:
196
+ @font = font
197
+ @font_size = size if size
198
+ end
199
+
200
+ # Saves the current font, and then yields. When the block
201
+ # finishes, the original font is restored.
215
202
  #
216
- # By putting width_of here, on Document itself, extensions may easily override
217
- # it and redefine the width calculation behavior.
218
- #++
219
- def width_of(string, options={})
220
- if p = options[:inline_format]
221
- p = [] unless p.is_a?(Array)
203
+ def save_font
204
+ @font ||= find_font("Helvetica")
205
+ original_font = @font
206
+ original_size = @font_size
222
207
 
223
- # Build up an Arranger with the entire string on one line, finalize it,
224
- # and find its width.
225
- arranger = Prawn::Text::Formatted::Arranger.new(self, options)
226
- arranger.consumed = self.text_formatter.format(string, *p)
227
- arranger.finalize_line
208
+ yield
209
+ ensure
210
+ set_font(original_font, original_size) if original_font
211
+ end
228
212
 
229
- arranger.line_width
230
- else
231
- width_of_string(string, options)
213
+ # Looks up the given font using the given criteria. Once a font has been
214
+ # found by that matches the criteria, it will be cached to subsequent lookups
215
+ # for that font will return the same object.
216
+ #--
217
+ # Challenges involved: the name alone is not sufficient to uniquely identify
218
+ # a font (think dfont suitcases that can hold multiple different fonts in a
219
+ # single file). Thus, the :name key is included in the cache key.
220
+ #
221
+ # It is further complicated, however, since fonts in some formats (like the
222
+ # dfont suitcases) can be identified either by numeric index, OR by their
223
+ # name within the suitcase, and both should hash to the same font object
224
+ # (to avoid the font being embedded multiple times). This is not yet implemented,
225
+ # which means if someone selects a font both by name, and by index, the
226
+ # font will be embedded twice. Since we do font subsetting, this double
227
+ # embedding won't be catastrophic, just annoying.
228
+ # ++
229
+ #
230
+ # @private
231
+ def find_font(name, options={}) #:nodoc:
232
+ if font_families.key?(name)
233
+ family, name = name, font_families[name][options[:style] || :normal]
234
+ if name.is_a?(::Hash)
235
+ options = options.merge(name)
236
+ name = options[:file]
237
+ end
232
238
  end
239
+ key = "#{name}:#{options[:font] || 0}"
240
+ font_registry[key] ||= Font.load(self, name, options.merge(:family => family))
241
+ end
242
+
243
+ # Hash of Font objects keyed by names
244
+ #
245
+ def font_registry #:nodoc:
246
+ @font_registry ||= {}
233
247
  end
234
248
 
235
249
  private
@@ -10,6 +10,9 @@ require_relative '../../prawn/encoding'
10
10
 
11
11
  module Prawn
12
12
  class Font
13
+
14
+ # @private
15
+
13
16
  class AFM < Font
14
17
  BUILT_INS = %w[ Courier Helvetica Times-Roman Symbol ZapfDingbats
15
18
  Courier-Bold Courier-Oblique Courier-BoldOblique
@@ -10,6 +10,7 @@ require_relative 'ttf'
10
10
 
11
11
  module Prawn
12
12
  class Font
13
+ # @private
13
14
  class DFont < TTF
14
15
 
15
16
  # Returns a list of the names of all named fonts in the given dfont file.
@@ -12,6 +12,8 @@ require 'ttfunk/subset_collection'
12
12
 
13
13
  module Prawn
14
14
  class Font
15
+
16
+ # @private
15
17
  class TTF < Font
16
18
  attr_reader :ttf, :subsets
17
19
 
@@ -11,7 +11,9 @@ module Prawn
11
11
 
12
12
  # Cache used internally by Prawn::Document instances to calculate the width
13
13
  # of various strings for layout purposes.
14
- class FontMetricCache
14
+ #
15
+ # @private
16
+ class FontMetricCache
15
17
 
16
18
  CacheEntry = Struct.new( :font, :options, :string )
17
19
 
@@ -33,6 +33,8 @@ module Prawn
33
33
  include Transformation
34
34
  include Patterns
35
35
 
36
+ # @group Stable API
37
+
36
38
  #######################################################################
37
39
  # Low level drawing operations must map the point to absolute coords! #
38
40
  #######################################################################
@@ -182,13 +184,6 @@ module Prawn
182
184
  #
183
185
  KAPPA = 4.0 * ((Math.sqrt(2) - 1.0) / 3.0)
184
186
 
185
- # <b>DEPRECATED:</b> Please use <tt>circle</tt> instead.
186
- def circle_at(point, options)
187
- warn "[DEPRECATION] 'circle_at' is deprecated in favor of 'circle'. " +
188
- "'circle_at' will be removed in release 1.1"
189
- circle(point, options[:radius])
190
- end
191
-
192
187
  # Draws a circle of radius <tt>radius</tt> with the centre-point at <tt>point</tt>
193
188
  # as a complete subpath. The drawing point will be moved to the
194
189
  # centre-point upon completion of the drawing the circle.
@@ -199,13 +194,6 @@ module Prawn
199
194
  ellipse(center, radius, radius)
200
195
  end
201
196
 
202
- # <b>DEPRECATED:</b> Please use <tt>ellipse</tt> instead.
203
- def ellipse_at(point, r1, r2=r1)
204
- warn "[DEPRECATION] 'ellipse_at' is deprecated in favor of 'ellipse'. " +
205
- "'ellipse_at' will be removed in release 1.1"
206
- ellipse(point, r1, r2)
207
- end
208
-
209
197
  # Draws an ellipse of +x+ radius <tt>r1</tt> and +y+ radius <tt>r2</tt>
210
198
  # with the centre-point at <tt>point</tt> as a complete subpath. The
211
199
  # drawing point will be moved to the centre-point upon completion of the
@@ -9,6 +9,7 @@
9
9
  module Prawn
10
10
  module Graphics
11
11
  module CapStyle
12
+ # @group Stable API
12
13
 
13
14
  CAP_STYLES = { :butt => 0, :round => 1, :projecting_square => 2 }
14
15
 
@@ -9,6 +9,7 @@
9
9
  module Prawn
10
10
  module Graphics
11
11
  module Color
12
+ # @group Stable API
12
13
 
13
14
  # Sets or returns the fill color.
14
15
  #
@@ -9,6 +9,7 @@
9
9
  module Prawn
10
10
  module Graphics
11
11
  module Dash
12
+ # @group Stable API
12
13
 
13
14
  # Sets the dash pattern for stroked lines and curves or return the
14
15
  # current dash pattern setting if +length+ is nil.
@@ -76,12 +77,12 @@ module Prawn
76
77
  current_dash_state != undashed_setting
77
78
  end
78
79
 
80
+ private
81
+
79
82
  def write_stroke_dash
80
83
  add_content dash_setting
81
84
  end
82
85
 
83
- private
84
-
85
86
  def undashed_setting
86
87
  { :dash => nil, :space => nil, :phase => 0 }
87
88
  end
@@ -11,6 +11,8 @@ module Prawn
11
11
  module JoinStyle
12
12
  JOIN_STYLES = { :miter => 0, :round => 1, :bevel => 2 }
13
13
 
14
+ # @group Stable API
15
+
14
16
  # Sets the join style for stroked lines and curves
15
17
  #
16
18
  # style is one of :miter, :round, or :bevel
@@ -10,6 +10,7 @@
10
10
  module Prawn
11
11
  module Graphics
12
12
  module Patterns
13
+ # @group Stable API
13
14
 
14
15
  # Sets the fill gradient from color1 to color2.
15
16
  # old arguments: point, width, height, color1, color2, options = {}
@@ -10,6 +10,7 @@
10
10
  module Prawn
11
11
  module Graphics
12
12
  module Transformation
13
+ # @group Stable API
13
14
 
14
15
  # Rotate the user space. If a block is not provided, then you must save
15
16
  # and restore the graphics state yourself.
@@ -30,6 +30,8 @@ module Prawn
30
30
  #
31
31
  module Transparency
32
32
 
33
+ # @group Stable API
34
+
33
35
  # Sets the <tt>opacity</tt> and <tt>stroke_opacity</tt> for all
34
36
  # the content within the <tt>block</tt>
35
37
  # If <tt>stroke_opacity</tt> is not provided, then it takes on
@@ -1,4 +1,6 @@
1
1
  module Prawn
2
+ # @group Extension API
3
+
2
4
  def self.image_handler
3
5
  @image_handler ||= ImageHandler.new
4
6
  end
@@ -11,6 +11,8 @@ require 'pathname'
11
11
  module Prawn
12
12
 
13
13
  module Images
14
+ # @group Stable API
15
+
14
16
  # Add the image at filename to the current page. Currently only
15
17
  # JPG and PNG files are supported. (Note that processing PNG
16
18
  # images with alpha channels can be processor and memory intensive.)
@@ -68,9 +70,11 @@ module Prawn
68
70
  info
69
71
  end
70
72
 
73
+
71
74
  # Builds an info object (Prawn::Images::*) and a PDF reference representing
72
75
  # the given image. Return a pair: [pdf_obj, info].
73
76
  #
77
+ # @private
74
78
  def build_image_object(file)
75
79
  io = verify_and_open_image(file)
76
80
  image_content = io.read
@@ -100,6 +104,7 @@ module Prawn
100
104
  # build_image_object), embed the image according to the <tt>options</tt>
101
105
  # given.
102
106
  #
107
+ # @private
103
108
  def embed_image(pdf_obj, info, options)
104
109
  # find where the image will be placed and how big it will be
105
110
  w,h = info.calc_image_dimensions(options)