prawn 2.1.0 → 2.2.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 (278) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +2 -0
  3. data.tar.gz.sig +0 -0
  4. data/Gemfile +1 -9
  5. data/Rakefile +12 -22
  6. data/lib/prawn.rb +29 -48
  7. data/lib/prawn/document.rb +148 -123
  8. data/lib/prawn/document/bounding_box.rb +33 -26
  9. data/lib/prawn/document/column_box.rb +5 -7
  10. data/lib/prawn/document/internals.rb +6 -6
  11. data/lib/prawn/document/span.rb +20 -17
  12. data/lib/prawn/encoding.rb +65 -67
  13. data/lib/prawn/errors.rb +10 -7
  14. data/lib/prawn/font.rb +78 -62
  15. data/lib/prawn/font/afm.rb +93 -66
  16. data/lib/prawn/font/dfont.rb +2 -10
  17. data/lib/prawn/font/ttc.rb +34 -0
  18. data/lib/prawn/font/ttf.rb +73 -65
  19. data/lib/prawn/font_metric_cache.rb +9 -8
  20. data/lib/prawn/graphics.rb +110 -70
  21. data/lib/prawn/graphics/blend_mode.rb +7 -8
  22. data/lib/prawn/graphics/cap_style.rb +2 -4
  23. data/lib/prawn/graphics/color.rb +23 -26
  24. data/lib/prawn/graphics/dash.rb +22 -12
  25. data/lib/prawn/graphics/join_style.rb +8 -4
  26. data/lib/prawn/graphics/patterns.rb +185 -96
  27. data/lib/prawn/graphics/transformation.rb +11 -9
  28. data/lib/prawn/graphics/transparency.rb +15 -13
  29. data/lib/prawn/grid.rb +20 -20
  30. data/lib/prawn/image_handler.rb +4 -6
  31. data/lib/prawn/images.rb +22 -15
  32. data/lib/prawn/images/image.rb +0 -1
  33. data/lib/prawn/images/jpg.rb +26 -22
  34. data/lib/prawn/images/png.rb +60 -57
  35. data/lib/prawn/measurement_extensions.rb +8 -9
  36. data/lib/prawn/measurements.rb +14 -15
  37. data/lib/prawn/outline.rb +96 -78
  38. data/lib/prawn/repeater.rb +12 -10
  39. data/lib/prawn/security.rb +66 -48
  40. data/lib/prawn/security/arcfour.rb +1 -3
  41. data/lib/prawn/soft_mask.rb +23 -25
  42. data/lib/prawn/stamp.rb +16 -12
  43. data/lib/prawn/text.rb +59 -45
  44. data/lib/prawn/text/box.rb +9 -8
  45. data/lib/prawn/text/formatted.rb +4 -6
  46. data/lib/prawn/text/formatted/arranger.rb +51 -30
  47. data/lib/prawn/text/formatted/box.rb +112 -88
  48. data/lib/prawn/text/formatted/fragment.rb +10 -15
  49. data/lib/prawn/text/formatted/line_wrap.rb +118 -61
  50. data/lib/prawn/text/formatted/parser.rb +134 -110
  51. data/lib/prawn/text/formatted/wrap.rb +42 -32
  52. data/lib/prawn/transformation_stack.rb +3 -4
  53. data/lib/prawn/utilities.rb +6 -21
  54. data/lib/prawn/version.rb +1 -3
  55. data/lib/prawn/view.rb +4 -2
  56. data/manual/basic_concepts/adding_pages.rb +4 -7
  57. data/manual/basic_concepts/basic_concepts.rb +29 -22
  58. data/manual/basic_concepts/creation.rb +8 -11
  59. data/manual/basic_concepts/cursor.rb +2 -5
  60. data/manual/basic_concepts/measurement.rb +3 -6
  61. data/manual/basic_concepts/origin.rb +3 -6
  62. data/manual/basic_concepts/other_cursor_helpers.rb +9 -12
  63. data/manual/basic_concepts/view.rb +20 -16
  64. data/manual/bounding_box/bounding_box.rb +27 -24
  65. data/manual/bounding_box/bounds.rb +9 -12
  66. data/manual/bounding_box/canvas.rb +2 -5
  67. data/manual/bounding_box/creation.rb +4 -7
  68. data/manual/bounding_box/indentation.rb +12 -15
  69. data/manual/bounding_box/nesting.rb +22 -17
  70. data/manual/bounding_box/russian_boxes.rb +8 -9
  71. data/manual/bounding_box/stretchy.rb +10 -13
  72. data/manual/contents.rb +26 -22
  73. data/manual/cover.rb +22 -20
  74. data/manual/document_and_page_options/background.rb +9 -13
  75. data/manual/document_and_page_options/document_and_page_options.rb +23 -20
  76. data/manual/document_and_page_options/metadata.rb +16 -16
  77. data/manual/document_and_page_options/page_margins.rb +16 -20
  78. data/manual/document_and_page_options/page_size.rb +11 -12
  79. data/manual/document_and_page_options/print_scaling.rb +15 -15
  80. data/manual/example_helper.rb +2 -4
  81. data/manual/graphics/blend_mode.rb +10 -9
  82. data/manual/graphics/circle_and_ellipse.rb +2 -5
  83. data/manual/graphics/color.rb +5 -9
  84. data/manual/graphics/common_lines.rb +5 -8
  85. data/manual/graphics/fill_and_stroke.rb +2 -5
  86. data/manual/graphics/fill_rules.rb +7 -10
  87. data/manual/graphics/gradients.rb +25 -21
  88. data/manual/graphics/graphics.rb +49 -43
  89. data/manual/graphics/helper.rb +10 -9
  90. data/manual/graphics/line_width.rb +5 -7
  91. data/manual/graphics/lines_and_curves.rb +5 -8
  92. data/manual/graphics/polygon.rb +4 -8
  93. data/manual/graphics/rectangle.rb +2 -5
  94. data/manual/graphics/rotate.rb +4 -7
  95. data/manual/graphics/scale.rb +12 -15
  96. data/manual/graphics/soft_masks.rb +1 -4
  97. data/manual/graphics/stroke_cap.rb +3 -6
  98. data/manual/graphics/stroke_dash.rb +9 -12
  99. data/manual/graphics/stroke_join.rb +2 -5
  100. data/manual/graphics/translate.rb +7 -10
  101. data/manual/graphics/transparency.rb +5 -8
  102. data/manual/how_to_read_this_manual.rb +4 -6
  103. data/manual/images/absolute_position.rb +4 -7
  104. data/manual/images/fit.rb +5 -8
  105. data/manual/images/horizontal.rb +6 -9
  106. data/manual/images/images.rb +25 -23
  107. data/manual/images/plain_image.rb +3 -6
  108. data/manual/images/scale.rb +7 -10
  109. data/manual/images/vertical.rb +10 -13
  110. data/manual/images/width_and_height.rb +8 -11
  111. data/manual/layout/boxes.rb +3 -6
  112. data/manual/layout/content.rb +5 -8
  113. data/manual/layout/layout.rb +16 -16
  114. data/manual/layout/simple_grid.rb +4 -7
  115. data/manual/outline/add_subsection_to.rb +18 -21
  116. data/manual/outline/insert_section_after.rb +13 -16
  117. data/manual/outline/outline.rb +19 -17
  118. data/manual/outline/sections_and_pages.rb +15 -18
  119. data/manual/repeatable_content/alternate_page_numbering.rb +19 -17
  120. data/manual/repeatable_content/page_numbering.rb +15 -16
  121. data/manual/repeatable_content/repeatable_content.rb +23 -19
  122. data/manual/repeatable_content/repeater.rb +12 -15
  123. data/manual/repeatable_content/stamp.rb +12 -15
  124. data/manual/security/encryption.rb +7 -10
  125. data/manual/security/permissions.rb +17 -14
  126. data/manual/security/security.rb +17 -16
  127. data/manual/table.rb +2 -4
  128. data/manual/text/alignment.rb +14 -17
  129. data/manual/text/color.rb +10 -11
  130. data/manual/text/column_box.rb +5 -8
  131. data/manual/text/fallback_fonts.rb +23 -21
  132. data/manual/text/font.rb +9 -12
  133. data/manual/text/font_size.rb +11 -14
  134. data/manual/text/font_style.rb +4 -7
  135. data/manual/text/formatted_callbacks.rb +23 -21
  136. data/manual/text/formatted_text.rb +31 -25
  137. data/manual/text/free_flowing_text.rb +18 -21
  138. data/manual/text/inline.rb +16 -19
  139. data/manual/text/kerning_and_character_spacing.rb +12 -15
  140. data/manual/text/leading.rb +5 -8
  141. data/manual/text/line_wrapping.rb +33 -17
  142. data/manual/text/paragraph_indentation.rb +11 -14
  143. data/manual/text/positioned_text.rb +13 -16
  144. data/manual/text/registering_families.rb +16 -19
  145. data/manual/text/rendering_and_color.rb +7 -10
  146. data/manual/text/right_to_left_text.rb +24 -19
  147. data/manual/text/rotation.rb +26 -23
  148. data/manual/text/single_usage.rb +6 -9
  149. data/manual/text/text.rb +56 -52
  150. data/manual/text/text_box_excess.rb +18 -17
  151. data/manual/text/text_box_extensions.rb +16 -15
  152. data/manual/text/text_box_overflow.rb +15 -18
  153. data/manual/text/utf8.rb +9 -12
  154. data/manual/text/win_ansi_charset.rb +18 -19
  155. data/prawn.gemspec +37 -27
  156. data/spec/extensions/encoding_helpers.rb +0 -2
  157. data/spec/manual_spec.rb +33 -0
  158. data/spec/prawn/document/bounding_box_spec.rb +546 -0
  159. data/spec/prawn/document/column_box_spec.rb +73 -0
  160. data/spec/prawn/document/security_spec.rb +173 -0
  161. data/spec/prawn/document_annotations_spec.rb +74 -0
  162. data/spec/prawn/document_destinations_spec.rb +13 -0
  163. data/spec/prawn/document_grid_spec.rb +96 -0
  164. data/spec/prawn/document_reference_spec.rb +25 -0
  165. data/spec/prawn/document_span_spec.rb +34 -0
  166. data/spec/prawn/document_spec.rb +751 -0
  167. data/spec/prawn/font_metric_cache_spec.rb +52 -0
  168. data/spec/prawn/font_spec.rb +513 -0
  169. data/spec/prawn/graphics/blend_mode_spec.rb +61 -0
  170. data/spec/prawn/graphics/transparency_spec.rb +79 -0
  171. data/spec/prawn/graphics_spec.rb +817 -0
  172. data/spec/prawn/graphics_stroke_styles_spec.rb +227 -0
  173. data/spec/{image_handler_spec.rb → prawn/image_handler_spec.rb} +13 -15
  174. data/spec/prawn/images/jpg_spec.rb +18 -0
  175. data/spec/prawn/images/png_spec.rb +281 -0
  176. data/spec/prawn/images_spec.rb +170 -0
  177. data/spec/prawn/measurements_extensions_spec.rb +22 -0
  178. data/spec/prawn/outline_spec.rb +408 -0
  179. data/spec/prawn/repeater_spec.rb +163 -0
  180. data/spec/prawn/soft_mask_spec.rb +72 -0
  181. data/spec/prawn/stamp_spec.rb +168 -0
  182. data/spec/prawn/text/box_spec.rb +1113 -0
  183. data/spec/prawn/text/formatted/arranger_spec.rb +464 -0
  184. data/spec/prawn/text/formatted/box_spec.rb +825 -0
  185. data/spec/prawn/text/formatted/fragment_spec.rb +341 -0
  186. data/spec/prawn/text/formatted/line_wrap_spec.rb +491 -0
  187. data/spec/prawn/text/formatted/parser_spec.rb +667 -0
  188. data/spec/prawn/text_draw_text_spec.rb +147 -0
  189. data/spec/prawn/text_rendering_mode_spec.rb +42 -0
  190. data/spec/prawn/text_spacing_spec.rb +93 -0
  191. data/spec/prawn/text_spec.rb +601 -0
  192. data/spec/prawn/text_with_inline_formatting_spec.rb +33 -0
  193. data/spec/{transformation_stack_spec.rb → prawn/transformation_stack_spec.rb} +21 -20
  194. data/spec/prawn/view_spec.rb +45 -0
  195. data/spec/spec_helper.rb +16 -16
  196. metadata +96 -151
  197. metadata.gz.sig +1 -0
  198. data/data/images/16bit.alpha +0 -0
  199. data/data/images/16bit.color +0 -0
  200. data/data/images/16bit.png +0 -0
  201. data/data/images/arrow.png +0 -0
  202. data/data/images/arrow2.png +0 -0
  203. data/data/images/blend_modes_bottom_layer.jpg +0 -0
  204. data/data/images/blend_modes_top_layer.jpg +0 -0
  205. data/data/images/dice.alpha +0 -0
  206. data/data/images/dice.color +0 -0
  207. data/data/images/dice.png +0 -0
  208. data/data/images/dice_interlaced.png +0 -0
  209. data/data/images/fractal.jpg +0 -0
  210. data/data/images/indexed_color.dat +0 -0
  211. data/data/images/indexed_color.png +0 -0
  212. data/data/images/indexed_transparency.png +0 -0
  213. data/data/images/indexed_transparency_alpha.dat +0 -0
  214. data/data/images/indexed_transparency_color.dat +0 -0
  215. data/data/images/letterhead.jpg +0 -0
  216. data/data/images/license.md +0 -8
  217. data/data/images/page_white_text.alpha +0 -0
  218. data/data/images/page_white_text.color +0 -0
  219. data/data/images/page_white_text.png +0 -0
  220. data/data/images/pigs.jpg +0 -0
  221. data/data/images/prawn.png +0 -0
  222. data/data/images/ruport.png +0 -0
  223. data/data/images/ruport_data.dat +0 -0
  224. data/data/images/ruport_transparent.png +0 -0
  225. data/data/images/ruport_type0.png +0 -0
  226. data/data/images/stef.jpg +0 -0
  227. data/data/images/tru256.bmp +0 -0
  228. data/data/images/web-links.dat +0 -1
  229. data/data/images/web-links.png +0 -0
  230. data/data/pdfs/complex_template.pdf +0 -0
  231. data/data/pdfs/contains_ttf_font.pdf +0 -0
  232. data/data/pdfs/encrypted.pdf +0 -0
  233. data/data/pdfs/form.pdf +1 -819
  234. data/data/pdfs/hexagon.pdf +0 -61
  235. data/data/pdfs/indirect_reference.pdf +0 -86
  236. data/data/pdfs/multipage_template.pdf +0 -127
  237. data/data/pdfs/nested_pages.pdf +0 -118
  238. data/data/pdfs/page_without_mediabox.pdf +0 -193
  239. data/data/pdfs/resources_as_indirect_object.pdf +0 -83
  240. data/data/pdfs/two_hexagons.pdf +0 -90
  241. data/data/pdfs/version_1_6.pdf +0 -61
  242. data/data/shift_jis_text.txt +0 -1
  243. data/spec/acceptance/png_spec.rb +0 -35
  244. data/spec/annotations_spec.rb +0 -67
  245. data/spec/blend_mode_spec.rb +0 -71
  246. data/spec/bounding_box_spec.rb +0 -501
  247. data/spec/column_box_spec.rb +0 -59
  248. data/spec/destinations_spec.rb +0 -13
  249. data/spec/document_spec.rb +0 -738
  250. data/spec/font_metric_cache_spec.rb +0 -52
  251. data/spec/font_spec.rb +0 -475
  252. data/spec/formatted_text_arranger_spec.rb +0 -452
  253. data/spec/formatted_text_box_spec.rb +0 -716
  254. data/spec/formatted_text_fragment_spec.rb +0 -299
  255. data/spec/graphics_spec.rb +0 -705
  256. data/spec/grid_spec.rb +0 -95
  257. data/spec/images_spec.rb +0 -167
  258. data/spec/inline_formatted_text_parser_spec.rb +0 -568
  259. data/spec/jpg_spec.rb +0 -23
  260. data/spec/line_wrap_spec.rb +0 -366
  261. data/spec/measurement_units_spec.rb +0 -22
  262. data/spec/outline_spec.rb +0 -409
  263. data/spec/png_spec.rb +0 -257
  264. data/spec/reference_spec.rb +0 -25
  265. data/spec/repeater_spec.rb +0 -154
  266. data/spec/security_spec.rb +0 -151
  267. data/spec/soft_mask_spec.rb +0 -78
  268. data/spec/span_spec.rb +0 -43
  269. data/spec/stamp_spec.rb +0 -179
  270. data/spec/stroke_styles_spec.rb +0 -208
  271. data/spec/text_at_spec.rb +0 -142
  272. data/spec/text_box_spec.rb +0 -1042
  273. data/spec/text_rendering_mode_spec.rb +0 -45
  274. data/spec/text_spacing_spec.rb +0 -93
  275. data/spec/text_spec.rb +0 -543
  276. data/spec/text_with_inline_formatting_spec.rb +0 -35
  277. data/spec/transparency_spec.rb +0 -91
  278. data/spec/view_spec.rb +0 -42
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  # text/rectangle.rb : Implements text boxes
4
2
  #
