Module: HarfBuzz::OT::Var

Defined in:
lib/harfbuzz/ot/var.rb

Overview

OpenType Variable Fonts API

Class Method Summary collapse

Class Method Details

.axis_count(face) ⇒ Integer

Returns Number of variation axes.

Parameters:

  • face (Face)

    Font face

Returns:

  • (Integer)

    Number of variation axes



17
18
19
# File 'lib/harfbuzz/ot/var.rb', line 17

def axis_count(face)
  C.hb_ot_var_get_axis_count(face.ptr)
end

.axis_infos(face) ⇒ Array<C::HbOtVarAxisInfoT>

Returns information about all variation axes

Parameters:

  • face (Face)

    Font face

Returns:



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/harfbuzz/ot/var.rb', line 24

def axis_infos(face)
  count = axis_count(face)
  return [] if count.zero?

  count_ptr = FFI::MemoryPointer.new(:uint)
  count_ptr.write_uint(count)
  infos_ptr = FFI::MemoryPointer.new(C::HbOtVarAxisInfoT, count)
  C.hb_ot_var_get_axis_infos(face.ptr, 0, count_ptr, infos_ptr)
  actual = count_ptr.read_uint
  actual.times.map { |i| C::HbOtVarAxisInfoT.new(infos_ptr + i * C::HbOtVarAxisInfoT.size) }
end

.find_axis_info(face, tag) ⇒ C::HbOtVarAxisInfoT?

Finds a specific axis by tag

Parameters:

  • face (Face)

    Font face

  • tag (Integer)

    Axis tag (e.g., HarfBuzz.tag("wght"))

Returns:



40
41
42
43
44
# File 'lib/harfbuzz/ot/var.rb', line 40

def find_axis_info(face, tag)
  info = C::HbOtVarAxisInfoT.new
  ok = C.hb_ot_var_find_axis_info(face.ptr, tag, info)
  ok.zero? ? nil : info
end

.has_data?(face) ⇒ Boolean

Returns true if the face has variation data.

Parameters:

  • face (Face)

    Font face

Returns:

  • (Boolean)

    true if the face has variation data



11
12
13
# File 'lib/harfbuzz/ot/var.rb', line 11

def has_data?(face)
  C.from_hb_bool(C.hb_ot_var_has_data(face.ptr))
end

.named_instance_count(face) ⇒ Integer

Returns Number of named instances.

Parameters:

  • face (Face)

    Font face

Returns:

  • (Integer)

    Number of named instances



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

def named_instance_count(face)
  C.hb_ot_var_get_named_instance_count(face.ptr)
end

.named_instance_design_coords(face, idx) ⇒ Array<Float>

Returns design coordinates for a named instance

Parameters:

  • face (Face)

    Font face

  • idx (Integer)

    Instance index

Returns:

  • (Array<Float>)

    Design coordinates



70
71
72
73
74
75
76
77
78
79
# File 'lib/harfbuzz/ot/var.rb', line 70

def named_instance_design_coords(face, idx)
  count = axis_count(face)
  return [] if count.zero?

  count_ptr = FFI::MemoryPointer.new(:uint)
  count_ptr.write_uint(count)
  coords_ptr = FFI::MemoryPointer.new(:float, count)
  C.hb_ot_var_named_instance_get_design_coords(face.ptr, idx, count_ptr, coords_ptr)
  coords_ptr.read_array_of_float(count_ptr.read_uint)
end

.named_instance_postscript_name_id(face, idx) ⇒ Integer

Returns Name ID for the PostScript name.

Parameters:

  • face (Face)

    Font face

  • idx (Integer)

    Instance index

Returns:

  • (Integer)

    Name ID for the PostScript name



62
63
64
# File 'lib/harfbuzz/ot/var.rb', line 62

def named_instance_postscript_name_id(face, idx)
  C.hb_ot_var_named_instance_get_postscript_name_id(face.ptr, idx)
end

.named_instance_subfamily_name_id(face, idx) ⇒ Integer

Returns Name ID for the subfamily name.

Parameters:

  • face (Face)

    Font face

  • idx (Integer)

    Instance index

Returns:

  • (Integer)

    Name ID for the subfamily name



55
56
57
# File 'lib/harfbuzz/ot/var.rb', line 55

def named_instance_subfamily_name_id(face, idx)
  C.hb_ot_var_named_instance_get_subfamily_name_id(face.ptr, idx)
end

.normalize_coords(face, coords) ⇒ Array<Integer>

Normalizes design-space coordinates to normalized space

Parameters:

  • face (Face)

    Font face

  • coords (Array<Float>)

    Design coordinates

Returns:

  • (Array<Integer>)

    Normalized coordinates



104
105
106
107
108
109
110
111
112
113
# File 'lib/harfbuzz/ot/var.rb', line 104

def normalize_coords(face, coords)
  count = axis_count(face)
  return [] if count.zero?

  in_ptr = FFI::MemoryPointer.new(:float, coords.size)
  in_ptr.put_array_of_float(0, coords)
  out_ptr = FFI::MemoryPointer.new(:int32, count)
  C.hb_ot_var_normalize_coords(face.ptr, coords.size, in_ptr, out_ptr)
  out_ptr.read_array_of_int32(count)
end

.normalize_variations(face, variations) ⇒ Array<Integer>

Normalizes variation axis values

Parameters:

  • face (Face)

    Font face

  • variations (Array<Variation>)

    Design-space variation values

Returns:

  • (Array<Integer>)

    Normalized coordinates



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/harfbuzz/ot/var.rb', line 85

def normalize_variations(face, variations)
  count = C.hb_ot_var_get_axis_count(face.ptr)
  return [] if count.zero?

  var_structs = variations.map(&:to_struct)
  var_ptr = FFI::MemoryPointer.new(C::HbVariationT, variations.size)
  var_structs.each_with_index do |s, i|
    var_ptr.put_bytes(i * C::HbVariationT.size,
                      s.to_ptr.read_bytes(C::HbVariationT.size))
  end
  coords_ptr = FFI::MemoryPointer.new(:int32, count)
  C.hb_ot_var_normalize_variations(face.ptr, var_ptr, variations.size, coords_ptr, count)
  coords_ptr.read_array_of_int32(count)
end