Class: Fontisan::Variable::RegionMatcher
- Inherits:
-
Object
- Object
- Fontisan::Variable::RegionMatcher
- 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
Instance Attribute Summary collapse
-
#config ⇒ Hash
readonly
Configuration settings.
-
#regions ⇒ Array<Array<Hash>>
readonly
Variation regions.
Instance Method Summary collapse
-
#clear_cache ⇒ Object
Clear scalar cache.
-
#initialize(variation_region_list, axis_tags, config = {}) ⇒ RegionMatcher
constructor
Initialize the region matcher.
-
#match(normalized_coords) ⇒ Array<Float>
Calculate scalars for all regions.
-
#match_region(region_index, normalized_coords) ⇒ Float
Calculate scalar for a specific region.
-
#region_count ⇒ Integer
Get number of regions.
Constructor Details
#initialize(variation_region_list, axis_tags, config = {}) ⇒ RegionMatcher
Initialize the region matcher
36 37 38 39 40 41 42 |
# File 'lib/fontisan/variable/region_matcher.rb', line 36 def initialize(variation_region_list, , config = {}) @variation_region_list = variation_region_list @axis_tags = @config = load_config.merge(config) @regions = build_regions @scalar_cache = {} if @config.dig(:region_matching, :cache_scalars) end |
Instance Attribute Details
#config ⇒ Hash (readonly)
Returns Configuration settings.
26 27 28 |
# File 'lib/fontisan/variable/region_matcher.rb', line 26 def config @config end |
#regions ⇒ Array<Array<Hash>> (readonly)
Returns Variation regions.
29 30 31 |
# File 'lib/fontisan/variable/region_matcher.rb', line 29 def regions @regions end |
Instance Method Details
#clear_cache ⇒ Object
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
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
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_count ⇒ Integer
Get number of regions
82 83 84 |
# File 'lib/fontisan/variable/region_matcher.rb', line 82 def region_count @regions.length end |