Class: Pdfrb::Layout::Line

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

Overview

A single line of text — an ordered list of TextFragment instances sharing a baseline. Knows its total width/height/ascent/descent.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fragments: []) ⇒ Line

Returns a new instance of Line.



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

def initialize(fragments: [])
  @fragments = fragments
end

Instance Attribute Details

#fragmentsObject (readonly)

Returns the value of attribute fragments.



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

def fragments
  @fragments
end

Instance Method Details

#ascenderObject



26
27
28
# File 'lib/pdfrb/layout/line.rb', line 26

def ascender
  @fragments.map(&:y_max).max || 0
end

#descenderObject



30
31
32
# File 'lib/pdfrb/layout/line.rb', line 30

def descender
  @fragments.map(&:y_min).min || 0
end

#draw(canvas, x, y, available_width:, alignment: :left) ⇒ Object

Draw the line at baseline (x, y) applying the requested alignment within the available width.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pdfrb/layout/line.rb', line 40

def draw(canvas, x, y, available_width:, alignment: :left)
  offset = if alignment == :center
             x + ((available_width - width) / 2)
           elsif alignment == :right
             x + available_width - width
           else
             x
           end
  @fragments.each do |frag|
    frag.draw(canvas, offset, y)
    offset += frag.width
  end
end

#empty?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/pdfrb/layout/line.rb', line 34

def empty?
  @fragments.empty?
end

#heightObject



18
19
20
21
22
23
24
# File 'lib/pdfrb/layout/line.rb', line 18

def height
  return 0 if @fragments.empty?

  max_top = @fragments.map(&:y_max).max
  min_bottom = @fragments.map(&:y_min).min
  max_top - min_bottom
end

#widthObject



14
15
16
# File 'lib/pdfrb/layout/line.rb', line 14

def width
  @fragments.sum(&:width)
end