prawn 2.1.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (280) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/GPLv2 +20 -21
  5. data/Gemfile +3 -9
  6. data/Rakefile +9 -41
  7. data/lib/prawn.rb +37 -49
  8. data/lib/prawn/document.rb +193 -141
  9. data/lib/prawn/document/bounding_box.rb +50 -30
  10. data/lib/prawn/document/column_box.rb +7 -7
  11. data/lib/prawn/document/internals.rb +8 -6
  12. data/lib/prawn/document/span.rb +22 -16
  13. data/lib/prawn/encoding.rb +69 -68
  14. data/lib/prawn/errors.rb +12 -7
  15. data/lib/prawn/font.rb +104 -69
  16. data/lib/prawn/font_metric_cache.rb +20 -13
  17. data/lib/prawn/{font → fonts}/afm.rb +108 -72
  18. data/lib/prawn/{font → fonts}/dfont.rb +5 -11
  19. data/lib/prawn/fonts/otf.rb +11 -0
  20. data/lib/prawn/fonts/ttc.rb +36 -0
  21. data/lib/prawn/{font → fonts}/ttf.rb +126 -81
  22. data/lib/prawn/graphics.rb +119 -81
  23. data/lib/prawn/graphics/blend_mode.rb +9 -8
  24. data/lib/prawn/graphics/cap_style.rb +3 -3
  25. data/lib/prawn/graphics/color.rb +43 -39
  26. data/lib/prawn/graphics/dash.rb +23 -11
  27. data/lib/prawn/graphics/join_style.rb +9 -3
  28. data/lib/prawn/graphics/patterns.rb +204 -102
  29. data/lib/prawn/graphics/transformation.rb +15 -9
  30. data/lib/prawn/graphics/transparency.rb +17 -13
  31. data/lib/prawn/grid.rb +84 -48
  32. data/lib/prawn/image_handler.rb +5 -5
  33. data/lib/prawn/images.rb +60 -49
  34. data/lib/prawn/images/image.rb +2 -1
  35. data/lib/prawn/images/jpg.rb +31 -22
  36. data/lib/prawn/images/png.rb +67 -63
  37. data/lib/prawn/measurement_extensions.rb +10 -9
  38. data/lib/prawn/measurements.rb +19 -15
  39. data/lib/prawn/outline.rb +98 -77
  40. data/lib/prawn/repeater.rb +15 -11
  41. data/lib/prawn/security.rb +93 -70
  42. data/lib/prawn/security/arcfour.rb +2 -2
  43. data/lib/prawn/soft_mask.rb +26 -26
  44. data/lib/prawn/stamp.rb +20 -13
  45. data/lib/prawn/text.rb +76 -60
  46. data/lib/prawn/text/box.rb +18 -14
  47. data/lib/prawn/text/formatted.rb +5 -5
  48. data/lib/prawn/text/formatted/arranger.rb +80 -40
  49. data/lib/prawn/text/formatted/box.rb +140 -101
  50. data/lib/prawn/text/formatted/fragment.rb +11 -14
  51. data/lib/prawn/text/formatted/line_wrap.rb +128 -67
  52. data/lib/prawn/text/formatted/parser.rb +147 -123
  53. data/lib/prawn/text/formatted/wrap.rb +48 -32
  54. data/lib/prawn/transformation_stack.rb +7 -5
  55. data/lib/prawn/utilities.rb +7 -22
  56. data/lib/prawn/version.rb +2 -2
  57. data/lib/prawn/view.rb +17 -7
  58. data/manual/basic_concepts/adding_pages.rb +6 -7
  59. data/manual/basic_concepts/basic_concepts.rb +31 -22
  60. data/manual/basic_concepts/creation.rb +10 -11
  61. data/manual/basic_concepts/cursor.rb +4 -5
  62. data/manual/basic_concepts/measurement.rb +7 -8
  63. data/manual/basic_concepts/origin.rb +5 -6
  64. data/manual/basic_concepts/other_cursor_helpers.rb +11 -12
  65. data/manual/basic_concepts/view.rb +22 -16
  66. data/manual/bounding_box/bounding_box.rb +29 -24
  67. data/manual/bounding_box/bounds.rb +11 -12
  68. data/manual/bounding_box/canvas.rb +7 -8
  69. data/manual/bounding_box/creation.rb +6 -7
  70. data/manual/bounding_box/indentation.rb +14 -15
  71. data/manual/bounding_box/nesting.rb +25 -18
  72. data/manual/bounding_box/russian_boxes.rb +14 -13
  73. data/manual/bounding_box/stretchy.rb +12 -13
  74. data/manual/contents.rb +28 -22
  75. data/manual/cover.rb +33 -28
  76. data/manual/document_and_page_options/background.rb +15 -13
  77. data/manual/document_and_page_options/document_and_page_options.rb +25 -20
  78. data/manual/document_and_page_options/metadata.rb +18 -16
  79. data/manual/document_and_page_options/page_margins.rb +18 -20
  80. data/manual/document_and_page_options/page_size.rb +13 -12
  81. data/manual/document_and_page_options/print_scaling.rb +18 -15
  82. data/manual/example_helper.rb +5 -4
  83. data/manual/graphics/blend_mode.rb +12 -9
  84. data/manual/graphics/circle_and_ellipse.rb +4 -5
  85. data/manual/graphics/color.rb +7 -9
  86. data/manual/graphics/common_lines.rb +7 -8
  87. data/manual/graphics/fill_and_stroke.rb +5 -6
  88. data/manual/graphics/fill_rules.rb +10 -10
  89. data/manual/graphics/gradients.rb +27 -21
  90. data/manual/graphics/graphics.rb +48 -40
  91. data/manual/graphics/helper.rb +19 -9
  92. data/manual/graphics/line_width.rb +8 -7
  93. data/manual/graphics/lines_and_curves.rb +7 -8
  94. data/manual/graphics/polygon.rb +6 -8
  95. data/manual/graphics/rectangle.rb +4 -5
  96. data/manual/graphics/rotate.rb +6 -7
  97. data/manual/graphics/scale.rb +14 -15
  98. data/manual/graphics/soft_masks.rb +3 -4
  99. data/manual/graphics/stroke_cap.rb +6 -7
  100. data/manual/graphics/stroke_dash.rb +15 -16
  101. data/manual/graphics/stroke_join.rb +5 -6
  102. data/manual/graphics/translate.rb +10 -10
  103. data/manual/graphics/transparency.rb +7 -8
  104. data/manual/how_to_read_this_manual.rb +6 -6
  105. data/manual/images/absolute_position.rb +6 -7
  106. data/manual/images/fit.rb +7 -8
  107. data/manual/images/horizontal.rb +10 -11
  108. data/manual/images/images.rb +28 -24
  109. data/manual/images/plain_image.rb +5 -6
  110. data/manual/images/scale.rb +9 -10
  111. data/manual/images/vertical.rb +16 -14
  112. data/manual/images/width_and_height.rb +10 -11
  113. data/manual/layout/boxes.rb +5 -6
  114. data/manual/layout/content.rb +7 -8
  115. data/manual/layout/layout.rb +18 -16
  116. data/manual/layout/simple_grid.rb +6 -7
  117. data/manual/outline/add_subsection_to.rb +20 -21
  118. data/manual/outline/insert_section_after.rb +15 -16
  119. data/manual/outline/outline.rb +21 -17
  120. data/manual/outline/sections_and_pages.rb +17 -18
  121. data/manual/repeatable_content/alternate_page_numbering.rb +21 -17
  122. data/manual/repeatable_content/page_numbering.rb +17 -16
  123. data/manual/repeatable_content/repeatable_content.rb +25 -19
  124. data/manual/repeatable_content/repeater.rb +14 -15
  125. data/manual/repeatable_content/stamp.rb +14 -15
  126. data/manual/security/encryption.rb +9 -10
  127. data/manual/security/permissions.rb +21 -14
  128. data/manual/security/security.rb +19 -16
  129. data/manual/table.rb +3 -3
  130. data/manual/text/alignment.rb +16 -17
  131. data/manual/text/color.rb +12 -11
  132. data/manual/text/column_box.rb +9 -10
  133. data/manual/text/fallback_fonts.rb +25 -21
  134. data/manual/text/font.rb +11 -12
  135. data/manual/text/font_size.rb +13 -14
  136. data/manual/text/font_style.rb +10 -8
  137. data/manual/text/formatted_callbacks.rb +33 -24
  138. data/manual/text/formatted_text.rb +36 -25
  139. data/manual/text/free_flowing_text.rb +22 -23
  140. data/manual/text/inline.rb +18 -19
  141. data/manual/text/kerning_and_character_spacing.rb +14 -15
  142. data/manual/text/leading.rb +7 -8
  143. data/manual/text/line_wrapping.rb +37 -18
  144. data/manual/text/paragraph_indentation.rb +12 -14
  145. data/manual/text/positioned_text.rb +15 -16
  146. data/manual/text/registering_families.rb +20 -21
  147. data/manual/text/rendering_and_color.rb +9 -10
  148. data/manual/text/right_to_left_text.rb +26 -19
  149. data/manual/text/rotation.rb +33 -23
  150. data/manual/text/single_usage.rb +8 -9
  151. data/manual/text/text.rb +57 -52
  152. data/manual/text/text_box_excess.rb +20 -17
  153. data/manual/text/text_box_extensions.rb +18 -15
  154. data/manual/text/text_box_overflow.rb +20 -19
  155. data/manual/text/utf8.rb +11 -12
  156. data/manual/text/win_ansi_charset.rb +27 -25
  157. data/prawn.gemspec +41 -34
  158. data/spec/extensions/encoding_helpers.rb +3 -3
  159. data/spec/prawn/document/bounding_box_spec.rb +550 -0
  160. data/spec/prawn/document/column_box_spec.rb +75 -0
  161. data/spec/prawn/document/security_spec.rb +176 -0
  162. data/spec/prawn/document_annotations_spec.rb +76 -0
  163. data/spec/prawn/document_destinations_spec.rb +15 -0
  164. data/spec/prawn/document_grid_spec.rb +99 -0
  165. data/spec/prawn/document_reference_spec.rb +27 -0
  166. data/spec/prawn/document_span_spec.rb +44 -0
  167. data/spec/prawn/document_spec.rb +805 -0
  168. data/spec/prawn/font_metric_cache_spec.rb +54 -0
  169. data/spec/prawn/font_spec.rb +544 -0
  170. data/spec/prawn/graphics/blend_mode_spec.rb +63 -0
  171. data/spec/prawn/graphics/transparency_spec.rb +81 -0
  172. data/spec/prawn/graphics_spec.rb +872 -0
  173. data/spec/prawn/graphics_stroke_styles_spec.rb +229 -0
  174. data/spec/{image_handler_spec.rb → prawn/image_handler_spec.rb} +14 -14
  175. data/spec/prawn/images/jpg_spec.rb +20 -0
  176. data/spec/prawn/images/png_spec.rb +283 -0
  177. data/spec/prawn/images_spec.rb +229 -0
  178. data/spec/prawn/measurements_extensions_spec.rb +24 -0
  179. data/spec/prawn/outline_spec.rb +512 -0
  180. data/spec/prawn/repeater_spec.rb +166 -0
  181. data/spec/prawn/soft_mask_spec.rb +74 -0
  182. data/spec/prawn/stamp_spec.rb +173 -0
  183. data/spec/prawn/text/box_spec.rb +1110 -0
  184. data/spec/prawn/text/formatted/arranger_spec.rb +466 -0
  185. data/spec/prawn/text/formatted/box_spec.rb +849 -0
  186. data/spec/prawn/text/formatted/fragment_spec.rb +343 -0
  187. data/spec/prawn/text/formatted/line_wrap_spec.rb +495 -0
  188. data/spec/prawn/text/formatted/parser_spec.rb +697 -0
  189. data/spec/prawn/text_draw_text_spec.rb +150 -0
  190. data/spec/prawn/text_rendering_mode_spec.rb +48 -0
  191. data/spec/prawn/text_spacing_spec.rb +95 -0
  192. data/spec/prawn/text_spec.rb +603 -0
  193. data/spec/prawn/text_with_inline_formatting_spec.rb +35 -0
  194. data/spec/{transformation_stack_spec.rb → prawn/transformation_stack_spec.rb} +22 -19
  195. data/spec/prawn/view_spec.rb +63 -0
  196. data/spec/prawn_manual_spec.rb +35 -0
  197. data/spec/spec_helper.rb +18 -19
  198. metadata +102 -222
  199. metadata.gz.sig +0 -0
  200. data/data/images/16bit.alpha +0 -0
  201. data/data/images/16bit.color +0 -0
  202. data/data/images/16bit.png +0 -0
  203. data/data/images/arrow.png +0 -0
  204. data/data/images/arrow2.png +0 -0
  205. data/data/images/blend_modes_bottom_layer.jpg +0 -0
  206. data/data/images/blend_modes_top_layer.jpg +0 -0
  207. data/data/images/dice.alpha +0 -0
  208. data/data/images/dice.color +0 -0
  209. data/data/images/dice.png +0 -0
  210. data/data/images/dice_interlaced.png +0 -0
  211. data/data/images/fractal.jpg +0 -0
  212. data/data/images/indexed_color.dat +0 -0
  213. data/data/images/indexed_color.png +0 -0
  214. data/data/images/indexed_transparency.png +0 -0
  215. data/data/images/indexed_transparency_alpha.dat +0 -0
  216. data/data/images/indexed_transparency_color.dat +0 -0
  217. data/data/images/letterhead.jpg +0 -0
  218. data/data/images/license.md +0 -8
  219. data/data/images/page_white_text.alpha +0 -0
  220. data/data/images/page_white_text.color +0 -0
  221. data/data/images/page_white_text.png +0 -0
  222. data/data/images/pigs.jpg +0 -0
  223. data/data/images/prawn.png +0 -0
  224. data/data/images/ruport.png +0 -0
  225. data/data/images/ruport_data.dat +0 -0
  226. data/data/images/ruport_transparent.png +0 -0
  227. data/data/images/ruport_type0.png +0 -0
  228. data/data/images/stef.jpg +0 -0
  229. data/data/images/tru256.bmp +0 -0
  230. data/data/images/web-links.dat +0 -1
  231. data/data/images/web-links.png +0 -0
  232. data/data/pdfs/complex_template.pdf +0 -0
  233. data/data/pdfs/contains_ttf_font.pdf +0 -0
  234. data/data/pdfs/encrypted.pdf +0 -0
  235. data/data/pdfs/form.pdf +1 -819
  236. data/data/pdfs/hexagon.pdf +0 -61
  237. data/data/pdfs/indirect_reference.pdf +0 -86
  238. data/data/pdfs/multipage_template.pdf +0 -127
  239. data/data/pdfs/nested_pages.pdf +0 -118
  240. data/data/pdfs/page_without_mediabox.pdf +0 -193
  241. data/data/pdfs/resources_as_indirect_object.pdf +0 -83
  242. data/data/pdfs/two_hexagons.pdf +0 -90
  243. data/data/pdfs/version_1_6.pdf +0 -61
  244. data/data/shift_jis_text.txt +0 -1
  245. data/spec/acceptance/png_spec.rb +0 -35
  246. data/spec/annotations_spec.rb +0 -67
  247. data/spec/blend_mode_spec.rb +0 -71
  248. data/spec/bounding_box_spec.rb +0 -501
  249. data/spec/column_box_spec.rb +0 -59
  250. data/spec/destinations_spec.rb +0 -13
  251. data/spec/document_spec.rb +0 -738
  252. data/spec/font_metric_cache_spec.rb +0 -52
  253. data/spec/font_spec.rb +0 -475
  254. data/spec/formatted_text_arranger_spec.rb +0 -452
  255. data/spec/formatted_text_box_spec.rb +0 -716
  256. data/spec/formatted_text_fragment_spec.rb +0 -299
  257. data/spec/graphics_spec.rb +0 -705
  258. data/spec/grid_spec.rb +0 -95
  259. data/spec/images_spec.rb +0 -167
  260. data/spec/inline_formatted_text_parser_spec.rb +0 -568
  261. data/spec/jpg_spec.rb +0 -23
  262. data/spec/line_wrap_spec.rb +0 -366
  263. data/spec/measurement_units_spec.rb +0 -22
  264. data/spec/outline_spec.rb +0 -409
  265. data/spec/png_spec.rb +0 -257
  266. data/spec/reference_spec.rb +0 -25
  267. data/spec/repeater_spec.rb +0 -154
  268. data/spec/security_spec.rb +0 -151
  269. data/spec/soft_mask_spec.rb +0 -78
  270. data/spec/span_spec.rb +0 -43
  271. data/spec/stamp_spec.rb +0 -179
  272. data/spec/stroke_styles_spec.rb +0 -208
  273. data/spec/text_at_spec.rb +0 -142
  274. data/spec/text_box_spec.rb +0 -1042
  275. data/spec/text_rendering_mode_spec.rb +0 -45
  276. data/spec/text_spacing_spec.rb +0 -93
  277. data/spec/text_spec.rb +0 -543
  278. data/spec/text_with_inline_formatting_spec.rb +0 -35
  279. data/spec/transparency_spec.rb +0 -91
  280. data/spec/view_spec.rb +0 -42
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  # Implementation of the "ARCFOUR" algorithm ("alleged RC4 (tm)"). Implemented
4
4
  # as described at:
