Class: Fontisan::Variation::VariationPreserver

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

Overview

Preserves variation data when converting between compatible font formats

VariationPreserver copies variation tables from source to target font during format conversion. It handles:

  • Common variation tables (fvar, avar, STAT) - shared by all variable fonts
  • Format-specific tables (gvar for TTF, CFF2 for OTF)
  • Metrics variation tables (HVAR, VVAR, MVAR)
  • Table checksum updates
  • Validation of table consistency

Use Cases:

  1. Variable TTF → Variable WOFF: Preserve all gvar-based variation
  2. Variable OTF → Variable WOFF: Preserve all CFF2-based variation
  3. Variable TTF → Variable OTF: Copy common tables (fvar, avar, STAT) but variation data conversion handled by Converter
  4. Variable OTF → Variable TTF: Copy common tables (fvar, avar, STAT) but variation data conversion handled by Converter

Preserved Tables:

Common (all variable fonts):

  • fvar (Font Variations)
  • avar (Axis Variations, optional)
  • STAT (Style Attributes)

TrueType-specific:

  • gvar (Glyph Variations)
  • cvar (CVT Variations, optional)

CFF2-specific:

  • CFF2 (with blend operators)

Metrics (optional):

  • HVAR (Horizontal Metrics Variations)
  • VVAR (Vertical Metrics Variations)
  • MVAR (Metrics Variations)

Examples:

Preserve variation when converting TTF to WOFF

preserver = VariationPreserver.new(ttf_font, woff_tables)
preserved_tables = preserver.preserve

Preserve only common tables for outline conversion

preserver = VariationPreserver.new(ttf_font, otf_tables,
                                   preserve_format_specific: false)
preserved_tables = preserver.preserve

Constant Summary collapse

COMMON_TABLES =

Common variation tables present in all variable fonts

%w[fvar avar STAT].freeze
TRUETYPE_TABLES =

TrueType-specific variation tables

%w[gvar cvar].freeze
CFF2_TABLES =

CFF2-specific variation tables

%w[CFF2].freeze
METRICS_TABLES =

Metrics variation tables

%w[HVAR VVAR MVAR].freeze
ALL_VARIATION_TABLES =

All variation-related tables

(COMMON_TABLES + TRUETYPE_TABLES +
CFF2_TABLES + METRICS_TABLES).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_font, target_tables, options = {}) ⇒ VariationPreserver

Initialize preserver

Parameters:

  • source_font (TrueTypeFont, OpenTypeFont)

    Variable font

  • target_tables (Hash<String, String>)

    Target font tables

  • options (Hash) (defaults to: {})

    Preservation options

Options Hash (options):

  • :preserve_format_specific (Boolean)

    Preserve format- specific variation tables (default: true)

  • :preserve_metrics (Boolean)

    Preserve metrics variation tables (default: true)

  • :validate (Boolean)

    Validate table consistency (default: true)



99
100
101
102
103
104
105
106
# File 'lib/fontisan/variation/variation_preserver.rb', line 99

def initialize(source_font, target_tables, options = {})
  @source_font = source_font
  @target_tables = target_tables.dup
  @options = options

  validate_source!
  @context = VariationContext.new(source_font)
end

Instance Attribute Details

#optionsHash (readonly)

Returns Preservation options.

Returns:

  • (Hash)

    Preservation options



86
87
88
# File 'lib/fontisan/variation/variation_preserver.rb', line 86

def options
  @options
end

#source_fontObject (readonly)

Returns Source font.

Returns:

  • (Object)

    Source font



80
81
82
# File 'lib/fontisan/variation/variation_preserver.rb', line 80

def source_font
  @source_font
end

#target_tablesHash<String, String> (readonly)

Returns Target tables.

Returns:

  • (Hash<String, String>)

    Target tables



83
84
85
# File 'lib/fontisan/variation/variation_preserver.rb', line 83

def target_tables
  @target_tables
end

Class Method Details

.preserve(source_font, target_tables, options = {}) ⇒ Hash<String, String>

Preserve variation data from source to target

Parameters:

  • source_font (TrueTypeFont, OpenTypeFont)

    Variable font

  • target_tables (Hash<String, String>)

    Target font tables

  • options (Hash) (defaults to: {})

    Preservation options

Returns:

  • (Hash<String, String>)

    Tables with variation data preserved



75
76
77
# File 'lib/fontisan/variation/variation_preserver.rb', line 75

def self.preserve(source_font, target_tables, options = {})
  new(source_font, target_tables, options).preserve
end

Instance Method Details

#preserveHash<String, String>

Preserve variation tables

Returns:

  • (Hash<String, String>)

    Target tables with variation preserved



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fontisan/variation/variation_preserver.rb', line 111

def preserve
  # Copy common variation tables (fvar, avar, STAT)
  copy_common_tables

  # Copy format-specific variation tables if requested
  if preserve_format_specific?
    copy_format_specific_tables
  end

  # Copy metrics variation tables if requested
  copy_metrics_tables if preserve_metrics?

  # Validate consistency if requested
  validate_consistency if validate?

  @target_tables
end

#variable_font?Boolean

Check if source font is a variable font

Returns:

  • (Boolean)

    True if source has fvar table



132
133
134
# File 'lib/fontisan/variation/variation_preserver.rb', line 132

def variable_font?
  @context.variable_font?
end

#variation_typeSymbol?

Get variation type of source font

Returns:

  • (Symbol, nil)

    :truetype, :cff2, or nil



139
140
141
# File 'lib/fontisan/variation/variation_preserver.rb', line 139

def variation_type
  @context.variation_type
end