prawn 2.4.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 +213 -141
  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 +20 -18
  7. data/lib/prawn/document.rb +257 -171
  8. data/lib/prawn/encoding.rb +2 -5
  9. data/lib/prawn/errors.rb +23 -34
  10. data/lib/prawn/font.rb +248 -135
  11. data/lib/prawn/font_metric_cache.rb +11 -10
  12. data/lib/prawn/fonts/afm.rb +85 -45
  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 +305 -93
  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 +57 -34
  22. data/lib/prawn/graphics/dash.rb +45 -42
  23. data/lib/prawn/graphics/join_style.rb +17 -11
  24. data/lib/prawn/graphics/patterns.rb +190 -69
  25. data/lib/prawn/graphics/transformation.rb +48 -41
  26. data/lib/prawn/graphics/transparency.rb +16 -40
  27. data/lib/prawn/graphics.rb +363 -253
  28. data/lib/prawn/grid.rb +184 -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 +42 -19
  32. data/lib/prawn/images/png.rb +92 -41
  33. data/lib/prawn/images.rb +44 -57
  34. data/lib/prawn/measurement_extensions.rb +39 -8
  35. data/lib/prawn/measurements.rb +60 -5
  36. data/lib/prawn/outline.rb +114 -108
  37. data/lib/prawn/repeater.rb +51 -35
  38. data/lib/prawn/security/arcfour.rb +4 -4
  39. data/lib/prawn/security.rb +75 -70
  40. data/lib/prawn/soft_mask.rb +42 -30
  41. data/lib/prawn/stamp.rb +38 -42
  42. data/lib/prawn/text/box.rb +146 -96
  43. data/lib/prawn/text/formatted/arranger.rb +87 -26
  44. data/lib/prawn/text/formatted/box.rb +221 -150
  45. data/lib/prawn/text/formatted/fragment.rb +130 -14
  46. data/lib/prawn/text/formatted/line_wrap.rb +33 -24
  47. data/lib/prawn/text/formatted/parser.rb +112 -72
  48. data/lib/prawn/text/formatted/wrap.rb +12 -17
  49. data/lib/prawn/text/formatted.rb +75 -0
  50. data/lib/prawn/text.rb +441 -196
  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 +68 -53
  55. data/lib/prawn.rb +23 -18
  56. data.tar.gz.sig +0 -0
  57. metadata +54 -177
  58. metadata.gz.sig +0 -0
  59. data/.yardopts +0 -10
  60. data/Gemfile +0 -5
  61. data/Rakefile +0 -25
  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 -29
  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 -23
  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 -38
  94. data/manual/graphics/gradients.rb +0 -43
  95. data/manual/graphics/graphics.rb +0 -64
  96. data/manual/graphics/helper.rb +0 -34
  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 -29
  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 -30
  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 -43
  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 -25
  142. data/manual/text/formatted_callbacks.rb +0 -70
  143. data/manual/text/formatted_text.rb +0 -61
  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 -31
  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 -52
  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 -51
  160. data/manual/text/utf8.rb +0 -27
  161. data/manual/text/win_ansi_charset.rb +0 -62
  162. data/prawn.gemspec +0 -51
  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 -550
  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 -44
  173. data/spec/prawn/document_spec.rb +0 -805
  174. data/spec/prawn/font_metric_cache_spec.rb +0 -54
  175. data/spec/prawn/font_spec.rb +0 -544
  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 -872
  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 -229
  184. data/spec/prawn/measurements_extensions_spec.rb +0 -24
  185. data/spec/prawn/outline_spec.rb +0 -512
  186. data/spec/prawn/repeater_spec.rb +0 -166
  187. data/spec/prawn/soft_mask_spec.rb +0 -74
  188. data/spec/prawn/stamp_spec.rb +0 -173
  189. data/spec/prawn/text/box_spec.rb +0 -1110
  190. data/spec/prawn/text/formatted/arranger_spec.rb +0 -466
  191. data/spec/prawn/text/formatted/box_spec.rb +0 -849
  192. data/spec/prawn/text/formatted/fragment_spec.rb +0 -343
  193. data/spec/prawn/text/formatted/line_wrap_spec.rb +0 -495
  194. data/spec/prawn/text/formatted/parser_spec.rb +0 -697
  195. data/spec/prawn/text_draw_text_spec.rb +0 -150
  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,108 +1,85 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # text/rectangle.rb : Implements text boxes