@@ -39,7 +39,7 @@ class Arcfour
39
39
  end
40
40
 
41
41
  def encrypt(string)
42
- string.unpack('c*').map{ |byte| byte ^ key_byte }.pack('c*')
42
+ string.unpack('c*').map { |byte| byte ^ key_byte }.pack('c*')
43
43
  end
44
44
 
45
45
  private
@@ -1,5 +1,5 @@
1
- # encoding: utf-8
2
- #
1
+ # frozen_string_literal: true
2
+
3
3
  # soft_mask.rb : Implements soft-masking
4
4
  #
5
5
  # Copyright September 2012, Alexander Mankuta. All Rights Reserved.
@@ -31,51 +31,51 @@ module Prawn
31
31
  renderer.min_version(1.4)
32
32
 
33
33
  group_attrs = ref!(
34
- :Type => :Group,
35
- :S => :Transparency,
36
- :CS => :DeviceRGB,
37
- :I => false,
38
- :K => false
34
+ Type: :Group,
35
+ S: :Transparency,
36
+ CS: :DeviceRGB,
37
+ I: false,
38
+ K: false
39
39
  )
40
40
 
41
41
  group = ref!(
42
- :Type => :XObject,
43
- :Subtype => :Form,
44
- :BBox => state.page.dimensions,
45
- :Group => group_attrs
42
+ Type: :XObject,
43
+ Subtype: :Form,
44
+ BBox: state.page.dimensions,
45
+ Group: group_attrs
46
46
  )