5
3
  # Copyright November 2009, Daniel Nelson. All Rights Reserved.
@@ -7,7 +5,7 @@
7
5
  # This is free software. Please see the LICENSE and COPYING files for details.
8
6
  #
9
7
 
10
- require_relative "formatted/box"
8
+ require_relative 'formatted/box'
11
9
 
12
10
  module Prawn
13
11
  module Text
@@ -52,7 +50,8 @@ module Prawn
52
50
  # <tt>[x, y]</tt>. The upper left corner of the box
53
51
  # [@document.bounds.left, @document.bounds.top]
54
52
  # <tt>:width</tt>::
55
- # <tt>number</tt>. The width of the box [@document.bounds.right - @at[0]]
53
+ # <tt>number</tt>. The width of the box
54
+ # [@document.bounds.right - @at[0]]
56
55
  # <tt>:height</tt>::
57
56
  # <tt>number</tt>. The height of the box [default_height()]
58
57
  # <tt>:direction</tt>::
@@ -82,7 +81,8 @@ module Prawn
82
81
  # <tt>number</tt>. Additional space between lines [value of
83
82
  # document.default_leading]
84
83
  # <tt>:single_line</tt>::
85
- # <tt>boolean</tt>. If true, then only the first line will be drawn [false]
84
+ # <tt>boolean</tt>. If true, then only the first line will be drawn
85
+ # [false]
86
86
  # <tt>:overflow</tt>::
