all files / fontkit/src/ TrueTypeCollection.coffee

92.31% Statements 24/26
75% Branches 3/4
100% Functions 5/5
92.31% Lines 24/26
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                                                
r = require 'restructure'
TTFFont = require './TTFFont'
Directory = require './tables/directory'
tables = require './tables'
 
class TrueTypeCollection
  get = require('./get')(this)
  TTCHeader = new r.VersionedStruct r.uint32,
    0x00010000:
      numFonts:   r.uint32
      offsets:    new r.Array(r.uint32, 'numFonts')
    0x00020000:
      numFonts:   r.uint32
      offsets:    new r.Array(r.uint32, 'numFonts')
      dsigTag:    r.uint32
      dsigLength: r.uint32
      dsigOffset: r.uint32
      
  @probe: (buffer) ->
    return buffer.toString('ascii', 0, 4) is 'ttcf'
      
  constructor: (@stream) ->
    Iif @stream.readString(4) isnt 'ttcf'
      throw new Error 'Not a TrueType collection'
      
    @header = TTCHeader.decode @stream
    
  getFont: (name) ->
    for offset in @header.offsets
      stream = new r.DecodeStream @stream.buffer
      stream.pos = offset
      font = new TTFFont stream
      if font.postscriptName is name
        return font
        
    return null
  
  get 'fonts', ->
    fonts = []
    for offset in @header.offsets
      stream = new r.DecodeStream @stream.buffer
      stream.pos = offset
      fonts.push new TTFFont stream
      
    return fonts
    
module.exports = TrueTypeCollection