47
47
 
48
48
  state.page.stamp_stream(group, &block)
49
49
 
50
50
  mask = ref!(
51
- :Type => :Mask,
52
- :S => :Luminosity,
53
- :G => group
51
+ Type: :Mask,
52
+ S: :Luminosity,
53
+ G: group
54
54
  )
55
55
 
56
56
  g_state = ref!(
57
- :Type => :ExtGState,
58
- :SMask => mask,
57
+ Type: :ExtGState,
58
+ SMask: mask,
59
59
 
60
- :AIS => false,
61
- :BM => :Normal,
62
- :OP => false,
63
- :op => false,
64
- :OPM => 1,
65
- :SA => true
60
+ AIS: false,
61
+ BM: :Normal,
62
+ OP: false,
63
+ op: false,
64
+ OPM: 1,
65
+ SA: true
66
66
  )
67
67
 
68
68
  registry_key = {
69
- :bbox => state.page.dimensions,
70
- :mask => [group.stream.filters.normalized, group.stream.filtered_stream],
71
- :page => state.page_count
69
+ bbox: state.page.dimensions,
70
+ mask: [group.stream.filters.normalized, group.stream.filtered_stream],
71
+ page: state.page_count
72
72
  }.hash
73
73
 
74
74
  if soft_mask_registry[registry_key]