87
87
  # <tt>:truncate</tt>, <tt>:shrink_to_fit</tt>, or <tt>:expand</tt>
88
88
  # This controls the behavior when the amount of text
@@ -105,9 +105,10 @@ module Prawn
105
105
  options = options.dup
106
106
  options[:document] = self
107
107
 
108
- box = if p = options.delete(:inline_format)
108
+ box = if options[:inline_format]
109
+ p = options.delete(:inline_format)
109
110
  p = [] unless p.is_a?(Array)
110
- array = self.text_formatter.format(string, *p)
111
+ array = text_formatter.format(string, *p)
111
112
  Text::Formatted::Box.new(array, options)
112
113
  else
113
114
  Text::Box.new(string, options)
@@ -126,7 +127,7 @@ module Prawn
126
127
  #
127
128
  class Box < Prawn::Text::Formatted::Box
128
129
  def initialize(string, options = {})
129
- super([{ :text => string }], options)
130
+ super([{ text: string }], options)
130
131
  end
131
132
 
132
133
  def render(flags = {})
@@ -1,7 +1,5 @@
1
- # encoding: utf-8
1
+ require_relative 'formatted/wrap'
2
2
 
3
- require_relative "formatted/wrap"
4
-
5
- require_relative "formatted/box"
6
- require_relative "formatted/parser"
7
- require_relative "formatted/fragment"
3
+ require_relative 'formatted/box'
4
+ require_relative 'formatted/parser'
5
+ require_relative 'formatted/fragment'
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  # core/text/formatted/arranger.rb : Implements a data structure for 2-stage
4
2
  # processing of lines of formatted text
