all files / fontkit/src/opentype/ GlyphIterator.coffee

97.37% Statements 37/38
100% Branches 4/4
90.91% Functions 10/11
97.06% Lines 33/34
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   116×     534×   6959×   6689× 6682× 6682×   7109× 7109×   7109× 427×   6682×     374×           423× 423× 423× 423×     58× 58× 58× 58× 58×     39183× 39183× 39183× 6735×   39183×    
class GlyphIterator
  get = require('../get')(this)
  constructor: (@glyphs, flags) ->
    @reset flags
    
  reset: (@flags = {}) ->
    @index = 0
  
  get 'cur', ->
    return @glyphs[@index] or null
    
  shouldIgnore = (glyph, flags) ->
    return ((flags.ignoreMarks and glyph.isMark) or
           (flags.ignoreBaseGlyphs and not glyph.isMark) or
           (flags.ignoreLigatures and glyph.isLigature))
           
  move = (dir) ->
    @index += dir
    while 0 <= @index < @glyphs.length and shouldIgnore @glyphs[@index], @flags
      @index += dir
      
    unless 0 <= @index < @glyphs.length
      return null
      
    return @glyphs[@index]
    
  next: ->
    move.call this, 1
    
  prev: ->
    move.call this, -1
    
  peek: (count = 1) ->
    idx = @index
    res = @increment count
    @index = idx
    return res
    
  peekIndex: (count = 1) ->
    idx = @index
    @increment count
    res = @index
    @index = idx
    return res
    
  increment: (count = 1) ->
    dir = if count < 0 then -1 else 1
    count = Math.abs count
    while count--
      move.call this, dir
      
    return @glyphs[@index]
 
module.exports = GlyphIterator