prawn 2.2.2 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) 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 +2 -0
  6. data/Rakefile +10 -3
  7. data/lib/prawn.rb +9 -3
  8. data/lib/prawn/document.rb +40 -18
  9. data/lib/prawn/document/bounding_box.rb +9 -4
  10. data/lib/prawn/document/column_box.rb +2 -0
  11. data/lib/prawn/document/internals.rb +6 -4
  12. data/lib/prawn/document/span.rb +2 -0
  13. data/lib/prawn/encoding.rb +4 -1
  14. data/lib/prawn/errors.rb +2 -0
  15. data/lib/prawn/font.rb +34 -17
  16. data/lib/prawn/font_metric_cache.rb +8 -3
  17. data/lib/prawn/{font → fonts}/afm.rb +14 -7
  18. data/lib/prawn/{font → fonts}/dfont.rb +3 -1
  19. data/lib/prawn/fonts/otf.rb +11 -0
  20. data/lib/prawn/{font → fonts}/ttc.rb +3 -1
  21. data/lib/prawn/{font → fonts}/ttf.rb +23 -12
  22. data/lib/prawn/graphics.rb +24 -26
  23. data/lib/prawn/graphics/blend_mode.rb +2 -0
  24. data/lib/prawn/graphics/cap_style.rb +2 -0
  25. data/lib/prawn/graphics/color.rb +8 -3
  26. data/lib/prawn/graphics/dash.rb +3 -1
  27. data/lib/prawn/graphics/join_style.rb +2 -0
  28. data/lib/prawn/graphics/patterns.rb +7 -2
  29. data/lib/prawn/graphics/transformation.rb +5 -1
  30. data/lib/prawn/graphics/transparency.rb +2 -0
  31. data/lib/prawn/grid.rb +39 -38
  32. data/lib/prawn/image_handler.rb +2 -0
  33. data/lib/prawn/images.rb +21 -19
  34. data/lib/prawn/images/image.rb +2 -0
  35. data/lib/prawn/images/jpg.rb +2 -0
  36. data/lib/prawn/images/png.rb +9 -9
  37. data/lib/prawn/measurement_extensions.rb +2 -0
  38. data/lib/prawn/measurements.rb +5 -0
  39. data/lib/prawn/outline.rb +2 -0
  40. data/lib/prawn/repeater.rb +2 -0
  41. data/lib/prawn/security.rb +26 -24
  42. data/lib/prawn/security/arcfour.rb +2 -0
  43. data/lib/prawn/soft_mask.rb +3 -1
  44. data/lib/prawn/stamp.rb +4 -1
  45. data/lib/prawn/text.rb +14 -12
  46. data/lib/prawn/text/box.rb +3 -1
  47. data/lib/prawn/text/formatted.rb +2 -0
  48. data/lib/prawn/text/formatted/arranger.rb +12 -12
  49. data/lib/prawn/text/formatted/box.rb +23 -13
  50. data/lib/prawn/text/formatted/fragment.rb +2 -0
  51. data/lib/prawn/text/formatted/line_wrap.rb +10 -8
  52. data/lib/prawn/text/formatted/parser.rb +8 -10
  53. data/lib/prawn/text/formatted/wrap.rb +4 -2
  54. data/lib/prawn/transformation_stack.rb +5 -2
  55. data/lib/prawn/utilities.rb +2 -2
  56. data/lib/prawn/version.rb +3 -1
  57. data/lib/prawn/view.rb +15 -7
  58. data/manual/basic_concepts/adding_pages.rb +2 -0
  59. data/manual/basic_concepts/basic_concepts.rb +4 -2
  60. data/manual/basic_concepts/creation.rb +2 -0
  61. data/manual/basic_concepts/cursor.rb +2 -0
  62. data/manual/basic_concepts/measurement.rb +3 -1
  63. data/manual/basic_concepts/origin.rb +2 -0
  64. data/manual/basic_concepts/other_cursor_helpers.rb +2 -0
  65. data/manual/basic_concepts/view.rb +2 -0
  66. data/manual/bounding_box/bounding_box.rb +4 -2
  67. data/manual/bounding_box/bounds.rb +6 -4
  68. data/manual/bounding_box/canvas.rb +2 -0
  69. data/manual/bounding_box/creation.rb +2 -0
  70. data/manual/bounding_box/indentation.rb +2 -0
  71. data/manual/bounding_box/nesting.rb +2 -0
  72. data/manual/bounding_box/russian_boxes.rb +6 -4
  73. data/manual/bounding_box/stretchy.rb +2 -0
  74. data/manual/contents.rb +2 -0
  75. data/manual/cover.rb +10 -7
  76. data/manual/document_and_page_options/background.rb +2 -0
  77. data/manual/document_and_page_options/document_and_page_options.rb +4 -2
  78. data/manual/document_and_page_options/metadata.rb +2 -0
  79. data/manual/document_and_page_options/page_margins.rb +2 -0
  80. data/manual/document_and_page_options/page_size.rb +2 -0
  81. data/manual/document_and_page_options/print_scaling.rb +2 -0
  82. data/manual/example_helper.rb +5 -2
  83. data/manual/graphics/blend_mode.rb +6 -4
  84. data/manual/graphics/circle_and_ellipse.rb +2 -0
  85. data/manual/graphics/color.rb +2 -0
  86. data/manual/graphics/common_lines.rb +2 -0
  87. data/manual/graphics/fill_and_stroke.rb +2 -0
  88. data/manual/graphics/fill_rules.rb +2 -0
  89. data/manual/graphics/gradients.rb +2 -0
  90. data/manual/graphics/graphics.rb +4 -2
  91. data/manual/graphics/helper.rb +2 -0
  92. data/manual/graphics/line_width.rb +3 -0
  93. data/manual/graphics/lines_and_curves.rb +2 -0
  94. data/manual/graphics/polygon.rb +2 -0
  95. data/manual/graphics/rectangle.rb +2 -0
  96. data/manual/graphics/rotate.rb +2 -0
  97. data/manual/graphics/scale.rb +2 -0
  98. data/manual/graphics/soft_masks.rb +2 -0
  99. data/manual/graphics/stroke_cap.rb +3 -1
  100. data/manual/graphics/stroke_dash.rb +2 -0
  101. data/manual/graphics/stroke_join.rb +3 -1
  102. data/manual/graphics/translate.rb +2 -0
  103. data/manual/graphics/transparency.rb +2 -0
  104. data/manual/how_to_read_this_manual.rb +2 -0
  105. data/manual/images/absolute_position.rb +2 -0
  106. data/manual/images/fit.rb +2 -0
  107. data/manual/images/horizontal.rb +3 -1
  108. data/manual/images/images.rb +4 -2
  109. data/manual/images/plain_image.rb +2 -0
  110. data/manual/images/scale.rb +2 -0
  111. data/manual/images/vertical.rb +3 -1
  112. data/manual/images/width_and_height.rb +2 -0
  113. data/manual/layout/boxes.rb +2 -0
  114. data/manual/layout/content.rb +2 -0
  115. data/manual/layout/layout.rb +4 -2
  116. data/manual/layout/simple_grid.rb +2 -0
  117. data/manual/outline/add_subsection_to.rb +2 -0
  118. data/manual/outline/insert_section_after.rb +2 -0
  119. data/manual/outline/outline.rb +4 -2
  120. data/manual/outline/sections_and_pages.rb +2 -0
  121. data/manual/repeatable_content/alternate_page_numbering.rb +2 -0
  122. data/manual/repeatable_content/page_numbering.rb +2 -0
  123. data/manual/repeatable_content/repeatable_content.rb +4 -2
  124. data/manual/repeatable_content/repeater.rb +2 -0
  125. data/manual/repeatable_content/stamp.rb +2 -0
  126. data/manual/security/encryption.rb +2 -0
  127. data/manual/security/permissions.rb +2 -0
  128. data/manual/security/security.rb +4 -2
  129. data/manual/table.rb +2 -0
  130. data/manual/text/alignment.rb +2 -0
  131. data/manual/text/color.rb +2 -0
  132. data/manual/text/column_box.rb +4 -2
  133. data/manual/text/fallback_fonts.rb +4 -2
  134. data/manual/text/font.rb +2 -0
  135. data/manual/text/font_size.rb +2 -0
  136. data/manual/text/font_style.rb +4 -2
  137. data/manual/text/formatted_callbacks.rb +2 -0
  138. data/manual/text/formatted_text.rb +3 -1
  139. data/manual/text/free_flowing_text.rb +2 -0
  140. data/manual/text/inline.rb +2 -0
  141. data/manual/text/kerning_and_character_spacing.rb +2 -0
  142. data/manual/text/leading.rb +2 -0
  143. data/manual/text/line_wrapping.rb +4 -1
  144. data/manual/text/paragraph_indentation.rb +2 -0
  145. data/manual/text/positioned_text.rb +2 -0
  146. data/manual/text/registering_families.rb +4 -2
  147. data/manual/text/rendering_and_color.rb +2 -0
  148. data/manual/text/right_to_left_text.rb +2 -0
  149. data/manual/text/rotation.rb +3 -1
  150. data/manual/text/single_usage.rb +2 -0
  151. data/manual/text/text.rb +4 -3
  152. data/manual/text/text_box_excess.rb +2 -0
  153. data/manual/text/text_box_extensions.rb +2 -0
  154. data/manual/text/text_box_overflow.rb +3 -1
  155. data/manual/text/utf8.rb +3 -1
  156. data/manual/text/win_ansi_charset.rb +6 -3
  157. data/prawn.gemspec +14 -11
  158. data/spec/extensions/encoding_helpers.rb +4 -2
  159. data/spec/prawn/document/bounding_box_spec.rb +8 -8
  160. data/spec/prawn/document/column_box_spec.rb +2 -0
  161. data/spec/prawn/document/security_spec.rb +12 -9
  162. data/spec/prawn/document_annotations_spec.rb +2 -0
  163. data/spec/prawn/document_destinations_spec.rb +2 -0
  164. data/spec/prawn/document_grid_spec.rb +3 -0
  165. data/spec/prawn/document_reference_spec.rb +2 -0
  166. data/spec/prawn/document_span_spec.rb +2 -0
  167. data/spec/prawn/document_spec.rb +59 -14
  168. data/spec/prawn/font_metric_cache_spec.rb +2 -0
  169. data/spec/prawn/font_spec.rb +45 -16
  170. data/spec/prawn/graphics/blend_mode_spec.rb +4 -2
  171. data/spec/prawn/graphics/transparency_spec.rb +2 -0
  172. data/spec/prawn/graphics_spec.rb +40 -20
  173. data/spec/prawn/graphics_stroke_styles_spec.rb +2 -0
  174. data/spec/prawn/image_handler_spec.rb +2 -0
  175. data/spec/prawn/images/jpg_spec.rb +2 -0
  176. data/spec/prawn/images/png_spec.rb +2 -0
  177. data/spec/prawn/images_spec.rb +56 -2
  178. data/spec/prawn/measurements_extensions_spec.rb +2 -0
  179. data/spec/prawn/outline_spec.rb +6 -2
  180. data/spec/prawn/repeater_spec.rb +6 -4
  181. data/spec/prawn/soft_mask_spec.rb +2 -0
  182. data/spec/prawn/stamp_spec.rb +11 -7
  183. data/spec/prawn/text/box_spec.rb +26 -27
  184. data/spec/prawn/text/formatted/arranger_spec.rb +30 -28
  185. data/spec/prawn/text/formatted/box_spec.rb +78 -57
  186. data/spec/prawn/text/formatted/fragment_spec.rb +10 -8
  187. data/spec/prawn/text/formatted/line_wrap_spec.rb +8 -5
  188. data/spec/prawn/text/formatted/parser_spec.rb +32 -2
  189. data/spec/prawn/text_draw_text_spec.rb +5 -3
  190. data/spec/prawn/text_rendering_mode_spec.rb +7 -1
  191. data/spec/prawn/text_spacing_spec.rb +6 -4
  192. data/spec/prawn/text_spec.rb +9 -7
  193. data/spec/prawn/text_with_inline_formatting_spec.rb +3 -1
  194. data/spec/prawn/transformation_stack_spec.rb +2 -0
  195. data/spec/prawn/view_spec.rb +18 -0
  196. data/spec/{manual_spec.rb → prawn_manual_spec.rb} +6 -4
  197. data/spec/spec_helper.rb +9 -10
  198. metadata +86 -73
  199. metadata.gz.sig +4 -2
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # transformation.rb: Implements rotate, translate, skew, scale and a generic
2
4
  # transformation_matrix
