Class: Fontisan::Svg::FontFaceGenerator

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

Overview

Generates SVG font-face element with font metadata

FontFaceGenerator extracts font metadata from font tables and formats it as SVG font-face attributes. This includes font family, style, weight, units-per-em, ascent, descent, and other font-level metrics.

Responsibilities:

  • Extract font metadata from name, head, hhea, OS/2 tables
  • Format metadata as SVG font-face attributes
  • Handle missing or invalid metadata gracefully
  • Provide sensible defaults

This class separates metadata extraction from XML generation, following separation of concerns principle.

Examples:

Generate font-face attributes

generator = FontFaceGenerator.new(font)
attributes = generator.generate_attributes
# => { font_family: "Arial", units_per_em: 1000, ... }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font) ⇒ FontFaceGenerator

Initialize generator with font

Parameters:

Raises:

  • (ArgumentError)

    If font is nil or invalid



33
34
35
36
37
38
39
40
41
# File 'lib/fontisan/svg/font_face_generator.rb', line 33

def initialize(font)
  raise ArgumentError, "Font cannot be nil" if font.nil?

  unless font.respond_to?(:table)
    raise ArgumentError, "Font must respond to :table method"
  end

  @font = font
end

Instance Attribute Details

#fontTrueTypeFont, OpenTypeFont (readonly)

Returns Font instance.

Returns:



27
28
29
# File 'lib/fontisan/svg/font_face_generator.rb', line 27

def font
  @font
end

Instance Method Details

#generate_attributesHash<Symbol, Object>

Generate font-face attributes

Returns a hash of font-face attributes suitable for SVG rendering. All values are properly formatted for SVG.

Returns:

  • (Hash<Symbol, Object>)

    Font-face attributes



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fontisan/svg/font_face_generator.rb', line 49

def generate_attributes
  {
    font_family: extract_font_family,
    font_weight: extract_font_weight,
    font_style: extract_font_style,
    units_per_em: extract_units_per_em,
    ascent: extract_ascent,
    descent: extract_descent,
    x_height: extract_x_height,
    cap_height: extract_cap_height,
    bbox: extract_bbox,
    underline_position: extract_underline_position,
    underline_thickness: extract_underline_thickness,
  }
end

#generate_xml(indent: " ") ⇒ String

Generate font-face element as XML string

Parameters:

  • indent (String) (defaults to: " ")

    Indentation string (default: " ")

Returns:

  • (String)

    XML font-face element



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fontisan/svg/font_face_generator.rb', line 69

def generate_xml(indent: "    ")
  attrs = generate_attributes

  # Build attribute string
  attr_parts = []
  attr_parts << "font-family=\"#{escape_xml(attrs[:font_family])}\""
  attr_parts << "units-per-em=\"#{attrs[:units_per_em]}\""
  attr_parts << "ascent=\"#{attrs[:ascent]}\""
  attr_parts << "descent=\"#{attrs[:descent]}\""

  # Optional attributes
  attr_parts << "font-weight=\"#{attrs[:font_weight]}\"" if attrs[:font_weight]
  attr_parts << "font-style=\"#{attrs[:font_style]}\"" if attrs[:font_style]
  attr_parts << "x-height=\"#{attrs[:x_height]}\"" if attrs[:x_height]
  attr_parts << "cap-height=\"#{attrs[:cap_height]}\"" if attrs[:cap_height]
  attr_parts << "bbox=\"#{attrs[:bbox]}\"" if attrs[:bbox]
  attr_parts << "underline-position=\"#{attrs[:underline_position]}\"" if attrs[:underline_position]
  attr_parts << "underline-thickness=\"#{attrs[:underline_thickness]}\"" if attrs[:underline_thickness]

  "#{indent}<font-face #{attr_parts.join(' ')}/>"
end