Class: Fontisan::Svg::FontGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/svg/font_generator.rb

Overview

Generates complete SVG font XML structure

FontGenerator orchestrates all SVG font generation components to produce a complete SVG font document. It coordinates FontFaceGenerator, GlyphGenerator, and ViewBoxCalculator to build valid SVG font XML.

Responsibilities:

  • Generate complete SVG font XML structure
  • Coordinate sub-generators (font-face, glyphs)
  • Create proper XML namespaces and structure
  • Handle font ID and default advance width
  • Format XML with proper indentation

This is the main orchestrator for SVG font generation, following the single responsibility principle by delegating specific tasks to specialized generators.

Examples:

Generate complete SVG font

generator = FontGenerator.new(font, glyph_data)
svg_xml = generator.generate
File.write("font.svg", svg_xml)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font, glyph_data, options = {}) ⇒ FontGenerator

Initialize generator

Parameters:

  • font (TrueTypeFont, OpenTypeFont)

    Font to generate SVG for

  • glyph_data (Hash)

    Glyph data map

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

    Generation options

Options Hash (options):

  • :pretty_print (Boolean)

    Pretty print XML (default: true)

  • :font_id (String)

    Font ID for SVG (default: from font name)

  • :default_advance (Integer)

    Default advance width (default: 500)

Raises:

  • (ArgumentError)

    If font or glyph_data is invalid



46
47
48
49
50
51
52
# File 'lib/fontisan/svg/font_generator.rb', line 46

def initialize(font, glyph_data, options = {})
  validate_parameters!(font, glyph_data)

  @font = font
  @glyph_data = glyph_data
  @options = default_options.merge(options)
end

Instance Attribute Details

#fontTrueTypeFont, OpenTypeFont (readonly)

Returns Font instance.

Returns:



29
30
31
# File 'lib/fontisan/svg/font_generator.rb', line 29

def font
  @font
end

#glyph_dataHash (readonly)

Returns Glyph data map (glyph_id => codepoints, name, advance).

Returns:

  • (Hash)

    Glyph data map (glyph_id => codepoints, name, advance)



32
33
34
# File 'lib/fontisan/svg/font_generator.rb', line 32

def glyph_data
  @glyph_data
end

#optionsHash (readonly)

Returns Generation options.

Returns:

  • (Hash)

    Generation options



35
36
37
# File 'lib/fontisan/svg/font_generator.rb', line 35

def options
  @options
end

Instance Method Details

#generateString

Generate complete SVG font XML

Creates a complete SVG document with embedded font definition. The structure follows SVG 1.1 font specification.

Returns:

  • (String)

    Complete SVG font XML



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fontisan/svg/font_generator.rb', line 60

def generate
  parts = []
  parts << xml_declaration
  parts << svg_opening_tag
  parts << "  <defs>"
  parts << generate_font_element
  parts << "  </defs>"
  parts << svg_closing_tag

  parts.join("\n")
end