Class: Harfbuzz::Font

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#define_finalizer, finalize

Constructor Details

#initialize(face, size = nil) ⇒ Font

Returns a new instance of Font.



139
140
141
142
143
144
145
# File 'lib/harfbuzz/font.rb', line 139

def initialize(face, size=nil)
  @hb_font = Harfbuzz.hb_font_create(face.hb_face)
  # Harfbuzz.hb_font_set_scale(@hb_font, face.upem, face.upem)
  Harfbuzz.hb_font_set_ptem(@hb_font, size) if size
  Harfbuzz.hb_ft_font_set_funcs(@hb_font)
  define_finalizer(:hb_font_destroy, @hb_font)
end

Instance Attribute Details

#hb_fontObject (readonly)

Returns the value of attribute hb_font.



137
138
139
# File 'lib/harfbuzz/font.rb', line 137

def hb_font
  @hb_font
end

Instance Method Details

#extentsObject



224
225
226
# File 'lib/harfbuzz/font.rb', line 224

def extents
  [h_extents, v_extents]
end

#extents_for_direction(direction) ⇒ Object



228
229
230
231
232
233
# File 'lib/harfbuzz/font.rb', line 228

def extents_for_direction(direction)
  extents = FontExtents.new
  func = "hb_font_get_#{direction}_extents".to_sym
  Harfbuzz.send(func, @hb_font, extents)
  extents
end

#glyph_advance_for_direction(glyph, direction) ⇒ Object



203
204
205
206
207
208
209
210
211
212
# File 'lib/harfbuzz/font.rb', line 203

def glyph_advance_for_direction(glyph, direction)
  x_ptr = FFI::MemoryPointer.new(:int32, 1)
  y_ptr = FFI::MemoryPointer.new(:int32, 1)
  Harfbuzz.hb_font_get_glyph_advance_for_direction(@hb_font, glyph, direction, x_ptr, y_ptr)
  if Harfbuzz.hb_direction_is_horizontal(direction)
    x_ptr.read_int32
  else
    y_ptr.read_int32
  end
end

#glyph_extents(codepoint) ⇒ Object



197
198
199
200
201
# File 'lib/harfbuzz/font.rb', line 197

def glyph_extents(codepoint)
  glyph_extents = GlyphExtents.new
  Harfbuzz.hb_font_get_glyph_extents(@hb_font, codepoint, glyph_extents)
  glyph_extents
end

#glyph_from_name(name) ⇒ Object



186
187
188
189
190
191
192
193
194
195
# File 'lib/harfbuzz/font.rb', line 186

def glyph_from_name(name)
  name_ptr = FFI::MemoryPointer.new(:char, name.bytesize)
  name_ptr.put_bytes(0, name)
  glyph_ptr = FFI::MemoryPointer.new(:uint, 1)
  if Harfbuzz.hb_font_get_glyph_from_name(@hb_font, name_ptr, name.length, glyph_ptr)
    glyph_ptr.read_uint
  else
    nil
  end
end

#glyph_name(glyph) ⇒ Object



177
178
179
180
181
182
183
184
# File 'lib/harfbuzz/font.rb', line 177

def glyph_name(glyph)
  string_ptr = FFI::MemoryPointer.new(:char, 20)
  if Harfbuzz.hb_font_get_glyph_name(@hb_font, glyph, string_ptr, 20)
    string_ptr.get_string(0, 20)
  else
    nil
  end
end

#glyph_to_string(glyph) ⇒ Object



171
172
173
174
175
# File 'lib/harfbuzz/font.rb', line 171

def glyph_to_string(glyph)
  string_ptr = FFI::MemoryPointer.new(:char, 20)
  Harfbuzz.hb_font_glyph_to_string(@hb_font, glyph, string_ptr, 20)
  string_ptr.get_string(0, 20)
end

#h_extentsObject



216
217
218
# File 'lib/harfbuzz/font.rb', line 216

def h_extents
  extents_for_direction(:h)
end

#ppemObject



157
158
159
160
161
162
163
164
165
# File 'lib/harfbuzz/font.rb', line 157

def ppem
  ppem_x_ptr = FFI::MemoryPointer.new(:uint, 1)
  ppem_y_ptr = FFI::MemoryPointer.new(:uint, 1)
  Harfbuzz.hb_font_get_ppem(@hb_font, ppem_x_ptr, ppem_y_ptr)
  [
    ppem_x_ptr.read_uint,
    ppem_y_ptr.read_uint,
  ]
end

#ptemObject



167
168
169
# File 'lib/harfbuzz/font.rb', line 167

def ptem
  Harfbuzz.hb_font_get_ptem(@hb_font)
end

#scaleObject



147
148
149
150
151
152
153
154
155
# File 'lib/harfbuzz/font.rb', line 147

def scale
  x_scale_ptr = FFI::MemoryPointer.new(:int, 1)
  y_scale_ptr = FFI::MemoryPointer.new(:int, 1)
  Harfbuzz.hb_font_get_scale(@hb_font, x_scale_ptr, y_scale_ptr)
  [
    x_scale_ptr.read_int,
    y_scale_ptr.read_int,
  ]
end

#v_extentsObject



220
221
222
# File 'lib/harfbuzz/font.rb', line 220

def v_extents
  extents_for_direction(:v)
end