Class: Fontisan::Variable::RegionMatcher

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

Overview

Calculates region scalars for variation regions

Given normalized coordinates and variation regions, computes scalar values (0.0 to 1.0) that determine how much each region contributes to the final delta values. The algorithm follows the OpenType specification for region matching.

A region is defined by start, peak, and end coordinates for each axis:

  • If coordinate is outside [start, end], scalar is 0.0
  • If coordinate is at peak, scalar contribution for that axis is 1.0
  • Otherwise, scalar is linearly interpolated
  • Final scalar is the product of all axis contributions

Examples:

Calculate region scalars

matcher = RegionMatcher.new(variation_region_list)
scalars = matcher.match({ "wght" => 0.5 })
# => [0.5, 1.0, 0.0, ...]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variation_region_list, axis_tags, config = {}) ⇒ RegionMatcher

Initialize the region matcher

Parameters:

  • variation_region_list (VariationCommon::VariationRegionList)

    Region list

  • axis_tags (Array<String>)

    Axis tags in order

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

    Optional configuration overrides



36
37
38
39
40
41
42
# File 'lib/fontisan/variable/region_matcher.rb', line 36

def initialize(variation_region_list, axis_tags, config = {})
  @variation_region_list = variation_region_list
  @axis_tags = axis_tags
  @config = load_config.merge(config)
  @regions = build_regions
  @scalar_cache = {} if @config.dig(:region_matching, :cache_scalars)
end

Instance Attribute Details

#configHash (readonly)

Returns Configuration settings.

Returns:

  • (Hash)

    Configuration settings



26
27
28
# File 'lib/fontisan/variable/region_matcher.rb', line 26

def config
  @config
end

#regionsArray<Array<Hash>> (readonly)

Returns Variation regions.

Returns:

  • (Array<Array<Hash>>)

    Variation regions



29
30
31
# File 'lib/fontisan/variable/region_matcher.rb', line 29

def regions
  @regions
end

Instance Method Details

#clear_cacheObject

Clear scalar cache



87
88
89
# File 'lib/fontisan/variable/region_matcher.rb', line 87

def clear_cache
  @scalar_cache&.clear
end

#match(normalized_coords) ⇒ Array<Float>

Calculate scalars for all regions

Parameters:

  • normalized_coords (Hash<String, Float>)

    Normalized coordinates

Returns:

  • (Array<Float>)

    Scalar for each region (0.0 to 1.0)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fontisan/variable/region_matcher.rb', line 48

def match(normalized_coords)
  # Check cache if enabled
  if @config.dig(:region_matching, :cache_scalars)
    cache_key = cache_key_for(normalized_coords)
    return @scalar_cache[cache_key] if @scalar_cache.key?(cache_key)
  end

  scalars = @regions.map do |region|
    calculate_region_scalar(region, normalized_coords)
  end

  # Cache result if enabled
  if @config.dig(:region_matching, :cache_scalars)
    @scalar_cache[cache_key_for(normalized_coords)] = scalars
  end

  scalars
end

#match_region(region_index, normalized_coords) ⇒ Float

Calculate scalar for a specific region

Parameters:

  • region_index (Integer)

    Region index

  • normalized_coords (Hash<String, Float>)

    Normalized coordinates

Returns:

  • (Float)

    Region scalar (0.0 to 1.0)



72
73
74
75
76
77
# File 'lib/fontisan/variable/region_matcher.rb', line 72

def match_region(region_index, normalized_coords)
  return 0.0 if region_index >= @regions.length

  region = @regions[region_index]
  calculate_region_scalar(region, normalized_coords)
end

#region_countInteger

Get number of regions

Returns:

  • (Integer)

    Region count



82
83
84
# File 'lib/fontisan/variable/region_matcher.rb', line 82

def region_count
  @regions.length
end