From d976d0a8ad64981054ecb1430ba33c1bd95c73b8 Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Thu, 26 May 2022 17:11:38 -0400 Subject: [PATCH] Reduce array allocations when loading a font --- lib/ttfunk/table/kern/format0.rb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/ttfunk/table/kern/format0.rb b/lib/ttfunk/table/kern/format0.rb index 165ad053..c6366e79 100644 --- a/lib/ttfunk/table/kern/format0.rb +++ b/lib/ttfunk/table/kern/format0.rb @@ -9,14 +9,14 @@ class Format0 include Reader attr_reader :attributes - attr_reader :pairs + # attr_reader :pairs def initialize(attributes = {}) @attributes = attributes num_pairs, *pairs = attributes.delete(:data).unpack('nx6n*') - @pairs = {} + @pairs = Hash.new { |h, k| h[k] = {} } num_pairs.times do |i| # sanity check, in case there's a bad length somewhere break if i * 3 + 2 > pairs.length @@ -24,10 +24,20 @@ def initialize(attributes = {}) left = pairs[i * 3] right = pairs[i * 3 + 1] value = to_signed(pairs[i * 3 + 2]) - @pairs[[left, right]] = value + @pairs[left][right] = value end end + def pairs + self + end + + def [](lookup) + left = lookup[0] + right = lookup[1] + @pairs[left][right] + end + def vertical? @attributes[:vertical] end @@ -42,9 +52,11 @@ def cross_stream? def recode(mapping) subset = [] - pairs.each do |(left, right), value| - if mapping[left] && mapping[right] - subset << [mapping[left], mapping[right], value] + @pairs.each do |left, rights| + rights.each do |right, value| + if mapping[left] && mapping[right] + subset << [mapping[left], mapping[right], value] + end end end