4
- #
5
- # Copyright November 2009, Daniel Nelson. All Rights Reserved.
6
- #
7
- # This is free software. Please see the LICENSE and COPYING files for details.
8
- #
9
-
10
3
  require_relative 'formatted/box'
11
4
 
12
5
  module Prawn
13
- module Text
6
+ module Text # rubocop: disable Style/Documentation
14
7
  # @group Stable API
15
8
 
16
- # Draws the requested text into a box. When the text overflows
17
- # the rectangle, you shrink to fit, or truncate the text. Text
18
- # boxes are independent of the document y position.
9
+ # Draws the requested text into a box.
10
+ #
11
+ # When the text overflows the rectangle, you shrink to fit, or truncate the
12
+ # text. Text boxes are independent of the document y position.
19
13
  #
20
- # == Encoding
14
+ # #### Encoding
21
15
  #
22
- # Note that strings passed to this function should be encoded as UTF-8.
23
- # If you get unexpected characters appearing in your rendered document,
24
- # check this.
16
+ # Note that strings passed to this function should be encoded as UTF-8. If
17
+ # you get unexpected characters appearing in your rendered document, check
18
+ # this.
25
19
  #
26
- # If the current font is a built-in one, although the string must be
27
- # encoded as UTF-8, only characters that are available in WinAnsi
28
- # are allowed.
20
+ # If the current font is a built-in one, although the string must be encoded
21
+ # as UTF-8, only characters that are available in WinAnsi are allowed.
29
22
  #
30
23
  # If an empty box is rendered to your PDF instead of the character you
31
24
  # wanted it usually means the current font doesn't include that character.
32
25
  #
