Code coverage report for fontkit/src/glyph/COLRGlyph.coffee

Statements: 82.05% (32 / 39)      Branches: 66.67% (4 / 6)      Functions: 83.33% (5 / 6)      Lines: 81.58% (31 / 38)      Ignored: none     

All files » fontkit/src/glyph/ » COLRGlyph.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 631 1   1     1 1       1 1 3 3 3   1   1 2 2 2 2   2 4 4   4   4 2   2 2       2                     2 6 6 6 6             1  
Glyph = require './Glyph'
BBox = require './BBox'
 
class COLRGlyph extends Glyph
  isColorGlyph: yes
  
  get = require('../get')(this)
  class COLRLayer
    constructor: (@glyph, @color) ->
    
  _getBBox: ->
    bbox = new BBox
    for layer in @layers
      b = layer.glyph.bbox
      bbox.addPoint b.minX, b.minY
      bbox.addPoint b.maxX, b.maxY
          
    return bbox
      
  get 'layers', ->
    cpal = @_font.CPAL
    colr = @_font.COLR
    low = 0
    high = colr.baseGlyphRecord.length - 1
 
    while low <= high
      mid = (low + high) >> 1
      rec = colr.baseGlyphRecord[mid]
 
      Iif @id < rec.gid
        high = mid - 1
      else if @id > rec.gid
        low = mid + 1
      else
        baseLayer = rec
        break
        
    # if base glyph not found in COLR table, 
    # default to normal glyph from glyf or CFF
    Iunless baseLayer?
      g = @_font._getBaseGlyph(@id)
      color = 
        red: 0
        green: 0
        blue: 0
        alpha: 255
        
      return [new COLRLayer g, color]
    
    # otherwise, return an array of all the layers
    for i in [baseLayer.firstLayerIndex...baseLayer.firstLayerIndex + baseLayer.numLayers] by 1
      rec = colr.layerRecords[i]
      color = cpal.colorRecords[rec.paletteIndex]
      g = @_font._getBaseGlyph rec.gid
      new COLRLayer g, color
  
  render: (ctx, size) ->
    for {glyph, color} in @layers
      ctx.fillColor [color.red, color.green, color.blue], color.alpha / 255 * 100
      glyph.render(ctx, size)
 
module.exports = COLRGlyph