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,288 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# core/text/formatted/line_wrap.rb : Implements individual line wrapping of
|
4
|
+
# formatted text
|
5
|
+
#
|
6
|
+
# Copyright February 2010, Daniel Nelson. All Rights Reserved.
|
7
|
+
#
|
8
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
9
|
+
#
|
10
|
+
|
11
|
+
module Prawn
|
12
|
+
module Core
|
13
|
+
module Text
|
14
|
+
module Formatted #:nodoc:
|
15
|
+
|
16
|
+
class LineWrap #:nodoc:
|
17
|
+
|
18
|
+
# The width of the last wrapped line
|
19
|
+
#
|
20
|
+
def width
|
21
|
+
@accumulated_width || 0
|
22
|
+
end
|
23
|
+
|
24
|
+
# The number of spaces in the last wrapped line
|
25
|
+
attr_reader :space_count
|
26
|
+
|
27
|
+
# Whether this line is the last line in the paragraph
|
28
|
+
def paragraph_finished?
|
29
|
+
@newline_encountered || is_next_string_newline? || @arranger.finished?
|
30
|
+
end
|
31
|
+
|
32
|
+
# Work in conjunction with the Prawn::Core::Formatted::Arranger
|
33
|
+
# defined in the :arranger option to determine what formatted text
|
34
|
+
# will fit within the width defined by the :width option
|
35
|
+
#
|
36
|
+
def wrap_line(options)
|
37
|
+
initialize_line(options)
|
38
|
+
|
39
|
+
while fragment = @arranger.next_string
|
40
|
+
@fragment_output = ""
|
41
|
+
|
42
|
+
fragment.lstrip! if first_fragment_on_this_line?(fragment)
|
43
|
+
next if empty_line?(fragment)
|
44
|
+
|
45
|
+
unless apply_font_settings_and_add_fragment_to_line(fragment)
|
46
|
+
break
|
47
|
+
end
|
48
|
+
end
|
49
|
+
@arranger.finalize_line
|
50
|
+
@accumulated_width = @arranger.line_width
|
51
|
+
@space_count = @arranger.space_count
|
52
|
+
@arranger.line
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def first_fragment_on_this_line?(fragment)
|
58
|
+
line_empty? && fragment != "\n"
|
59
|
+
end
|
60
|
+
|
61
|
+
def empty_line?(fragment)
|
62
|
+
empty = line_empty? && fragment.empty? && is_next_string_newline?
|
63
|
+
@arranger.update_last_string("", "", soft_hyphen) if empty
|
64
|
+
empty
|
65
|
+
end
|
66
|
+
|
67
|
+
def is_next_string_newline?
|
68
|
+
@arranger.preview_next_string == "\n"
|
69
|
+
end
|
70
|
+
|
71
|
+
def apply_font_settings_and_add_fragment_to_line(fragment)
|
72
|
+
result = nil
|
73
|
+
@arranger.apply_font_settings do
|
74
|
+
# if font has changed from Unicode to non-Unicode, or vice versa, the characters used for soft hyphens
|
75
|
+
# and zero-width spaces will be different
|
76
|
+
set_soft_hyphen_and_zero_width_space
|
77
|
+
result = add_fragment_to_line(fragment)
|
78
|
+
end
|
79
|
+
result
|
80
|
+
end
|
81
|
+
|
82
|
+
# returns true iff all text was printed without running into the end of
|
83
|
+
# the line
|
84
|
+
#
|
85
|
+
def add_fragment_to_line(fragment)
|
86
|
+
if fragment == ""
|
87
|
+
true
|
88
|
+
elsif fragment == "\n"
|
89
|
+
@newline_encountered = true
|
90
|
+
false
|
91
|
+
else
|
92
|
+
fragment.scan(scan_pattern).each do |segment|
|
93
|
+
if segment == zero_width_space
|
94
|
+
segment_width = 0
|
95
|
+
else
|
96
|
+
segment_width = @document.width_of(segment, :kerning => @kerning)
|
97
|
+
end
|
98
|
+
|
99
|
+
if @accumulated_width + segment_width <= @width
|
100
|
+
@accumulated_width += segment_width
|
101
|
+
@fragment_output += segment
|
102
|
+
else
|
103
|
+
end_of_the_line_reached(segment)
|
104
|
+
fragment_finished(fragment)
|
105
|
+
return false
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
fragment_finished(fragment)
|
110
|
+
true
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# The pattern used to determine chunks of text to place on a given line
|
115
|
+
#
|
116
|
+
def scan_pattern
|
117
|
+
pattern = "[^#{break_chars}]+#{soft_hyphen}|" +
|
118
|
+
"[^#{break_chars}]+#{hyphen}+|" +
|
119
|
+
"[^#{break_chars}]+|" +
|
120
|
+
"[#{whitespace}]+|" +
|
121
|
+
"#{hyphen}+[^#{break_chars}]*|" +
|
122
|
+
"#{soft_hyphen}"
|
123
|
+
new_regexp(pattern)
|
124
|
+
end
|
125
|
+
|
126
|
+
# The pattern used to determine whether any word breaks exist on a
|
127
|
+
# current line, which in turn determines whether character level
|
128
|
+
# word breaking is needed
|
129
|
+
#
|
130
|
+
def word_division_scan_pattern
|
131
|
+
new_regexp("\\s|[#{zero_width_space}#{soft_hyphen}#{hyphen}]")
|
132
|
+
end
|
133
|
+
|
134
|
+
def break_chars
|
135
|
+
"#{whitespace}#{soft_hyphen}#{hyphen}"
|
136
|
+
end
|
137
|
+
|
138
|
+
def whitespace
|
139
|
+
" \\t#{zero_width_space}"
|
140
|
+
end
|
141
|
+
|
142
|
+
def hyphen
|
143
|
+
"-"
|
144
|
+
end
|
145
|
+
|
146
|
+
def soft_hyphen
|
147
|
+
@soft_hyphen
|
148
|
+
end
|
149
|
+
|
150
|
+
def zero_width_space
|
151
|
+
@zero_width_space
|
152
|
+
end
|
153
|
+
|
154
|
+
def line_empty?
|
155
|
+
@line_empty && @accumulated_width == 0
|
156
|
+
end
|
157
|
+
|
158
|
+
def initialize_line(options)
|
159
|
+
@document = options[:document]
|
160
|
+
@kerning = options[:kerning]
|
161
|
+
@width = options[:width]
|
162
|
+
|
163
|
+
@accumulated_width = 0
|
164
|
+
@line_empty = true
|
165
|
+
@line_contains_more_than_one_word = false
|
166
|
+
|
167
|
+
@arranger = options[:arranger]
|
168
|
+
@arranger.initialize_line
|
169
|
+
|
170
|
+
@newline_encountered = false
|
171
|
+
@line_full = false
|
172
|
+
end
|
173
|
+
|
174
|
+
def set_soft_hyphen_and_zero_width_space
|
175
|
+
# this is done once per fragment, after the font settings for the fragment are applied --
|
176
|
+
# it could actually be skipped if the font hasn't changed
|
177
|
+
font = @document.font
|
178
|
+
@soft_hyphen = font.normalize_encoding(Prawn::Text::SHY)
|
179
|
+
@zero_width_space = font.unicode? ? Prawn::Text::ZWSP : ""
|
180
|
+
end
|
181
|
+
|
182
|
+
def fragment_finished(fragment)
|
183
|
+
if fragment == "\n"
|
184
|
+
@newline_encountered = true
|
185
|
+
@line_empty = false
|
186
|
+
else
|
187
|
+
update_output_based_on_last_fragment(fragment, soft_hyphen)
|
188
|
+
update_line_status_based_on_last_output
|
189
|
+
determine_whether_to_pull_preceding_fragment_to_join_this_one(fragment)
|
190
|
+
end
|
191
|
+
remember_this_fragment_for_backward_looking_ops
|
192
|
+
end
|
193
|
+
|
194
|
+
def update_output_based_on_last_fragment(fragment, normalized_soft_hyphen=nil)
|
195
|
+
remaining_text = fragment.slice(@fragment_output.length..fragment.length)
|
196
|
+
raise Errors::CannotFit if line_finished? && line_empty? &&
|
197
|
+
@fragment_output.empty? && !fragment.strip.empty?
|
198
|
+
@arranger.update_last_string(@fragment_output, remaining_text, normalized_soft_hyphen)
|
199
|
+
end
|
200
|
+
|
201
|
+
def determine_whether_to_pull_preceding_fragment_to_join_this_one(current_fragment)
|
202
|
+
if @fragment_output.empty? &&
|
203
|
+
!current_fragment.empty? &&
|
204
|
+
@line_contains_more_than_one_word
|
205
|
+
unless previous_fragment_ended_with_breakable? ||
|
206
|
+
fragment_begins_with_breakable?(current_fragment)
|
207
|
+
@fragment_output = @previous_fragment_output_without_last_word
|
208
|
+
update_output_based_on_last_fragment(@previous_fragment)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
def remember_this_fragment_for_backward_looking_ops
|
214
|
+
@previous_fragment = @fragment_output.dup
|
215
|
+
pf = @previous_fragment
|
216
|
+
@previous_fragment_ended_with_breakable = pf =~ /[#{break_chars}]$/
|
217
|
+
last_word = pf.slice(/[^#{break_chars}]*$/)
|
218
|
+
last_word_length = last_word.nil? ? 0 : last_word.length
|
219
|
+
@previous_fragment_output_without_last_word = pf.slice(0, pf.length - last_word_length)
|
220
|
+
end
|
221
|
+
|
222
|
+
def previous_fragment_ended_with_breakable?
|
223
|
+
@previous_fragment_ended_with_breakable
|
224
|
+
end
|
225
|
+
|
226
|
+
def fragment_begins_with_breakable?(fragment)
|
227
|
+
fragment =~ /^[#{break_chars}]/
|
228
|
+
end
|
229
|
+
|
230
|
+
def line_finished?
|
231
|
+
@line_full || paragraph_finished?
|
232
|
+
end
|
233
|
+
|
234
|
+
def update_line_status_based_on_last_output
|
235
|
+
@line_contains_more_than_one_word = true if @fragment_output =~ word_division_scan_pattern
|
236
|
+
end
|
237
|
+
|
238
|
+
def end_of_the_line_reached(segment)
|
239
|
+
update_line_status_based_on_last_output
|
240
|
+
wrap_by_char(segment) unless @line_contains_more_than_one_word
|
241
|
+
@line_full = true
|
242
|
+
end
|
243
|
+
|
244
|
+
def wrap_by_char(segment)
|
245
|
+
# this conditional is only necessary for Ruby 1.8 compatibility
|
246
|
+
# String#unicode_characters is a helper which iterates over UTF-8 characters
|
247
|
+
# under Ruby 1.9, it is implemented simply by aliasing #each_char
|
248
|
+
font = @document.font
|
249
|
+
if font.unicode?
|
250
|
+
segment.unicode_characters do |char|
|
251
|
+
break unless append_char(char,font)
|
252
|
+
end
|
253
|
+
else
|
254
|
+
segment.each_char do |char|
|
255
|
+
break unless append_char(char,font)
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
def append_char(char,font)
|
261
|
+
# kerning doesn't make sense in the context of a single character
|
262
|
+
char_width = font.compute_width_of(char)
|
263
|
+
|
264
|
+
if @accumulated_width + char_width <= @width
|
265
|
+
@accumulated_width += char_width
|
266
|
+
@fragment_output << char
|
267
|
+
true
|
268
|
+
else
|
269
|
+
false
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
def new_regexp(pattern)
|
274
|
+
regexp = ruby_19 {
|
275
|
+
Regexp.new(pattern)
|
276
|
+
}
|
277
|
+
regexp = regexp || ruby_18 {
|
278
|
+
lang = @document.font.unicode? ? 'U' : 'N'
|
279
|
+
Regexp.new(pattern, 0, lang)
|
280
|
+
}
|
281
|
+
regexp
|
282
|
+
end
|
283
|
+
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require "prawn/core/text/formatted/line_wrap"
|
2
|
+
require "prawn/core/text/formatted/arranger"
|
3
|
+
|
4
|
+
module Prawn
|
5
|
+
module Core
|
6
|
+
module Text
|
7
|
+
module Formatted #:nodoc:
|
8
|
+
module Wrap #:nodoc:
|
9
|
+
|
10
|
+
def initialize(array, options)
|
11
|
+
@line_wrap = Prawn::Core::Text::Formatted::LineWrap.new
|
12
|
+
@arranger = Prawn::Core::Text::Formatted::Arranger.new(@document,
|
13
|
+
:kerning => options[:kerning])
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
# See the developer documentation for Prawn::Core::Text#wrap
|
18
|
+
#
|
19
|
+
# Formatted#wrap should set the following variables:
|
20
|
+
# <tt>@line_height</tt>::
|
21
|
+
# the height of the tallest fragment in the last printed line
|
22
|
+
# <tt>@descender</tt>::
|
23
|
+
# the descender height of the tallest fragment in the last
|
24
|
+
# printed line
|
25
|
+
# <tt>@ascender</tt>::
|
26
|
+
# the ascender heigth of the tallest fragment in the last
|
27
|
+
# printed line
|
28
|
+
# <tt>@baseline_y</tt>::
|
29
|
+
# the baseline of the current line
|
30
|
+
# <tt>@nothing_printed</tt>::
|
31
|
+
# set to true until something is printed, then false
|
32
|
+
# <tt>@everything_printed</tt>::
|
33
|
+
# set to false until everything printed, then true
|
34
|
+
#
|
35
|
+
# Returns any formatted text that was not printed
|
36
|
+
#
|
37
|
+
def wrap(array) #:nodoc:
|
38
|
+
initialize_wrap(array)
|
39
|
+
|
40
|
+
stop = false
|
41
|
+
while !stop
|
42
|
+
# wrap before testing if enough height for this line because the
|
43
|
+
# height of the highest fragment on this line will be used to
|
44
|
+
# determine the line height
|
45
|
+
@line_wrap.wrap_line(:document => @document,
|
46
|
+
:kerning => @kerning,
|
47
|
+
:width => available_width,
|
48
|
+
:arranger => @arranger)
|
49
|
+
|
50
|
+
if enough_height_for_this_line?
|
51
|
+
move_baseline_down
|
52
|
+
print_line
|
53
|
+
else
|
54
|
+
stop = true
|
55
|
+
end
|
56
|
+
|
57
|
+
stop ||= @single_line || @arranger.finished?
|
58
|
+
end
|
59
|
+
@text = @printed_lines.join("\n")
|
60
|
+
@everything_printed = @arranger.finished?
|
61
|
+
@arranger.unconsumed
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def print_line
|
67
|
+
@nothing_printed = false
|
68
|
+
printed_fragments = []
|
69
|
+
fragments_this_line = []
|
70
|
+
|
71
|
+
word_spacing = word_spacing_for_this_line
|
72
|
+
while fragment = @arranger.retrieve_fragment
|
73
|
+
fragment.word_spacing = word_spacing
|
74
|
+
if fragment.text == "\n"
|
75
|
+
printed_fragments << "\n" if @printed_lines.last == ""
|
76
|
+
break
|
77
|
+
end
|
78
|
+
printed_fragments << fragment.text
|
79
|
+
fragments_this_line << fragment
|
80
|
+
end
|
81
|
+
|
82
|
+
accumulated_width = 0
|
83
|
+
fragments_this_line.reverse! if @direction == :rtl
|
84
|
+
fragments_this_line.each do |fragment|
|
85
|
+
fragment.default_direction = @direction
|
86
|
+
format_and_draw_fragment(fragment, accumulated_width,
|
87
|
+
@line_wrap.width, word_spacing)
|
88
|
+
accumulated_width += fragment.width
|
89
|
+
end
|
90
|
+
|
91
|
+
if "".respond_to?(:force_encoding)
|
92
|
+
printed_fragments.map! { |s| s.force_encoding("utf-8") }
|
93
|
+
end
|
94
|
+
@printed_lines << printed_fragments.join
|
95
|
+
end
|
96
|
+
|
97
|
+
def word_spacing_for_this_line
|
98
|
+
if @align == :justify &&
|
99
|
+
@line_wrap.space_count > 0 &&
|
100
|
+
!@line_wrap.paragraph_finished?
|
101
|
+
(available_width - @line_wrap.width) / @line_wrap.space_count
|
102
|
+
else
|
103
|
+
0
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def enough_height_for_this_line?
|
108
|
+
@line_height = @arranger.max_line_height
|
109
|
+
@descender = @arranger.max_descender
|
110
|
+
@ascender = @arranger.max_ascender
|
111
|
+
if @baseline_y == 0
|
112
|
+
diff = @ascender + @descender
|
113
|
+
else
|
114
|
+
diff = @descender + @line_height + @leading
|
115
|
+
end
|
116
|
+
required_total_height = @baseline_y.abs + diff
|
117
|
+
if required_total_height > @height + 0.0001
|
118
|
+
# no room for the full height of this line
|
119
|
+
@arranger.repack_unretrieved
|
120
|
+
false
|
121
|
+
else
|
122
|
+
true
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def initialize_wrap(array)
|
127
|
+
@text = nil
|
128
|
+
@arranger.format_array = array
|
129
|
+
|
130
|
+
# these values will depend on the maximum value within a given line
|
131
|
+
@line_height = 0
|
132
|
+
@descender = 0
|
133
|
+
@ascender = 0
|
134
|
+
@baseline_y = 0
|
135
|
+
|
136
|
+
@printed_lines = []
|
137
|
+
@nothing_printed = true
|
138
|
+
@everything_printed = false
|
139
|
+
end
|
140
|
+
|
141
|
+
def format_and_draw_fragment(fragment, accumulated_width,
|
142
|
+
line_width, word_spacing)
|
143
|
+
@arranger.apply_color_and_font_settings(fragment) do
|
144
|
+
draw_fragment(fragment, accumulated_width,
|
145
|
+
line_width, word_spacing)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
@@ -0,0 +1,707 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# document.rb : Implements PDF document generation for Prawn
|
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 "stringio"
|
10
|
+
require "prawn/document/page_geometry"
|
11
|
+
require "prawn/document/bounding_box"
|
12
|
+
require "prawn/document/column_box"
|
13
|
+
require "prawn/document/internals"
|
14
|
+
require "prawn/document/span"
|
15
|
+
require "prawn/document/snapshot"
|
16
|
+
require "prawn/document/graphics_state"
|
17
|
+
|
18
|
+
module Prawn
|
19
|
+
|
20
|
+
# The Prawn::Document class is how you start creating a PDF document.
|
21
|
+
#
|
22
|
+
# There are three basic ways you can instantiate PDF Documents in Prawn, they
|
23
|
+
# are through assignment, implicit block or explicit block. Below is an exmple
|
24
|
+
# of each type, each example does exactly the same thing, makes a PDF document
|
25
|
+
# with all the defaults and puts in the default font "Hello There" and then
|
26
|
+
# saves it to the current directory as "example.pdf"
|
27
|
+
#
|
28
|
+
# For example, assignment can be like this:
|
29
|
+
#
|
30
|
+
# pdf = Prawn::Document.new
|
31
|
+
# pdf.text "Hello There"
|
32
|
+
# pdf.render_file "example.pdf"
|
33
|
+
#
|
34
|
+
# Or you can do an implied block form:
|
35
|
+
#
|
36
|
+
# Prawn::Document.generate "example.pdf" do
|
37
|
+
# text "Hello There"
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
# Or if you need to access a variable outside the scope of the block, the
|
41
|
+
# explicit block form:
|
42
|
+
#
|
43
|
+
# words = "Hello There"
|
44
|
+
# Prawn::Document.generate "example.pdf" do |pdf|
|
45
|
+
# pdf.text words
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# Usually, the block forms are used when you are simply creating a PDF document
|
49
|
+
# that you want to immediately save or render out.
|
50
|
+
#
|
51
|
+
# See the new and generate methods for further details on the above.
|
52
|
+
#
|
53
|
+
class Document
|
54
|
+
include Prawn::Document::Internals
|
55
|
+
include Prawn::Core::Annotations
|
56
|
+
include Prawn::Core::Destinations
|
57
|
+
include Prawn::Document::Snapshot
|
58
|
+
include Prawn::Document::GraphicsState
|
59
|
+
include Prawn::Document::Security
|
60
|
+
include Prawn::Text
|
61
|
+
include Prawn::Graphics
|
62
|
+
include Prawn::Images
|
63
|
+
include Prawn::Stamp
|
64
|
+
include Prawn::SoftMask
|
65
|
+
|
66
|
+
# Any module added to this array will be included into instances of
|
67
|
+
# Prawn::Document at the per-object level. These will also be inherited by
|
68
|
+
# any subclasses.
|
69
|
+
#
|
70
|
+
# Example:
|
71
|
+
#
|
72
|
+
# module MyFancyModule
|
73
|
+
#
|
74
|
+
# def party!
|
75
|
+
# text "It's a big party!"
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# end
|
79
|
+
#
|
80
|
+
# Prawn::Document.extensions << MyFancyModule
|
81
|
+
#
|
82
|
+
# Prawn::Document.generate("foo.pdf") do
|
83
|
+
# party!
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
def self.extensions
|
87
|
+
@extensions ||= []
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.inherited(base) #:nodoc:
|
91
|
+
extensions.each { |e| base.extensions << e }
|
92
|
+
end
|
93
|
+
|
94
|
+
# Creates and renders a PDF document.
|
95
|
+
#
|
96
|
+
# When using the implicit block form, Prawn will evaluate the block
|
97
|
+
# within an instance of Prawn::Document, simplifying your syntax.
|
98
|
+
# However, please note that you will not be able to reference variables
|
99
|
+
# from the enclosing scope within this block.
|
100
|
+
#
|
101
|
+
# # Using implicit block form and rendering to a file
|
102
|
+
# Prawn::Document.generate "example.pdf" do
|
103
|
+
# # self here is set to the newly instantiated Prawn::Document
|
104
|
+
# # and so any variables in the outside scope are unavailable
|
105
|
+
# font "Times-Roman"
|
106
|
+
# draw_text "Hello World", :at => [200,720], :size => 32
|
107
|
+
# end
|
108
|
+
#
|
109
|
+
# If you need to access your local and instance variables, use the explicit
|
110
|
+
# block form shown below. In this case, Prawn yields an instance of
|
111
|
+
# PDF::Document and the block is an ordinary closure:
|
112
|
+
#
|
113
|
+
# # Using explicit block form and rendering to a file
|
114
|
+
# content = "Hello World"
|
115
|
+
# Prawn::Document.generate "example.pdf" do |pdf|
|
116
|
+
# # self here is left alone
|
117
|
+
# pdf.font "Times-Roman"
|
118
|
+
# pdf.draw_text content, :at => [200,720], :size => 32
|
119
|
+
# end
|
120
|
+
#
|
121
|
+
def self.generate(filename,options={},&block)
|
122
|
+
pdf = new(options,&block)
|
123
|
+
pdf.render_file(filename)
|
124
|
+
end
|
125
|
+
|
126
|
+
# Creates a new PDF Document. The following options are available (with
|
127
|
+
# the default values marked in [])
|
128
|
+
#
|
129
|
+
# <tt>:page_size</tt>:: One of the Document::PageGeometry sizes [LETTER]
|
130
|
+
# <tt>:page_layout</tt>:: Either <tt>:portrait</tt> or <tt>:landscape</tt>
|
131
|
+
# <tt>:margin</tt>:: Sets the margin on all sides in points [0.5 inch]
|
132
|
+
# <tt>:left_margin</tt>:: Sets the left margin in points [0.5 inch]
|
133
|
+
# <tt>:right_margin</tt>:: Sets the right margin in points [0.5 inch]
|
134
|
+
# <tt>:top_margin</tt>:: Sets the top margin in points [0.5 inch]
|
135
|
+
# <tt>:bottom_margin</tt>:: Sets the bottom margin in points [0.5 inch]
|
136
|
+
# <tt>:skip_page_creation</tt>:: Creates a document without starting the first page [false]
|
137
|
+
# <tt>:compress</tt>:: Compresses content streams before rendering them [false]
|
138
|
+
# <tt>:optimize_objects</tt>:: Reduce number of PDF objects in output, at expense of render time [false]
|
139
|
+
# <tt>:background</tt>:: An image path to be used as background on all pages [nil]
|
140
|
+
# <tt>:background_scale</tt>:: Backgound image scale [1] [nil]
|
141
|
+
# <tt>:info</tt>:: Generic hash allowing for custom metadata properties [nil]
|
142
|
+
# <tt>:template</tt>:: The path to an existing PDF file to use as a template [nil]
|
143
|
+
#
|
144
|
+
# Setting e.g. the :margin to 100 points and the :left_margin to 50 will result in margins
|
145
|
+
# of 100 points on every side except for the left, where it will be 50.
|
146
|
+
#
|
147
|
+
# The :margin can also be an array much like CSS shorthand:
|
148
|
+
#
|
149
|
+
# # Top and bottom are 20, left and right are 100.
|
150
|
+
# :margin => [20, 100]
|
151
|
+
# # Top is 50, left and right are 100, bottom is 20.
|
152
|
+
# :margin => [50, 100, 20]
|
153
|
+
# # Top is 10, right is 20, bottom is 30, left is 40.
|
154
|
+
# :margin => [10, 20, 30, 40]
|
155
|
+
#
|
156
|
+
# Additionally, :page_size can be specified as a simple two value array giving
|
157
|
+
# the width and height of the document you need in PDF Points.
|
158
|
+
#
|
159
|
+
# Usage:
|
160
|
+
#
|
161
|
+
# # New document, US Letter paper, portrait orientation
|
162
|
+
# pdf = Prawn::Document.new
|
163
|
+
#
|
164
|
+
# # New document, A4 paper, landscaped
|
165
|
+
# pdf = Prawn::Document.new(:page_size => "A4", :page_layout => :landscape)
|
166
|
+
#
|
167
|
+
# # New document, Custom size
|
168
|
+
# pdf = Prawn::Document.new(:page_size => [200, 300])
|
169
|
+
#
|
170
|
+
# # New document, with background
|
171
|
+
# pdf = Prawn::Document.new(:background => "#{Prawn::DATADIR}/images/pigs.jpg")
|
172
|
+
#
|
173
|
+
def initialize(options={},&block)
|
174
|
+
options = options.dup
|
175
|
+
|
176
|
+
Prawn.verify_options [:page_size, :page_layout, :margin, :left_margin,
|
177
|
+
:right_margin, :top_margin, :bottom_margin, :skip_page_creation,
|
178
|
+
:compress, :skip_encoding, :background, :info,
|
179
|
+
:optimize_objects, :template], options
|
180
|
+
|
181
|
+
# need to fix, as the refactoring breaks this
|
182
|
+
# raise NotImplementedError if options[:skip_page_creation]
|
183
|
+
|
184
|
+
self.class.extensions.reverse_each { |e| extend e }
|
185
|
+
@internal_state = Prawn::Core::DocumentState.new(options)
|
186
|
+
@internal_state.populate_pages_from_store(self)
|
187
|
+
min_version(state.store.min_version) if state.store.min_version
|
188
|
+
|
189
|
+
@background = options[:background]
|
190
|
+
@background_scale = options[:background_scale] || 1
|
191
|
+
@font_size = 12
|
192
|
+
|
193
|
+
@bounding_box = nil
|
194
|
+
@margin_box = nil
|
195
|
+
|
196
|
+
@page_number = 0
|
197
|
+
|
198
|
+
options[:size] = options.delete(:page_size)
|
199
|
+
options[:layout] = options.delete(:page_layout)
|
200
|
+
|
201
|
+
if options[:template]
|
202
|
+
fresh_content_streams(options)
|
203
|
+
go_to_page(1)
|
204
|
+
else
|
205
|
+
if options[:skip_page_creation] || options[:template]
|
206
|
+
start_new_page(options.merge(:orphan => true))
|
207
|
+
else
|
208
|
+
start_new_page(options)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
@bounding_box = @margin_box
|
213
|
+
|
214
|
+
if block
|
215
|
+
block.arity < 1 ? instance_eval(&block) : block[self]
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
attr_accessor :margin_box
|
220
|
+
attr_reader :margins, :y
|
221
|
+
attr_writer :font_size
|
222
|
+
attr_accessor :page_number
|
223
|
+
|
224
|
+
def state
|
225
|
+
@internal_state
|
226
|
+
end
|
227
|
+
|
228
|
+
def page
|
229
|
+
state.page
|
230
|
+
end
|
231
|
+
|
232
|
+
# Creates and advances to a new page in the document.
|
233
|
+
#
|
234
|
+
# Page size, margins, and layout can also be set when generating a
|
235
|
+
# new page. These values will become the new defaults for page creation
|
236
|
+
#
|
237
|
+
# pdf.start_new_page #=> Starts new page keeping current values
|
238
|
+
# pdf.start_new_page(:size => "LEGAL", :layout => :landscape)
|
239
|
+
# pdf.start_new_page(:left_margin => 50, :right_margin => 50)
|
240
|
+
# pdf.start_new_page(:margin => 100)
|
241
|
+
#
|
242
|
+
# A template for a page can be specified by pointing to the path of and existing pdf.
|
243
|
+
# One can also specify which page of the template which defaults otherwise to 1.
|
244
|
+
#
|
245
|
+
# pdf.start_new_page(:template => multipage_template.pdf, :template_page => 2)
|
246
|
+
#
|
247
|
+
# Note: templates get indexed by either the object_id of the filename or stream
|
248
|
+
# entered so that if you reuse the same template multiple times be sure to use the
|
249
|
+
# same instance for more efficient use of resources and smaller rendered pdfs.
|
250
|
+
def start_new_page(options = {})
|
251
|
+
if last_page = state.page
|
252
|
+
last_page_size = last_page.size
|
253
|
+
last_page_layout = last_page.layout
|
254
|
+
last_page_margins = last_page.margins
|
255
|
+
end
|
256
|
+
|
257
|
+
page_options = {:size => options[:size] || last_page_size,
|
258
|
+
:layout => options[:layout] || last_page_layout,
|
259
|
+
:margins => last_page_margins}
|
260
|
+
if last_page
|
261
|
+
new_graphic_state = last_page.graphic_state.dup
|
262
|
+
#erase the color space so that it gets reset on new page for fussy pdf-readers
|
263
|
+
new_graphic_state.color_space = {}
|
264
|
+
page_options.merge!(:graphic_state => new_graphic_state)
|
265
|
+
end
|
266
|
+
merge_template_options(page_options, options) if options[:template]
|
267
|
+
|
268
|
+
state.page = Prawn::Core::Page.new(self, page_options)
|
269
|
+
|
270
|
+
apply_margin_options(options)
|
271
|
+
generate_margin_box
|
272
|
+
|
273
|
+
# Reset the bounding box if the new page has different size or layout
|
274
|
+
if last_page && (last_page.size != state.page.size ||
|
275
|
+
last_page.layout != state.page.layout)
|
276
|
+
@bounding_box = @margin_box
|
277
|
+
end
|
278
|
+
|
279
|
+
state.page.new_content_stream if options[:template]
|
280
|
+
use_graphic_settings(options[:template])
|
281
|
+
|
282
|
+
unless options[:orphan]
|
283
|
+
state.insert_page(state.page, @page_number)
|
284
|
+
@page_number += 1
|
285
|
+
|
286
|
+
canvas { image(@background, :scale => @background_scale, :at => bounds.top_left) } if @background
|
287
|
+
@y = @bounding_box.absolute_top
|
288
|
+
|
289
|
+
float do
|
290
|
+
state.on_page_create_action(self)
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
# Returns the number of pages in the document
|
296
|
+
#
|
297
|
+
# pdf = Prawn::Document.new
|
298
|
+
# pdf.page_count #=> 1
|
299
|
+
# 3.times { pdf.start_new_page }
|
300
|
+
# pdf.page_count #=> 4
|
301
|
+
#
|
302
|
+
def page_count
|
303
|
+
state.page_count
|
304
|
+
end
|
305
|
+
|
306
|
+
# Re-opens the page with the given (1-based) page number so that you can
|
307
|
+
# draw on it.
|
308
|
+
#
|
309
|
+
# See Prawn::Document#number_pages for a sample usage of this capability.
|
310
|
+
#
|
311
|
+
def go_to_page(k)
|
312
|
+
@page_number = k
|
313
|
+
state.page = state.pages[k-1]
|
314
|
+
generate_margin_box
|
315
|
+
@y = @bounding_box.absolute_top
|
316
|
+
end
|
317
|
+
|
318
|
+
def y=(new_y)
|
319
|
+
@y = new_y
|
320
|
+
bounds.update_height
|
321
|
+
end
|
322
|
+
|
323
|
+
# The current y drawing position relative to the innermost bounding box,
|
324
|
+
# or to the page margins at the top level.
|
325
|
+
#
|
326
|
+
def cursor
|
327
|
+
y - bounds.absolute_bottom
|
328
|
+
end
|
329
|
+
|
330
|
+
# Moves to the specified y position in relative terms to the bottom margin.
|
331
|
+
#
|
332
|
+
def move_cursor_to(new_y)
|
333
|
+
self.y = new_y + bounds.absolute_bottom
|
334
|
+
end
|
335
|
+
|
336
|
+
# Executes a block and then restores the original y position. If new pages
|
337
|
+
# were created during this block, it will teleport back to the original
|
338
|
+
# page when done.
|
339
|
+
#
|
340
|
+
# pdf.text "A"
|
341
|
+
#
|
342
|
+
# pdf.float do
|
343
|
+
# pdf.move_down 100
|
344
|
+
# pdf.text "C"
|
345
|
+
# end
|
346
|
+
#
|
347
|
+
# pdf.text "B"
|
348
|
+
#
|
349
|
+
def float
|
350
|
+
original_page = page_number
|
351
|
+
original_y = y
|
352
|
+
yield
|
353
|
+
go_to_page(original_page) unless page_number == original_page
|
354
|
+
self.y = original_y
|
355
|
+
end
|
356
|
+
|
357
|
+
# Renders the PDF document to string
|
358
|
+
#
|
359
|
+
def render
|
360
|
+
output = StringIO.new
|
361
|
+
finalize_all_page_contents
|
362
|
+
|
363
|
+
render_header(output)
|
364
|
+
render_body(output)
|
365
|
+
render_xref(output)
|
366
|
+
render_trailer(output)
|
367
|
+
str = output.string
|
368
|
+
str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
|
369
|
+
str
|
370
|
+
end
|
371
|
+
|
372
|
+
# Renders the PDF document to file.
|
373
|
+
#
|
374
|
+
# pdf.render_file "foo.pdf"
|
375
|
+
#
|
376
|
+
def render_file(filename)
|
377
|
+
Kernel.const_defined?("Encoding") ? mode = "wb:ASCII-8BIT" : mode = "wb"
|
378
|
+
File.open(filename,mode) { |f| f << render }
|
379
|
+
end
|
380
|
+
|
381
|
+
# The bounds method returns the current bounding box you are currently in,
|
382
|
+
# which is by default the box represented by the margin box on the
|
383
|
+
# document itself. When called from within a created <tt>bounding_box</tt>
|
384
|
+
# block, the box defined by that call will be returned instead of the
|
385
|
+
# document margin box.
|
386
|
+
#
|
387
|
+
# Another important point about bounding boxes is that all x and y measurements
|
388
|
+
# within a bounding box code block are relative to the bottom left corner of the
|
389
|
+
# bounding box.
|
390
|
+
#
|
391
|
+
# For example:
|
392
|
+
#
|
393
|
+
# Prawn::Document.new do
|
394
|
+
# # In the default "margin box" of a Prawn document of 0.5in along each edge
|
395
|
+
#
|
396
|
+
# # Draw a border around the page (the manual way)
|
397
|
+
# stroke do
|
398
|
+
# line(bounds.bottom_left, bounds.bottom_right)
|
399
|
+
# line(bounds.bottom_right, bounds.top_right)
|
400
|
+
# line(bounds.top_right, bounds.top_left)
|
401
|
+
# line(bounds.top_left, bounds.bottom_left)
|
402
|
+
# end
|
403
|
+
#
|
404
|
+
# # Draw a border around the page (the easy way)
|
405
|
+
# stroke_bounds
|
406
|
+
# end
|
407
|
+
#
|
408
|
+
def bounds
|
409
|
+
@bounding_box
|
410
|
+
end
|
411
|
+
|
412
|
+
# Returns the innermost non-stretchy bounding box.
|
413
|
+
#
|
414
|
+
def reference_bounds
|
415
|
+
@bounding_box.reference_bounds
|
416
|
+
end
|
417
|
+
|
418
|
+
# Sets Document#bounds to the BoundingBox provided. See above for a brief
|
419
|
+
# description of what a bounding box is. This function is useful if you
|
420
|
+
# really need to change the bounding box manually, but usually, just entering
|
421
|
+
# and exiting bounding box code blocks is good enough.
|
422
|
+
#
|
423
|
+
def bounds=(bounding_box)
|
424
|
+
@bounding_box = bounding_box
|
425
|
+
end
|
426
|
+
|
427
|
+
# Moves up the document by n points relative to the current position inside
|
428
|
+
# the current bounding box.
|
429
|
+
#
|
430
|
+
def move_up(n)
|
431
|
+
self.y += n
|
432
|
+
end
|
433
|
+
|
434
|
+
# Moves down the document by n points relative to the current position inside
|
435
|
+
# the current bounding box.
|
436
|
+
#
|
437
|
+
def move_down(n)
|
438
|
+
self.y -= n
|
439
|
+
end
|
440
|
+
|
441
|
+
# Moves down the document and then executes a block.
|
442
|
+
#
|
443
|
+
# pdf.text "some text"
|
444
|
+
# pdf.pad_top(100) do
|
445
|
+
# pdf.text "This is 100 points below the previous line of text"
|
446
|
+
# end
|
447
|
+
# pdf.text "This text appears right below the previous line of text"
|
448
|
+
#
|
449
|
+
def pad_top(y)
|
450
|
+
move_down(y)
|
451
|
+
yield
|
452
|
+
end
|
453
|
+
|
454
|
+
# Executes a block then moves down the document
|
455
|
+
#
|
456
|
+
# pdf.text "some text"
|
457
|
+
# pdf.pad_bottom(100) do
|
458
|
+
# pdf.text "This text appears right below the previous line of text"
|
459
|
+
# end
|
460
|
+
# pdf.text "This is 100 points below the previous line of text"
|
461
|
+
#
|
462
|
+
def pad_bottom(y)
|
463
|
+
yield
|
464
|
+
move_down(y)
|
465
|
+
end
|
466
|
+
|
467
|
+
# Moves down the document by y, executes a block, then moves down the
|
468
|
+
# document by y again.
|
469
|
+
#
|
470
|
+
# pdf.text "some text"
|
471
|
+
# pdf.pad(100) do
|
472
|
+
# pdf.text "This is 100 points below the previous line of text"
|
473
|
+
# end
|
474
|
+
# pdf.text "This is 100 points below the previous line of text"
|
475
|
+
#
|
476
|
+
def pad(y)
|
477
|
+
move_down(y)
|
478
|
+
yield
|
479
|
+
move_down(y)
|
480
|
+
end
|
481
|
+
|
482
|
+
|
483
|
+
# Indents the specified number of PDF points for the duration of the block
|
484
|
+
#
|
485
|
+
# pdf.text "some text"
|
486
|
+
# pdf.indent(20) do
|
487
|
+
# pdf.text "This is indented 20 points"
|
488
|
+
# end
|
489
|
+
# pdf.text "This starts 20 points left of the above line " +
|
490
|
+
# "and is flush with the first line"
|
491
|
+
# pdf.indent 20, 20 do
|
492
|
+
# pdf.text "This line is indented on both sides."
|
493
|
+
# end
|
494
|
+
#
|
495
|
+
def indent(left, right = 0, &block)
|
496
|
+
bounds.indent(left, right, &block)
|
497
|
+
end
|
498
|
+
|
499
|
+
|
500
|
+
def mask(*fields) # :nodoc:
|
501
|
+
# Stores the current state of the named attributes, executes the block, and
|
502
|
+
# then restores the original values after the block has executed.
|
503
|
+
# -- I will remove the nodoc if/when this feature is a little less hacky
|
504
|
+
stored = {}
|
505
|
+
fields.each { |f| stored[f] = send(f) }
|
506
|
+
yield
|
507
|
+
fields.each { |f| send("#{f}=", stored[f]) }
|
508
|
+
end
|
509
|
+
|
510
|
+
# Attempts to group the given block vertically within the current context.
|
511
|
+
# First attempts to render it in the current position on the current page.
|
512
|
+
# If that attempt overflows, it is tried anew after starting a new context
|
513
|
+
# (page or column). Returns a logically true value if the content fits in
|
514
|
+
# one page/column, false if a new page or column was needed.
|
515
|
+
#
|
516
|
+
# Raises CannotGroup if the provided content is too large to fit alone in
|
517
|
+
# the current page or column.
|
518
|
+
#
|
519
|
+
def group(second_attempt=false)
|
520
|
+
old_bounding_box = @bounding_box
|
521
|
+
@bounding_box = SimpleDelegator.new(@bounding_box)
|
522
|
+
|
523
|
+
def @bounding_box.move_past_bottom
|
524
|
+
raise RollbackTransaction
|
525
|
+
end
|
526
|
+
|
527
|
+
success = transaction { yield }
|
528
|
+
|
529
|
+
@bounding_box = old_bounding_box
|
530
|
+
|
531
|
+
unless success
|
532
|
+
raise Prawn::Errors::CannotGroup if second_attempt
|
533
|
+
old_bounding_box.move_past_bottom
|
534
|
+
group(second_attempt=true) { yield }
|
535
|
+
end
|
536
|
+
|
537
|
+
success
|
538
|
+
end
|
539
|
+
|
540
|
+
# Places a text box on specified pages for page numbering. This should be called
|
541
|
+
# towards the end of document creation, after all your content is already in
|
542
|
+
# place. In your template string, <page> refers to the current page, and
|
543
|
+
# <total> refers to the total amount of pages in the document. Page numbering should
|
544
|
+
# occur at the end of your Prawn::Document.generate block because the method iterates
|
545
|
+
# through existing pages after they are created.
|
546
|
+
#
|
547
|
+
# Parameters are:
|
548
|
+
#
|
549
|
+
# <tt>string</tt>:: Template string for page number wording.
|
550
|
+
# Should include '<page>' and, optionally, '<total>'.
|
551
|
+
# <tt>options</tt>:: A hash for page numbering and text box options.
|
552
|
+
# <tt>:page_filter</tt>:: A filter to specify which pages to place page numbers on.
|
553
|
+
# Refer to the method 'page_match?'
|
554
|
+
# <tt>:start_count_at</tt>:: The starting count to increment pages from.
|
555
|
+
# <tt>:total_pages</tt>:: If provided, will replace <total> with the value given.
|
556
|
+
# Useful to override the total number of pages when using
|
557
|
+
# the start_count_at option.
|
558
|
+
# <tt>:color</tt>:: Text fill color.
|
559
|
+
#
|
560
|
+
# Please refer to Prawn::Text::text_box for additional options concerning text
|
561
|
+
# formatting and placement.
|
562
|
+
#
|
563
|
+
# Example: Print page numbers on every page except for the first. Start counting from
|
564
|
+
# five.
|
565
|
+
#
|
566
|
+
# Prawn::Document.generate("page_with_numbering.pdf") do
|
567
|
+
# number_pages "<page> in a total of <total>",
|
568
|
+
# {:start_count_at => 5,
|
569
|
+
# :page_filter => lambda{ |pg| pg != 1 },
|
570
|
+
# :at => [bounds.right - 50, 0],
|
571
|
+
# :align => :right,
|
572
|
+
# :size => 14}
|
573
|
+
# end
|
574
|
+
#
|
575
|
+
def number_pages(string, options={})
|
576
|
+
opts = options.dup
|
577
|
+
start_count_at = opts.delete(:start_count_at).to_i
|
578
|
+
page_filter = if opts.has_key?(:page_filter)
|
579
|
+
opts.delete(:page_filter)
|
580
|
+
else
|
581
|
+
:all
|
582
|
+
end
|
583
|
+
total_pages = opts.delete(:total_pages)
|
584
|
+
txtcolor = opts.delete(:color)
|
585
|
+
# An explicit height so that we can draw page numbers in the margins
|
586
|
+
opts[:height] = 50 unless opts.has_key?(:height)
|
587
|
+
|
588
|
+
start_count = false
|
589
|
+
pseudopage = 0
|
590
|
+
(1..page_count).each do |p|
|
591
|
+
unless start_count
|
592
|
+
pseudopage = case start_count_at
|
593
|
+
when 0
|
594
|
+
1
|
595
|
+
else
|
596
|
+
start_count_at.to_i
|
597
|
+
end
|
598
|
+
end
|
599
|
+
if page_match?(page_filter, p)
|
600
|
+
go_to_page(p)
|
601
|
+
# have to use fill_color here otherwise text reverts back to default fill color
|
602
|
+
fill_color txtcolor unless txtcolor.nil?
|
603
|
+
total_pages = total_pages.nil? ? page_count : total_pages
|
604
|
+
str = string.gsub("<page>","#{pseudopage}").gsub("<total>","#{total_pages}")
|
605
|
+
text_box str, opts
|
606
|
+
start_count = true # increment page count as soon as first match found
|
607
|
+
end
|
608
|
+
pseudopage += 1 if start_count
|
609
|
+
end
|
610
|
+
end
|
611
|
+
|
612
|
+
# Provides a way to execute a block of code repeatedly based on a
|
613
|
+
# page_filter.
|
614
|
+
#
|
615
|
+
# Available page filters are:
|
616
|
+
# :all repeats on every page
|
617
|
+
# :odd repeats on odd pages
|
618
|
+
# :even repeats on even pages
|
619
|
+
# some_array repeats on every page listed in the array
|
620
|
+
# some_range repeats on every page included in the range
|
621
|
+
# some_lambda yields page number and repeats for true return values
|
622
|
+
def page_match?(page_filter, page_number)
|
623
|
+
case page_filter
|
624
|
+
when :all
|
625
|
+
true
|
626
|
+
when :odd
|
627
|
+
page_number % 2 == 1
|
628
|
+
when :even
|
629
|
+
page_number % 2 == 0
|
630
|
+
when Range, Array
|
631
|
+
page_filter.include?(page_number)
|
632
|
+
when Proc
|
633
|
+
page_filter.call(page_number)
|
634
|
+
end
|
635
|
+
end
|
636
|
+
|
637
|
+
|
638
|
+
# Returns true if content streams will be compressed before rendering,
|
639
|
+
# false otherwise
|
640
|
+
#
|
641
|
+
def compression_enabled?
|
642
|
+
!!state.compress
|
643
|
+
end
|
644
|
+
|
645
|
+
private
|
646
|
+
|
647
|
+
def merge_template_options(page_options, options)
|
648
|
+
object_id = state.store.import_page(options[:template], options[:template_page] || 1)
|
649
|
+
page_options.merge!(:object_id => object_id, :page_template => true)
|
650
|
+
end
|
651
|
+
|
652
|
+
# setting override_settings to true ensures that a new graphic state does not end up using
|
653
|
+
# previous settings especially from imported template streams
|
654
|
+
def use_graphic_settings(override_settings = false)
|
655
|
+
set_fill_color if current_fill_color != "000000" || override_settings
|
656
|
+
set_stroke_color if current_stroke_color != "000000" || override_settings
|
657
|
+
write_line_width if line_width != 1 || override_settings
|
658
|
+
write_stroke_cap_style if cap_style != :butt || override_settings
|
659
|
+
write_stroke_join_style if join_style != :miter || override_settings
|
660
|
+
write_stroke_dash if dashed? || override_settings
|
661
|
+
end
|
662
|
+
|
663
|
+
def generate_margin_box
|
664
|
+
old_margin_box = @margin_box
|
665
|
+
page = state.page
|
666
|
+
|
667
|
+
@margin_box = BoundingBox.new(
|
668
|
+
self,
|
669
|
+
nil, # margin box has no parent
|
670
|
+
[ page.margins[:left], page.dimensions[-1] - page.margins[:top] ] ,
|
671
|
+
:width => page.dimensions[-2] - (page.margins[:left] + page.margins[:right]),
|
672
|
+
:height => page.dimensions[-1] - (page.margins[:top] + page.margins[:bottom])
|
673
|
+
)
|
674
|
+
|
675
|
+
# This check maintains indentation settings across page breaks
|
676
|
+
if old_margin_box
|
677
|
+
@margin_box.add_left_padding(old_margin_box.total_left_padding)
|
678
|
+
@margin_box.add_right_padding(old_margin_box.total_right_padding)
|
679
|
+
end
|
680
|
+
|
681
|
+
# we must update bounding box if not flowing from the previous page
|
682
|
+
#
|
683
|
+
# FIXME: This may have a bug where the old margin is restored
|
684
|
+
# when the bounding box exits.
|
685
|
+
@bounding_box = @margin_box if old_margin_box == @bounding_box
|
686
|
+
end
|
687
|
+
|
688
|
+
def apply_margin_options(options)
|
689
|
+
if options[:margin]
|
690
|
+
# Treat :margin as CSS shorthand with 1-4 values.
|
691
|
+
margin = Array(options[:margin])
|
692
|
+
positions = { 4 => [0,1,2,3], 3 => [0,1,2,1],
|
693
|
+
2 => [0,1,0,1], 1 => [0,0,0,0] }[margin.length]
|
694
|
+
|
695
|
+
[:top, :right, :bottom, :left].zip(positions).each do |p,i|
|
696
|
+
options[:"#{p}_margin"] ||= margin[i]
|
697
|
+
end
|
698
|
+
end
|
699
|
+
|
700
|
+
[:left,:right,:top,:bottom].each do |side|
|
701
|
+
if margin = options[:"#{side}_margin"]
|
702
|
+
state.page.margins[side] = margin
|
703
|
+
end
|
704
|
+
end
|
705
|
+
end
|
706
|
+
end
|
707
|
+
end
|