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:

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”.

ruby text_layout_line_wrapping.rb
Resulting PDF:


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

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