prawn 0.13.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data/.yardopts +10 -0
- data/GPLv2 +20 -21
- data/Gemfile +3 -16
- data/Rakefile +17 -39
- data/lib/prawn/document/bounding_box.rb +85 -42
- data/lib/prawn/document/column_box.rb +21 -11
- data/lib/prawn/document/internals.rb +40 -147
- data/lib/prawn/document/span.rb +25 -17
- data/lib/prawn/document.rb +286 -245
- data/lib/prawn/encoding.rb +68 -101
- data/lib/prawn/errors.rb +47 -43
- data/lib/prawn/font.rb +204 -155
- data/lib/prawn/font_metric_cache.rb +25 -21
- data/lib/prawn/fonts/afm.rb +292 -0
- data/lib/prawn/{font → fonts}/dfont.rb +7 -13
- data/lib/prawn/fonts/otf.rb +11 -0
- data/lib/prawn/fonts/ttc.rb +36 -0
- data/lib/prawn/{font → fonts}/ttf.rb +142 -80
- data/lib/prawn/graphics/blend_mode.rb +65 -0
- data/lib/prawn/graphics/cap_style.rb +6 -5
- data/lib/prawn/graphics/color.rb +47 -44
- data/lib/prawn/graphics/dash.rb +30 -13
- data/lib/prawn/graphics/join_style.rb +13 -6
- data/lib/prawn/graphics/patterns.rb +221 -90
- data/lib/prawn/graphics/transformation.rb +21 -12
- data/lib/prawn/graphics/transparency.rb +21 -17
- data/lib/prawn/graphics.rb +155 -128
- data/lib/prawn/{layout/grid.rb → grid.rb} +110 -47
- data/lib/prawn/image_handler.rb +16 -2
- data/lib/prawn/images/image.rb +4 -2
- data/lib/prawn/images/jpg.rb +39 -30
- data/lib/prawn/images/png.rb +132 -169
- data/lib/prawn/images.rb +70 -62
- data/lib/prawn/measurement_extensions.rb +15 -10
- data/lib/prawn/measurements.rb +22 -23
- data/lib/prawn/outline.rb +301 -13
- data/lib/prawn/repeater.rb +19 -17
- data/lib/prawn/security/arcfour.rb +54 -0
- data/lib/prawn/security.rb +108 -86
- data/lib/prawn/soft_mask.rb +40 -41
- data/lib/prawn/stamp.rb +29 -12
- data/lib/prawn/text/box.rb +27 -29
- data/lib/prawn/text/formatted/arranger.rb +110 -67
- data/lib/prawn/text/formatted/box.rb +233 -165
- data/lib/prawn/text/formatted/fragment.rb +27 -27
- data/lib/prawn/text/formatted/line_wrap.rb +137 -97
- data/lib/prawn/text/formatted/parser.rb +149 -127
- data/lib/prawn/text/formatted/wrap.rb +57 -37
- data/lib/prawn/text/formatted.rb +6 -4
- data/lib/prawn/text.rb +105 -73
- data/lib/prawn/transformation_stack.rb +44 -0
- data/lib/prawn/utilities.rb +11 -21
- data/lib/prawn/version.rb +5 -0
- data/lib/prawn/view.rb +101 -0
- data/lib/prawn.rb +42 -68
- data/{data/images/fractal.jpg → manual/absolute_position.pdf} +0 -0
- data/manual/basic_concepts/adding_pages.rb +9 -10
- data/manual/basic_concepts/basic_concepts.rb +33 -24
- data/manual/basic_concepts/creation.rb +10 -11
- data/manual/basic_concepts/cursor.rb +9 -10
- data/manual/basic_concepts/measurement.rb +10 -11
- data/manual/basic_concepts/origin.rb +8 -9
- data/manual/basic_concepts/other_cursor_helpers.rb +17 -18
- data/manual/basic_concepts/view.rb +48 -0
- data/manual/bounding_box/bounding_box.rb +31 -29
- data/manual/bounding_box/bounds.rb +17 -18
- data/manual/bounding_box/canvas.rb +8 -9
- data/manual/bounding_box/creation.rb +8 -9
- data/manual/bounding_box/indentation.rb +22 -23
- data/manual/bounding_box/nesting.rb +32 -25
- data/manual/bounding_box/russian_boxes.rb +19 -19
- data/manual/bounding_box/stretchy.rb +18 -20
- data/manual/contents.rb +35 -0
- data/manual/cover.rb +43 -0
- data/manual/document_and_page_options/background.rb +16 -14
- data/manual/document_and_page_options/document_and_page_options.rb +26 -23
- data/manual/document_and_page_options/metadata.rb +21 -19
- data/manual/document_and_page_options/page_margins.rb +20 -22
- data/manual/document_and_page_options/page_size.rb +15 -15
- data/manual/document_and_page_options/print_scaling.rb +23 -0
- data/manual/example_helper.rb +5 -408
- data/manual/graphics/blend_mode.rb +52 -0
- data/manual/graphics/circle_and_ellipse.rb +8 -9
- data/manual/graphics/color.rb +11 -13
- data/manual/graphics/common_lines.rb +13 -12
- data/manual/graphics/fill_and_stroke.rb +10 -11
- data/manual/graphics/fill_rules.rb +13 -12
- data/manual/graphics/gradients.rb +28 -22
- data/manual/graphics/graphics.rb +52 -46
- data/manual/graphics/helper.rb +20 -10
- data/manual/graphics/line_width.rb +13 -12
- data/manual/graphics/lines_and_curves.rb +13 -14
- data/manual/graphics/polygon.rb +10 -12
- data/manual/graphics/rectangle.rb +7 -8
- data/manual/graphics/rotate.rb +9 -12
- data/manual/graphics/scale.rb +19 -18
- data/manual/graphics/soft_masks.rb +5 -7
- data/manual/graphics/stroke_cap.rb +10 -11
- data/manual/graphics/stroke_dash.rb +16 -17
- data/manual/graphics/stroke_join.rb +10 -11
- data/manual/graphics/translate.rb +13 -13
- data/manual/graphics/transparency.rb +11 -13
- data/manual/{manual/how_to_read_this_manual.rb → how_to_read_this_manual.rb} +23 -25
- data/manual/images/absolute_position.rb +9 -10
- data/manual/images/fit.rb +9 -10
- data/manual/images/horizontal.rb +13 -14
- data/manual/images/images.rb +31 -30
- data/manual/images/plain_image.rb +6 -7
- data/manual/images/scale.rb +12 -13
- data/manual/images/vertical.rb +19 -17
- data/manual/images/width_and_height.rb +13 -14
- data/manual/layout/boxes.rb +14 -15
- data/manual/layout/content.rb +12 -13
- data/manual/layout/layout.rb +19 -20
- data/manual/layout/simple_grid.rb +8 -9
- data/manual/outline/add_subsection_to.rb +26 -27
- data/manual/outline/insert_section_after.rb +19 -20
- data/manual/outline/outline.rb +23 -22
- data/manual/outline/sections_and_pages.rb +24 -25
- data/manual/repeatable_content/alternate_page_numbering.rb +36 -0
- data/manual/repeatable_content/page_numbering.rb +20 -19
- data/manual/repeatable_content/repeatable_content.rb +26 -22
- data/manual/repeatable_content/repeater.rb +18 -19
- data/manual/repeatable_content/stamp.rb +18 -19
- data/manual/security/encryption.rb +8 -11
- data/manual/security/permissions.rb +20 -15
- data/manual/security/security.rb +20 -20
- data/manual/table.rb +16 -0
- data/manual/text/alignment.rb +17 -18
- data/manual/text/color.rb +13 -13
- data/manual/text/column_box.rb +10 -12
- data/manual/text/fallback_fonts.rb +29 -25
- data/manual/text/font.rb +17 -18
- data/manual/text/font_size.rb +21 -22
- data/manual/text/font_style.rb +12 -10
- data/manual/text/formatted_callbacks.rb +36 -26
- data/manual/text/formatted_text.rb +41 -34
- data/manual/text/free_flowing_text.rb +28 -29
- data/manual/text/inline.rb +23 -26
- data/manual/text/kerning_and_character_spacing.rb +20 -21
- data/manual/text/leading.rb +10 -11
- data/manual/text/line_wrapping.rb +40 -21
- data/manual/text/paragraph_indentation.rb +17 -12
- data/manual/text/positioned_text.rb +19 -20
- data/manual/text/registering_families.rb +33 -30
- data/manual/text/rendering_and_color.rb +11 -12
- data/manual/text/right_to_left_text.rb +31 -20
- data/manual/text/rotation.rb +36 -27
- data/manual/text/single_usage.rb +13 -14
- data/manual/text/text.rb +62 -62
- data/manual/text/text_box_excess.rb +22 -19
- data/manual/text/text_box_extensions.rb +21 -18
- data/manual/text/text_box_overflow.rb +28 -21
- data/manual/text/utf8.rb +16 -17
- data/manual/text/win_ansi_charset.rb +29 -26
- data/prawn.gemspec +45 -43
- data/spec/extensions/encoding_helpers.rb +4 -3
- data/spec/prawn/document/bounding_box_spec.rb +550 -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 +44 -0
- data/spec/prawn/document_spec.rb +805 -0
- data/spec/prawn/font_metric_cache_spec.rb +54 -0
- data/spec/prawn/font_spec.rb +544 -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 +872 -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 +229 -0
- data/spec/prawn/measurements_extensions_spec.rb +24 -0
- data/spec/prawn/outline_spec.rb +512 -0
- data/spec/prawn/repeater_spec.rb +166 -0
- data/spec/prawn/soft_mask_spec.rb +74 -0
- data/spec/prawn/stamp_spec.rb +173 -0
- data/spec/prawn/text/box_spec.rb +1110 -0
- data/spec/prawn/text/formatted/arranger_spec.rb +466 -0
- data/spec/prawn/text/formatted/box_spec.rb +849 -0
- data/spec/prawn/text/formatted/fragment_spec.rb +343 -0
- data/spec/prawn/text/formatted/line_wrap_spec.rb +495 -0
- data/spec/prawn/text/formatted/parser_spec.rb +697 -0
- data/spec/prawn/text_draw_text_spec.rb +150 -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 +22 -21
- data.tar.gz.sig +0 -0
- metadata +168 -307
- metadata.gz.sig +0 -0
- data/README.md +0 -109
- data/data/encodings/win_ansi.txt +0 -29
- data/data/images/16bit.alpha +0 -0
- data/data/images/16bit.dat +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/barcode_issue.png +0 -0
- data/data/images/dice.alpha +0 -0
- data/data/images/dice.dat +0 -0
- data/data/images/dice.png +0 -0
- data/data/images/dice_interlaced.png +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/page_white_text.alpha +0 -0
- data/data/images/page_white_text.dat +0 -0
- data/data/images/page_white_text.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/lib/pdf/core/annotations.rb +0 -60
- data/lib/pdf/core/byte_string.rb +0 -9
- data/lib/pdf/core/destinations.rb +0 -90
- data/lib/pdf/core/document_state.rb +0 -78
- data/lib/pdf/core/filter_list.rb +0 -51
- data/lib/pdf/core/filters.rb +0 -36
- data/lib/pdf/core/graphics_state.rb +0 -68
- data/lib/pdf/core/literal_string.rb +0 -16
- data/lib/pdf/core/name_tree.rb +0 -177
- data/lib/pdf/core/object_store.rb +0 -320
- data/lib/pdf/core/outline.rb +0 -315
- data/lib/pdf/core/page.rb +0 -212
- data/lib/pdf/core/page_geometry.rb +0 -126
- data/lib/pdf/core/pdf_object.rb +0 -124
- data/lib/pdf/core/reference.rb +0 -103
- data/lib/pdf/core/stream.rb +0 -98
- data/lib/pdf/core/text.rb +0 -275
- data/lib/pdf/core.rb +0 -35
- data/lib/prawn/compatibility.rb +0 -91
- data/lib/prawn/document/graphics_state.rb +0 -73
- data/lib/prawn/document/snapshot.rb +0 -89
- data/lib/prawn/font/afm.rb +0 -203
- data/lib/prawn/layout.rb +0 -20
- data/lib/prawn/table/cell/image.rb +0 -70
- data/lib/prawn/table/cell/in_table.rb +0 -27
- data/lib/prawn/table/cell/span_dummy.rb +0 -92
- data/lib/prawn/table/cell/subtable.rb +0 -65
- data/lib/prawn/table/cell/text.rb +0 -153
- data/lib/prawn/table/cell.rb +0 -770
- data/lib/prawn/table/cells.rb +0 -295
- data/lib/prawn/table.rb +0 -643
- data/manual/example_file.rb +0 -116
- data/manual/example_package.rb +0 -53
- data/manual/example_section.rb +0 -46
- data/manual/manual/cover.rb +0 -35
- data/manual/manual/foreword.rb +0 -85
- data/manual/manual/manual.rb +0 -35
- data/manual/syntax_highlight.rb +0 -52
- data/manual/table/basic_block.rb +0 -53
- data/manual/table/before_rendering_page.rb +0 -26
- data/manual/table/cell_border_lines.rb +0 -24
- data/manual/table/cell_borders_and_bg.rb +0 -31
- data/manual/table/cell_dimensions.rb +0 -30
- data/manual/table/cell_text.rb +0 -38
- data/manual/table/column_widths.rb +0 -30
- data/manual/table/content_and_subtables.rb +0 -39
- data/manual/table/creation.rb +0 -27
- data/manual/table/filtering.rb +0 -36
- data/manual/table/flow_and_header.rb +0 -17
- data/manual/table/image_cells.rb +0 -33
- data/manual/table/position.rb +0 -29
- data/manual/table/row_colors.rb +0 -20
- data/manual/table/span.rb +0 -30
- data/manual/table/style.rb +0 -22
- data/manual/table/table.rb +0 -52
- data/manual/table/width.rb +0 -27
- data/manual/templates/full_template.rb +0 -25
- data/manual/templates/page_template.rb +0 -48
- data/manual/templates/templates.rb +0 -27
- data/manual/text/group.rb +0 -29
- data/spec/acceptance/png.rb +0 -23
- data/spec/annotations_spec.rb +0 -74
- data/spec/bounding_box_spec.rb +0 -493
- data/spec/cell_spec.rb +0 -628
- data/spec/column_box_spec.rb +0 -33
- data/spec/destinations_spec.rb +0 -15
- data/spec/document_spec.rb +0 -761
- data/spec/extensions/mocha.rb +0 -44
- data/spec/filters_spec.rb +0 -34
- data/spec/font_metric_cache_spec.rb +0 -52
- data/spec/font_spec.rb +0 -464
- data/spec/formatted_text_arranger_spec.rb +0 -421
- data/spec/formatted_text_box_spec.rb +0 -650
- data/spec/formatted_text_fragment_spec.rb +0 -298
- data/spec/graphics_spec.rb +0 -651
- data/spec/grid_spec.rb +0 -85
- data/spec/image_handler_spec.rb +0 -42
- data/spec/images_spec.rb +0 -157
- data/spec/inline_formatted_text_parser_spec.rb +0 -564
- data/spec/jpg_spec.rb +0 -25
- data/spec/line_wrap_spec.rb +0 -333
- data/spec/measurement_units_spec.rb +0 -23
- data/spec/name_tree_spec.rb +0 -112
- data/spec/object_store_spec.rb +0 -170
- data/spec/outline_spec.rb +0 -448
- data/spec/pdf_object_spec.rb +0 -172
- data/spec/png_spec.rb +0 -240
- data/spec/reference_spec.rb +0 -82
- data/spec/repeater_spec.rb +0 -158
- data/spec/security_spec.rb +0 -158
- data/spec/snapshot_spec.rb +0 -186
- data/spec/soft_mask_spec.rb +0 -117
- data/spec/span_spec.rb +0 -44
- data/spec/stamp_spec.rb +0 -158
- data/spec/stream_spec.rb +0 -58
- data/spec/stroke_styles_spec.rb +0 -211
- data/spec/table/span_dummy_spec.rb +0 -17
- data/spec/table_spec.rb +0 -1355
- data/spec/template_spec.rb +0 -351
- data/spec/text_at_spec.rb +0 -130
- data/spec/text_box_spec.rb +0 -1030
- data/spec/text_rendering_mode_spec.rb +0 -45
- data/spec/text_spacing_spec.rb +0 -93
- data/spec/text_spec.rb +0 -425
- data/spec/text_with_inline_formatting_spec.rb +0 -35
- data/spec/transparency_spec.rb +0 -89
@@ -1,11 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# grid.rb: Provides a basic grid layout system for Prawn
|
4
|
+
#
|
5
|
+
# Contributed by Andrew O'Brien in March 2009
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
1
9
|
module Prawn
|
2
10
|
class Document
|
11
|
+
# @group Experimental API
|
3
12
|
|
4
13
|
# Defines the grid system for a particular document. Takes the number of
|
5
14
|
# rows and columns and the width to use for the gutter as the
|
6
15
|
# keys :rows, :columns, :gutter, :row_gutter, :column_gutter
|
7
16
|
#
|
17
|
+
# Note that a completely new grid object is built each time define_grid()
|
18
|
+
# is called. This means that all subsequent calls to grid() will use
|
19
|
+
# the newly defined Grid object -- grids are not nestable like
|
20
|
+
# bounding boxes are.
|
21
|
+
|
8
22
|
def define_grid(options = {})
|
23
|
+
@boxes = nil
|
9
24
|
@grid = Grid.new(self, options)
|
10
25
|
end
|
11
26
|
|
@@ -13,36 +28,43 @@ module Prawn
|
|
13
28
|
# or work with the grid system directly.
|
14
29
|
#
|
15
30
|
# @pdf.grid # Get the Grid directly
|
16
|
-
# @pdf.grid([0,1]) # Get the
|
31
|
+
# @pdf.grid([0,1]) # Get the GridBox at [0,1]
|
17
32
|
# @pdf.grid([0,1], [1,2]) # Get a multi-box spanning from [0,1] to [1,2]
|
18
33
|
#
|
19
34
|
def grid(*args)
|
20
35
|
@boxes ||= {}
|
21
|
-
@boxes[args] ||=
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
30
50
|
end
|
31
|
-
end
|
32
51
|
end
|
33
52
|
|
34
53
|
# A Grid represents the entire grid system of a Page and calculates
|
35
54
|
# the column width and row height of the base box.
|
55
|
+
#
|
56
|
+
# @group Experimental API
|
36
57
|
class Grid
|
37
58
|
attr_reader :pdf, :columns, :rows, :gutter, :row_gutter, :column_gutter
|
59
|
+
|
38
60
|
def initialize(pdf, options = {}) # :nodoc:
|
39
|
-
valid_options = [
|
61
|
+
valid_options = %i[columns rows gutter row_gutter column_gutter]
|
40
62
|
Prawn.verify_options valid_options, options
|
41
63
|
|
42
64
|
@pdf = pdf
|
43
65
|
@columns = options[:columns]
|
44
66
|
@rows = options[:rows]
|
45
|
-
|
67
|
+
apply_gutter(options)
|
46
68
|
end
|
47
69
|
|
48
70
|
# Calculates the base width of boxes.
|
@@ -52,14 +74,14 @@ module Prawn
|
|
52
74
|
|
53
75
|
# Calculates the base height of boxes.
|
54
76
|
def row_height
|
55
|
-
|
77
|
+
@row_height ||= subdivide(pdf.bounds.height, rows, row_gutter)
|
56
78
|
end
|
57
79
|
|
58
80
|
# Diagnostic tool to show all of the grids. Defaults to gray.
|
59
|
-
def show_all(color =
|
60
|
-
|
61
|
-
|
62
|
-
pdf.grid(
|
81
|
+
def show_all(color = 'CCCCCC')
|
82
|
+
rows.times do |row|
|
83
|
+
columns.times do |column|
|
84
|
+
pdf.grid(row, column).show(color)
|
63
85
|
end
|
64
86
|
end
|
65
87
|
end
|
@@ -70,12 +92,13 @@ module Prawn
|
|
70
92
|
(total.to_f - (gutter * (num - 1).to_f)) / num.to_f
|
71
93
|
end
|
72
94
|
|
73
|
-
def
|
74
|
-
if options.
|
95
|
+
def apply_gutter(options)
|
96
|
+
if options.key?(:gutter)
|
75
97
|
@gutter = options[:gutter].to_f
|
76
|
-
@row_gutter
|
98
|
+
@row_gutter = @gutter
|
99
|
+
@column_gutter = @gutter
|
77
100
|
else
|
78
|
-
@row_gutter
|
101
|
+
@row_gutter = options[:row_gutter].to_f
|
79
102
|
@column_gutter = options[:column_gutter].to_f
|
80
103
|
@gutter = 0
|
81
104
|
end
|
@@ -86,20 +109,21 @@ module Prawn
|
|
86
109
|
# A Grid object has methods that allow easy access to the coordinates of
|
87
110
|
# its corners, which can be plugged into most existing prawnmethods.
|
88
111
|
#
|
89
|
-
|
112
|
+
# @group Experimental API
|
113
|
+
class GridBox
|
90
114
|
attr_reader :pdf
|
91
115
|
|
92
|
-
def initialize(pdf,
|
116
|
+
def initialize(pdf, rows, columns)
|
93
117
|
@pdf = pdf
|
94
|
-
@
|
95
|
-
@
|
118
|
+
@rows = rows
|
119
|
+
@columns = columns
|
96
120
|
end
|
97
121
|
|
98
122
|
# Mostly diagnostic method that outputs the name of a box as
|
99
123
|
# col_num, row_num
|
100
124
|
#
|
101
125
|
def name
|
102
|
-
"#{@
|
126
|
+
"#{@rows},#{@columns}"
|
103
127
|
end
|
104
128
|
|
105
129
|
# :nodoc
|
@@ -124,7 +148,7 @@ module Prawn
|
|
124
148
|
|
125
149
|
# x-coordinate of left side
|
126
150
|
def left
|
127
|
-
@left ||= (width + grid.column_gutter) * @
|
151
|
+
@left ||= (width + grid.column_gutter) * @columns.to_f
|
128
152
|
end
|
129
153
|
|
130
154
|
# x-coordinate of right side
|
@@ -134,7 +158,7 @@ module Prawn
|
|
134
158
|
|
135
159
|
# y-coordinate of the top
|
136
160
|
def top
|
137
|
-
@top ||= total_height - ((height + grid.row_gutter) * @
|
161
|
+
@top ||= total_height - ((height + grid.row_gutter) * @rows.to_f)
|
138
162
|
end
|
139
163
|
|
140
164
|
# y-coordinate of the bottom
|
@@ -164,16 +188,16 @@ module Prawn
|
|
164
188
|
|
165
189
|
# Creates a standard bounding box based on the grid box.
|
166
190
|
def bounding_box(&blk)
|
167
|
-
pdf.bounding_box(top_left, :
|
191
|
+
pdf.bounding_box(top_left, width: width, height: height, &blk)
|
168
192
|
end
|
169
193
|
|
170
194
|
# Diagnostic method
|
171
|
-
def show(grid_color =
|
172
|
-
|
195
|
+
def show(grid_color = 'CCCCCC')
|
196
|
+
bounding_box do
|
173
197
|
original_stroke_color = pdf.stroke_color
|
174
198
|
|
175
199
|
pdf.stroke_color = grid_color
|
176
|
-
pdf.text
|
200
|
+
pdf.text name
|
177
201
|
pdf.stroke_bounds
|
178
202
|
|
179
203
|
pdf.stroke_color = original_stroke_color
|
@@ -181,24 +205,29 @@ module Prawn
|
|
181
205
|
end
|
182
206
|
|
183
207
|
private
|
208
|
+
|
184
209
|
def grid
|
185
210
|
pdf.grid
|
186
211
|
end
|
187
212
|
end
|
188
213
|
|
189
214
|
# A MultiBox is specified by 2 Boxes and spans the areas between.
|
190
|
-
|
191
|
-
|
215
|
+
#
|
216
|
+
# @group Experimental API
|
217
|
+
class MultiBox
|
218
|
+
def initialize(pdf, box1, box2)
|
192
219
|
@pdf = pdf
|
193
|
-
@
|
220
|
+
@boxes = [box1, box2]
|
194
221
|
end
|
195
222
|
|
223
|
+
attr_reader :pdf
|
224
|
+
|
196
225
|
def name
|
197
|
-
@
|
226
|
+
@boxes.map(&:name).join(':')
|
198
227
|
end
|
199
228
|
|
200
229
|
def total_height
|
201
|
-
@
|
230
|
+
@boxes[0].total_height
|
202
231
|
end
|
203
232
|
|
204
233
|
def width
|
@@ -210,7 +239,7 @@ module Prawn
|
|
210
239
|
end
|
211
240
|
|
212
241
|
def gutter
|
213
|
-
@
|
242
|
+
@boxes[0].gutter
|
214
243
|
end
|
215
244
|
|
216
245
|
def left
|
@@ -229,31 +258,65 @@ module Prawn
|
|
229
258
|
bottom_box.bottom
|
230
259
|
end
|
231
260
|
|
261
|
+
def top_left
|
262
|
+
[left, top]
|
263
|
+
end
|
264
|
+
|
265
|
+
def top_right
|
266
|
+
[right, top]
|
267
|
+
end
|
268
|
+
|
269
|
+
def bottom_left
|
270
|
+
[left, bottom]
|
271
|
+
end
|
272
|
+
|
273
|
+
def bottom_right
|
274
|
+
[right, bottom]
|
275
|
+
end
|
276
|
+
|
277
|
+
def bounding_box(&blk)
|
278
|
+
pdf.bounding_box(top_left, width: width, height: height, &blk)
|
279
|
+
end
|
280
|
+
|
281
|
+
def show(grid_color = 'CCCCCC')
|
282
|
+
bounding_box do
|
283
|
+
original_stroke_color = pdf.stroke_color
|
284
|
+
|
285
|
+
pdf.stroke_color = grid_color
|
286
|
+
pdf.text name
|
287
|
+
pdf.stroke_bounds
|
288
|
+
|
289
|
+
pdf.stroke_color = original_stroke_color
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
232
293
|
private
|
294
|
+
|
233
295
|
def left_box
|
234
|
-
@left_box ||= @
|
296
|
+
@left_box ||= @boxes.min_by(&:left)
|
235
297
|
end
|
236
298
|
|
237
299
|
def right_box
|
238
|
-
@right_box ||= @
|
300
|
+
@right_box ||= @boxes.max_by(&:right)
|
239
301
|
end
|
240
302
|
|
241
303
|
def top_box
|
242
|
-
@top_box ||= @
|
304
|
+
@top_box ||= @boxes.max_by(&:top)
|
243
305
|
end
|
244
306
|
|
245
307
|
def bottom_box
|
246
|
-
@bottom_box ||= @
|
308
|
+
@bottom_box ||= @boxes.min_by(&:bottom)
|
247
309
|
end
|
248
310
|
end
|
249
311
|
|
250
312
|
private
|
251
|
-
|
252
|
-
|
313
|
+
|
314
|
+
def single_box(rows, columns)
|
315
|
+
GridBox.new(self, rows, columns)
|
253
316
|
end
|
254
317
|
|
255
|
-
def multi_box(
|
256
|
-
MultiBox.new(self,
|
318
|
+
def multi_box(box1, box2)
|
319
|
+
MultiBox.new(self, box1, box2)
|
257
320
|
end
|
258
321
|
end
|
259
322
|
end
|
data/lib/prawn/image_handler.rb
CHANGED
@@ -1,4 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# ImageHandler provides a way to register image processors with Prawn
|
4
|
+
#
|
5
|
+
# Contributed by Evan Sharp in November 2013.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
1
9
|
module Prawn
|
10
|
+
# @group Extension API
|
11
|
+
|
2
12
|
def self.image_handler
|
3
13
|
@image_handler ||= ImageHandler.new
|
4
14
|
end
|
@@ -18,13 +28,17 @@ module Prawn
|
|
18
28
|
@handlers.unshift handler
|
19
29
|
end
|
20
30
|
|
31
|
+
def unregister(handler)
|
32
|
+
@handlers.reject! { |h| h == handler }
|
33
|
+
end
|
34
|
+
|
21
35
|
def find(image_blob)
|
22
|
-
handler = @handlers.find{ |h| h.can_render? image_blob }
|
36
|
+
handler = @handlers.find { |h| h.can_render? image_blob }
|
23
37
|
|
24
38
|
return handler if handler
|
25
39
|
|
26
40
|
raise Prawn::Errors::UnsupportedImageType,
|
27
|
-
|
41
|
+
'image file is an unrecognised format'
|
28
42
|
end
|
29
43
|
end
|
30
44
|
end
|
data/lib/prawn/images/image.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
# image.rb : Base class for image info objects
|
3
4
|
#
|
4
5
|
# Copyright September 2011, Brad Ediger. All rights reserved.
|
@@ -10,6 +11,7 @@ require 'digest/sha1'
|
|
10
11
|
module Prawn
|
11
12
|
module Images
|
12
13
|
class Image
|
14
|
+
# @group Extension API
|
13
15
|
|
14
16
|
def calc_image_dimensions(options)
|
15
17
|
w = options[:width] || width
|
@@ -41,7 +43,7 @@ module Prawn
|
|
41
43
|
self.scaled_width = w
|
42
44
|
self.scaled_height = h
|
43
45
|
|
44
|
-
[w,h]
|
46
|
+
[w, h]
|
45
47
|
end
|
46
48
|
end
|
47
49
|
end
|
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.
|
@@ -14,14 +16,20 @@ module Prawn
|
|
14
16
|
# of a JPG image that we need to embed them in a PDF
|
15
17
|
#
|
16
18
|
class JPG < Image
|
19
|
+
class FormatError < StandardError; end
|
20
|
+
|
21
|
+
# @group Extension API
|
22
|
+
|
17
23
|
attr_reader :width, :height, :bits, :channels
|
18
24
|
attr_accessor :scaled_width, :scaled_height
|
19
25
|
|
20
|
-
JPEG_SOF_BLOCKS =
|
21
|
-
|
26
|
+
JPEG_SOF_BLOCKS = [
|
27
|
+
0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6, 0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE,
|
28
|
+
0xCF
|
29
|
+
].freeze
|
22
30
|
|
23
31
|
def self.can_render?(image_blob)
|
24
|
-
image_blob[0, 3].unpack(
|
32
|
+
image_blob[0, 3].unpack('C*') == [255, 216, 255]
|
25
33
|
end
|
26
34
|
|
27
35
|
# Process a new JPG image
|
@@ -29,22 +37,23 @@ module Prawn
|
|
29
37
|
# <tt>:data</tt>:: A binary string of JPEG data
|
30
38
|
#
|
31
39
|
def initialize(data)
|
32
|
-
|
33
|
-
|
34
|
-
|
40
|
+
super()
|
41
|
+
@data = data
|
42
|
+
d = StringIO.new(@data)
|
43
|
+
d.binmode
|
35
44
|
|
36
|
-
c_marker =
|
37
|
-
|
45
|
+
c_marker = 0xff # Section marker.
|
46
|
+
d.seek(2) # Skip the first two bytes of JPEG identifier.
|
38
47
|
loop do
|
39
|
-
marker, code, length =
|
40
|
-
raise
|
48
|
+
marker, code, length = d.read(4).unpack('CCn')
|
49
|
+
raise FormatError, 'JPEG marker not found!' if marker != c_marker
|
41
50
|
|
42
51
|
if JPEG_SOF_BLOCKS.include?(code)
|
43
|
-
@bits, @height, @width, @channels =
|
52
|
+
@bits, @height, @width, @channels = d.read(6).unpack('CnnC')
|
44
53
|
break
|
45
54
|
end
|
46
55
|
|
47
|
-
|
56
|
+
d.seek(length - 2, IO::SEEK_CUR)
|
48
57
|
end
|
49
58
|
end
|
50
59
|
|
@@ -52,38 +61,38 @@ module Prawn
|
|
52
61
|
# a Reference to it.
|
53
62
|
#
|
54
63
|
def build_pdf_object(document)
|
55
|
-
color_space =
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
64
|
+
color_space =
|
65
|
+
case channels
|
66
|
+
when 1
|
67
|
+
:DeviceGray
|
68
|
+
when 3
|
69
|
+
:DeviceRGB
|
70
|
+
when 4
|
71
|
+
:DeviceCMYK
|
72
|
+
else
|
73
|
+
raise ArgumentError, 'JPG uses an unsupported number of channels'
|
74
|
+
end
|
65
75
|
|
66
76
|
obj = document.ref!(
|
67
|
-
:
|
68
|
-
:
|
69
|
-
:
|
70
|
-
:
|
71
|
-
:
|
72
|
-
:
|
77
|
+
Type: :XObject,
|
78
|
+
Subtype: :Image,
|
79
|
+
ColorSpace: color_space,
|
80
|
+
BitsPerComponent: bits,
|
81
|
+
Width: width,
|
82
|
+
Height: height
|
73
83
|
)
|
74
84
|
|
75
85
|
# add extra decode params for CMYK images. By swapping the
|
76
86
|
# min and max values from the default, we invert the colours. See
|
77
87
|
# section 4.8.4 of the spec.
|
78
88
|
if color_space == :DeviceCMYK
|
79
|
-
obj.data[:Decode] = [
|
89
|
+
obj.data[:Decode] = [1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0]
|
80
90
|
end
|
81
91
|
|
82
92
|
obj.stream << @data
|
83
93
|
obj.stream.filters << :DCTDecode
|
84
94
|
obj
|
85
95
|
end
|
86
|
-
|
87
96
|
end
|
88
97
|
end
|
89
98
|
end
|