Class: Fontisan::Variation::CacheKeyBuilder

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

Overview

Builds cache keys for variation calculations

This class centralizes cache key generation with consistent formatting and efficient string construction. All variation caches should use this builder to ensure key compatibility.

Examples:

Building cache keys

builder = CacheKeyBuilder

# Scalars key
key = builder.scalars_key(coordinates, axes)

# Instance key
key = builder.instance_key(font_checksum, coordinates)

Class Method Summary collapse

Class Method Details

.blend_key(blend_index, scalars) ⇒ String

Build cache key for blend operations

Generates key for CFF2 blend operator results.

Examples:

key = CacheKeyBuilder.blend_key(0, [0.8, 0.5])
# => "blend:0:0.8,0.5"

Parameters:

  • blend_index (Integer)

    Blend operation index

  • scalars (Array<Float>)

    Variation scalars

Returns:

  • (String)

    Cache key



140
141
142
# File 'lib/fontisan/variation/cache_key_builder.rb', line 140

def blend_key(blend_index, scalars)
  "blend:#{blend_index}:#{scalars.join(',')}"
end

.custom_key(prefix, *components) ⇒ String

Build custom cache key

Generates key with custom prefix and components. Use for specialized caching needs.

Examples:

key = CacheKeyBuilder.custom_key("mydata", [font_id, value1, value2])
# => "mydata:font_123:100:200"

Parameters:

  • prefix (String)

    Key prefix

  • components (Array)

    Key components (will be joined with :)

Returns:

  • (String)

    Cache key



156
157
158
# File 'lib/fontisan/variation/cache_key_builder.rb', line 156

def custom_key(prefix, *components)
  "#{prefix}:#{components.join(':')}"
end

.glyph_deltas_key(glyph_id, coordinates) ⇒ String

Build cache key for glyph deltas

Generates key for cached glyph delta application results.

Examples:

key = CacheKeyBuilder.glyph_deltas_key(42, { "wght" => 700 })
# => "glyph:42:{\"wght\"=>700.0}"

Parameters:

  • glyph_id (Integer)

    Glyph ID

  • coordinates (Hash<String, Float>)

    Design space coordinates

Returns:

  • (String)

    Cache key



106
107
108
109
# File 'lib/fontisan/variation/cache_key_builder.rb', line 106

def glyph_deltas_key(glyph_id, coordinates)
  sorted_coords = coordinates.sort.to_h
  "glyph:#{glyph_id}:#{sorted_coords}"
end

.instance_key(font_checksum, coordinates) ⇒ String

Build cache key for font instance

Generates key for entire instance generation result. Coordinates are sorted to ensure consistency.

Examples:

key = CacheKeyBuilder.instance_key("font_123", { "wght" => 700 })
# => "instance:font_123:{\"wght\"=>700.0}"

Parameters:

  • font_checksum (String)

    Font identifier

  • coordinates (Hash<String, Float>)

    Instance coordinates

Returns:

  • (String)

    Cache key



71
72
73
74
# File 'lib/fontisan/variation/cache_key_builder.rb', line 71

def instance_key(font_checksum, coordinates)
  sorted_coords = coordinates.sort.to_h
  "instance:#{font_checksum}:#{sorted_coords}"
end

.interpolation_key(base_value, deltas, scalars) ⇒ String

Build cache key for interpolated value

Generates key based on base value, deltas, and scalars. Useful for caching individual interpolation results.

Examples:

key = CacheKeyBuilder.interpolation_key(100, [10, 5], [0.8, 0.5])
# => "interp:100:10,5:0.8,0.5"

Parameters:

  • base_value (Numeric)

    Base value

  • deltas (Array<Numeric>)

    Delta values

  • scalars (Array<Float>)

    Region scalars

Returns:

  • (String)

    Cache key



55
56
57
# File 'lib/fontisan/variation/cache_key_builder.rb', line 55

def interpolation_key(base_value, deltas, scalars)
  "interp:#{base_value}:#{deltas.join(',')}:#{scalars.join(',')}"
end

.metrics_deltas_key(metrics_type, glyph_id, coordinates) ⇒ String

Build cache key for metrics deltas

Generates key for cached metrics variation results.

Examples:

key = CacheKeyBuilder.metrics_deltas_key("HVAR", 42, coords)
# => "metrics:HVAR:42:{\"wght\"=>700.0}"

Parameters:

  • metrics_type (String)

    Metrics table tag (HVAR, VVAR, MVAR)

  • glyph_id (Integer, nil)

    Glyph ID (nil for font-wide metrics)

  • coordinates (Hash<String, Float>)

    Design space coordinates

Returns:

  • (String)

    Cache key



123
124
125
126
127
# File 'lib/fontisan/variation/cache_key_builder.rb', line 123

def metrics_deltas_key(metrics_type, glyph_id, coordinates)
  sorted_coords = coordinates.sort.to_h
  glyph_part = glyph_id ? ":#{glyph_id}" : ""
  "metrics:#{metrics_type}#{glyph_part}:#{sorted_coords}"
end

.region_matches_key(coordinates, regions) ⇒ String

Build cache key for region matches

Generates key based on coordinates and region hash. Region hash is used to quickly identify region set without serializing entire region data.

Examples:

key = CacheKeyBuilder.region_matches_key(coords, regions)
# => "regions:{\"wght\"=>700.0}:12345678"

Parameters:

  • coordinates (Hash<String, Float>)

    Design space coordinates

  • regions (Array<Hash>)

    Variation regions

Returns:

  • (String)

    Cache key



89
90
91
92
93
# File 'lib/fontisan/variation/cache_key_builder.rb', line 89

def region_matches_key(coordinates, regions)
  sorted_coords = coordinates.sort.to_h
  region_hash = regions.hash
  "regions:#{sorted_coords}:#{region_hash}"
end

.scalars_key(coordinates, axes) ⇒ String

Build cache key for scalars

Generates a deterministic key based on axis tags and coordinate values. Axes are sorted to ensure consistent keys regardless of hash order.

Examples:

key = CacheKeyBuilder.scalars_key(
  { "wght" => 700, "wdth" => 100 },
  axes
)
# => "scalars:wdth,wght:100.0,700.0"

Parameters:

  • coordinates (Hash<String, Float>)

    Design space coordinates

  • axes (Array<VariationAxisRecord>)

    Variation axes

Returns:

  • (String)

    Cache key



36
37
38
39
40
# File 'lib/fontisan/variation/cache_key_builder.rb', line 36

def scalars_key(coordinates, axes)
  axis_tags = axes.map(&:axis_tag).sort
  coord_values = axis_tags.map { |tag| coordinates[tag] || 0.0 }
  "scalars:#{axis_tags.join(',')}:#{coord_values.join(',')}"
end