Class: Fontisan::Variable::StaticFontBuilder

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

Overview

Builds static font instances from variable font data

This class takes a variable font and applied variation data, then constructs a complete static font by:

  1. Copying all non-variation tables unchanged
  2. Removing variation-specific tables (fvar, gvar, HVAR, etc.)
  3. Updating metric tables (hmtx, hhea) with varied values
  4. Updating head table's modified timestamp
  5. Writing the complete static font binary

The result is a valid static font at the specified instance point.

Examples:

Build static font

builder = StaticFontBuilder.new(font)
static_binary = builder.build(varied_metrics, font_metrics)

Constant Summary collapse

VARIATION_TABLES =

Tables to remove from static font (variation-specific)

%w[fvar avar gvar cvar HVAR VVAR MVAR STAT].freeze
TABLE_UPDATERS =

Per-tag table update dispatch. Each entry maps a tag to [method_name, context_key] for looking up the right args.

{
  "hmtx" => %i[update_hmtx_table varied_metrics],
  "hhea" => %i[update_hhea_table font_metrics],
  "OS/2" => %i[update_os2_table font_metrics],
  "head" => %i[update_head_table options],
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font) ⇒ StaticFontBuilder

Initialize the builder

Parameters:



30
31
32
33
# File 'lib/fontisan/variable/static_font_builder.rb', line 30

def initialize(font)
  @font = font
  @table_updater = TableUpdater.new
end

Instance Attribute Details

#table_updaterTableUpdater (readonly)

Returns Table updater instance.

Returns:



25
26
27
# File 'lib/fontisan/variable/static_font_builder.rb', line 25

def table_updater
  @table_updater
end

Instance Method Details

#build(varied_metrics = {}, font_metrics = {}, options = {}) ⇒ String

Build static font from varied data

Parameters:

  • varied_metrics (Hash<Integer, Hash>) (defaults to: {})

    Varied metrics by glyph ID { glyph_id => { advance_width: 500, lsb: 50 } }

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

    Varied font-level metrics { ascent: 2048, descent: -512, line_gap: 0 }

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

    Build options

Options Hash (options):

  • :update_modified (Boolean)

    Update head modified timestamp

Returns:

  • (String)

    Complete static font binary



44
45
46
47
48
49
50
51
52
53
# File 'lib/fontisan/variable/static_font_builder.rb', line 44

def build(varied_metrics = {}, font_metrics = {}, options = {})
  # Collect tables for static font
  tables = collect_tables(varied_metrics, font_metrics, options)

  # Detect sfnt version
  sfnt_version = detect_sfnt_version(tables)

  # Write font using FontWriter
  FontWriter.write_font(tables, sfnt_version: sfnt_version)
end

#build_to_file(output_path, varied_metrics = {}, font_metrics = {}, options = {}) ⇒ Integer

Build static font and write to file

Parameters:

  • output_path (String)

    Output file path

  • varied_metrics (Hash<Integer, Hash>) (defaults to: {})

    Varied metrics by glyph ID

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

    Varied font-level metrics

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

    Build options

Returns:

  • (Integer)

    Number of bytes written



62
63
64
65
66
# File 'lib/fontisan/variable/static_font_builder.rb', line 62

def build_to_file(output_path, varied_metrics = {}, font_metrics = {},
options = {})
  binary = build(varied_metrics, font_metrics, options)
  File.binwrite(output_path, binary)
end