prawn 2.2.2 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/GPLv2 +20 -21
- data/Gemfile +2 -0
- data/Rakefile +10 -3
- data/lib/prawn.rb +9 -3
- data/lib/prawn/document.rb +40 -18
- data/lib/prawn/document/bounding_box.rb +9 -4
- data/lib/prawn/document/column_box.rb +2 -0
- data/lib/prawn/document/internals.rb +6 -4
- data/lib/prawn/document/span.rb +2 -0
- data/lib/prawn/encoding.rb +4 -1
- data/lib/prawn/errors.rb +2 -0
- data/lib/prawn/font.rb +34 -17
- data/lib/prawn/font_metric_cache.rb +8 -3
- data/lib/prawn/{font → fonts}/afm.rb +14 -7
- data/lib/prawn/{font → fonts}/dfont.rb +3 -1
- data/lib/prawn/fonts/otf.rb +11 -0
- data/lib/prawn/{font → fonts}/ttc.rb +3 -1
- data/lib/prawn/{font → fonts}/ttf.rb +23 -12
- data/lib/prawn/graphics.rb +24 -26
- data/lib/prawn/graphics/blend_mode.rb +2 -0
- data/lib/prawn/graphics/cap_style.rb +2 -0
- data/lib/prawn/graphics/color.rb +8 -3
- data/lib/prawn/graphics/dash.rb +3 -1
- data/lib/prawn/graphics/join_style.rb +2 -0
- data/lib/prawn/graphics/patterns.rb +7 -2
- data/lib/prawn/graphics/transformation.rb +5 -1
- data/lib/prawn/graphics/transparency.rb +2 -0
- data/lib/prawn/grid.rb +39 -38
- data/lib/prawn/image_handler.rb +2 -0
- data/lib/prawn/images.rb +21 -19
- data/lib/prawn/images/image.rb +2 -0
- data/lib/prawn/images/jpg.rb +2 -0
- data/lib/prawn/images/png.rb +9 -9
- data/lib/prawn/measurement_extensions.rb +2 -0
- data/lib/prawn/measurements.rb +5 -0
- data/lib/prawn/outline.rb +2 -0
- data/lib/prawn/repeater.rb +2 -0
- data/lib/prawn/security.rb +26 -24
- data/lib/prawn/security/arcfour.rb +2 -0
- data/lib/prawn/soft_mask.rb +3 -1
- data/lib/prawn/stamp.rb +4 -1
- data/lib/prawn/text.rb +14 -12
- data/lib/prawn/text/box.rb +3 -1
- data/lib/prawn/text/formatted.rb +2 -0
- data/lib/prawn/text/formatted/arranger.rb +12 -12
- data/lib/prawn/text/formatted/box.rb +23 -13
- data/lib/prawn/text/formatted/fragment.rb +2 -0
- data/lib/prawn/text/formatted/line_wrap.rb +10 -8
- data/lib/prawn/text/formatted/parser.rb +8 -10
- data/lib/prawn/text/formatted/wrap.rb +4 -2
- data/lib/prawn/transformation_stack.rb +5 -2
- data/lib/prawn/utilities.rb +2 -2
- data/lib/prawn/version.rb +3 -1
- data/lib/prawn/view.rb +15 -7
- data/manual/basic_concepts/adding_pages.rb +2 -0
- data/manual/basic_concepts/basic_concepts.rb +4 -2
- data/manual/basic_concepts/creation.rb +2 -0
- data/manual/basic_concepts/cursor.rb +2 -0
- data/manual/basic_concepts/measurement.rb +3 -1
- data/manual/basic_concepts/origin.rb +2 -0
- data/manual/basic_concepts/other_cursor_helpers.rb +2 -0
- data/manual/basic_concepts/view.rb +2 -0
- data/manual/bounding_box/bounding_box.rb +4 -2
- data/manual/bounding_box/bounds.rb +6 -4
- data/manual/bounding_box/canvas.rb +2 -0
- data/manual/bounding_box/creation.rb +2 -0
- data/manual/bounding_box/indentation.rb +2 -0
- data/manual/bounding_box/nesting.rb +2 -0
- data/manual/bounding_box/russian_boxes.rb +6 -4
- data/manual/bounding_box/stretchy.rb +2 -0
- data/manual/contents.rb +2 -0
- data/manual/cover.rb +10 -7
- data/manual/document_and_page_options/background.rb +2 -0
- data/manual/document_and_page_options/document_and_page_options.rb +4 -2
- data/manual/document_and_page_options/metadata.rb +2 -0
- data/manual/document_and_page_options/page_margins.rb +2 -0
- data/manual/document_and_page_options/page_size.rb +2 -0
- data/manual/document_and_page_options/print_scaling.rb +2 -0
- data/manual/example_helper.rb +5 -2
- data/manual/graphics/blend_mode.rb +6 -4
- data/manual/graphics/circle_and_ellipse.rb +2 -0
- data/manual/graphics/color.rb +2 -0
- data/manual/graphics/common_lines.rb +2 -0
- data/manual/graphics/fill_and_stroke.rb +2 -0
- data/manual/graphics/fill_rules.rb +2 -0
- data/manual/graphics/gradients.rb +2 -0
- data/manual/graphics/graphics.rb +4 -2
- data/manual/graphics/helper.rb +2 -0
- data/manual/graphics/line_width.rb +3 -0
- data/manual/graphics/lines_and_curves.rb +2 -0
- data/manual/graphics/polygon.rb +2 -0
- data/manual/graphics/rectangle.rb +2 -0
- data/manual/graphics/rotate.rb +2 -0
- data/manual/graphics/scale.rb +2 -0
- data/manual/graphics/soft_masks.rb +2 -0
- data/manual/graphics/stroke_cap.rb +3 -1
- data/manual/graphics/stroke_dash.rb +2 -0
- data/manual/graphics/stroke_join.rb +3 -1
- data/manual/graphics/translate.rb +2 -0
- data/manual/graphics/transparency.rb +2 -0
- data/manual/how_to_read_this_manual.rb +2 -0
- data/manual/images/absolute_position.rb +2 -0
- data/manual/images/fit.rb +2 -0
- data/manual/images/horizontal.rb +3 -1
- data/manual/images/images.rb +4 -2
- data/manual/images/plain_image.rb +2 -0
- data/manual/images/scale.rb +2 -0
- data/manual/images/vertical.rb +3 -1
- data/manual/images/width_and_height.rb +2 -0
- data/manual/layout/boxes.rb +2 -0
- data/manual/layout/content.rb +2 -0
- data/manual/layout/layout.rb +4 -2
- data/manual/layout/simple_grid.rb +2 -0
- data/manual/outline/add_subsection_to.rb +2 -0
- data/manual/outline/insert_section_after.rb +2 -0
- data/manual/outline/outline.rb +4 -2
- data/manual/outline/sections_and_pages.rb +2 -0
- data/manual/repeatable_content/alternate_page_numbering.rb +2 -0
- data/manual/repeatable_content/page_numbering.rb +2 -0
- data/manual/repeatable_content/repeatable_content.rb +4 -2
- data/manual/repeatable_content/repeater.rb +2 -0
- data/manual/repeatable_content/stamp.rb +2 -0
- data/manual/security/encryption.rb +2 -0
- data/manual/security/permissions.rb +2 -0
- data/manual/security/security.rb +4 -2
- data/manual/table.rb +2 -0
- data/manual/text/alignment.rb +2 -0
- data/manual/text/color.rb +2 -0
- data/manual/text/column_box.rb +4 -2
- data/manual/text/fallback_fonts.rb +4 -2
- data/manual/text/font.rb +2 -0
- data/manual/text/font_size.rb +2 -0
- data/manual/text/font_style.rb +4 -2
- data/manual/text/formatted_callbacks.rb +2 -0
- data/manual/text/formatted_text.rb +3 -1
- data/manual/text/free_flowing_text.rb +2 -0
- data/manual/text/inline.rb +2 -0
- data/manual/text/kerning_and_character_spacing.rb +2 -0
- data/manual/text/leading.rb +2 -0
- data/manual/text/line_wrapping.rb +4 -1
- data/manual/text/paragraph_indentation.rb +2 -0
- data/manual/text/positioned_text.rb +2 -0
- data/manual/text/registering_families.rb +4 -2
- data/manual/text/rendering_and_color.rb +2 -0
- data/manual/text/right_to_left_text.rb +2 -0
- data/manual/text/rotation.rb +3 -1
- data/manual/text/single_usage.rb +2 -0
- data/manual/text/text.rb +4 -3
- data/manual/text/text_box_excess.rb +2 -0
- data/manual/text/text_box_extensions.rb +2 -0
- data/manual/text/text_box_overflow.rb +3 -1
- data/manual/text/utf8.rb +3 -1
- data/manual/text/win_ansi_charset.rb +6 -3
- data/prawn.gemspec +14 -11
- data/spec/extensions/encoding_helpers.rb +4 -2
- data/spec/prawn/document/bounding_box_spec.rb +8 -8
- data/spec/prawn/document/column_box_spec.rb +2 -0
- data/spec/prawn/document/security_spec.rb +12 -9
- data/spec/prawn/document_annotations_spec.rb +2 -0
- data/spec/prawn/document_destinations_spec.rb +2 -0
- data/spec/prawn/document_grid_spec.rb +3 -0
- data/spec/prawn/document_reference_spec.rb +2 -0
- data/spec/prawn/document_span_spec.rb +2 -0
- data/spec/prawn/document_spec.rb +59 -14
- data/spec/prawn/font_metric_cache_spec.rb +2 -0
- data/spec/prawn/font_spec.rb +45 -16
- data/spec/prawn/graphics/blend_mode_spec.rb +4 -2
- data/spec/prawn/graphics/transparency_spec.rb +2 -0
- data/spec/prawn/graphics_spec.rb +40 -20
- data/spec/prawn/graphics_stroke_styles_spec.rb +2 -0
- data/spec/prawn/image_handler_spec.rb +2 -0
- data/spec/prawn/images/jpg_spec.rb +2 -0
- data/spec/prawn/images/png_spec.rb +2 -0
- data/spec/prawn/images_spec.rb +56 -2
- data/spec/prawn/measurements_extensions_spec.rb +2 -0
- data/spec/prawn/outline_spec.rb +6 -2
- data/spec/prawn/repeater_spec.rb +6 -4
- data/spec/prawn/soft_mask_spec.rb +2 -0
- data/spec/prawn/stamp_spec.rb +11 -7
- data/spec/prawn/text/box_spec.rb +26 -27
- data/spec/prawn/text/formatted/arranger_spec.rb +30 -28
- data/spec/prawn/text/formatted/box_spec.rb +78 -57
- data/spec/prawn/text/formatted/fragment_spec.rb +10 -8
- data/spec/prawn/text/formatted/line_wrap_spec.rb +8 -5
- data/spec/prawn/text/formatted/parser_spec.rb +32 -2
- data/spec/prawn/text_draw_text_spec.rb +5 -3
- data/spec/prawn/text_rendering_mode_spec.rb +7 -1
- data/spec/prawn/text_spacing_spec.rb +6 -4
- data/spec/prawn/text_spec.rb +9 -7
- data/spec/prawn/text_with_inline_formatting_spec.rb +3 -1
- data/spec/prawn/transformation_stack_spec.rb +2 -0
- data/spec/prawn/view_spec.rb +18 -0
- data/spec/{manual_spec.rb → prawn_manual_spec.rb} +6 -4
- data/spec/spec_helper.rb +9 -10
- metadata +86 -73
- 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|
|
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)
|
data/lib/prawn/grid.rb
CHANGED
@@ -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
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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 = [
|
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 |
|
81
|
-
columns.times do |
|
82
|
-
pdf.grid(
|
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,
|
115
|
+
def initialize(pdf, rows, columns)
|
115
116
|
@pdf = pdf
|
116
|
-
@
|
117
|
-
@
|
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
|
-
"#{@
|
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) * @
|
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) * @
|
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,
|
217
|
+
def initialize(pdf, box1, box2)
|
217
218
|
@pdf = pdf
|
218
|
-
@
|
219
|
+
@boxes = [box1, box2]
|
219
220
|
end
|
220
221
|
|
221
222
|
def name
|
222
|
-
@
|
223
|
+
@boxes.map(&:name).join(':')
|
223
224
|
end
|
224
225
|
|
225
226
|
def total_height
|
226
|
-
@
|
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
|
-
@
|
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 ||= @
|
261
|
+
@left_box ||= @boxes.min_by(&:left)
|
261
262
|
end
|
262
263
|
|
263
264
|
def right_box
|
264
|
-
@right_box ||= @
|
265
|
+
@right_box ||= @boxes.max_by(&:right)
|
265
266
|
end
|
266
267
|
|
267
268
|
def top_box
|
268
|
-
@top_box ||= @
|
269
|
+
@top_box ||= @boxes.max_by(&:top)
|
269
270
|
end
|
270
271
|
|
271
272
|
def bottom_box
|
272
|
-
@bottom_box ||= @
|
273
|
+
@bottom_box ||= @boxes.min_by(&:bottom)
|
273
274
|
end
|
274
275
|
end
|
275
276
|
|
276
277
|
private
|
277
278
|
|
278
|
-
def single_box(
|
279
|
-
GridBox.new(self,
|
279
|
+
def single_box(rows, columns)
|
280
|
+
GridBox.new(self, rows, columns)
|
280
281
|
end
|
281
282
|
|
282
|
-
def multi_box(
|
283
|
-
MultiBox.new(self,
|
283
|
+
def multi_box(box1, box2)
|
284
|
+
MultiBox.new(self, box1, box2)
|
284
285
|
end
|
285
286
|
end
|
286
287
|
end
|
data/lib/prawn/image_handler.rb
CHANGED
data/lib/prawn/images.rb
CHANGED
@@ -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
|
-
|
68
|
-
|
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
|
-
|
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
|
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
|
-
|
151
|
-
|
152
|
+
|
153
|
+
io_or_path.binread
|
152
154
|
end
|
153
155
|
|
154
|
-
def image_position(
|
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 -
|
163
|
+
bounds.absolute_top - (bounds.height - height) / 2.0
|
162
164
|
when :bottom
|
163
|
-
bounds.absolute_bottom +
|
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(
|
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 -
|
176
|
+
bounds.left_side + (bounds.width - width) / 2.0
|
175
177
|
when :right
|
176
|
-
bounds.right_side -
|
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(
|
185
|
-
if overruns_page?(
|
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?(
|
192
|
-
(y -
|
193
|
+
def overruns_page?(height)
|
194
|
+
(y - height) < reference_bounds.absolute_bottom
|
193
195
|
end
|
194
196
|
|
195
197
|
def image_registry
|
data/lib/prawn/images/image.rb
CHANGED
data/lib/prawn/images/jpg.rb
CHANGED
data/lib/prawn/images/png.rb
CHANGED
@@ -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).
|
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).
|
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
|
-
|
100
|
+
1
|
101
101
|
when 2, 6
|
102
|
-
|
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).
|
324
|
+
color = data.read(1).unpack1('C')
|
325
325
|
alpha.putc alpha_palette[color]
|
326
326
|
end
|
327
327
|
end
|
data/lib/prawn/measurements.rb
CHANGED
@@ -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
|
data/lib/prawn/outline.rb
CHANGED
data/lib/prawn/repeater.rb
CHANGED
data/lib/prawn/security.rb
CHANGED
@@ -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 [
|
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
|
-
|
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
|
150
|
+
unless PERMISSIONS_BITS[key]
|
149
151
|
raise(
|
150
152
|
ArgumentError,
|
151
153
|
"Unknown permission :#{key}. Valid options: " +
|
152
|
-
|
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 =
|
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
|
-
|
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 +
|
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(
|
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
|
-
'['
|
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
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
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
|
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"
|