3
5
  #
@@ -47,6 +49,7 @@ module Prawn
47
49
  transformation_matrix(cos, sin, -sin, cos, 0, 0, &block)
48
50
  else
49
51
  raise Prawn::Errors::BlockRequired unless block_given?
52
+
50
53
  x = options[:origin][0] + bounds.absolute_left
51
54
  y = options[:origin][1] + bounds.absolute_bottom
52
55
  x_prime = x * cos - y * sin
@@ -116,6 +119,7 @@ module Prawn
116
119
  transformation_matrix(factor, 0, 0, factor, 0, 0, &block)
117
120
  else
118
121
  raise Prawn::Errors::BlockRequired unless block_given?
122
+
119
123
  x = options[:origin][0] + bounds.absolute_left
120
124
  y = options[:origin][1] + bounds.absolute_bottom
121
125
  x_prime = factor * x
@@ -145,7 +149,7 @@ module Prawn
145
149
  raise ArgumentError,
146
150
  'Transformation matrix must have exacty 6 elements'
147
151
  end
148
- values = matrix.map { |x| format '%.5f', x }.join(' ')
152
+ values = matrix.map { |x| x.to_f.round(5) }.join(' ')
149
153
  save_graphics_state if block_given?
150
154
 
151
155
  add_to_transformation_stack(*matrix)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # transparency.rb : Implements transparency