75
75
  renderer.add_content "/#{soft_mask_registry[registry_key]} gs"
76
76
  else
77
77
  masks = page.resources[:ExtGState] ||= {}
78
- id = masks.empty? ? 'GS1' : masks.keys.sort.last.succ
78
+ id = masks.empty? ? 'GS1' : masks.keys.max.succ
79
79
  masks[id] = g_state
80
80
 
81
81
  soft_mask_registry[registry_key] = id
@@ -1,5 +1,5 @@
1
- # encoding: utf-8
2
- #
1
+ # frozen_string_literal: true
2
+
3
3
  # stamp.rb : Implements a repeatable stamp
4
4
  #
5
5
  # Copyright October 2009, Daniel Nelson. All Rights Reserved.
@@ -92,9 +92,9 @@ module Prawn
92
92
  end
93
93
 
94
94
  def stamp_dictionary(name)
95
- fail Prawn::Errors::InvalidName if name.empty?
95
+ raise Prawn::Errors::InvalidName if name.empty?
96
96
  if stamp_dictionary_registry[name].nil?
97
- fail Prawn::Errors::UndefinedObjectName
97
+ raise Prawn::Errors::UndefinedObjectName
98
98
  end
99
99
 
100
100
  dict = stamp_dictionary_registry[name]