5
3
  #
@@ -7,6 +5,8 @@
7
5
  #
8
6
  # This is free software. Please see the LICENSE and COPYING files for details.
9
7
 
8
+ # rubocop: disable Style/AccessorMethodName
9
+
10
10
  module Prawn
11
11
  module Text
12
12
  module Formatted #:nodoc:
@@ -33,7 +33,7 @@ module Prawn
33
33
 
34
34
  def space_count
35
35
  unless finalized
36
- fail "Lines must be finalized before calling #space_count"
36
+ raise 'Lines must be finalized before calling #space_count'
37
37
  end
38
38
 
39
39
  @fragments.inject(0) do |sum, fragment|
@@ -43,7 +43,7 @@ module Prawn
43
43
 
44
44
  def line_width
45
45
  unless finalized
46
- fail "Lines must be finalized before calling #line_width"
46
+ raise 'Lines must be finalized before calling #line_width'
47
47
  end
48
48
 
49
49
  @fragments.inject(0) do |sum, fragment|
@@ -53,11 +53,16 @@ module Prawn
53
53
 
54
54
  def line
55
55
  unless finalized
56
- fail "Lines must be finalized before calling #line"
56
+ raise 'Lines must be finalized before calling #line'
57
57
  end
58
58
 
59
59
  @fragments.collect do |fragment|
