prawn 2.3.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (203) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/lib/prawn/document/bounding_box.rb +223 -143
  4. data/lib/prawn/document/column_box.rb +61 -26
  5. data/lib/prawn/document/internals.rb +25 -16
  6. data/lib/prawn/document/span.rb +21 -18
  7. data/lib/prawn/document.rb +273 -182
  8. data/lib/prawn/encoding.rb +2 -5
  9. data/lib/prawn/errors.rb +23 -34
  10. data/lib/prawn/font.rb +254 -139
  11. data/lib/prawn/font_metric_cache.rb +18 -16
  12. data/lib/prawn/fonts/afm.rb +99 -57
  13. data/lib/prawn/fonts/dfont.rb +7 -1
  14. data/lib/prawn/fonts/otf.rb +4 -1
  15. data/lib/prawn/fonts/to_unicode_cmap.rb +151 -0
  16. data/lib/prawn/fonts/ttc.rb +7 -2
  17. data/lib/prawn/fonts/ttf.rb +345 -107
  18. data/lib/prawn/fonts.rb +14 -0
  19. data/lib/prawn/graphics/blend_mode.rb +25 -28
  20. data/lib/prawn/graphics/cap_style.rb +9 -12
  21. data/lib/prawn/graphics/color.rb +75 -50
  22. data/lib/prawn/graphics/dash.rb +45 -42
  23. data/lib/prawn/graphics/join_style.rb +18 -12
  24. data/lib/prawn/graphics/patterns.rb +239 -110
  25. data/lib/prawn/graphics/transformation.rb +51 -44
  26. data/lib/prawn/graphics/transparency.rb +16 -40
  27. data/lib/prawn/graphics.rb +370 -260
  28. data/lib/prawn/grid.rb +219 -57
  29. data/lib/prawn/image_handler.rb +27 -10
  30. data/lib/prawn/images/image.rb +8 -10
  31. data/lib/prawn/images/jpg.rb +46 -20
  32. data/lib/prawn/images/png.rb +94 -42
  33. data/lib/prawn/images.rb +70 -81
  34. data/lib/prawn/measurement_extensions.rb +39 -8
  35. data/lib/prawn/measurements.rb +60 -5
  36. data/lib/prawn/outline.rb +120 -113
  37. data/lib/prawn/repeater.rb +52 -36
  38. data/lib/prawn/security/arcfour.rb +4 -4
  39. data/lib/prawn/security.rb +106 -98
  40. data/lib/prawn/soft_mask.rb +42 -30
  41. data/lib/prawn/stamp.rb +38 -42
  42. data/lib/prawn/text/box.rb +156 -105
  43. data/lib/prawn/text/formatted/arranger.rb +121 -41
  44. data/lib/prawn/text/formatted/box.rb +239 -163
  45. data/lib/prawn/text/formatted/fragment.rb +130 -14
  46. data/lib/prawn/text/formatted/line_wrap.rb +49 -38
  47. data/lib/prawn/text/formatted/parser.rb +116 -74
  48. data/lib/prawn/text/formatted/wrap.rb +25 -26
  49. data/lib/prawn/text/formatted.rb +75 -0
  50. data/lib/prawn/text.rb +456 -211
  51. data/lib/prawn/transformation_stack.rb +29 -10
  52. data/lib/prawn/utilities.rb +13 -13
  53. data/lib/prawn/version.rb +2 -1
  54. data/lib/prawn/view.rb +69 -54
  55. data/lib/prawn.rb +24 -18
  56. data.tar.gz.sig +0 -0
  57. metadata +55 -262
  58. metadata.gz.sig +3 -4
  59. data/.yardopts +0 -10
  60. data/Gemfile +0 -5
  61. data/Rakefile +0 -54
  62. data/manual/absolute_position.pdf +0 -0
  63. data/manual/basic_concepts/adding_pages.rb +0 -26
  64. data/manual/basic_concepts/basic_concepts.rb +0 -43
  65. data/manual/basic_concepts/creation.rb +0 -38
  66. data/manual/basic_concepts/cursor.rb +0 -32
  67. data/manual/basic_concepts/measurement.rb +0 -24
  68. data/manual/basic_concepts/origin.rb +0 -37
  69. data/manual/basic_concepts/other_cursor_helpers.rb +0 -39
  70. data/manual/basic_concepts/view.rb +0 -48
  71. data/manual/bounding_box/bounding_box.rb +0 -41
  72. data/manual/bounding_box/bounds.rb +0 -48
  73. data/manual/bounding_box/canvas.rb +0 -23
  74. data/manual/bounding_box/creation.rb +0 -22
  75. data/manual/bounding_box/indentation.rb +0 -45
  76. data/manual/bounding_box/nesting.rb +0 -52
  77. data/manual/bounding_box/russian_boxes.rb +0 -40
  78. data/manual/bounding_box/stretchy.rb +0 -29
  79. data/manual/contents.rb +0 -35
  80. data/manual/cover.rb +0 -43
  81. data/manual/document_and_page_options/background.rb +0 -25
  82. data/manual/document_and_page_options/document_and_page_options.rb +0 -34
  83. data/manual/document_and_page_options/metadata.rb +0 -25
  84. data/manual/document_and_page_options/page_margins.rb +0 -36
  85. data/manual/document_and_page_options/page_size.rb +0 -34
  86. data/manual/document_and_page_options/print_scaling.rb +0 -22
  87. data/manual/example_helper.rb +0 -8
  88. data/manual/graphics/blend_mode.rb +0 -52
  89. data/manual/graphics/circle_and_ellipse.rb +0 -21
  90. data/manual/graphics/color.rb +0 -22
  91. data/manual/graphics/common_lines.rb +0 -29
  92. data/manual/graphics/fill_and_stroke.rb +0 -41
  93. data/manual/graphics/fill_rules.rb +0 -37
  94. data/manual/graphics/gradients.rb +0 -43
  95. data/manual/graphics/graphics.rb +0 -64
  96. data/manual/graphics/helper.rb +0 -27
  97. data/manual/graphics/line_width.rb +0 -36
  98. data/manual/graphics/lines_and_curves.rb +0 -40
  99. data/manual/graphics/polygon.rb +0 -27
  100. data/manual/graphics/rectangle.rb +0 -20
  101. data/manual/graphics/rotate.rb +0 -25
  102. data/manual/graphics/scale.rb +0 -42
  103. data/manual/graphics/soft_masks.rb +0 -44
  104. data/manual/graphics/stroke_cap.rb +0 -30
  105. data/manual/graphics/stroke_dash.rb +0 -47
  106. data/manual/graphics/stroke_join.rb +0 -29
  107. data/manual/graphics/translate.rb +0 -28
  108. data/manual/graphics/transparency.rb +0 -33
  109. data/manual/how_to_read_this_manual.rb +0 -39
  110. data/manual/images/absolute_position.rb +0 -22
  111. data/manual/images/fit.rb +0 -20
  112. data/manual/images/horizontal.rb +0 -24
  113. data/manual/images/images.rb +0 -41
  114. data/manual/images/plain_image.rb +0 -17
  115. data/manual/images/scale.rb +0 -21
  116. data/manual/images/vertical.rb +0 -27
  117. data/manual/images/width_and_height.rb +0 -24
  118. data/manual/layout/boxes.rb +0 -26
  119. data/manual/layout/content.rb +0 -24
  120. data/manual/layout/layout.rb +0 -27
  121. data/manual/layout/simple_grid.rb +0 -22
  122. data/manual/outline/add_subsection_to.rb +0 -60
  123. data/manual/outline/insert_section_after.rb +0 -46
  124. data/manual/outline/outline.rb +0 -33
  125. data/manual/outline/sections_and_pages.rb +0 -66
  126. data/manual/repeatable_content/alternate_page_numbering.rb +0 -36
  127. data/manual/repeatable_content/page_numbering.rb +0 -55
  128. data/manual/repeatable_content/repeatable_content.rb +0 -35
  129. data/manual/repeatable_content/repeater.rb +0 -54
  130. data/manual/repeatable_content/stamp.rb +0 -40
  131. data/manual/security/encryption.rb +0 -28
  132. data/manual/security/permissions.rb +0 -41
  133. data/manual/security/security.rb +0 -28
  134. data/manual/table.rb +0 -16
  135. data/manual/text/alignment.rb +0 -43
  136. data/manual/text/color.rb +0 -24
  137. data/manual/text/column_box.rb +0 -30
  138. data/manual/text/fallback_fonts.rb +0 -41
  139. data/manual/text/font.rb +0 -40
  140. data/manual/text/font_size.rb +0 -44
  141. data/manual/text/font_style.rb +0 -22
  142. data/manual/text/formatted_callbacks.rb +0 -65
  143. data/manual/text/formatted_text.rb +0 -58
  144. data/manual/text/free_flowing_text.rb +0 -50
  145. data/manual/text/inline.rb +0 -40
  146. data/manual/text/kerning_and_character_spacing.rb +0 -38
  147. data/manual/text/leading.rb +0 -24
  148. data/manual/text/line_wrapping.rb +0 -60
  149. data/manual/text/paragraph_indentation.rb +0 -32
  150. data/manual/text/positioned_text.rb +0 -37
  151. data/manual/text/registering_families.rb +0 -51
  152. data/manual/text/rendering_and_color.rb +0 -36
  153. data/manual/text/right_to_left_text.rb +0 -54
  154. data/manual/text/rotation.rb +0 -47
  155. data/manual/text/single_usage.rb +0 -36
  156. data/manual/text/text.rb +0 -75
  157. data/manual/text/text_box_excess.rb +0 -35
  158. data/manual/text/text_box_extensions.rb +0 -48
  159. data/manual/text/text_box_overflow.rb +0 -49
  160. data/manual/text/utf8.rb +0 -27
  161. data/manual/text/win_ansi_charset.rb +0 -62
  162. data/prawn.gemspec +0 -57
  163. data/spec/data/curves.pdf +0 -66
  164. data/spec/extensions/encoding_helpers.rb +0 -11
  165. data/spec/prawn/document/bounding_box_spec.rb +0 -546
  166. data/spec/prawn/document/column_box_spec.rb +0 -75
  167. data/spec/prawn/document/security_spec.rb +0 -176
  168. data/spec/prawn/document_annotations_spec.rb +0 -76
  169. data/spec/prawn/document_destinations_spec.rb +0 -15
  170. data/spec/prawn/document_grid_spec.rb +0 -99
  171. data/spec/prawn/document_reference_spec.rb +0 -27
  172. data/spec/prawn/document_span_spec.rb +0 -36
  173. data/spec/prawn/document_spec.rb +0 -802
  174. data/spec/prawn/font_metric_cache_spec.rb +0 -54
  175. data/spec/prawn/font_spec.rb +0 -542
  176. data/spec/prawn/graphics/blend_mode_spec.rb +0 -63
  177. data/spec/prawn/graphics/transparency_spec.rb +0 -81
  178. data/spec/prawn/graphics_spec.rb +0 -837
  179. data/spec/prawn/graphics_stroke_styles_spec.rb +0 -229
  180. data/spec/prawn/image_handler_spec.rb +0 -53
  181. data/spec/prawn/images/jpg_spec.rb +0 -20
  182. data/spec/prawn/images/png_spec.rb +0 -283
  183. data/spec/prawn/images_spec.rb +0 -224
  184. data/spec/prawn/measurements_extensions_spec.rb +0 -24
  185. data/spec/prawn/outline_spec.rb +0 -412
  186. data/spec/prawn/repeater_spec.rb +0 -165
  187. data/spec/prawn/soft_mask_spec.rb +0 -74
  188. data/spec/prawn/stamp_spec.rb +0 -172
  189. data/spec/prawn/text/box_spec.rb +0 -1112
  190. data/spec/prawn/text/formatted/arranger_spec.rb +0 -466
  191. data/spec/prawn/text/formatted/box_spec.rb +0 -846
  192. data/spec/prawn/text/formatted/fragment_spec.rb +0 -343
  193. data/spec/prawn/text/formatted/line_wrap_spec.rb +0 -494
  194. data/spec/prawn/text/formatted/parser_spec.rb +0 -697
  195. data/spec/prawn/text_draw_text_spec.rb +0 -149
  196. data/spec/prawn/text_rendering_mode_spec.rb +0 -48
  197. data/spec/prawn/text_spacing_spec.rb +0 -95
  198. data/spec/prawn/text_spec.rb +0 -603
  199. data/spec/prawn/text_with_inline_formatting_spec.rb +0 -35
  200. data/spec/prawn/transformation_stack_spec.rb +0 -66
  201. data/spec/prawn/view_spec.rb +0 -63
  202. data/spec/prawn_manual_spec.rb +0 -35
  203. data/spec/spec_helper.rb +0 -48
