Class: Fontisan::Variation::VariableSvgGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/variation/variable_svg_generator.rb

Overview

Generates SVG fonts from variable fonts at specific coordinates

VariableSvgGenerator combines instance generation with SVG conversion to create static SVG fonts from variable fonts at any point in the design space.

Process:

  1. Accept variable font + axis coordinates
  2. Generate static instance using InstanceGenerator
  3. Build temporary font from instance tables
  4. Delegate to SvgGenerator for SVG creation
  5. Return SVG with variation metadata

This enables generating SVG fonts at specific weights, widths, or other variation axes without creating intermediate font files.

Examples:

Generate SVG at Bold weight

generator = VariableSvgGenerator.new(variable_font, { "wght" => 700.0 })
svg_result = generator.generate
File.write("bold.svg", svg_result[:svg_xml])

Generate SVG at specific width and weight

coords = { "wght" => 700.0, "wdth" => 75.0 }
generator = VariableSvgGenerator.new(variable_font, coords)
svg_result = generator.generate(pretty_print: true)

Defined Under Namespace

Classes: InstanceFontWrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font, coordinates = {}) ⇒ VariableSvgGenerator

Initialize generator

Parameters:

  • font (TrueTypeFont, OpenTypeFont)

    Variable font

  • coordinates (Hash<String, Float>) (defaults to: {})

    Design space coordinates

Raises:

  • (Error)

    If font is not a variable font



42
43
44
45
46
47
# File 'lib/fontisan/variation/variable_svg_generator.rb', line 42

def initialize(font, coordinates = {})
  @font = font
  @coordinates = coordinates || {}

  validate_variable_font!
end

Instance Attribute Details

#coordinatesHash<String, Float> (readonly)

Returns Design space coordinates.

Returns:

  • (Hash<String, Float>)

    Design space coordinates



35
36
37
# File 'lib/fontisan/variation/variable_svg_generator.rb', line 35

def coordinates
  @coordinates
end

#fontTrueTypeFont, OpenTypeFont (readonly)

Returns Variable font.

Returns:



32
33
34
# File 'lib/fontisan/variation/variable_svg_generator.rb', line 32

def font
  @font
end

Instance Method Details

#default_coordinatesHash<String, Float>

Get default coordinates for font

Returns all axes at their default values.

Returns:

  • (Hash<String, Float>)

    Default coordinates



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fontisan/variation/variable_svg_generator.rb', line 110

def default_coordinates
  return {} unless @font.has_table?("fvar")

  fvar = @font.table("fvar")
  return {} unless fvar

  coords = {}
  fvar.axes.each do |axis|
    coords[axis.axis_tag] = axis.default_value
  end
  coords
end

#generate(options = {}) ⇒ Hash

Generate SVG font at specified coordinates

Creates a static instance at the given coordinates and converts it to SVG format. Returns the same format as SvgGenerator for consistency.

Parameters:

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

    SVG generation options

Options Hash (options):

  • :pretty_print (Boolean)

    Pretty print XML (default: true)

  • :glyph_ids (Array<Integer>)

    Specific glyphs (default: all)

  • :max_glyphs (Integer)

    Maximum glyphs (default: all)

  • :font_id (String)

    Font ID for SVG

  • :default_advance (Integer)

    Default advance width

Returns:

  • (Hash)

    Hash with :svg_xml key containing SVG XML string

Raises:

  • (Error)

    If generation fails



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fontisan/variation/variable_svg_generator.rb', line 63

def generate(options = {})
  # Generate static instance tables
  instance_tables = generate_static_instance

  # Build temporary font from instance tables
  static_font = build_font_from_tables(instance_tables)

  # Generate SVG using standard generator
  svg_generator = Converters::SvgGenerator.new
  result = svg_generator.convert(static_font, options)

  # Add variation metadata to result
  result[:variation_metadata] = {
    coordinates: @coordinates,
    source_font: extract_font_name,
  }

  result
end

#generate_named_instance(instance_index, options = {}) ⇒ Hash

Generate SVG for a named instance

Parameters:

  • instance_index (Integer)

    Index of named instance in fvar

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

    SVG generation options

Returns:

  • (Hash)

    Hash with :svg_xml key



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fontisan/variation/variable_svg_generator.rb', line 88

def generate_named_instance(instance_index, options = {})
  instance_generator = InstanceGenerator.new(@font)
  instance_tables = instance_generator.generate_named_instance(instance_index)

  static_font = build_font_from_tables(instance_tables)
  svg_generator = Converters::SvgGenerator.new
  result = svg_generator.convert(static_font, options)

  # Add instance metadata
  result[:variation_metadata] = {
    instance_index: instance_index,
    source_font: extract_font_name,
  }

  result
end

#named_instancesArray<Hash>

Get list of named instances

Returns:

  • (Array<Hash>)

    Array of instance info



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fontisan/variation/variable_svg_generator.rb', line 126

def named_instances
  return [] unless @font.has_table?("fvar")

  fvar = @font.table("fvar")
  return [] unless fvar

  fvar.instances.map.with_index do |instance, index|
    {
      index: index,
      name: instance[:subfamily_name_id],
      coordinates: build_instance_coordinates(instance, fvar.axes),
    }
  end
end