Class: Pdfrb::Layout::TextLayouter

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/layout/text_layouter.rb

Overview

Breaks a string into Layout::Line instances that fit a given width. Uses real per-glyph widths from the font's AFM or hmtx table when available, falling back to a heuristic estimate.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(style) ⇒ TextLayouter

Returns a new instance of TextLayouter.



11
12
13
14
# File 'lib/pdfrb/layout/text_layouter.rb', line 11

def initialize(style)
  @style = style
  @metrics = lookup_metrics
end

Instance Attribute Details

#styleObject (readonly)

Returns the value of attribute style.



9
10
11
# File 'lib/pdfrb/layout/text_layouter.rb', line 9

def style
  @style
end

Instance Method Details

#layout(text, width) ⇒ Array<Line>

Returns lines, each fitting in width.

Parameters:

  • text (String)

    the text to lay out.

  • width (Float)

    target line width in PDF units.

Returns:

  • (Array<Line>)

    lines, each fitting in width.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pdfrb/layout/text_layouter.rb', line 19

def layout(text, width)
  reordered = reorder_for_layout(text.to_s)
  clusters = reordered.grapheme_clusters
  return [] if clusters.empty?

  lines = []
  current = []
  current_width = 0.0
  clusters.each do |cluster|
    cw = cluster_width(cluster)
    if current_width + cw > width && !current.empty?
      lines << build_line(current)
      current = []
      current_width = 0.0
    end
    current << cluster
    current_width += cw
  end
  lines << build_line(current) unless current.empty?
  lines
end