Class: HarfBuzz::Face

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

Overview

Wraps hb_face_t — a font face (font file + index)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blob, index = 0) ⇒ Face

Creates a Face from a Blob and face index

Parameters:

  • blob (Blob)

    Font data blob

  • index (Integer) (defaults to: 0)

    Face index within the blob (0 for single-face fonts)

Raises:



11
12
13
14
15
16
17
# File 'lib/harfbuzz/face.rb', line 11

def initialize(blob, index = 0)
  @blob = blob
  @ptr = C.hb_face_create(blob.ptr, index)
  raise AllocationError, "Failed to create face" if @ptr.null?

  register_finalizer
end

Instance Attribute Details

#ptrObject (readonly)

Returns the value of attribute ptr.



6
7
8
# File 'lib/harfbuzz/face.rb', line 6

def ptr
  @ptr
end

Class Method Details

.count(blob) ⇒ Integer

Returns the number of faces in a blob

Parameters:

  • blob (Blob)

    Font blob

Returns:

  • (Integer)

    Face count



48
49
50
# File 'lib/harfbuzz/face.rb', line 48

def self.count(blob)
  C.hb_face_count(blob.ptr)
end

.define_finalizer(obj, ptr) ⇒ Object



181
182
183
184
# File 'lib/harfbuzz/face.rb', line 181

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

.emptyFace

Returns the singleton empty face

Returns:



41
42
43
# File 'lib/harfbuzz/face.rb', line 41

def self.empty
  wrap_borrowed(C.hb_face_get_empty)
end

.for_tables {|tag| ... } ⇒ Face

Creates a Face with custom table access (callback-based)

Yields:

  • (tag)

    Called with the hb_tag_t for each table requested

Yield Returns:

  • (Blob)

    Table blob

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/harfbuzz/face.rb', line 23

def self.for_tables(&block)
  callback_holder = []
  cb = FFI::Function.new(:pointer, [:pointer, :uint32, :pointer]) do |_face, tag, _user_data|
    blob = block.call(tag)
    blob ? blob.ptr : C.hb_blob_get_empty
  end
  callback_holder << cb

  ptr = C.hb_face_create_for_tables(cb, nil, nil)
  obj = allocate
  obj.instance_variable_set(:@ptr, ptr)
  obj.instance_variable_set(:@callback_holder, callback_holder)
  obj.send(:register_finalizer)
  obj
end

.wrap_borrowed(ptr) ⇒ Object



166
167
168
169
170
171
# File 'lib/harfbuzz/face.rb', line 166

def self.wrap_borrowed(ptr)
  obj = allocate
  obj.instance_variable_set(:@ptr, ptr)
  obj.instance_variable_set(:@borrowed, true)
  obj
end

.wrap_owned(ptr) ⇒ Object



159
160
161
162
163
164
# File 'lib/harfbuzz/face.rb', line 159

def self.wrap_owned(ptr)
  obj = allocate
  obj.instance_variable_set(:@ptr, ptr)
  obj.send(:register_finalizer)
  obj
end

Instance Method Details

#blobBlob

Returns the blob for the entire font face

Returns:

  • (Blob)

    Face blob (owned)



109
110
111
112
# File 'lib/harfbuzz/face.rb', line 109

def blob
  ptr = C.hb_face_reference_blob(@ptr)
  Blob.wrap_owned(ptr)
end

#glyph_countInteger

Returns Number of glyphs in this face.

Returns:

  • (Integer)

    Number of glyphs in this face



73
74
75
# File 'lib/harfbuzz/face.rb', line 73

def glyph_count
  C.hb_face_get_glyph_count(@ptr)
end

#glyph_count=(count) ⇒ Object

Parameters:

  • count (Integer)

    Glyph count override



78
79
80
# File 'lib/harfbuzz/face.rb', line 78

def glyph_count=(count)
  C.hb_face_set_glyph_count(@ptr, count)
end

#immutable?Boolean

Returns true if face is immutable.

Returns:

  • (Boolean)

    true if face is immutable



144
145
146
# File 'lib/harfbuzz/face.rb', line 144

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

#indexInteger

Returns Face index.