@@ -1,26 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # bounding_box.rb : Implements a mechanism for shifting the coordinate space
4
- #
5
- # Copyright May 2008, Gregory Brown. All Rights Reserved.
6
- #
7
- # This is free software. Please see the LICENSE and COPYING files for details.
8
-
9
3
  module Prawn
10
- class Document
4
+ class Document # rubocop: disable Style/Documentation
11
5
  # @group Stable API
12
6
 
13
- # :call-seq:
14
- # bounding_box(point, options={}, &block)
15
- #
16
7
  # A bounding box serves two important purposes:
17
8
  # * Provide bounds for flowing text, starting at a given point
18
- # * Translate the origin (0,0) for graphics primitives
19
- #
20
- # A point and :width must be provided. :height is optional.
21
- # (See stretchyness below)
9
+ # * Translate the origin (0, 0) for graphics primitives
22
10
  #
23
- # ==Positioning
11
+ # #### Positioning
24
12
  #
25
13
  # Bounding boxes are positioned relative to their top left corner and
26
14
  # the width measurement is towards the right and height measurement is
@@ -28,22 +16,26 @@ module Prawn
28
16
  #
29
17
  # Usage:
30
18
  #
31
- # * Bounding box 100pt x 100pt in the absolute bottom left of the
19
+ # * Bounding box 100pt×100pt in the absolute bottom left of the
32
20
  # containing box:
33
21
  #
34
- # pdf.bounding_box([0,100], :width => 100, :height => 100)
22
+ # ```ruby
23
+ # pdf.bounding_box([0, 100], width: 100, height: 100)
35
24
  # stroke_bounds
36
25
  # end
26
+ # ```
37
27
  #
38
- # * Bounding box 200pt x 400pt high in the center of the page:
28
+ # * Bounding box 200pt×400pt high in the center of the page:
39
29
  #
30
+ # ```ruby
40
31
  # x_pos = ((bounds.width / 2) - 150)
41
32
  # y_pos = ((bounds.height / 2) + 200)
42
- # pdf.bounding_box([x_pos, y_pos], :width => 300, :height => 400) do
33
+ # pdf.bounding_box([x_pos, y_pos], width: 300, height: 400) do
43
34
  # stroke_bounds
44
35
  # end
36
+ # ```
45
37
  #
46
- # ==Flowing Text
38
+ # #### Flowing Text
47
39
  #
48
40
  # When flowing text, the usage of a bounding box is simple. Text will
49
41
  # begin at the point specified, flowing the width of the bounding box.
@@ -54,17 +46,19 @@ module Prawn
54
46
  #
55
47
  # Usage:
56
48
  #
57
- # pdf.bounding_box([100,500], :width => 100, :height => 300) do
58
- # pdf.text "This text will flow in a very narrow box starting" +
59
- # "from [100,500]. The pointer will then be moved to [100,200]" +
60
- # "and return to the margin_box"
61
- # end
49
+ # ```ruby
50
+ # pdf.bounding_box([100, 500], width: 100, height: 300) do
51
+ # pdf.text "This text will flow in a very narrow box starting" +
52
+ # "from [100, 500]. The pointer will then be moved to [100, 200]" +
53
+ # "and return to the margin_box"
54
+ # end
55
+ # ```
62
56
  #