2
4
  #
3
5
  # Copyright October 2009, Daniel Nelson. All Rights Reserved.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # grid.rb: Provides a basic grid layout system for Prawn
2
4
  #
3
5
  # Contributed by Andrew O'Brien in March 2009
@@ -31,22 +33,21 @@ module Prawn
31
33
  #
32
34
  def grid(*args)
33
35
  @boxes ||= {}
34
- return @boxes[args] if @boxes[args]
35
-
36
- if args.empty?
37
- @boxes[args] = @grid
38
- else
39
- g1, g2 = args
40
-
41
- if g1.is_a?(Array) && g2.is_a?(Array) && g1.length == 2 &&
42
- g2.length == 2
43
- @boxes[args] = multi_box(single_box(*g1), single_box(*g2))
44
- else
45
- @boxes[args] = single_box(g1, g2)
36
+ @boxes[args] ||=
37
+ begin
38
+ if args.empty?
39
+ @grid
40
+ else
41
+ g1, g2 = args
42
+
43
+ if g1.is_a?(Array) && g2.is_a?(Array) &&
44
+ g1.length == 2 && g2.length == 2
45
+ multi_box(single_box(*g1), single_box(*g2))
46
+ else
47
+ single_box(g1, g2)
48
+ end
49
+ end
46
50
  end
