Sha256: 1c5d8cd1764ee97fe2a7da66c8bbc0d8ce419a43299078eb4f4942471ed9f5a7

Contents?: true

Size: 1.98 KB

Versions: 16

Compression:

Stored size: 1.98 KB

Contents

# ## Text Layouter - Line Wrapping
#
# The [HexaPDF::Layout::TextLayouter] class can be used to easily lay out text,
# automatically wrapping it appropriately.
#
# Text is broken only at certain characters:
#
# * The most important break points are **spaces**.
#
# * Lines can be broken at **tabulators** which represent eight spaces.
#
# * **Newline characters** are respected when wrapping and introduce a line
#   break. They have to be removed beforehand if this is not wanted. All Unicode
#   newline separators are recognized.
#
# * **Hyphens** are used as break points, possibly breaking just after them.
#
# * In addition to hyphens, **soft-hyphens** can be used to indicate break
#   points. In contrast to hyphens, soft-hyphens won't be visible unless a line
#   is broken at its position.
#
# * **Zero-width spaces** can be used to indicate break points at any position.
#
# * **Non-breaking spaces** can be used to prohibit a break between two words.
#   It has the same appearance as a space in the PDF.
#
# This example shows all these specially handled characters in action, e.g. a
# hard line break after "Fly-fishing", soft-hyphen in "wandering", tabulator
# instead of space after "wandering", zero-width space in "fantastic" and
# non-breaking spaces in "1 0 1".
#
# Usage:
# : `ruby text_layout_line_wrapping.rb`
#

require 'hexapdf'

doc = HexaPDF::Document.new
canvas = doc.pages.add([0, 0, 180, 230]).canvas
canvas.font("Times", size: 10, variant: :bold)

text = "Hello! Fly-fishing\nand wand\u{00AD}ering\taround - fanta\u{200B}stic" \
  " 1\u{00A0}0\u{00A0}1"

x = 10
y = 220
frag = HexaPDF::Layout::TextFragment.create(text, font: doc.fonts.add("Times"))
layouter = HexaPDF::Layout::TextLayouter.new
[30, 60, 100, 160].each do |width|
  result = layouter.fit([frag], width, 400)
  result.draw(canvas, x, y)
  canvas.stroke_color(255, 0, 0).line_width(0.2)
  canvas.rectangle(x, y, width, -result.height).stroke
  y -= result.height + 5
end

doc.write("text_layouter_line_wrapping.pdf", optimize: true)

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
hexapdf-0.11.9 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.11.8 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.11.7 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.11.6 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.11.5 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.11.4 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.11.3 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.11.2 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.11.1 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.11.0 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.10.0 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.9.3 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.9.2 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.9.1 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.9.0 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.8.0 examples/011-text_layouter_line_wrapping.rb