63
57
  # Note, this is a low level tool and is designed primarily for building
64
- # other abstractions. If you just need to flow text on the page, you
65
- # will want to look at span() and text_box() instead
58
+ # other abstractions. If you just need to flow text on the page, you
59
+ # will want to look at {span} and {text_box} instead.
66
60
  #
67
- # ==Translating Coordinates
61
+ # #### Translating Coordinates
68
62
  #
69
63
  # When translating coordinates, the idea is to allow the user to draw
70
64
  # relative to the origin, and then translate their drawing to a specified
@@ -74,14 +68,18 @@ module Prawn
74
68
  # Take for example two triangles which share one point, drawn from the
75
69
  # origin:
76
70
  #
77
- # pdf.polygon [0,250], [0,0], [150,100]
78
- # pdf.polygon [100,0], [150,100], [200,0]
71
+ # ```ruby
72
+ # pdf.polygon [0, 250], [0, 0], [150, 100]
73
+ # pdf.polygon [100, 0], [150, 100], [200, 0]
74
+ # ```
79
75
  #
80
76
  # It would be easy enough to translate these triangles to another point,
81
- # e.g [200,200]
77
+ # e.g `[200, 200]`
82
78
  #
83
- # pdf.polygon [200,450], [200,200], [350,300]
84
- # pdf.polygon [300,200], [350,300], [400,200]
79
+ # ```ruby
80
+ # pdf.polygon [200, 450], [200, 200], [350, 300]
81
+ # pdf.polygon [300, 200], [350, 300], [400, 200]
82
+ # ```
85
83
  #
86
84
  # However, each time you want to move the drawing, you'd need to alter
87
85
  # every point in the drawing calls, which as you might imagine, can become
@@ -90,46 +88,51 @@ module Prawn
90
88
  # If instead, we think of the drawing as being bounded by a box, we can
91
89
  # see that the image is 200 points wide by 250 points tall.
92
90
  #
93
- # To translate it to a new origin, we simply select a point at (x,y+height)
91
+ # To translate it to a new origin, we simply select a point at
92
+ # (x, y + height).
94
93
  #
95
- # Using the [200,200] example:
94
+ # Using the [200, 200] example:
96
95
  #
97
- # pdf.bounding_box([200,450], :width => 200, :height => 250) do
98
- # pdf.stroke do
99
- # pdf.polygon [0,250], [0,0], [150,100]
100
- # pdf.polygon [100,0], [150,100], [200,0]
101
- # end
96
+ # ```ruby
97
+ # pdf.bounding_box([200, 450], width: 200, height: 250) do
98
+ # pdf.stroke do
99
+ # pdf.polygon [0, 250], [0, 0], [150, 100]
100
+ # pdf.polygon [100, 0], [150, 100], [200, 0]
102
101
  # end
102
+ # end
103
+ # ```
103
104
  #
104
105
  # Notice that the drawing is still relative to the origin. If we want to
105
106
  # move this drawing around the document, we simply need to recalculate the
106
107
  # top-left corner of the rectangular bounding-box, and all of our graphics
107
108
  # calls remain unmodified.
108
109
  #
109
- # ==Nesting Bounding Boxes
110
+ # #### Nesting Bounding Boxes
110
111
  #
111
112
  # At the top level, bounding boxes are specified relative to the document's
112
- # margin_box (which is itself a bounding box). You can also nest bounding
113
+ # margin_box (which is itself a bounding box). You can also nest bounding
113
114
  # boxes, allowing you to build components which are relative to each other
114
115
  #
115
116
  # Usage:
116
117
  #