47
- end
48
-
49
- @boxes[args]
50
51
  end
51
52
 
52
53
  # A Grid represents the entire grid system of a Page and calculates
@@ -56,7 +57,7 @@ module Prawn
56
57
  class Grid
57
58
  attr_reader :pdf, :columns, :rows, :gutter, :row_gutter, :column_gutter
58
59
  def initialize(pdf, options = {}) # :nodoc:
59
- valid_options = [:columns, :rows, :gutter, :row_gutter, :column_gutter]
60
+ valid_options = %i[columns rows gutter row_gutter column_gutter]
60
61
  Prawn.verify_options valid_options, options
61
62
 
62
63
  @pdf = pdf
@@ -77,9 +78,9 @@ module Prawn
77
78
 
78
79
  # Diagnostic tool to show all of the grids. Defaults to gray.
79
80
  def show_all(color = 'CCCCCC')
80
- rows.times do |i|
81
- columns.times do |j|
82
- pdf.grid(i, j).show(color)
81
+ rows.times do |row|
82
+ columns.times do |column|
83
+ pdf.grid(row, column).show(color)
83
84
  end
84
85
  end
85
86
  end
@@ -111,17 +112,17 @@ module Prawn
111
112
  class GridBox
112
113
  attr_reader :pdf
113
114
 
114
- def initialize(pdf, i, j)
115
+ def initialize(pdf, rows, columns)
115
116
  @pdf = pdf
116
- @i = i
117
- @j = j
117
+ @rows = rows
118
+ @columns = columns
118
119
  end
119
120
 
120
121
  # Mostly diagnostic method that outputs the name of a box as