@@ -105,20 +105,27 @@ module Prawn
105
105
  end
106
106
 
107
107
  def create_stamp_dictionary(name)
108
- fail Prawn::Errors::InvalidName if name.empty?
109
- fail Prawn::Errors::NameTaken unless stamp_dictionary_registry[name].nil?
108
+ raise Prawn::Errors::InvalidName if name.empty?
109
+ raise Prawn::Errors::NameTaken unless stamp_dictionary_registry[name].nil?
110
+
110
111
  # BBox origin is the lower left margin of the page, so we need
111
112
  # it to be the full dimension of the page, or else things that
112
113
  # should appear near the top or right margin are invisible
113
- dictionary = ref!(:Type => :XObject,
114
- :Subtype => :Form,
115
- :BBox => [0, 0,
116
- state.page.dimensions[2], state.page.dimensions[3]])
114
+ dictionary = ref!(
115
+ Type: :XObject,
116
+ Subtype: :Form,
117
+ BBox: [
118
+ 0, 0,
119
+ state.page.dimensions[2], state.page.dimensions[3]
120
+ ]
121
+ )
117
122
 
118
123
  dictionary_name = "Stamp#{next_stamp_dictionary_id}"
119
124
 
120
- stamp_dictionary_registry[name] = { :stamp_dictionary_name => dictionary_name,
121
- :stamp_dictionary => dictionary }
125
+ stamp_dictionary_registry[name] = {
126
+ stamp_dictionary_name: dictionary_name,
127
+ stamp_dictionary: dictionary
128
+ }
122
129
  dictionary
123
130
  end
124
131
 
@@ -127,7 +134,7 @@ module Prawn
127
134
  # to the /Annot dictionary of the object that contains the
128
135
  # call to the stamp object.
129
136
  def update_annotation_references(annots)
130
- if annots && annots.any?
137
+ if annots&.any?
131
138
  state.page.dictionary.data[:Annots] ||= []
132
139
  state.page.dictionary.data[:Annots] |= annots
133
140
  end
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  # text.rb : Implements PDF text primitives
4
4
  #
@@ -6,12 +6,10 @@
6
6
  #
7
7
  # This is free software. Please see the LICENSE and COPYING files for details.
8
8
 
9
- require "zlib"
9
+ require 'zlib'
10
10
 
11
- require "pdf/core/text"
12
-
13
- require_relative "text/formatted"
14
- require_relative "text/box"
11
+ require_relative 'text/formatted'
12
+ require_relative 'text/box'
15
13
 
16
14
  module Prawn
17
15
  module Text
@@ -19,11 +17,11 @@ module Prawn
19
17
  include Prawn::Text::Formatted
