Class: Fontisan::Commands::VariableCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/fontisan/commands/variable_command.rb

Overview

Command to extract variable font information.

This command extracts variation axes and named instances from variable fonts using the fvar (Font Variations) table. It also provides detailed inspection capabilities through the Inspector class.

Examples:

Extract variable font information

command = VariableCommand.new("path/to/variable-font.ttf")
info = command.run
puts info.axes.first.tag

Inspect variable font structure

command = VariableCommand.new("path/to/variable-font.ttf")
command.inspect(format: "json")

Instance Method Summary collapse

Methods inherited from BaseCommand

#initialize

Constructor Details

This class inherits a constructor from Fontisan::Commands::BaseCommand

Instance Method Details

#inspect(options = {}) ⇒ String

Inspect variable font structure

Provides detailed analysis of variable font structure including axes, instances, regions, and statistics.

Parameters:

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

    Inspection options

Options Hash (options):

  • :format (String)

    Output format (“json” or “yaml”)

Returns:

  • (String)

    Formatted inspection output



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fontisan/commands/variable_command.rb', line 76

def inspect(options = {})
  inspector = Variation::Inspector.new(font)

  format = options[:format] || "json"

  case format.downcase
  when "yaml"
    inspector.export_yaml
  else
    inspector.export_json
  end
end

#runModels::VariableFontInfo

Extract variable font information from the fvar table.

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fontisan/commands/variable_command.rb', line 26

def run
  result = Models::VariableFontInfo.new

  # Check if font has fvar table
  unless font.has_table?(Constants::FVAR_TAG)
    result.is_variable = false
    result.axis_count = 0
    result.instance_count = 0
    result.axes = []
    result.instances = []
    return result
  end

  fvar_table = font.table(Constants::FVAR_TAG)
  name_table = font.table(Constants::NAME_TAG) if font.has_table?(Constants::NAME_TAG)

  result.is_variable = true
  result.axis_count = fvar_table.axis_count
  result.instance_count = fvar_table.instance_count

  # Extract axes information
  result.axes = fvar_table.axes.map do |axis|
    Models::AxisInfo.new(
      tag: axis.axis_tag,
      name: name_table&.english_name(axis.axis_name_id),
      min_value: axis.min_value,
      default_value: axis.default_value,
      max_value: axis.max_value,
    )
  end

  # Extract instances information
  result.instances = fvar_table.instances.map do |instance|
    Models::InstanceInfo.new(
      name: name_table&.english_name(instance[:name_id]),
      coordinates: instance[:coordinates],
    )
  end

  result
end