prawn 2.4.0 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/prawn/document/bounding_box.rb +213 -141
- data/lib/prawn/document/column_box.rb +61 -26
- data/lib/prawn/document/internals.rb +25 -16
- data/lib/prawn/document/span.rb +20 -18
- data/lib/prawn/document.rb +257 -171
- data/lib/prawn/encoding.rb +2 -5
- data/lib/prawn/errors.rb +23 -34
- data/lib/prawn/font.rb +248 -135
- data/lib/prawn/font_metric_cache.rb +11 -10
- data/lib/prawn/fonts/afm.rb +85 -45
- data/lib/prawn/fonts/dfont.rb +7 -1
- data/lib/prawn/fonts/otf.rb +4 -1
- data/lib/prawn/fonts/to_unicode_cmap.rb +151 -0
- data/lib/prawn/fonts/ttc.rb +7 -2
- data/lib/prawn/fonts/ttf.rb +305 -93
- data/lib/prawn/fonts.rb +14 -0
- data/lib/prawn/graphics/blend_mode.rb +25 -28
- data/lib/prawn/graphics/cap_style.rb +9 -12
- data/lib/prawn/graphics/color.rb +57 -34
- data/lib/prawn/graphics/dash.rb +45 -42
- data/lib/prawn/graphics/join_style.rb +17 -11
- data/lib/prawn/graphics/patterns.rb +190 -69
- data/lib/prawn/graphics/transformation.rb +48 -41
- data/lib/prawn/graphics/transparency.rb +16 -40
- data/lib/prawn/graphics.rb +363 -253
- data/lib/prawn/grid.rb +184 -57
- data/lib/prawn/image_handler.rb +27 -10
- data/lib/prawn/images/image.rb +8 -10
- data/lib/prawn/images/jpg.rb +42 -19
- data/lib/prawn/images/png.rb +92 -41
- data/lib/prawn/images.rb +44 -57
- data/lib/prawn/measurement_extensions.rb +39 -8
- data/lib/prawn/measurements.rb +60 -5
- data/lib/prawn/outline.rb +114 -108
- data/lib/prawn/repeater.rb +51 -35
- data/lib/prawn/security/arcfour.rb +4 -4
- data/lib/prawn/security.rb +75 -70
- data/lib/prawn/soft_mask.rb +42 -30
- data/lib/prawn/stamp.rb +38 -42
- data/lib/prawn/text/box.rb +146 -96
- data/lib/prawn/text/formatted/arranger.rb +87 -26
- data/lib/prawn/text/formatted/box.rb +221 -150
- data/lib/prawn/text/formatted/fragment.rb +130 -14
- data/lib/prawn/text/formatted/line_wrap.rb +33 -24
- data/lib/prawn/text/formatted/parser.rb +112 -72
- data/lib/prawn/text/formatted/wrap.rb +12 -17
- data/lib/prawn/text/formatted.rb +75 -0
- data/lib/prawn/text.rb +441 -196
- data/lib/prawn/transformation_stack.rb +29 -10
- data/lib/prawn/utilities.rb +13 -13
- data/lib/prawn/version.rb +2 -1
- data/lib/prawn/view.rb +68 -53
- data/lib/prawn.rb +23 -18
- data.tar.gz.sig +0 -0
- metadata +54 -177
- metadata.gz.sig +0 -0
- data/.yardopts +0 -10
- data/Gemfile +0 -5
- data/Rakefile +0 -25
- data/manual/absolute_position.pdf +0 -0
- data/manual/basic_concepts/adding_pages.rb +0 -26
- data/manual/basic_concepts/basic_concepts.rb +0 -43
- data/manual/basic_concepts/creation.rb +0 -38
- data/manual/basic_concepts/cursor.rb +0 -32
- data/manual/basic_concepts/measurement.rb +0 -24
- data/manual/basic_concepts/origin.rb +0 -37
- data/manual/basic_concepts/other_cursor_helpers.rb +0 -39
- data/manual/basic_concepts/view.rb +0 -48
- data/manual/bounding_box/bounding_box.rb +0 -41
- data/manual/bounding_box/bounds.rb +0 -48
- data/manual/bounding_box/canvas.rb +0 -23
- data/manual/bounding_box/creation.rb +0 -22
- data/manual/bounding_box/indentation.rb +0 -45
- data/manual/bounding_box/nesting.rb +0 -52
- data/manual/bounding_box/russian_boxes.rb +0 -40
- data/manual/bounding_box/stretchy.rb +0 -29
- data/manual/contents.rb +0 -35
- data/manual/cover.rb +0 -43
- data/manual/document_and_page_options/background.rb +0 -29
- data/manual/document_and_page_options/document_and_page_options.rb +0 -34
- data/manual/document_and_page_options/metadata.rb +0 -25
- data/manual/document_and_page_options/page_margins.rb +0 -36
- data/manual/document_and_page_options/page_size.rb +0 -34
- data/manual/document_and_page_options/print_scaling.rb +0 -23
- data/manual/example_helper.rb +0 -8
- data/manual/graphics/blend_mode.rb +0 -52
- data/manual/graphics/circle_and_ellipse.rb +0 -21
- data/manual/graphics/color.rb +0 -22
- data/manual/graphics/common_lines.rb +0 -29
- data/manual/graphics/fill_and_stroke.rb +0 -41
- data/manual/graphics/fill_rules.rb +0 -38
- data/manual/graphics/gradients.rb +0 -43
- data/manual/graphics/graphics.rb +0 -64
- data/manual/graphics/helper.rb +0 -34
- data/manual/graphics/line_width.rb +0 -36
- data/manual/graphics/lines_and_curves.rb +0 -40
- data/manual/graphics/polygon.rb +0 -27
- data/manual/graphics/rectangle.rb +0 -20
- data/manual/graphics/rotate.rb +0 -25
- data/manual/graphics/scale.rb +0 -42
- data/manual/graphics/soft_masks.rb +0 -44
- data/manual/graphics/stroke_cap.rb +0 -30
- data/manual/graphics/stroke_dash.rb +0 -47
- data/manual/graphics/stroke_join.rb +0 -29
- data/manual/graphics/translate.rb +0 -29
- data/manual/graphics/transparency.rb +0 -33
- data/manual/how_to_read_this_manual.rb +0 -39
- data/manual/images/absolute_position.rb +0 -22
- data/manual/images/fit.rb +0 -20
- data/manual/images/horizontal.rb +0 -24
- data/manual/images/images.rb +0 -41
- data/manual/images/plain_image.rb +0 -17
- data/manual/images/scale.rb +0 -21
- data/manual/images/vertical.rb +0 -30
- data/manual/images/width_and_height.rb +0 -24
- data/manual/layout/boxes.rb +0 -26
- data/manual/layout/content.rb +0 -24
- data/manual/layout/layout.rb +0 -27
- data/manual/layout/simple_grid.rb +0 -22
- data/manual/outline/add_subsection_to.rb +0 -60
- data/manual/outline/insert_section_after.rb +0 -46
- data/manual/outline/outline.rb +0 -33
- data/manual/outline/sections_and_pages.rb +0 -66
- data/manual/repeatable_content/alternate_page_numbering.rb +0 -36
- data/manual/repeatable_content/page_numbering.rb +0 -55
- data/manual/repeatable_content/repeatable_content.rb +0 -35
- data/manual/repeatable_content/repeater.rb +0 -54
- data/manual/repeatable_content/stamp.rb +0 -40
- data/manual/security/encryption.rb +0 -28
- data/manual/security/permissions.rb +0 -43
- data/manual/security/security.rb +0 -28
- data/manual/table.rb +0 -16
- data/manual/text/alignment.rb +0 -43
- data/manual/text/color.rb +0 -24
- data/manual/text/column_box.rb +0 -30
- data/manual/text/fallback_fonts.rb +0 -41
- data/manual/text/font.rb +0 -40
- data/manual/text/font_size.rb +0 -44
- data/manual/text/font_style.rb +0 -25
- data/manual/text/formatted_callbacks.rb +0 -70
- data/manual/text/formatted_text.rb +0 -61
- data/manual/text/free_flowing_text.rb +0 -50
- data/manual/text/inline.rb +0 -40
- data/manual/text/kerning_and_character_spacing.rb +0 -38
- data/manual/text/leading.rb +0 -24
- data/manual/text/line_wrapping.rb +0 -60
- data/manual/text/paragraph_indentation.rb +0 -31
- data/manual/text/positioned_text.rb +0 -37
- data/manual/text/registering_families.rb +0 -51
- data/manual/text/rendering_and_color.rb +0 -36
- data/manual/text/right_to_left_text.rb +0 -54
- data/manual/text/rotation.rb +0 -52
- data/manual/text/single_usage.rb +0 -36
- data/manual/text/text.rb +0 -75
- data/manual/text/text_box_excess.rb +0 -35
- data/manual/text/text_box_extensions.rb +0 -48
- data/manual/text/text_box_overflow.rb +0 -51
- data/manual/text/utf8.rb +0 -27
- data/manual/text/win_ansi_charset.rb +0 -62
- data/prawn.gemspec +0 -51
- data/spec/data/curves.pdf +0 -66
- data/spec/extensions/encoding_helpers.rb +0 -11
- data/spec/prawn/document/bounding_box_spec.rb +0 -550
- data/spec/prawn/document/column_box_spec.rb +0 -75
- data/spec/prawn/document/security_spec.rb +0 -176
- data/spec/prawn/document_annotations_spec.rb +0 -76
- data/spec/prawn/document_destinations_spec.rb +0 -15
- data/spec/prawn/document_grid_spec.rb +0 -99
- data/spec/prawn/document_reference_spec.rb +0 -27
- data/spec/prawn/document_span_spec.rb +0 -44
- data/spec/prawn/document_spec.rb +0 -805
- data/spec/prawn/font_metric_cache_spec.rb +0 -54
- data/spec/prawn/font_spec.rb +0 -544
- data/spec/prawn/graphics/blend_mode_spec.rb +0 -63
- data/spec/prawn/graphics/transparency_spec.rb +0 -81
- data/spec/prawn/graphics_spec.rb +0 -872
- data/spec/prawn/graphics_stroke_styles_spec.rb +0 -229
- data/spec/prawn/image_handler_spec.rb +0 -53
- data/spec/prawn/images/jpg_spec.rb +0 -20
- data/spec/prawn/images/png_spec.rb +0 -283
- data/spec/prawn/images_spec.rb +0 -229
- data/spec/prawn/measurements_extensions_spec.rb +0 -24
- data/spec/prawn/outline_spec.rb +0 -512
- data/spec/prawn/repeater_spec.rb +0 -166
- data/spec/prawn/soft_mask_spec.rb +0 -74
- data/spec/prawn/stamp_spec.rb +0 -173
- data/spec/prawn/text/box_spec.rb +0 -1110
- data/spec/prawn/text/formatted/arranger_spec.rb +0 -466
- data/spec/prawn/text/formatted/box_spec.rb +0 -849
- data/spec/prawn/text/formatted/fragment_spec.rb +0 -343
- data/spec/prawn/text/formatted/line_wrap_spec.rb +0 -495
- data/spec/prawn/text/formatted/parser_spec.rb +0 -697
- data/spec/prawn/text_draw_text_spec.rb +0 -150
- data/spec/prawn/text_rendering_mode_spec.rb +0 -48
- data/spec/prawn/text_spacing_spec.rb +0 -95
- data/spec/prawn/text_spec.rb +0 -603
- data/spec/prawn/text_with_inline_formatting_spec.rb +0 -35
- data/spec/prawn/transformation_stack_spec.rb +0 -66
- data/spec/prawn/view_spec.rb +0 -63
- data/spec/prawn_manual_spec.rb +0 -35
- data/spec/spec_helper.rb +0 -48
data/lib/prawn/text/box.rb
CHANGED
@@ -1,108 +1,85 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# text/rectangle.rb : Implements text boxes
|
4
|
-
#
|
5
|
-
# Copyright November 2009, Daniel Nelson. All Rights Reserved.
|
6
|
-
#
|
7
|
-
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
-
#
|
9
|
-
|
10
3
|
require_relative 'formatted/box'
|
11
4
|
|
12
5
|
module Prawn
|
13
|
-
module Text
|
6
|
+
module Text # rubocop: disable Style/Documentation
|
14
7
|
# @group Stable API
|
15
8
|
|
16
|
-
# Draws the requested text into a box.
|
17
|
-
#
|
18
|
-
#
|
9
|
+
# Draws the requested text into a box.
|
10
|
+
#
|
11
|
+
# When the text overflows the rectangle, you shrink to fit, or truncate the
|
12
|
+
# text. Text boxes are independent of the document y position.
|
19
13
|
#
|
20
|
-
#
|
14
|
+
# #### Encoding
|
21
15
|
#
|
22
|
-
# Note that strings passed to this function should be encoded as UTF-8.
|
23
|
-
#
|
24
|
-
#
|
16
|
+
# Note that strings passed to this function should be encoded as UTF-8. If
|
17
|
+
# you get unexpected characters appearing in your rendered document, check
|
18
|
+
# this.
|
25
19
|
#
|
26
|
-
# If the current font is a built-in one, although the string must be
|
27
|
-
#
|
28
|
-
# are allowed.
|
20
|
+
# If the current font is a built-in one, although the string must be encoded
|
21
|
+
# as UTF-8, only characters that are available in WinAnsi are allowed.
|
29
22
|
#
|
30
23
|
# If an empty box is rendered to your PDF instead of the character you
|
31
24
|
# wanted it usually means the current font doesn't include that character.
|
32
25
|
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
# or not to prevent mid-word breaks when text does not fit in box.
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
# <
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
78
|
-
#
|
79
|
-
#
|
80
|
-
#
|
81
|
-
#
|
82
|
-
#
|
83
|
-
#
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
# This controls the behavior when the amount of text
|
91
|
-
# exceeds the available space. [:truncate]
|
92
|
-
# <tt>:min_font_size</tt>::
|
93
|
-
# <tt>number</tt>. The minimum font size to use when :overflow is set to
|
94
|
-
# :shrink_to_fit (that is the font size will not be reduced to less than
|
95
|
-
# this value, even if it means that some text will be cut off). [5]
|
96
|
-
#
|
97
|
-
# == Returns
|
98
|
-
#
|
99
|
-
# Returns any text that did not print under the current settings.
|
100
|
-
#
|
101
|
-
# == Exceptions
|
102
|
-
#
|
103
|
-
# Raises <tt>Prawn::Errors::CannotFit</tt> if not wide enough to print
|
104
|
-
# any text
|
105
|
-
#
|
26
|
+
# @param string [String]
|
27
|
+
# @param options [Hash{Symbol => any}]
|
28
|
+
# @option options :kerning [Boolean] (value of document.default_kerning?)
|
29
|
+
# Whether or not to use kerning (if it is available with the current
|
30
|
+
# font).
|
31
|
+
# @option options :size [Number] (current font size)
|
32
|
+
# The font size to use.
|
33
|
+
# @option options :character_spacing [Number] (0)
|
34
|
+
# The amount of space to add to or remove from the default character
|
35
|
+
# spacing.
|
36
|
+
# @option options :disable_wrap_by_char [Boolean] (false)
|
37
|
+
# Whether or not to prevent mid-word breaks when text does not fit in box.
|
38
|
+
# @option options :mode [Symbol] (:fill)
|
39
|
+
# The text rendering mode. See documentation for
|
40
|
+
# {Prawn::Document#text_rendering_mode} for a list of valid options.
|
41
|
+
# @option option :style [Symbol] (current style)
|
42
|
+
# The style to use. The requested style must be part of the current font
|
43
|
+
# family.
|
44
|
+
# @option option :at [Array(Number, Number)] (bounds top left corner)
|
45
|
+
# The upper left corner of the box.
|
46
|
+
# @option options :width [Number] (bounds.right - at[0])
|
47
|
+
# The width of the box.
|
48
|
+
# @option options :height [Number] (default_height())
|
49
|
+
# The height of the box.
|
50
|
+
# @option options :direction [:ltr, :rtl] (value of document.text_direction)
|
51
|
+
# Direction of the text (left-to-right or right-to-left).
|
52
|
+
# @option options :fallback_fonts [Array<String>]
|
53
|
+
# An array of font names. Each name must be the name of an AFM font or the
|
54
|
+
# name that was used to register a family of external fonts (see
|
55
|
+
# {Prawn::Document#font_families}). If present, then each glyph will be
|
56
|
+
# rendered using the first font that includes the glyph, starting with the
|
57
|
+
# current font and then moving through `:fallback_fonts`.
|
58
|
+
# @option options :align [:left, :center, :right, :justify]
|
59
|
+
# (:left if direction is :ltr, :right if direction is :rtl)
|
60
|
+
# Alignment within the bounding box.
|
61
|
+
# @option options :valign [:top, :center, :bottom] (:top)
|
62
|
+
# Vertical alignment within the bounding box.
|
63
|
+
# @option options :rotate [Number]
|
64
|
+
# The angle to rotate the text.
|
65
|
+
# @option options :rotate_around
|
66
|
+
# [:center, :upper_left, :upper_right, :lower_right, :lower_left]
|
67
|
+
# (:upper_left)
|
68
|
+
# The point around which to rotate the text.
|
69
|
+
# @option options :leading [Number] (value of document.default_leading)
|
70
|
+
# Additional space between lines.
|
71
|
+
# @option options :single_line [Boolean] (false)
|
72
|
+
# If true, then only the first line will be drawn.
|
73
|
+
# @option options :overflow [:truncate, :shrink_to_fit, :expand] (:truncate)
|
74
|
+
# This controls the behavior when the amount of text exceeds the available
|
75
|
+
# space.
|
76
|
+
# @option options :min_font_size [Number] (5)
|
77
|
+
# The minimum font size to use when `:overflow` is set to `:shrink_to_fit`
|
78
|
+
# (that is the font size will not be reduced to less than this value, even
|
79
|
+
# if it means that some text will be cut off).
|
80
|
+
# @return [String] Any text that did not print under the current settings.
|
81
|
+
# @raise [Prawn::Errors::CannotFit]
|
82
|
+
# If not wide enough to print any text.
|
106
83
|
def text_box(string, options = {})
|
107
84
|
options = options.dup
|
108
85
|
options[:document] = self
|
@@ -122,17 +99,90 @@ module Prawn
|
|
122
99
|
|
123
100
|
# @group Experimental API
|
124
101
|
|
125
|
-
#
|
126
|
-
# method. However, using Text::Box.new in conjunction with
|
127
|
-
# #render(:dry_run=> true) enables one to do look-ahead calculations prior
|
128
|
-
# to placing text on the page, or to determine how much vertical space was
|
129
|
-
# consumed by the printed text
|
102
|
+
# Text box.
|
130
103
|
#
|
104
|
+
# Generally, one would use the {Prawn::Text#text_box} convenience method.
|
105
|
+
# However, using {Prawn::Text::Box#initialize Box.new} in conjunction with
|
106
|
+
# `render(dry_run: true)` enables one to do calculations prior to placing
|
107
|
+
# text on the page, or to determine how much vertical space was consumed by
|
108
|
+
# the printed text.
|
131
109
|
class Box < Prawn::Text::Formatted::Box
|
110
|
+
# @param string [String]
|
111
|
+
# @param options [Hash{Symbol => any}]
|
112
|
+
# @option options :document [Prawn::Document] Owning document.
|
113
|
+
# @option options :kerning [Boolean] (value of document.default_kerning?)
|
114
|
+
# Whether or not to use kerning (if it is available with the current
|
115
|
+
# font).
|
116
|
+
# @option options :size [Number] (current font size)
|
117
|
+
# The font size to use.
|
118
|
+
# @option options :character_spacing [Number] (0)
|
119
|
+
# The amount of space to add to or remove from the default character
|
120
|
+
# spacing.
|
121
|
+
# @option options :disable_wrap_by_char [Boolean] (false)
|
122
|
+
# Whether or not to prevent mid-word breaks when text does not fit in box.
|
123
|
+
# @option options :mode [Symbol] (:fill)
|
124
|
+
# The text rendering mode. See documentation for
|
125
|
+
# {Prawn::Document#text_rendering_mode} for a list of valid options.
|
126
|
+
# @option option :style [Symbol] (current style)
|
127
|
+
# The style to use. The requested style must be part of the current font
|
128
|
+
# family.
|
129
|
+
# @option option :at [Array(Number, Number)] (bounds top left corner)
|
130
|
+
# The upper left corner of the box.
|
131
|
+
# @option options :width [Number] (bounds.right - at[0])
|
132
|
+
# The width of the box.
|
133
|
+
# @option options :height [Number] (default_height())
|
134
|
+
# The height of the box.
|
135
|
+
# @option options :direction [:ltr, :rtl] (value of document.text_direction)
|
136
|
+
# Direction of the text (left-to-right or right-to-left).
|
137
|
+
# @option options :fallback_fonts [Array<String>]
|
138
|
+
# An array of font names. Each name must be the name of an AFM font or the
|
139
|
+
# name that was used to register a family of external fonts (see
|
140
|
+
# {Prawn::Document#font_families}). If present, then each glyph will be
|
141
|
+
# rendered using the first font that includes the glyph, starting with the
|
142
|
+
# current font and then moving through `:fallback_fonts`.
|
143
|
+
# @option options :align [:left, :center, :right, :justify]
|
144
|
+
# (:left if direction is :ltr, :right if direction is :rtl)
|
145
|
+
# Alignment within the bounding box.
|
146
|
+
# @option options :valign [:top, :center, :bottom] (:top)
|
147
|
+
# Vertical alignment within the bounding box.
|
148
|
+
# @option options :rotate [Number]
|
149
|
+
# The angle to rotate the text.
|
150
|
+
# @option options :rotate_around
|
151
|
+
# [:center, :upper_left, :upper_right, :lower_right, :lower_left]
|
152
|
+
# (:upper_left)
|
153
|
+
# The point around which to rotate the text.
|
154
|
+
# @option options :leading [Number] (value of document.default_leading)
|
155
|
+
# Additional space between lines.
|
156
|
+
# @option options :single_line [Boolean] (false)
|
157
|
+
# If true, then only the first line will be drawn.
|
158
|
+
# @option options :overflow [:truncate, :shrink_to_fit, :expand] (:truncate)
|
159
|
+
# This controls the behavior when the amount of text exceeds the available
|
160
|
+
# space.
|
161
|
+
# @option options :min_font_size [Number] (5)
|
162
|
+
# The minimum font size to use when `:overflow` is set to `:shrink_to_fit`
|
163
|
+
# (that is the font size will not be reduced to less than this value, even
|
164
|
+
# if it means that some text will be cut off).
|
132
165
|
def initialize(string, options = {})
|
133
166
|
super([{ text: string }], options)
|
134
167
|
end
|
135
168
|
|
169
|
+
# Render text to the document based on the settings defined in
|
170
|
+
# constructor.
|
171
|
+
#
|
172
|
+
# In order to facilitate look-ahead calculations, this method accepts
|
173
|
+
# a `dry_run: true` option. If provided, then everything is executed as if
|
174
|
+
# rendering, with the exception that nothing is drawn on the page. Useful
|
175
|
+
# for look-ahead computations of height, unprinted text, etc.
|
176
|
+
#
|
177
|
+
# @param flags [Hash{Symbol => any}]
|
178
|
+
# @option flags :dry_run [Boolean] (false)
|
179
|
+
# Do not draw the text. Everything else is done.
|
180
|
+
# @return [String]
|
181
|
+
# Any text that did not print under the current settings.
|
182
|
+
# @raise [Prawn::Text::Formatted::Arranger::BadFontFamily]
|
183
|
+
# If no font family is defined for the current font.
|
184
|
+
# @raise [Prawn::Errors::CannotFit]
|
185
|
+
# If not wide enough to print any text.
|
136
186
|
def render(flags = {})
|
137
187
|
leftover = super(flags)
|
138
188
|
leftover.map { |hash| hash[:text] }.join
|
@@ -1,31 +1,30 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
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
3
|
module Prawn
|
11
4
|
module Text
|
12
|
-
module Formatted
|
5
|
+
module Formatted
|
6
|
+
# D data structure for 2-stage processing of lines of formatted text.
|
13
7
|
# @private
|
14
|
-
|
15
|
-
|
8
|
+
class Arranger
|
9
|
+
# You're getting this because you're trying to get some information from
|
10
|
+
# the arranger before it finished processing text.
|
16
11
|
class NotFinalized < StandardError
|
12
|
+
# @private
|
17
13
|
DEFAULT_MESSAGE = 'Lines must be finalized'
|
14
|
+
|
15
|
+
# @private
|
18
16
|
MESSAGE_WITH_METHOD = 'Lines must be finalized before calling #%<method>s'
|
19
17
|
|
20
18
|
def initialize(message = DEFAULT_MESSAGE, method: nil)
|
21
19
|
if method && message == DEFAULT_MESSAGE
|
22
|
-
super
|
20
|
+
super(format(MESSAGE_WITH_METHOD, method: method))
|
23
21
|
else
|
24
|
-
super
|
22
|
+
super(message)
|
25
23
|
end
|
26
24
|
end
|
27
25
|
end
|
28
26
|
|
27
|
+
# You're getting this because a font doesn't have a family name.
|
29
28
|
class BadFontFamily < StandardError
|
30
29
|
def initialize(message = 'Bad font family')
|
31
30
|
super
|
@@ -50,6 +49,10 @@ module Prawn
|
|
50
49
|
@kerning = options[:kerning]
|
51
50
|
end
|
52
51
|
|
52
|
+
# Number of spaces in the text.
|
53
|
+
#
|
54
|
+
# @return [Integer]
|
55
|
+
# @raise [NotFinalized]
|
53
56
|
def space_count
|
54
57
|
unless finalized
|
55
58
|
raise NotFinalized.new(method: 'space_count')
|
@@ -60,6 +63,10 @@ module Prawn
|
|
60
63
|
end
|
61
64
|
end
|
62
65
|
|
66
|
+
# Line width.
|
67
|
+
#
|
68
|
+
# @return [Number]
|
69
|
+
# @raise [NotFinalized]
|
63
70
|
def line_width
|
64
71
|
unless finalized
|
65
72
|
raise raise NotFinalized.new(method: 'line_width')
|
@@ -70,19 +77,27 @@ module Prawn
|
|
70
77
|
end
|
71
78
|
end
|
72
79
|
|
80
|
+
# Line text.
|
81
|
+
#
|
82
|
+
# @return [String]
|
83
|
+
# @raise [NotFinalized]
|
73
84
|
def line
|
74
85
|
unless finalized
|
75
86
|
raise NotFinalized.new(method: 'line')
|
76
87
|
end
|
77
88
|
|
78
|
-
@fragments.map
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
89
|
+
@fragments.map { |fragment|
|
90
|
+
begin
|
91
|
+
fragment.text.dup.encode(::Encoding::UTF_8)
|
92
|
+
rescue ::Encoding::InvalidByteSequenceError, ::Encoding::UndefinedConversionError
|
93
|
+
fragment.text.dup.force_encoding(::Encoding::UTF_8)
|
94
|
+
end
|
95
|
+
}.join
|
84
96
|
end
|
85
97
|
|
98
|
+
# Finish laying out current line.
|
99
|
+
#
|
100
|
+
# @return [void]
|
86
101
|
def finalize_line
|
87
102
|
@finalized = true
|
88
103
|
|
@@ -95,7 +110,7 @@ module Prawn
|
|
95
110
|
fragment = Prawn::Text::Formatted::Fragment.new(
|
96
111
|
text,
|
97
112
|
format_state,
|
98
|
-
@document
|
113
|
+
@document,
|
99
114
|
)
|
100
115
|
@fragments << fragment
|
101
116
|
self.fragment_measurements = fragment
|
@@ -103,6 +118,10 @@ module Prawn
|
|
103
118
|
end
|
104
119
|
end
|
105
120
|
|
121
|
+
# Set new fragment array.
|
122
|
+
#
|
123
|
+
# @param array [Array<Hash>]
|
124
|
+
# @return [void]
|
106
125
|
def format_array=(array)
|
107
126
|
initialize_line
|
108
127
|
@unconsumed = []
|
@@ -113,6 +132,9 @@ module Prawn
|
|
113
132
|
end
|
114
133
|
end
|
115
134
|
|
135
|
+
# Prepare for new line layout.
|
136
|
+
#
|
137
|
+
# @return [void]
|
116
138
|
def initialize_line
|
117
139
|
@finalized = false
|
118
140
|
@max_line_height = 0
|
@@ -123,10 +145,17 @@ module Prawn
|
|
123
145
|
@fragments = []
|
124
146
|
end
|
125
147
|
|
148
|
+
# Were all fragments processed?
|
149
|
+
#
|
150
|
+
# @return [Boolean]
|
126
151
|
def finished?
|
127
152
|
@unconsumed.empty?
|
128
153
|
end
|
129
154
|
|
155
|
+
# Get the next unprocessed string.
|
156
|
+
#
|
157
|
+
# @return [String, nil]
|
158
|
+
# @raise [NotFinalized]
|
130
159
|
def next_string
|
131
160
|
if finalized
|
132
161
|
raise NotFinalized.new(method: 'next_string')
|
@@ -143,6 +172,9 @@ module Prawn
|
|
143
172
|
end
|
144
173
|
end
|
145
174
|
|
175
|
+
# Get the next unprocessed string keeping it in the queue.
|
176
|
+
#
|
177
|
+
# @return [String, nil]
|
146
178
|
def preview_next_string
|
147
179
|
next_unconsumed_hash = @unconsumed.first
|
148
180
|
|
@@ -151,6 +183,11 @@ module Prawn
|
|
151
183
|
end
|
152
184
|
end
|
153
185
|
|
186
|
+
# Apply color and font settings.
|
187
|
+
#
|
188
|
+
# @param fragment [Prawn::Text::Formatted::Fragment]
|
189
|
+
# @yield
|
190
|
+
# @return [void]
|
154
191
|
def apply_color_and_font_settings(fragment, &block)
|
155
192
|
if fragment.color
|
156
193
|
original_fill_color = @document.fill_color
|
@@ -165,6 +202,11 @@ module Prawn
|
|
165
202
|
end
|
166
203
|
end
|
167
204
|
|
205
|
+
# Apply font settings.
|
206
|
+
#
|
207
|
+
# @param fragment [Prawn::Text::Formatted::Fragment]
|
208
|
+
# @yield
|
209
|
+
# @return [void]
|
168
210
|
def apply_font_settings(fragment = nil, &block)
|
169
211
|
if fragment.nil?
|
170
212
|
font = current_format_state[:font]
|
@@ -185,7 +227,7 @@ module Prawn
|
|
185
227
|
raise BadFontFamily unless @document.font.family
|
186
228
|
|
187
229
|
@document.font(
|
188
|
-
font || @document.font.family, style: font_style
|
230
|
+
font || @document.font.family, style: font_style,
|
189
231
|
) do
|
190
232
|
apply_font_size(size, styles, &block)
|
191
233
|
end
|
@@ -195,6 +237,12 @@ module Prawn
|
|
195
237
|
end
|
196
238
|
end
|
197
239
|
|
240
|
+
# Update last fragment's text.
|
241
|
+
#
|
242
|
+
# @param printed [String]
|
243
|
+
# @param unprinted [String]
|
244
|
+
# @param normalized_soft_hyphen [Boolean]
|
245
|
+
# @return [void]
|
198
246
|
def update_last_string(printed, unprinted, normalized_soft_hyphen = nil)
|
199
247
|
return if printed.nil?
|
200
248
|
|
@@ -214,6 +262,10 @@ module Prawn
|
|
214
262
|
load_previous_format_state if printed.empty?
|
215
263
|
end
|
216
264
|
|
265
|
+
# Get the next fragment.
|
266
|
+
#
|
267
|
+
# @return [Prawn::Text::Formatted::Fragment]
|
268
|
+
# @raise [NotFinalized]
|
217
269
|
def retrieve_fragment
|
218
270
|
unless finalized
|
219
271
|
raise NotFinalized, 'Lines must be finalized before fragments can be retrieved'
|
@@ -222,6 +274,9 @@ module Prawn
|
|
222
274
|
@fragments.shift
|
223
275
|
end
|
224
276
|
|
277
|
+
# Repack remaining fragments.
|
278
|
+
#
|
279
|
+
# @return [void]
|
225
280
|
def repack_unretrieved
|
226
281
|
new_unconsumed = []
|
227
282
|
# rubocop: disable Lint/AssignmentInCondition
|
@@ -233,6 +288,10 @@ module Prawn
|
|
233
288
|
@unconsumed = new_unconsumed.concat(@unconsumed)
|
234
289
|
end
|
235
290
|
|
291
|
+
# Get font variant from fragment styles.
|
292
|
+
#
|
293
|
+
# @param styles [Array<Symbol>]
|
294
|
+
# @return [Symbol]
|
236
295
|
def font_style(styles)
|
237
296
|
styles = Array(styles)
|
238
297
|
if styles.include?(:bold) && styles.include?(:italic)
|
@@ -277,13 +336,15 @@ module Prawn
|
|
277
336
|
|
278
337
|
def subscript?(styles)
|
279
338
|
if styles.nil? then false
|
280
|
-
else
|
339
|
+
else
|
340
|
+
styles.include?(:subscript)
|
281
341
|
end
|
282
342
|
end
|
283
343
|
|
284
344
|
def superscript?(styles)
|
285
345
|
if styles.nil? then false
|
286
|
-
else
|
346
|
+
else
|
347
|
+
styles.include?(:superscript)
|
287
348
|
end
|
288
349
|
end
|
289
350
|
|
@@ -307,7 +368,7 @@ module Prawn
|
|
307
368
|
apply_font_settings(fragment) do
|
308
369
|
fragment.width = @document.width_of(
|
309
370
|
fragment.text,
|
310
|
-
kerning: @kerning
|
371
|
+
kerning: @kerning,
|
311
372
|
)
|
312
373
|
fragment.line_height = @document.font.height
|
313
374
|
fragment.descender = @document.font.descender
|
@@ -318,15 +379,15 @@ module Prawn
|
|
318
379
|
def line_measurement_maximums=(fragment)
|
319
380
|
@max_line_height = [
|
320
381
|
defined?(@max_line_height) && @max_line_height,
|
321
|
-
fragment.line_height
|
382
|
+
fragment.line_height,
|
322
383
|
].compact.max
|
323
384
|
@max_descender = [
|
324
385
|
defined?(@max_descender) && @max_descender,
|
325
|
-
fragment.descender
|
386
|
+
fragment.descender,
|
326
387
|
].compact.max
|
327
388
|
@max_ascender = [
|
328
389
|
defined?(@max_ascender) && @max_ascender,
|
329
|
-
fragment.ascender
|
390
|
+
fragment.ascender,
|
330
391
|
].compact.max
|
331
392
|
end
|
332
393
|
end
|