60
- fragment.text.dup.force_encoding(::Encoding::UTF_8)
60
+ begin
61
+ fragment.text.dup.encode(::Encoding::UTF_8)
62
+ rescue ::Encoding::InvalidByteSequenceError,
63
+ ::Encoding::UndefinedConversionError
64
+ fragment.text.dup.force_encoding(::Encoding::UTF_8)
65
+ end
61
66
  end.join
62
67
  end
63
68
 
@@ -70,9 +75,11 @@ module Prawn
70
75
  text = hash[:text]
71
76
  format_state = hash.dup
72
77
  format_state.delete(:text)
73
- fragment = Prawn::Text::Formatted::Fragment.new(text,
74
- format_state,
75
- @document)
78
+ fragment = Prawn::Text::Formatted::Fragment.new(
79
+ text,
80
+ format_state,
81
+ @document
82
+ )
76
83
  @fragments << fragment
77
84
  set_fragment_measurements(fragment)
78
85
  set_line_measurement_maximums(fragment)
@@ -84,7 +91,7 @@ module Prawn
84
91
  @unconsumed = []
85
92
  array.each do |hash|
86
93
  hash[:text].scan(/[^\n]+|\n/) do |line|
87
- @unconsumed << hash.merge(:text => line)
94
+ @unconsumed << hash.merge(text: line)
88
95
  end
89
96
  end
90
97
  end
@@ -100,12 +107,12 @@ module Prawn
100
107
  end
101
108
 
102
109
  def finished?
103
- @unconsumed.length == 0
110
+ @unconsumed.empty?
104
111
  end
105
112
 
106
113
  def next_string
107
114
  if finalized
108
- fail "Lines must not be finalized when calling #next_string"
115
+ raise 'Lines must not be finalized when calling #next_string'
109
116
  end
110
117
 
111
118
  next_unconsumed_hash = @unconsumed.shift
@@ -146,21 +153,22 @@ module Prawn
146
153
  font = current_format_state[:font]
147
154
  size = current_format_state[:size]
148
155
  character_spacing = current_format_state[:character_spacing] ||
149
- @document.character_spacing
156
+ @document.character_spacing
150
157
  styles = current_format_state[:styles]
151
- font_style = font_style(styles)
152
158
  else
153
159
  font = fragment.font
154
160
  size = fragment.size
155
161
  character_spacing = fragment.character_spacing
156
162
  styles = fragment.styles
157
- font_style = font_style(styles)
158
163
  end
164
+ font_style = font_style(styles)
159
165
 
160
166
  @document.character_spacing(character_spacing) do
161
167
  if font || font_style != :normal
162
- fail "Bad font family" unless @document.font.family
163
- @document.font(font || @document.font.family, :style => font_style) do
168
+ raise 'Bad font family' unless @document.font.family
169
+ @document.font(
170
+ font || @document.font.family, style: font_style
171
+ ) do
164
172
  apply_font_size(size, styles, &block)
165
173
  end
166
174
  else
@@ -181,7 +189,7 @@ module Prawn
181
189
  end
182
190
 
183
191
  unless unprinted.empty?
184
- @unconsumed.unshift(@current_format_state.merge(:text => unprinted))
192
+ @unconsumed.unshift(@current_format_state.merge(text: unprinted))
185
193
  end
186
194
 
187
195
  load_previous_format_state if printed.empty?
@@ -189,7 +197,7 @@ module Prawn
189
197
 
190
198
  def retrieve_fragment
191
199
  unless finalized
192
- fail "Lines must be finalized before fragments can be retrieved"
200
+ raise 'Lines must be finalized before fragments can be retrieved'
193
201
  end
194
202
 
195
203
  @fragments.shift
@@ -197,9 +205,11 @@ module Prawn
197
205
 
198
206
  def repack_unretrieved
199
207
  new_unconsumed = []
208
+ # rubocop: disable Lint/AssignmentInCondition
200
209
  while fragment = retrieve_fragment
210
+ # rubocop: enable Lint/AssignmentInCondition
201
211
  fragment.include_trailing_white_space!
202
- new_unconsumed << fragment.format_state.merge(:text => fragment.text)
212
+ new_unconsumed << fragment.format_state.merge(text: fragment.text)
203
213
  end
204
214
  @unconsumed = new_unconsumed.concat(@unconsumed)
205
215
  end
@@ -233,11 +243,11 @@ module Prawn
233
243
  def apply_font_size(size, styles)
234
244
  if subscript?(styles) || superscript?(styles)
235
245
  relative_size = 0.583
236
- if size.nil?
237
- size = @document.font_size * relative_size
238
- else
239
- size = size * relative_size
240
- end
246
+ size = if size.nil?
247
+ @document.font_size * relative_size
248
+ else
249
+ size * relative_size
250
+ end
241
251
  end
