Class: HarfBuzz::ShapingResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/harfbuzz/shaping_result.rb

Overview

Value object returned by HarfBuzz.shape_text — provides convenient access to shaped glyphs, positions, and rendering helpers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer:, font:) ⇒ ShapingResult

Returns a new instance of ShapingResult.

Parameters:

  • buffer (Buffer)

    Shaped buffer

  • font (Font)

    Font used for shaping



13
14
15
16
17
18
# File 'lib/harfbuzz/shaping_result.rb', line 13

def initialize(buffer:, font:)
  @buffer = buffer
  @font = font
  @glyph_infos = buffer.glyph_infos
  @glyph_positions = buffer.glyph_positions
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



9
10
11
# File 'lib/harfbuzz/shaping_result.rb', line 9

def buffer
  @buffer
end

#fontObject (readonly)

Returns the value of attribute font.



9
10
11
# File 'lib/harfbuzz/shaping_result.rb', line 9

def font
  @font
end

Instance Method Details

#eachObject

Iterates over [GlyphInfo, GlyphPosition] pairs



31
32
33
# File 'lib/harfbuzz/shaping_result.rb', line 31

def each
  @glyph_infos.zip(@glyph_positions).each { |info, pos| yield info, pos }
end

#glyph_infosArray<GlyphInfo>

Returns Glyph info array.

Returns:



21
22
23
# File 'lib/harfbuzz/shaping_result.rb', line 21

def glyph_infos
  @glyph_infos
end

#glyph_positionsArray<GlyphPosition>

Returns Glyph position array.

Returns:



26
27
28
# File 'lib/harfbuzz/shaping_result.rb', line 26

def glyph_positions
  @glyph_positions
end

#inspectObject



67
68
69
# File 'lib/harfbuzz/shaping_result.rb', line 67

def inspect
  "#<HarfBuzz::ShapingResult length=#{length} total_advance=#{total_advance.inspect}>"
end

#lengthInteger Also known as: size

Returns Number of glyphs.

Returns:

  • (Integer)

    Number of glyphs



36
37
38
# File 'lib/harfbuzz/shaping_result.rb', line 36

def length
  @glyph_infos.length
end

#to_svg_pathString

Generates an SVG path string for all glyphs at their shaped positions

Returns:

  • (String)

    SVG path data



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/harfbuzz/shaping_result.rb', line 52

def to_svg_path
  paths = []
  cx = 0
  cy = 0
  each do |info, pos|
    glyph_path = extract_glyph_path(info.glyph_id)
    ox = cx + pos.x_offset
    oy = cy + pos.y_offset
    paths << translate_path(glyph_path, ox, oy) unless glyph_path.empty?
    cx += pos.x_advance
    cy += pos.y_advance
  end
  paths.join(" ")
end

#total_advanceArray<Integer>

Returns the total advance as [x_advance, y_advance]

Returns:

  • (Array<Integer>)


44
45
46
47
48
# File 'lib/harfbuzz/shaping_result.rb', line 44

def total_advance
  @glyph_positions.inject([0, 0]) do |(x, y), pos|
    [x + pos.x_advance, y + pos.y_advance]
  end
end