33
- # == Options (default values marked in [])
34
- #
35
- # <tt>:kerning</tt>:: <tt>boolean</tt>. Whether or not to use kerning (if it
36
- # is available with the current font)
37
- # [value of document.default_kerning?]
38
- # <tt>:size</tt>:: <tt>number</tt>. The font size to use. [current font
39
- # size]
40
- # <tt>:character_spacing</tt>:: <tt>number</tt>. The amount of space to add
41
- # to or remove from the default character
42
- # spacing. [0]
43
- # <tt>:disable_wrap_by_char</tt>:: <tt>boolean</tt> Whether
44
- # or not to prevent mid-word breaks when text does not fit in box. [false]
45
- # <tt>:mode</tt>:: <tt>symbol</tt>. The text rendering mode. See
46
- # documentation for Prawn::Document#text_rendering_mode
47
- # for a list of valid options. [:fill]
48
- # <tt>:style</tt>:: The style to use. The requested style must be part of
49
- # the current font familly. [current style]
50
- #
51
- # <tt>:at</tt>::
52
- # <tt>[x, y]</tt>. The upper left corner of the box
53
- # [@document.bounds.left, @document.bounds.top]
54
- # <tt>:width</tt>::
55
- # <tt>number</tt>. The width of the box
56
- # [@document.bounds.right - @at[0]]
57
- # <tt>:height</tt>::
58
- # <tt>number</tt>. The height of the box [default_height()]
59
- # <tt>:direction</tt>::
60
- # <tt>:ltr</tt>, <tt>:rtl</tt>, Direction of the text (left-to-right
61
- # or right-to-left) [value of document.text_direction]
62
- # <tt>:fallback_fonts</tt>::
63
- # An array of font names. Each name must be the name of an AFM font or
64
- # the name that was used to register a family of external fonts (see
65
- # Prawn::Document#font_families). If present, then each glyph will be
66
- # rendered using the first font that includes the glyph, starting with
67
- # the current font and then moving through :fallback_fonts from
68
- # left to right.
69
- # <tt>:align</tt>::
70
- # <tt>:left</tt>, <tt>:center</tt>, <tt>:right</tt>, or
71
- # <tt>:justify</tt> Alignment within the bounding box
72
- # [:left if direction is :ltr, :right if direction is :rtl]
73
- # <tt>:valign</tt>::
74
- # <tt>:top</tt>, <tt>:center</tt>, or <tt>:bottom</tt>. Vertical
75
- # alignment within the bounding box [:top]
76
- # <tt>:rotate</tt>::
77
- # <tt>number</tt>. The angle to rotate the text
78
- # <tt>:rotate_around</tt>::
79
- # <tt>:center</tt>, <tt>:upper_left</tt>, <tt>:upper_right</tt>,
80
- # <tt>:lower_right</tt>, or <tt>:lower_left</tt>. The point around which
81
- # to rotate the text [:upper_left]
82
- # <tt>:leading</tt>::
83
- # <tt>number</tt>. Additional space between lines [value of
84
- # document.default_leading]
85
- # <tt>:single_line</tt>::
86
- # <tt>boolean</tt>. If true, then only the first line will be drawn
87
- # [false]
88
- # <tt>:overflow</tt>::
89
- # <tt>:truncate</tt>, <tt>:shrink_to_fit</tt>, or <tt>:expand</tt>
90
- # This controls the behavior when the amount of text
91
- # exceeds the available space. [:truncate]
92
- # <tt>:min_font_size</tt>::
93
- # <tt>number</tt>. The minimum font size to use when :overflow is set to
94
- # :shrink_to_fit (that is the font size will not be reduced to less than
95
- # this value, even if it means that some text will be cut off). [5]
96
- #
97
- # == Returns
98
- #
99
- # Returns any text that did not print under the current settings.
100
- #
101
- # == Exceptions
102
- #
103
- # Raises <tt>Prawn::Errors::CannotFit</tt> if not wide enough to print
104
- # any text
105
- #
26
+ # @param string [String]
27
+ # @param options [Hash{Symbol => any}]
28
+ # @option options :kerning [Boolean] (value of document.default_kerning?)
29
+ # Whether or not to use kerning (if it is available with the current
30
+ # font).
31
+ # @option options :size [Number] (current font size)
32
+ # The font size to use.
33
+ # @option options :character_spacing [Number] (0)
34
+ # The amount of space to add to or remove from the default character
35
+ # spacing.
36
+ # @option options :disable_wrap_by_char [Boolean] (false)
37
+ # Whether or not to prevent mid-word breaks when text does not fit in box.
38
+ # @option options :mode [Symbol] (:fill)
39
+ # The text rendering mode. See documentation for
40
+ # {Prawn::Document#text_rendering_mode} for a list of valid options.
41
+ # @option option :style [Symbol] (current style)
42
+ # The style to use. The requested style must be part of the current font
43
+ # family.
44
+ # @option option :at [Array(Number, Number)] (bounds top left corner)
45
+ # The upper left corner of the box.
46
+ # @option options :width [Number] (bounds.right - at[0])
47
+ # The width of the box.
48
+ # @option options :height [Number] (default_height())
49
+ # The height of the box.
50
+ # @option options :direction [:ltr, :rtl] (value of document.text_direction)
51
+ # Direction of the text (left-to-right or right-to-left).
52
+ # @option options :fallback_fonts [Array<String>]
53
+ # An array of font names. Each name must be the name of an AFM font or the
54
+ # name that was used to register a family of external fonts (see
55
+ # {Prawn::Document#font_families}). If present, then each glyph will be
56
+ # rendered using the first font that includes the glyph, starting with the
57
+ # current font and then moving through `:fallback_fonts`.
58
+ # @option options :align [:left, :center, :right, :justify]
59
+ # (:left if direction is :ltr, :right if direction is :rtl)
60
+ # Alignment within the bounding box.
61
+ # @option options :valign [:top, :center, :bottom] (:top)
62
+ # Vertical alignment within the bounding box.
63
+ # @option options :rotate [Number]
64
+ # The angle to rotate the text.
65
+ # @option options :rotate_around
66
+ # [:center, :upper_left, :upper_right, :lower_right, :lower_left]
67
+ # (:upper_left)
68
+ # The point around which to rotate the text.
69
+ # @option options :leading [Number] (value of document.default_leading)
70
+ # Additional space between lines.
71
+ # @option options :single_line [Boolean] (false)
72
+ # If true, then only the first line will be drawn.
73
+ # @option options :overflow [:truncate, :shrink_to_fit, :expand] (:truncate)
74
+ # This controls the behavior when the amount of text exceeds the available
75
+ # space.
76
+ # @option options :min_font_size [Number] (5)
77
+ # The minimum font size to use when `:overflow` is set to `:shrink_to_fit`
78
+ # (that is the font size will not be reduced to less than this value, even
79
+ # if it means that some text will be cut off).
80
+ # @return [String] Any text that did not print under the current settings.
81
+ # @raise [Prawn::Errors::CannotFit]
82
+ # If not wide enough to print any text.
106
83
  def text_box(string, options = {})