20
18
 
21
19
  # No-Break Space
22
- Prawn::Text::NBSP = " "
20
+ NBSP = "\u00A0"
23
21
  # Zero Width Space (indicate word boundaries without a space)
24
- Prawn::Text::ZWSP = [8203].pack("U")
22
+ ZWSP = "\u200B"
25
23
  # Soft Hyphen (invisible, except when causing a line break)
26
- Prawn::Text::SHY = "­"
24
+ SHY = "\u00AD"
27
25
 
28
26
  # @group Stable API
29
27
 
@@ -139,9 +137,9 @@ module Prawn
139
137
  # each line is included below the last line;
140
138
  # otherwise, document.y is placed just below the
141
139
  # descender of the last line printed [true]
142
- # <tt>:mode</tt>:: The text rendering mode to use. Use this to specify if the
143
- # text should render with the fill color, stroke color or
144
- # both. See the comments to text_rendering_mode() to see
140
+ # <tt>:mode</tt>:: The text rendering mode to use. Use this to specify if
141
+ # the text should render with the fill color, stroke color
142
+ # or both. See the comments to text_rendering_mode() to see
145
143
  # a list of valid options. [0]
146
144
  #
147
145
  # == Exceptions
@@ -153,15 +151,17 @@ module Prawn
153
151
  #
154
152
  def text(string, options = {})
155
153
  return false if string.nil?
154
+
156
155
  # we modify the options. don't change the user's hash
157
156
  options = options.dup
158
157
 
159
- if p = options[:inline_format]
158
+ p = options[:inline_format]
159
+ if p
160
160
  p = [] unless p.is_a?(Array)
161
161
  options.delete(:inline_format)
162
- array = self.text_formatter.format(string, *p)
162
+ array = text_formatter.format(string, *p)
163
163
  else
164
- array = [{ :text => string }]
164
+ array = [{ text: string }]
165
165
  end
166
166
 
167
167
  formatted_text(array, options)
@@ -190,26 +190,27 @@ module Prawn
190
190
  def formatted_text(array, options = {})
191
191
  options = inspect_options_for_text(options.dup)
192
192
 
193
- if color = options.delete(:color)
194
- array = array.map do |fragment|
195
- fragment[:color] ? fragment : fragment.merge(:color => color)
196
- end
193
+ color = options.delete(:color)
194
+ if color
195
+ array =
196
+ array.map do |fragment|
197
+ fragment[:color] ? fragment : fragment.merge(color: color)
198
+ end
197
199
  end
198
200
 
199
201
  if @indent_paragraphs
200
- self.text_formatter.array_paragraphs(array).each do |paragraph|
202
+ text_formatter.array_paragraphs(array).each do |paragraph|
201
203
  remaining_text = draw_indented_formatted_line(paragraph, options)
202
204
 
203
- if @no_text_printed
204
- # unless this paragraph was an empty line
205
- unless @all_text_printed
206
- @bounding_box.move_past_bottom
207
- remaining_text = draw_indented_formatted_line(paragraph, options)
208
- end
205
+ if @no_text_printed && !@all_text_printed
206
+ @bounding_box.move_past_bottom
207
+ remaining_text = draw_indented_formatted_line(paragraph, options)
209
208
  end
210
209
 
211
- remaining_text = fill_formatted_text_box(remaining_text, options)
212
- draw_remaining_formatted_text_on_new_pages(remaining_text, options)
210
+ unless @all_text_printed
211
+ remaining_text = fill_formatted_text_box(remaining_text, options)
212
+ draw_remaining_formatted_text_on_new_pages(remaining_text, options)
213
+ end
213
214
  end
214
215
  else
215
216
  remaining_text = fill_formatted_text_box(array, options)
@@ -236,7 +237,8 @@ module Prawn
236
237
  # == Rotation
237
238
  #
238
239
  # Text can be rotated before it is placed on the canvas by specifying the
239
- # <tt>:rotate</tt> option with a given angle. Rotation occurs counter-clockwise.
240
+ # <tt>:rotate</tt> option with a given angle. Rotation occurs
241
+ # counter-clockwise.
240
242
  #
241
243
  # == Encoding
242
244
  #
@@ -253,7 +255,8 @@ module Prawn
253
255
  #
254
256
  # == Options (default values marked in [])
255
257
  #
256
- # <tt>:at</tt>:: <tt>[x, y]</tt>(required). The position at which to start the text
258
+ # <tt>:at</tt>:: <tt>[x, y]</tt>(required). The position at which to start
259
+ # the text
257
260
  # <tt>:kerning</tt>:: <tt>boolean</tt>. Whether or not to use kerning (if it
258
261
  # is available with the current font)
259
262
  # [value of default_kerning?]
@@ -277,7 +280,7 @@ module Prawn
277
280
  text = text.to_s.dup
278
281
  save_font do
279
282
  process_text_options(options)
