Class: Skia::TextBlob

Inherits:
Base
  • Object
show all
Defined in:
lib/skia/text_blob.rb

Instance Attribute Summary

Attributes inherited from Base

#ptr

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#null?, release_callback, #to_ptr

Constructor Details

#initialize(ptr) ⇒ TextBlob

Returns a new instance of TextBlob.



5
6
7
# File 'lib/skia/text_blob.rb', line 5

def initialize(ptr)
  super(ptr, :sk_textblob_unref)
end

Class Method Details

.from_text(text, font, x: 0.0, y: 0.0, bounds: nil, encoding: :utf8) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/skia/text_blob.rb', line 9

def self.from_text(text, font, x: 0.0, y: 0.0, bounds: nil, encoding: :utf8)
  glyphs = font.text_to_glyphs(text, encoding: encoding)
  return nil if glyphs.empty?

  builder = Native.sk_textblob_builder_new
  raise Error, 'Failed to create text blob builder' if builder.nil? || builder.null?

  begin
    run_buffer = Native::SKRunBuffer.new
    bounds_struct = bounds&.to_struct
    Native.sk_textblob_builder_alloc_run_pos_h(builder, font.ptr, glyphs.length, y.to_f, bounds_struct, run_buffer)

    glyphs_ptr = run_buffer[:glyphs]
    glyphs_ptr.write_array_of_uint16(glyphs)

    xpos = font.glyph_x_positions(glyphs, origin: x.to_f)
    xpos_ptr = run_buffer[:pos]
    xpos_ptr.write_array_of_float(xpos)

    ptr = Native.sk_textblob_builder_make(builder)
    return nil if ptr.nil? || ptr.null?

    new(ptr)
  ensure
    Native.sk_textblob_builder_delete(builder) if builder && !builder.null?
  end
end

Instance Method Details

#boundsObject



37
38
39
40
41
# File 'lib/skia/text_blob.rb', line 37

def bounds
  rect_struct = Native::SKRect.new
  Native.sk_textblob_get_bounds(@ptr, rect_struct)
  Rect.from_struct(rect_struct)
end

#unique_idObject



43
44
45
# File 'lib/skia/text_blob.rb', line 43

def unique_id
  Native.sk_textblob_get_unique_id(@ptr)
end