117
- # pdf.bounding_box([200,450], :width => 200, :height => 250) do
118
- # pdf.stroke_bounds # Show the containing bounding box
119
- # pdf.bounding_box([50,200], :width => 50, :height => 50) do
120
- # # a 50x50 bounding box that starts 50 pixels left and 50 pixels down
121
- # # the parent bounding box.
122
- # pdf.stroke_bounds
123
- # end
124
- # end
118
+ # ```ruby
119
+ # pdf.bounding_box([200, 450], width: 200, height: 250) do
120
+ # pdf.stroke_bounds # Show the containing bounding box
121
+ # pdf.bounding_box([50, 200], width: 50, height: 50) do
122
+ # # a 50x50 bounding box that starts 50 pixels left and 50 pixels down
123
+ # # the parent bounding box.
124
+ # pdf.stroke_bounds
125
+ # end
126
+ # end
127
+ # ```
125
128
  #
126
- # ==Stretchyness
129
+ # #### Stretchiness
127
130
  #
128
131
  # If you do not specify a height to a bounding box, it will become stretchy
129
132
  # and its height will be calculated automatically as you stretch the box
130
133
  # downwards.
131
134
  #
132
- # pdf.bounding_box([100,400], :width => 400) do
135
+ # pdf.bounding_box([100, 400], width: 400) do
133
136
  # pdf.text("The height of this box is #{pdf.bounds.height}")
134
137
  # pdf.text('this is some text')
135
138
  # pdf.text('this is some more text')
@@ -137,24 +140,37 @@ module Prawn
137
140
  # pdf.text("Now the height of this box is #{pdf.bounds.height}")
138
141
  # end
139
142
  #
140
- # ==Absolute Positioning
143
+ # #### Absolute Positioning
141
144
  #
142
145
  # If you wish to position the bounding boxes at absolute coordinates rather
143
146
  # than relative to the margins or other bounding boxes, you can use canvas()
144
147
  #
145
- # pdf.bounding_box([50,500], :width => 200, :height => 300) do
146
- # pdf.stroke_bounds
147
- # pdf.canvas do
148
- # Positioned outside the containing box at the 'real' (300,450)
149
- # pdf.bounding_box([300,450], :width => 200, :height => 200) do
150
- # pdf.stroke_bounds
151
- # end
152
- # end
153
- # end
148
+ # ```ruby
149
+ # pdf.bounding_box([50, 500], width: 200, height: 300) do
150
+ # pdf.stroke_bounds
151
+ # pdf.canvas do
152
+ # Positioned outside the containing box at the 'real' (300, 450)
153
+ # pdf.bounding_box([300, 450], width: 200, height: 200) do
154
+ # pdf.stroke_bounds
155
+ # end
156
+ # end
157
+ # end
158
+ # ```
154
159
  #
155
160
  # Of course, if you use canvas, you will be responsible for ensuring that
156
161
  # you remain within the printable area of your document.
157
162
  #
163
+ # @overload bounding_box(point, options = {}, &block)
164
+ # @param point [Array(Number, Number)]
165
+ # top left corner of the new bounding box
166
+ # @param options [Hash{Symbol => any}]
167
+ # @option options :width [Number]
168
+ # width of the new bounding box, must be specified.
169
+ # @option options :height [Number]
170
+ # height of the new bounding box, stretchy box if omitted.
171
+ # @yieldparam parent_box [BoundingBox] parent bounding box
172
+ # @yieldreturn [void]
173
+ # @return [void]
158
174
  def bounding_box(point, *args, &block)
159
175
  init_bounding_box(block) do |parent_box|
160
176
  point = map_to_absolute(point)
@@ -165,17 +181,22 @@ module Prawn
165
181
  # A shortcut to produce a bounding box which is mapped to the document's
166
182
  # absolute coordinates, regardless of how things are nested or margin sizes.
167
183
  #
184
+ # @example
168
185
  # pdf.canvas do
169
186
  # pdf.line pdf.bounds.bottom_left, pdf.bounds.top_right
170
187
  # end
171
188
  #
189
+ # @yieldreturn [void]
190
+ # @return [void]
172
191
  def canvas(&block)
173
192
  init_bounding_box(block, hold_position: true) do |_|
174
193
  # Canvas bbox acts like margin_box in that its parent bounds are unset.
175
194
  @bounding_box = BoundingBox.new(
176
- self, nil, [0, page.dimensions[3]],
195
+ self,
196
+ nil,
197
+ [0, page.dimensions[3]],
177
198
  width: page.dimensions[2],
178
- height: page.dimensions[3]
199
+ height: page.dimensions[3],
179
200
  )
180
201
  end
181
202
  end
@@ -217,11 +238,23 @@ module Prawn
217
238
 
218
239
  # Low level layout helper that simplifies coordinate math.
219
240
  #
220
- # See Prawn::Document#bounding_box for a description of what this class
241
+ # See {Prawn::Document#bounding_box} for a description of what this class
221
242
  # is used for.
222
- #
223
243
  class BoundingBox
244
+ # Indicates absence of a reference bounding box of a fixed height.
245
+ class NoReferenceBounds < StandardError
246
+ def initialize(message = "Can't find reference bounds: my parent is unset")
247
+ super
248
+ end
249
+ end
250
+
224
251
  # @private
