Class: HarfBuzz::DrawFuncs

Inherits:
Object
  • Object
show all
Defined in:
lib/harfbuzz/draw_funcs.rb

Overview

Wraps hb_draw_funcs_t — callbacks for glyph outline rendering

Examples:

Extracting an SVG path

draw = HarfBuzz::DrawFuncs.new
path = []
draw.on_move_to    { |x, y| path << "M#{x},#{y}" }
draw.on_line_to    { |x, y| path << "L#{x},#{y}" }
draw.on_cubic_to   { |c1x, c1y, c2x, c2y, x, y| path << "C#{c1x},#{c1y},#{c2x},#{c2y},#{x},#{y}" }
draw.on_close_path { path << "Z" }
draw.make_immutable!
font.draw_glyph(glyph_id, draw)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDrawFuncs

Returns a new instance of DrawFuncs.

Raises:



18
19
20
21
22
23
# File 'lib/harfbuzz/draw_funcs.rb', line 18

def initialize
  @ptr = C.hb_draw_funcs_create
  raise AllocationError, "Failed to create draw_funcs" if @ptr.null?

  HarfBuzz::DrawFuncs.define_finalizer(self, @ptr)
end

Instance Attribute Details

#ptrObject (readonly)

Returns the value of attribute ptr.



16
17
18
# File 'lib/harfbuzz/draw_funcs.rb', line 16

def ptr
  @ptr
end

Class Method Details

.define_finalizer(obj, ptr) ⇒ Object



107
108
109
110
# File 'lib/harfbuzz/draw_funcs.rb', line 107

def self.define_finalizer(obj, ptr)
  destroy = C.method(:hb_draw_funcs_destroy)
  ObjectSpace.define_finalizer(obj, proc { destroy.call(ptr) })
end

Instance Method Details

#immutable?Boolean

Returns true if immutable.

Returns:

  • (Boolean)

    true if immutable



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

def immutable?
  C.from_hb_bool(C.hb_draw_funcs_is_immutable(@ptr))
end

#inspectObject



103
104
105
# File 'lib/harfbuzz/draw_funcs.rb', line 103

def inspect
  "#<HarfBuzz::DrawFuncs immutable=#{immutable?}>"
end

#make_immutable!self

Makes immutable (call after setting all callbacks)

Returns:

  • (self)


32
33
34
35
# File 'lib/harfbuzz/draw_funcs.rb', line 32

def make_immutable!
  C.hb_draw_funcs_make_immutable(@ptr)
  self
end

#on_close_path { ... } ⇒ Object

Sets the close_path callback

Yields:

  • Called when a path is closed



93
94
95
96
97
98
99
100
101
# File 'lib/harfbuzz/draw_funcs.rb', line 93

def on_close_path(&block)
  @close_path_callback = block
  cb = FFI::Function.new(:void, [:pointer, :pointer, :pointer, :pointer]) do
    |_dfuncs, _draw_data, _st, _user_data|
    block.call
  end
  @close_path_ffi = cb
  C.hb_draw_funcs_set_close_path_func(@ptr, cb, nil, nil)
end

#on_cubic_to {|c1x, c1y, c2x, c2y, x, y| ... } ⇒ Object

Sets the cubic_to callback

Yields:

  • (c1x, c1y, c2x, c2y, x, y)

    Two control points and end point



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/harfbuzz/draw_funcs.rb', line 79

def on_cubic_to(&block)
  @cubic_to_callback = block
  cb = FFI::Function.new(:void,
    [:pointer, :pointer, :pointer,
     :float, :float, :float, :float, :float, :float, :pointer]) do
    |_dfuncs, _draw_data, _st, c1x, c1y, c2x, c2y, x, y, _user_data|
    block.call(c1x, c1y, c2x, c2y, x, y)
  end
  @cubic_to_ffi = cb
  C.hb_draw_funcs_set_cubic_to_func(@ptr, cb, nil, nil)
end

#on_line_to {|x, y| ... } ⇒ Object

Sets the line_to callback

Yields:

  • (x, y)

    Called for line-to operations



52
53
54
55
56
57
58
59
60
61
# File 'lib/harfbuzz/draw_funcs.rb', line 52

def on_line_to(&block)
  @line_to_callback = block
  cb = FFI::Function.new(:void,
    [:pointer, :pointer, :pointer, :float, :float, :pointer]) do
    |_dfuncs, _draw_data, _st, x, y, _user_data|
    block.call(x, y)
  end
  @line_to_ffi = cb
  C.hb_draw_funcs_set_line_to_func(@ptr, cb, nil, nil)
end

#on_move_to {|x, y| ... } ⇒ Object

Sets the move_to callback

Yields:

  • (x, y)

    Called for move-to operations



39
40
41
42
43
44
45
46
47
48
# File 'lib/harfbuzz/draw_funcs.rb', line 39

def on_move_to(&block)
  @move_to_callback = block
  cb = FFI::Function.new(:void,
    [:pointer, :pointer, :pointer, :float, :float, :pointer]) do
    |_dfuncs, _draw_data, _st, x, y, _user_data|
    block.call(x, y)
  end
  @move_to_ffi = cb
  C.hb_draw_funcs_set_move_to_func(@ptr, cb, nil, nil)
end

#on_quadratic_to {|cx, cy, x, y| ... } ⇒ Object

Sets the quadratic_to callback

Yields:

  • (cx, cy, x, y)

    Control point and end point



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/harfbuzz/draw_funcs.rb', line 65

def on_quadratic_to(&block)
  @quadratic_to_callback = block
  cb = FFI::Function.new(:void,
    [:pointer, :pointer, :pointer,
     :float, :float, :float, :float, :pointer]) do
    |_dfuncs, _draw_data, _st, cx, cy, x, y, _user_data|
    block.call(cx, cy, x, y)
  end
  @quadratic_to_ffi = cb
  C.hb_draw_funcs_set_quadratic_to_func(@ptr, cb, nil, nil)
end