prawn 0.15.0 → 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +2 -2
- data/LICENSE +1 -1
- data/README.md +96 -0
- data/Rakefile +27 -30
- 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/DejaVuSans.ttf +0 -0
- data/data/fonts/Dustismo_Roman.ttf +0 -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/dice.alpha +0 -0
- data/data/images/dice.dat +0 -0
- data/data/images/page_white_text.alpha +0 -0
- data/data/images/page_white_text.dat +0 -0
- data/data/images/rails.dat +0 -0
- data/data/images/rails.png +0 -0
- data/data/pdfs/nested_pages.pdf +13 -13
- data/lib/prawn.rb +21 -85
- data/lib/prawn/compatibility.rb +51 -0
- data/lib/prawn/core.rb +85 -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 +78 -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 +264 -0
- data/lib/prawn/core/page.rb +215 -0
- data/lib/prawn/core/pdf_object.rb +108 -0
- data/lib/prawn/core/reference.rb +115 -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 +273 -0
- data/lib/prawn/core/text/formatted/wrap.rb +153 -0
- data/lib/prawn/document.rb +122 -155
- data/lib/prawn/document/bounding_box.rb +7 -36
- data/lib/prawn/document/column_box.rb +10 -38
- data/lib/prawn/document/graphics_state.rb +74 -11
- data/lib/prawn/document/internals.rb +23 -24
- data/lib/prawn/document/page_geometry.rb +136 -0
- data/lib/prawn/document/snapshot.rb +6 -7
- data/lib/prawn/document/span.rb +10 -12
- data/lib/prawn/encoding.rb +10 -9
- data/lib/prawn/errors.rb +30 -15
- data/lib/prawn/font.rb +104 -136
- data/lib/prawn/font/afm.rb +44 -46
- data/lib/prawn/font/dfont.rb +3 -4
- data/lib/prawn/font/ttf.rb +50 -31
- data/lib/prawn/graphics.rb +57 -302
- data/lib/prawn/graphics/cap_style.rb +3 -4
- data/lib/prawn/graphics/color.rb +5 -13
- data/lib/prawn/graphics/dash.rb +31 -53
- data/lib/prawn/graphics/gradient.rb +84 -0
- data/lib/prawn/graphics/join_style.rb +7 -9
- data/lib/prawn/graphics/transformation.rb +9 -10
- data/lib/prawn/graphics/transparency.rb +1 -3
- data/lib/prawn/images.rb +59 -69
- data/lib/prawn/images/image.rb +22 -6
- data/lib/prawn/images/jpg.rb +14 -20
- data/lib/prawn/images/png.rb +118 -61
- data/lib/prawn/layout.rb +15 -10
- data/lib/prawn/layout/grid.rb +54 -66
- data/lib/prawn/measurement_extensions.rb +6 -10
- data/lib/prawn/measurements.rb +21 -27
- data/lib/prawn/outline.rb +308 -6
- data/lib/prawn/repeater.rb +8 -10
- data/lib/prawn/security.rb +33 -55
- data/lib/prawn/security/arcfour.rb +0 -1
- data/lib/prawn/stamp.rb +3 -5
- data/lib/prawn/table.rb +60 -188
- data/lib/prawn/table/cell.rb +44 -272
- data/lib/prawn/table/cell/image.rb +3 -2
- data/lib/prawn/table/cell/in_table.rb +2 -4
- data/lib/prawn/table/cell/subtable.rb +2 -2
- data/lib/prawn/table/cell/text.rb +18 -41
- data/lib/prawn/table/cells.rb +48 -142
- data/lib/prawn/text.rb +25 -32
- data/lib/prawn/text/box.rb +6 -12
- data/lib/prawn/text/formatted.rb +4 -5
- data/lib/prawn/text/formatted/box.rb +59 -96
- data/lib/prawn/text/formatted/fragment.rb +23 -34
- data/lib/prawn/text/formatted/parser.rb +5 -15
- data/prawn.gemspec +13 -24
- data/spec/annotations_spec.rb +32 -16
- data/spec/bounding_box_spec.rb +17 -119
- data/spec/cell_spec.rb +42 -112
- data/spec/destinations_spec.rb +5 -5
- data/spec/document_spec.rb +111 -155
- data/spec/extensions/mocha.rb +0 -1
- data/spec/font_spec.rb +99 -149
- data/spec/formatted_text_arranger_spec.rb +43 -43
- data/spec/formatted_text_box_spec.rb +44 -43
- data/spec/formatted_text_fragment_spec.rb +8 -8
- data/spec/graphics_spec.rb +68 -151
- data/spec/grid_spec.rb +15 -26
- data/spec/images_spec.rb +30 -51
- data/spec/inline_formatted_text_parser_spec.rb +20 -69
- data/spec/jpg_spec.rb +4 -4
- data/spec/line_wrap_spec.rb +28 -28
- data/spec/measurement_units_spec.rb +6 -6
- data/spec/name_tree_spec.rb +112 -0
- data/spec/object_store_spec.rb +106 -17
- data/spec/outline_spec.rb +63 -103
- data/spec/pdf_object_spec.rb +170 -0
- data/spec/png_spec.rb +25 -25
- data/spec/reference_spec.rb +65 -8
- data/spec/repeater_spec.rb +10 -10
- data/spec/security_spec.rb +12 -44
- data/spec/snapshot_spec.rb +7 -7
- data/spec/span_spec.rb +15 -10
- data/spec/spec_helper.rb +8 -32
- data/spec/stamp_spec.rb +30 -29
- data/spec/stroke_styles_spec.rb +18 -36
- data/spec/table_spec.rb +111 -706
- data/spec/template_spec.rb +297 -0
- data/spec/text_at_spec.rb +33 -19
- data/spec/text_box_spec.rb +64 -100
- data/spec/text_rendering_mode_spec.rb +5 -5
- data/spec/text_spacing_spec.rb +4 -4
- data/spec/text_spec.rb +64 -84
- data/spec/transparency_spec.rb +5 -5
- metadata +290 -463
- checksums.yaml +0 -7
- data/.yardopts +0 -10
- data/Gemfile +0 -11
- data/data/images/16bit.color +0 -0
- data/data/images/dice.color +0 -0
- data/data/images/indexed_color.dat +0 -0
- data/data/images/indexed_color.png +0 -0
- data/data/images/page_white_text.color +0 -0
- data/lib/prawn/font_metric_cache.rb +0 -47
- data/lib/prawn/graphics/patterns.rb +0 -138
- data/lib/prawn/image_handler.rb +0 -36
- data/lib/prawn/soft_mask.rb +0 -96
- data/lib/prawn/table/cell/span_dummy.rb +0 -93
- data/lib/prawn/table/column_width_calculator.rb +0 -61
- data/lib/prawn/text/formatted/arranger.rb +0 -290
- data/lib/prawn/text/formatted/line_wrap.rb +0 -266
- data/lib/prawn/text/formatted/wrap.rb +0 -150
- data/lib/prawn/utilities.rb +0 -46
- data/manual/basic_concepts/adding_pages.rb +0 -27
- data/manual/basic_concepts/basic_concepts.rb +0 -34
- data/manual/basic_concepts/creation.rb +0 -39
- data/manual/basic_concepts/cursor.rb +0 -33
- data/manual/basic_concepts/measurement.rb +0 -25
- data/manual/basic_concepts/origin.rb +0 -38
- data/manual/basic_concepts/other_cursor_helpers.rb +0 -40
- data/manual/bounding_box/bounding_box.rb +0 -39
- data/manual/bounding_box/bounds.rb +0 -49
- data/manual/bounding_box/canvas.rb +0 -24
- data/manual/bounding_box/creation.rb +0 -23
- data/manual/bounding_box/indentation.rb +0 -46
- data/manual/bounding_box/nesting.rb +0 -45
- data/manual/bounding_box/russian_boxes.rb +0 -40
- data/manual/bounding_box/stretchy.rb +0 -31
- data/manual/document_and_page_options/background.rb +0 -27
- data/manual/document_and_page_options/document_and_page_options.rb +0 -32
- data/manual/document_and_page_options/metadata.rb +0 -23
- data/manual/document_and_page_options/page_margins.rb +0 -38
- data/manual/document_and_page_options/page_size.rb +0 -34
- data/manual/document_and_page_options/print_scaling.rb +0 -20
- data/manual/example_file.rb +0 -111
- data/manual/example_helper.rb +0 -411
- data/manual/example_package.rb +0 -53
- data/manual/example_section.rb +0 -46
- data/manual/graphics/circle_and_ellipse.rb +0 -22
- data/manual/graphics/color.rb +0 -24
- data/manual/graphics/common_lines.rb +0 -30
- data/manual/graphics/fill_and_stroke.rb +0 -42
- data/manual/graphics/fill_rules.rb +0 -37
- data/manual/graphics/gradients.rb +0 -37
- data/manual/graphics/graphics.rb +0 -58
- data/manual/graphics/helper.rb +0 -24
- data/manual/graphics/line_width.rb +0 -35
- data/manual/graphics/lines_and_curves.rb +0 -41
- data/manual/graphics/polygon.rb +0 -29
- data/manual/graphics/rectangle.rb +0 -21
- data/manual/graphics/rotate.rb +0 -28
- data/manual/graphics/scale.rb +0 -41
- data/manual/graphics/soft_masks.rb +0 -46
- data/manual/graphics/stroke_cap.rb +0 -31
- data/manual/graphics/stroke_dash.rb +0 -48
- data/manual/graphics/stroke_join.rb +0 -30
- data/manual/graphics/translate.rb +0 -29
- data/manual/graphics/transparency.rb +0 -35
- data/manual/images/absolute_position.rb +0 -23
- data/manual/images/fit.rb +0 -21
- data/manual/images/horizontal.rb +0 -25
- data/manual/images/images.rb +0 -40
- data/manual/images/plain_image.rb +0 -18
- data/manual/images/scale.rb +0 -22
- data/manual/images/vertical.rb +0 -28
- data/manual/images/width_and_height.rb +0 -25
- data/manual/layout/boxes.rb +0 -27
- data/manual/layout/content.rb +0 -25
- data/manual/layout/layout.rb +0 -28
- data/manual/layout/simple_grid.rb +0 -23
- data/manual/manual/cover.rb +0 -36
- data/manual/manual/foreword.rb +0 -85
- data/manual/manual/how_to_read_this_manual.rb +0 -41
- data/manual/manual/manual.rb +0 -34
- data/manual/outline/add_subsection_to.rb +0 -61
- data/manual/outline/insert_section_after.rb +0 -47
- data/manual/outline/outline.rb +0 -32
- data/manual/outline/sections_and_pages.rb +0 -67
- data/manual/repeatable_content/page_numbering.rb +0 -54
- data/manual/repeatable_content/repeatable_content.rb +0 -31
- data/manual/repeatable_content/repeater.rb +0 -55
- data/manual/repeatable_content/stamp.rb +0 -41
- data/manual/security/encryption.rb +0 -31
- data/manual/security/permissions.rb +0 -38
- data/manual/security/security.rb +0 -28
- 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/text/alignment.rb +0 -44
- data/manual/text/color.rb +0 -24
- data/manual/text/column_box.rb +0 -32
- data/manual/text/fallback_fonts.rb +0 -37
- data/manual/text/font.rb +0 -41
- data/manual/text/font_size.rb +0 -45
- data/manual/text/font_style.rb +0 -23
- data/manual/text/formatted_callbacks.rb +0 -60
- data/manual/text/formatted_text.rb +0 -54
- data/manual/text/free_flowing_text.rb +0 -51
- data/manual/text/group.rb +0 -31
- data/manual/text/inline.rb +0 -43
- data/manual/text/kerning_and_character_spacing.rb +0 -39
- data/manual/text/leading.rb +0 -25
- data/manual/text/line_wrapping.rb +0 -41
- data/manual/text/paragraph_indentation.rb +0 -26
- data/manual/text/positioned_text.rb +0 -38
- data/manual/text/registering_families.rb +0 -48
- data/manual/text/rendering_and_color.rb +0 -37
- data/manual/text/right_to_left_text.rb +0 -43
- data/manual/text/rotation.rb +0 -43
- data/manual/text/single_usage.rb +0 -37
- data/manual/text/text.rb +0 -75
- data/manual/text/text_box_excess.rb +0 -32
- data/manual/text/text_box_extensions.rb +0 -45
- data/manual/text/text_box_overflow.rb +0 -44
- data/manual/text/utf8.rb +0 -28
- data/manual/text/win_ansi_charset.rb +0 -59
- data/spec/acceptance/png.rb +0 -23
- data/spec/column_box_spec.rb +0 -65
- data/spec/extensions/encoding_helpers.rb +0 -9
- data/spec/font_metric_cache_spec.rb +0 -52
- data/spec/image_handler_spec.rb +0 -54
- data/spec/soft_mask_spec.rb +0 -117
- data/spec/table/span_dummy_spec.rb +0 -17
@@ -0,0 +1,215 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# prawn/core/page.rb : Implements low-level representation of a PDF page
|
4
|
+
#
|
5
|
+
# Copyright February 2010, Gregory Brown. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'prawn/document/graphics_state'
|
11
|
+
|
12
|
+
module Prawn
|
13
|
+
module Core
|
14
|
+
class Page #:nodoc:
|
15
|
+
|
16
|
+
include Prawn::Core::Page::GraphicsState
|
17
|
+
|
18
|
+
attr_accessor :document, :content, :dictionary, :margins, :stack
|
19
|
+
|
20
|
+
def initialize(document, options={})
|
21
|
+
@document = document
|
22
|
+
@margins = options[:margins] || { :left => 36,
|
23
|
+
:right => 36,
|
24
|
+
:top => 36,
|
25
|
+
:bottom => 36 }
|
26
|
+
@stack = Prawn::GraphicStateStack.new(options[:graphic_state])
|
27
|
+
if options[:object_id]
|
28
|
+
init_from_object(options)
|
29
|
+
else
|
30
|
+
init_new_page(options)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def layout
|
35
|
+
return @layout if @layout
|
36
|
+
|
37
|
+
mb = dictionary.data[:MediaBox]
|
38
|
+
if mb[3] > mb[2]
|
39
|
+
:portrait
|
40
|
+
else
|
41
|
+
:landscape
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def size
|
46
|
+
@size || dimensions[2,2]
|
47
|
+
end
|
48
|
+
|
49
|
+
def in_stamp_stream?
|
50
|
+
!!@stamp_stream
|
51
|
+
end
|
52
|
+
|
53
|
+
def stamp_stream(dictionary)
|
54
|
+
@stamp_stream = ""
|
55
|
+
@stamp_dictionary = dictionary
|
56
|
+
graphic_stack_size = stack.stack.size
|
57
|
+
|
58
|
+
document.save_graphics_state
|
59
|
+
document.send(:freeze_stamp_graphics)
|
60
|
+
yield if block_given?
|
61
|
+
|
62
|
+
until graphic_stack_size == stack.stack.size
|
63
|
+
document.restore_graphics_state
|
64
|
+
end
|
65
|
+
|
66
|
+
@stamp_dictionary.data[:Length] = @stamp_stream.length + 1
|
67
|
+
@stamp_dictionary << @stamp_stream
|
68
|
+
|
69
|
+
@stamp_stream = nil
|
70
|
+
@stamp_dictionary = nil
|
71
|
+
end
|
72
|
+
|
73
|
+
def content
|
74
|
+
@stamp_stream || document.state.store[@content]
|
75
|
+
end
|
76
|
+
|
77
|
+
# As per the PDF spec, each page can have multiple content streams. This will
|
78
|
+
# add a fresh, empty content stream this the page, mainly for use in loading
|
79
|
+
# template files.
|
80
|
+
#
|
81
|
+
def new_content_stream
|
82
|
+
return if in_stamp_stream?
|
83
|
+
|
84
|
+
unless dictionary.data[:Contents].is_a?(Array)
|
85
|
+
dictionary.data[:Contents] = [content]
|
86
|
+
end
|
87
|
+
@content = document.ref(:Length => 0)
|
88
|
+
dictionary.data[:Contents] << document.state.store[@content]
|
89
|
+
document.open_graphics_state
|
90
|
+
end
|
91
|
+
|
92
|
+
def dictionary
|
93
|
+
@stamp_dictionary || document.state.store[@dictionary]
|
94
|
+
end
|
95
|
+
|
96
|
+
def resources
|
97
|
+
if dictionary.data[:Resources]
|
98
|
+
document.deref(dictionary.data[:Resources])
|
99
|
+
else
|
100
|
+
dictionary.data[:Resources] = {}
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def fonts
|
105
|
+
if resources[:Font]
|
106
|
+
document.deref(resources[:Font])
|
107
|
+
else
|
108
|
+
resources[:Font] = {}
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def xobjects
|
113
|
+
if resources[:XObject]
|
114
|
+
document.deref(resources[:XObject])
|
115
|
+
else
|
116
|
+
resources[:XObject] = {}
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def ext_gstates
|
121
|
+
if resources[:ExtGState]
|
122
|
+
document.deref(resources[:ExtGState])
|
123
|
+
else
|
124
|
+
resources[:ExtGState] = {}
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def finalize
|
129
|
+
if dictionary.data[:Contents].is_a?(Array)
|
130
|
+
dictionary.data[:Contents].each do |stream|
|
131
|
+
stream.compress_stream if document.compression_enabled?
|
132
|
+
stream.data[:Length] = stream.stream.size
|
133
|
+
end
|
134
|
+
else
|
135
|
+
content.compress_stream if document.compression_enabled?
|
136
|
+
content.data[:Length] = content.stream.size
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def imported_page?
|
141
|
+
@imported_page
|
142
|
+
end
|
143
|
+
|
144
|
+
def dimensions
|
145
|
+
return inherited_dictionary_value(:MediaBox) if imported_page?
|
146
|
+
|
147
|
+
coords = Prawn::Document::PageGeometry::SIZES[size] || size
|
148
|
+
[0,0] + case(layout)
|
149
|
+
when :portrait
|
150
|
+
coords
|
151
|
+
when :landscape
|
152
|
+
coords.reverse
|
153
|
+
else
|
154
|
+
raise Prawn::Errors::InvalidPageLayout,
|
155
|
+
"Layout must be either :portrait or :landscape"
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
private
|
160
|
+
|
161
|
+
def init_from_object(options)
|
162
|
+
@dictionary = options[:object_id].to_i
|
163
|
+
dictionary.data[:Parent] = document.state.store.pages
|
164
|
+
|
165
|
+
unless dictionary.data[:Contents].is_a?(Array) # content only on leafs
|
166
|
+
@content = dictionary.data[:Contents].identifier
|
167
|
+
end
|
168
|
+
|
169
|
+
@stamp_stream = nil
|
170
|
+
@stamp_dictionary = nil
|
171
|
+
@imported_page = true
|
172
|
+
end
|
173
|
+
|
174
|
+
def init_new_page(options)
|
175
|
+
@size = options[:size] || "LETTER"
|
176
|
+
@layout = options[:layout] || :portrait
|
177
|
+
|
178
|
+
@content = document.ref(:Length => 0)
|
179
|
+
content << "q" << "\n"
|
180
|
+
@dictionary = document.ref(:Type => :Page,
|
181
|
+
:Parent => document.state.store.pages,
|
182
|
+
:MediaBox => dimensions,
|
183
|
+
:Contents => content)
|
184
|
+
|
185
|
+
resources[:ProcSet] = [:PDF, :Text, :ImageB, :ImageC, :ImageI]
|
186
|
+
|
187
|
+
@stamp_stream = nil
|
188
|
+
@stamp_dictionary = nil
|
189
|
+
end
|
190
|
+
|
191
|
+
# some entries in the Page dict can be inherited from parent Pages dicts.
|
192
|
+
#
|
193
|
+
# Starting with the current page dict, this method will walk up the
|
194
|
+
# inheritance chain return the first value that is found for key
|
195
|
+
#
|
196
|
+
# inherited_dictionary_value(:MediaBox)
|
197
|
+
# => [ 0, 0, 595, 842 ]
|
198
|
+
#
|
199
|
+
def inherited_dictionary_value(key, local_dict = nil)
|
200
|
+
local_dict ||= dictionary.data
|
201
|
+
|
202
|
+
if local_dict.has_key?(key)
|
203
|
+
local_dict[key]
|
204
|
+
elsif local_dict.has_key?(:Parent)
|
205
|
+
inherited_dictionary_value(key, local_dict[:Parent].data)
|
206
|
+
else
|
207
|
+
nil
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# pdf_object.rb : Handles Ruby to PDF object serialization
|
4
|
+
#
|
5
|
+
# Copyright April 2008, Gregory Brown. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
9
|
+
# Top level Module
|
10
|
+
#
|
11
|
+
module Prawn
|
12
|
+
module Core #:nodoc:
|
13
|
+
|
14
|
+
module_function
|
15
|
+
|
16
|
+
if "".respond_to?(:encode)
|
17
|
+
# Ruby 1.9+
|
18
|
+
def utf8_to_utf16(str)
|
19
|
+
"\xFE\xFF".force_encoding("UTF-16BE") + str.encode("UTF-16BE")
|
20
|
+
end
|
21
|
+
else
|
22
|
+
# Ruby 1.8
|
23
|
+
def utf8_to_utf16(str)
|
24
|
+
utf16 = "\xFE\xFF"
|
25
|
+
|
26
|
+
str.unpack("U*").each do |cp|
|
27
|
+
if cp < 0x10000 # Basic Multilingual Plane
|
28
|
+
utf16 << [cp].pack("n")
|
29
|
+
else
|
30
|
+
# pull out high/low 10 bits
|
31
|
+
hi, lo = (cp - 0x10000).divmod(2**10)
|
32
|
+
# encode a surrogate pair
|
33
|
+
utf16 << [0xD800 + hi, 0xDC00 + lo].pack("n*")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
utf16
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Serializes Ruby objects to their PDF equivalents. Most primitive objects
|
42
|
+
# will work as expected, but please note that Name objects are represented
|
43
|
+
# by Ruby Symbol objects and Dictionary objects are represented by Ruby hashes
|
44
|
+
# (keyed by symbols)
|
45
|
+
#
|
46
|
+
# Examples:
|
47
|
+
#
|
48
|
+
# PdfObject(true) #=> "true"
|
49
|
+
# PdfObject(false) #=> "false"
|
50
|
+
# PdfObject(1.2124) #=> "1.2124"
|
51
|
+
# PdfObject("foo bar") #=> "(foo bar)"
|
52
|
+
# PdfObject(:Symbol) #=> "/Symbol"
|
53
|
+
# PdfObject(["foo",:bar, [1,2]]) #=> "[foo /bar [1 2]]"
|
54
|
+
#
|
55
|
+
def PdfObject(obj, in_content_stream = false)
|
56
|
+
case(obj)
|
57
|
+
when NilClass then "null"
|
58
|
+
when TrueClass then "true"
|
59
|
+
when FalseClass then "false"
|
60
|
+
when Numeric then String(obj)
|
61
|
+
when Array
|
62
|
+
"[" << obj.map { |e| PdfObject(e, in_content_stream) }.join(' ') << "]"
|
63
|
+
when Prawn::Core::LiteralString
|
64
|
+
obj = obj.gsub(/[\\\n\r\t\b\f\(\)]/n) { |m| "\\#{m}" }
|
65
|
+
"(#{obj})"
|
66
|
+
when Time
|
67
|
+
obj = obj.strftime("D:%Y%m%d%H%M%S%z").chop.chop + "'00'"
|
68
|
+
obj = obj.gsub(/[\\\n\r\t\b\f\(\)]/n) { |m| "\\#{m}" }
|
69
|
+
"(#{obj})"
|
70
|
+
when Prawn::Core::ByteString
|
71
|
+
"<" << obj.unpack("H*").first << ">"
|
72
|
+
when String
|
73
|
+
obj = utf8_to_utf16(obj) unless in_content_stream
|
74
|
+
"<" << obj.unpack("H*").first << ">"
|
75
|
+
when Symbol
|
76
|
+
"/" + obj.to_s.unpack("C*").map { |n|
|
77
|
+
if n < 33 || n > 126 || [35,40,41,47,60,62].include?(n)
|
78
|
+
"#" + n.to_s(16).upcase
|
79
|
+
else
|
80
|
+
[n].pack("C*")
|
81
|
+
end
|
82
|
+
}.join
|
83
|
+
when Hash
|
84
|
+
output = "<< "
|
85
|
+
obj.each do |k,v|
|
86
|
+
unless String === k || Symbol === k
|
87
|
+
raise Prawn::Errors::FailedObjectConversion,
|
88
|
+
"A PDF Dictionary must be keyed by names"
|
89
|
+
end
|
90
|
+
output << PdfObject(k.to_sym, in_content_stream) << " " <<
|
91
|
+
PdfObject(v, in_content_stream) << "\n"
|
92
|
+
end
|
93
|
+
output << ">>"
|
94
|
+
when Prawn::Core::Reference
|
95
|
+
obj.to_s
|
96
|
+
when Prawn::Core::NameTree::Node
|
97
|
+
PdfObject(obj.to_hash)
|
98
|
+
when Prawn::Core::NameTree::Value
|
99
|
+
PdfObject(obj.name) + " " + PdfObject(obj.value)
|
100
|
+
when Prawn::OutlineRoot, Prawn::OutlineItem
|
101
|
+
PdfObject(obj.to_hash)
|
102
|
+
else
|
103
|
+
raise Prawn::Errors::FailedObjectConversion,
|
104
|
+
"This object cannot be serialized to PDF (#{obj.inspect})"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# reference.rb : Implementation of PDF indirect objects
|
4
|
+
#
|
5
|
+
# Copyright April 2008, Gregory Brown. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
9
|
+
require 'zlib'
|
10
|
+
|
11
|
+
module Prawn
|
12
|
+
module Core
|
13
|
+
|
14
|
+
class Reference #:nodoc:
|
15
|
+
|
16
|
+
attr_accessor :gen, :data, :offset, :stream, :live, :identifier
|
17
|
+
|
18
|
+
def initialize(id, data)
|
19
|
+
@identifier = id
|
20
|
+
@gen = 0
|
21
|
+
@data = data
|
22
|
+
@compressed = false
|
23
|
+
@stream = nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def object
|
27
|
+
output = "#{@identifier} #{gen} obj\n" <<
|
28
|
+
Prawn::Core::PdfObject(data) << "\n"
|
29
|
+
if @stream
|
30
|
+
output << "stream\n" << @stream << "\nendstream\n"
|
31
|
+
end
|
32
|
+
output << "endobj\n"
|
33
|
+
end
|
34
|
+
|
35
|
+
def <<(data)
|
36
|
+
raise 'Cannot add data to a stream that is compressed' if @compressed
|
37
|
+
(@stream ||= "") << data
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_s
|
41
|
+
"#{@identifier} #{gen} R"
|
42
|
+
end
|
43
|
+
|
44
|
+
def compress_stream
|
45
|
+
@stream = Zlib::Deflate.deflate(@stream)
|
46
|
+
@data[:Filter] = :FlateDecode
|
47
|
+
@data[:Length] ||= @stream.length
|
48
|
+
@compressed = true
|
49
|
+
end
|
50
|
+
|
51
|
+
def compressed?
|
52
|
+
@compressed
|
53
|
+
end
|
54
|
+
|
55
|
+
# Creates a deep copy of this ref. If +share+ is provided, shares the
|
56
|
+
# given dictionary entries between the old ref and the new.
|
57
|
+
#
|
58
|
+
def deep_copy(share=[])
|
59
|
+
r = dup
|
60
|
+
|
61
|
+
case r.data
|
62
|
+
when Hash
|
63
|
+
# Copy each entry not in +share+.
|
64
|
+
(r.data.keys - share).each do |k|
|
65
|
+
r.data[k] = Marshal.load(Marshal.dump(r.data[k]))
|
66
|
+
end
|
67
|
+
when Prawn::Core::NameTree::Node
|
68
|
+
r.data = r.data.deep_copy
|
69
|
+
else
|
70
|
+
r.data = Marshal.load(Marshal.dump(r.data))
|
71
|
+
end
|
72
|
+
|
73
|
+
r.stream = Marshal.load(Marshal.dump(r.stream))
|
74
|
+
r
|
75
|
+
end
|
76
|
+
|
77
|
+
# Replaces the data and stream with that of other_ref. Preserves compressed
|
78
|
+
# status.
|
79
|
+
def replace(other_ref)
|
80
|
+
@data = other_ref.data
|
81
|
+
@stream = other_ref.stream
|
82
|
+
@compressed = other_ref.compressed?
|
83
|
+
end
|
84
|
+
|
85
|
+
# Marks this and all referenced objects live, recursively.
|
86
|
+
def mark_live
|
87
|
+
return if @live
|
88
|
+
@live = true
|
89
|
+
referenced_objects.each { |o| o.mark_live }
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
# All objects referenced by this one. Used for GC.
|
95
|
+
def referenced_objects(obj=@data)
|
96
|
+
case obj
|
97
|
+
when self.class
|
98
|
+
[]
|
99
|
+
when Hash
|
100
|
+
obj.values.map{|v| [v] + referenced_objects(v) }
|
101
|
+
when Array
|
102
|
+
obj.map{|v| [v] + referenced_objects(v) }
|
103
|
+
else []
|
104
|
+
end.flatten.grep(self.class)
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
module_function
|
110
|
+
|
111
|
+
def Reference(*args, &block) #:nodoc:
|
112
|
+
Reference.new(*args, &block)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,268 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# prawn/core/text.rb : Implements low level text helpers for Prawn
|
4
|
+
#
|
5
|
+
# Copyright January 2010, Daniel Nelson. 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 Text #:nodoc:
|
12
|
+
|
13
|
+
# These should be used as a base. Extensions may build on this list
|
14
|
+
#
|
15
|
+
VALID_OPTIONS = [:kerning, :size, :style]
|
16
|
+
MODES = { :fill => 0, :stroke => 1, :fill_stroke => 2, :invisible => 3,
|
17
|
+
:fill_clip => 4, :stroke_clip => 5, :fill_stroke_clip => 6,
|
18
|
+
:clip => 7 }
|
19
|
+
|
20
|
+
attr_reader :skip_encoding
|
21
|
+
|
22
|
+
# Low level text placement method. All font and size alterations
|
23
|
+
# should already be set
|
24
|
+
#
|
25
|
+
def draw_text!(text, options)
|
26
|
+
x,y = map_to_absolute(options[:at])
|
27
|
+
add_text_content(text,x,y,options)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Low level call to set the current font style and extract text options from
|
31
|
+
# an options hash. Should be called from within a save_font block
|
32
|
+
#
|
33
|
+
def process_text_options(options)
|
34
|
+
if options[:style]
|
35
|
+
raise "Bad font family" unless font.family
|
36
|
+
font(font.family, :style => options[:style])
|
37
|
+
end
|
38
|
+
|
39
|
+
# must compare against false to keep kerning on as default
|
40
|
+
unless options[:kerning] == false
|
41
|
+
options[:kerning] = font.has_kerning_data?
|
42
|
+
end
|
43
|
+
|
44
|
+
options[:size] ||= font_size
|
45
|
+
end
|
46
|
+
|
47
|
+
# Retrieve the current default kerning setting.
|
48
|
+
#
|
49
|
+
# Defaults to true
|
50
|
+
#
|
51
|
+
def default_kerning?
|
52
|
+
return true if @default_kerning.nil?
|
53
|
+
@default_kerning
|
54
|
+
end
|
55
|
+
|
56
|
+
# Call with a boolean to set the document-wide kerning setting. This can be
|
57
|
+
# overridden using the :kerning text option when drawing text or a text
|
58
|
+
# box.
|
59
|
+
#
|
60
|
+
# pdf.default_kerning = false
|
61
|
+
# pdf.text("hello world") # text is not kerned
|
62
|
+
# pdf.text("hello world", :kerning => true) # text is kerned
|
63
|
+
#
|
64
|
+
def default_kerning(boolean)
|
65
|
+
@default_kerning = boolean
|
66
|
+
end
|
67
|
+
|
68
|
+
alias_method :default_kerning=, :default_kerning
|
69
|
+
|
70
|
+
# Call with no argument to retrieve the current default leading.
|
71
|
+
#
|
72
|
+
# Call with a number to set the document-wide text leading. This can be
|
73
|
+
# overridden using the :leading text option when drawing text or a text
|
74
|
+
# box.
|
75
|
+
#
|
76
|
+
# pdf.default_leading = 7
|
77
|
+
# pdf.text("hello world") # a leading of 7 is used
|
78
|
+
# pdf.text("hello world", :leading => 0) # a leading of 0 is used
|
79
|
+
#
|
80
|
+
# Defaults to 0
|
81
|
+
#
|
82
|
+
def default_leading(number=nil)
|
83
|
+
if number.nil?
|
84
|
+
return 0 if @default_leading.nil?
|
85
|
+
@default_leading
|
86
|
+
else
|
87
|
+
@default_leading = number
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
alias_method :default_leading=, :default_leading
|
92
|
+
|
93
|
+
# Call with no argument to retrieve the current text direction.
|
94
|
+
#
|
95
|
+
# Call with a symbol to set the document-wide text direction. This can be
|
96
|
+
# overridden using the :direction text option when drawing text or a text
|
97
|
+
# box.
|
98
|
+
#
|
99
|
+
# pdf.text_direction = :rtl
|
100
|
+
# pdf.text("hello world") # prints "dlrow olleh"
|
101
|
+
# pdf.text("hello world", :direction => :ltr) # prints "hello world"
|
102
|
+
#
|
103
|
+
# Valid directions are:
|
104
|
+
#
|
105
|
+
# * :ltr - left-to-right (default)
|
106
|
+
# * :rtl - right-to-left
|
107
|
+
#
|
108
|
+
# Side effects:
|
109
|
+
#
|
110
|
+
# * When printing left-to-right, the default text alignment is :left
|
111
|
+
# * When printing right-to-left, the default text alignment is :right
|
112
|
+
#
|
113
|
+
def text_direction(direction=nil)
|
114
|
+
if direction.nil?
|
115
|
+
return :ltr if @text_direction.nil?
|
116
|
+
@text_direction
|
117
|
+
else
|
118
|
+
@text_direction = direction
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
alias_method :text_direction=, :text_direction
|
123
|
+
|
124
|
+
# Call with no argument to retrieve the current fallback fonts.
|
125
|
+
#
|
126
|
+
# Call with an array of font names. Each name must be the name of an AFM
|
127
|
+
# font or the name that was used to register a family of TTF fonts (see
|
128
|
+
# Prawn::Document#font_families). If present, then each glyph will be
|
129
|
+
# rendered using the first font that includes the glyph, starting with the
|
130
|
+
# current font and then moving through :fallback_fonts from left to right.
|
131
|
+
#
|
132
|
+
# Call with an empty array to turn off fallback fonts
|
133
|
+
#
|
134
|
+
# file = "#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf"
|
135
|
+
# font_families["Kai"] = {
|
136
|
+
# :normal => { :file => file, :font => "Kai" }
|
137
|
+
# }
|
138
|
+
# file = "#{Prawn::BASEDIR}/data/fonts/Action Man.dfont"
|
139
|
+
# font_families["Action Man"] = {
|
140
|
+
# :normal => { :file => file, :font => "ActionMan" },
|
141
|
+
# }
|
142
|
+
# fallback_fonts ["Times-Roman", "Kai"]
|
143
|
+
# font "Action Man"
|
144
|
+
# text "hello ƒ 你好"
|
145
|
+
# > hello prints in Action Man
|
146
|
+
# > ƒ prints in Times-Roman
|
147
|
+
# > 你好 prints in Kai
|
148
|
+
#
|
149
|
+
# fallback_fonts [] # clears document-wide fallback fonts
|
150
|
+
#
|
151
|
+
# Side effects:
|
152
|
+
#
|
153
|
+
# * Increased overhead when fallback fonts are declared as each glyph is
|
154
|
+
# checked to see whether it exists in the current font
|
155
|
+
#
|
156
|
+
def fallback_fonts(fallback_fonts=nil)
|
157
|
+
if fallback_fonts.nil?
|
158
|
+
return [] if @fallback_fonts.nil?
|
159
|
+
@fallback_fonts
|
160
|
+
else
|
161
|
+
@fallback_fonts = fallback_fonts
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
alias_method :fallback_fonts=, :fallback_fonts
|
166
|
+
|
167
|
+
# Call with no argument to retrieve the current text rendering mode.
|
168
|
+
#
|
169
|
+
# Call with a symbol and block to temporarily change the current
|
170
|
+
# text rendering mode.
|
171
|
+
#
|
172
|
+
# pdf.text_rendering_mode(:stroke) do
|
173
|
+
# pdf.text("Outlined Text")
|
174
|
+
# end
|
175
|
+
#
|
176
|
+
# Valid modes are:
|
177
|
+
#
|
178
|
+
# * :fill - fill text (default)
|
179
|
+
# * :stroke - stroke text
|
180
|
+
# * :fill_stroke - fill, then stroke text
|
181
|
+
# * :invisible - invisible text
|
182
|
+
# * :fill_clip - fill text then add to path for clipping
|
183
|
+
# * :stroke_clip - stroke text then add to path for clipping
|
184
|
+
# * :fill_stroke_clip - fill then stroke text, then add to path for clipping
|
185
|
+
# * :clip - add text to path for clipping
|
186
|
+
#
|
187
|
+
def text_rendering_mode(mode=nil)
|
188
|
+
return @text_rendering_mode || :fill if mode.nil?
|
189
|
+
unless MODES.keys.include?(mode)
|
190
|
+
raise ArgumentError, "mode must be between one of #{MODES.keys.join(', ')} (#{mode})"
|
191
|
+
end
|
192
|
+
original_mode = text_rendering_mode
|
193
|
+
if original_mode == mode
|
194
|
+
yield
|
195
|
+
else
|
196
|
+
@text_rendering_mode = mode
|
197
|
+
add_content "\n#{MODES[mode]} Tr"
|
198
|
+
yield
|
199
|
+
add_content "\n#{MODES[original_mode]} Tr"
|
200
|
+
@text_rendering_mode = original_mode
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# Increases or decreases the space between characters.
|
205
|
+
# For horizontal text, a positive value will increase the space.
|
206
|
+
# For veritical text, a positive value will decrease the space.
|
207
|
+
#
|
208
|
+
def character_spacing(amount=nil)
|
209
|
+
return @character_spacing || 0 if amount.nil?
|
210
|
+
original_character_spacing = character_spacing
|
211
|
+
if original_character_spacing == amount
|
212
|
+
yield
|
213
|
+
else
|
214
|
+
@character_spacing = amount
|
215
|
+
add_content "\n%.3f Tc" % amount
|
216
|
+
yield
|
217
|
+
add_content "\n%.3f Tc" % original_character_spacing
|
218
|
+
@character_spacing = original_character_spacing
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
# Increases or decreases the space between words.
|
223
|
+
# For horizontal text, a positive value will increase the space.
|
224
|
+
# For veritical text, a positive value will decrease the space.
|
225
|
+
#
|
226
|
+
def word_spacing(amount=nil)
|
227
|
+
return @word_spacing || 0 if amount.nil?
|
228
|
+
original_word_spacing = word_spacing
|
229
|
+
if original_word_spacing == amount
|
230
|
+
yield
|
231
|
+
else
|
232
|
+
@word_spacing = amount
|
233
|
+
add_content "\n%.3f Tw" % amount
|
234
|
+
yield
|
235
|
+
add_content "\n%.3f Tw" % original_word_spacing
|
236
|
+
@word_spacing = original_word_spacing
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
private
|
241
|
+
|
242
|
+
def add_text_content(text, x, y, options)
|
243
|
+
chunks = font.encode_text(text,options)
|
244
|
+
|
245
|
+
add_content "\nBT"
|
246
|
+
|
247
|
+
if options[:rotate]
|
248
|
+
rad = options[:rotate].to_f * Math::PI / 180
|
249
|
+
arr = [ Math.cos(rad), Math.sin(rad), -Math.sin(rad), Math.cos(rad), x, y ]
|
250
|
+
add_content "%.3f %.3f %.3f %.3f %.3f %.3f Tm" % arr
|
251
|
+
else
|
252
|
+
add_content "#{x} #{y} Td"
|
253
|
+
end
|
254
|
+
|
255
|
+
chunks.each do |(subset, string)|
|
256
|
+
font.add_to_current_page(subset)
|
257
|
+
add_content "/#{font.identifier_for(subset)} #{font_size} Tf"
|
258
|
+
|
259
|
+
operation = options[:kerning] && string.is_a?(Array) ? "TJ" : "Tj"
|
260
|
+
add_content Prawn::Core::PdfObject(string, true) << " " << operation
|
261
|
+
end
|
262
|
+
|
263
|
+
add_content "ET\n"
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
end
|
268
|
+
end
|