252
+ # @param document [Prawn::Document] ownding document
253
+ # @param parent [BoundingBox?] parent bounding box
254
+ # @param point [Array(Number, Number)] coordinates of the top left corner
255
+ # @param options [Hash{Symbol => any}]
256
+ # @option options :width [Number] width
257
+ # @option options :height [Number] optional height
225
258
  def initialize(document, parent, point, options = {})
226
259
  unless options[:width]
227
260
  raise ArgumentError, 'BoundingBox needs the :width option to be set'
@@ -237,52 +270,67 @@ module Prawn
237
270
  @stretched_height = nil
238
271
  end
239
272
 
273
+ # Owning document.
274
+ #
240
275
  # @private
276
+ # @return [Prawn::Document]
277
+ attr_reader :document
241
278
 
242
- attr_reader :document, :parent
243
-
279
+ # Parent bounding box.
280
+ #
244
281
  # @private
282
+ # @return [BoundingBox?]
283
+ attr_reader :parent
284
+
245
285
  # The current indentation of the left side of the bounding box.
286
+ #
287
+ # @private
288
+ # @return [Number]
246
289
  attr_reader :total_left_padding
247
290
 
248
- # @private
249
291
  # The current indentation of the right side of the bounding box.
292
+ #
293
+ # @private
294
+ # @return [Number]
250
295
  attr_reader :total_right_padding
251
296
 
252
- # The translated origin (x,y-height) which describes the location
253
- # of the bottom left corner of the bounding box
297
+ # The translated origin (x, y - height) which describes the location of
298
+ # the bottom left corner of the bounding box.
254
299
  #
255
300
  # @private
301
+ # @return [Array(Number, Number)]
256
302
  def anchor
257
303
  [@x, @y - height]
258
304
  end
259
305
 
260
- # Relative left x-coordinate of the bounding box. (Always 0)
261
- #
262
- # Example, position some text 3 pts from the left of the containing box:
306
+ # Relative left x-coordinate of the bounding box. Always 0.
263
307
  #
264
- # draw_text('hello', :at => [(bounds.left + 3), 0])
308
+ # @example Position some text 3 pts from the left of the containing box
309
+ # draw_text('hello', at: [(bounds.left + 3), 0])
265
310
  #
311
+ # @return [Number]
266
312
  def left
267
313
  0
268
314
  end
269
315
 
270
- # Temporarily adjust the @x coordinate to allow for left_padding
271
- #
272
- # Example:
316
+ # Temporarily adjust the x coordinate to allow for left padding
273
317
  #
274
- # indent 20 do
318
+ # @example
319
+ # indent 20 do
275
320
  # text "20 points in"
276
321
  # indent 30 do
277
322
  # text "50 points in"
278
323
  # end
279
324
  # end
280
325
  #
281
- # indent 20, 20 do
282
- # text "indented on both sides"
283
- # end
326
+ # indent 20, 20 do
327
+ # text "indented on both sides"
328
+ # end
284
329
  #
285
330
  # @private
331
+ # @param left_padding [Number]
332
+ # @param right_padding [Number]
333
+ # @return [void]
286
334
  def indent(left_padding, right_padding = 0)
287
335
  add_left_padding(left_padding)
288
336
  add_right_padding(right_padding)
@@ -293,7 +341,10 @@ module Prawn
293
341
  end
294
342
 
295
343
  # Increase the left padding of the bounding box.
344
+ #
296
345
  # @private
346
+ # @param left_padding [Number]
347
+ # @return [void]
297
348
  def add_left_padding(left_padding)
298
349
  @total_left_padding += left_padding
299
350
  @x += left_padding
@@ -301,7 +352,10 @@ module Prawn
301
352
  end
302
353
 
303
354
  # Decrease the left padding of the bounding box.
355
+ #
304
356
  # @private
357
+ # @param left_padding [Number]
358
+ # @return [void]
305
359
  def subtract_left_padding(left_padding)
306
360
  @total_left_padding -= left_padding
307
361
  @x -= left_padding
@@ -309,172 +363,190 @@ module Prawn
309
363
  end
310
364
 
311
365
  # Increase the right padding of the bounding box.
366
+ #
312
367
  # @private
368
+ # @param right_padding [Number]
369
+ # @return [void]
313
370
  def add_right_padding(right_padding)
314
371
  @total_right_padding += right_padding
315
372
  @width -= right_padding
316
373
  end
317
374
 
318
375
  # Decrease the right padding of the bounding box.
376
+ #
319
377
  # @private
378
+ # @param right_padding [Number]
379
+ # @return [void]
320
380
  def subtract_right_padding(right_padding)
321
381
  @total_right_padding -= right_padding
322
382
  @width += right_padding
323
383
  end
324
384
 
325
- # Relative right x-coordinate of the bounding box. (Equal to the box
326
- # width)
327
- #
328
- # Example, position some text 3 pts from the right of the containing box:
385
+ # Relative right x-coordinate of the bounding box. Equal to the box width.
329
386
  #