107
84
  options = options.dup
108
85
  options[:document] = self
@@ -122,17 +99,90 @@ module Prawn
122
99
 
123
100
  # @group Experimental API
124
101
 
125
- # Generally, one would use the Prawn::Text#text_box convenience
126
- # method. However, using Text::Box.new in conjunction with
127
- # #render(:dry_run=> true) enables one to do look-ahead calculations prior
128
- # to placing text on the page, or to determine how much vertical space was
129
- # consumed by the printed text
102
+ # Text box.
130
103
  #
104
+ # Generally, one would use the {Prawn::Text#text_box} convenience method.
105
+ # However, using {Prawn::Text::Box#initialize Box.new} in conjunction with
106
+ # `render(dry_run: true)` enables one to do calculations prior to placing
107
+ # text on the page, or to determine how much vertical space was consumed by
108
+ # the printed text.
131
109
  class Box < Prawn::Text::Formatted::Box
110
+ # @param string [String]
111
+ # @param options [Hash{Symbol => any}]
112
+ # @option options :document [Prawn::Document] Owning document.
113
+ # @option options :kerning [Boolean] (value of document.default_kerning?)
114
+ # Whether or not to use kerning (if it is available with the current
115
+ # font).
116
+ # @option options :size [Number] (current font size)
117
+ # The font size to use.
118
+ # @option options :character_spacing [Number] (0)
119
+ # The amount of space to add to or remove from the default character
120
+ # spacing.
121
+ # @option options :disable_wrap_by_char [Boolean] (false)
122
+ # Whether or not to prevent mid-word breaks when text does not fit in box.
123
+ # @option options :mode [Symbol] (:fill)
124
+ # The text rendering mode. See documentation for
125
+ # {Prawn::Document#text_rendering_mode} for a list of valid options.
126
+ # @option option :style [Symbol] (current style)
127
+ # The style to use. The requested style must be part of the current font
128
+ # family.
129
+ # @option option :at [Array(Number, Number)] (bounds top left corner)
130
+ # The upper left corner of the box.
131
+ # @option options :width [Number] (bounds.right - at[0])
132
+ # The width of the box.
133
+ # @option options :height [Number] (default_height())
134
+ # The height of the box.
135
+ # @option options :direction [:ltr, :rtl] (value of document.text_direction)
136
+ # Direction of the text (left-to-right or right-to-left).
137
+ # @option options :fallback_fonts [Array<String>]
138
+ # An array of font names. Each name must be the name of an AFM font or the
139
+ # name that was used to register a family of external fonts (see
140
+ # {Prawn::Document#font_families}). If present, then each glyph will be
141
+ # rendered using the first font that includes the glyph, starting with the
142
+ # current font and then moving through `:fallback_fonts`.
143
+ # @option options :align [:left, :center, :right, :justify]
144
+ # (:left if direction is :ltr, :right if direction is :rtl)
145
+ # Alignment within the bounding box.
146
+ # @option options :valign [:top, :center, :bottom] (:top)
147
+ # Vertical alignment within the bounding box.
148
+ # @option options :rotate [Number]
149
+ # The angle to rotate the text.
150
+ # @option options :rotate_around
151
+ # [:center, :upper_left, :upper_right, :lower_right, :lower_left]
152
+ # (:upper_left)
153
+ # The point around which to rotate the text.
154
+ # @option options :leading [Number] (value of document.default_leading)
155
+ # Additional space between lines.
156
+ # @option options :single_line [Boolean] (false)
157
+ # If true, then only the first line will be drawn.
158
+ # @option options :overflow [:truncate, :shrink_to_fit, :expand] (:truncate)
159
+ # This controls the behavior when the amount of text exceeds the available
160
+ # space.
161
+ # @option options :min_font_size [Number] (5)
162
+ # The minimum font size to use when `:overflow` is set to `:shrink_to_fit`
163
+ # (that is the font size will not be reduced to less than this value, even
164
+ # if it means that some text will be cut off).
132
165
  def initialize(string, options = {})
