prawn 2.0.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 +3 -9
- data/Rakefile +20 -23
- data/lib/prawn.rb +37 -49
- data/lib/prawn/document.rb +181 -133
- data/lib/prawn/document/bounding_box.rb +41 -29
- data/lib/prawn/document/column_box.rb +7 -7
- data/lib/prawn/document/internals.rb +18 -8
- data/lib/prawn/document/span.rb +21 -16
- data/lib/prawn/encoding.rb +69 -68
- data/lib/prawn/errors.rb +12 -7
- data/lib/prawn/font.rb +115 -69
- data/lib/prawn/font_metric_cache.rb +14 -8
- data/lib/prawn/{font → fonts}/afm.rb +102 -68
- data/lib/prawn/{font → fonts}/dfont.rb +5 -11
- data/lib/prawn/fonts/otf.rb +11 -0
- data/lib/prawn/fonts/ttc.rb +36 -0
- data/lib/prawn/{font → fonts}/ttf.rb +87 -68
- data/lib/prawn/graphics.rb +120 -80
- data/lib/prawn/graphics/blend_mode.rb +65 -0
- data/lib/prawn/graphics/cap_style.rb +3 -3
- data/lib/prawn/graphics/color.rb +27 -25
- data/lib/prawn/graphics/dash.rb +23 -11
- data/lib/prawn/graphics/join_style.rb +9 -3
- data/lib/prawn/graphics/patterns.rb +197 -67
- data/lib/prawn/graphics/transformation.rb +17 -8
- data/lib/prawn/graphics/transparency.rb +17 -13
- data/lib/prawn/grid.rb +48 -47
- data/lib/prawn/image_handler.rb +5 -5
- data/lib/prawn/images.rb +39 -30
- data/lib/prawn/images/image.rb +2 -1
- data/lib/prawn/images/jpg.rb +28 -22
- data/lib/prawn/images/png.rb +107 -66
- data/lib/prawn/measurement_extensions.rb +10 -9
- data/lib/prawn/measurements.rb +19 -15
- data/lib/prawn/outline.rb +97 -77
- data/lib/prawn/repeater.rb +14 -10
- data/lib/prawn/security.rb +81 -61
- data/lib/prawn/security/arcfour.rb +2 -2
- data/lib/prawn/soft_mask.rb +26 -26
- data/lib/prawn/stamp.rb +20 -13
- data/lib/prawn/text.rb +68 -52
- data/lib/prawn/text/box.rb +11 -8
- data/lib/prawn/text/formatted.rb +5 -5
- data/lib/prawn/text/formatted/arranger.rb +78 -49
- data/lib/prawn/text/formatted/box.rb +134 -100
- data/lib/prawn/text/formatted/fragment.rb +11 -14
- data/lib/prawn/text/formatted/line_wrap.rb +121 -63
- data/lib/prawn/text/formatted/parser.rb +139 -117
- data/lib/prawn/text/formatted/wrap.rb +43 -31
- data/lib/prawn/transformation_stack.rb +44 -0
- data/lib/prawn/utilities.rb +7 -22
- data/lib/prawn/version.rb +2 -2
- data/lib/prawn/view.rb +17 -7
- data/manual/basic_concepts/adding_pages.rb +6 -7
- data/manual/basic_concepts/basic_concepts.rb +31 -22
- data/manual/basic_concepts/creation.rb +10 -11
- data/manual/basic_concepts/cursor.rb +4 -5
- data/manual/basic_concepts/measurement.rb +6 -7
- data/manual/basic_concepts/origin.rb +5 -6
- data/manual/basic_concepts/other_cursor_helpers.rb +11 -12
- data/manual/basic_concepts/view.rb +22 -16
- data/manual/bounding_box/bounding_box.rb +29 -24
- data/manual/bounding_box/bounds.rb +11 -12
- data/manual/bounding_box/canvas.rb +4 -5
- data/manual/bounding_box/creation.rb +6 -7
- data/manual/bounding_box/indentation.rb +14 -15
- data/manual/bounding_box/nesting.rb +24 -17
- data/manual/bounding_box/russian_boxes.rb +14 -13
- data/manual/bounding_box/stretchy.rb +12 -13
- data/manual/contents.rb +28 -22
- data/manual/cover.rb +33 -28
- data/manual/document_and_page_options/background.rb +11 -13
- data/manual/document_and_page_options/document_and_page_options.rb +25 -20
- data/manual/document_and_page_options/metadata.rb +18 -16
- data/manual/document_and_page_options/page_margins.rb +18 -20
- data/manual/document_and_page_options/page_size.rb +13 -12
- data/manual/document_and_page_options/print_scaling.rb +17 -15
- data/manual/example_helper.rb +5 -4
- data/manual/graphics/blend_mode.rb +52 -0
- data/manual/graphics/circle_and_ellipse.rb +4 -5
- data/manual/graphics/color.rb +7 -9
- data/manual/graphics/common_lines.rb +7 -8
- data/manual/graphics/fill_and_stroke.rb +4 -5
- data/manual/graphics/fill_rules.rb +9 -10
- data/manual/graphics/gradients.rb +27 -21
- data/manual/graphics/graphics.rb +48 -39
- data/manual/graphics/helper.rb +12 -9
- data/manual/graphics/line_width.rb +8 -7
- data/manual/graphics/lines_and_curves.rb +7 -8
- data/manual/graphics/polygon.rb +6 -8
- data/manual/graphics/rectangle.rb +4 -5
- data/manual/graphics/rotate.rb +6 -7
- data/manual/graphics/scale.rb +14 -15
- data/manual/graphics/soft_masks.rb +4 -5
- data/manual/graphics/stroke_cap.rb +6 -7
- data/manual/graphics/stroke_dash.rb +11 -12
- data/manual/graphics/stroke_join.rb +5 -6
- data/manual/graphics/translate.rb +9 -10
- data/manual/graphics/transparency.rb +7 -8
- data/manual/how_to_read_this_manual.rb +6 -6
- data/manual/images/absolute_position.rb +6 -7
- data/manual/images/fit.rb +7 -8
- data/manual/images/horizontal.rb +9 -10
- data/manual/images/images.rb +28 -24
- data/manual/images/plain_image.rb +5 -6
- data/manual/images/scale.rb +9 -10
- data/manual/images/vertical.rb +13 -14
- data/manual/images/width_and_height.rb +10 -11
- data/manual/layout/boxes.rb +5 -6
- data/manual/layout/content.rb +7 -8
- data/manual/layout/layout.rb +18 -16
- data/manual/layout/simple_grid.rb +6 -7
- data/manual/outline/add_subsection_to.rb +20 -21
- data/manual/outline/insert_section_after.rb +15 -16
- data/manual/outline/outline.rb +21 -17
- data/manual/outline/sections_and_pages.rb +17 -18
- data/manual/repeatable_content/alternate_page_numbering.rb +21 -17
- data/manual/repeatable_content/page_numbering.rb +17 -16
- data/manual/repeatable_content/repeatable_content.rb +25 -19
- data/manual/repeatable_content/repeater.rb +14 -15
- data/manual/repeatable_content/stamp.rb +14 -15
- data/manual/security/encryption.rb +9 -10
- data/manual/security/permissions.rb +19 -14
- data/manual/security/security.rb +19 -16
- data/manual/table.rb +3 -3
- data/manual/text/alignment.rb +16 -17
- data/manual/text/color.rb +12 -11
- data/manual/text/column_box.rb +9 -10
- data/manual/text/fallback_fonts.rb +25 -21
- data/manual/text/font.rb +11 -12
- data/manual/text/font_size.rb +13 -14
- data/manual/text/font_style.rb +7 -8
- data/manual/text/formatted_callbacks.rb +25 -21
- data/manual/text/formatted_text.rb +33 -25
- data/manual/text/free_flowing_text.rb +20 -21
- data/manual/text/inline.rb +18 -19
- data/manual/text/kerning_and_character_spacing.rb +14 -15
- data/manual/text/leading.rb +7 -8
- data/manual/text/line_wrapping.rb +37 -18
- data/manual/text/paragraph_indentation.rb +13 -14
- data/manual/text/positioned_text.rb +15 -16
- data/manual/text/registering_families.rb +20 -21
- data/manual/text/rendering_and_color.rb +9 -10
- data/manual/text/right_to_left_text.rb +26 -19
- data/manual/text/rotation.rb +28 -23
- data/manual/text/single_usage.rb +8 -9
- data/manual/text/text.rb +57 -52
- data/manual/text/text_box_excess.rb +20 -17
- data/manual/text/text_box_extensions.rb +18 -15
- data/manual/text/text_box_overflow.rb +18 -19
- data/manual/text/utf8.rb +11 -12
- data/manual/text/win_ansi_charset.rb +21 -19
- data/prawn.gemspec +45 -33
- data/spec/extensions/encoding_helpers.rb +3 -3
- data/spec/prawn/document/bounding_box_spec.rb +546 -0
- data/spec/prawn/document/column_box_spec.rb +75 -0
- data/spec/prawn/document/security_spec.rb +176 -0
- data/spec/prawn/document_annotations_spec.rb +76 -0
- data/spec/prawn/document_destinations_spec.rb +15 -0
- data/spec/prawn/document_grid_spec.rb +99 -0
- data/spec/prawn/document_reference_spec.rb +27 -0
- data/spec/prawn/document_span_spec.rb +36 -0
- data/spec/prawn/document_spec.rb +802 -0
- data/spec/prawn/font_metric_cache_spec.rb +54 -0
- data/spec/prawn/font_spec.rb +542 -0
- data/spec/prawn/graphics/blend_mode_spec.rb +63 -0
- data/spec/prawn/graphics/transparency_spec.rb +81 -0
- data/spec/prawn/graphics_spec.rb +837 -0
- data/spec/prawn/graphics_stroke_styles_spec.rb +229 -0
- data/spec/prawn/image_handler_spec.rb +53 -0
- data/spec/prawn/images/jpg_spec.rb +20 -0
- data/spec/prawn/images/png_spec.rb +283 -0
- data/spec/prawn/images_spec.rb +224 -0
- data/spec/prawn/measurements_extensions_spec.rb +24 -0
- data/spec/prawn/outline_spec.rb +412 -0
- data/spec/prawn/repeater_spec.rb +165 -0
- data/spec/prawn/soft_mask_spec.rb +74 -0
- data/spec/prawn/stamp_spec.rb +172 -0
- data/spec/prawn/text/box_spec.rb +1112 -0
- data/spec/prawn/text/formatted/arranger_spec.rb +466 -0
- data/spec/prawn/text/formatted/box_spec.rb +846 -0
- data/spec/prawn/text/formatted/fragment_spec.rb +343 -0
- data/spec/prawn/text/formatted/line_wrap_spec.rb +494 -0
- data/spec/prawn/text/formatted/parser_spec.rb +697 -0
- data/spec/prawn/text_draw_text_spec.rb +149 -0
- data/spec/prawn/text_rendering_mode_spec.rb +48 -0
- data/spec/prawn/text_spacing_spec.rb +95 -0
- data/spec/prawn/text_spec.rb +603 -0
- data/spec/prawn/text_with_inline_formatting_spec.rb +35 -0
- data/spec/prawn/transformation_stack_spec.rb +66 -0
- data/spec/prawn/view_spec.rb +63 -0
- data/spec/prawn_manual_spec.rb +35 -0
- data/spec/spec_helper.rb +19 -23
- metadata +145 -185
- metadata.gz.sig +4 -0
- data/data/images/16bit.alpha +0 -0
- data/data/images/16bit.color +0 -0
- data/data/images/16bit.png +0 -0
- data/data/images/arrow.png +0 -0
- data/data/images/arrow2.png +0 -0
- data/data/images/dice.alpha +0 -0
- data/data/images/dice.color +0 -0
- data/data/images/dice.png +0 -0
- data/data/images/dice_interlaced.png +0 -0
- data/data/images/fractal.jpg +0 -0
- data/data/images/indexed_color.dat +0 -0
- data/data/images/indexed_color.png +0 -0
- data/data/images/letterhead.jpg +0 -0
- data/data/images/license.md +0 -8
- data/data/images/page_white_text.alpha +0 -0
- data/data/images/page_white_text.color +0 -0
- data/data/images/page_white_text.png +0 -0
- data/data/images/pal_bk.png +0 -0
- data/data/images/pigs.jpg +0 -0
- data/data/images/prawn.png +0 -0
- data/data/images/ruport.png +0 -0
- data/data/images/ruport_data.dat +0 -0
- data/data/images/ruport_transparent.png +0 -0
- data/data/images/ruport_type0.png +0 -0
- data/data/images/stef.jpg +0 -0
- data/data/images/tru256.bmp +0 -0
- data/data/images/web-links.dat +0 -1
- data/data/images/web-links.png +0 -0
- data/data/pdfs/complex_template.pdf +0 -0
- data/data/pdfs/contains_ttf_font.pdf +0 -0
- data/data/pdfs/encrypted.pdf +0 -0
- data/data/pdfs/form.pdf +1 -819
- data/data/pdfs/hexagon.pdf +0 -61
- data/data/pdfs/indirect_reference.pdf +0 -86
- data/data/pdfs/multipage_template.pdf +0 -127
- data/data/pdfs/nested_pages.pdf +0 -118
- data/data/pdfs/page_without_mediabox.pdf +0 -193
- data/data/pdfs/resources_as_indirect_object.pdf +0 -83
- data/data/pdfs/two_hexagons.pdf +0 -90
- data/data/pdfs/version_1_6.pdf +0 -61
- data/data/shift_jis_text.txt +0 -1
- data/spec/acceptance/png.rb +0 -24
- data/spec/annotations_spec.rb +0 -67
- data/spec/bounding_box_spec.rb +0 -501
- data/spec/column_box_spec.rb +0 -59
- data/spec/destinations_spec.rb +0 -13
- data/spec/document_spec.rb +0 -742
- data/spec/extensions/mocha.rb +0 -45
- data/spec/font_metric_cache_spec.rb +0 -52
- data/spec/font_spec.rb +0 -475
- data/spec/formatted_text_arranger_spec.rb +0 -423
- data/spec/formatted_text_box_spec.rb +0 -716
- data/spec/formatted_text_fragment_spec.rb +0 -299
- data/spec/graphics_spec.rb +0 -666
- data/spec/grid_spec.rb +0 -95
- data/spec/image_handler_spec.rb +0 -53
- data/spec/images_spec.rb +0 -167
- data/spec/inline_formatted_text_parser_spec.rb +0 -568
- data/spec/jpg_spec.rb +0 -23
- data/spec/line_wrap_spec.rb +0 -366
- data/spec/measurement_units_spec.rb +0 -22
- data/spec/outline_spec.rb +0 -409
- data/spec/png_spec.rb +0 -235
- data/spec/reference_spec.rb +0 -25
- data/spec/repeater_spec.rb +0 -154
- data/spec/security_spec.rb +0 -151
- data/spec/soft_mask_spec.rb +0 -78
- data/spec/span_spec.rb +0 -43
- data/spec/stamp_spec.rb +0 -179
- data/spec/stroke_styles_spec.rb +0 -208
- data/spec/text_at_spec.rb +0 -142
- data/spec/text_box_spec.rb +0 -1038
- data/spec/text_rendering_mode_spec.rb +0 -45
- data/spec/text_spacing_spec.rb +0 -93
- data/spec/text_spec.rb +0 -549
- data/spec/text_with_inline_formatting_spec.rb +0 -35
- data/spec/transparency_spec.rb +0 -91
- data/spec/view_spec.rb +0 -42
data/lib/prawn/images/image.rb
CHANGED
data/lib/prawn/images/jpg.rb
CHANGED
@@ -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.
|
@@ -19,10 +21,13 @@ module Prawn
|
|
19
21
|
attr_reader :width, :height, :bits, :channels
|
20
22
|
attr_accessor :scaled_width, :scaled_height
|
21
23
|
|
22
|
-
JPEG_SOF_BLOCKS = [
|
24
|
+
JPEG_SOF_BLOCKS = [
|
25
|
+
0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6, 0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE,
|
26
|
+
0xCF
|
27
|
+
].freeze
|
23
28
|
|
24
29
|
def self.can_render?(image_blob)
|
25
|
-
image_blob[0, 3].unpack(
|
30
|
+
image_blob[0, 3].unpack('C*') == [255, 216, 255]
|
26
31
|
end
|
27
32
|
|
28
33
|
# Process a new JPG image
|
@@ -35,13 +40,13 @@ module Prawn
|
|
35
40
|
d.binmode
|
36
41
|
|
37
42
|
c_marker = 0xff # Section marker.
|
38
|
-
d.seek(2)
|
43
|
+
d.seek(2) # Skip the first two bytes of JPEG identifier.
|
39
44
|
loop do
|
40
45
|
marker, code, length = d.read(4).unpack('CCn')
|
41
|
-
|
46
|
+
raise 'JPEG marker not found!' if marker != c_marker
|
42
47
|
|
43
48
|
if JPEG_SOF_BLOCKS.include?(code)
|
44
|
-
@bits, @height, @width, @channels = d.read(6).unpack(
|
49
|
+
@bits, @height, @width, @channels = d.read(6).unpack('CnnC')
|
45
50
|
break
|
46
51
|
end
|
47
52
|
|
@@ -53,31 +58,32 @@ module Prawn
|
|
53
58
|
# a Reference to it.
|
54
59
|
#
|
55
60
|
def build_pdf_object(document)
|
56
|
-
color_space =
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
61
|
+
color_space =
|
62
|
+
case channels
|
63
|
+
when 1
|
64
|
+
:DeviceGray
|
65
|
+
when 3
|
66
|
+
:DeviceRGB
|
67
|
+
when 4
|
68
|
+
:DeviceCMYK
|
69
|
+
else
|
70
|
+
raise ArgumentError, 'JPG uses an unsupported number of channels'
|
71
|
+
end
|
66
72
|
|
67
73
|
obj = document.ref!(
|
68
|
-
:
|
69
|
-
:
|
70
|
-
:
|
71
|
-
:
|
72
|
-
:
|
73
|
-
:
|
74
|
+
Type: :XObject,
|
75
|
+
Subtype: :Image,
|
76
|
+
ColorSpace: color_space,
|
77
|
+
BitsPerComponent: bits,
|
78
|
+
Width: width,
|
79
|
+
Height: height
|
74
80
|
)
|
75
81
|
|
76
82
|
# add extra decode params for CMYK images. By swapping the
|
77
83
|
# min and max values from the default, we invert the colours. See
|
78
84
|
# section 4.8.4 of the spec.
|
79
85
|
if color_space == :DeviceCMYK
|
80
|
-
obj.data[:Decode] = [
|
86
|
+
obj.data[:Decode] = [1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0]
|
81
87
|
end
|
82
88
|
|
83
89
|
obj.stream << @data
|
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
|
@@ -26,7 +26,7 @@ module Prawn
|
|
26
26
|
attr_accessor :scaled_width, :scaled_height
|
27
27
|
|
28
28
|
def self.can_render?(image_blob)
|
29
|
-
image_blob[0, 8].unpack(
|
29
|
+
image_blob[0, 8].unpack('C*') == [137, 80, 78, 71, 13, 10, 26, 10]
|
30
30
|
end
|
31
31
|
|
32
32
|
# Process a new PNG image
|
@@ -36,28 +36,28 @@ module Prawn
|
|
36
36
|
def initialize(data)
|
37
37
|
data = StringIO.new(data.dup)
|
38
38
|
|
39
|
-
data.read(8)
|
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
|
47
|
-
section
|
46
|
+
chunk_size = data.read(4).unpack1('N')
|
47
|
+
section = data.read(4)
|
48
48
|
case section
|
49
49
|
when 'IHDR'
|
50
50
|
# we can grab other interesting values from here (like width,
|
51
51
|
# height, etc)
|
52
|
-
values = data.read(chunk_size).unpack(
|
52
|
+
values = data.read(chunk_size).unpack('NNCCCCC')
|
53
53
|
|
54
|
-
@width
|
55
|
-
@height
|
56
|
-
@bits
|
57
|
-
@color_type
|
54
|
+
@width = values[0]
|
55
|
+
@height = values[1]
|
56
|
+
@bits = values[2]
|
57
|
+
@color_type = values[3]
|
58
58
|
@compression_method = values[4]
|
59
|
-
@filter_method
|
60
|
-
@interlace_method
|
59
|
+
@filter_method = values[5]
|
60
|
+
@interlace_method = values[6]
|
61
61
|
when 'PLTE'
|
62
62
|
@palette << data.read(chunk_size)
|
63
63
|
when 'IDAT'
|
@@ -68,17 +68,15 @@ module Prawn
|
|
68
68
|
@transparency = {}
|
69
69
|
case @color_type
|
70
70
|
when 3
|
71
|
-
|
72
|
-
"Pallete-based transparency in PNG is not currently supported.\n" \
|
73
|
-
"See https://github.com/prawnpdf/prawn/issues/783"
|
71
|
+
@transparency[:palette] = data.read(chunk_size).unpack('C*')
|
74
72
|
when 0
|
75
73
|
# Greyscale. Corresponding to entries in the PLTE chunk.
|
76
74
|
# Grey is two bytes, range 0 .. (2 ^ bit-depth) - 1
|
77
|
-
grayval = data.read(chunk_size).
|
75
|
+
grayval = data.read(chunk_size).unpack1('n')
|
78
76
|
@transparency[:grayscale] = grayval
|
79
77
|
when 2
|
80
78
|
# True colour with proper alpha channel.
|
81
|
-
@transparency[:rgb] = data.read(chunk_size).unpack(
|
79
|
+
@transparency[:rgb] = data.read(chunk_size).unpack('nnn')
|
82
80
|
end
|
83
81
|
when 'IEND'
|
84
82
|
# we've got everything we need, exit the loop
|
@@ -88,7 +86,7 @@ module Prawn
|
|
88
86
|
data.seek(data.pos + chunk_size)
|
89
87
|
end
|
90
88
|
|
91
|
-
data.read(4)
|
89
|
+
data.read(4) # Skip the CRC
|
92
90
|
end
|
93
91
|
|
94
92
|
@img_data = Zlib::Inflate.inflate(@img_data)
|
@@ -97,11 +95,11 @@ module Prawn
|
|
97
95
|
# number of color components to each pixel
|
98
96
|
#
|
99
97
|
def colors
|
100
|
-
case
|
98
|
+
case color_type
|
101
99
|
when 0, 3, 4
|
102
|
-
|
100
|
+
1
|
103
101
|
when 2, 6
|
104
|
-
|
102
|
+
3
|
105
103
|
end
|
106
104
|
end
|
107
105
|
|
@@ -109,11 +107,20 @@ module Prawn
|
|
109
107
|
# where it's required.
|
110
108
|
#
|
111
109
|
def split_alpha_channel!
|
112
|
-
|
110
|
+
if alpha_channel?
|
111
|
+
if color_type == 3
|
112
|
+
generate_alpha_channel
|
113
|
+
else
|
114
|
+
split_image_data
|
115
|
+
end
|
116
|
+
end
|
113
117
|
end
|
114
118
|
|
115
119
|
def alpha_channel?
|
116
|
-
|
120
|
+
return true if color_type == 4 || color_type == 6
|
121
|
+
return @transparency.any? if color_type == 3
|
122
|
+
|
123
|
+
false
|
117
124
|
end
|
118
125
|
|
119
126
|
# Build a PDF object representing this image in +document+, and return
|
@@ -121,18 +128,18 @@ module Prawn
|
|
121
128
|
#
|
122
129
|
def build_pdf_object(document)
|
123
130
|
if compression_method != 0
|
124
|
-
|
125
|
-
|
131
|
+
raise Errors::UnsupportedImageType,
|
132
|
+
'PNG uses an unsupported compression method'
|
126
133
|
end
|
127
134
|
|
128
135
|
if filter_method != 0
|
129
|
-
|
130
|
-
|
136
|
+
raise Errors::UnsupportedImageType,
|
137
|
+
'PNG uses an unsupported filter method'
|
131
138
|
end
|
132
139
|
|
133
140
|
if interlace_method != 0
|
134
|
-
|
135
|
-
|
141
|
+
raise Errors::UnsupportedImageType,
|
142
|
+
'PNG uses unsupported interlace method'
|
136
143
|
end
|
137
144
|
|
138
145
|
# some PNG types store the colour and alpha channel data together,
|
@@ -145,28 +152,28 @@ module Prawn
|
|
145
152
|
when 3
|
146
153
|
color = :DeviceRGB
|
147
154
|
else
|
148
|
-
|
149
|
-
|
155
|
+
raise Errors::UnsupportedImageType,
|
156
|
+
"PNG uses an unsupported number of colors (#{png.colors})"
|
150
157
|
end
|
151
158
|
|
152
159
|
# build the image dict
|
153
160
|
obj = document.ref!(
|
154
|
-
:
|
155
|
-
:
|
156
|
-
:
|
157
|
-
:
|
158
|
-
:
|
161
|
+
Type: :XObject,
|
162
|
+
Subtype: :Image,
|
163
|
+
Height: height,
|
164
|
+
Width: width,
|
165
|
+
BitsPerComponent: bits
|
159
166
|
)
|
160
167
|
|
161
168
|
# append the actual image data to the object as a stream
|
162
169
|
obj << img_data
|
163
170
|
|
164
171
|
obj.stream.filters << {
|
165
|
-
:
|
166
|
-
:
|
167
|
-
:
|
168
|
-
:
|
169
|
-
:
|
172
|
+
FlateDecode: {
|
173
|
+
Predictor: 15,
|
174
|
+
Colors: colors,
|
175
|
+
BitsPerComponent: bits,
|
176
|
+
Columns: width
|
170
177
|
}
|
171
178
|
}
|
172
179
|
|
@@ -179,10 +186,12 @@ module Prawn
|
|
179
186
|
palette_obj << palette
|
180
187
|
|
181
188
|
# build the color space array for the image
|
182
|
-
obj.data[:ColorSpace] = [
|
183
|
-
|
184
|
-
|
185
|
-
|
189
|
+
obj.data[:ColorSpace] = [
|
190
|
+
:Indexed,
|
191
|
+
:DeviceRGB,
|
192
|
+
(palette.size / 3) - 1,
|
193
|
+
palette_obj
|
194
|
+
]
|
186
195
|
end
|
187
196
|
|
188
197
|
# *************************************
|
@@ -206,27 +215,28 @@ module Prawn
|
|
206
215
|
obj.data[:Mask] = rgb.collect { |x| [x, x] }.flatten
|
207
216
|
end
|
208
217
|
|
209
|
-
# For PNG color types 4 and 6, the transparency data is stored as
|
210
|
-
# channel mixed in with the main image data. The PNG class
|
211
|
-
# it out for us and makes it available via the alpha_channel
|
218
|
+
# For PNG color types 4 and 6, the transparency data is stored as
|
219
|
+
# a alpha channel mixed in with the main image data. The PNG class
|
220
|
+
# seperates it out for us and makes it available via the alpha_channel
|
221
|
+
# attribute
|
212
222
|
if alpha_channel?
|
213
223
|
smask_obj = document.ref!(
|
214
|
-
:
|
215
|
-
:
|
216
|
-
:
|
217
|
-
:
|
218
|
-
:
|
219
|
-
:
|
220
|
-
:
|
224
|
+
Type: :XObject,
|
225
|
+
Subtype: :Image,
|
226
|
+
Height: height,
|
227
|
+
Width: width,
|
228
|
+
BitsPerComponent: bits,
|
229
|
+
ColorSpace: :DeviceGray,
|
230
|
+
Decode: [0, 1]
|
221
231
|
)
|
222
232
|
smask_obj.stream << alpha_channel
|
223
233
|
|
224
234
|
smask_obj.stream.filters << {
|
225
|
-
:
|
226
|
-
:
|
227
|
-
:
|
228
|
-
:
|
229
|
-
:
|
235
|
+
FlateDecode: {
|
236
|
+
Predictor: 15,
|
237
|
+
Colors: 1,
|
238
|
+
BitsPerComponent: bits,
|
239
|
+
Columns: width
|
230
240
|
}
|
231
241
|
}
|
232
242
|
obj.data[:SMask] = smask_obj
|
@@ -254,9 +264,9 @@ module Prawn
|
|
254
264
|
alpha_bytes = bits / 8
|
255
265
|
color_bytes = colors * bits / 8
|
256
266
|
|
257
|
-
scanline_length
|
267
|
+
scanline_length = (color_bytes + alpha_bytes) * width + 1
|
258
268
|
scanlines = @img_data.bytesize / scanline_length
|
259
|
-
pixels =
|
269
|
+
pixels = width * height
|
260
270
|
|
261
271
|
data = StringIO.new(@img_data)
|
262
272
|
data.binmode
|
@@ -277,7 +287,7 @@ module Prawn
|
|
277
287
|
color.putc filter
|
278
288
|
alpha.putc filter
|
279
289
|
|
280
|
-
|
290
|
+
width.times do
|
281
291
|
color.write data.read(color_bytes)
|
282
292
|
alpha.write data.read(alpha_bytes)
|
283
293
|
end
|
@@ -285,6 +295,37 @@ module Prawn
|
|
285
295
|
|
286
296
|
@img_data = color_data
|
287
297
|
end
|
298
|
+
|
299
|
+
def generate_alpha_channel
|
300
|
+
alpha_palette = Hash.new(0xff)
|
301
|
+
0.upto(palette.bytesize / 3) do |n|
|
302
|
+
alpha_palette[n] = @transparency[:palette][n] || 0xff
|
303
|
+
end
|
304
|
+
|
305
|
+
scanline_length = width + 1
|
306
|
+
scanlines = @img_data.bytesize / scanline_length
|
307
|
+
pixels = width * height
|
308
|
+
|
309
|
+
data = StringIO.new(@img_data)
|
310
|
+
data.binmode
|
311
|
+
|
312
|
+
@alpha_channel = [0x00].pack('C') * (pixels + scanlines)
|
313
|
+
alpha = StringIO.new(@alpha_channel)
|
314
|
+
alpha.binmode
|
315
|
+
|
316
|
+
scanlines.times do |line|
|
317
|
+
data.seek(line * scanline_length)
|
318
|
+
|
319
|
+
filter = data.getbyte
|
320
|
+
|
321
|
+
alpha.putc filter
|
322
|
+
|
323
|
+
width.times do
|
324
|
+
color = data.read(1).unpack1('C')
|
325
|
+
alpha.putc alpha_palette[color]
|
326
|
+
end
|
327
|
+
end
|
328
|
+
end
|
288
329
|
end
|
289
330
|
end
|
290
331
|
end
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
# measurement_extensions.rb: Core extensions for Prawn::Measurements
|
3
4
|
#
|
4
5
|
# Copyright December 2008, Florian Witteler. All Rights Reserved.
|
@@ -17,34 +18,34 @@ class Numeric
|
|
17
18
|
# @group Experimental API
|
18
19
|
|
19
20
|
def mm
|
20
|
-
|
21
|
+
mm2pt(self)
|
21
22
|
end
|
22
23
|
|
23
24
|
def cm
|
24
|
-
|
25
|
+
cm2pt(self)
|
25
26
|
end
|
26
27
|
|
27
28
|
def dm
|
28
|
-
|
29
|
+
dm2pt(self)
|
29
30
|
end
|
30
31
|
|
31
32
|
def m
|
32
|
-
|
33
|
+
m2pt(self)
|
33
34
|
end
|
34
35
|
|
35
36
|
def in
|
36
|
-
|
37
|
+
in2pt(self)
|
37
38
|
end
|
38
39
|
|
39
40
|
def yd
|
40
|
-
|
41
|
+
yd2pt(self)
|
41
42
|
end
|
42
43
|
|
43
44
|
def ft
|
44
|
-
|
45
|
+
ft2pt(self)
|
45
46
|
end
|
46
47
|
|
47
48
|
def pt
|
48
|
-
|
49
|
+
pt2pt(self)
|
49
50
|
end
|
50
51
|
end
|