121
122
  # col_num, row_num
122
123
  #
123
124
  def name
124
- "#{@i},#{@j}"
125
+ "#{@rows},#{@columns}"
125
126
  end
126
127
 
127
128
  # :nodoc
@@ -146,7 +147,7 @@ module Prawn
146
147
 
147
148
  # x-coordinate of left side
148
149
  def left
149
- @left ||= (width + grid.column_gutter) * @j.to_f
150
+ @left ||= (width + grid.column_gutter) * @columns.to_f
150
151
  end
151
152
 
152
153
  # x-coordinate of right side
@@ -156,7 +157,7 @@ module Prawn
156
157
 
157
158
  # y-coordinate of the top
158
159
  def top
159
- @top ||= total_height - ((height + grid.row_gutter) * @i.to_f)
160
+ @top ||= total_height - ((height + grid.row_gutter) * @rows.to_f)
160
161
  end
161
162
 
162
163
  # y-coordinate of the bottom
@@ -213,17 +214,17 @@ module Prawn
213
214
  #
214
215
  # @group Experimental API
215
216
  class MultiBox < GridBox
216
- def initialize(pdf, b1, b2)
217
+ def initialize(pdf, box1, box2)
217
218
  @pdf = pdf
218
- @bs = [b1, b2]
219
+ @boxes = [box1, box2]
219
220
  end
220
221
 
221
222
  def name
222
- @bs.map(&:name).join(':')
223
+ @boxes.map(&:name).join(':')
223
224
  end
224
225
 
225
226
  def total_height
226
- @bs[0].total_height
227
+ @boxes[0].total_height
227
228
  end
228
229
 
229
230
  def width
@@ -235,7 +236,7 @@ module Prawn
235
236
  end
236
237
 
237
238
  def gutter
238
- @bs[0].gutter
239
+ @boxes[0].gutter
239
240
  end
240
241
 
241
242
  def left
@@ -257,30 +258,30 @@ module Prawn
257
258
  private
258
259
 
259
260
  def left_box
260
- @left_box ||= @bs.min_by(&:left)
261
+ @left_box ||= @boxes.min_by(&:left)
261
262
  end
262
263
 
263
264
  def right_box
264
- @right_box ||= @bs.max_by(&:right)
265
+ @right_box ||= @boxes.max_by(&:right)
265
266
  end
266
267
 
267
268
  def top_box
268
- @top_box ||= @bs.max_by(&:top)
269
+ @top_box ||= @boxes.max_by(&:top)
269
270
  end
270
271
 
271
272
  def bottom_box
272
- @bottom_box ||= @bs.min_by(&:bottom)
273
+ @bottom_box ||= @boxes.min_by(&:bottom)
273
274
  end
274
275
  end
275
276
 
276
277
  private
277
278
 
278
- def single_box(i, j)
279
- GridBox.new(self, i, j)
279
+ def single_box(rows, columns)
280
+ GridBox.new(self, rows, columns)
280
281
  end
281
282
 
282
- def multi_box(b1, b2)
283
- MultiBox.new(self, b1, b2)
283
+ def multi_box(box1, box2)
284
+ MultiBox.new(self, box1, box2)
284
285
  end
285
286
  end
286
287
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # ImageHandler provides a way to register image processors with Prawn
2
4
  #
3
5
  # Contributed by Evan Sharp in November 2013.
@@ -1,4 +1,7 @@
1
1
  # encoding: ASCII-8BIT
2
+
3
+ # frozen_string_literal: true
4
+
2
5
  # images.rb : Implements PDF image embedding
3
6
  #
4
7
  # Copyright April 2008, James Healy, Gregory Brown. All Rights Reserved.
@@ -63,9 +66,9 @@ module Prawn
63
66
  # (See also: Prawn::Images::PNG , Prawn::Images::JPG)
64
67
  #
65
68
  def image(file, options = {})