330
- # draw_text('hello', :at => [(bounds.right - 3), 0])
387
+ # @example Position some text 3 pts from the right of the containing box
388
+ # draw_text('hello', at: [(bounds.right - 3), 0])
331
389
  #
390
+ # @return [Number]
332
391
  def right
333
392
  @width
334
393
  end
335
394
 
336
- # Relative top y-coordinate of the bounding box. (Equal to the box height)
395
+ # Relative top y-coordinate of the bounding box. Equal to the box height.
337
396
  #
338
- # Example, position some text 3 pts from the top of the containing box:
339
- #
340
- # draw_text('hello', :at => [0, (bounds.top - 3)])
397
+ # @example Position some text 3 pts from the top of the containing box
398
+ # draw_text('hello', at: [0, (bounds.top - 3)])
341
399
  #
400
+ # @return [Number]
342
401
  def top
343
402
  height
344
403
  end
345
404
 
346
- # Relative bottom y-coordinate of the bounding box (Always 0)
347
- #
348
- # Example, position some text 3 pts from the bottom of the containing box:
405
+ # Relative bottom y-coordinate of the bounding box. Always 0.
349
406
  #
350
- # draw_text('hello', :at => [0, (bounds.bottom + 3)])
407
+ # @example Position some text 3 pts from the bottom of the containing box
408
+ # draw_text('hello', at: [0, (bounds.bottom + 3)])
351
409
  #
410
+ # @return [Number]
352
411
  def bottom
353
412
  0
354
413
  end
355
414
 
356
- # Relative top-left point of the bounding_box
415
+ # Relative top-left point of the bounding_box.
357
416
  #
358
- # Example, draw a line from the top left of the box diagonally to the
359
- # bottom right:
360
- #
361
- # stroke do
362
- # line(bounds.top_left, bounds.bottom_right)
363
- # end
417
+ # @example Draw a line from the top left of the box diagonally to the bottom right
418
+ # stroke do
419
+ # line(bounds.top_left, bounds.bottom_right)
420
+ # end
364
421
  #
422
+ # @return [Array(Number, Number)]
365
423
  def top_left
366
424
  [left, top]
367
425
  end
368
426
 
369
- # Relative top-right point of the bounding box
370
- #
371
- # Example, draw a line from the top_right of the box diagonally to the
372
- # bottom left:
427
+ # Relative top-right point of the bounding box.
373
428
  #
374
- # stroke do
375
- # line(bounds.top_right, bounds.bottom_left)
376
- # end
429
+ # @example Draw a line from the top_right of the box diagonally to the bottom left
430
+ # stroke do
431
+ # line(bounds.top_right, bounds.bottom_left)
432
+ # end
377
433
  #
434
+ # @return [Array(Number, Number)]
378
435
  def top_right
379
436
  [right, top]
380
437
  end
381
438
 
382
- # Relative bottom-right point of the bounding box
383
- #
384
- # Example, draw a line along the right hand side of the page:
439
+ # Relative bottom-right point of the bounding box.
385
440
  #
386
- # stroke do
387
- # line(bounds.bottom_right, bounds.top_right)
388
- # end
441
+ # @example Draw a line along the right hand side of the page
442
+ # stroke do
443
+ # line(bounds.bottom_right, bounds.top_right)
444
+ # end
389
445
  #
446
+ # @return [Array(Number, Number)]
390
447
  def bottom_right
391
448
  [right, bottom]
392
449
  end
393
450
 
394
- # Relative bottom-left point of the bounding box
451
+ # Relative bottom-left point of the bounding box.
395
452
  #
396
- # Example, draw a line along the left hand side of the page:
397
- #
398
- # stroke do
399
- # line(bounds.bottom_left, bounds.top_left)
400
- # end
453
+ # @example Draw a line along the left hand side of the page
454
+ # stroke do
455
+ # line(bounds.bottom_left, bounds.top_left)
456
+ # end
401
457
  #
458
+ # @return [Array(Number, Number)]
402
459
  def bottom_left
403
460
  [left, bottom]
404
461
  end
405
462
 
406
- # Absolute left x-coordinate of the bounding box
463
+ # Absolute left x-coordinate of the bounding box.
407
464
  #
465
+ # @return [Number]
408
466
  def absolute_left
409
467
  @x
410
468
  end
411
469
 
412
- # Absolute right x-coordinate of the bounding box
470
+ # Absolute right x-coordinate of the bounding box.
413
471
  #
472
+ # @return [Number]
414
473
  def absolute_right
415
474
  @x + width
416
475
  end
417
476
 
418
- # Absolute top y-coordinate of the bounding box
477
+ # Absolute top y-coordinate of the bounding box.
419
478
  #
479
+ # @return [Number]
420
480
  def absolute_top
421
481
  @y
422
482
  end
423
483
 
424
- # Absolute bottom y-coordinate of the bottom box
484
+ # Absolute bottom y-coordinate of the bottom box.
425
485
  #
486
+ # @return [Number]
426
487
  def absolute_bottom
427
488
  @y - height
428
489
  end
429
490
 
430
- # Absolute top-left point of the bounding box
491
+ # Absolute top-left point of the bounding box.
431
492
  #