Returns:

  • (Integer)

    Face index



53
54
55
# File 'lib/harfbuzz/face.rb', line 53

def index
  C.hb_face_get_index(@ptr)
end

#index=(idx) ⇒ Object

Parameters:

  • idx (Integer)

    Face index



58
59
60
# File 'lib/harfbuzz/face.rb', line 58

def index=(idx)
  C.hb_face_set_index(@ptr, idx)
end

#inspectObject



155
156
157
# File 'lib/harfbuzz/face.rb', line 155

def inspect
  "#<HarfBuzz::Face index=#{index} upem=#{upem} glyph_count=#{glyph_count}>"
end

#make_immutable!self

Makes the face immutable (thread-safe to share after this)

Returns:

  • (self)


150
151
152
153
# File 'lib/harfbuzz/face.rb', line 150

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

#nominal_glyph_mappingMap

Returns Map from Unicode codepoints to nominal glyph IDs.

Returns:

  • (Map)

    Map from Unicode codepoints to nominal glyph IDs



122
123
124
125
126
# File 'lib/harfbuzz/face.rb', line 122

def nominal_glyph_mapping
  map = Map.new
  C.hb_face_collect_nominal_glyph_mapping(@ptr, map.ptr, FFI::Pointer::NULL)
  map
end

#table(tag) ⇒ Blob Also known as: reference_table

Returns the blob for a specific table

Parameters:

  • tag (Integer)

    OpenType table tag

Returns:

  • (Blob)

    Table blob (owned)



100
101
102
103
# File 'lib/harfbuzz/face.rb', line 100

def table(tag)
  ptr = C.hb_face_reference_table(@ptr, tag)
  Blob.wrap_owned(ptr)
end

#table_tagsArray<Integer>

Returns Array of table tags.

Returns:

  • (Array<Integer>)

    Array of table tags



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/harfbuzz/face.rb', line 83

def table_tags
  # First call: pass count=0 to get total number of tables (return value)
  count_ptr = FFI::MemoryPointer.new(:uint)
  count_ptr.write_uint(0)
  total = C.hb_face_get_table_tags(@ptr, 0, count_ptr, nil)
  return [] if total.zero?

  tags_ptr = FFI::MemoryPointer.new(:uint32, total)
  count_ptr.write_uint(total)
  C.hb_face_get_table_tags(@ptr, 0, count_ptr, tags_ptr)
  actual = count_ptr.read_uint
  tags_ptr.read_array_of_uint32(actual)
end

#unicodesSet

Returns Set of all Unicode codepoints in this face.

Returns:

  • (Set)

    Set of all Unicode codepoints in this face



115
116
117
118
119
# File 'lib/harfbuzz/face.rb', line 115

def unicodes
  set = Set.new
  C.hb_face_collect_unicodes(@ptr, set.ptr)
  set
end

#upemInteger

Returns Units per em.

Returns:

  • (Integer)

    Units per em



63
64
65
# File 'lib/harfbuzz/face.rb', line 63

def upem
  C.hb_face_get_upem(@ptr)
end

#upem=(u) ⇒ Object

Parameters:

  • u (Integer)

    Units per em



68
69
70
# File 'lib/harfbuzz/face.rb', line 68

def upem=(u)
  C.hb_face_set_upem(@ptr, u)
end

#variation_selectorsSet

Returns Set of variation selector codepoints.

Returns:

  • (Set)

    Set of variation selector codepoints



129
130
131
132
133
# File 'lib/harfbuzz/face.rb', line 129

def variation_selectors
  set = Set.new
  C.hb_face_collect_variation_selectors(@ptr, set.ptr)
  set
end

#variation_unicodes(selector) ⇒ Set

Returns Set of Unicode codepoints supported via the variation selector.

Parameters:

  • selector (Integer)

    Variation selector codepoint

Returns:

  • (Set)

    Set of Unicode codepoints supported via the variation selector



137
138
139
140
141
# File 'lib/harfbuzz/face.rb', line 137

def variation_unicodes(selector)
  set = Set.new
  C.hb_face_collect_variation_unicodes(@ptr, selector, set.ptr)
  set
end