66
- Prawn.verify_options [
67
- :at, :position, :vposition, :height,
68
- :width, :scale, :fit
69
+ Prawn.verify_options %i[
70
+ at position vposition height
71
+ width scale fit
69
72
  ], options
70
73
 
71
74
  pdf_obj, info = build_image_object(file)
@@ -79,8 +82,7 @@ module Prawn
79
82
  #
80
83
  # @private
81
84
  def build_image_object(file)
82
- io = verify_and_open_image(file)
83
- image_content = io.read
85
+ image_content = verify_and_read_image(file)
84
86
  image_sha1 = Digest::SHA1.hexdigest(image_content)
85
87
 
86
88
  # if this image has already been embedded, just reuse it
@@ -132,7 +134,7 @@ module Prawn
132
134
 
133
135
  private
134
136
 
135
- def verify_and_open_image(io_or_path)
137
+ def verify_and_read_image(io_or_path)
136
138
  # File or IO
137
139
  if io_or_path.respond_to?(:rewind)
138
140
  io = io_or_path
@@ -142,38 +144,38 @@ module Prawn
142
144
  # read the file as binary so the size is calculated correctly
143
145
  # guard binmode because some objects acting io-like don't implement it
144
146
  io.binmode if io.respond_to?(:binmode)
145
- return io
147
+ return io.read
146
148
  end
147
149
  # String or Pathname
148
150
  io_or_path = Pathname.new(io_or_path)
149
151
  raise ArgumentError, "#{io_or_path} not found" unless io_or_path.file?
150
- io = io_or_path.open('rb')
151
- io
152
+
153
+ io_or_path.binread
152
154
  end
153
155
 
154
- def image_position(w, h, options)
156
+ def image_position(width, height, options)
155
157
  options[:position] ||= :left
156
158
 
157
159
  y = case options[:vposition]
158
160
  when :top
159
161
  bounds.absolute_top
160
162
  when :center
161
- bounds.absolute_top - (bounds.height - h) / 2.0
163
+ bounds.absolute_top - (bounds.height - height) / 2.0
162
164
  when :bottom
163
- bounds.absolute_bottom + h
165
+ bounds.absolute_bottom + height
164
166
  when Numeric
165
167
  bounds.absolute_top - options[:vposition]
166
168
  else
167
- determine_y_with_page_flow(h)
169
+ determine_y_with_page_flow(height)
168
170
  end
169
171
 
170
172
  x = case options[:position]
171
173
  when :left
172
174
  bounds.left_side
173
175
  when :center
174
- bounds.left_side + (bounds.width - w) / 2.0
176
+ bounds.left_side + (bounds.width - width) / 2.0
175
177
  when :right
176
- bounds.right_side - w
178
+ bounds.right_side - width
177
179
  when Numeric
178
180
  options[:position] + bounds.left_side
179
181
  end
@@ -181,15 +183,15 @@ module Prawn
181
183
  [x, y]
182
184
  end
183
185
 
184
- def determine_y_with_page_flow(h)
185
- if overruns_page?(h)
186
+ def determine_y_with_page_flow(height)
187
+ if overruns_page?(height)
186
188
  bounds.move_past_bottom
187
189
  end
188
190
  y
189
191
  end
190
192
 
191
- def overruns_page?(h)
192
- (y - h) < reference_bounds.absolute_bottom
193
+ def overruns_page?(height)
194
+ (y - height) < reference_bounds.absolute_bottom
193
195
  end
194
196
 
195
197
  def image_registry
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # image.rb : Base class for image info objects
2
4
  #
3
5
  # Copyright September 2011, Brad Ediger. All rights reserved.
@@ -1,5 +1,7 @@
1
1
  # encoding: ASCII-8BIT
2
2
 
3
+ # frozen_string_literal: true
4
+
3
5
  # jpg.rb : Extracts the data from a JPG that is needed for embedding
4
6
  #
5
7
  # Copyright April 2008, James Healy. All Rights Reserved.
@@ -1,5 +1,7 @@
1
1
  # encoding: ASCII-8BIT
2
2
 
3
+ # frozen_string_literal: true
4
+
3
5
  # png.rb : Extracts the data from a PNG that is needed for embedding
4
6
  #
5
7
  # Based on some similar code in PDF::Writer by Austin Ziegler
@@ -9,8 +11,6 @@
9
11
  # This is free software. Please see the LICENSE and COPYING files for details.
10
12
 
11
13
  require 'stringio'
12
- require 'enumerator'
13
-
14
14
  module Prawn
15
15
  module Images
16
16
  # A convenience class that wraps the logic for extracting the parts
@@ -38,12 +38,12 @@ module Prawn
38
38
 
39
39
  data.read(8) # Skip the default header
40
40
 
41
- @palette = ''
42
- @img_data = ''
41
+ @palette = +''
42
+ @img_data = +''
43
43
  @transparency = {}
44
44
 
45
45
  loop do
46
- chunk_size = data.read(4).unpack('N')[0]
46
+ chunk_size = data.read(4).unpack1('N')
47
47
  section = data.read(4)
48
48
  case section
49
49
  when 'IHDR'
@@ -72,7 +72,7 @@ module Prawn
72
72
  when 0
73
73
  # Greyscale. Corresponding to entries in the PLTE chunk.
74
74
  # Grey is two bytes, range 0 .. (2 ^ bit-depth) - 1
75
- grayval = data.read(chunk_size).unpack('n').first
75
+ grayval = data.read(chunk_size).unpack1('n')
76
76
  @transparency[:grayscale] = grayval
77
77
  when 2
78
78
  # True colour with proper alpha channel.
@@ -97,9 +97,9 @@ module Prawn
97
97
  def colors
98
98
  case color_type
99
99
  when 0, 3, 4
100
- return 1
100
+ 1
101
101
  when 2, 6
102
- return 3
102
+ 3
103
103
  end
104
104
  end
105
105
 
@@ -321,7 +321,7 @@ module Prawn
321
321
  alpha.putc filter
322
322
 
323
323
  width.times do
324
- color = data.read(1).unpack('C').first
324
+ color = data.read(1).unpack1('C')
325
325
  alpha.putc alpha_palette[color]
326
326
  end
327
327
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # measurement_extensions.rb: Core extensions for Prawn::Measurements
2
4
  #
3
5
  # Copyright December 2008, Florian Witteler. All Rights Reserved.
@@ -1,7 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # measurements.rb: Conversions from other measurements to PDF points
2
4
  #
3
5
  # Copyright December 2008, Florian Witteler. All Rights Reserved.
4
6
  #
7
+
8
+ # rubocop: disable Naming/MethodParameterName
5
9
  module Prawn
6
10
  # @group Stable API
7
11
 
@@ -67,3 +71,4 @@ module Prawn
67
71
  end
68
72
  end
69
73
  end
74
+ # rubocop: enable Naming/MethodParameterName
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Prawn
2
4
  class Document
3
5
  # @group Stable API
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # repeater.rb : Implements repeated page elements.
2
4
  # Heavy inspired by repeating_element() in PDF::Wrapper
3
5
  # http://pdf-wrapper.rubyforge.org/
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # encryption.rb : Implements encrypted PDF and access permissions.
2
4
  #
3
5
  # Copyright August 2008, Brad Ediger. All Rights Reserved.
@@ -6,8 +8,6 @@
6
8
 
7
9
  require 'digest/md5'
8
10
 
9
- require 'pdf/core/byte_string'
10
-
11
11
  require_relative 'security/arcfour'
12
12
 
13
13
  module Prawn
@@ -86,7 +86,7 @@ module Prawn
86
86
  # PDF format.
87
87
  #
88
88
  def encrypt_document(options = {})
89
- Prawn.verify_options [:user_password, :owner_password, :permissions],
89
+ Prawn.verify_options %i[user_password owner_password permissions],
90
90
  options
91
91
  @user_password = options.delete(:user_password) || ''
92
92
 
@@ -133,28 +133,30 @@ module Prawn
133
133
  end
134
134
 
135
135
  # Flags in the permissions word, numbered as LSB = 1
136
- PermissionsBits = {
136
+ PERMISSIONS_BITS = {
137
137
  print_document: 3,
138
138
  modify_contents: 4,
139
139
  copy_contents: 5,
140
140
  modify_annotations: 6
141
141
  }.freeze
142
+ private_constant :PERMISSIONS_BITS
142
143
 
143
144
  FULL_PERMISSIONS = 0b1111_1111_1111_1111_1111_1111_1111_1111
145
+ private_constant :FULL_PERMISSIONS
144
146
 
145
147
  def permissions=(perms = {})
146
148
  @permissions ||= FULL_PERMISSIONS
147
149
  perms.each do |key, value|
148
- unless PermissionsBits[key]
150
+ unless PERMISSIONS_BITS[key]
149
151
  raise(
150
152
  ArgumentError,
151
153
  "Unknown permission :#{key}. Valid options: " +
152
- PermissionsBits.keys.map(&:inspect).join(', ')
154
+ PERMISSIONS_BITS.keys.map(&:inspect).join(', ')
153
155
  )
154
156
  end
155
157
 
156
158
  # 0-based bit number, from LSB
157
- bit_position = PermissionsBits[key] - 1
159
+ bit_position = PERMISSIONS_BITS[key] - 1
158
160
 
159
161
  if value # set bit
160
162
  @permissions |= (1 << bit_position)
@@ -168,14 +170,14 @@ module Prawn
168
170
  @permissions || FULL_PERMISSIONS
169
171
  end
170
172
 
171
- PasswordPadding =
173
+ PASSWORD_PADDING =
172
174
  '28BF4E5E4E758A4164004E56FFFA01082E2E00B6D0683E802F0CA9FE6453697A'
173
175
  .scan(/../).map { |x| x.to_i(16) }.pack('c*')
174
176
 
175
177
  # Pads or truncates a password to 32 bytes as per Alg 3.2.
176
178
  def pad_password(password)
177
179
  password = password[0, 32]
178
- password + PasswordPadding[0, 32 - password.length]
180
+ password + PASSWORD_PADDING[0, 32 - password.length]
179
181
  end
180
182
 
181
183
  def user_encryption_key
@@ -198,7 +200,7 @@ module Prawn
198
200
 
199
201
  # The U (user) value in the encryption dictionary. Algorithm 3.4.
200
202
  def user_password_hash
201
- Arcfour.new(user_encryption_key).encrypt(PasswordPadding)
203
+ Arcfour.new(user_encryption_key).encrypt(PASSWORD_PADDING)
202
204
  end
203
205
  end
204
206
  end
@@ -217,9 +219,9 @@ module PDF
217
219
  def encrypted_pdf_object(obj, key, id, gen, in_content_stream = false)
218
220
  case obj
219
221
  when Array
220
- '[' << obj.map do |e|
222
+ '[' + obj.map do |e|
221
223
  encrypted_pdf_object(e, key, id, gen, in_content_stream)
222
- end.join(' ') << ']'
224
+ end.join(' ') + ']'
223
225
  when LiteralString
224
226
  obj = ByteString.new(
225
227
  Prawn::Document::Security.encrypt_string(obj, key, id, gen)
@@ -239,16 +241,16 @@ module PDF
239
241
  in_content_stream
240
242
  )
241
243
  when ::Hash
242
- output = '<< '
243
- obj.each do |k, v|
244
- unless k.is_a?(String) || k.is_a?(Symbol)
245
- raise PDF::Core::Errors::FailedObjectConversion,
246
- 'A PDF Dictionary must be keyed by names'
247
- end
248
- output << pdf_object(k.to_sym, in_content_stream) << ' ' <<
249
- encrypted_pdf_object(v, key, id, gen, in_content_stream) << "\n"
250
- end
251
- output << '>>'
244
+ '<< ' +
245
+ obj.map do |k, v|
246
+ unless k.is_a?(String) || k.is_a?(Symbol)
247
+ raise PDF::Core::Errors::FailedObjectConversion,
248
+ 'A PDF Dictionary must be keyed by names'
249
+ end
250
+ pdf_object(k.to_sym, in_content_stream) + ' ' +
251
+ encrypted_pdf_object(v, key, id, gen, in_content_stream) + "\n"
252
+ end.join('') +
253
+ '>>'
252
254
  when NameTree::Value
253
255
  pdf_object(obj.name) + ' ' +
254
256
  encrypted_pdf_object(obj.value, key, id, gen, in_content_stream)
@@ -278,9 +280,9 @@ module PDF
278
280
  # Returns the object definition for the object this references, keyed from
279
281
  # +key+.
280
282
  def encrypted_object(key)
281
- @on_encode.call(self) if @on_encode
283
+ @on_encode&.call(self)
282
284
 
283
- output = "#{@identifier} #{gen} obj\n"
285
+ output = +"#{@identifier} #{gen} obj\n"
284
286
  if @stream.empty?
285
287
  output <<
286
288
  PDF::Core.encrypted_pdf_object(data, key, @identifier, gen) << "\n"