vendor/ttfunk/lib/ttfunk/table/hmtx.rb in prawn-0.3.0 vs vendor/ttfunk/lib/ttfunk/table/hmtx.rb in prawn-0.4.0
- old
+ new
@@ -1,20 +1,47 @@
+require 'ttfunk/table'
+
module TTFunk
class Table
class Hmtx < Table
- def initialize(fh, font, info)
- fh.pos = info[:offset]
- @values = []
+ attr_reader :metrics
+ attr_reader :left_side_bearings
+ attr_reader :widths
- font.hhea.number_of_hmetrics.times do
- advance = fh.read(2).unpack("n").first
- lsb = to_signed(fh.read(2).unpack("n").first)
- @values << [advance,lsb]
+ def self.encode(hmtx, mapping)
+ metrics = mapping.keys.sort.map do |new_id|
+ metric = hmtx.for(mapping[new_id])
+ [metric.advance_width, metric.left_side_bearing]
end
-
- lsb_count = font.hhea.number_of_hmetrics - font.maxp.num_glyphs
- pattern = "n#{lsb_count}"
- @lsb = fh.read(2*lsb_count).unpack(pattern).map { |e| to_signed(e) }
+
+ { :number_of_metrics => metrics.length,
+ :table => metrics.flatten.pack("n*") }
end
+
+ HorizontalMetric = Struct.new(:advance_width, :left_side_bearing)
+
+ def for(glyph_id)
+ @metrics[glyph_id] ||
+ HorizontalMetric.new(@metrics.last.advance_width,
+ @left_side_bearings[glyph_id - @metrics.length])
+ end
+
+ private
+
+ def parse!
+ @metrics = []
+
+ file.horizontal_header.number_of_metrics.times do
+ advance = read(2, "n").first
+ lsb = read_signed(1).first
+ @metrics.push HorizontalMetric.new(advance, lsb)
+ end
+
+ lsb_count = file.maximum_profile.num_glyphs - file.horizontal_header.number_of_metrics
+ @left_side_bearings = read_signed(lsb_count)
+
+ @widths = @metrics.map { |metric| metric.advance_width }
+ @widths += [@widths.last] * @left_side_bearings.length
+ end
end
end
-end
\ No newline at end of file
+end