133
166
  super([{ text: string }], options)
134
167
  end
135
168
 
169
+ # Render text to the document based on the settings defined in
170
+ # constructor.
171
+ #
172
+ # In order to facilitate look-ahead calculations, this method accepts
173
+ # a `dry_run: true` option. If provided, then everything is executed as if
174
+ # rendering, with the exception that nothing is drawn on the page. Useful
175
+ # for look-ahead computations of height, unprinted text, etc.
176
+ #
177
+ # @param flags [Hash{Symbol => any}]
178
+ # @option flags :dry_run [Boolean] (false)
179
+ # Do not draw the text. Everything else is done.
180
+ # @return [String]
181
+ # Any text that did not print under the current settings.
182
+ # @raise [Prawn::Text::Formatted::Arranger::BadFontFamily]
183
+ # If no font family is defined for the current font.
184
+ # @raise [Prawn::Errors::CannotFit]
185
+ # If not wide enough to print any text.
136
186
  def render(flags = {})
137
187
  leftover = super(flags)
138
188
  leftover.map { |hash| hash[:text] }.join
@@ -1,31 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # core/text/formatted/arranger.rb : Implements a data structure for 2-stage
4
- # processing of lines of formatted text
5
- #
6
- # Copyright February 2010, Daniel Nelson. All Rights Reserved.
7
- #
8
- # This is free software. Please see the LICENSE and COPYING files for details.
9
-
10
3
  module Prawn
11
4
  module Text
12
- module Formatted #:nodoc:
5
+ module Formatted
6
+ # D data structure for 2-stage processing of lines of formatted text.
13
7
  # @private
14
-
15
- class Arranger #:nodoc:
8
+ class Arranger
9
+ # You're getting this because you're trying to get some information from
10
+ # the arranger before it finished processing text.
16
11
  class NotFinalized < StandardError
12
+ # @private
17
13
  DEFAULT_MESSAGE = 'Lines must be finalized'
14
+
15
+ # @private
18
16
  MESSAGE_WITH_METHOD = 'Lines must be finalized before calling #%<method>s'
19
17
 
20
18
  def initialize(message = DEFAULT_MESSAGE, method: nil)
21
19
  if method && message == DEFAULT_MESSAGE
22
- super format(MESSAGE_WITH_METHOD, method: method)
20
+ super(format(MESSAGE_WITH_METHOD, method: method))
23
21
  else
24
- super message
22
+ super(message)
25
23
  end
26
24
  end
27
25
  end
28
26
 
27
+ # You're getting this because a font doesn't have a family name.
29
28
  class BadFontFamily < StandardError
30
29
  def initialize(message = 'Bad font family')
31
30
  super
@@ -50,6 +49,10 @@ module Prawn
50
49
  @kerning = options[:kerning]
51
50
  end
52
51
 
52
+ # Number of spaces in the text.
53
+ #
54
+ # @return [Integer]
55
+ # @raise [NotFinalized]
53
56
  def space_count
