Class: Fontisan::Converters::SvgGenerator

Inherits:
Object
  • Object
show all
Includes:
ConversionStrategy
Defined in:
lib/fontisan/converters/svg_generator.rb

Overview

SVG font generator conversion strategy

SvgGenerator implements the ConversionStrategy interface to convert TTF or OTF fonts to SVG font format for web use, inspection, or conversion purposes.

SVG font generation process:

  1. Extract font metadata from tables
  2. Extract glyph outlines using OutlineExtractor
  3. Get unicode mappings from cmap table
  4. Get advance widths from hmtx table
  5. Build glyph data map
  6. Generate complete SVG XML using FontGenerator

Note: SVG fonts are deprecated in favor of WOFF/WOFF2 but remain useful for fallback, conversion workflows, and font inspection.

Examples:

Convert TTF to SVG

generator = SvgGenerator.new
svg_xml = generator.convert(font)
File.write('font.svg', svg_xml[:svg_xml])

Instance Method Summary collapse

Methods included from ConversionStrategy

included, #supports?

Instance Method Details

#convert(font, options = {}) ⇒ Hash

Convert font to SVG format

Returns a hash with :svg_xml key containing complete SVG font XML. This follows the same pattern as Woff2Encoder.

Parameters:

Options Hash (options):

  • :pretty_print (Boolean)

    Pretty print XML (default: true)

  • :glyph_ids (Array<Integer>)

    Specific glyph IDs to include (default: all)

  • :max_glyphs (Integer)

    Maximum glyphs to include (default: all)

Returns:

  • (Hash)

    Hash with :svg_xml key containing SVG XML string

Raises:

  • (Error)

    If conversion fails



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fontisan/converters/svg_generator.rb', line 41

def convert(font, options = {})
  validate(font, :svg)

  # Extract glyph data
  glyph_data = extract_glyph_data(font, options)

  # Generate SVG XML
  generator = Svg::FontGenerator.new(font, glyph_data, options)
  svg_xml = generator.generate

  # Return in special format for ConvertCommand to handle
  { svg_xml: svg_xml }
end

#supported_conversionsArray<Array<Symbol>>

Get list of supported conversions

Returns:

  • (Array<Array<Symbol>>)

    Supported conversion pairs



58
59
60
61
62
63
# File 'lib/fontisan/converters/svg_generator.rb', line 58

def supported_conversions
  [
    %i[ttf svg],
    %i[otf svg],
  ]
end

#validate(font, target_format) ⇒ Boolean

Validate that conversion is possible

Parameters:

Returns:

  • (Boolean)

    True if valid

Raises:

  • (Error)

    If conversion is not possible



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fontisan/converters/svg_generator.rb', line 71

def validate(font, target_format)
  unless target_format == :svg
    raise Fontisan::Error,
          "SvgGenerator only supports conversion to svg, " \
          "got: #{target_format}"
  end

  # Verify font has required tables
  required_tables = %w[head hhea maxp cmap]
  required_tables.each do |tag|
    unless font.table(tag)
      raise Fontisan::Error,
            "Font is missing required table: #{tag}"
    end
  end

  # Verify font has either glyf or CFF table
  unless font.has_table?("glyf") || font.has_table?("CFF ") || font.has_table?("CFF2")
    raise Fontisan::Error,
          "Font must have either glyf or CFF/CFF2 table"
  end

  true
end