all files / fontkit/src/tables/ directory.coffee

100% Statements 20/20
100% Branches 0/0
100% Functions 2/2
100% Lines 20/20
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                           51× 51× 855×   51×   18×                
r = require 'restructure'
Tables = require './'
 
TableEntry = new r.Struct
  tag:        new r.String(4)
  checkSum:   r.uint32
  offset:     new r.Pointer(r.uint32, 'void', type: 'global')
  length:     r.uint32
 
Directory = new r.Struct
  tag:            new r.String(4)
  numTables:      r.uint16
  searchRange:    r.uint16
  entrySelector:  r.uint16
  rangeShift:     r.uint16
  tables:         new r.Array(TableEntry, 'numTables')
  
Directory.process = ->
  tables = {}
  for table in @tables
    tables[table.tag] = table
    
  @tables = tables
  
Directory.preEncode = (stream) ->
  tables = []
  for tag, table of @tables when table?
    tables.push
      tag: tag
      checkSum: 0
      offset: new r.VoidPointer(Tables[tag], table)
      length: Tables[tag].size(table)
  
  @tag = 'true'
  @numTables = tables.length
  @tables = tables
  
  @searchRange = Math.floor(Math.log(@numTables) / Math.LN2) * 16
  @entrySelector = Math.floor @searchRange / Math.LN2
  @rangeShift = @numTables * 16 - @searchRange
    
module.exports = Directory