Sha256: ad92b59dcca5615a48096bd8e50f562acccb2db56e2721cc1a5a2f8932d76807

Contents?: true

Size: 1.92 KB

Versions: 18

Compression:

Stored size: 1.92 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
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 = doc.layout.text_fragments(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("hp-blue-dark").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

18 entries across 18 versions & 1 rubygems

Version Path
hexapdf-1.0.2 examples/011-text_layouter_line_wrapping.rb
hexapdf-1.0.1 examples/011-text_layouter_line_wrapping.rb
hexapdf-1.0.0 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.47.0 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.46.0 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.45.0 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.44.0 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.41.0 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.40.0 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.39.1 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.39.0 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.38.0 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.37.2 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.37.1 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.37.0 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.36.0 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.35.1 examples/011-text_layouter_line_wrapping.rb
hexapdf-0.35.0 examples/011-text_layouter_line_wrapping.rb