Class: Pdfrb::Layout::TextFragment

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

Overview

A single fragment of styled text — one run of consecutive glyphs with the same Style. Pieces are [[glyph_id, x_offset, width], ...] in font units.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pieces:, style:, width:, height:, y_min:, y_max:) ⇒ TextFragment

Returns a new instance of TextFragment.



11
12
13
14
15
16
17
18
# File 'lib/pdfrb/layout/text_fragment.rb', line 11

def initialize(pieces:, style:, width:, height:, y_min:, y_max:)
  @pieces = pieces
  @style = style
  @width = width
  @height = height
  @y_min = y_min
  @y_max = y_max
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



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

def height
  @height
end

#piecesObject (readonly)

Returns the value of attribute pieces.



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

def pieces
  @pieces
end

#styleObject (readonly)

Returns the value of attribute style.



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

def style
  @style
end

#widthObject (readonly)

Returns the value of attribute width.



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

def width
  @width
end

#y_maxObject (readonly)

Returns the value of attribute y_max.



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

def y_max
  @y_max
end

#y_minObject (readonly)

Returns the value of attribute y_min.



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

def y_min
  @y_min
end

Instance Method Details

#ascenderObject



20
21
22
# File 'lib/pdfrb/layout/text_fragment.rb', line 20

def ascender
  @y_max
end

#descenderObject



24
25
26
# File 'lib/pdfrb/layout/text_fragment.rb', line 24

def descender
  -@y_min
end

#draw(canvas, x, y) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/pdfrb/layout/text_fragment.rb', line 28

def draw(canvas, x, y)
  canvas.save_graphics_state do
    if @style.fill_color
      canvas.fill_color(@style.fill_color)
    end
    canvas.text(@pieces.map(&:first).pack("U*"), at: [x, y],
                                                 font: font_name_for_canvas, size: @style.font_size)
  end
end