prawn 0.12.0 → 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/COPYING +2 -2
- data/Gemfile +18 -0
- data/LICENSE +1 -1
- data/README.md +17 -4
- data/Rakefile +18 -22
- data/data/images/indexed_color.dat +0 -0
- data/data/images/indexed_color.png +0 -0
- data/data/pdfs/nested_pages.pdf +13 -13
- data/lib/pdf/core.rb +35 -0
- data/lib/{prawn → pdf}/core/annotations.rb +6 -7
- data/lib/{prawn → pdf}/core/byte_string.rb +1 -1
- data/lib/{prawn → pdf}/core/destinations.rb +23 -23
- data/lib/{prawn → pdf}/core/document_state.rb +8 -8
- data/lib/pdf/core/filter_list.rb +51 -0
- data/lib/pdf/core/filters.rb +36 -0
- data/lib/pdf/core/graphics_state.rb +68 -0
- data/lib/{prawn → pdf}/core/literal_string.rb +1 -1
- data/lib/{prawn → pdf}/core/name_tree.rb +14 -2
- data/lib/{prawn → pdf}/core/object_store.rb +80 -24
- data/lib/pdf/core/outline.rb +315 -0
- data/lib/{prawn → pdf}/core/page.rb +23 -26
- data/lib/{prawn/document → pdf/core}/page_geometry.rb +11 -21
- data/lib/{prawn → pdf}/core/pdf_object.rb +48 -32
- data/lib/{prawn → pdf}/core/reference.rb +35 -44
- data/lib/pdf/core/stream.rb +98 -0
- data/lib/{prawn → pdf}/core/text.rb +24 -17
- data/lib/prawn.rb +95 -17
- data/lib/prawn/compatibility.rb +66 -26
- data/lib/prawn/document.rb +48 -30
- data/lib/prawn/document/bounding_box.rb +3 -3
- data/lib/prawn/document/column_box.rb +46 -8
- data/lib/prawn/document/graphics_state.rb +10 -73
- data/lib/prawn/document/internals.rb +24 -23
- data/lib/prawn/document/snapshot.rb +6 -7
- data/lib/prawn/document/span.rb +10 -10
- data/lib/prawn/encoding.rb +7 -7
- data/lib/prawn/errors.rb +18 -29
- data/lib/prawn/font.rb +64 -28
- data/lib/prawn/font/afm.rb +32 -74
- data/lib/prawn/font/dfont.rb +2 -2
- data/lib/prawn/font/ttf.rb +28 -57
- data/lib/prawn/font_metric_cache.rb +45 -0
- data/lib/prawn/graphics.rb +307 -41
- data/lib/prawn/graphics/cap_style.rb +3 -3
- data/lib/prawn/graphics/color.rb +12 -5
- data/lib/prawn/graphics/dash.rb +52 -31
- data/lib/prawn/graphics/join_style.rb +7 -7
- data/lib/prawn/graphics/patterns.rb +137 -0
- data/lib/prawn/graphics/transformation.rb +9 -9
- data/lib/prawn/graphics/transparency.rb +1 -1
- data/lib/prawn/image_handler.rb +30 -0
- data/lib/prawn/images.rb +86 -105
- data/lib/prawn/images/image.rb +48 -0
- data/lib/prawn/images/jpg.rb +14 -10
- data/lib/prawn/images/png.rb +50 -37
- data/lib/prawn/layout.rb +2 -2
- data/lib/prawn/layout/grid.rb +51 -51
- data/lib/prawn/measurement_extensions.rb +5 -5
- data/lib/prawn/measurements.rb +25 -21
- data/lib/prawn/outline.rb +4 -308
- data/lib/prawn/repeater.rb +8 -8
- data/lib/prawn/security.rb +50 -36
- data/lib/prawn/soft_mask.rb +94 -0
- data/lib/prawn/stamp.rb +3 -3
- data/lib/prawn/table.rb +292 -118
- data/lib/prawn/table/cell.rb +272 -45
- data/lib/prawn/table/cell/image.rb +70 -0
- data/lib/prawn/table/cell/in_table.rb +2 -2
- data/lib/prawn/table/cell/span_dummy.rb +92 -0
- data/lib/prawn/table/cell/subtable.rb +2 -2
- data/lib/prawn/table/cell/text.rb +42 -24
- data/lib/prawn/table/cells.rb +137 -48
- data/lib/prawn/text.rb +35 -23
- data/lib/prawn/text/box.rb +18 -5
- data/lib/prawn/text/formatted.rb +5 -4
- data/lib/prawn/text/formatted/arranger.rb +292 -0
- data/lib/prawn/text/formatted/box.rb +52 -13
- data/lib/prawn/text/formatted/fragment.rb +37 -22
- data/lib/prawn/text/formatted/line_wrap.rb +286 -0
- data/lib/prawn/text/formatted/parser.rb +14 -6
- data/lib/prawn/text/formatted/wrap.rb +151 -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 +411 -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 +24 -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 +48 -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 +35 -0
- data/manual/manual/foreword.rb +85 -0
- data/manual/manual/how_to_read_this_manual.rb +41 -0
- data/manual/manual/manual.rb +35 -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 +25 -0
- data/manual/templates/page_template.rb +48 -0
- data/manual/templates/templates.rb +27 -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 +54 -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/{examples/m17n → manual/text}/win_ansi_charset.rb +14 -10
- data/prawn.gemspec +18 -12
- data/spec/acceptance/png.rb +23 -0
- data/spec/annotations_spec.rb +16 -32
- data/spec/bounding_box_spec.rb +128 -15
- data/spec/cell_spec.rb +169 -38
- data/spec/column_box_spec.rb +33 -0
- data/spec/destinations_spec.rb +5 -5
- data/spec/document_spec.rb +150 -104
- data/spec/extensions/encoding_helpers.rb +10 -0
- data/spec/extensions/mocha.rb +1 -0
- data/spec/filters_spec.rb +34 -0
- data/spec/font_metric_cache_spec.rb +52 -0
- data/spec/font_spec.rb +183 -97
- data/spec/formatted_text_arranger_spec.rb +43 -43
- data/spec/formatted_text_box_spec.rb +30 -20
- data/spec/formatted_text_fragment_spec.rb +8 -8
- data/spec/graphics_spec.rb +158 -69
- data/spec/grid_spec.rb +15 -15
- data/spec/image_handler_spec.rb +42 -0
- data/spec/images_spec.rb +49 -24
- data/spec/inline_formatted_text_parser_spec.rb +73 -19
- data/spec/jpg_spec.rb +4 -4
- data/spec/line_wrap_spec.rb +26 -26
- data/spec/measurement_units_spec.rb +6 -6
- data/spec/name_tree_spec.rb +21 -21
- data/spec/object_store_spec.rb +39 -39
- data/spec/outline_spec.rb +93 -53
- data/spec/pdf_object_spec.rb +88 -86
- data/spec/png_spec.rb +31 -28
- data/spec/reference_spec.rb +32 -32
- data/spec/repeater_spec.rb +25 -11
- data/spec/security_spec.rb +44 -12
- data/spec/snapshot_spec.rb +8 -9
- data/spec/soft_mask_spec.rb +117 -0
- data/spec/span_spec.rb +10 -15
- data/spec/spec_helper.rb +25 -8
- data/spec/stamp_spec.rb +29 -30
- data/spec/stream_spec.rb +58 -0
- data/spec/stroke_styles_spec.rb +36 -18
- data/spec/table/span_dummy_spec.rb +17 -0
- data/spec/table_spec.rb +697 -105
- data/spec/template_spec.rb +108 -54
- data/spec/text_at_spec.rb +18 -17
- data/spec/text_box_spec.rb +111 -62
- data/spec/text_rendering_mode_spec.rb +5 -5
- data/spec/text_spacing_spec.rb +4 -4
- data/spec/text_spec.rb +57 -49
- data/spec/transparency_spec.rb +5 -5
- metadata +421 -213
- 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/rails.dat +0 -0
- data/data/images/rails.png +0 -0
- data/examples/bounding_box/russian_boxes.rb +0 -37
- data/examples/example_helper.rb +0 -11
- data/examples/general/context_sensitive_headers.rb +0 -38
- data/examples/graphics/cmyk.rb +0 -13
- data/examples/graphics/gradient.rb +0 -23
- data/examples/graphics/png_types.rb +0 -23
- data/examples/graphics/remote_images.rb +0 -13
- data/examples/m17n/full_win_ansi_character_list.rb +0 -20
- data/examples/m17n/sjis.rb +0 -29
- data/examples/table/bill.rb +0 -54
- data/examples/table/header.rb +0 -15
- data/examples/text/font_calculations.rb +0 -92
- data/examples/text/hyphenation.rb +0 -45
- data/examples/text/indent_paragraphs.rb +0 -24
- data/lib/prawn/core.rb +0 -85
- data/lib/prawn/core/text/formatted/arranger.rb +0 -294
- data/lib/prawn/core/text/formatted/line_wrap.rb +0 -273
- data/lib/prawn/core/text/formatted/wrap.rb +0 -153
- data/lib/prawn/graphics/gradient.rb +0 -84
- data/lib/prawn/security/arcfour.rb +0 -51
data/lib/prawn/text.rb
CHANGED
@@ -5,15 +5,17 @@
|
|
5
5
|
# Copyright May 2008, Gregory Brown. All Rights Reserved.
|
6
6
|
#
|
7
7
|
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
-
|
9
|
-
require "prawn/text/formatted"
|
10
|
-
require "prawn/text/box"
|
8
|
+
|
11
9
|
require "zlib"
|
12
10
|
|
11
|
+
require_relative "../pdf/core/text"
|
12
|
+
require_relative "text/formatted"
|
13
|
+
require_relative "text/box"
|
14
|
+
|
13
15
|
module Prawn
|
14
16
|
module Text
|
15
17
|
|
16
|
-
include
|
18
|
+
include PDF::Core::Text
|
17
19
|
include Prawn::Text::Formatted
|
18
20
|
|
19
21
|
# No-Break Space
|
@@ -27,7 +29,7 @@ module Prawn
|
|
27
29
|
# method to use. If, instead, if you want to place bounded text outside of
|
28
30
|
# the flow of a document (for captions, labels, charts, etc.), use Text::Box
|
29
31
|
# or its convenience method text_box.
|
30
|
-
#
|
32
|
+
#
|
31
33
|
# Draws text on the page. Prawn attempts to wrap the text to fit within your
|
32
34
|
# current bounding box (or margin_box if no bounding box is being used).
|
33
35
|
# Text will flow onto the next page when it reaches the bottom of the
|
@@ -49,28 +51,29 @@ module Prawn
|
|
49
51
|
# entire document, set default_kerning = false for that document
|
50
52
|
#
|
51
53
|
# === Text Positioning Details
|
52
|
-
#
|
54
|
+
#
|
53
55
|
# The text is positioned at font.ascender below the baseline,
|
54
56
|
# making it easy to use this method within bounding boxes and spans.
|
55
57
|
#
|
56
58
|
# == Encoding
|
57
59
|
#
|
58
60
|
# Note that strings passed to this function should be encoded as UTF-8.
|
59
|
-
# If you get unexpected characters appearing in your rendered document,
|
61
|
+
# If you get unexpected characters appearing in your rendered document,
|
60
62
|
# check this.
|
61
63
|
#
|
62
64
|
# If the current font is a built-in one, although the string must be
|
63
65
|
# encoded as UTF-8, only characters that are available in WinAnsi
|
64
66
|
# are allowed.
|
65
67
|
#
|
66
|
-
# If an empty box is rendered to your PDF instead of the character you
|
68
|
+
# If an empty box is rendered to your PDF instead of the character you
|
67
69
|
# wanted it usually means the current font doesn't include that character.
|
68
70
|
#
|
69
71
|
# == Options (default values marked in [])
|
70
72
|
#
|
71
73
|
# <tt>:inline_format</tt>::
|
72
74
|
# <tt>boolean</tt>. If true, then the string parameter is interpreted
|
73
|
-
# as a HTML-esque string that recognizes the following tags
|
75
|
+
# as a HTML-esque string that recognizes the following tags
|
76
|
+
# (assuming the default text formatter is used):
|
74
77
|
# <tt>\<b></b></tt>:: bold
|
75
78
|
# <tt>\<i></i></tt>:: italic
|
76
79
|
# <tt>\<u></u></tt>:: underline
|
@@ -97,8 +100,8 @@ module Prawn
|
|
97
100
|
# <tt>anchor="ToC"</tt>::
|
98
101
|
# where the value of the anchor attribute is the name of a
|
99
102
|
# destination that has already been or will be registered
|
100
|
-
# using
|
101
|
-
# will be created to that destination.
|
103
|
+
# using PDF::Core::Destinations#add_dest. A clickable link
|
104
|
+
# will be created to that destination.
|
102
105
|
# Note that you must explicitly underline and color using the
|
103
106
|
# appropriate tags if you which to draw attention to the link
|
104
107
|
#
|
@@ -107,6 +110,7 @@ module Prawn
|
|
107
110
|
# [value of document.default_kerning?]
|
108
111
|
# <tt>:size</tt>:: <tt>number</tt>. The font size to use. [current font
|
109
112
|
# size]
|
113
|
+
# <tt>:color</tt>:: an RGB color ("ff0000") or CMYK array [10, 20, 30, 40].
|
110
114
|
# <tt>:character_spacing</tt>:: <tt>number</tt>. The amount of space to add
|
111
115
|
# to or remove from the default character
|
112
116
|
# spacing. [0]
|
@@ -142,7 +146,7 @@ module Prawn
|
|
142
146
|
# text should render with the fill color, stroke color or
|
143
147
|
# both. See the comments to text_rendering_mode() to see
|
144
148
|
# a list of valid options. [0]
|
145
|
-
#
|
149
|
+
#
|
146
150
|
# == Exceptions
|
147
151
|
#
|
148
152
|
# Raises <tt>ArgumentError</tt> if <tt>:at</tt> option included
|
@@ -151,12 +155,14 @@ module Prawn
|
|
151
155
|
# any text
|
152
156
|
#
|
153
157
|
def text(string, options={})
|
158
|
+
return false if string.nil?
|
154
159
|
# we modify the options. don't change the user's hash
|
155
160
|
options = options.dup
|
156
161
|
|
157
|
-
if options[:inline_format]
|
162
|
+
if p = options[:inline_format]
|
163
|
+
p = [] unless p.is_a?(Array)
|
158
164
|
options.delete(:inline_format)
|
159
|
-
array =
|
165
|
+
array = self.text_formatter.format(string, *p)
|
160
166
|
else
|
161
167
|
array = [{ :text => string }]
|
162
168
|
end
|
@@ -175,7 +181,7 @@ module Prawn
|
|
175
181
|
# text([{ :text => "hello" },
|
176
182
|
# { :text => "world",
|
177
183
|
# :size => 24,
|
178
|
-
# :
|
184
|
+
# :styles => [:bold, :italic] }])
|
179
185
|
#
|
180
186
|
# == Options
|
181
187
|
#
|
@@ -188,8 +194,14 @@ module Prawn
|
|
188
194
|
def formatted_text(array, options={})
|
189
195
|
options = inspect_options_for_text(options.dup)
|
190
196
|
|
197
|
+
if color = options.delete(:color)
|
198
|
+
array = array.map do |fragment|
|
199
|
+
fragment[:color] ? fragment : fragment.merge(:color => color)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
191
203
|
if @indent_paragraphs
|
192
|
-
|
204
|
+
self.text_formatter.array_paragraphs(array).each do |paragraph|
|
193
205
|
options[:skip_encoding] = false
|
194
206
|
remaining_text = draw_indented_formatted_line(paragraph, options)
|
195
207
|
options[:skip_encoding] = true
|
@@ -216,7 +228,7 @@ module Prawn
|
|
216
228
|
|
217
229
|
# Draws text on the page, beginning at the point specified by the :at option
|
218
230
|
# the string is assumed to be pre-formatted to properly fit the page.
|
219
|
-
#
|
231
|
+
#
|
220
232
|
# pdf.draw_text "Hello World", :at => [100,100]
|
221
233
|
# pdf.draw_text "Goodbye World", :at => [50,50], :size => 16
|
222
234
|
#
|
@@ -238,14 +250,14 @@ module Prawn
|
|
238
250
|
# == Encoding
|
239
251
|
#
|
240
252
|
# Note that strings passed to this function should be encoded as UTF-8.
|
241
|
-
# If you get unexpected characters appearing in your rendered document,
|
253
|
+
# If you get unexpected characters appearing in your rendered document,
|
242
254
|
# check this.
|
243
255
|
#
|
244
256
|
# If the current font is a built-in one, although the string must be
|
245
257
|
# encoded as UTF-8, only characters that are available in WinAnsi
|
246
258
|
# are allowed.
|
247
259
|
#
|
248
|
-
# If an empty box is rendered to your PDF instead of the character you
|
260
|
+
# If an empty box is rendered to your PDF instead of the character you
|
249
261
|
# wanted it usually means the current font doesn't include that character.
|
250
262
|
#
|
251
263
|
# == Options (default values marked in [])
|
@@ -274,7 +286,7 @@ module Prawn
|
|
274
286
|
text = text.to_s.dup
|
275
287
|
save_font do
|
276
288
|
process_text_options(options)
|
277
|
-
font.normalize_encoding!(text)
|
289
|
+
font.normalize_encoding!(text)
|
278
290
|
font_size(options[:size]) { draw_text!(text, options) }
|
279
291
|
end
|
280
292
|
end
|
@@ -307,7 +319,7 @@ module Prawn
|
|
307
319
|
# height_of_formatted([{ :text => "hello" },
|
308
320
|
# { :text => "world",
|
309
321
|
# :size => 24,
|
310
|
-
# :
|
322
|
+
# :styles => [:bold, :italic] }])
|
311
323
|
#
|
312
324
|
def height_of_formatted(array, options={})
|
313
325
|
if options[:indent_paragraphs]
|
@@ -318,7 +330,7 @@ module Prawn
|
|
318
330
|
box = Text::Formatted::Box.new(array,
|
319
331
|
options.merge(:height => 100000000,
|
320
332
|
:document => self))
|
321
|
-
|
333
|
+
box.render(:dry_run => true)
|
322
334
|
|
323
335
|
height = box.height
|
324
336
|
height += box.line_gap + box.leading if @final_gap
|
@@ -374,7 +386,7 @@ module Prawn
|
|
374
386
|
if options[:kerning].nil? then
|
375
387
|
options[:kerning] = default_kerning?
|
376
388
|
end
|
377
|
-
valid_options =
|
389
|
+
valid_options = PDF::Core::Text::VALID_OPTIONS + [:at, :rotate]
|
378
390
|
Prawn.verify_options(valid_options, options)
|
379
391
|
options
|
380
392
|
end
|
data/lib/prawn/text/box.rb
CHANGED
@@ -7,6 +7,8 @@
|
|
7
7
|
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
8
|
#
|
9
9
|
|
10
|
+
require_relative "formatted/box"
|
11
|
+
|
10
12
|
module Prawn
|
11
13
|
module Text
|
12
14
|
|
@@ -17,14 +19,14 @@ module Prawn
|
|
17
19
|
# == Encoding
|
18
20
|
#
|
19
21
|
# Note that strings passed to this function should be encoded as UTF-8.
|
20
|
-
# If you get unexpected characters appearing in your rendered document,
|
22
|
+
# If you get unexpected characters appearing in your rendered document,
|
21
23
|
# check this.
|
22
24
|
#
|
23
25
|
# If the current font is a built-in one, although the string must be
|
24
26
|
# encoded as UTF-8, only characters that are available in WinAnsi
|
25
27
|
# are allowed.
|
26
28
|
#
|
27
|
-
# If an empty box is rendered to your PDF instead of the character you
|
29
|
+
# If an empty box is rendered to your PDF instead of the character you
|
28
30
|
# wanted it usually means the current font doesn't include that character.
|
29
31
|
#
|
30
32
|
# == Options (default values marked in [])
|
@@ -67,7 +69,7 @@ module Prawn
|
|
67
69
|
# <tt>:valign</tt>::
|
68
70
|
# <tt>:top</tt>, <tt>:center</tt>, or <tt>:bottom</tt>. Vertical
|
69
71
|
# alignment within the bounding box [:top]
|
70
|
-
#
|
72
|
+
#
|
71
73
|
# <tt>:rotate</tt>::
|
72
74
|
# <tt>number</tt>. The angle to rotate the text
|
73
75
|
# <tt>:rotate_around</tt>::
|
@@ -105,11 +107,22 @@ module Prawn
|
|
105
107
|
# any text
|
106
108
|
#
|
107
109
|
def text_box(string, options={})
|
108
|
-
|
110
|
+
options = options.dup
|
111
|
+
options[:document] = self
|
112
|
+
|
113
|
+
box = if p = options.delete(:inline_format)
|
114
|
+
p = [] unless p.is_a?(Array)
|
115
|
+
array = self.text_formatter.format(string, *p)
|
116
|
+
Text::Formatted::Box.new(array, options)
|
117
|
+
else
|
118
|
+
Text::Box.new(string, options)
|
119
|
+
end
|
120
|
+
|
121
|
+
box.render
|
109
122
|
end
|
110
123
|
|
111
124
|
# Generally, one would use the Prawn::Text#text_box convenience
|
112
|
-
# method. However, using Text::Box.new in conjunction with
|
125
|
+
# method. However, using Text::Box.new in conjunction with
|
113
126
|
# #render(:dry_run=> true) enables one to do look-ahead calculations prior
|
114
127
|
# to placing text on the page, or to determine how much vertical space was
|
115
128
|
# consumed by the printed text
|
data/lib/prawn/text/formatted.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
require_relative "formatted/wrap"
|
2
|
+
|
3
|
+
require_relative "formatted/box"
|
4
|
+
require_relative "formatted/parser"
|
5
|
+
require_relative "formatted/fragment"
|
@@ -0,0 +1,292 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# core/text/formatted/arranger.rb : Implements a data structure for 2-stage
|
4
|
+
# processing of lines of 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
|
+
module Prawn
|
11
|
+
module Text
|
12
|
+
module Formatted #:nodoc:
|
13
|
+
|
14
|
+
class Arranger #:nodoc:
|
15
|
+
attr_reader :max_line_height
|
16
|
+
attr_reader :max_descender
|
17
|
+
attr_reader :max_ascender
|
18
|
+
attr_accessor :consumed
|
19
|
+
|
20
|
+
# The following present only for testing purposes
|
21
|
+
attr_reader :unconsumed
|
22
|
+
attr_reader :fragments
|
23
|
+
attr_reader :current_format_state
|
24
|
+
|
25
|
+
def initialize(document, options={})
|
26
|
+
@document = document
|
27
|
+
@fragments = []
|
28
|
+
@unconsumed = []
|
29
|
+
@kerning = options[:kerning]
|
30
|
+
end
|
31
|
+
|
32
|
+
def space_count
|
33
|
+
if @unfinalized_line
|
34
|
+
raise "Lines must be finalized before calling #space_count"
|
35
|
+
end
|
36
|
+
@fragments.inject(0) do |sum, fragment|
|
37
|
+
sum + fragment.space_count
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def line_width
|
42
|
+
if @unfinalized_line
|
43
|
+
raise "Lines must be finalized before calling #line_width"
|
44
|
+
end
|
45
|
+
@fragments.inject(0) do |sum, fragment|
|
46
|
+
sum + fragment.width
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def line
|
51
|
+
if @unfinalized_line
|
52
|
+
raise "Lines must be finalized before calling #line"
|
53
|
+
end
|
54
|
+
@fragments.collect do |fragment|
|
55
|
+
if ruby_18 { true }
|
56
|
+
fragment.text
|
57
|
+
else
|
58
|
+
fragment.text.dup.force_encoding("utf-8")
|
59
|
+
end
|
60
|
+
end.join
|
61
|
+
end
|
62
|
+
|
63
|
+
def finalize_line
|
64
|
+
@unfinalized_line = false
|
65
|
+
omit_trailing_whitespace_from_line_width
|
66
|
+
@fragments = []
|
67
|
+
@consumed.each do |hash|
|
68
|
+
text = hash[:text]
|
69
|
+
format_state = hash.dup
|
70
|
+
format_state.delete(:text)
|
71
|
+
fragment = Prawn::Text::Formatted::Fragment.new(text,
|
72
|
+
format_state,
|
73
|
+
@document)
|
74
|
+
@fragments << fragment
|
75
|
+
set_fragment_measurements(fragment)
|
76
|
+
set_line_measurement_maximums(fragment)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def format_array=(array)
|
81
|
+
initialize_line
|
82
|
+
@unconsumed = []
|
83
|
+
array.each do |hash|
|
84
|
+
hash[:text].scan(/[^\n]+|\n/) do |line|
|
85
|
+
@unconsumed << hash.merge(:text => line)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def initialize_line
|
91
|
+
@unfinalized_line = true
|
92
|
+
@max_line_height = 0
|
93
|
+
@max_descender = 0
|
94
|
+
@max_ascender = 0
|
95
|
+
|
96
|
+
@consumed = []
|
97
|
+
@fragments = []
|
98
|
+
end
|
99
|
+
|
100
|
+
def finished?
|
101
|
+
@unconsumed.length == 0
|
102
|
+
end
|
103
|
+
|
104
|
+
def next_string
|
105
|
+
unless @unfinalized_line
|
106
|
+
raise "Lines must not be finalized when calling #next_string"
|
107
|
+
end
|
108
|
+
hash = @unconsumed.shift
|
109
|
+
if hash.nil?
|
110
|
+
nil
|
111
|
+
else
|
112
|
+
@consumed << hash.dup
|
113
|
+
@current_format_state = hash.dup
|
114
|
+
@current_format_state.delete(:text)
|
115
|
+
hash[:text]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def preview_next_string
|
120
|
+
hash = @unconsumed.first
|
121
|
+
if hash.nil? then nil
|
122
|
+
else hash[:text]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def apply_color_and_font_settings(fragment, &block)
|
127
|
+
if fragment.color
|
128
|
+
original_fill_color = @document.fill_color
|
129
|
+
original_stroke_color = @document.stroke_color
|
130
|
+
@document.fill_color(*fragment.color)
|
131
|
+
@document.stroke_color(*fragment.color)
|
132
|
+
apply_font_settings(fragment, &block)
|
133
|
+
@document.stroke_color = original_stroke_color
|
134
|
+
@document.fill_color = original_fill_color
|
135
|
+
else
|
136
|
+
apply_font_settings(fragment, &block)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def apply_font_settings(fragment=nil, &block)
|
141
|
+
if fragment.nil?
|
142
|
+
font = current_format_state[:font]
|
143
|
+
size = current_format_state[:size]
|
144
|
+
character_spacing = current_format_state[:character_spacing] ||
|
145
|
+
@document.character_spacing
|
146
|
+
styles = current_format_state[:styles]
|
147
|
+
font_style = font_style(styles)
|
148
|
+
else
|
149
|
+
font = fragment.font
|
150
|
+
size = fragment.size
|
151
|
+
character_spacing = fragment.character_spacing
|
152
|
+
styles = fragment.styles
|
153
|
+
font_style = font_style(styles)
|
154
|
+
end
|
155
|
+
|
156
|
+
@document.character_spacing(character_spacing) do
|
157
|
+
if font || font_style != :normal
|
158
|
+
raise "Bad font family" unless @document.font.family
|
159
|
+
@document.font(font || @document.font.family, :style => font_style) do
|
160
|
+
apply_font_size(size, styles, &block)
|
161
|
+
end
|
162
|
+
else
|
163
|
+
apply_font_size(size, styles, &block)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
def update_last_string(printed, unprinted, normalized_soft_hyphen=nil)
|
169
|
+
return if printed.nil?
|
170
|
+
if printed.empty?
|
171
|
+
@consumed.pop
|
172
|
+
else
|
173
|
+
@consumed.last[:text] = printed
|
174
|
+
if normalized_soft_hyphen
|
175
|
+
@consumed.last[:normalized_soft_hyphen] = normalized_soft_hyphen
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
unless unprinted.empty?
|
180
|
+
@unconsumed.unshift(@current_format_state.merge(:text => unprinted))
|
181
|
+
end
|
182
|
+
|
183
|
+
load_previous_format_state if printed.empty?
|
184
|
+
end
|
185
|
+
|
186
|
+
def retrieve_fragment
|
187
|
+
if @unfinalized_line
|
188
|
+
raise "Lines must be finalized before fragments can be retrieved"
|
189
|
+
end
|
190
|
+
@fragments.shift
|
191
|
+
end
|
192
|
+
|
193
|
+
def repack_unretrieved
|
194
|
+
new_unconsumed = []
|
195
|
+
while fragment = retrieve_fragment
|
196
|
+
fragment.include_trailing_white_space!
|
197
|
+
new_unconsumed << fragment.format_state.merge(:text => fragment.text)
|
198
|
+
end
|
199
|
+
@unconsumed = new_unconsumed.concat(@unconsumed)
|
200
|
+
end
|
201
|
+
|
202
|
+
def font_style(styles)
|
203
|
+
if styles.nil?
|
204
|
+
:normal
|
205
|
+
elsif styles.include?(:bold) && styles.include?(:italic)
|
206
|
+
:bold_italic
|
207
|
+
elsif styles.include?(:bold)
|
208
|
+
:bold
|
209
|
+
elsif styles.include?(:italic)
|
210
|
+
:italic
|
211
|
+
else
|
212
|
+
:normal
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
private
|
217
|
+
|
218
|
+
def load_previous_format_state
|
219
|
+
if @consumed.empty?
|
220
|
+
@current_format_state = {}
|
221
|
+
else
|
222
|
+
hash = @consumed.last
|
223
|
+
@current_format_state = hash.dup
|
224
|
+
@current_format_state.delete(:text)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
def apply_font_size(size, styles)
|
229
|
+
if subscript?(styles) || superscript?(styles)
|
230
|
+
relative_size = 0.583
|
231
|
+
if size.nil?
|
232
|
+
size = @document.font_size * relative_size
|
233
|
+
else
|
234
|
+
size = size * relative_size
|
235
|
+
end
|
236
|
+
end
|
237
|
+
if size.nil?
|
238
|
+
yield
|
239
|
+
else
|
240
|
+
@document.font_size(size) { yield }
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
def subscript?(styles)
|
245
|
+
if styles.nil? then false
|
246
|
+
else styles.include?(:subscript)
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
def superscript?(styles)
|
251
|
+
if styles.nil? then false
|
252
|
+
else styles.include?(:superscript)
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
def omit_trailing_whitespace_from_line_width
|
257
|
+
@consumed.reverse_each do |hash|
|
258
|
+
if hash[:text] == "\n"
|
259
|
+
break
|
260
|
+
elsif hash[:text].strip.empty? && @consumed.length > 1
|
261
|
+
# this entire fragment is trailing white space
|
262
|
+
hash[:exclude_trailing_white_space] = true
|
263
|
+
else
|
264
|
+
# this fragment contains the first non-white space we have
|
265
|
+
# encountered since the end of the line
|
266
|
+
hash[:exclude_trailing_white_space] = true
|
267
|
+
break
|
268
|
+
end
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
def set_fragment_measurements(fragment)
|
273
|
+
apply_font_settings(fragment) do
|
274
|
+
fragment.width = @document.width_of(fragment.text,
|
275
|
+
:kerning => @kerning)
|
276
|
+
fragment.line_height = @document.font.height
|
277
|
+
fragment.descender = @document.font.descender
|
278
|
+
fragment.ascender = @document.font.ascender
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
def set_line_measurement_maximums(fragment)
|
283
|
+
@max_line_height = [defined?(@max_line_height) && @max_line_height, fragment.line_height].compact.max
|
284
|
+
@max_descender = [defined?(@max_descender) && @max_descender, fragment.descender].compact.max
|
285
|
+
@max_ascender = [defined?(@max_ascender) && @max_ascender, fragment.ascender].compact.max
|
286
|
+
end
|
287
|
+
|
288
|
+
end
|
289
|
+
|
290
|
+
end
|
291
|
+
end
|
292
|
+
end
|