prawn 2.4.0 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/prawn/document/bounding_box.rb +213 -141
- data/lib/prawn/document/column_box.rb +61 -26
- data/lib/prawn/document/internals.rb +25 -16
- data/lib/prawn/document/span.rb +20 -18
- data/lib/prawn/document.rb +257 -171
- data/lib/prawn/encoding.rb +2 -5
- data/lib/prawn/errors.rb +23 -34
- data/lib/prawn/font.rb +248 -135
- data/lib/prawn/font_metric_cache.rb +11 -10
- data/lib/prawn/fonts/afm.rb +85 -45
- data/lib/prawn/fonts/dfont.rb +7 -1
- data/lib/prawn/fonts/otf.rb +4 -1
- data/lib/prawn/fonts/to_unicode_cmap.rb +151 -0
- data/lib/prawn/fonts/ttc.rb +7 -2
- data/lib/prawn/fonts/ttf.rb +305 -93
- data/lib/prawn/fonts.rb +14 -0
- data/lib/prawn/graphics/blend_mode.rb +25 -28
- data/lib/prawn/graphics/cap_style.rb +9 -12
- data/lib/prawn/graphics/color.rb +57 -34
- data/lib/prawn/graphics/dash.rb +45 -42
- data/lib/prawn/graphics/join_style.rb +17 -11
- data/lib/prawn/graphics/patterns.rb +190 -69
- data/lib/prawn/graphics/transformation.rb +48 -41
- data/lib/prawn/graphics/transparency.rb +16 -40
- data/lib/prawn/graphics.rb +363 -253
- data/lib/prawn/grid.rb +184 -57
- data/lib/prawn/image_handler.rb +27 -10
- data/lib/prawn/images/image.rb +8 -10
- data/lib/prawn/images/jpg.rb +42 -19
- data/lib/prawn/images/png.rb +92 -41
- data/lib/prawn/images.rb +44 -57
- data/lib/prawn/measurement_extensions.rb +39 -8
- data/lib/prawn/measurements.rb +60 -5
- data/lib/prawn/outline.rb +114 -108
- data/lib/prawn/repeater.rb +51 -35
- data/lib/prawn/security/arcfour.rb +4 -4
- data/lib/prawn/security.rb +75 -70
- data/lib/prawn/soft_mask.rb +42 -30
- data/lib/prawn/stamp.rb +38 -42
- data/lib/prawn/text/box.rb +146 -96
- data/lib/prawn/text/formatted/arranger.rb +87 -26
- data/lib/prawn/text/formatted/box.rb +221 -150
- data/lib/prawn/text/formatted/fragment.rb +130 -14
- data/lib/prawn/text/formatted/line_wrap.rb +33 -24
- data/lib/prawn/text/formatted/parser.rb +112 -72
- data/lib/prawn/text/formatted/wrap.rb +12 -17
- data/lib/prawn/text/formatted.rb +75 -0
- data/lib/prawn/text.rb +441 -196
- data/lib/prawn/transformation_stack.rb +29 -10
- data/lib/prawn/utilities.rb +13 -13
- data/lib/prawn/version.rb +2 -1
- data/lib/prawn/view.rb +68 -53
- data/lib/prawn.rb +23 -18
- data.tar.gz.sig +0 -0
- metadata +54 -177
- metadata.gz.sig +0 -0
- data/.yardopts +0 -10
- data/Gemfile +0 -5
- data/Rakefile +0 -25
- data/manual/absolute_position.pdf +0 -0
- data/manual/basic_concepts/adding_pages.rb +0 -26
- data/manual/basic_concepts/basic_concepts.rb +0 -43
- data/manual/basic_concepts/creation.rb +0 -38
- data/manual/basic_concepts/cursor.rb +0 -32
- data/manual/basic_concepts/measurement.rb +0 -24
- data/manual/basic_concepts/origin.rb +0 -37
- data/manual/basic_concepts/other_cursor_helpers.rb +0 -39
- data/manual/basic_concepts/view.rb +0 -48
- data/manual/bounding_box/bounding_box.rb +0 -41
- data/manual/bounding_box/bounds.rb +0 -48
- data/manual/bounding_box/canvas.rb +0 -23
- data/manual/bounding_box/creation.rb +0 -22
- data/manual/bounding_box/indentation.rb +0 -45
- data/manual/bounding_box/nesting.rb +0 -52
- data/manual/bounding_box/russian_boxes.rb +0 -40
- data/manual/bounding_box/stretchy.rb +0 -29
- data/manual/contents.rb +0 -35
- data/manual/cover.rb +0 -43
- data/manual/document_and_page_options/background.rb +0 -29
- data/manual/document_and_page_options/document_and_page_options.rb +0 -34
- data/manual/document_and_page_options/metadata.rb +0 -25
- data/manual/document_and_page_options/page_margins.rb +0 -36
- data/manual/document_and_page_options/page_size.rb +0 -34
- data/manual/document_and_page_options/print_scaling.rb +0 -23
- data/manual/example_helper.rb +0 -8
- data/manual/graphics/blend_mode.rb +0 -52
- data/manual/graphics/circle_and_ellipse.rb +0 -21
- data/manual/graphics/color.rb +0 -22
- data/manual/graphics/common_lines.rb +0 -29
- data/manual/graphics/fill_and_stroke.rb +0 -41
- data/manual/graphics/fill_rules.rb +0 -38
- data/manual/graphics/gradients.rb +0 -43
- data/manual/graphics/graphics.rb +0 -64
- data/manual/graphics/helper.rb +0 -34
- data/manual/graphics/line_width.rb +0 -36
- data/manual/graphics/lines_and_curves.rb +0 -40
- data/manual/graphics/polygon.rb +0 -27
- data/manual/graphics/rectangle.rb +0 -20
- data/manual/graphics/rotate.rb +0 -25
- data/manual/graphics/scale.rb +0 -42
- data/manual/graphics/soft_masks.rb +0 -44
- data/manual/graphics/stroke_cap.rb +0 -30
- data/manual/graphics/stroke_dash.rb +0 -47
- data/manual/graphics/stroke_join.rb +0 -29
- data/manual/graphics/translate.rb +0 -29
- data/manual/graphics/transparency.rb +0 -33
- data/manual/how_to_read_this_manual.rb +0 -39
- data/manual/images/absolute_position.rb +0 -22
- data/manual/images/fit.rb +0 -20
- data/manual/images/horizontal.rb +0 -24
- data/manual/images/images.rb +0 -41
- data/manual/images/plain_image.rb +0 -17
- data/manual/images/scale.rb +0 -21
- data/manual/images/vertical.rb +0 -30
- data/manual/images/width_and_height.rb +0 -24
- data/manual/layout/boxes.rb +0 -26
- data/manual/layout/content.rb +0 -24
- data/manual/layout/layout.rb +0 -27
- data/manual/layout/simple_grid.rb +0 -22
- data/manual/outline/add_subsection_to.rb +0 -60
- data/manual/outline/insert_section_after.rb +0 -46
- data/manual/outline/outline.rb +0 -33
- data/manual/outline/sections_and_pages.rb +0 -66
- data/manual/repeatable_content/alternate_page_numbering.rb +0 -36
- data/manual/repeatable_content/page_numbering.rb +0 -55
- data/manual/repeatable_content/repeatable_content.rb +0 -35
- data/manual/repeatable_content/repeater.rb +0 -54
- data/manual/repeatable_content/stamp.rb +0 -40
- data/manual/security/encryption.rb +0 -28
- data/manual/security/permissions.rb +0 -43
- data/manual/security/security.rb +0 -28
- data/manual/table.rb +0 -16
- data/manual/text/alignment.rb +0 -43
- data/manual/text/color.rb +0 -24
- data/manual/text/column_box.rb +0 -30
- data/manual/text/fallback_fonts.rb +0 -41
- data/manual/text/font.rb +0 -40
- data/manual/text/font_size.rb +0 -44
- data/manual/text/font_style.rb +0 -25
- data/manual/text/formatted_callbacks.rb +0 -70
- data/manual/text/formatted_text.rb +0 -61
- data/manual/text/free_flowing_text.rb +0 -50
- data/manual/text/inline.rb +0 -40
- data/manual/text/kerning_and_character_spacing.rb +0 -38
- data/manual/text/leading.rb +0 -24
- data/manual/text/line_wrapping.rb +0 -60
- data/manual/text/paragraph_indentation.rb +0 -31
- data/manual/text/positioned_text.rb +0 -37
- data/manual/text/registering_families.rb +0 -51
- data/manual/text/rendering_and_color.rb +0 -36
- data/manual/text/right_to_left_text.rb +0 -54
- data/manual/text/rotation.rb +0 -52
- data/manual/text/single_usage.rb +0 -36
- data/manual/text/text.rb +0 -75
- data/manual/text/text_box_excess.rb +0 -35
- data/manual/text/text_box_extensions.rb +0 -48
- data/manual/text/text_box_overflow.rb +0 -51
- data/manual/text/utf8.rb +0 -27
- data/manual/text/win_ansi_charset.rb +0 -62
- data/prawn.gemspec +0 -51
- data/spec/data/curves.pdf +0 -66
- data/spec/extensions/encoding_helpers.rb +0 -11
- data/spec/prawn/document/bounding_box_spec.rb +0 -550
- data/spec/prawn/document/column_box_spec.rb +0 -75
- data/spec/prawn/document/security_spec.rb +0 -176
- data/spec/prawn/document_annotations_spec.rb +0 -76
- data/spec/prawn/document_destinations_spec.rb +0 -15
- data/spec/prawn/document_grid_spec.rb +0 -99
- data/spec/prawn/document_reference_spec.rb +0 -27
- data/spec/prawn/document_span_spec.rb +0 -44
- data/spec/prawn/document_spec.rb +0 -805
- data/spec/prawn/font_metric_cache_spec.rb +0 -54
- data/spec/prawn/font_spec.rb +0 -544
- data/spec/prawn/graphics/blend_mode_spec.rb +0 -63
- data/spec/prawn/graphics/transparency_spec.rb +0 -81
- data/spec/prawn/graphics_spec.rb +0 -872
- data/spec/prawn/graphics_stroke_styles_spec.rb +0 -229
- data/spec/prawn/image_handler_spec.rb +0 -53
- data/spec/prawn/images/jpg_spec.rb +0 -20
- data/spec/prawn/images/png_spec.rb +0 -283
- data/spec/prawn/images_spec.rb +0 -229
- data/spec/prawn/measurements_extensions_spec.rb +0 -24
- data/spec/prawn/outline_spec.rb +0 -512
- data/spec/prawn/repeater_spec.rb +0 -166
- data/spec/prawn/soft_mask_spec.rb +0 -74
- data/spec/prawn/stamp_spec.rb +0 -173
- data/spec/prawn/text/box_spec.rb +0 -1110
- data/spec/prawn/text/formatted/arranger_spec.rb +0 -466
- data/spec/prawn/text/formatted/box_spec.rb +0 -849
- data/spec/prawn/text/formatted/fragment_spec.rb +0 -343
- data/spec/prawn/text/formatted/line_wrap_spec.rb +0 -495
- data/spec/prawn/text/formatted/parser_spec.rb +0 -697
- data/spec/prawn/text_draw_text_spec.rb +0 -150
- data/spec/prawn/text_rendering_mode_spec.rb +0 -48
- data/spec/prawn/text_spacing_spec.rb +0 -95
- data/spec/prawn/text_spec.rb +0 -603
- data/spec/prawn/text_with_inline_formatting_spec.rb +0 -35
- data/spec/prawn/transformation_stack_spec.rb +0 -66
- data/spec/prawn/view_spec.rb +0 -63
- data/spec/prawn_manual_spec.rb +0 -35
- data/spec/spec_helper.rb +0 -48
data/lib/prawn/images/png.rb
CHANGED
@@ -1,38 +1,77 @@
|
|
1
1
|
# encoding: ASCII-8BIT
|
2
|
-
|
3
2
|
# frozen_string_literal: true
|
4
3
|
|
5
|
-
# png.rb : Extracts the data from a PNG that is needed for embedding
|
6
|
-
#
|
7
|
-
# Based on some similar code in PDF::Writer by Austin Ziegler
|
8
|
-
#
|
9
|
-
# Copyright April 2008, James Healy. All Rights Reserved.
|
10
|
-
#
|
11
|
-
# This is free software. Please see the LICENSE and COPYING files for details.
|
12
|
-
|
13
4
|
require 'stringio'
|
14
5
|
module Prawn
|
15
|
-
module Images
|
16
|
-
# A convenience class that wraps the logic for extracting the parts
|
17
|
-
#
|
18
|
-
#
|
6
|
+
module Images # rubocop: disable Style/Documentation
|
7
|
+
# A convenience class that wraps the logic for extracting the parts of a PNG
|
8
|
+
# image that we need to embed them in a PDF.
|
19
9
|
class PNG < Image
|
20
10
|
# @group Extension API
|
21
11
|
|
22
|
-
|
23
|
-
|
24
|
-
attr_reader :
|
25
|
-
|
26
|
-
|
12
|
+
# Palette data.
|
13
|
+
# @return [String]
|
14
|
+
attr_reader :palette
|
15
|
+
|
16
|
+
# Image data.
|
17
|
+
# @return [String]
|
18
|
+
attr_reader :img_data
|
19
|
+
|
20
|
+
# Transparency data.
|
21
|
+
# @return [Hash{Symbol => String}]
|
22
|
+
attr_reader :transparency
|
23
|
+
|
24
|
+
# Image width in pixels.
|
25
|
+
# @return [Integer]
|
26
|
+
attr_reader :width
|
27
|
+
|
28
|
+
# Image height in pixels.
|
29
|
+
# @return [Integer]
|
30
|
+
attr_reader :height
|
31
|
+
|
32
|
+
# Bits per sample or per palette index.
|
33
|
+
# @return [Integer]
|
34
|
+
attr_reader :bits
|
35
|
+
|
36
|
+
# Color type.
|
37
|
+
# @return [Integer]
|
38
|
+
attr_reader :color_type
|
39
|
+
|
40
|
+
# Compression method.
|
41
|
+
# @return [Integer]
|
42
|
+
attr_reader :compression_method
|
43
|
+
|
44
|
+
# Filter method.
|
45
|
+
# @return [Integer]
|
46
|
+
attr_reader :filter_method
|
27
47
|
|
48
|
+
# Interlace method.
|
49
|
+
# @return [Integer]
|
50
|
+
attr_reader :interlace_method
|
51
|
+
|
52
|
+
# Extracted alpha-channel.
|
53
|
+
# @return [String, nil]
|
54
|
+
attr_reader :alpha_channel
|
55
|
+
|
56
|
+
# Scaled width of the image in PDF points.
|
57
|
+
# @return [Number]
|
58
|
+
attr_accessor :scaled_width
|
59
|
+
|
60
|
+
# Scaled height of the image in PDF points.
|
61
|
+
# @return [Number]
|
62
|
+
attr_accessor :scaled_height
|
63
|
+
|
64
|
+
# Can this image handler process this image?
|
65
|
+
#
|
66
|
+
# @param image_blob [String]
|
67
|
+
# @return [Boolean]
|
28
68
|
def self.can_render?(image_blob)
|
29
69
|
image_blob[0, 8].unpack('C*') == [137, 80, 78, 71, 13, 10, 26, 10]
|
30
70
|
end
|
31
71
|
|
32
72
|
# Process a new PNG image
|
33
73
|
#
|
34
|
-
#
|
35
|
-
#
|
74
|
+
# @param data [String] A binary string of PNG data.
|
36
75
|
def initialize(data)
|
37
76
|
super()
|
38
77
|
data = StringIO.new(data.dup)
|
@@ -93,8 +132,9 @@ module Prawn
|
|
93
132
|
@img_data = Zlib::Inflate.inflate(@img_data)
|
94
133
|
end
|
95
134
|
|
96
|
-
#
|
135
|
+
# Number of color components to each pixel.
|
97
136
|
#
|
137
|
+
# @return [Integer]
|
98
138
|
def colors
|
99
139
|
case color_type
|
100
140
|
when 0, 3, 4
|
@@ -104,9 +144,11 @@ module Prawn
|
|
104
144
|
end
|
105
145
|
end
|
106
146
|
|
107
|
-
#
|
108
|
-
#
|
147
|
+
# Split the alpha channel data from the raw image data in images where
|
148
|
+
# it's required.
|
109
149
|
#
|
150
|
+
# @private
|
151
|
+
# @return [void]
|
110
152
|
def split_alpha_channel!
|
111
153
|
if alpha_channel?
|
112
154
|
if color_type == 3
|
@@ -117,6 +159,9 @@ module Prawn
|
|
117
159
|
end
|
118
160
|
end
|
119
161
|
|
162
|
+
# Is there an alpha-channel in this image?
|
163
|
+
#
|
164
|
+
# @return [Boolean]
|
120
165
|
def alpha_channel?
|
121
166
|
return true if color_type == 4 || color_type == 6
|
122
167
|
return @transparency.any? if color_type == 3
|
@@ -124,9 +169,11 @@ module Prawn
|
|
124
169
|
false
|
125
170
|
end
|
126
171
|
|
127
|
-
# Build a PDF object representing this image in
|
172
|
+
# Build a PDF object representing this image in `document`, and return
|
128
173
|
# a Reference to it.
|
129
174
|
#
|
175
|
+
# @param document [Prawn::Document]
|
176
|
+
# @return [PDF::Core::Reference]
|
130
177
|
def build_pdf_object(document)
|
131
178
|
if compression_method != 0
|
132
179
|
raise Errors::UnsupportedImageType,
|
@@ -163,7 +210,7 @@ module Prawn
|
|
163
210
|
Subtype: :Image,
|
164
211
|
Height: height,
|
165
212
|
Width: width,
|
166
|
-
BitsPerComponent: bits
|
213
|
+
BitsPerComponent: bits,
|
167
214
|
)
|
168
215
|
|
169
216
|
# append the actual image data to the object as a stream
|
@@ -174,8 +221,8 @@ module Prawn
|
|
174
221
|
Predictor: 15,
|
175
222
|
Colors: colors,
|
176
223
|
BitsPerComponent: bits,
|
177
|
-
Columns: width
|
178
|
-
}
|
224
|
+
Columns: width,
|
225
|
+
},
|
179
226
|
}
|
180
227
|
|
181
228
|
# sort out the colours of the image
|
@@ -191,7 +238,7 @@ module Prawn
|
|
191
238
|
:Indexed,
|
192
239
|
:DeviceRGB,
|
193
240
|
(palette.size / 3) - 1,
|
194
|
-
palette_obj
|
241
|
+
palette_obj,
|
195
242
|
]
|
196
243
|
end
|
197
244
|
|
@@ -218,7 +265,7 @@ module Prawn
|
|
218
265
|
|
219
266
|
# For PNG color types 4 and 6, the transparency data is stored as
|
220
267
|
# a alpha channel mixed in with the main image data. The PNG class
|
221
|
-
#
|
268
|
+
# separates it out for us and makes it available via the alpha_channel
|
222
269
|
# attribute
|
223
270
|
if alpha_channel?
|
224
271
|
smask_obj = document.ref!(
|
@@ -228,7 +275,7 @@ module Prawn
|
|
228
275
|
Width: width,
|
229
276
|
BitsPerComponent: bits,
|
230
277
|
ColorSpace: :DeviceGray,
|
231
|
-
Decode: [0, 1]
|
278
|
+
Decode: [0, 1],
|
232
279
|
)
|
233
280
|
smask_obj.stream << alpha_channel
|
234
281
|
|
@@ -237,8 +284,8 @@ module Prawn
|
|
237
284
|
Predictor: 15,
|
238
285
|
Colors: 1,
|
239
286
|
BitsPerComponent: bits,
|
240
|
-
Columns: width
|
241
|
-
}
|
287
|
+
Columns: width,
|
288
|
+
},
|
242
289
|
}
|
243
290
|
obj.data[:SMask] = smask_obj
|
244
291
|
end
|
@@ -247,6 +294,8 @@ module Prawn
|
|
247
294
|
end
|
248
295
|
|
249
296
|
# Returns the minimum PDF version required to support this image.
|
297
|
+
#
|
298
|
+
# @return [Float]
|
250
299
|
def min_pdf_version
|
251
300
|
if bits > 8
|
252
301
|
# 16-bit color only supported in 1.5+ (ISO 32000-1:2008 8.9.5.1)
|
@@ -265,18 +314,18 @@ module Prawn
|
|
265
314
|
alpha_bytes = bits / 8
|
266
315
|
color_bytes = colors * bits / 8
|
267
316
|
|
268
|
-
scanline_length = (color_bytes + alpha_bytes) * width + 1
|
317
|
+
scanline_length = ((color_bytes + alpha_bytes) * width) + 1
|
269
318
|
scanlines = @img_data.bytesize / scanline_length
|
270
319
|
pixels = width * height
|
271
320
|
|
272
321
|
data = StringIO.new(@img_data)
|
273
322
|
data.binmode
|
274
323
|
|
275
|
-
color_data = [0x00].pack('C') * (pixels * color_bytes + scanlines)
|
324
|
+
color_data = [0x00].pack('C') * ((pixels * color_bytes) + scanlines)
|
276
325
|
color = StringIO.new(color_data)
|
277
326
|
color.binmode
|
278
327
|
|
279
|
-
@alpha_channel = [0x00].pack('C') * (pixels * alpha_bytes + scanlines)
|
328
|
+
@alpha_channel = [0x00].pack('C') * ((pixels * alpha_bytes) + scanlines)
|
280
329
|
alpha = StringIO.new(@alpha_channel)
|
281
330
|
alpha.binmode
|
282
331
|
|
@@ -285,12 +334,12 @@ module Prawn
|
|
285
334
|
|
286
335
|
filter = data.getbyte
|
287
336
|
|
288
|
-
color.putc
|
289
|
-
alpha.putc
|
337
|
+
color.putc(filter)
|
338
|
+
alpha.putc(filter)
|
290
339
|
|
291
340
|
width.times do
|
292
|
-
color.write
|
293
|
-
alpha.write
|
341
|
+
color.write(data.read(color_bytes))
|
342
|
+
alpha.write(data.read(alpha_bytes))
|
294
343
|
end
|
295
344
|
end
|
296
345
|
|
@@ -319,14 +368,16 @@ module Prawn
|
|
319
368
|
|
320
369
|
filter = data.getbyte
|
321
370
|
|
322
|
-
alpha.putc
|
371
|
+
alpha.putc(filter)
|
323
372
|
|
324
373
|
width.times do
|
325
374
|
color = data.read(1).unpack1('C')
|
326
|
-
alpha.putc
|
375
|
+
alpha.putc(alpha_palette[color])
|
327
376
|
end
|
328
377
|
end
|
329
378
|
end
|
330
379
|
end
|
380
|
+
|
381
|
+
Prawn.image_handler.register(Prawn::Images::PNG)
|
331
382
|
end
|
332
383
|
end
|
data/lib/prawn/images.rb
CHANGED
@@ -1,74 +1,62 @@
|
|
1
1
|
# encoding: ASCII-8BIT
|
2
|
-
|
3
2
|
# frozen_string_literal: true
|
4
3
|
|
5
|
-
# images.rb : Implements PDF image embedding
|
6
|
-
#
|
7
|
-
# Copyright April 2008, James Healy, Gregory Brown. All Rights Reserved.
|
8
|
-
#
|
9
|
-
# This is free software. Please see the LICENSE and COPYING files for details.
|
10
|
-
|
11
4
|
require 'digest/sha1'
|
12
5
|
require 'pathname'
|
13
6
|
|
14
7
|
module Prawn
|
8
|
+
# PDF image embedding.
|
15
9
|
module Images
|
16
10
|
# @group Stable API
|
17
11
|
|
18
|
-
# Add the image at
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
# Arguments:
|
23
|
-
# <tt>file</tt>:: path to file or an object that responds to #read and
|
24
|
-
# #rewind
|
12
|
+
# Add the image at `file` to the current page. Currently only JPG and
|
13
|
+
# PNG files are supported. (Note that processing PNG images with alpha
|
14
|
+
# channels can be processor and memory intensive.)
|
25
15
|
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
# <tt>:position</tt>:: One of (:left, :center, :right) or an x-offset
|
30
|
-
# <tt>:vposition</tt>:: One of (:top, :center, :bottom) or an y-offset
|
31
|
-
# <tt>:height</tt>:: the height of the image [actual height of the image]
|
32
|
-
# <tt>:width</tt>:: the width of the image [actual width of the image]
|
33
|
-
# <tt>:scale</tt>:: scale the dimensions of the image proportionally
|
34
|
-
# <tt>:fit</tt>:: scale the dimensions of the image proportionally to fit
|
35
|
-
# inside [width,height]
|
16
|
+
# If only one of `:width` or `:height` are provided, the image will be
|
17
|
+
# scaled proportionally. When both are provided, the image will be stretched
|
18
|
+
# to fit the dimensions without maintaining the aspect ratio.
|
36
19
|
#
|
37
|
-
#
|
20
|
+
# @example
|
21
|
+
# Prawn::Document.generate("image2.pdf", page_layout: :landscape) do
|
38
22
|
# pigs = "#{Prawn::DATADIR}/images/pigs.jpg"
|
39
|
-
# image pigs, :
|
23
|
+
# image pigs, at: [50,450], width: 450
|
40
24
|
#
|
41
25
|
# dice = "#{Prawn::DATADIR}/images/dice.png"
|
42
|
-
# image dice, :
|
43
|
-
# end
|
44
|
-
#
|
45
|
-
# If only one of :width / :height are provided, the image will be scaled
|
46
|
-
# proportionally. When both are provided, the image will be stretched to
|
47
|
-
# fit the dimensions without maintaining the aspect ratio.
|
48
|
-
#
|
49
|
-
#
|
50
|
-
# If :at is provided, the image will be place in the current page but
|
51
|
-
# the text position will not be changed.
|
52
|
-
#
|
53
|
-
#
|
54
|
-
# If instead of an explicit filename, an object with a read method is
|
55
|
-
# passed as +file+, you can embed images from IO objects and things
|
56
|
-
# that act like them (including Tempfiles and open-uri objects).
|
57
|
-
#
|
58
|
-
# require "open-uri"
|
59
|
-
#
|
60
|
-
# Prawn::Document.generate("remote_images.pdf") do
|
61
|
-
# image open("http://prawnpdf.org/media/prawn_logo.png")
|
26
|
+
# image dice, at: [50, 450], scale: 0.75
|
62
27
|
# end
|
63
28
|
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
#
|
29
|
+
# @param file [String, IO]
|
30
|
+
# Path to file or an object that responds to `#read` and `#rewind`.
|
31
|
+
# @param options [Hash{Symbol => any}]
|
32
|
+
# @option options :at [Array(Number, Number)]
|
33
|
+
# The location of the top left corner of the image. If provided,
|
34
|
+
# the image will be place in the current page but the text position will
|
35
|
+
# not be changed.
|
36
|
+
# @option options :position [:left, :center, :right, Number]
|
37
|
+
# Horizontal position relative to the current bounding box.
|
38
|
+
# @option options :vposition [:topm :center, :bottom, Number]
|
39
|
+
# Vertical position relative to the current bounding box.
|
40
|
+
# @option options :height [Number] (actual height of the image)
|
41
|
+
# The height of the image.
|
42
|
+
# @option options :width [Number] (actual width of the image)
|
43
|
+
# The width of the image.
|
44
|
+
# @option options :scale [Number]
|
45
|
+
# Scale the dimensions of the image proportionally.
|
46
|
+
# @option options :fit [Array(Number, Number)]
|
47
|
+
# Scale the dimensions of the image proportionally to fit
|
48
|
+
# inside the rectangle of specified size (width, height).
|
49
|
+
# @return [Prawn::Images::Image]
|
50
|
+
# An image handler. All image handlers provided by Prawn are subclasses of
|
51
|
+
# {Prawn::Images::Image}. This object can be used to check the image
|
52
|
+
# dimensions and get other format-specific information.
|
67
53
|
#
|
54
|
+
# @see Prawn::Images::PNG
|
55
|
+
# @see Prawn::Images::JPG
|
68
56
|
def image(file, options = {})
|
69
57
|
Prawn.verify_options(
|
70
58
|
%i[at position vposition height width scale fit],
|
71
|
-
options
|
59
|
+
options,
|
72
60
|
)
|
73
61
|
|
74
62
|
pdf_obj, info = build_image_object(file)
|
@@ -106,10 +94,9 @@ module Prawn
|
|
106
94
|
[image_obj, info]
|
107
95
|
end
|
108
96
|
|
109
|
-
# Given a PDF image resource
|
110
|
-
#
|
111
|
-
# build_image_object), embed the image according to the
|
112
|
-
# given.
|
97
|
+
# Given a PDF image resource `pdf_obj` that has been added to the page's
|
98
|
+
# resources and an `info` object (the pair returned from
|
99
|
+
# {build_image_object}), embed the image according to the `options` given.
|
113
100
|
#
|
114
101
|
# @private
|
115
102
|
def embed_image(pdf_obj, info, options)
|
@@ -120,7 +107,7 @@ module Prawn
|
|
120
107
|
x, y = map_to_absolute(options[:at])
|
121
108
|
else
|
122
109
|
x, y = image_position(w, h, options)
|
123
|
-
move_text_position
|
110
|
+
move_text_position(h)
|
124
111
|
end
|
125
112
|
|
126
113
|
# add a reference to the image object to the current page
|
@@ -161,7 +148,7 @@ module Prawn
|
|
161
148
|
when :top
|
162
149
|
bounds.absolute_top
|
163
150
|
when :center
|
164
|
-
bounds.absolute_top - (bounds.height - height) / 2.0
|
151
|
+
bounds.absolute_top - ((bounds.height - height) / 2.0)
|
165
152
|
when :bottom
|
166
153
|
bounds.absolute_bottom + height
|
167
154
|
when Numeric
|
@@ -175,7 +162,7 @@ module Prawn
|
|
175
162
|
when :left
|
176
163
|
bounds.left_side
|
177
164
|
when :center
|
178
|
-
bounds.left_side + (bounds.width - width) / 2.0
|
165
|
+
bounds.left_side + ((bounds.width - width) / 2.0)
|
179
166
|
when :right
|
180
167
|
bounds.right_side - width
|
181
168
|
when Numeric
|
@@ -1,50 +1,81 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# measurement_extensions.rb: Core extensions for Prawn::Measurements
|
4
|
-
#
|
5
|
-
# Copyright December 2008, Florian Witteler. All Rights Reserved.
|
6
|
-
#
|
7
|
-
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
-
|
9
3
|
require_relative 'measurements'
|
10
4
|
|
11
5
|
# @group Stable API
|
12
6
|
|
7
|
+
# Core extensions for {Prawn::Measurements}.
|
8
|
+
#
|
9
|
+
# This mainly enables measurements DSL.
|
10
|
+
#
|
11
|
+
# You have to explicitly require "prawn/measurement_extensions" to enable these.
|
12
|
+
#
|
13
|
+
# ```ruby
|
14
|
+
# require 'prawn/measurement_extensions'
|
15
|
+
#
|
16
|
+
# 12.mm
|
17
|
+
# 2.cm
|
18
|
+
# 0.5.in
|
19
|
+
# 4.yd + 2.ft
|
20
|
+
# ```
|
13
21
|
class Numeric
|
14
22
|
include Prawn::Measurements
|
15
|
-
#
|
16
|
-
# 72 points per inch
|
23
|
+
# Prawn's basic unit is PostScript-Point: 72 points per inch.
|
17
24
|
|
18
25
|
# @group Experimental API
|
19
26
|
|
27
|
+
# Convert from millimeters to points.
|
28
|
+
#
|
29
|
+
# @return [Number]
|
20
30
|
def mm
|
21
31
|
mm2pt(self)
|
22
32
|
end
|
23
33
|
|
34
|
+
# Convert from centimeters to points.
|
35
|
+
#
|
36
|
+
# @return [Number]
|
24
37
|
def cm
|
25
38
|
cm2pt(self)
|
26
39
|
end
|
27
40
|
|
41
|
+
# Convert from decimeters to points.
|
42
|
+
#
|
43
|
+
# @return [Number]
|
28
44
|
def dm
|
29
45
|
dm2pt(self)
|
30
46
|
end
|
31
47
|
|
48
|
+
# Convert from meters to points.
|
49
|
+
#
|
50
|
+
# @return [Number]
|
32
51
|
def m
|
33
52
|
m2pt(self)
|
34
53
|
end
|
35
54
|
|
55
|
+
# Convert from inches to points.
|
56
|
+
#
|
57
|
+
# @return [Number]
|
36
58
|
def in
|
37
59
|
in2pt(self)
|
38
60
|
end
|
39
61
|
|
62
|
+
# Convert from yards to points.
|
63
|
+
#
|
64
|
+
# @return [Number]
|
40
65
|
def yd
|
41
66
|
yd2pt(self)
|
42
67
|
end
|
43
68
|
|
69
|
+
# Convert from feet to points.
|
70
|
+
#
|
71
|
+
# @return [Number]
|
44
72
|
def ft
|
45
73
|
ft2pt(self)
|
46
74
|
end
|
47
75
|
|
76
|
+
# Convert from points to points.
|
77
|
+
#
|
78
|
+
# @return [Number]
|
48
79
|
def pt
|
49
80
|
pt2pt(self)
|
50
81
|
end
|
data/lib/prawn/measurements.rb
CHANGED
@@ -1,71 +1,126 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# measurements.rb: Conversions from other measurements to PDF points
|
4
|
-
#
|
5
|
-
# Copyright December 2008, Florian Witteler. All Rights Reserved.
|
6
|
-
#
|
7
|
-
|
8
3
|
# rubocop: disable Naming/MethodParameterName
|
9
4
|
module Prawn
|
10
5
|
# @group Stable API
|
11
6
|
|
7
|
+
# Distance unit conversions between metric, imperial, and PDF.
|
12
8
|
module Measurements
|
13
9
|
# metric conversions
|
10
|
+
|
11
|
+
# Convert centimeter to millimeters.
|
12
|
+
#
|
13
|
+
# @param cm [Number]
|
14
|
+
# @return [Number]
|
14
15
|
def cm2mm(cm)
|
15
16
|
cm * 10
|
16
17
|
end
|
17
18
|
|
19
|
+
# Convert decimeters to millimeters.
|
20
|
+
#
|
21
|
+
# @param dm [Number]
|
22
|
+
# @return [Number]
|
18
23
|
def dm2mm(dm)
|
19
24
|
dm * 100
|
20
25
|
end
|
21
26
|
|
27
|
+
# Convert meters to millimeters.
|
28
|
+
#
|
29
|
+
# @param m [Number]
|
30
|
+
# @return [Number]
|
22
31
|
def m2mm(m)
|
23
32
|
m * 1000
|
24
33
|
end
|
25
34
|
|
26
35
|
# imperial conversions
|
27
36
|
# from http://en.wikipedia.org/wiki/Imperial_units
|
37
|
+
|
38
|
+
# Convert feet to inches.
|
39
|
+
#
|
40
|
+
# @param ft [Number]
|
41
|
+
# @return [Number]
|
28
42
|
def ft2in(ft)
|
29
43
|
ft * 12
|
30
44
|
end
|
31
45
|
|
46
|
+
# Convert yards to inches.
|
47
|
+
#
|
48
|
+
# @param yd [Number]
|
49
|
+
# @return [Number]
|
32
50
|
def yd2in(yd)
|
33
51
|
yd * 36
|
34
52
|
end
|
35
53
|
|
36
54
|
# PostscriptPoint-converisons
|
55
|
+
|
56
|
+
# Convert points to points. For completeness.
|
57
|
+
#
|
58
|
+
# @param pt [Number]
|
59
|
+
# @return [Number]
|
37
60
|
def pt2pt(pt)
|
38
61
|
pt
|
39
62
|
end
|
40
63
|
|
64
|
+
# Convert inches to points.
|
65
|
+
#
|
66
|
+
# @param inch [Number]
|
67
|
+
# @return [Number]
|
41
68
|
def in2pt(inch)
|
42
69
|
inch * 72
|
43
70
|
end
|
44
71
|
|
72
|
+
# Convert feet to points.
|
73
|
+
#
|
74
|
+
# @param ft [Number]
|
75
|
+
# @return [Number]
|
45
76
|
def ft2pt(ft)
|
46
77
|
in2pt(ft2in(ft))
|
47
78
|
end
|
48
79
|
|
80
|
+
# Convert yards to points.
|
81
|
+
#
|
82
|
+
# @param yd [Number]
|
83
|
+
# @return [Number]
|
49
84
|
def yd2pt(yd)
|
50
85
|
in2pt(yd2in(yd))
|
51
86
|
end
|
52
87
|
|
88
|
+
# Convert millimeters to points.
|
89
|
+
#
|
90
|
+
# @param mm [Number]
|
91
|
+
# @return [Number]
|
53
92
|
def mm2pt(mm)
|
54
93
|
mm * (72 / 25.4)
|
55
94
|
end
|
56
95
|
|
96
|
+
# Convert centimeters to points.
|
97
|
+
#
|
98
|
+
# @param cm [Number]
|
99
|
+
# @return [Number]
|
57
100
|
def cm2pt(cm)
|
58
101
|
mm2pt(cm2mm(cm))
|
59
102
|
end
|
60
103
|
|
104
|
+
# Convert decimeters to points.
|
105
|
+
#
|
106
|
+
# @param dm [Number]
|
107
|
+
# @return [Number]
|
61
108
|
def dm2pt(dm)
|
62
109
|
mm2pt(dm2mm(dm))
|
63
110
|
end
|
64
111
|
|
112
|
+
# Convert meters to points.
|
113
|
+
#
|
114
|
+
# @param m [Number]
|
115
|
+
# @return [Number]
|
65
116
|
def m2pt(m)
|
66
117
|
mm2pt(m2mm(m))
|
67
118
|
end
|
68
119
|
|
120
|
+
# Convert points to millimeters.
|
121
|
+
#
|
122
|
+
# @param pt [Number]
|
123
|
+
# @return [Number]
|
69
124
|
def pt2mm(pt)
|
70
125
|
pt * 1 / mm2pt(1) # (25.4 / 72)
|
71
126
|
end
|