493
+ # @return [Array(Number, Number)]
432
494
  def absolute_top_left
433
495
  [absolute_left, absolute_top]
434
496
  end
435
497
 
436
- # Absolute top-right point of the bounding box
498
+ # Absolute top-right point of the bounding box.
437
499
  #
500
+ # @return [Array(Number, Number)]
438
501
  def absolute_top_right
439
502
  [absolute_right, absolute_top]
440
503
  end
441
504
 
442
- # Absolute bottom-left point of the bounding box
505
+ # Absolute bottom-left point of the bounding box.
443
506
  #
507
+ # @return [Array(Number, Number)]
444
508
  def absolute_bottom_left
445
509
  [absolute_left, absolute_bottom]
446
510
  end
447
511
 
448
- # Absolute bottom-left point of the bounding box
512
+ # Absolute bottom-left point of the bounding box.
449
513
  #
514
+ # @return [Array(Number, Number)]
450
515
  def absolute_bottom_right
451
516
  [absolute_right, absolute_bottom]
452
517
  end
453
518
 
454
- # Width of the bounding box
519
+ # Width of the bounding box.
520
+ #
521
+ # @return [Number]
455
522
  attr_reader :width
456
523
 
457
- # Height of the bounding box. If the box is 'stretchy' (unspecified
524
+ # Height of the bounding box. If the box is 'stretchy' (unspecified
458
525
  # height attribute), height is calculated as the distance from the top of
459
526
  # the box to the current drawing position.
460
527
  #
528
+ # @return [Number]
461
529
  def height
462
530
  return @height if @height
463
531
 
464
532
  @stretched_height = [
465
533
  (absolute_top - @document.y),
466
- @stretched_height.to_f
534
+ Float(@stretched_height || 0.0),
467
535
  ].max
468
536
  end
469
537
 
470
- # an alias for absolute_left
538
+ # An alias for {absolute_left}.
539
+ #
471
540
  # @private
541
+ # @return [Number]
472
542
  def left_side
473
543
  absolute_left
474
544
  end
475
545
 
476
- # an alias for absolute_right
546
+ # An alias for {absolute_right}.
547
+ #
477
548
  # @private
549
+ # @return [Number]
478
550
  def right_side
479
551
  absolute_right
480
552
  end
@@ -484,6 +556,7 @@ module Prawn
484
556
  # Moves to the top of the next page of the document, starting a new page
485
557
  # if necessary.
486
558
  #
559
+ # @return [void]
487
560
  def move_past_bottom
488
561
  if @document.page_number == @document.page_count
489
562
  @document.start_new_page
@@ -492,19 +565,22 @@ module Prawn
492
565
  end
493
566
  end
494
567
 
495
- # Returns +false+ when the box has a defined height, +true+ when the
568
+ # Returns `false` when the box has a defined height, `true` when the
496
569
  # height is being calculated on the fly based on the current vertical
497
570
  # position.
498
571
  #
572
+ # @return [Boolean]
499
573
  def stretchy?
500
574
  !@height
501
575
  end
502
576
 
503
577
  # Returns the innermost non-stretchy bounding box.
504
578
  #
579
+ # @return [BoundingBox]
580
+ # @raise [NoReferenceBounds]
505
581
  def reference_bounds
506
582
  if stretchy?
507
- raise "Can't find reference bounds: my parent is unset" unless @parent
583
+ raise NoReferenceBounds unless @parent
508
584
 
509
585
  @parent.reference_bounds
510
586
  else
@@ -518,26 +594,30 @@ module Prawn
518
594
  # not copying the reference to the Document).
519
595
  #
520
596
  # @private
597
+ # @return [BoundingBox]
521
598
  def deep_copy
522
599
  copy = dup
523
600
  # Deep-copy the parent bounds
524
601
  copy.instance_variable_set(
525
- '@parent',
602
+ :@parent,
526
603
  if @parent.is_a?(BoundingBox)
527
604
  @parent.deep_copy
528
- end
605
+ end,
529
606
  )
530
- copy.instance_variable_set('@document', nil)
607
+ copy.instance_variable_set(:@document, nil)
531
608
  copy
532
609
  end
533
610
 
534
- # Restores a copy of the bounds taken by BoundingBox.deep_copy in the
535
- # context of the given +document+. Does *not* set the bounds of the
536
- # document to the resulting BoundingBox, only returns it.
611
+ # Restores a copy of the bounds taken by {BoundingBox#deep_copy} in the
612
+ # context of the given `document`. Does *not* set the bounds of the
613
+ # document to the resulting {BoundingBox}, only returns it.
537
614
  #
538
615
  # @private
616
+ # @param bounds [BoundingBox]
617
+ # @param document [Prawn::Document]
618
+ # @return [BoundingBox]
539
619
  def self.restore_deep_copy(bounds, document)
540
- bounds.instance_variable_set('@document', document)
620
+ bounds.instance_variable_set(:@document, document)
541
621
  bounds
542
622
  end
543
623
  end