Module: Fontisan::LoadingModes

Defined in:
lib/fontisan/loading_modes.rb

Overview

Loading modes module that defines which tables are loaded in each mode.

This module provides a MECE (Mutually Exclusive, Collectively Exhaustive) architecture for font loading modes. Each mode defines a specific set of tables to load, enabling efficient parsing for different use cases.

Examples:

Using metadata mode

mode = LoadingModes::METADATA
tables = LoadingModes.tables_for(mode)  # => ["name", "head", "hhea", "maxp", "OS/2", "post"]

Checking table availability

LoadingModes.table_allowed?(:metadata, "GSUB")  # => false
LoadingModes.table_allowed?(:full, "GSUB")      # => true

Constant Summary collapse

METADATA =

Metadata mode: loads only tables needed for font identification and metrics Equivalent to otfinfo functionality

:metadata
FULL =

Full mode: loads all tables in the font

:full
MODES =

Mode definitions with their respective table lists

{
  METADATA => {
    tables: %w[name head hhea maxp OS/2 post].freeze,
    description: "Metadata mode - loads only identification and metrics tables (otfinfo-equivalent)",
  }.freeze,
  FULL => {
    tables: :all,
    description: "Full mode - loads all tables in the font",
  }.freeze,
}.freeze
METADATA_TABLES_SET =

Pre-computed Set for O(1) lookup of metadata tables This constant avoids recreating the Set on every font load

MODES[METADATA][:tables].to_set.freeze

Class Method Summary collapse

Class Method Details

.all_modesArray<Symbol>

Get all available modes

Returns:

  • (Array<Symbol>)

    List of all mode symbols



97
98
99
# File 'lib/fontisan/loading_modes.rb', line 97

def self.all_modes
  MODES.keys
end

.default_lazy?(mode) ⇒ Boolean

Get the default lazy loading setting for a mode

Parameters:

  • mode (Symbol)

    The loading mode

Returns:

  • (Boolean)

    true if lazy loading is recommended for this mode

Raises:

  • (ArgumentError)

    if mode is invalid



79
80
81
82
# File 'lib/fontisan/loading_modes.rb', line 79

def self.default_lazy?(mode)
  validate_mode!(mode)
  true # Lazy loading is recommended for all modes
end

.description(mode) ⇒ String

Get mode description

Parameters:

  • mode (Symbol)

    The loading mode

Returns:

  • (String)

    Description of the mode

Raises:

  • (ArgumentError)

    if mode is invalid



89
90
91
92
# File 'lib/fontisan/loading_modes.rb', line 89

def self.description(mode)
  validate_mode!(mode)
  MODES[mode][:description]
end

.table_allowed?(mode, tag) ⇒ Boolean

Check if a table is allowed in a given mode

Parameters:

  • mode (Symbol)

    The loading mode (:metadata or :full)

  • tag (String)

    The table tag to check

Returns:

  • (Boolean)

    true if table is allowed in the mode

Raises:

  • (ArgumentError)

    if mode is invalid



57
58
59
60
61
62
63
64
# File 'lib/fontisan/loading_modes.rb', line 57

def self.table_allowed?(mode, tag)
  validate_mode!(mode)

  tables = MODES[mode][:tables]
  return true if tables == :all

  tables.include?(tag)
end

.tables_for(mode) ⇒ Array<String>, Symbol

Get the list of tables allowed for a given mode

Parameters:

  • mode (Symbol)

    The loading mode (:metadata or :full)

Returns:

  • (Array<String>, Symbol)

    Array of table tags or :all for full mode

Raises:

  • (ArgumentError)

    if mode is invalid



46
47
48
49
# File 'lib/fontisan/loading_modes.rb', line 46

def self.tables_for(mode)
  validate_mode!(mode)
  MODES[mode][:tables]
end

.valid_mode?(mode) ⇒ Boolean

Validate that a mode is valid

Parameters:

  • mode (Symbol)

    The mode to validate

Returns:

  • (Boolean)

    true if mode is valid



70
71
72
# File 'lib/fontisan/loading_modes.rb', line 70

def self.valid_mode?(mode)
  MODES.key?(mode)
end

.validate_mode!(mode) ⇒ void

This method returns an undefined value.

Validate mode and raise error if invalid

Parameters:

  • mode (Symbol)

    The mode to validate

Raises:

  • (ArgumentError)

    if mode is invalid



106
107
108
109
110
111
# File 'lib/fontisan/loading_modes.rb', line 106

def self.validate_mode!(mode)
  return if valid_mode?(mode)

  raise ArgumentError,
        "Invalid mode: #{mode.inspect}. Valid modes are: #{all_modes.map(&:inspect).join(', ')}"
end