Class: Fontisan::OutlineExtractor

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

Overview

Extracts glyph outlines from font tables

[‘OutlineExtractor`](lib/fontisan/outline_extractor.rb) provides a unified interface for extracting glyph outline data from both TrueType (glyf table) and CFF (Compact Font Format) fonts. It uses a strategy pattern to handle the different outline formats transparently.

The extractor:

  • Automatically detects font format (TrueType vs CFF)

  • Handles simple glyphs (direct outline data)

  • Handles compound glyphs (recursively resolves components)

  • Returns standardized [‘GlyphOutline`](lib/fontisan/models/glyph_outline.rb) objects

This class is responsible for extraction only, not business logic or presentation. It’s designed to be composed with [‘GlyphAccessor`](lib/fontisan/glyph_accessor.rb) for higher-level operations.

Reference: [‘docs/GETTING_STARTED.md:125-172`](docs/GETTING_STARTED.md:125)

Examples:

Extracting a glyph outline

extractor = Fontisan::OutlineExtractor.new(font)
outline = extractor.extract(65)  # 'A' character

puts outline.contour_count
puts outline.to_svg_path

Using with GlyphAccessor

accessor = Fontisan::GlyphAccessor.new(font)
outline = accessor.outline_for_char('A')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font) ⇒ OutlineExtractor

Initialize a new outline extractor

Parameters:

Raises:

  • (ArgumentError)

    If font is nil or doesn’t have required tables



43
44
45
46
47
48
49
50
51
# File 'lib/fontisan/outline_extractor.rb', line 43

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:



37
38
39
# File 'lib/fontisan/outline_extractor.rb', line 37

def font
  @font
end

Instance Method Details

#extract(glyph_id) ⇒ Models::GlyphOutline?

Extract outline for a specific glyph

This method automatically detects the font format and delegates to the appropriate extraction strategy. For compound glyphs (TrueType only), it recursively resolves component outlines and combines them with proper transformations.

Examples:

Extract a simple glyph

outline = extractor.extract(65)
puts "Glyph has #{outline.contour_count} contours"

Handle empty glyphs (like space)

outline = extractor.extract(space_glyph_id)
# => nil (empty glyphs return nil)

Parameters:

  • glyph_id (Integer)

    The glyph index (0-based, 0 is .notdef)

Returns:

Raises:



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fontisan/outline_extractor.rb', line 73

def extract(glyph_id)
  validate_glyph_id!(glyph_id)

  if cff_font?
    extract_cff_outline(glyph_id)
  elsif truetype_font?
    extract_truetype_outline(glyph_id)
  else
    raise Fontisan::MissingTableError,
          "Font has neither glyf nor CFF table"
  end
end