Class: Fontisan::FontLoader

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

Overview

FontLoader provides unified font loading with automatic format detection.

This class is the primary entry point for loading fonts in Fontisan. It automatically detects the font format and returns the appropriate domain object (TrueTypeFont, OpenTypeFont, TrueTypeCollection, or OpenTypeCollection).

Examples:

Load any font type

font = FontLoader.load("font.ttf")  # => TrueTypeFont
font = FontLoader.load("font.otf")  # => OpenTypeFont
font = FontLoader.load("fonts.ttc") # => TrueTypeFont (first in collection)
font = FontLoader.load("fonts.ttc", font_index: 2) # => TrueTypeFont (third in collection)

Class Method Summary collapse

Class Method Details

.load(path, font_index: 0) ⇒ TrueTypeFont, OpenTypeFont

Load a font from file with automatic format detection

Parameters:

  • path (String)

    Path to the font file

  • font_index (Integer) (defaults to: 0)

    Index of font in collection (0-based, default: 0)

Returns:

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fontisan/font_loader.rb', line 31

def self.load(path, font_index: 0)
  raise Errno::ENOENT, "File not found: #{path}" unless File.exist?(path)

  File.open(path, "rb") do |io|
    signature = io.read(4)
    io.rewind

    case signature
    when Constants::TTC_TAG
      load_from_collection(io, path, font_index)
    when pack_uint32(Constants::SFNT_VERSION_TRUETYPE)
      TrueTypeFont.from_file(path)
    when "OTTO"
      OpenTypeFont.from_file(path)
    when "wOFF"
      raise UnsupportedFormatError,
            "Unsupported font format: WOFF. Fontisan currently supports TTF, OTF, TTC, and OTC files."
    when "wOF2"
      raise UnsupportedFormatError,
            "Unsupported font format: WOFF2. Fontisan currently supports TTF, OTF, TTC, and OTC files."
    else
      raise InvalidFontError,
            "Unknown font format. Expected TTF, OTF, TTC, or OTC file."
    end
  end
end