Code coverage report for fontkit/src/opentype/OTLayoutEngine.coffee

Statements: 100% (39 / 39)      Branches: 71.43% (10 / 14)      Functions: 100% (7 / 7)      Lines: 100% (39 / 39)      Ignored: none     

All files » fontkit/src/opentype/ » OTLayoutEngine.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 691 1 1 1 1   1   9 9   9 7   9 9         19 205       19 19 19     19 19     19 195   19     19 17     19 4 4   19     19 19     1   1 1 1   1 1 1   1   1  
ShapingPlan = require './ShapingPlan'
Shapers = require './shapers'
GlyphInfo = require './GlyphInfo'
GSUBProcessor = require './GSUBProcessor'
GPOSProcessor = require './GPOSProcessor'
 
class OTLayoutEngine
  constructor: (@font) ->
    Eif @font.GSUB
      @GSUBProcessor = new GSUBProcessor(@font, @font.GSUB)
      
    if @font.GPOS
      @GPOSProcessor = new GPOSProcessor(@font, @font.GPOS)
      
    @glyphInfos = null
    @plan = null
    
  setup: (glyphs, features, script, language) ->    
    # Map glyphs to GlyphInfo objects so data can be passed between
    # GSUB and GPOS without mutating the real (shared) Glyph objects.
    @glyphInfos = for glyph, i in glyphs
      new GlyphInfo glyph.id, [glyph.codePoints...]
      
    # Choose a shaper based on the script, and setup a shaping plan.
    # This determines which features to apply to which glyphs.
    shaper = Shapers.choose script
    @plan = new ShapingPlan @font, script, language
    shaper.plan(@plan, @glyphInfos, features)
    
  substitute: (glyphs) ->
    Eif @GSUBProcessor
      @plan.process @GSUBProcessor, @glyphInfos
      
      # Map glyph infos back to normal Glyph objects
      glyphs = for glyphInfo in @glyphInfos
        @font.getGlyph glyphInfo.id, glyphInfo.codePoints
        
    return glyphs
    
  position: (glyphs, positions) ->
    if @GPOSProcessor
      @plan.process @GPOSProcessor, @glyphInfos, positions
            
    # Reverse the glyphs and positions if the script is right-to-left
    if @plan.direction is 'rtl'
      glyphs.reverse()
      positions.reverse()
    
    return @GPOSProcessor?.features
    
  cleanup: ->
    @glyphInfos = null
    @plan = null
    
  getAvailableFeatures: (script, language) ->
    features = []
  
    Eif @GSUBProcessor
      @GSUBProcessor.selectScript script, language
      features.push Object.keys(@GSUBProcessor.features)...
  
    Eif @GPOSProcessor
      @GPOSProcessor.selectScript script, language
      features.push Object.keys(@GPOSProcessor.features)...
      
    return features
 
module.exports = OTLayoutEngine