242
252
  if size.nil?
243
253
  yield
@@ -276,8 +286,10 @@ module Prawn
276
286
 
277
287
  def set_fragment_measurements(fragment)
278
288
  apply_font_settings(fragment) do
279
- fragment.width = @document.width_of(fragment.text,
280
- :kerning => @kerning)
289
+ fragment.width = @document.width_of(
290
+ fragment.text,
291
+ kerning: @kerning
292
+ )
281
293
  fragment.line_height = @document.font.height
282
294
  fragment.descender = @document.font.descender
283
295
  fragment.ascender = @document.font.ascender
@@ -285,9 +297,18 @@ module Prawn
285
297
  end
286
298
 
287
299
  def set_line_measurement_maximums(fragment)
288
- @max_line_height = [defined?(@max_line_height) && @max_line_height, fragment.line_height].compact.max
289
- @max_descender = [defined?(@max_descender) && @max_descender, fragment.descender].compact.max
290
- @max_ascender = [defined?(@max_ascender) && @max_ascender, fragment.ascender].compact.max
300
+ @max_line_height = [
301
+ defined?(@max_line_height) && @max_line_height,
302
+ fragment.line_height
303
+ ].compact.max
304
+ @max_descender = [
305
+ defined?(@max_descender) && @max_descender,
306
+ fragment.descender
307
+ ].compact.max
308
+ @max_ascender = [
309
+ defined?(@max_ascender) && @max_ascender,
310
+ fragment.ascender
311
+ ].compact.max
291
312
  end
292
313
  end
293
314
  end
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  # text/formatted/rectangle.rb : Implements text boxes with formatted text
4
2
  #
5
3
  # Copyright February 2010, Daniel Nelson. All Rights Reserved.
@@ -54,8 +52,8 @@ module Prawn
54
52
  # a file or application to be opened locally. A clickable link will be
55
53
  # created to the provided local file or application. If the file is
56
54
  # another PDF, it will be opened in a new window. Note that you must
57
- # explicitly underline and color using the appropriate tags if you which
58
- # to draw attention to the link
55
+ # explicitly underline and color using the appropriate tags if you
56
+ # which to draw attention to the link
59
57
  # <tt>:draw_text_callback</tt>:
60
58
  # if provided, this Proc will be called instead of #draw_text! once
61
59
  # per fragment for every low-level addition of text to the page.
@@ -83,13 +81,14 @@ module Prawn
83
81
  #
84
82
  # == Exceptions
85
83
  #
86
- # Raises "Bad font family" if no font family is defined for the current font
84
+ # Raises "Bad font family" if no font family is defined for the current
85
+ # font
87
86
  #
88
87
  # Raises <tt>Prawn::Errors::CannotFit</tt> if not wide enough to print
89
88
  # any text
90
89
  #
91
90
  def formatted_text_box(array, options = {})
92
- Text::Formatted::Box.new(array, options.merge(:document => self)).render
91
+ Text::Formatted::Box.new(array, options.merge(document: self)).render
93
92
  end
94
93
 
95
94
  # Generally, one would use the Prawn::Text::Formatted#formatted_text_box
@@ -137,37 +136,38 @@ module Prawn
137
136
  # See Prawn::Text#text_box for valid options
138
137
  #
139
138
  def initialize(formatted_text, options = {})
140
- @inked = false
139
+ @inked = false
141
140
  Prawn.verify_options(valid_options, options)
142
- options = options.dup
141
+ options = options.dup
143
142
 
144
143
  self.class.extensions.reverse_each { |e| extend e }
145
144
 
146
- @overflow = options[:overflow] || :truncate
145
+ @overflow = options[:overflow] || :truncate
147
146
  @disable_wrap_by_char = options[:disable_wrap_by_char]
148
147
 
149
148
  self.original_text = formatted_text
150
- @text = nil
151
-
152
- @document = options[:document]
153
- @direction = options[:direction] || @document.text_direction
154
- @fallback_fonts = options[:fallback_fonts] ||
155
- @document.fallback_fonts
156
- @at = (options[:at] ||
157
- [@document.bounds.left, @document.bounds.top]).dup
158
- @width = options[:width] ||
159
- @document.bounds.right - @at[0]
160
- @height = options[:height] || default_height
161
- @align = options[:align] ||
162
- (@direction == :rtl ? :right : :left)
163
- @vertical_align = options[:valign] || :top
164
- @leading = options[:leading] || @document.default_leading
149
+ @text = nil
150
+
151
+ @document = options[:document]
152
+ @direction = options[:direction] || @document.text_direction
153
+ @fallback_fonts = options[:fallback_fonts] ||
154
+ @document.fallback_fonts
155
+ @at = (
156
+ options[:at] || [@document.bounds.left, @document.bounds.top]
157
+ ).dup
158
+ @width = options[:width] ||
159
+ @document.bounds.right - @at[0]
160
+ @height = options[:height] || default_height
161
+ @align = options[:align] ||
162
+ (@direction == :rtl ? :right : :left)
163
+ @vertical_align = options[:valign] || :top
164
+ @leading = options[:leading] || @document.default_leading
165
165
  @character_spacing = options[:character_spacing] ||