54
57
  unless finalized
55
58
  raise NotFinalized.new(method: 'space_count')
@@ -60,6 +63,10 @@ module Prawn
60
63
  end
61
64
  end
62
65
 
66
+ # Line width.
67
+ #
68
+ # @return [Number]
69
+ # @raise [NotFinalized]
63
70
  def line_width
64
71
  unless finalized
65
72
  raise raise NotFinalized.new(method: 'line_width')
@@ -70,19 +77,27 @@ module Prawn
70
77
  end
71
78
  end
72
79
 
80
+ # Line text.
81
+ #
82
+ # @return [String]
83
+ # @raise [NotFinalized]
73
84
  def line
74
85
  unless finalized
75
86
  raise NotFinalized.new(method: 'line')
76
87
  end
77
88
 
78
- @fragments.map do |fragment|
79
- fragment.text.dup.encode(::Encoding::UTF_8)
80
- rescue ::Encoding::InvalidByteSequenceError,
81
- ::Encoding::UndefinedConversionError
82
- fragment.text.dup.force_encoding(::Encoding::UTF_8)
83
- end.join
89
+ @fragments.map { |fragment|
90
+ begin
91
+ fragment.text.dup.encode(::Encoding::UTF_8)
92
+ rescue ::Encoding::InvalidByteSequenceError, ::Encoding::UndefinedConversionError
93
+ fragment.text.dup.force_encoding(::Encoding::UTF_8)
94
+ end
95
+ }.join
84
96
  end
85
97
 
98
+ # Finish laying out current line.
99
+ #
100
+ # @return [void]
86
101
  def finalize_line
87
102
  @finalized = true
88
103
 
@@ -95,7 +110,7 @@ module Prawn
95
110
  fragment = Prawn::Text::Formatted::Fragment.new(
96
111
  text,
97
112
  format_state,
98
- @document
113
+ @document,
99
114
  )
100
115
  @fragments << fragment
101
116
  self.fragment_measurements = fragment
@@ -103,6 +118,10 @@ module Prawn
103
118
  end
104
119
  end
105
120
 
121
+ # Set new fragment array.
122
+ #
123
+ # @param array [Array<Hash>]
124
+ # @return [void]
106
125
  def format_array=(array)
107
126
  initialize_line
108
127
  @unconsumed = []
@@ -113,6 +132,9 @@ module Prawn
113
132
  end
114
133
  end
115
134
 
135
+ # Prepare for new line layout.
136
+ #
137
+ # @return [void]
116
138
  def initialize_line
117
139
  @finalized = false
118
140
  @max_line_height = 0
@@ -123,10 +145,17 @@ module Prawn
123
145
  @fragments = []
124
146
  end
125
147
 
148
+ # Were all fragments processed?
149
+ #
150
+ # @return [Boolean]
126
151
  def finished?
127
152
  @unconsumed.empty?
128
153
  end
129
154
 
155
+ # Get the next unprocessed string.
156
+ #
157
+ # @return [String, nil]
158
+ # @raise [NotFinalized]
130
159
  def next_string
131
160
  if finalized
132
161
  raise NotFinalized.new(method: 'next_string')
@@ -143,6 +172,9 @@ module Prawn
143
172
  end
144
173
  end
145
174
 
175
+ # Get the next unprocessed string keeping it in the queue.
176
+ #
177
+ # @return [String, nil]
146
178
  def preview_next_string
147
179
  next_unconsumed_hash = @unconsumed.first
148
180
 
@@ -151,6 +183,11 @@ module Prawn
151
183
  end
152
184
  end
153
185
 
186
+ # Apply color and font settings.
187
+ #
188
+ # @param fragment [Prawn::Text::Formatted::Fragment]
189
+ # @yield
190
+ # @return [void]
154
191
  def apply_color_and_font_settings(fragment, &block)
155
192
  if fragment.color
156
193
  original_fill_color = @document.fill_color
@@ -165,6 +202,11 @@ module Prawn
165
202
  end
