all files / fontkit/playground/ BitmapGlyph.coffee

0% Statements 0/24
0% Branches 0/8
0% Functions 0/5
0% Lines 0/22
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                                                                     
Glyph = require './Glyph'
CBDT = require '../tables/EBDT'
 
class BitmapGlyph extends Glyph
  _decode: ->
    unless @_decoded
      cblc = @_font.CBLC
      table = cblc.sizes[0].indexSubTableArray[0]
      offset = table.subtable.offsetArray[@id - table.firstGlyphIndex]
      return null unless offset?
    
      @_font.stream.pos = @_font.directory.tables.CBDT.offset + table.subtable.imageDataOffset + offset
      @_decoded = CBDT.glyph.decode @_font.stream, version: table.subtable.imageFormat
      
    return @_decoded
  
  getImageForSize: (size) ->
    return @_decode()?.data
    
  _getBBox: ->
    metrics = @_decode()?.metrics
    if metrics
      return [0, -metrics.height+metrics.bearingY, metrics.width, metrics.bearingY]
      
    return [0, 0, 0, 0]
    
  render: (ctx, size) ->
    img = @getImageForSize size
    cblc = @_font.CBLC
    ppem = cblc.sizes[0].ppemY
    if img
      ctx.image img, height: (@bbox[3] - @bbox[1]) * size / ppem, x: 0, y: @bbox[1] * size / ppem
    
module.exports = BitmapGlyph