Class: Pdfrb::Layout::Line
- Inherits:
-
Object
- Object
- Pdfrb::Layout::Line
- 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
-
#fragments ⇒ Object
readonly
Returns the value of attribute fragments.
Instance Method Summary collapse
- #ascender ⇒ Object
- #descender ⇒ Object
-
#draw(canvas, x, y, available_width:, alignment: :left) ⇒ Object
Draw the line at baseline (x, y) applying the requested alignment within the available width.
- #empty? ⇒ Boolean
- #height ⇒ Object
-
#initialize(fragments: []) ⇒ Line
constructor
A new instance of Line.
- #width ⇒ Object
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
#fragments ⇒ Object (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
#ascender ⇒ Object
26 27 28 |
# File 'lib/pdfrb/layout/line.rb', line 26 def ascender @fragments.map(&:y_max).max || 0 end |
#descender ⇒ Object
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
34 35 36 |
# File 'lib/pdfrb/layout/line.rb', line 34 def empty? @fragments.empty? end |
#height ⇒ Object
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 |
#width ⇒ Object
14 15 16 |
# File 'lib/pdfrb/layout/line.rb', line 14 def width @fragments.sum(&:width) end |