prawn 2.1.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +2 -0
- data.tar.gz.sig +0 -0
- data/Gemfile +1 -9
- data/Rakefile +12 -22
- data/lib/prawn.rb +29 -48
- data/lib/prawn/document.rb +148 -123
- data/lib/prawn/document/bounding_box.rb +33 -26
- data/lib/prawn/document/column_box.rb +5 -7
- data/lib/prawn/document/internals.rb +6 -6
- data/lib/prawn/document/span.rb +20 -17
- data/lib/prawn/encoding.rb +65 -67
- data/lib/prawn/errors.rb +10 -7
- data/lib/prawn/font.rb +78 -62
- data/lib/prawn/font/afm.rb +93 -66
- data/lib/prawn/font/dfont.rb +2 -10
- data/lib/prawn/font/ttc.rb +34 -0
- data/lib/prawn/font/ttf.rb +73 -65
- data/lib/prawn/font_metric_cache.rb +9 -8
- data/lib/prawn/graphics.rb +110 -70
- data/lib/prawn/graphics/blend_mode.rb +7 -8
- data/lib/prawn/graphics/cap_style.rb +2 -4
- data/lib/prawn/graphics/color.rb +23 -26
- data/lib/prawn/graphics/dash.rb +22 -12
- data/lib/prawn/graphics/join_style.rb +8 -4
- data/lib/prawn/graphics/patterns.rb +185 -96
- data/lib/prawn/graphics/transformation.rb +11 -9
- data/lib/prawn/graphics/transparency.rb +15 -13
- data/lib/prawn/grid.rb +20 -20
- data/lib/prawn/image_handler.rb +4 -6
- data/lib/prawn/images.rb +22 -15
- data/lib/prawn/images/image.rb +0 -1
- data/lib/prawn/images/jpg.rb +26 -22
- data/lib/prawn/images/png.rb +60 -57
- data/lib/prawn/measurement_extensions.rb +8 -9
- data/lib/prawn/measurements.rb +14 -15
- data/lib/prawn/outline.rb +96 -78
- data/lib/prawn/repeater.rb +12 -10
- data/lib/prawn/security.rb +66 -48
- data/lib/prawn/security/arcfour.rb +1 -3
- data/lib/prawn/soft_mask.rb +23 -25
- data/lib/prawn/stamp.rb +16 -12
- data/lib/prawn/text.rb +59 -45
- data/lib/prawn/text/box.rb +9 -8
- data/lib/prawn/text/formatted.rb +4 -6
- data/lib/prawn/text/formatted/arranger.rb +51 -30
- data/lib/prawn/text/formatted/box.rb +112 -88
- data/lib/prawn/text/formatted/fragment.rb +10 -15
- data/lib/prawn/text/formatted/line_wrap.rb +118 -61
- data/lib/prawn/text/formatted/parser.rb +134 -110
- data/lib/prawn/text/formatted/wrap.rb +42 -32
- data/lib/prawn/transformation_stack.rb +3 -4
- data/lib/prawn/utilities.rb +6 -21
- data/lib/prawn/version.rb +1 -3
- data/lib/prawn/view.rb +4 -2
- data/manual/basic_concepts/adding_pages.rb +4 -7
- data/manual/basic_concepts/basic_concepts.rb +29 -22
- data/manual/basic_concepts/creation.rb +8 -11
- data/manual/basic_concepts/cursor.rb +2 -5
- data/manual/basic_concepts/measurement.rb +3 -6
- data/manual/basic_concepts/origin.rb +3 -6
- data/manual/basic_concepts/other_cursor_helpers.rb +9 -12
- data/manual/basic_concepts/view.rb +20 -16
- data/manual/bounding_box/bounding_box.rb +27 -24
- data/manual/bounding_box/bounds.rb +9 -12
- data/manual/bounding_box/canvas.rb +2 -5
- data/manual/bounding_box/creation.rb +4 -7
- data/manual/bounding_box/indentation.rb +12 -15
- data/manual/bounding_box/nesting.rb +22 -17
- data/manual/bounding_box/russian_boxes.rb +8 -9
- data/manual/bounding_box/stretchy.rb +10 -13
- data/manual/contents.rb +26 -22
- data/manual/cover.rb +22 -20
- data/manual/document_and_page_options/background.rb +9 -13
- data/manual/document_and_page_options/document_and_page_options.rb +23 -20
- data/manual/document_and_page_options/metadata.rb +16 -16
- data/manual/document_and_page_options/page_margins.rb +16 -20
- data/manual/document_and_page_options/page_size.rb +11 -12
- data/manual/document_and_page_options/print_scaling.rb +15 -15
- data/manual/example_helper.rb +2 -4
- data/manual/graphics/blend_mode.rb +10 -9
- data/manual/graphics/circle_and_ellipse.rb +2 -5
- data/manual/graphics/color.rb +5 -9
- data/manual/graphics/common_lines.rb +5 -8
- data/manual/graphics/fill_and_stroke.rb +2 -5
- data/manual/graphics/fill_rules.rb +7 -10
- data/manual/graphics/gradients.rb +25 -21
- data/manual/graphics/graphics.rb +49 -43
- data/manual/graphics/helper.rb +10 -9
- data/manual/graphics/line_width.rb +5 -7
- data/manual/graphics/lines_and_curves.rb +5 -8
- data/manual/graphics/polygon.rb +4 -8
- data/manual/graphics/rectangle.rb +2 -5
- data/manual/graphics/rotate.rb +4 -7
- data/manual/graphics/scale.rb +12 -15
- data/manual/graphics/soft_masks.rb +1 -4
- data/manual/graphics/stroke_cap.rb +3 -6
- data/manual/graphics/stroke_dash.rb +9 -12
- data/manual/graphics/stroke_join.rb +2 -5
- data/manual/graphics/translate.rb +7 -10
- data/manual/graphics/transparency.rb +5 -8
- data/manual/how_to_read_this_manual.rb +4 -6
- data/manual/images/absolute_position.rb +4 -7
- data/manual/images/fit.rb +5 -8
- data/manual/images/horizontal.rb +6 -9
- data/manual/images/images.rb +25 -23
- data/manual/images/plain_image.rb +3 -6
- data/manual/images/scale.rb +7 -10
- data/manual/images/vertical.rb +10 -13
- data/manual/images/width_and_height.rb +8 -11
- data/manual/layout/boxes.rb +3 -6
- data/manual/layout/content.rb +5 -8
- data/manual/layout/layout.rb +16 -16
- data/manual/layout/simple_grid.rb +4 -7
- data/manual/outline/add_subsection_to.rb +18 -21
- data/manual/outline/insert_section_after.rb +13 -16
- data/manual/outline/outline.rb +19 -17
- data/manual/outline/sections_and_pages.rb +15 -18
- data/manual/repeatable_content/alternate_page_numbering.rb +19 -17
- data/manual/repeatable_content/page_numbering.rb +15 -16
- data/manual/repeatable_content/repeatable_content.rb +23 -19
- data/manual/repeatable_content/repeater.rb +12 -15
- data/manual/repeatable_content/stamp.rb +12 -15
- data/manual/security/encryption.rb +7 -10
- data/manual/security/permissions.rb +17 -14
- data/manual/security/security.rb +17 -16
- data/manual/table.rb +2 -4
- data/manual/text/alignment.rb +14 -17
- data/manual/text/color.rb +10 -11
- data/manual/text/column_box.rb +5 -8
- data/manual/text/fallback_fonts.rb +23 -21
- data/manual/text/font.rb +9 -12
- data/manual/text/font_size.rb +11 -14
- data/manual/text/font_style.rb +4 -7
- data/manual/text/formatted_callbacks.rb +23 -21
- data/manual/text/formatted_text.rb +31 -25
- data/manual/text/free_flowing_text.rb +18 -21
- data/manual/text/inline.rb +16 -19
- data/manual/text/kerning_and_character_spacing.rb +12 -15
- data/manual/text/leading.rb +5 -8
- data/manual/text/line_wrapping.rb +33 -17
- data/manual/text/paragraph_indentation.rb +11 -14
- data/manual/text/positioned_text.rb +13 -16
- data/manual/text/registering_families.rb +16 -19
- data/manual/text/rendering_and_color.rb +7 -10
- data/manual/text/right_to_left_text.rb +24 -19
- data/manual/text/rotation.rb +26 -23
- data/manual/text/single_usage.rb +6 -9
- data/manual/text/text.rb +56 -52
- data/manual/text/text_box_excess.rb +18 -17
- data/manual/text/text_box_extensions.rb +16 -15
- data/manual/text/text_box_overflow.rb +15 -18
- data/manual/text/utf8.rb +9 -12
- data/manual/text/win_ansi_charset.rb +18 -19
- data/prawn.gemspec +37 -27
- data/spec/extensions/encoding_helpers.rb +0 -2
- data/spec/manual_spec.rb +33 -0
- data/spec/prawn/document/bounding_box_spec.rb +546 -0
- data/spec/prawn/document/column_box_spec.rb +73 -0
- data/spec/prawn/document/security_spec.rb +173 -0
- data/spec/prawn/document_annotations_spec.rb +74 -0
- data/spec/prawn/document_destinations_spec.rb +13 -0
- data/spec/prawn/document_grid_spec.rb +96 -0
- data/spec/prawn/document_reference_spec.rb +25 -0
- data/spec/prawn/document_span_spec.rb +34 -0
- data/spec/prawn/document_spec.rb +751 -0
- data/spec/prawn/font_metric_cache_spec.rb +52 -0
- data/spec/prawn/font_spec.rb +513 -0
- data/spec/prawn/graphics/blend_mode_spec.rb +61 -0
- data/spec/prawn/graphics/transparency_spec.rb +79 -0
- data/spec/prawn/graphics_spec.rb +817 -0
- data/spec/prawn/graphics_stroke_styles_spec.rb +227 -0
- data/spec/{image_handler_spec.rb → prawn/image_handler_spec.rb} +13 -15
- data/spec/prawn/images/jpg_spec.rb +18 -0
- data/spec/prawn/images/png_spec.rb +281 -0
- data/spec/prawn/images_spec.rb +170 -0
- data/spec/prawn/measurements_extensions_spec.rb +22 -0
- data/spec/prawn/outline_spec.rb +408 -0
- data/spec/prawn/repeater_spec.rb +163 -0
- data/spec/prawn/soft_mask_spec.rb +72 -0
- data/spec/prawn/stamp_spec.rb +168 -0
- data/spec/prawn/text/box_spec.rb +1113 -0
- data/spec/prawn/text/formatted/arranger_spec.rb +464 -0
- data/spec/prawn/text/formatted/box_spec.rb +825 -0
- data/spec/prawn/text/formatted/fragment_spec.rb +341 -0
- data/spec/prawn/text/formatted/line_wrap_spec.rb +491 -0
- data/spec/prawn/text/formatted/parser_spec.rb +667 -0
- data/spec/prawn/text_draw_text_spec.rb +147 -0
- data/spec/prawn/text_rendering_mode_spec.rb +42 -0
- data/spec/prawn/text_spacing_spec.rb +93 -0
- data/spec/prawn/text_spec.rb +601 -0
- data/spec/prawn/text_with_inline_formatting_spec.rb +33 -0
- data/spec/{transformation_stack_spec.rb → prawn/transformation_stack_spec.rb} +21 -20
- data/spec/prawn/view_spec.rb +45 -0
- data/spec/spec_helper.rb +16 -16
- metadata +96 -151
- metadata.gz.sig +1 -0
- data/data/images/16bit.alpha +0 -0
- data/data/images/16bit.color +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/blend_modes_bottom_layer.jpg +0 -0
- data/data/images/blend_modes_top_layer.jpg +0 -0
- data/data/images/dice.alpha +0 -0
- data/data/images/dice.color +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/indexed_color.dat +0 -0
- data/data/images/indexed_color.png +0 -0
- data/data/images/indexed_transparency.png +0 -0
- data/data/images/indexed_transparency_alpha.dat +0 -0
- data/data/images/indexed_transparency_color.dat +0 -0
- data/data/images/letterhead.jpg +0 -0
- data/data/images/license.md +0 -8
- data/data/images/page_white_text.alpha +0 -0
- data/data/images/page_white_text.color +0 -0
- data/data/images/page_white_text.png +0 -0
- data/data/images/pigs.jpg +0 -0
- data/data/images/prawn.png +0 -0
- data/data/images/ruport.png +0 -0
- data/data/images/ruport_data.dat +0 -0
- data/data/images/ruport_transparent.png +0 -0
- data/data/images/ruport_type0.png +0 -0
- data/data/images/stef.jpg +0 -0
- data/data/images/tru256.bmp +0 -0
- data/data/images/web-links.dat +0 -1
- data/data/images/web-links.png +0 -0
- data/data/pdfs/complex_template.pdf +0 -0
- data/data/pdfs/contains_ttf_font.pdf +0 -0
- data/data/pdfs/encrypted.pdf +0 -0
- data/data/pdfs/form.pdf +1 -819
- data/data/pdfs/hexagon.pdf +0 -61
- data/data/pdfs/indirect_reference.pdf +0 -86
- data/data/pdfs/multipage_template.pdf +0 -127
- data/data/pdfs/nested_pages.pdf +0 -118
- data/data/pdfs/page_without_mediabox.pdf +0 -193
- data/data/pdfs/resources_as_indirect_object.pdf +0 -83
- data/data/pdfs/two_hexagons.pdf +0 -90
- data/data/pdfs/version_1_6.pdf +0 -61
- data/data/shift_jis_text.txt +0 -1
- data/spec/acceptance/png_spec.rb +0 -35
- data/spec/annotations_spec.rb +0 -67
- data/spec/blend_mode_spec.rb +0 -71
- data/spec/bounding_box_spec.rb +0 -501
- data/spec/column_box_spec.rb +0 -59
- data/spec/destinations_spec.rb +0 -13
- data/spec/document_spec.rb +0 -738
- data/spec/font_metric_cache_spec.rb +0 -52
- data/spec/font_spec.rb +0 -475
- data/spec/formatted_text_arranger_spec.rb +0 -452
- data/spec/formatted_text_box_spec.rb +0 -716
- data/spec/formatted_text_fragment_spec.rb +0 -299
- data/spec/graphics_spec.rb +0 -705
- data/spec/grid_spec.rb +0 -95
- data/spec/images_spec.rb +0 -167
- data/spec/inline_formatted_text_parser_spec.rb +0 -568
- data/spec/jpg_spec.rb +0 -23
- data/spec/line_wrap_spec.rb +0 -366
- data/spec/measurement_units_spec.rb +0 -22
- data/spec/outline_spec.rb +0 -409
- data/spec/png_spec.rb +0 -257
- data/spec/reference_spec.rb +0 -25
- data/spec/repeater_spec.rb +0 -154
- data/spec/security_spec.rb +0 -151
- data/spec/soft_mask_spec.rb +0 -78
- data/spec/span_spec.rb +0 -43
- data/spec/stamp_spec.rb +0 -179
- data/spec/stroke_styles_spec.rb +0 -208
- data/spec/text_at_spec.rb +0 -142
- data/spec/text_box_spec.rb +0 -1042
- data/spec/text_rendering_mode_spec.rb +0 -45
- data/spec/text_spacing_spec.rb +0 -93
- data/spec/text_spec.rb +0 -543
- data/spec/text_with_inline_formatting_spec.rb +0 -35
- data/spec/transparency_spec.rb +0 -91
- data/spec/view_spec.rb +0 -42
data/lib/prawn/text/box.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
# text/rectangle.rb : Implements text boxes
|
4
2
|
#
|
5
3
|
# Copyright November 2009, Daniel Nelson. All Rights Reserved.
|
@@ -7,7 +5,7 @@
|
|
7
5
|
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
6
|
#
|
9
7
|
|
10
|
-
require_relative
|
8
|
+
require_relative 'formatted/box'
|
11
9
|
|
12
10
|
module Prawn
|
13
11
|
module Text
|
@@ -52,7 +50,8 @@ module Prawn
|
|
52
50
|
# <tt>[x, y]</tt>. The upper left corner of the box
|
53
51
|
# [@document.bounds.left, @document.bounds.top]
|
54
52
|
# <tt>:width</tt>::
|
55
|
-
# <tt>number</tt>. The width of the box
|
53
|
+
# <tt>number</tt>. The width of the box
|
54
|
+
# [@document.bounds.right - @at[0]]
|
56
55
|
# <tt>:height</tt>::
|
57
56
|
# <tt>number</tt>. The height of the box [default_height()]
|
58
57
|
# <tt>:direction</tt>::
|
@@ -82,7 +81,8 @@ module Prawn
|
|
82
81
|
# <tt>number</tt>. Additional space between lines [value of
|
83
82
|
# document.default_leading]
|
84
83
|
# <tt>:single_line</tt>::
|
85
|
-
# <tt>boolean</tt>. If true, then only the first line will be drawn
|
84
|
+
# <tt>boolean</tt>. If true, then only the first line will be drawn
|
85
|
+
# [false]
|
86
86
|
# <tt>:overflow</tt>::
|
87
87
|
# <tt>:truncate</tt>, <tt>:shrink_to_fit</tt>, or <tt>:expand</tt>
|
88
88
|
# This controls the behavior when the amount of text
|
@@ -105,9 +105,10 @@ module Prawn
|
|
105
105
|
options = options.dup
|
106
106
|
options[:document] = self
|
107
107
|
|
108
|
-
box = if
|
108
|
+
box = if options[:inline_format]
|
109
|
+
p = options.delete(:inline_format)
|
109
110
|
p = [] unless p.is_a?(Array)
|
110
|
-
array =
|
111
|
+
array = text_formatter.format(string, *p)
|
111
112
|
Text::Formatted::Box.new(array, options)
|
112
113
|
else
|
113
114
|
Text::Box.new(string, options)
|
@@ -126,7 +127,7 @@ module Prawn
|
|
126
127
|
#
|
127
128
|
class Box < Prawn::Text::Formatted::Box
|
128
129
|
def initialize(string, options = {})
|
129
|
-
super([{ :
|
130
|
+
super([{ text: string }], options)
|
130
131
|
end
|
131
132
|
|
132
133
|
def render(flags = {})
|
data/lib/prawn/text/formatted.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
|
-
|
1
|
+
require_relative 'formatted/wrap'
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
|
5
|
-
require_relative
|
6
|
-
require_relative "formatted/parser"
|
7
|
-
require_relative "formatted/fragment"
|
3
|
+
require_relative 'formatted/box'
|
4
|
+
require_relative 'formatted/parser'
|
5
|
+
require_relative 'formatted/fragment'
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
# core/text/formatted/arranger.rb : Implements a data structure for 2-stage
|
4
2
|
# processing of lines of formatted text
|
5
3
|
#
|
@@ -7,6 +5,8 @@
|
|
7
5
|
#
|
8
6
|
# This is free software. Please see the LICENSE and COPYING files for details.
|
9
7
|
|
8
|
+
# rubocop: disable Style/AccessorMethodName
|
9
|
+
|
10
10
|
module Prawn
|
11
11
|
module Text
|
12
12
|
module Formatted #:nodoc:
|
@@ -33,7 +33,7 @@ module Prawn
|
|
33
33
|
|
34
34
|
def space_count
|
35
35
|
unless finalized
|
36
|
-
|
36
|
+
raise 'Lines must be finalized before calling #space_count'
|
37
37
|
end
|
38
38
|
|
39
39
|
@fragments.inject(0) do |sum, fragment|
|
@@ -43,7 +43,7 @@ module Prawn
|
|
43
43
|
|
44
44
|
def line_width
|
45
45
|
unless finalized
|
46
|
-
|
46
|
+
raise 'Lines must be finalized before calling #line_width'
|
47
47
|
end
|
48
48
|
|
49
49
|
@fragments.inject(0) do |sum, fragment|
|
@@ -53,11 +53,16 @@ module Prawn
|
|
53
53
|
|
54
54
|
def line
|
55
55
|
unless finalized
|
56
|
-
|
56
|
+
raise 'Lines must be finalized before calling #line'
|
57
57
|
end
|
58
58
|
|
59
59
|
@fragments.collect do |fragment|
|
60
|
-
|
60
|
+
begin
|
61
|
+
fragment.text.dup.encode(::Encoding::UTF_8)
|
62
|
+
rescue ::Encoding::InvalidByteSequenceError,
|
63
|
+
::Encoding::UndefinedConversionError
|
64
|
+
fragment.text.dup.force_encoding(::Encoding::UTF_8)
|
65
|
+
end
|
61
66
|
end.join
|
62
67
|
end
|
63
68
|
|
@@ -70,9 +75,11 @@ module Prawn
|
|
70
75
|
text = hash[:text]
|
71
76
|
format_state = hash.dup
|
72
77
|
format_state.delete(:text)
|
73
|
-
fragment = Prawn::Text::Formatted::Fragment.new(
|
74
|
-
|
75
|
-
|
78
|
+
fragment = Prawn::Text::Formatted::Fragment.new(
|
79
|
+
text,
|
80
|
+
format_state,
|
81
|
+
@document
|
82
|
+
)
|
76
83
|
@fragments << fragment
|
77
84
|
set_fragment_measurements(fragment)
|
78
85
|
set_line_measurement_maximums(fragment)
|
@@ -84,7 +91,7 @@ module Prawn
|
|
84
91
|
@unconsumed = []
|
85
92
|
array.each do |hash|
|
86
93
|
hash[:text].scan(/[^\n]+|\n/) do |line|
|
87
|
-
@unconsumed << hash.merge(:
|
94
|
+
@unconsumed << hash.merge(text: line)
|
88
95
|
end
|
89
96
|
end
|
90
97
|
end
|
@@ -100,12 +107,12 @@ module Prawn
|
|
100
107
|
end
|
101
108
|
|
102
109
|
def finished?
|
103
|
-
@unconsumed.
|
110
|
+
@unconsumed.empty?
|
104
111
|
end
|
105
112
|
|
106
113
|
def next_string
|
107
114
|
if finalized
|
108
|
-
|
115
|
+
raise 'Lines must not be finalized when calling #next_string'
|
109
116
|
end
|
110
117
|
|
111
118
|
next_unconsumed_hash = @unconsumed.shift
|
@@ -146,21 +153,22 @@ module Prawn
|
|
146
153
|
font = current_format_state[:font]
|
147
154
|
size = current_format_state[:size]
|
148
155
|
character_spacing = current_format_state[:character_spacing] ||
|
149
|
-
|
156
|
+
@document.character_spacing
|
150
157
|
styles = current_format_state[:styles]
|
151
|
-
font_style = font_style(styles)
|
152
158
|
else
|
153
159
|
font = fragment.font
|
154
160
|
size = fragment.size
|
155
161
|
character_spacing = fragment.character_spacing
|
156
162
|
styles = fragment.styles
|
157
|
-
font_style = font_style(styles)
|
158
163
|
end
|
164
|
+
font_style = font_style(styles)
|
159
165
|
|
160
166
|
@document.character_spacing(character_spacing) do
|
161
167
|
if font || font_style != :normal
|
162
|
-
|
163
|
-
@document.font(
|
168
|
+
raise 'Bad font family' unless @document.font.family
|
169
|
+
@document.font(
|
170
|
+
font || @document.font.family, style: font_style
|
171
|
+
) do
|
164
172
|
apply_font_size(size, styles, &block)
|
165
173
|
end
|
166
174
|
else
|
@@ -181,7 +189,7 @@ module Prawn
|
|
181
189
|
end
|
182
190
|
|
183
191
|
unless unprinted.empty?
|
184
|
-
@unconsumed.unshift(@current_format_state.merge(:
|
192
|
+
@unconsumed.unshift(@current_format_state.merge(text: unprinted))
|
185
193
|
end
|
186
194
|
|
187
195
|
load_previous_format_state if printed.empty?
|
@@ -189,7 +197,7 @@ module Prawn
|
|
189
197
|
|
190
198
|
def retrieve_fragment
|
191
199
|
unless finalized
|
192
|
-
|
200
|
+
raise 'Lines must be finalized before fragments can be retrieved'
|
193
201
|
end
|
194
202
|
|
195
203
|
@fragments.shift
|
@@ -197,9 +205,11 @@ module Prawn
|
|
197
205
|
|
198
206
|
def repack_unretrieved
|
199
207
|
new_unconsumed = []
|
208
|
+
# rubocop: disable Lint/AssignmentInCondition
|
200
209
|
while fragment = retrieve_fragment
|
210
|
+
# rubocop: enable Lint/AssignmentInCondition
|
201
211
|
fragment.include_trailing_white_space!
|
202
|
-
new_unconsumed << fragment.format_state.merge(:
|
212
|
+
new_unconsumed << fragment.format_state.merge(text: fragment.text)
|
203
213
|
end
|
204
214
|
@unconsumed = new_unconsumed.concat(@unconsumed)
|
205
215
|
end
|
@@ -233,11 +243,11 @@ module Prawn
|
|
233
243
|
def apply_font_size(size, styles)
|
234
244
|
if subscript?(styles) || superscript?(styles)
|
235
245
|
relative_size = 0.583
|
236
|
-
if size.nil?
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
246
|
+
size = if size.nil?
|
247
|
+
@document.font_size * relative_size
|
248
|
+
else
|
249
|
+
size * relative_size
|
250
|
+
end
|
241
251
|
end
|
242
252
|
if size.nil?
|
243
253
|
yield
|
@@ -276,8 +286,10 @@ module Prawn
|
|
276
286
|
|
277
287
|
def set_fragment_measurements(fragment)
|
278
288
|
apply_font_settings(fragment) do
|
279
|
-
fragment.width = @document.width_of(
|
280
|
-
|
289
|
+
fragment.width = @document.width_of(
|
290
|
+
fragment.text,
|
291
|
+
kerning: @kerning
|
292
|
+
)
|
281
293
|
fragment.line_height = @document.font.height
|
282
294
|
fragment.descender = @document.font.descender
|
283
295
|
fragment.ascender = @document.font.ascender
|
@@ -285,9 +297,18 @@ module Prawn
|
|
285
297
|
end
|
286
298
|
|
287
299
|
def set_line_measurement_maximums(fragment)
|
288
|
-
@max_line_height = [
|
289
|
-
|
290
|
-
|
300
|
+
@max_line_height = [
|
301
|
+
defined?(@max_line_height) && @max_line_height,
|
302
|
+
fragment.line_height
|
303
|
+
].compact.max
|
304
|
+
@max_descender = [
|
305
|
+
defined?(@max_descender) && @max_descender,
|
306
|
+
fragment.descender
|
307
|
+
].compact.max
|
308
|
+
@max_ascender = [
|
309
|
+
defined?(@max_ascender) && @max_ascender,
|
310
|
+
fragment.ascender
|
311
|
+
].compact.max
|
291
312
|
end
|
292
313
|
end
|
293
314
|
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
# text/formatted/rectangle.rb : Implements text boxes with formatted text
|
4
2
|
#
|
5
3
|
# Copyright February 2010, Daniel Nelson. All Rights Reserved.
|
@@ -54,8 +52,8 @@ module Prawn
|
|
54
52
|
# a file or application to be opened locally. A clickable link will be
|
55
53
|
# created to the provided local file or application. If the file is
|
56
54
|
# another PDF, it will be opened in a new window. Note that you must
|
57
|
-
# explicitly underline and color using the appropriate tags if you
|
58
|
-
# to draw attention to the link
|
55
|
+
# explicitly underline and color using the appropriate tags if you
|
56
|
+
# which to draw attention to the link
|
59
57
|
# <tt>:draw_text_callback</tt>:
|
60
58
|
# if provided, this Proc will be called instead of #draw_text! once
|
61
59
|
# per fragment for every low-level addition of text to the page.
|
@@ -83,13 +81,14 @@ module Prawn
|
|
83
81
|
#
|
84
82
|
# == Exceptions
|
85
83
|
#
|
86
|
-
# Raises "Bad font family" if no font family is defined for the current
|
84
|
+
# Raises "Bad font family" if no font family is defined for the current
|
85
|
+
# font
|
87
86
|
#
|
88
87
|
# Raises <tt>Prawn::Errors::CannotFit</tt> if not wide enough to print
|
89
88
|
# any text
|
90
89
|
#
|
91
90
|
def formatted_text_box(array, options = {})
|
92
|
-
Text::Formatted::Box.new(array, options.merge(:
|
91
|
+
Text::Formatted::Box.new(array, options.merge(document: self)).render
|
93
92
|
end
|
94
93
|
|
95
94
|
# Generally, one would use the Prawn::Text::Formatted#formatted_text_box
|
@@ -137,37 +136,38 @@ module Prawn
|
|
137
136
|
# See Prawn::Text#text_box for valid options
|
138
137
|
#
|
139
138
|
def initialize(formatted_text, options = {})
|
140
|
-
@inked
|
139
|
+
@inked = false
|
141
140
|
Prawn.verify_options(valid_options, options)
|
142
|
-
options
|
141
|
+
options = options.dup
|
143
142
|
|
144
143
|
self.class.extensions.reverse_each { |e| extend e }
|
145
144
|
|
146
|
-
@overflow
|
145
|
+
@overflow = options[:overflow] || :truncate
|
147
146
|
@disable_wrap_by_char = options[:disable_wrap_by_char]
|
148
147
|
|
149
148
|
self.original_text = formatted_text
|
150
|
-
@text
|
151
|
-
|
152
|
-
@document
|
153
|
-
@direction
|
154
|
-
@fallback_fonts
|
155
|
-
|
156
|
-
@at
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
@
|
162
|
-
|
163
|
-
|
164
|
-
@
|
149
|
+
@text = nil
|
150
|
+
|
151
|
+
@document = options[:document]
|
152
|
+
@direction = options[:direction] || @document.text_direction
|
153
|
+
@fallback_fonts = options[:fallback_fonts] ||
|
154
|
+
@document.fallback_fonts
|
155
|
+
@at = (
|
156
|
+
options[:at] || [@document.bounds.left, @document.bounds.top]
|
157
|
+
).dup
|
158
|
+
@width = options[:width] ||
|
159
|
+
@document.bounds.right - @at[0]
|
160
|
+
@height = options[:height] || default_height
|
161
|
+
@align = options[:align] ||
|
162
|
+
(@direction == :rtl ? :right : :left)
|
163
|
+
@vertical_align = options[:valign] || :top
|
164
|
+
@leading = options[:leading] || @document.default_leading
|
165
165
|
@character_spacing = options[:character_spacing] ||
|
166
|
-
|
167
|
-
@mode
|
168
|
-
@rotate
|
169
|
-
@rotate_around
|
170
|
-
@single_line
|
166
|
+
@document.character_spacing
|
167
|
+
@mode = options[:mode] || @document.text_rendering_mode
|
168
|
+
@rotate = options[:rotate] || 0
|
169
|
+
@rotate_around = options[:rotate_around] || :upper_left
|
170
|
+
@single_line = options[:single_line]
|
171
171
|
@draw_text_callback = options[:draw_text_callback]
|
172
172
|
|
173
173
|
# if the text rendering mode is :unknown, force it back to :fill
|
@@ -183,25 +183,28 @@ module Prawn
|
|
183
183
|
@overflow = :truncate
|
184
184
|
end
|
185
185
|
@min_font_size = options[:min_font_size] || 5
|
186
|
-
if options[:kerning].nil?
|
186
|
+
if options[:kerning].nil?
|
187
187
|
options[:kerning] = @document.default_kerning?
|
188
188
|
end
|
189
|
-
@options = {
|
190
|
-
|
191
|
-
|
189
|
+
@options = {
|
190
|
+
kerning: options[:kerning],
|
191
|
+
size: options[:size],
|
192
|
+
style: options[:style]
|
193
|
+
}
|
192
194
|
|
193
195
|
super(formatted_text, options)
|
194
196
|
end
|
195
197
|
|
196
|
-
# Render text to the document based on the settings defined in
|
198
|
+
# Render text to the document based on the settings defined in
|
199
|
+
# initialize.
|
197
200
|
#
|
198
|
-
# In order to facilitate look-ahead calculations, <tt>render</tt>
|
199
|
-
# a <tt>:dry_run => true</tt> option. If provided, then
|
200
|
-
# executed as if rendering, with the exception that
|
201
|
-
# the page. Useful for look-ahead computations of
|
202
|
-
# etc.
|
201
|
+
# In order to facilitate look-ahead calculations, <tt>render</tt>
|
202
|
+
# accepts a <tt>:dry_run => true</tt> option. If provided, then
|
203
|
+
# everything is executed as if rendering, with the exception that
|
204
|
+
# nothing is drawn on the page. Useful for look-ahead computations of
|
205
|
+
# height, unprinted text, etc.
|
203
206
|
#
|
204
|
-
# Returns any text that did not print under the current settings
|
207
|
+
# Returns any text that did not print under the current settings.
|
205
208
|
#
|
206
209
|
def render(flags = {})
|
207
210
|
unprinted_text = []
|
@@ -217,11 +220,11 @@ module Prawn
|
|
217
220
|
shrink_to_fit(text) if @overflow == :shrink_to_fit
|
218
221
|
process_vertical_alignment(text)
|
219
222
|
@inked = true unless flags[:dry_run]
|
220
|
-
if @rotate != 0 && @inked
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
223
|
+
unprinted_text = if @rotate != 0 && @inked
|
224
|
+
render_rotated(text)
|
225
|
+
else
|
226
|
+
wrap(text)
|
227
|
+
end
|
225
228
|
@inked = false
|
226
229
|
end
|
227
230
|
end
|
@@ -229,7 +232,7 @@ module Prawn
|
|
229
232
|
end
|
230
233
|
|
231
234
|
unprinted_text.map do |e|
|
232
|
-
e.merge(:
|
235
|
+
e.merge(text: @document.font.to_utf8(e[:text]))
|
233
236
|
end
|
234
237
|
end
|
235
238
|
|
@@ -248,8 +251,10 @@ module Prawn
|
|
248
251
|
|
249
252
|
# <tt>fragment</tt> is a Prawn::Text::Formatted::Fragment object
|
250
253
|
#
|
251
|
-
def draw_fragment(
|
252
|
-
|
254
|
+
def draw_fragment(
|
255
|
+
fragment, accumulated_width = 0, line_width = 0, word_spacing = 0
|
256
|
+
) #:nodoc:
|
257
|
+
case @align
|
253
258
|
when :left
|
254
259
|
x = @at[0]
|
255
260
|
when :center
|
@@ -257,11 +262,11 @@ module Prawn
|
|
257
262
|
when :right
|
258
263
|
x = @at[0] + @width - line_width
|
259
264
|
when :justify
|
260
|
-
if @direction == :ltr
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
+
x = if @direction == :ltr
|
266
|
+
@at[0]
|
267
|
+
else
|
268
|
+
@at[0] + @width - line_width
|
269
|
+
end
|
265
270
|
end
|
266
271
|
|
267
272
|
x += accumulated_width
|
@@ -276,15 +281,19 @@ module Prawn
|
|
276
281
|
if @inked
|
277
282
|
draw_fragment_underlays(fragment)
|
278
283
|
|
279
|
-
@document.word_spacing(word_spacing)
|
284
|
+
@document.word_spacing(word_spacing) do
|
280
285
|
if @draw_text_callback
|
281
|
-
@draw_text_callback.call(
|
282
|
-
|
286
|
+
@draw_text_callback.call(
|
287
|
+
fragment.text, at: [x, y],
|
288
|
+
kerning: @kerning
|
289
|
+
)
|
283
290
|
else
|
284
|
-
@document.draw_text!(
|
285
|
-
|
291
|
+
@document.draw_text!(
|
292
|
+
fragment.text, at: [x, y],
|
293
|
+
kerning: @kerning
|
294
|
+
)
|
286
295
|
end
|
287
|
-
|
296
|
+
end
|
288
297
|
|
289
298
|
draw_fragment_overlays(fragment)
|
290
299
|
end
|
@@ -407,10 +416,14 @@ module Prawn
|
|
407
416
|
|
408
417
|
@document.save_font do
|
409
418
|
hash[:text].each_char do |char|
|
410
|
-
font_glyph_pairs << [
|
411
|
-
|
412
|
-
|
413
|
-
|
419
|
+
font_glyph_pairs << [
|
420
|
+
find_font_for_this_glyph(
|
421
|
+
char,
|
422
|
+
fragment_font,
|
423
|
+
fallback_fonts.dup
|
424
|
+
),
|
425
|
+
char
|
426
|
+
]
|
414
427
|
end
|
415
428
|
end
|
416
429
|
|
@@ -426,7 +439,7 @@ module Prawn
|
|
426
439
|
|
427
440
|
def find_font_for_this_glyph(char, current_font, fallback_fonts)
|
428
441
|
@document.font(current_font)
|
429
|
-
if fallback_fonts.
|
442
|
+
if fallback_fonts.empty? || @document.font.glyph_present?(char)
|
430
443
|
current_font
|
431
444
|
else
|
432
445
|
find_font_for_this_glyph(char, fallback_fonts.shift, fallback_fonts)
|
@@ -439,7 +452,7 @@ module Prawn
|
|
439
452
|
current_font = nil
|
440
453
|
|
441
454
|
font_glyph_pairs.each do |font, char|
|
442
|
-
if font != current_font || fragments.count
|
455
|
+
if font != current_font || fragments.count.zero?
|
443
456
|
current_font = font
|
444
457
|
fragment = hash.dup
|
445
458
|
fragment[:text] = char
|
@@ -454,8 +467,8 @@ module Prawn
|
|
454
467
|
end
|
455
468
|
|
456
469
|
def move_baseline_down
|
457
|
-
if @baseline_y
|
458
|
-
@baseline_y
|
470
|
+
if @baseline_y.zero?
|
471
|
+
@baseline_y = -@ascender
|
459
472
|
else
|
460
473
|
@baseline_y -= (@line_height + @leading)
|
461
474
|
end
|
@@ -479,7 +492,10 @@ module Prawn
|
|
479
492
|
# we need to wait until render() is called so that the fonts are set
|
480
493
|
# up properly for wrapping. So guard with a boolean to ensure this is
|
481
494
|
# only run once.
|
482
|
-
|
495
|
+
if defined?(@vertical_alignment_processed) &&
|
496
|
+
@vertical_alignment_processed
|
497
|
+
return
|
498
|
+
end
|
483
499
|
@vertical_alignment_processed = true
|
484
500
|
|
485
501
|
return if @vertical_align == :top
|
@@ -523,7 +539,7 @@ module Prawn
|
|
523
539
|
# document.process_text_options sets the font
|
524
540
|
@document.process_text_options(@options)
|
525
541
|
@font_size = @options[:size]
|
526
|
-
@kerning
|
542
|
+
@kerning = @options[:kerning]
|
527
543
|
end
|
528
544
|
|
529
545
|
def render_rotated(text)
|
@@ -547,7 +563,7 @@ module Prawn
|
|
547
563
|
y = @at[1]
|
548
564
|
end
|
549
565
|
|
550
|
-
@document.rotate(@rotate, :
|
566
|
+
@document.rotate(@rotate, origin: [x, y]) do
|
551
567
|
unprinted_text = wrap(text)
|
552
568
|
end
|
553
569
|
unprinted_text
|
@@ -572,40 +588,48 @@ module Prawn
|
|
572
588
|
def draw_fragment_overlay_link(fragment)
|
573
589
|
return unless fragment.link
|
574
590
|
box = fragment.absolute_bounding_box
|
575
|
-
@document.link_annotation(
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
591
|
+
@document.link_annotation(
|
592
|
+
box,
|
593
|
+
Border: [0, 0, 0],
|
594
|
+
A: {
|
595
|
+
Type: :Action,
|
596
|
+
S: :URI,
|
597
|
+
URI: PDF::Core::LiteralString.new(fragment.link)
|
598
|
+
}
|
599
|
+
)
|
580
600
|
end
|
581
601
|
|
582
602
|
def draw_fragment_overlay_anchor(fragment)
|
583
603
|
return unless fragment.anchor
|
584
604
|
box = fragment.absolute_bounding_box
|
585
|
-
@document.link_annotation(
|
586
|
-
|
587
|
-
|
605
|
+
@document.link_annotation(
|
606
|
+
box,
|
607
|
+
Border: [0, 0, 0],
|
608
|
+
Dest: fragment.anchor
|
609
|
+
)
|
588
610
|
end
|
589
611
|
|
590
612
|
def draw_fragment_overlay_local(fragment)
|
591
613
|
return unless fragment.local
|
592
614
|
box = fragment.absolute_bounding_box
|
593
|
-
@document.link_annotation(
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
615
|
+
@document.link_annotation(
|
616
|
+
box,
|
617
|
+
Border: [0, 0, 0],
|
618
|
+
A: {
|
619
|
+
Type: :Action,
|
620
|
+
S: :Launch,
|
621
|
+
F: PDF::Core::LiteralString.new(fragment.local),
|
622
|
+
NewWindow: true
|
623
|
+
}
|
624
|
+
)
|
599
625
|
end
|
600
626
|
|
601
627
|
def draw_fragment_overlay_styles(fragment)
|
602
|
-
|
603
|
-
if underline
|
628
|
+
if fragment.styles.include?(:underline)
|
604
629
|
@document.stroke_line(fragment.underline_points)
|
605
630
|
end
|
606
631
|
|
607
|
-
|
608
|
-
if strikethrough
|
632
|
+
if fragment.styles.include?(:strikethrough)
|
609
633
|
@document.stroke_line(fragment.strikethrough_points)
|
610
634
|
end
|
611
635
|
end
|