nurettin-prawn 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +2 -0
- data/GPLv2 +340 -0
- data/GPLv3 +674 -0
- data/Gemfile +18 -0
- data/LICENSE +56 -0
- data/README.md +98 -0
- data/Rakefile +45 -0
- data/data/encodings/win_ansi.txt +29 -0
- data/data/fonts/Action Man.dfont +0 -0
- data/data/fonts/Activa.ttf +0 -0
- data/data/fonts/Chalkboard.ttf +0 -0
- data/data/fonts/Courier-Bold.afm +342 -0
- data/data/fonts/Courier-BoldOblique.afm +342 -0
- data/data/fonts/Courier-Oblique.afm +342 -0
- data/data/fonts/Courier.afm +342 -0
- data/data/fonts/DejaVuSans.ttf +0 -0
- data/data/fonts/Dustismo_Roman.ttf +0 -0
- data/data/fonts/Helvetica-Bold.afm +2827 -0
- data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
- data/data/fonts/Helvetica-Oblique.afm +3051 -0
- data/data/fonts/Helvetica.afm +3051 -0
- data/data/fonts/MustRead.html +19 -0
- data/data/fonts/Symbol.afm +213 -0
- data/data/fonts/Times-Bold.afm +2588 -0
- data/data/fonts/Times-BoldItalic.afm +2384 -0
- data/data/fonts/Times-Italic.afm +2667 -0
- data/data/fonts/Times-Roman.afm +2419 -0
- data/data/fonts/ZapfDingbats.afm +225 -0
- data/data/fonts/comicsans.ttf +0 -0
- data/data/fonts/gkai00mp.ttf +0 -0
- 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/fractal.jpg +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/rails.dat +0 -0
- data/data/images/rails.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 +1 -0
- 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 +820 -0
- data/data/pdfs/hexagon.pdf +61 -0
- data/data/pdfs/indirect_reference.pdf +86 -0
- data/data/pdfs/multipage_template.pdf +127 -0
- data/data/pdfs/nested_pages.pdf +118 -0
- data/data/pdfs/page_without_mediabox.pdf +193 -0
- data/data/pdfs/resources_as_indirect_object.pdf +83 -0
- data/data/pdfs/two_hexagons.pdf +90 -0
- data/data/pdfs/version_1_6.pdf +61 -0
- data/data/shift_jis_text.txt +1 -0
- data/lib/prawn.rb +29 -0
- data/lib/prawn/compatibility.rb +87 -0
- data/lib/prawn/core.rb +87 -0
- data/lib/prawn/core/annotations.rb +61 -0
- data/lib/prawn/core/byte_string.rb +9 -0
- data/lib/prawn/core/destinations.rb +90 -0
- data/lib/prawn/core/document_state.rb +79 -0
- data/lib/prawn/core/literal_string.rb +16 -0
- data/lib/prawn/core/name_tree.rb +177 -0
- data/lib/prawn/core/object_store.rb +320 -0
- data/lib/prawn/core/page.rb +212 -0
- data/lib/prawn/core/pdf_object.rb +125 -0
- data/lib/prawn/core/reference.rb +119 -0
- data/lib/prawn/core/text.rb +268 -0
- data/lib/prawn/core/text/formatted/arranger.rb +294 -0
- data/lib/prawn/core/text/formatted/line_wrap.rb +288 -0
- data/lib/prawn/core/text/formatted/wrap.rb +153 -0
- data/lib/prawn/document.rb +707 -0
- data/lib/prawn/document/bounding_box.rb +510 -0
- data/lib/prawn/document/column_box.rb +132 -0
- data/lib/prawn/document/graphics_state.rb +136 -0
- data/lib/prawn/document/internals.rb +173 -0
- data/lib/prawn/document/page_geometry.rb +136 -0
- data/lib/prawn/document/snapshot.rb +89 -0
- data/lib/prawn/document/span.rb +55 -0
- data/lib/prawn/encoding.rb +121 -0
- data/lib/prawn/errors.rb +99 -0
- data/lib/prawn/font.rb +386 -0
- data/lib/prawn/font/afm.rb +203 -0
- data/lib/prawn/font/dfont.rb +42 -0
- data/lib/prawn/font/ttf.rb +343 -0
- data/lib/prawn/graphics.rb +523 -0
- data/lib/prawn/graphics/cap_style.rb +46 -0
- data/lib/prawn/graphics/color.rb +231 -0
- data/lib/prawn/graphics/dash.rb +82 -0
- data/lib/prawn/graphics/join_style.rb +47 -0
- data/lib/prawn/graphics/patterns.rb +137 -0
- data/lib/prawn/graphics/transformation.rb +156 -0
- data/lib/prawn/graphics/transparency.rb +99 -0
- data/lib/prawn/images.rb +196 -0
- data/lib/prawn/images/image.rb +65 -0
- data/lib/prawn/images/jpg.rb +85 -0
- data/lib/prawn/images/png.rb +362 -0
- data/lib/prawn/layout.rb +20 -0
- data/lib/prawn/layout/grid.rb +259 -0
- data/lib/prawn/measurement_extensions.rb +46 -0
- data/lib/prawn/measurements.rb +71 -0
- data/lib/prawn/outline.rb +326 -0
- data/lib/prawn/repeater.rb +122 -0
- data/lib/prawn/security.rb +269 -0
- data/lib/prawn/soft_mask.rb +94 -0
- data/lib/prawn/stamp.rb +134 -0
- data/lib/prawn/table.rb +609 -0
- data/lib/prawn/table/cell.rb +776 -0
- data/lib/prawn/table/cell/image.rb +70 -0
- data/lib/prawn/table/cell/in_table.rb +27 -0
- data/lib/prawn/table/cell/span_dummy.rb +88 -0
- data/lib/prawn/table/cell/subtable.rb +65 -0
- data/lib/prawn/table/cell/text.rb +152 -0
- data/lib/prawn/table/cells.rb +260 -0
- data/lib/prawn/text.rb +420 -0
- data/lib/prawn/text/box.rb +141 -0
- data/lib/prawn/text/formatted.rb +4 -0
- data/lib/prawn/text/formatted/box.rb +563 -0
- data/lib/prawn/text/formatted/fragment.rb +253 -0
- data/lib/prawn/text/formatted/parser.rb +217 -0
- data/lib/prawn/utilities.rb +44 -0
- data/manual/basic_concepts/adding_pages.rb +27 -0
- data/manual/basic_concepts/basic_concepts.rb +34 -0
- data/manual/basic_concepts/creation.rb +39 -0
- data/manual/basic_concepts/cursor.rb +33 -0
- data/manual/basic_concepts/measurement.rb +25 -0
- data/manual/basic_concepts/origin.rb +38 -0
- data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
- data/manual/bounding_box/bounding_box.rb +39 -0
- data/manual/bounding_box/bounds.rb +49 -0
- data/manual/bounding_box/canvas.rb +24 -0
- data/manual/bounding_box/creation.rb +23 -0
- data/manual/bounding_box/indentation.rb +46 -0
- data/manual/bounding_box/nesting.rb +45 -0
- data/manual/bounding_box/russian_boxes.rb +40 -0
- data/manual/bounding_box/stretchy.rb +31 -0
- data/manual/document_and_page_options/background.rb +27 -0
- data/manual/document_and_page_options/document_and_page_options.rb +31 -0
- data/manual/document_and_page_options/metadata.rb +23 -0
- data/manual/document_and_page_options/page_margins.rb +38 -0
- data/manual/document_and_page_options/page_size.rb +34 -0
- data/manual/example_file.rb +116 -0
- data/manual/example_helper.rb +430 -0
- data/manual/example_package.rb +53 -0
- data/manual/example_section.rb +46 -0
- data/manual/graphics/circle_and_ellipse.rb +22 -0
- data/manual/graphics/color.rb +24 -0
- data/manual/graphics/common_lines.rb +28 -0
- data/manual/graphics/fill_and_stroke.rb +42 -0
- data/manual/graphics/fill_rules.rb +37 -0
- data/manual/graphics/gradients.rb +37 -0
- data/manual/graphics/graphics.rb +58 -0
- data/manual/graphics/helper.rb +17 -0
- data/manual/graphics/line_width.rb +35 -0
- data/manual/graphics/lines_and_curves.rb +41 -0
- data/manual/graphics/polygon.rb +29 -0
- data/manual/graphics/rectangle.rb +21 -0
- data/manual/graphics/rotate.rb +28 -0
- data/manual/graphics/scale.rb +41 -0
- data/manual/graphics/soft_masks.rb +46 -0
- data/manual/graphics/stroke_cap.rb +31 -0
- data/manual/graphics/stroke_dash.rb +43 -0
- data/manual/graphics/stroke_join.rb +30 -0
- data/manual/graphics/translate.rb +29 -0
- data/manual/graphics/transparency.rb +35 -0
- data/manual/images/absolute_position.rb +23 -0
- data/manual/images/fit.rb +21 -0
- data/manual/images/horizontal.rb +25 -0
- data/manual/images/images.rb +40 -0
- data/manual/images/plain_image.rb +18 -0
- data/manual/images/scale.rb +22 -0
- data/manual/images/vertical.rb +28 -0
- data/manual/images/width_and_height.rb +25 -0
- data/manual/layout/boxes.rb +27 -0
- data/manual/layout/content.rb +25 -0
- data/manual/layout/layout.rb +28 -0
- data/manual/layout/simple_grid.rb +23 -0
- data/manual/manual/cover.rb +26 -0
- data/manual/manual/foreword.rb +13 -0
- data/manual/manual/how_to_read_this_manual.rb +41 -0
- data/manual/manual/manual.rb +36 -0
- data/manual/outline/add_subsection_to.rb +61 -0
- data/manual/outline/insert_section_after.rb +47 -0
- data/manual/outline/outline.rb +32 -0
- data/manual/outline/sections_and_pages.rb +67 -0
- data/manual/repeatable_content/page_numbering.rb +54 -0
- data/manual/repeatable_content/repeatable_content.rb +31 -0
- data/manual/repeatable_content/repeater.rb +55 -0
- data/manual/repeatable_content/stamp.rb +41 -0
- data/manual/security/encryption.rb +31 -0
- data/manual/security/permissions.rb +38 -0
- data/manual/security/security.rb +28 -0
- data/manual/syntax_highlight.rb +52 -0
- data/manual/table/basic_block.rb +53 -0
- data/manual/table/before_rendering_page.rb +26 -0
- data/manual/table/cell_border_lines.rb +24 -0
- data/manual/table/cell_borders_and_bg.rb +31 -0
- data/manual/table/cell_dimensions.rb +30 -0
- data/manual/table/cell_text.rb +38 -0
- data/manual/table/column_widths.rb +30 -0
- data/manual/table/content_and_subtables.rb +39 -0
- data/manual/table/creation.rb +27 -0
- data/manual/table/filtering.rb +36 -0
- data/manual/table/flow_and_header.rb +17 -0
- data/manual/table/image_cells.rb +33 -0
- data/manual/table/position.rb +29 -0
- data/manual/table/row_colors.rb +20 -0
- data/manual/table/span.rb +30 -0
- data/manual/table/style.rb +22 -0
- data/manual/table/table.rb +52 -0
- data/manual/table/width.rb +27 -0
- data/manual/templates/full_template.rb +23 -0
- data/manual/templates/page_template.rb +47 -0
- data/manual/templates/templates.rb +26 -0
- data/manual/text/alignment.rb +44 -0
- data/manual/text/color.rb +24 -0
- data/manual/text/column_box.rb +32 -0
- data/manual/text/fallback_fonts.rb +37 -0
- data/manual/text/font.rb +41 -0
- data/manual/text/font_size.rb +45 -0
- data/manual/text/font_style.rb +23 -0
- data/manual/text/formatted_callbacks.rb +60 -0
- data/manual/text/formatted_text.rb +50 -0
- data/manual/text/free_flowing_text.rb +51 -0
- data/manual/text/group.rb +29 -0
- data/manual/text/inline.rb +43 -0
- data/manual/text/kerning_and_character_spacing.rb +39 -0
- data/manual/text/leading.rb +25 -0
- data/manual/text/line_wrapping.rb +41 -0
- data/manual/text/paragraph_indentation.rb +26 -0
- data/manual/text/positioned_text.rb +38 -0
- data/manual/text/registering_families.rb +48 -0
- data/manual/text/rendering_and_color.rb +37 -0
- data/manual/text/right_to_left_text.rb +43 -0
- data/manual/text/rotation.rb +43 -0
- data/manual/text/single_usage.rb +37 -0
- data/manual/text/text.rb +75 -0
- data/manual/text/text_box_excess.rb +32 -0
- data/manual/text/text_box_extensions.rb +45 -0
- data/manual/text/text_box_overflow.rb +44 -0
- data/manual/text/utf8.rb +28 -0
- data/manual/text/win_ansi_charset.rb +59 -0
- data/prawn.gemspec +46 -0
- data/spec/annotations_spec.rb +90 -0
- data/spec/bounding_box_spec.rb +493 -0
- data/spec/cell_spec.rb +584 -0
- data/spec/column_box_spec.rb +33 -0
- data/spec/data/curves.pdf +66 -0
- data/spec/destinations_spec.rb +15 -0
- data/spec/document_spec.rb +736 -0
- data/spec/extensions/encoding_helpers.rb +6 -0
- data/spec/extensions/mocha.rb +44 -0
- data/spec/font_spec.rb +433 -0
- data/spec/formatted_text_arranger_spec.rb +421 -0
- data/spec/formatted_text_box_spec.rb +640 -0
- data/spec/formatted_text_fragment_spec.rb +298 -0
- data/spec/graphics_spec.rb +651 -0
- data/spec/grid_spec.rb +85 -0
- data/spec/images_spec.rb +140 -0
- data/spec/inline_formatted_text_parser_spec.rb +515 -0
- data/spec/jpg_spec.rb +25 -0
- data/spec/line_wrap_spec.rb +333 -0
- data/spec/measurement_units_spec.rb +23 -0
- data/spec/name_tree_spec.rb +112 -0
- data/spec/object_store_spec.rb +170 -0
- data/spec/outline_spec.rb +426 -0
- data/spec/pdf_object_spec.rb +172 -0
- data/spec/png_spec.rb +240 -0
- data/spec/reference_spec.rb +105 -0
- data/spec/repeater_spec.rb +158 -0
- data/spec/security_spec.rb +126 -0
- data/spec/snapshot_spec.rb +186 -0
- data/spec/soft_mask_spec.rb +117 -0
- data/spec/span_spec.rb +49 -0
- data/spec/spec_helper.rb +36 -0
- data/spec/stamp_spec.rb +159 -0
- data/spec/stroke_styles_spec.rb +193 -0
- data/spec/table_spec.rb +1209 -0
- data/spec/template_spec.rb +351 -0
- data/spec/text_at_spec.rb +129 -0
- data/spec/text_box_spec.rb +1029 -0
- data/spec/text_rendering_mode_spec.rb +45 -0
- data/spec/text_spacing_spec.rb +93 -0
- data/spec/text_spec.rb +421 -0
- data/spec/text_with_inline_formatting_spec.rb +35 -0
- data/spec/transparency_spec.rb +89 -0
- metadata +544 -0
@@ -0,0 +1,90 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# prawn/core/destinations.rb : Implements destination support for PDF
|
4
|
+
#
|
5
|
+
# Copyright November 2008, Jamis Buck. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
9
|
+
module Prawn
|
10
|
+
module Core
|
11
|
+
module Destinations #:nodoc:
|
12
|
+
|
13
|
+
# The maximum number of children to fit into a single node in the Dests tree.
|
14
|
+
NAME_TREE_CHILDREN_LIMIT = 20 #:nodoc:
|
15
|
+
|
16
|
+
# The Dests name tree in the Name dictionary (see Prawn::Document::Internal#names).
|
17
|
+
# This name tree is used to store named destinations (PDF spec 8.2.1).
|
18
|
+
# (For more on name trees, see section 3.8.4 in the PDF spec.)
|
19
|
+
#
|
20
|
+
def dests
|
21
|
+
names.data[:Dests] ||= ref!(Prawn::Core::NameTree::Node.new(self, NAME_TREE_CHILDREN_LIMIT))
|
22
|
+
end
|
23
|
+
|
24
|
+
# Adds a new destination to the dests name tree (see #dests). The
|
25
|
+
# +reference+ parameter will be converted into a Prawn::Reference if
|
26
|
+
# it is not already one.
|
27
|
+
#
|
28
|
+
def add_dest(name, reference)
|
29
|
+
reference = ref!(reference) unless reference.is_a?(Prawn::Core::Reference)
|
30
|
+
dests.data.add(name, reference)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Return a Dest specification for a specific location (and optional zoom
|
34
|
+
# level).
|
35
|
+
#
|
36
|
+
def dest_xyz(left, top, zoom=nil, dest_page=page)
|
37
|
+
[dest_page.dictionary, :XYZ, left, top, zoom]
|
38
|
+
end
|
39
|
+
|
40
|
+
# Return a Dest specification that will fit the given page into the
|
41
|
+
# viewport.
|
42
|
+
#
|
43
|
+
def dest_fit(dest_page=page)
|
44
|
+
[dest_page.dictionary, :Fit]
|
45
|
+
end
|
46
|
+
|
47
|
+
# Return a Dest specification that will fit the given page horizontally
|
48
|
+
# into the viewport, aligned vertically at the given top coordinate.
|
49
|
+
#
|
50
|
+
def dest_fit_horizontally(top, dest_page=page)
|
51
|
+
[dest_page.dictionary, :FitH, top]
|
52
|
+
end
|
53
|
+
|
54
|
+
# Return a Dest specification that will fit the given page vertically
|
55
|
+
# into the viewport, aligned horizontally at the given left coordinate.
|
56
|
+
#
|
57
|
+
def dest_fit_vertically(left, dest_page=page)
|
58
|
+
[dest_page.dictionary, :FitV, left]
|
59
|
+
end
|
60
|
+
|
61
|
+
# Return a Dest specification that will fit the given rectangle into the
|
62
|
+
# viewport, for the given page.
|
63
|
+
#
|
64
|
+
def dest_fit_rect(left, bottom, right, top, dest_page=page)
|
65
|
+
[dest_page.dictionary, :FitR, left, bottom, right, top]
|
66
|
+
end
|
67
|
+
|
68
|
+
# Return a Dest specfication that will fit the given page's bounding box
|
69
|
+
# into the viewport.
|
70
|
+
#
|
71
|
+
def dest_fit_bounds(dest_page=page)
|
72
|
+
[dest_page.dictionary, :FitB]
|
73
|
+
end
|
74
|
+
|
75
|
+
# Same as #dest_fit_horizontally, but works on the page's bounding box
|
76
|
+
# instead of the entire page.
|
77
|
+
#
|
78
|
+
def dest_fit_bounds_horizontally(top, dest_page=page)
|
79
|
+
[dest_page.dictionary, :FitBH, top]
|
80
|
+
end
|
81
|
+
|
82
|
+
# Same as #dest_fit_vertically, but works on the page's bounding box
|
83
|
+
# instead of the entire page.
|
84
|
+
#
|
85
|
+
def dest_fit_bounds_vertically(left, dest_page=page)
|
86
|
+
[dest_page.dictionary, :FitBV, left]
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Prawn
|
2
|
+
module Core
|
3
|
+
class DocumentState #:nodoc:
|
4
|
+
def initialize(options)
|
5
|
+
normalize_metadata(options)
|
6
|
+
|
7
|
+
if options[:template]
|
8
|
+
@store = Prawn::Core::ObjectStore.new(:template => options[:template])
|
9
|
+
@store.info.data.merge!(options[:info]) if options[:info]
|
10
|
+
else
|
11
|
+
@store = Prawn::Core::ObjectStore.new(:info => options[:info])
|
12
|
+
end
|
13
|
+
|
14
|
+
@version = 1.3
|
15
|
+
@pages = []
|
16
|
+
@page = nil
|
17
|
+
@trailer = {}
|
18
|
+
@compress = options.fetch(:compress, false)
|
19
|
+
@encrypt = options.fetch(:encrypt, false)
|
20
|
+
@encryption_key = options[:encryption_key]
|
21
|
+
@optimize_objects = options.fetch(:optimize_objects, false)
|
22
|
+
@skip_encoding = options.fetch(:skip_encoding, false)
|
23
|
+
@before_render_callbacks = []
|
24
|
+
@on_page_create_callback = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_accessor :store, :version, :pages, :page, :trailer, :compress,
|
28
|
+
:encrypt, :encryption_key, :optimize_objects, :skip_encoding,
|
29
|
+
:before_render_callbacks, :on_page_create_callback
|
30
|
+
|
31
|
+
def populate_pages_from_store(document)
|
32
|
+
return 0 if @store.page_count <= 0 || @pages.size > 0
|
33
|
+
|
34
|
+
count = (1..@store.page_count)
|
35
|
+
@pages = count.map do |index|
|
36
|
+
orig_dict_id = @store.object_id_for_page(index)
|
37
|
+
Prawn::Core::Page.new(document, :object_id => orig_dict_id)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
def normalize_metadata(options)
|
43
|
+
options[:info] ||= {}
|
44
|
+
options[:info][:Creator] ||= "Prawn"
|
45
|
+
options[:info][:Producer] ||= "Prawn"
|
46
|
+
|
47
|
+
info = options[:info]
|
48
|
+
end
|
49
|
+
|
50
|
+
def insert_page(page, page_number)
|
51
|
+
pages.insert(page_number, page)
|
52
|
+
store.pages.data[:Kids].insert(page_number, page.dictionary)
|
53
|
+
store.pages.data[:Count] += 1
|
54
|
+
end
|
55
|
+
|
56
|
+
def on_page_create_action(doc)
|
57
|
+
on_page_create_callback[doc] if on_page_create_callback
|
58
|
+
end
|
59
|
+
|
60
|
+
def before_render_actions(doc)
|
61
|
+
before_render_callbacks.each{ |c| c.call(self) }
|
62
|
+
end
|
63
|
+
|
64
|
+
def page_count
|
65
|
+
pages.length
|
66
|
+
end
|
67
|
+
|
68
|
+
def render_body(output)
|
69
|
+
store.compact if optimize_objects
|
70
|
+
store.each do |ref|
|
71
|
+
ref.offset = output.size
|
72
|
+
output << (@encrypt ? ref.encrypted_object(@encryption_key) :
|
73
|
+
ref.object)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Prawn
|
3
|
+
module Core
|
4
|
+
# This is used to differentiate strings that must be encoded as
|
5
|
+
# a *literal* string, versus those that can be encoded in
|
6
|
+
# the PDF hexadecimal format.
|
7
|
+
#
|
8
|
+
# Some features of the PDF format appear to require that literal
|
9
|
+
# strings be used. One such feature is the /Dest key of a link
|
10
|
+
# annotation; if a hex encoded string is used there, the links
|
11
|
+
# do not work (as tested in Mac OS X Preview, and Adobe Acrobat
|
12
|
+
# Reader).
|
13
|
+
class LiteralString < String #:nodoc:
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,177 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# name_tree.rb : Implements NameTree for PDF
|
4
|
+
#
|
5
|
+
# Copyright November 2008, Jamis Buck. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
#
|
9
|
+
module Prawn
|
10
|
+
module Core
|
11
|
+
module NameTree #:nodoc:
|
12
|
+
class Node #:nodoc:
|
13
|
+
attr_reader :children
|
14
|
+
attr_reader :limit
|
15
|
+
attr_reader :document
|
16
|
+
attr_accessor :parent
|
17
|
+
attr_accessor :ref
|
18
|
+
|
19
|
+
def initialize(document, limit, parent=nil)
|
20
|
+
@document = document
|
21
|
+
@children = []
|
22
|
+
@limit = limit
|
23
|
+
@parent = parent
|
24
|
+
@ref = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def empty?
|
28
|
+
children.empty?
|
29
|
+
end
|
30
|
+
|
31
|
+
def size
|
32
|
+
leaf? ? children.size : children.inject(0) { |sum, child| sum + child.size }
|
33
|
+
end
|
34
|
+
|
35
|
+
def leaf?
|
36
|
+
children.empty? || children.first.is_a?(Value)
|
37
|
+
end
|
38
|
+
|
39
|
+
def add(name, value)
|
40
|
+
self << Value.new(name, value)
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_hash
|
44
|
+
hash = {}
|
45
|
+
|
46
|
+
hash[:Limits] = [least, greatest] if parent
|
47
|
+
if leaf?
|
48
|
+
hash[:Names] = children if leaf?
|
49
|
+
else
|
50
|
+
hash[:Kids] = children.map { |child| child.ref }
|
51
|
+
end
|
52
|
+
|
53
|
+
return hash
|
54
|
+
end
|
55
|
+
|
56
|
+
def least
|
57
|
+
if leaf?
|
58
|
+
children.first.name
|
59
|
+
else
|
60
|
+
children.first.least
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def greatest
|
65
|
+
if leaf?
|
66
|
+
children.last.name
|
67
|
+
else
|
68
|
+
children.last.greatest
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def <<(value)
|
73
|
+
if children.empty?
|
74
|
+
children << value
|
75
|
+
elsif leaf?
|
76
|
+
children.insert(insertion_point(value), value)
|
77
|
+
split! if children.length > limit
|
78
|
+
else
|
79
|
+
fit = children.detect { |child| child >= value }
|
80
|
+
fit = children.last unless fit
|
81
|
+
fit << value
|
82
|
+
end
|
83
|
+
|
84
|
+
value
|
85
|
+
end
|
86
|
+
|
87
|
+
def >=(value)
|
88
|
+
children.empty? || children.last >= value
|
89
|
+
end
|
90
|
+
|
91
|
+
def split!
|
92
|
+
if parent
|
93
|
+
parent.split(self)
|
94
|
+
else
|
95
|
+
left, right = new_node(self), new_node(self)
|
96
|
+
split_children(self, left, right)
|
97
|
+
children.replace([left, right])
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Returns a deep copy of this node, without copying expensive things
|
102
|
+
# like the ref to @document.
|
103
|
+
#
|
104
|
+
def deep_copy
|
105
|
+
node = dup
|
106
|
+
node.instance_variable_set("@children",
|
107
|
+
Marshal.load(Marshal.dump(children)))
|
108
|
+
node.instance_variable_set("@ref",
|
109
|
+
node.ref ? node.ref.deep_copy : nil)
|
110
|
+
node
|
111
|
+
end
|
112
|
+
|
113
|
+
protected
|
114
|
+
|
115
|
+
def split(node)
|
116
|
+
new_child = new_node(self)
|
117
|
+
split_children(node, node, new_child)
|
118
|
+
index = children.index(node)
|
119
|
+
children.insert(index+1, new_child)
|
120
|
+
split! if children.length > limit
|
121
|
+
end
|
122
|
+
|
123
|
+
private
|
124
|
+
|
125
|
+
def new_node(parent=nil)
|
126
|
+
node = Node.new(document, limit, parent)
|
127
|
+
node.ref = document.ref!(node)
|
128
|
+
return node
|
129
|
+
end
|
130
|
+
|
131
|
+
def split_children(node, left, right)
|
132
|
+
half = (node.limit+1)/2
|
133
|
+
|
134
|
+
left_children, right_children = node.children[0...half], node.children[half..-1]
|
135
|
+
|
136
|
+
left.children.replace(left_children)
|
137
|
+
right.children.replace(right_children)
|
138
|
+
|
139
|
+
unless node.leaf?
|
140
|
+
left_children.each { |child| child.parent = left }
|
141
|
+
right_children.each { |child| child.parent = right }
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def insertion_point(value)
|
146
|
+
children.each_with_index do |child, index|
|
147
|
+
return index if child >= value
|
148
|
+
end
|
149
|
+
return children.length
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
class Value #:nodoc:
|
154
|
+
include Comparable
|
155
|
+
|
156
|
+
attr_reader :name
|
157
|
+
attr_reader :value
|
158
|
+
|
159
|
+
def initialize(name, value)
|
160
|
+
@name, @value = Prawn::Core::LiteralString.new(name), value
|
161
|
+
end
|
162
|
+
|
163
|
+
def <=>(leaf)
|
164
|
+
name <=> leaf.name
|
165
|
+
end
|
166
|
+
|
167
|
+
def inspect
|
168
|
+
"#<Value: #{name.inspect} : #{value.inspect}>"
|
169
|
+
end
|
170
|
+
|
171
|
+
def to_s
|
172
|
+
"#{name} : #{value}"
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
@@ -0,0 +1,320 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# prawn/core/object_store.rb : Implements PDF object repository for Prawn
|
4
|
+
#
|
5
|
+
# Copyright August 2009, Brad Ediger. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
9
|
+
|
10
|
+
require 'pdf/reader'
|
11
|
+
|
12
|
+
module Prawn
|
13
|
+
module Core
|
14
|
+
class ObjectStore #:nodoc:
|
15
|
+
include Enumerable
|
16
|
+
|
17
|
+
attr_reader :min_version
|
18
|
+
|
19
|
+
BASE_OBJECTS = %w[info pages root]
|
20
|
+
|
21
|
+
def initialize(opts = {})
|
22
|
+
@objects = {}
|
23
|
+
@identifiers = []
|
24
|
+
|
25
|
+
load_file(opts[:template]) if opts[:template]
|
26
|
+
|
27
|
+
@info ||= ref(opts[:info] || {}).identifier
|
28
|
+
@root ||= ref(:Type => :Catalog).identifier
|
29
|
+
if pages.nil?
|
30
|
+
root.data[:Pages] = ref(:Type => :Pages, :Count => 0, :Kids => [])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def ref(data, &block)
|
35
|
+
push(size + 1, data, &block)
|
36
|
+
end
|
37
|
+
|
38
|
+
def info
|
39
|
+
@objects[@info]
|
40
|
+
end
|
41
|
+
|
42
|
+
def root
|
43
|
+
@objects[@root]
|
44
|
+
end
|
45
|
+
|
46
|
+
def pages
|
47
|
+
root.data[:Pages]
|
48
|
+
end
|
49
|
+
|
50
|
+
def page_count
|
51
|
+
pages.data[:Count]
|
52
|
+
end
|
53
|
+
|
54
|
+
# Adds the given reference to the store and returns the reference object.
|
55
|
+
# If the object provided is not a Prawn::Core::Reference, one is created from the
|
56
|
+
# arguments provided.
|
57
|
+
#
|
58
|
+
def push(*args, &block)
|
59
|
+
reference = if args.first.is_a?(Prawn::Core::Reference)
|
60
|
+
args.first
|
61
|
+
else
|
62
|
+
Prawn::Core::Reference.new(*args, &block)
|
63
|
+
end
|
64
|
+
|
65
|
+
@objects[reference.identifier] = reference
|
66
|
+
@identifiers << reference.identifier
|
67
|
+
reference
|
68
|
+
end
|
69
|
+
|
70
|
+
alias_method :<<, :push
|
71
|
+
|
72
|
+
def each
|
73
|
+
@identifiers.each do |id|
|
74
|
+
yield @objects[id]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def [](id)
|
79
|
+
@objects[id]
|
80
|
+
end
|
81
|
+
|
82
|
+
def size
|
83
|
+
@identifiers.size
|
84
|
+
end
|
85
|
+
alias_method :length, :size
|
86
|
+
|
87
|
+
def compact
|
88
|
+
# Clear live markers
|
89
|
+
each { |o| o.live = false }
|
90
|
+
|
91
|
+
# Recursively mark reachable objects live, starting from the roots
|
92
|
+
# (the only objects referenced in the trailer)
|
93
|
+
root.mark_live
|
94
|
+
info.mark_live
|
95
|
+
|
96
|
+
# Renumber live objects to eliminate gaps (shrink the xref table)
|
97
|
+
if @objects.any?{ |_, o| !o.live }
|
98
|
+
new_id = 1
|
99
|
+
new_objects = {}
|
100
|
+
new_identifiers = []
|
101
|
+
|
102
|
+
each do |obj|
|
103
|
+
if obj.live
|
104
|
+
obj.identifier = new_id
|
105
|
+
new_objects[new_id] = obj
|
106
|
+
new_identifiers << new_id
|
107
|
+
new_id += 1
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
@objects = new_objects
|
112
|
+
@identifiers = new_identifiers
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# returns the object ID for a particular page in the document. Pages
|
117
|
+
# are indexed starting at 1 (not 0!).
|
118
|
+
#
|
119
|
+
# object_id_for_page(1)
|
120
|
+
# => 5
|
121
|
+
# object_id_for_page(10)
|
122
|
+
# => 87
|
123
|
+
# object_id_for_page(-11)
|
124
|
+
# => 17
|
125
|
+
#
|
126
|
+
def object_id_for_page(k)
|
127
|
+
k -= 1 if k > 0
|
128
|
+
flat_page_ids = get_page_objects(pages).flatten
|
129
|
+
flat_page_ids[k]
|
130
|
+
end
|
131
|
+
|
132
|
+
# imports all objects required to render a page from another PDF. The
|
133
|
+
# objects are added to the current object store, but NOT linked
|
134
|
+
# anywhere.
|
135
|
+
#
|
136
|
+
# The object ID of the root Page object is returned, it's up to the
|
137
|
+
# calling code to link that into the document structure somewhere. If
|
138
|
+
# this isn't done the imported objects will just be removed when the
|
139
|
+
# store is compacted.
|
140
|
+
#
|
141
|
+
# Imports nothing and returns nil if the requested page number doesn't
|
142
|
+
# exist. page_num is 1 indexed, so 1 indicates the first page.
|
143
|
+
#
|
144
|
+
def import_page(input, page_num)
|
145
|
+
@loaded_objects = {}
|
146
|
+
if template_id = indexed_template(input, page_num)
|
147
|
+
return template_id
|
148
|
+
end
|
149
|
+
|
150
|
+
io = if input.respond_to?(:seek) && input.respond_to?(:read)
|
151
|
+
input
|
152
|
+
elsif File.file?(input.to_s)
|
153
|
+
StringIO.new(File.binread(input.to_s))
|
154
|
+
else
|
155
|
+
raise ArgumentError, "input must be an IO-like object or a filename"
|
156
|
+
end
|
157
|
+
|
158
|
+
# unless File.file?(filename)
|
159
|
+
# raise ArgumentError, "#{filename} does not exist"
|
160
|
+
# end
|
161
|
+
|
162
|
+
hash = indexed_hash(input, io)
|
163
|
+
ref = hash.page_references[page_num - 1]
|
164
|
+
|
165
|
+
if ref.nil?
|
166
|
+
nil
|
167
|
+
else
|
168
|
+
index_template(input, page_num, load_object_graph(hash, ref).identifier)
|
169
|
+
end
|
170
|
+
|
171
|
+
rescue PDF::Reader::MalformedPDFError, PDF::Reader::InvalidObjectError
|
172
|
+
msg = "Error reading template file. If you are sure it's a valid PDF, it may be a bug."
|
173
|
+
raise Prawn::Errors::TemplateError, msg
|
174
|
+
rescue PDF::Reader::UnsupportedFeatureError
|
175
|
+
msg = "Template file contains unsupported PDF features"
|
176
|
+
raise Prawn::Errors::TemplateError, msg
|
177
|
+
end
|
178
|
+
|
179
|
+
private
|
180
|
+
|
181
|
+
# An index for page templates so that their loaded object graph
|
182
|
+
# can be reused without multiple loading
|
183
|
+
def template_index
|
184
|
+
@template_index ||= {}
|
185
|
+
end
|
186
|
+
|
187
|
+
# An index for the read object hash of a pdf template so that the
|
188
|
+
# object hash does not need to be parsed multiple times when using
|
189
|
+
# different pages of the pdf as page templates
|
190
|
+
def hash_index
|
191
|
+
@hash_index ||= {}
|
192
|
+
end
|
193
|
+
|
194
|
+
# returns the indexed object graph identifier for a template page if
|
195
|
+
# it exists
|
196
|
+
def indexed_template(input, page_number)
|
197
|
+
key = indexing_key(input)
|
198
|
+
template_index[key] && template_index[key][page_number]
|
199
|
+
end
|
200
|
+
|
201
|
+
# indexes the identifier for a page from a template
|
202
|
+
def index_template(input, page_number, id)
|
203
|
+
(template_index[indexing_key(input)] ||= {})[page_number] ||= id
|
204
|
+
end
|
205
|
+
|
206
|
+
# reads and indexes a new IO for a template
|
207
|
+
# if the IO has been indexed already then the parsed object hash
|
208
|
+
# is returned directly
|
209
|
+
def indexed_hash(input, io)
|
210
|
+
hash_index[indexing_key(input)] ||= PDF::Reader::ObjectHash.new(io)
|
211
|
+
end
|
212
|
+
|
213
|
+
# the index key for the input.
|
214
|
+
# uses object_id so that both a string filename or an IO stream can be
|
215
|
+
# indexed and reused provided the same object gets used in multiple page
|
216
|
+
# template calls.
|
217
|
+
def indexing_key(input)
|
218
|
+
input.object_id
|
219
|
+
end
|
220
|
+
|
221
|
+
# returns a nested array of object IDs for all pages in this object store.
|
222
|
+
#
|
223
|
+
def get_page_objects(obj)
|
224
|
+
if obj.data[:Type] == :Page
|
225
|
+
obj.identifier
|
226
|
+
elsif obj.data[:Type] == :Pages
|
227
|
+
obj.data[:Kids].map { |kid| get_page_objects(kid) }
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
# takes a source PDF and uses it as a template for this document.
|
232
|
+
#
|
233
|
+
def load_file(template)
|
234
|
+
unless (template.respond_to?(:seek) && template.respond_to?(:read)) ||
|
235
|
+
File.file?(template)
|
236
|
+
raise ArgumentError, "#{template} does not exist"
|
237
|
+
end
|
238
|
+
|
239
|
+
hash = PDF::Reader::ObjectHash.new(template)
|
240
|
+
src_info = hash.trailer[:Info]
|
241
|
+
src_root = hash.trailer[:Root]
|
242
|
+
@min_version = hash.pdf_version.to_f
|
243
|
+
|
244
|
+
if hash.trailer[:Encrypt]
|
245
|
+
msg = "Template file is an encrypted PDF, it can't be used as a template"
|
246
|
+
raise Prawn::Errors::TemplateError, msg
|
247
|
+
end
|
248
|
+
|
249
|
+
if src_info
|
250
|
+
@info = load_object_graph(hash, src_info).identifier
|
251
|
+
end
|
252
|
+
|
253
|
+
if src_root
|
254
|
+
@root = load_object_graph(hash, src_root).identifier
|
255
|
+
end
|
256
|
+
rescue PDF::Reader::MalformedPDFError, PDF::Reader::InvalidObjectError
|
257
|
+
msg = "Error reading template file. If you are sure it's a valid PDF, it may be a bug."
|
258
|
+
raise Prawn::Errors::TemplateError, msg
|
259
|
+
rescue PDF::Reader::UnsupportedFeatureError
|
260
|
+
msg = "Template file contains unsupported PDF features"
|
261
|
+
raise Prawn::Errors::TemplateError, msg
|
262
|
+
end
|
263
|
+
|
264
|
+
# recurse down an object graph from a source PDF, importing all the
|
265
|
+
# indirect objects we find.
|
266
|
+
#
|
267
|
+
# hash is the PDF::Reader::ObjectHash to extract objects from, object is
|
268
|
+
# the object to extract.
|
269
|
+
#
|
270
|
+
def load_object_graph(hash, object)
|
271
|
+
@loaded_objects ||= {}
|
272
|
+
case object
|
273
|
+
when ::Hash then
|
274
|
+
object.each { |key,value| object[key] = load_object_graph(hash, value) }
|
275
|
+
object
|
276
|
+
when Array then
|
277
|
+
object.map { |item| load_object_graph(hash, item)}
|
278
|
+
when PDF::Reader::Reference then
|
279
|
+
unless @loaded_objects.has_key?(object.id)
|
280
|
+
@loaded_objects[object.id] = ref(nil)
|
281
|
+
new_obj = load_object_graph(hash, hash[object])
|
282
|
+
if new_obj.kind_of?(PDF::Reader::Stream)
|
283
|
+
stream_dict = load_object_graph(hash, new_obj.hash)
|
284
|
+
@loaded_objects[object.id].data = stream_dict
|
285
|
+
@loaded_objects[object.id] << new_obj.data
|
286
|
+
else
|
287
|
+
@loaded_objects[object.id].data = new_obj
|
288
|
+
end
|
289
|
+
end
|
290
|
+
@loaded_objects[object.id]
|
291
|
+
when PDF::Reader::Stream
|
292
|
+
# Stream is a subclass of string, so this is here to prevent the stream
|
293
|
+
# being wrapped in a LiteralString
|
294
|
+
object
|
295
|
+
when String
|
296
|
+
is_utf8?(object) ? object : Prawn::Core::ByteString.new(object)
|
297
|
+
else
|
298
|
+
object
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
ruby_18 do
|
303
|
+
def is_utf8?(str)
|
304
|
+
begin
|
305
|
+
str.unpack("U*")
|
306
|
+
true
|
307
|
+
rescue
|
308
|
+
false
|
309
|
+
end
|
310
|
+
end
|
311
|
+
end
|
312
|
+
ruby_19 do
|
313
|
+
def is_utf8?(str)
|
314
|
+
str.force_encoding("utf-8")
|
315
|
+
str.valid_encoding?
|
316
|
+
end
|
317
|
+
end
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|