Code coverage report for fontkit/src/subset/TTFSubset.coffee

Statements: 97.5% (39 / 40)      Branches: 75% (3 / 4)      Functions: 100% (3 / 3)      Lines: 97.5% (39 / 40)      Ignored: none     

All files » fontkit/src/subset/ » TTFSubset.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 1141 1 1 1   1   9     9 9   9 9   9     9 1 1 2 2   9 9   9 9           9 9                 2 2 2     2             2 2 9   2 2   2 2   2 2   2 2                                                 2                               1  
cloneDeep = require 'clone'
Subset = require './Subset'
Directory = require '../tables/directory'
Tables = require '../tables'
 
class TTFSubset extends Subset
  _addGlyph: (gid) ->
    glyf = @font.getGlyph(gid)._decode()
    
    # get the offset to the glyph from the loca table
    curOffset = @font.loca.offsets[gid]
    nextOffset = @font.loca.offsets[gid + 1]
    
    stream = @font._getTableStream 'glyf'
    stream.pos += curOffset
    
    buffer = stream.readBuffer(nextOffset - curOffset)
  
    # if it is a compound glyph, include its components
    if glyf?.numberOfContours < 0
      buffer = new Buffer(buffer)
      for component in glyf.components
        gid = @includeGlyph component.glyphID
        buffer.writeUInt16BE gid, component.pos
        
    @glyf.push buffer
    @loca.offsets.push @offset
    
    Eif gid < @font.hmtx.metrics.length
      @hmtx.metrics.push @font.hmtx.metrics.get gid
    else
      @hmtx.metrics.push
        advance: @font.hmtx.metrics.get(@font.hmtx.metrics.length - 1).advance
        bearing: @font.hmtx.bearings.get(gid - @font.hmtx.metrics.length)
      
    @offset += buffer.length
    return @glyf.length - 1
          
  encode: (stream) ->      
    # tables required by PDF spec: 
    #   head, hhea, loca, maxp, cvt , prep, glyf, hmtx, fpgm
    #
    # additional tables required for standalone fonts: 
    #   name, cmap, OS/2, post
              
    @glyf = []
    @offset = 0
    @loca = 
      offsets: []
    
    @hmtx =
      metrics: []
      bearings: []
      
    # include all the glyphs
    # not using a for loop because we need to support adding more
    # glyphs to the array as we go, and CoffeeScript caches the length.
    i = 0
    while i < @glyphs.length
      @_addGlyph @glyphs[i++]
      
    maxp = cloneDeep @font.maxp
    maxp.numGlyphs = @glyf.length
      
    @loca.offsets.push @offset
    Tables.loca.preEncode.call @loca
    
    head = cloneDeep @font.head
    head.indexToLocFormat = @loca.version
    
    hhea = cloneDeep @font.hhea
    hhea.numberOfMetrics = @hmtx.metrics.length
        
    # map = []
    # for index in [0...256]
    #     if index < @numGlyphs
    #         map[index] = index
    #     else
    #         map[index] = 0
    # 
    # cmapTable = 
    #     version: 0
    #     length: 262
    #     language: 0
    #     codeMap: map
    # 
    # cmap = 
    #     version: 0
    #     numSubtables: 1
    #     tables: [
    #         platformID: 1
    #         encodingID: 0
    #         table: cmapTable
    #     ]
        
    # TODO: subset prep, cvt, fpgm?
    Directory.encode stream,
      tables:
        head: head
        hhea: hhea
        loca: @loca
        maxp: maxp
        'cvt ': @font['cvt ']
        prep: @font.prep
        glyf: @glyf
        hmtx: @hmtx
        fpgm: @font.fpgm
        # name: clone @font.name
        # 'OS/2': clone @font['OS/2']
        # post: clone @font.post
        # cmap: cmap
        
module.exports = TTFSubset