Class: Fontisan::Variable::Instancer

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/variable/instancer.rb

Overview

Main entry point for variable font instancing

This class orchestrates the complete process of generating a static font instance from a variable font at specified coordinates:

  1. Validates the font is a variable font (has fvar table)

  2. Normalizes user coordinates using AxisNormalizer

  3. Calculates region scalars using RegionMatcher

  4. Applies deltas using DeltaApplicator

  5. Builds static font using StaticFontBuilder

Examples:

Generate instance at specific coordinates

instancer = Instancer.new(variable_font)
static_binary = instancer.instance({ "wght" => 700 })
File.binwrite("bold.ttf", static_binary)

Generate instance for named instance

instancer = Instancer.new(variable_font)
static_binary = instancer.instance_named("Bold")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font) ⇒ Instancer

Initialize the instancer

Parameters:

Raises:

  • (ArgumentError)

    If font is not a variable font



41
42
43
44
45
46
47
# File 'lib/fontisan/variable/instancer.rb', line 41

def initialize(font)
  @font = font
  validate_variable_font!

  @delta_applicator = DeltaApplicator.new(font)
  @static_font_builder = StaticFontBuilder.new(font)
end

Instance Attribute Details

#delta_applicatorDeltaApplicator (readonly)

Returns Delta applicator.

Returns:



32
33
34
# File 'lib/fontisan/variable/instancer.rb', line 32

def delta_applicator
  @delta_applicator
end

#fontObject (readonly)

Returns The variable font object.

Returns:

  • (Object)

    The variable font object



29
30
31
# File 'lib/fontisan/variable/instancer.rb', line 29

def font
  @font
end

#static_font_builderStaticFontBuilder (readonly)

Returns Static font builder.

Returns:



35
36
37
# File 'lib/fontisan/variable/instancer.rb', line 35

def static_font_builder
  @static_font_builder
end

Instance Method Details

#axesHash

Get available axis information

Returns:

  • (Hash)

    Axis information



154
155
156
# File 'lib/fontisan/variable/instancer.rb', line 154

def axes
  @delta_applicator.axes
end

#axis_tagsArray<String>

Get available axis tags

Returns:

  • (Array<String>)

    Array of axis tags



161
162
163
# File 'lib/fontisan/variable/instancer.rb', line 161

def axis_tags
  @delta_applicator.axis_tags
end

#instance(user_coords, options = {}) ⇒ String

Generate static font instance at specified coordinates

Examples:

binary = instancer.instance({ "wght" => 700, "wdth" => 100 })

Parameters:

  • user_coords (Hash<String, Numeric>)

    User coordinates { “wght” => 700, “wdth” => 100 }

  • options (Hash) (defaults to: {})

    Instance options

Options Hash (options):

  • :update_modified (Boolean)

    Update head modified timestamp

Returns:

  • (String)

    Complete static font binary



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fontisan/variable/instancer.rb', line 59

def instance(user_coords, options = {})
  # Apply deltas to get all varied data
  delta_result = @delta_applicator.apply(user_coords)

  # Collect varied metrics for all glyphs
  varied_metrics = collect_varied_glyph_metrics(
    delta_result[:normalized_coords],
    delta_result[:region_scalars],
  )

  # Extract font-level metrics
  font_metrics = delta_result[:font_metrics]

  # Build static font
  @static_font_builder.build(varied_metrics, font_metrics, options)
end

#instance_named(instance_name, options = {}) ⇒ String

Generate static font instance for a named instance

Examples:

binary = instancer.instance_named("Bold")

Parameters:

  • instance_name (String)

    Named instance name (from fvar)

  • options (Hash) (defaults to: {})

    Instance options

Returns:

  • (String)

    Complete static font binary

Raises:

  • (ArgumentError)

    If named instance not found



99
100
101
102
# File 'lib/fontisan/variable/instancer.rb', line 99

def instance_named(instance_name, options = {})
  coords = find_named_instance_coords(instance_name)
  instance(coords, options)
end

#instance_named_to_file(output_path, instance_name, options = {}) ⇒ Integer

Generate static font instance for a named instance and write to file

Examples:

instancer.instance_named_to_file("bold.ttf", "Bold")

Parameters:

  • output_path (String)

    Output file path

  • instance_name (String)

    Named instance name

  • options (Hash) (defaults to: {})

    Instance options

Returns:

  • (Integer)

    Number of bytes written



113
114
115
116
# File 'lib/fontisan/variable/instancer.rb', line 113

def instance_named_to_file(output_path, instance_name, options = {})
  binary = instance_named(instance_name, options)
  File.binwrite(output_path, binary)
end

#instance_to_file(output_path, user_coords, options = {}) ⇒ Integer

Generate static font instance and write to file

Examples:

instancer.instance_to_file("bold.ttf", { "wght" => 700 })

Parameters:

  • output_path (String)

    Output file path

  • user_coords (Hash<String, Numeric>)

    User coordinates

  • options (Hash) (defaults to: {})

    Instance options

Returns:

  • (Integer)

    Number of bytes written



85
86
87
88
# File 'lib/fontisan/variable/instancer.rb', line 85

def instance_to_file(output_path, user_coords, options = {})
  binary = instance(user_coords, options)
  File.binwrite(output_path, binary)
end

#named_instancesArray<Hash>

Get list of available named instances

Returns:

  • (Array<Hash>)

    Array of named instance information

    { name: “Bold”, coords: { “wght” => 700 } }, …


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/fontisan/variable/instancer.rb', line 122

def named_instances
  fvar = load_fvar_table
  return [] unless fvar

  # Get name table for instance names
  name_table = load_name_table

  fvar.instances.map do |instance|
    coords = extract_instance_coords(instance, fvar)
    name = if name_table
             get_instance_name(instance[:name_id],
                               name_table)
           end

    {
      name_id: instance[:name_id],
      name: name || "Instance #{instance[:name_id]}",
      coordinates: coords,
    }
  end
end

#variable_font?Boolean

Check if font is a variable font

Returns:

  • (Boolean)

    True if variable font



147
148
149
# File 'lib/fontisan/variable/instancer.rb', line 147

def variable_font?
  @delta_applicator.variable_font?
end