Class: Fontisan::Svg::FontFaceGenerator
- Inherits:
-
Object
- Object
- Fontisan::Svg::FontFaceGenerator
- 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.
Instance Attribute Summary collapse
-
#font ⇒ TrueTypeFont, OpenTypeFont
readonly
Font instance.
Instance Method Summary collapse
-
#generate_attributes ⇒ Hash<Symbol, Object>
Generate font-face attributes.
-
#generate_xml(indent: " ") ⇒ String
Generate font-face element as XML string.
-
#initialize(font) ⇒ FontFaceGenerator
constructor
Initialize generator with font.
Constructor Details
#initialize(font) ⇒ FontFaceGenerator
Initialize generator with font
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
#font ⇒ TrueTypeFont, OpenTypeFont (readonly)
Returns Font instance.
27 28 29 |
# File 'lib/fontisan/svg/font_face_generator.rb', line 27 def font @font end |
Instance Method Details
#generate_attributes ⇒ Hash<Symbol, Object>
Generate font-face attributes
Returns a hash of font-face attributes suitable for SVG rendering. All values are properly formatted for SVG.
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
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 |