166
- @document.character_spacing
167
- @mode = options[:mode] || @document.text_rendering_mode
168
- @rotate = options[:rotate] || 0
169
- @rotate_around = options[:rotate_around] || :upper_left
170
- @single_line = options[:single_line]
166
+ @document.character_spacing
167
+ @mode = options[:mode] || @document.text_rendering_mode
168
+ @rotate = options[:rotate] || 0
169
+ @rotate_around = options[:rotate_around] || :upper_left
170
+ @single_line = options[:single_line]
171
171
  @draw_text_callback = options[:draw_text_callback]
172
172
 
173
173
  # if the text rendering mode is :unknown, force it back to :fill
@@ -183,25 +183,28 @@ module Prawn
183
183
  @overflow = :truncate
184
184
  end
185
185
  @min_font_size = options[:min_font_size] || 5
186
- if options[:kerning].nil? then
186
+ if options[:kerning].nil?
187
187
  options[:kerning] = @document.default_kerning?
188
188
  end
189
- @options = { :kerning => options[:kerning],
190
- :size => options[:size],
191
- :style => options[:style] }
189
+ @options = {
190
+ kerning: options[:kerning],
191
+ size: options[:size],
192
+ style: options[:style]
193
+ }
192
194
 
193
195
  super(formatted_text, options)
194
196
  end
195
197
 
196
- # Render text to the document based on the settings defined in initialize.
198
+ # Render text to the document based on the settings defined in
199
+ # initialize.
197
200
  #
198
- # In order to facilitate look-ahead calculations, <tt>render</tt> accepts
199
- # a <tt>:dry_run => true</tt> option. If provided, then everything is
200
- # executed as if rendering, with the exception that nothing is drawn on
201
- # the page. Useful for look-ahead computations of height, unprinted text,
202
- # etc.
201
+ # In order to facilitate look-ahead calculations, <tt>render</tt>
202
+ # accepts a <tt>:dry_run => true</tt> option. If provided, then
203
+ # everything is executed as if rendering, with the exception that
204
+ # nothing is drawn on the page. Useful for look-ahead computations of
205
+ # height, unprinted text, etc.
203
206
  #
204
- # Returns any text that did not print under the current settings
207
+ # Returns any text that did not print under the current settings.
205
208
  #
206
209
  def render(flags = {})
207
210
  unprinted_text = []
@@ -217,11 +220,11 @@ module Prawn
217
220
  shrink_to_fit(text) if @overflow == :shrink_to_fit
218
221
  process_vertical_alignment(text)
219
222
  @inked = true unless flags[:dry_run]
220
- if @rotate != 0 && @inked
221
- unprinted_text = render_rotated(text)
222
- else
223
- unprinted_text = wrap(text)
224
- end
223
+ unprinted_text = if @rotate != 0 && @inked
224
+ render_rotated(text)
225
+ else
226
+ wrap(text)
227
+ end
225
228
  @inked = false
226
229
  end
227
230
  end
@@ -229,7 +232,7 @@ module Prawn
229
232
  end
230
233
 
231
234
  unprinted_text.map do |e|
232
- e.merge(:text => @document.font.to_utf8(e[:text]))
235
+ e.merge(text: @document.font.to_utf8(e[:text]))
233
236
  end
234
237
  end
235
238
 
@@ -248,8 +251,10 @@ module Prawn
248
251
 
249
252
  # <tt>fragment</tt> is a Prawn::Text::Formatted::Fragment object
250
253
  #
251
- def draw_fragment(fragment, accumulated_width = 0, line_width = 0, word_spacing = 0) #:nodoc:
252
- case(@align)
254
+ def draw_fragment(
255
+ fragment, accumulated_width = 0, line_width = 0, word_spacing = 0
256
+ ) #:nodoc:
257
+ case @align
253
258
  when :left
254
259
  x = @at[0]
255
260
  when :center
@@ -257,11 +262,11 @@ module Prawn
257
262
  when :right
258
263
  x = @at[0] + @width - line_width
259
264
  when :justify
260
- if @direction == :ltr
261
- x = @at[0]
262
- else
263
- x = @at[0] + @width - line_width
264
- end
265
+ x = if @direction == :ltr
266
+ @at[0]
267
+ else
268
+ @at[0] + @width - line_width
269
+ end
265
270
  end
266
271
 
267
272
  x += accumulated_width
@@ -276,15 +281,19 @@ module Prawn
276
281
  if @inked
277
282
  draw_fragment_underlays(fragment)
278
283
 
279
- @document.word_spacing(word_spacing) {
284
+ @document.word_spacing(word_spacing) do
280
285
  if @draw_text_callback
281
- @draw_text_callback.call(fragment.text, :at => [x, y],
282
- :kerning => @kerning)
286
+ @draw_text_callback.call(
287
+ fragment.text, at: [x, y],
288
+ kerning: @kerning
289
+ )
283
290
  else
284
- @document.draw_text!(fragment.text, :at => [x, y],
285
- :kerning => @kerning)
291
+ @document.draw_text!(
292
+ fragment.text, at: [x, y],
293
+ kerning: @kerning
294
+ )
286
295
  end
287
- }
296
+ end
288
297
 
289
298
  draw_fragment_overlays(fragment)
290
299
  end
@@ -407,10 +416,14 @@ module Prawn
407
416
 
408
417
  @document.save_font do
409
418
  hash[:text].each_char do |char|
