Sha256: 2d50eef5bf4a1390024169f9484a4f429eea86765fb8e26faa9e1dd399dacae2

Contents?: true

Size: 1.91 KB

Versions: 34

Compression:

Stored size: 1.91 KB

Contents

require 'ttfunk/table'

module TTFunk
  class Table
    class Glyf < Table
      # Accepts a hash mapping (old) glyph-ids to glyph objects, and a hash
      # mapping old glyph-ids to new glyph-ids.
      #
      # Returns a hash containing:
      # 
      # * :table - a string representing the encoded 'glyf' table containing
      #   the given glyphs.
      # * :offsets - an array of offsets for each glyph
      def self.encode(glyphs, new2old, old2new)
        result = { :table => "", :offsets => [] }

        new2old.keys.sort.each do |new_id|
          glyph = glyphs[new2old[new_id]]
          result[:offsets] << result[:table].length
          result[:table] << glyph.recode(old2new) if glyph
        end

        # include an offset at the end of the table, for use in computing the
        # size of the last glyph
        result[:offsets] << result[:table].length
        return result
      end

      def for(glyph_id)
        return @cache[glyph_id] if @cache.key?(glyph_id)

        index = file.glyph_locations.index_of(glyph_id)
        size  = file.glyph_locations.size_of(glyph_id)

        if size.zero? # blank glyph, e.g. space character
          @cache[glyph_id] = nil
          return nil
        end

        parse_from(offset + index) do
          raw = io.read(size)
          number_of_contours, x_min, y_min, x_max, y_max = raw.unpack("n5").map { |i| to_signed(i) }

          @cache[glyph_id] = if number_of_contours == -1
              Compound.new(raw, x_min, y_min, x_max, y_max)
            else
              Simple.new(raw, number_of_contours, x_min, y_min, x_max, y_max)
            end
        end
      end

      private

        def parse!
          # because the glyf table is rather complex to parse, we defer
          # the parse until we need a specific glyf, and then cache it.
          @cache = {}
        end
    end
  end
end

require 'ttfunk/table/glyf/compound'
require 'ttfunk/table/glyf/simple'

Version data entries

34 entries across 34 versions & 10 rubygems

Version Path
ttfunk-1.2.2 lib/ttfunk/table/glyf.rb
ttfunk-1.2.1 lib/ttfunk/table/glyf.rb
ttfunk-1.2.0 lib/ttfunk/table/glyf.rb
ttfunk-1.1.1 lib/ttfunk/table/glyf.rb
ttfunk-1.1.0 lib/ttfunk/table/glyf.rb
ttfunk-1.0.3 lib/ttfunk/table/glyf.rb
ttfunk-1.0.2 lib/ttfunk/table/glyf.rb
ttfunk-1.0.1 lib/ttfunk/table/glyf.rb
davebenvenuti-prawn-0.11.1.pre vendor/ttfunk/lib/ttfunk/table/glyf.rb
piglop-prawn-0.10.2.3 vendor/ttfunk/lib/ttfunk/table/glyf.rb
piglop-prawn-0.10.2.2 vendor/ttfunk/lib/ttfunk/table/glyf.rb
piglop-prawn-0.10.2.1 vendor/ttfunk/lib/ttfunk/table/glyf.rb
glyph_imager-0.1.1 vendor/ttfunk/lib/ttfunk/table/glyf.rb
prawn-0.11.1.pre vendor/ttfunk/lib/ttfunk/table/glyf.rb
goodwill-prawn-edge-0.10.0 vendor/ttfunk/lib/ttfunk/table/glyf.rb
alphasights-prawn-0.10.4 vendor/ttfunk/lib/ttfunk/table/glyf.rb
alphasights-prawn-0.10.3 vendor/ttfunk/lib/ttfunk/table/glyf.rb
alphasights-prawn-0.10.2 vendor/ttfunk/lib/ttfunk/table/glyf.rb
alphasights-prawn-0.10.1 vendor/ttfunk/lib/ttfunk/table/glyf.rb
glyph_imager-0.1.0 vendor/ttfunk/lib/ttfunk/table/glyf.rb