Class: Skia::Font

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

Instance Attribute Summary

Attributes inherited from Base

#ptr

Instance Method Summary collapse

Methods inherited from Base

#null?, release_callback, #to_ptr

Constructor Details

#initialize(typeface = nil, size = 12.0, scale_x = 1.0, skew_x = 0.0) ⇒ Font

Returns a new instance of Font.

Raises:



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/skia/font.rb', line 5

def initialize(typeface = nil, size = 12.0, scale_x = 1.0, skew_x = 0.0)
  ptr = if typeface
          Native.sk_font_new_with_values(typeface.ptr, size.to_f, scale_x.to_f, skew_x.to_f)
        else
          Native.sk_font_new
        end
  raise Error, 'Failed to create font' if ptr.nil? || ptr.null?

  super(ptr, :sk_font_delete)

  # Set size explicitly when no typeface is provided (sk_font_new uses default size)
  self.size = size unless typeface
end

Instance Method Details

#count_glyphs(text, encoding: :utf8) ⇒ Object



51
52
53
54
# File 'lib/skia/font.rb', line 51

def count_glyphs(text, encoding: :utf8)
  text_bytes = encode_text(text, encoding)
  Native.sk_font_text_to_glyphs(@ptr, text_bytes, text_bytes.bytesize, encoding, nil, 0)
end

#glyph_x_positions(glyphs, origin: 0.0) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/skia/font.rb', line 66

def glyph_x_positions(glyphs, origin: 0.0)
  return [] if glyphs.empty?

  glyphs_ptr = FFI::MemoryPointer.new(:uint16, glyphs.length)
  glyphs_ptr.write_array_of_uint16(glyphs)

  xpos_ptr = FFI::MemoryPointer.new(:float, glyphs.length)
  Native.sk_font_get_xpos(@ptr, glyphs_ptr, glyphs.length, xpos_ptr, origin.to_f)
  xpos_ptr.read_array_of_float(glyphs.length)
end

#measure_text(text, paint = nil) ⇒ Object



44
45
46
47
48
49
# File 'lib/skia/font.rb', line 44

def measure_text(text, paint = nil)
  text_bytes = text.encode('UTF-8')
  bounds = Native::SKRect.new
  width = Native.sk_font_measure_text(@ptr, text_bytes, text_bytes.bytesize, :utf8, bounds, paint&.ptr)
  [width, Rect.from_struct(bounds)]
end

#metricsObject



38
39
40
41
42
# File 'lib/skia/font.rb', line 38

def metrics
  metrics_struct = Native::SKFontMetrics.new
  Native.sk_font_get_metrics(@ptr, metrics_struct)
  FontMetrics.new(metrics_struct)
end

#sizeObject



30
31
32
# File 'lib/skia/font.rb', line 30

def size
  Native.sk_font_get_size(@ptr)
end

#size=(value) ⇒ Object



34
35
36
# File 'lib/skia/font.rb', line 34

def size=(value)
  Native.sk_font_set_size(@ptr, value.to_f)
end

#text_to_glyphs(text, encoding: :utf8) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/skia/font.rb', line 56

def text_to_glyphs(text, encoding: :utf8)
  text_bytes = encode_text(text, encoding)
  count = Native.sk_font_text_to_glyphs(@ptr, text_bytes, text_bytes.bytesize, encoding, nil, 0)
  return [] if count <= 0

  glyphs_ptr = FFI::MemoryPointer.new(:uint16, count)
  Native.sk_font_text_to_glyphs(@ptr, text_bytes, text_bytes.bytesize, encoding, glyphs_ptr, count)
  glyphs_ptr.read_array_of_uint16(count)
end

#typefaceObject



19
20
21
22
23
24
# File 'lib/skia/font.rb', line 19

def typeface
  ptr = Native.sk_font_get_typeface(@ptr)
  return nil if ptr.nil? || ptr.null?

  Typeface.new(ptr)
end

#typeface=(value) ⇒ Object



26
27
28
# File 'lib/skia/font.rb', line 26

def typeface=(value)
  Native.sk_font_set_typeface(@ptr, value&.ptr)
end