Code coverage report for fontkit/src/opentype/shapers/DefaultShaper.coffee

Statements: 100% (33 / 33)      Branches: 100% (2 / 2)      Functions: 100% (6 / 6)      Lines: 100% (33 / 33)      Ignored: none     

All files » fontkit/src/opentype/shapers/ » DefaultShaper.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 661   1 1 1 1 1       1     19 19 19     19     19     19               19       10 10 156 156 1 1     1 1 1 1     1 2 2 2     1 1     155   10   1  
unicode = require 'unicode-properties'
 
COMMON_FEATURES = ['ccmp', 'locl', 'rlig', 'mark', 'mkmk']
FRACTIONAL_FEATURES = ['frac', 'numr', 'dnom']
HORIZONTAL_FEATURES = ['calt', 'clig', 'liga', 'rclt', 'curs', 'kern']
VERTICAL_FEATURES = ['vert']
DIRECTIONAL_FEATURES =
  ltr: ['ltra', 'ltrm']
  rtl: ['rtla', 'rtlm']
 
class DefaultShaper
  @plan: (plan, glyphs, features) ->
    # Plan the features we want to apply
    @planPreprocessing plan
    @planFeatures plan
    @planPostprocessing plan, features
    
    # Assign the global features to all the glyphs
    plan.assignGlobalFeatures glyphs
    
    # Assign local features to glyphs
    @assignFeatures plan, glyphs
    
  @planPreprocessing: (plan) ->
    plan.add
      global: DIRECTIONAL_FEATURES[plan.direction]
      local: FRACTIONAL_FEATURES
        
  @planFeatures: (plan) ->
    # Do nothing by default. Let subclasses override this.
  
  @planPostprocessing: (plan, userFeatures) ->
    plan.add [COMMON_FEATURES..., HORIZONTAL_FEATURES..., userFeatures...]
      
  @assignFeatures: (plan, glyphs) ->
    # Enable contextual fractions
    i = 0
    while i < glyphs.length
      glyph = glyphs[i]
      if glyph.codePoints[0] is 0x2044 # fraction slash
        start = i - 1
        end = i + 1
        
        # Apply numerator
        while start >= 0 and unicode.isDigit(glyphs[start].codePoints[0])
          glyphs[start].features.numr = true
          glyphs[start].features.frac = true
          start--
          
        # Apply denominator
        while end < glyphs.length and unicode.isDigit(glyphs[end].codePoints[0])
          glyphs[end].features.dnom = true
          glyphs[end].features.frac = true
          end++
          
        # Apply fraction slash
        glyph.features.frac = true
        i = end - 1
        
      else
        i++
        
    return
    
module.exports = DefaultShaper