410
- font_glyph_pairs << [find_font_for_this_glyph(char,
411
- fragment_font,
412
- fallback_fonts.dup),
413
- char]
419
+ font_glyph_pairs << [
420
+ find_font_for_this_glyph(
421
+ char,
422
+ fragment_font,
423
+ fallback_fonts.dup
424
+ ),
425
+ char
426
+ ]
414
427
  end
415
428
  end
416
429
 
@@ -426,7 +439,7 @@ module Prawn
426
439
 
427
440
  def find_font_for_this_glyph(char, current_font, fallback_fonts)
428
441
  @document.font(current_font)
429
- if fallback_fonts.length == 0 || @document.font.glyph_present?(char)
442
+ if fallback_fonts.empty? || @document.font.glyph_present?(char)
430
443
  current_font
431
444
  else
432
445
  find_font_for_this_glyph(char, fallback_fonts.shift, fallback_fonts)
@@ -439,7 +452,7 @@ module Prawn
439
452
  current_font = nil
440
453
 
441
454
  font_glyph_pairs.each do |font, char|
442
- if font != current_font || fragments.count == 0
455
+ if font != current_font || fragments.count.zero?
443
456
  current_font = font
444
457
  fragment = hash.dup
445
458
  fragment[:text] = char
@@ -454,8 +467,8 @@ module Prawn
454
467
  end
455
468
 
456
469
  def move_baseline_down
457
- if @baseline_y == 0
458
- @baseline_y = -@ascender
470
+ if @baseline_y.zero?
471
+ @baseline_y = -@ascender
459
472
  else
460
473
  @baseline_y -= (@line_height + @leading)
461
474
  end
@@ -479,7 +492,10 @@ module Prawn
479
492
  # we need to wait until render() is called so that the fonts are set
480
493
  # up properly for wrapping. So guard with a boolean to ensure this is
481
494
  # only run once.
482
- return if defined?(@vertical_alignment_processed) && @vertical_alignment_processed
495
+ if defined?(@vertical_alignment_processed) &&
496
+ @vertical_alignment_processed
497
+ return
498
+ end
483
499
  @vertical_alignment_processed = true
484
500
 
485
501
  return if @vertical_align == :top
@@ -523,7 +539,7 @@ module Prawn
523
539
  # document.process_text_options sets the font
524
540
  @document.process_text_options(@options)
525
541
  @font_size = @options[:size]
526
- @kerning = @options[:kerning]
542
+ @kerning = @options[:kerning]
527
543
  end
528
544
 
529
545
  def render_rotated(text)
@@ -547,7 +563,7 @@ module Prawn
547
563
  y = @at[1]
548
564
  end
549
565
 
550
- @document.rotate(@rotate, :origin => [x, y]) do
566
+ @document.rotate(@rotate, origin: [x, y]) do
551
567
  unprinted_text = wrap(text)
552
568
  end
553
569
  unprinted_text
@@ -572,40 +588,48 @@ module Prawn
572
588
  def draw_fragment_overlay_link(fragment)
573
589
  return unless fragment.link
574
590
  box = fragment.absolute_bounding_box
575
- @document.link_annotation(box,
576
- :Border => [0, 0, 0],
577
- :A => { :Type => :Action,
578
- :S => :URI,
579
- :URI => PDF::Core::LiteralString.new(fragment.link) })
591
+ @document.link_annotation(
592
+ box,
593
+ Border: [0, 0, 0],
594
+ A: {
595
+ Type: :Action,
596
+ S: :URI,
597
+ URI: PDF::Core::LiteralString.new(fragment.link)
598
+ }
599
+ )
580
600
  end
581
601
 
582
602
  def draw_fragment_overlay_anchor(fragment)
583
603
  return unless fragment.anchor
584
604
  box = fragment.absolute_bounding_box
585
- @document.link_annotation(box,
586
- :Border => [0, 0, 0],
587
- :Dest => fragment.anchor)
605
+ @document.link_annotation(
606
+ box,
607
+ Border: [0, 0, 0],
608
+ Dest: fragment.anchor
609
+ )
588
610
  end
589
611
 
590
612
  def draw_fragment_overlay_local(fragment)
591
613
  return unless fragment.local
592
614
  box = fragment.absolute_bounding_box
593
- @document.link_annotation(box,
594
- :Border => [0, 0, 0],
595
- :A => { :Type => :Action,
596
- :S => :Launch,
597
- :F => PDF::Core::LiteralString.new(fragment.local),
598
- :NewWindow => true })
615
+ @document.link_annotation(
616
+ box,
617
+ Border: [0, 0, 0],
618
+ A: {
619
+ Type: :Action,
620
+ S: :Launch,
621
+ F: PDF::Core::LiteralString.new(fragment.local),
622
+ NewWindow: true
623
+ }
624
+ )
599
625
  end
600
626
 
601
627
  def draw_fragment_overlay_styles(fragment)
602
- underline = fragment.styles.include?(:underline)
603
- if underline
628
+ if fragment.styles.include?(:underline)
604
629
  @document.stroke_line(fragment.underline_points)
605
630
  end
606
631
 
607
- strikethrough = fragment.styles.include?(:strikethrough)
608
- if strikethrough
632
+ if fragment.styles.include?(:strikethrough)
609
633
  @document.stroke_line(fragment.strikethrough_points)
610
634
  end
611
635
  end