280
- font.normalize_encoding!(text)
283
+ text = font.normalize_encoding(text)
281
284
  font_size(options[:size]) { draw_text!(text, options) }
282
285
  end
283
286
  end
@@ -289,9 +292,9 @@ module Prawn
289
292
  unless font.unicode? || font.class.hide_m17n_warning || text.ascii_only?
290
293
  warn "PDF's built-in fonts have very limited support for " \
291
294
  "internationalized text.\nIf you need full UTF-8 support, " \
292
- "consider using a TTF font instead.\n\nTo disable this " \
295
+ "consider using an external font instead.\n\nTo disable this " \
293
296
  "warning, add the following line to your code:\n" \
294
- "Prawn::Font::AFM.hide_m17n_warning = true\n"
297
+ "Prawn::Fonts::AFM.hide_m17n_warning = true\n"
295
298
 
296
299
  font.class.hide_m17n_warning = true
297
300
  end
@@ -317,7 +320,7 @@ module Prawn
317
320
  # any text
318
321
  #
319
322
  def height_of(string, options = {})
320
- height_of_formatted([{ :text => string }], options)
323
+ height_of_formatted([{ text: string }], options)
321
324
  end
322
325
 
323
326
  # Gets height of formatted text in PDF points.
@@ -332,15 +335,15 @@ module Prawn
332
335
  #
333
336
  def height_of_formatted(array, options = {})
334
337
  if options[:indent_paragraphs]
335
- fail NotImplementedError, ":indent_paragraphs option not available" \
336
- "with height_of"
338
+ raise NotImplementedError,
339
+ ':indent_paragraphs option not available with height_of'
337
340
  end
338
341
  process_final_gap_option(options)
339
342
  box = Text::Formatted::Box.new(
340
343
  array,
341
- options.merge(:height => 100000000, :document => self)
344
+ options.merge(height: 100_000_000, document: self)
342
345
  )
343
- box.render(:dry_run => true)
346
+ box.render(dry_run: true)
344
347
 
345
348
  height = box.height
346
349
  height += box.line_gap + box.leading if @final_gap
@@ -350,7 +353,7 @@ module Prawn
350
353
  private
351
354
 
352
355
  def draw_remaining_formatted_text_on_new_pages(remaining_text, options)
353
- while remaining_text.length > 0
356
+ until remaining_text.empty?
354
357
  @bounding_box.move_past_bottom
355
358
  previous_remaining_text = remaining_text
356
359
  remaining_text = fill_formatted_text_box(remaining_text, options)
@@ -359,14 +362,15 @@ module Prawn
359
362
  end
360
363
 
361
364
  def draw_indented_formatted_line(string, options)
362
- if options.fetch(:direction, text_direction) == :ltr
363
- gap = [@indent_paragraphs, 0]
364
- else
365
- gap = [0, @indent_paragraphs]
366
- end
365
+ gap =
366
+ if options.fetch(:direction, text_direction) == :ltr
367
+ [@indent_paragraphs, 0]
368
+ else
369
+ [0, @indent_paragraphs]
370
+ end
367
371
 
368
372
  indent(*gap) do
369
- fill_formatted_text_box(string, options.dup.merge(:single_line => true))
373
+ fill_formatted_text_box(string, options.dup.merge(single_line: true))
370
374
  end
371
375
  end
372
376
 
@@ -384,33 +388,41 @@ module Prawn
384
388
  end
385
389
 
386
390
  def merge_text_box_positioning_options(options)
387
- bottom = @bounding_box.stretchy? ? @margin_box.absolute_bottom :
388
- @bounding_box.absolute_bottom
391
+ bottom =
392
+ if @bounding_box.stretchy?
393
+ @margin_box.absolute_bottom
394
+ else
395
+ @bounding_box.absolute_bottom
396
+ end
389
397
 
390
398
  options[:height] = y - bottom
391
399
  options[:width] = bounds.width
392
- options[:at] = [@bounding_box.left_side - @bounding_box.absolute_left,
393
- y - @bounding_box.absolute_bottom]
400
+ options[:at] = [
401
+ @bounding_box.left_side - @bounding_box.absolute_left,
402
+ y - @bounding_box.absolute_bottom
403
+ ]
394
404
  end
395
405
 
396
406
  def inspect_options_for_draw_text(options)
397
407
  if options[:at].nil?
398
- fail ArgumentError, "The :at option is required for draw_text"
408
+ raise ArgumentError, 'The :at option is required for draw_text'
399
409
  elsif options[:align]
400
- fail ArgumentError, "The :align option does not work with draw_text"
410
+ raise ArgumentError, 'The :align option does not work with draw_text'
401
411
  end
402
- if options[:kerning].nil? then
412
+
413
+ if options[:kerning].nil?
403
414
  options[:kerning] = default_kerning?
404
415
  end
405
- valid_options = PDF::Core::Text::VALID_OPTIONS + [:at, :rotate]
416
+ valid_options = PDF::Core::Text::VALID_OPTIONS + %i[at rotate]
406
417
  Prawn.verify_options(valid_options, options)