166
203
  end
167
204
 
205
+ # Apply font settings.
206
+ #
207
+ # @param fragment [Prawn::Text::Formatted::Fragment]
208
+ # @yield
209
+ # @return [void]
168
210
  def apply_font_settings(fragment = nil, &block)
169
211
  if fragment.nil?
170
212
  font = current_format_state[:font]
@@ -185,7 +227,7 @@ module Prawn
185
227
  raise BadFontFamily unless @document.font.family
186
228
 
187
229
  @document.font(
188
- font || @document.font.family, style: font_style
230
+ font || @document.font.family, style: font_style,
189
231
  ) do
190
232
  apply_font_size(size, styles, &block)
191
233
  end
@@ -195,6 +237,12 @@ module Prawn
195
237
  end
196
238
  end
197
239
 
240
+ # Update last fragment's text.
241
+ #
242
+ # @param printed [String]
243
+ # @param unprinted [String]
244
+ # @param normalized_soft_hyphen [Boolean]
245
+ # @return [void]
198
246
  def update_last_string(printed, unprinted, normalized_soft_hyphen = nil)
199
247
  return if printed.nil?
200
248
 
@@ -214,6 +262,10 @@ module Prawn
214
262
  load_previous_format_state if printed.empty?
215
263
  end
216
264
 
265
+ # Get the next fragment.
266
+ #
267
+ # @return [Prawn::Text::Formatted::Fragment]
268
+ # @raise [NotFinalized]
217
269
  def retrieve_fragment
218
270
  unless finalized
219
271
  raise NotFinalized, 'Lines must be finalized before fragments can be retrieved'
@@ -222,6 +274,9 @@ module Prawn
222
274
  @fragments.shift
223
275
  end
224
276
 
277
+ # Repack remaining fragments.
278
+ #
279
+ # @return [void]
225
280
  def repack_unretrieved
226
281
  new_unconsumed = []
227
282
  # rubocop: disable Lint/AssignmentInCondition
@@ -233,6 +288,10 @@ module Prawn
233
288
  @unconsumed = new_unconsumed.concat(@unconsumed)
234
289
  end
235
290
 
291
+ # Get font variant from fragment styles.
292
+ #
293
+ # @param styles [Array<Symbol>]
294
+ # @return [Symbol]
236
295
  def font_style(styles)
237
296
  styles = Array(styles)
238
297
  if styles.include?(:bold) && styles.include?(:italic)
@@ -277,13 +336,15 @@ module Prawn
277
336
 
278
337
  def subscript?(styles)
279
338
  if styles.nil? then false
280
- else styles.include?(:subscript)
339
+ else
340
+ styles.include?(:subscript)
281
341
  end
282
342
  end
283
343
 
284
344
  def superscript?(styles)
285
345
  if styles.nil? then false
286
- else styles.include?(:superscript)
346
+ else
347
+ styles.include?(:superscript)
287
348
  end
288
349
  end
289
350
 
@@ -307,7 +368,7 @@ module Prawn
307
368
  apply_font_settings(fragment) do
308
369
  fragment.width = @document.width_of(
309
370
  fragment.text,
310
- kerning: @kerning
371
+ kerning: @kerning,
311
372
  )
312
373
  fragment.line_height = @document.font.height
313
374
  fragment.descender = @document.font.descender
@@ -318,15 +379,15 @@ module Prawn
318
379
  def line_measurement_maximums=(fragment)
319
380
  @max_line_height = [
320
381
  defined?(@max_line_height) && @max_line_height,
321
- fragment.line_height
382
+ fragment.line_height,
322
383
  ].compact.max
323
384
  @max_descender = [
324
385
  defined?(@max_descender) && @max_descender,
325
- fragment.descender
386
+ fragment.descender,
326
387
  ].compact.max
327
388
  @max_ascender = [
328
389
  defined?(@max_ascender) && @max_ascender,
329
- fragment.ascender
390
+ fragment.ascender,
330
391
  ].compact.max
331
392
  end
332
393
  end