Module: Prawn::Text
- Includes:
- PDF::Core::Text, Formatted
- Included in:
- Document
- Defined in:
- lib/prawn/text.rb,
lib/prawn/text/box.rb,
lib/prawn/text/formatted.rb,
lib/prawn/text/formatted/box.rb,
lib/prawn/text/formatted/wrap.rb,
lib/prawn/text/formatted/parser.rb,
lib/prawn/text/formatted/arranger.rb,
lib/prawn/text/formatted/fragment.rb,
lib/prawn/text/formatted/line_wrap.rb
Overview
rubocop: disable Style/Documentation
Defined Under Namespace
Modules: Formatted Classes: Box
Constant Summary collapse
- NBSP =
No-Break Space
"\u00A0"
- ZWSP =
Zero Width Space (indicate word boundaries without a space)
"\u200B"
- SHY =
Soft Hyphen (invisible, except when causing a line break)
"\u00AD"
Stable API collapse
-
#draw_text(text, options) ⇒ void
Draws text on the page, beginning at the point specified by the
:at
option the string is assumed to be pre-formatted to properly fit the page. -
#draw_text!(text, options) ⇒ void
Low level text placement method.
-
#formatted_text(array, options = {}) ⇒ void
Draws formatted text to the page.
-
#formatted_text_box(array, options = {}) ⇒ Array<Hash>
included
from Formatted
Draws the requested formatted text into a box.
-
#height_of(string, options = {}) ⇒ void
Gets height of text in PDF points.
-
#height_of_formatted(array, options = {}) ⇒ void
Gets height of formatted text in PDF points.
-
#text(string, options = {}) ⇒ void
Draws text on the page.
-
#text_box(string, options = {}) ⇒ String
Draws the requested text into a box.
Instance Method Details
#draw_text(text, options) ⇒ void
This method returns an undefined value.
Draws text on the page, beginning at the point specified by the :at
option the string is assumed to be pre-formatted to properly fit the page.
pdf.draw_text "Hello World", at: [100, 100]
pdf.draw_text "Goodbye World", at: [50,50], size: 16
If your font contains kerning pair data that Prawn can parse, the
text will be kerned by default. You can disable kerning by including
a false
:kerning
option. If you want to disable kerning on an
entire document, set default_kerning = false
for that document
Text Positioning Details
Prawn will position your text by the left-most edge of its baseline, and
flow along a single line. (This means that :align
will not work)
Rotation
Text can be rotated before it is placed on the canvas by specifying the
:rotate
option with a given angle. Rotation occurs counter-clockwise.
Encoding
Note that strings passed to this function should be encoded as UTF-8. If you get unexpected characters appearing in your rendered document, check this.
If the current font is a built-in one, although the string must be encoded as UTF-8, only characters that are available in WinAnsi are allowed.
If an empty box is rendered to your PDF instead of the character you wanted it usually means the current font doesn’t include that character.
Source Code
345 | def draw_text(text, options) |
346 | options = inspect_options_for_draw_text(options.dup) |
347 | |
348 | # dup because normalize_encoding changes the string
|
349 | text = text.to_s.dup |
350 | save_font do |
351 | process_text_options(options) |
352 | text = font.normalize_encoding(text) |
353 | font_size(options[:size]) { draw_text!(text, options) } |
354 | end
|
355 | end
|
#draw_text!(text, options) ⇒ void
This method returns an undefined value.
Low level text placement method.
All font and size alterations should already be set.
Source Code
375 | def draw_text!(text, options) |
376 | unless font.unicode? || font.class.hide_m17n_warning || text.ascii_only? |
377 | warn( |
378 | "PDF's built-in fonts have very limited support for " \ |
379 | "internationalized text.\nIf you need full UTF-8 support, " \ |
380 | "consider using an external font instead.\n\nTo disable this " \ |
381 | "warning, add the following line to your code:\n" \ |
382 | "Prawn::Fonts::AFM.hide_m17n_warning = true\n", |
383 | )
|
384 | |
385 | font.class.hide_m17n_warning = true |
386 | end
|
387 | |
388 | x, y = map_to_absolute(options[:at]) |
389 | add_text_content(text, x, y, options) |
390 | end
|
#formatted_text(array, options = {}) ⇒ void
This method returns an undefined value.
Draws formatted text to the page.
Formatted text is an array of hashes, where each hash defines text and format information.
Source Code
263 | def formatted_text(array, options = {}) |
264 | options = inspect_options_for_text(options.dup) |
265 | |
266 | color = options.delete(:color) |
267 | if color |
268 | array = |
269 | array.map { |fragment| |
270 | fragment[:color] ? fragment : fragment.merge(color: color) |
271 | }
|
272 | end
|
273 | |
274 | if @indent_paragraphs |
275 | text_formatter.array_paragraphs(array).each do |paragraph| |
276 | remaining_text = draw_indented_formatted_line(paragraph, options) |
277 | |
278 | if @no_text_printed && !@all_text_printed |
279 | @bounding_box.move_past_bottom |
280 | remaining_text = draw_indented_formatted_line(paragraph, options) |
281 | end
|
282 | |
283 | unless @all_text_printed |
284 | remaining_text = fill_formatted_text_box(remaining_text, options) |
285 | draw_remaining_formatted_text_on_new_pages(remaining_text, options) |
286 | end
|
287 | end
|
288 | else
|
289 | remaining_text = fill_formatted_text_box(array, options) |
290 | draw_remaining_formatted_text_on_new_pages(remaining_text, options) |
291 | end
|
292 | end
|
#formatted_text_box(array, options = {}) ⇒ Array<Hash> Originally defined in module Formatted
Draws the requested formatted text into a box.
When the text overflows the rectangle shrink to fit or truncate the text. Text boxes are independent of the document y position.
#height_of(string, options = {}) ⇒ void
This method takes the same options as #text, except
:indent_paragraphs
.
This method returns an undefined value.
Gets height of text in PDF points.
Source Code
480 | def height_of(string, options = {}) |
481 | height_of_formatted([{ text: string }], options) |
482 | end
|
#height_of_formatted(array, options = {}) ⇒ void
This method takes the same options as #text, except
:indent_paragraphs
.
This method returns an undefined value.
Gets height of formatted text in PDF points.
Source Code
575 | def height_of_formatted(array, options = {}) |
576 | if options[:indent_paragraphs] |
577 | raise NotImplementedError, |
578 | ':indent_paragraphs option not available with height_of'
|
579 | end
|
580 | process_final_gap_option(options) |
581 | box = Text::Formatted::Box.new( |
582 | array, |
583 | options.merge(height: 100_000_000, document: self), |
584 | )
|
585 | box.render(dry_run: true) |
586 | |
587 | height = box.height |
588 | height += box.line_gap + box.leading if @final_gap |
589 | height
|
590 | end
|
#text(string, options = {}) ⇒ void
This method returns an undefined value.
Draws text on the page.
If you want text to flow onto a new page or between columns, this is the method to use. If, instead, you want to place bounded text outside of the flow of a document (for captions, labels, charts, etc.), use Box or its convenience method #text_box.
Prawn attempts to wrap the text to fit within your current bounding box
(or margin_box
if no bounding box is being used). Text will flow onto
the next page when it reaches the bottom of the bounding box. Text wrap in
Prawn does not re-flow line breaks, so if you want fully automated text
wrapping, be sure to remove newlines before attempting to draw your
string.
Examples
pdf.text "Will be wrapped when it hits the edge of your bounding box"
pdf.text "This will be centered", align: :center
pdf.text "This will be right aligned", align: :right
pdf.text "This <i>includes <b>inline</b></i> <font size='24'>formatting</font>", inline_format: true
If your font contains kerning pair data that Prawn can parse, the text
will be kerned by default. You can disable kerning by including a false
:kerning
option. If you want to disable kerning on an entire document,
set default_kerning = false
for that document.
Text Positioning Details
The text is positioned at font.ascender
below the baseline, making it
easy to use this method within bounding boxes and spans.
Encoding
Note that strings passed to this function should be encoded as UTF-8. If you get unexpected characters appearing in your rendered document, check this.
If the current font is a built-in one, although the string must be encoded as UTF-8, only characters that are available in WinAnsi are allowed.
If an empty box is rendered to your PDF instead of the character you wanted it usually means the current font doesn’t include that character.
Source Code
151 | def text(string, options = {}) |
152 | return false if string.nil? |
153 | |
154 | # we modify the options. don't change the user's hash
|
155 | options = options.dup |
156 | |
157 | p = options[:inline_format] |
158 | if p |
159 | p = [] unless p.is_a?(Array) |
160 | options.delete(:inline_format) |
161 | array = text_formatter.format(string, *p) |
162 | else
|
163 | array = [{ text: string }] |
164 | end
|
165 | |
166 | formatted_text(array, options) |
167 | end
|
#text_box(string, options = {}) ⇒ String
Draws the requested text into a box.
When the text overflows the rectangle, you shrink to fit, or truncate the text. Text boxes are independent of the document y position.
Encoding
Note that strings passed to this function should be encoded as UTF-8. If you get unexpected characters appearing in your rendered document, check this.
If the current font is a built-in one, although the string must be encoded as UTF-8, only characters that are available in WinAnsi are allowed.
If an empty box is rendered to your PDF instead of the character you wanted it usually means the current font doesn’t include that character.
Source Code
83 | def text_box(string, options = {}) |
84 | options = options.dup |
85 | options[:document] = self |
86 | |
87 | box = |
88 | if options[:inline_format] |
89 | p = options.delete(:inline_format) |
90 | p = [] unless p.is_a?(Array) |
91 | array = text_formatter.format(string, *p) |
92 | Text::Formatted::Box.new(array, options) |
93 | else
|
94 | Text::Box.new(string, options) |
95 | end
|
96 | |
97 | box.render |
98 | end
|