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



38
39
40
41
42
43
44
# File 'lib/fontisan/variable/instancer.rb', line 38

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:



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

def delta_applicator
  @delta_applicator
end

#fontObject (readonly)

Returns The variable font object.

Returns:

  • (Object)

    The variable font object



26
27
28
# File 'lib/fontisan/variable/instancer.rb', line 26

def font
  @font
end

#static_font_builderStaticFontBuilder (readonly)

Returns Static font builder.

Returns:



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

def static_font_builder
  @static_font_builder
end

Instance Method Details

#axesHash

Get available axis information

Returns:

  • (Hash)

    Axis information



151
152
153
# File 'lib/fontisan/variable/instancer.rb', line 151

def axes
  @delta_applicator.axes
end

#axis_tagsArray<String>

Get available axis tags

Returns:

  • (Array<String>)

    Array of axis tags



158
159
160
# File 'lib/fontisan/variable/instancer.rb', line 158

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



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

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



96
97
98
99
# File 'lib/fontisan/variable/instancer.rb', line 96

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



110
111
112
113
# File 'lib/fontisan/variable/instancer.rb', line 110

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



82
83
84
85
# File 'lib/fontisan/variable/instancer.rb', line 82

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 } }, ...]



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

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



144
145
146
# File 'lib/fontisan/variable/instancer.rb', line 144

def variable_font?
  @delta_applicator.variable_font?
end