407
418
  options
408
419
  end
409
420
 
410
421
  def inspect_options_for_text(options)
411
422
  if options[:at]
412
- fail ArgumentError, ":at is no longer a valid option with text." \
413
- "use draw_text or text_box instead"
423
+ raise ArgumentError,
424
+ ':at is no longer a valid option with text.' \
425
+ 'use draw_text or text_box instead'
414
426
  end
415
427
  process_final_gap_option(options)
416
428
  process_indent_paragraphs_option(options)
@@ -428,13 +440,17 @@ module Prawn
428
440
  options.delete(:indent_paragraphs)
429
441
  end
430
442
 
431
- def move_text_position(dy)
432
- bottom = @bounding_box.stretchy? ? @margin_box.absolute_bottom :
433
- @bounding_box.absolute_bottom
443
+ def move_text_position(amount)
444
+ bottom =
445
+ if @bounding_box.stretchy?
446
+ @margin_box.absolute_bottom
447
+ else
448
+ @bounding_box.absolute_bottom
449
+ end
434
450
 
435
- @bounding_box.move_past_bottom if (y - dy) < bottom
451
+ @bounding_box.move_past_bottom if (y - amount) < bottom
436
452
 
437
- self.y -= dy
453
+ self.y -= amount
438
454
  end
439
455
  end
440
456
  end
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  # text/rectangle.rb : Implements text boxes
4
4
  #
@@ -7,7 +7,7 @@
7
7
  # This is free software. Please see the LICENSE and COPYING files for details.
8
8
  #
9
9
 
10
- require_relative "formatted/box"
10
+ require_relative 'formatted/box'
11
11
 
12
12
  module Prawn
13
13
  module Text
@@ -52,7 +52,8 @@ module Prawn
52
52
  # <tt>[x, y]</tt>. The upper left corner of the box
53
53
  # [@document.bounds.left, @document.bounds.top]
54
54
  # <tt>:width</tt>::
55
- # <tt>number</tt>. The width of the box [@document.bounds.right - @at[0]]
55
+ # <tt>number</tt>. The width of the box
56
+ # [@document.bounds.right - @at[0]]
56
57
  # <tt>:height</tt>::
57
58
  # <tt>number</tt>. The height of the box [default_height()]
58
59
  # <tt>:direction</tt>::
@@ -60,7 +61,7 @@ module Prawn
60
61
  # or right-to-left) [value of document.text_direction]
61
62
  # <tt>:fallback_fonts</tt>::
62
63
  # An array of font names. Each name must be the name of an AFM font or
63
- # the name that was used to register a family of TTF fonts (see
64
+ # the name that was used to register a family of external fonts (see
64
65
  # Prawn::Document#font_families). If present, then each glyph will be
65
66
  # rendered using the first font that includes the glyph, starting with
66
67
  # the current font and then moving through :fallback_fonts from
@@ -82,7 +83,8 @@ module Prawn
82
83
  # <tt>number</tt>. Additional space between lines [value of
83
84
  # document.default_leading]
84
85
  # <tt>:single_line</tt>::
85
- # <tt>boolean</tt>. If true, then only the first line will be drawn [false]
86
+ # <tt>boolean</tt>. If true, then only the first line will be drawn
87
+ # [false]
86
88
  # <tt>:overflow</tt>::
87
89
  # <tt>:truncate</tt>, <tt>:shrink_to_fit</tt>, or <tt>:expand</tt>
88
90
  # This controls the behavior when the amount of text
@@ -105,13 +107,15 @@ module Prawn
105
107
  options = options.dup
106
108
  options[:document] = self
107
109
 
108
- box = if p = options.delete(:inline_format)
109
- p = [] unless p.is_a?(Array)
110
- array = self.text_formatter.format(string, *p)
111
- Text::Formatted::Box.new(array, options)
112
- else
113
- Text::Box.new(string, options)
114
- end
110
+ box =
111
+ if options[:inline_format]
112
+ p = options.delete(:inline_format)
113
+ p = [] unless p.is_a?(Array)
114
+ array = text_formatter.format(string, *p)
115
+ Text::Formatted::Box.new(array, options)
116
+ else
117
+ Text::Box.new(string, options)
118
+ end
115
119
 
116
120
  box.render
117
121
  end
@@ -126,12 +130,12 @@ module Prawn
126
130
  #
127
131
  class Box < Prawn::Text::Formatted::Box
128
132
  def initialize(string, options = {})
129
- super([{ :text => string }], options)
133
+ super([{ text: string }], options)
130
134
  end
131
135
 
132
136
  def render(flags = {})
133
137
  leftover = super(flags)
134
- leftover.collect { |hash| hash[:text] }.join
138
+ leftover.map { |hash| hash[:text] }.join
135
139
  end
136
140
  end
137
141
  end