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

Statements: 55.21% (175 / 317)      Branches: 46.53% (47 / 101)      Functions: 100% (6 / 6)      Lines: 55.7% (166 / 298)      Ignored: none     

All files » fontkit/src/glyph/ » CFFGlyph.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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 4301 1   1   1   1 60 53 7 7         30 30   30 30 30 30   30 30 30   30 30 30   30 30   30 30   30 30 30   30 71 25   71 71   30 149 1894 1894 554   52     1     1 1     28 36 36 36     66 66 183 103   80   183 183     3 3 3 3 3 3 3 3     52 52 52 52 52 52 52 52 52 52 52     96     30 1   30     19 19     37       37 37 37     15     15 15     5 5 5 5 5 5 5 5   5 5 5     3 3 3 3   3 3 3 3 3 3 3     7 7   7 7 7 7 7 7 7 7     19 19   19 19 19 19 19 19 19 19     7     67 67 67 67 67 67 67 67 67 67 67     47 47 91 54 54 54 54 54 54   37 37 37 37 37 37   91 91                                                                                                                                                                                                                                                                                                                                                                                                 1340 897 443 307 307 136 136 136       30   1  
Glyph = require './Glyph'
Path = require './Path'
 
class CFFGlyph extends Glyph
  _getName: ->
    return @_font['CFF '].getGlyphName @id
  
  bias = (s) ->
    if s.length < 1240
      return 107
    else Eif s.length < 33900
      return 1131
    else
      return 32768
    
  _getPath: ->
    stream = @_font.stream
    pos = stream.pos
    
    cff = @_font['CFF ']
    str = cff.topDict.CharStrings[@id]
    end = str.offset + str.length
    stream.pos = str.offset
    
    path = new Path
    stack = []
    trans = []
    
    width = null
    nStems = 0
    x = y = 0
        
    @_usedGsubrs = usedGsubrs = {}
    @_usedSubrs = usedSubrs = {}
    
    gsubrs = cff.globalSubrIndex or []
    gsubrsBias = bias gsubrs
    
    privateDict = cff.privateDictForGlyph @id
    subrs = privateDict.Subrs or []
    subrsBias = bias subrs
    
    parseStems = ->
      if stack.length % 2 isnt 0
        width ?= stack.shift() + privateDict.nominalWidthX
        
      nStems += stack.length >> 1
      stack.length = 0
    
    do parse = ->
      while stream.pos < end        
        op = stream.readUInt8()
        if op < 32
          switch op
            when 1, 3, 18, 23 # hstem, vstem, hstemhm, vstemhm
              parseStems()
          
            when 4 # vmoveto
              Iif stack.length > 1
                width ?= stack.shift() + privateDict.nominalWidthX
              
              y += stack.shift()
              path.moveTo x, y
          
            when 5 # rlineto
              while stack.length >= 2
                x += stack.shift()
                y += stack.shift()
                path.lineTo x, y
            
            when 6, 7 # hlineto, vlineto
              phase = op is 6
              while stack.length >= 1
                if phase
                  x += stack.shift()
                else
                  y += stack.shift()
                
                path.lineTo x, y
                phase = !phase
            
            when 8 # rrcurveto
              while stack.length > 0
                c1x = x + stack.shift()
                c1y = y + stack.shift()
                c2x = c1x + stack.shift()
                c2y = c1y + stack.shift()
                x = c2x + stack.shift()
                y = c2y + stack.shift()
                path.bezierCurveTo c1x, c1y, c2x, c2y, x, y
            
            when 10 # callsubr
              index = stack.pop() + subrsBias
              subr = subrs[index]
              Eif subr
                usedSubrs[index] = true
                p = stream.pos
                e = end
                stream.pos = subr.offset
                end = subr.offset + subr.length
                parse()
                stream.pos = p
                end = e
          
            when 11 # return
              return
          
            when 14 # endchar
              if stack.length > 0
                width ?= stack.shift() + privateDict.nominalWidthX
            
              path.closePath()
          
            when 19, 20 # hintmask, cntrmask
              parseStems()
              stream.pos += (nStems + 7) >> 3
          
            when 21 # rmoveto
              Iif stack.length > 2
                width ?= stack.shift() + privateDict.nominalWidthX
                haveWidth = true
            
              x += stack.shift()
              y += stack.shift()
              path.moveTo x, y
          
            when 22 # hmoveto
              Iif stack.length > 1
                width ?= stack.shift() + privateDict.nominalWidthX
            
              x += stack.shift()
              path.moveTo x, y
          
            when 24 # rcurveline
              while stack.length >= 8
                c1x = x + stack.shift()
                c1y = y + stack.shift()
                c2x = c1x + stack.shift()
                c2y = c1y + stack.shift()
                x = c2x + stack.shift()
                y = c2y + stack.shift()
                path.bezierCurveTo c1x, c1y, c2x, c2y, x, y
          
              x += stack.shift()
              y += stack.shift()
              path.lineTo x, y
          
            when 25 # rlinecurve
              while stack.length >= 8
                x += stack.shift()
                y += stack.shift()
                path.lineTo x, y
          
              c1x = x + stack.shift()
              c1y = y + stack.shift()
              c2x = c1x + stack.shift()
              c2y = c1y + stack.shift()
              x = c2x + stack.shift()
              y = c2y + stack.shift()
              path.bezierCurveTo c1x, c1y, c2x, c2y, x, y
          
            when 26 # vvcurveto
              Eif stack.length % 2
                x += stack.shift()
          
              while stack.length >= 4
                c1x = x
                c1y = y + stack.shift()
                c2x = c1x + stack.shift()
                c2y = c1y + stack.shift()
                x = c2x
                y = c2y + stack.shift()
                path.bezierCurveTo c1x, c1y, c2x, c2y, x, y
          
            when 27 # hhcurveto
              Eif stack.length % 2
                y += stack.shift()
          
              while stack.length >= 4
                c1x = x + stack.shift()
                c1y = y
                c2x = c1x + stack.shift()
                c2y = c1y + stack.shift()
                x = c2x + stack.shift()
                y = c2y
                path.bezierCurveTo c1x, c1y, c2x, c2y, x, y
          
            when 28 # shortint
              stack.push stream.readInt16BE()
          
            when 29 # callgsubr
              index = stack.pop() + gsubrsBias
              subr = gsubrs[index]
              Eif subr
                usedGsubrs[index] = true
                p = stream.pos
                e = end
                stream.pos = subr.offset
                end = subr.offset + subr.length
                parse()
                stream.pos = p
                end = e
          
            when 30, 31 # vhcurveto, hvcurveto
              phase = op is 31
              while stack.length >= 4
                if phase
                  c1x = x + stack.shift()
                  c1y = y
                  c2x = c1x + stack.shift()
                  c2y = c1y + stack.shift()
                  y = c2y + stack.shift()
                  x = c2x + (if stack.length is 1 then stack.shift() else 0)
                else
                  c1x = x
                  c1y = y + stack.shift()
                  c2x = c1x + stack.shift()
                  c2y = c1y + stack.shift()
                  x = c2x + stack.shift()
                  y = c2y + (if stack.length is 1 then stack.shift() else 0)
                
                path.bezierCurveTo c1x, c1y, c2x, c2y, x, y
                phase = !phase
              
            when 12
              op = stream.readUInt8()
              switch op
                when 3 # and
                  a = stack.pop()
                  b = stack.pop()
                  stack.push if a and b then 1 else 0
            
                when 4 # or
                  a = stack.pop()
                  b = stack.pop()
                  stack.push if a or b then 1 else 0
            
                when 5 # not
                  a = stack.pop()
                  stack.push if a then 0 else 1
            
                when 9 # abs
                  a = stack.pop()
                  stack.push Math.abs a
            
                when 10 # add
                  a = stack.pop()
                  b = stack.pop()
                  stack.push a + b
            
                when 11 # sub
                  a = stack.pop()
                  b = stack.pop()
                  stack.push a - b
            
                when 12 # div
                  a = stack.pop()
                  b = stack.pop()
                  stack.push a / b
            
                when 14 # neg
                  a = stack.pop()
                  stack.push -a
            
                when 15 # eq
                  a = stack.pop()
                  b = stack.pop()
                  stack.push if a is b then 1 else 0
            
                when 18 # drop
                  stack.pop()
            
                when 20 # put
                  val = stack.pop()
                  idx = stack.pop()
                  trans[idx] = val
            
                when 21 # get
                  idx = stack.pop()
                  stack.push trans[idx] or 0
            
                when 22 # ifelse
                  s1 = stack.pop()
                  s2 = stack.pop()
                  v1 = stack.pop()
                  v2 = stack.pop()
                  stack.push if v1 <= v2 then s1 else s2
            
                when 23 # random
                  stack.push Math.random()
            
                when 24 # mul
                  a = stack.pop()
                  b = stack.pop()
                  stack.push a * b
            
                when 26 # sqrt
                  a = stack.pop()
                  stack.push Math.sqrt a
            
                when 27 # dup
                  a = stack.pop()
                  stack.push a, a
            
                when 28 # exch
                  a = stack.pop()
                  b = stack.pop()
                  stack.push b, a
            
                when 29 # index
                  idx = stack.pop()
                  if idx < 0
                    idx = 0
                  else if idx > stack.length - 1
                    idx = stack.length - 1
                
                  stack.push stack[idx]
            
                when 30 # roll
                  n = stack.pop()
                  j = stack.pop()
                  
                  if j >= 0
                    while j > 0
                      t = stack[n - 1]
                      for i in [n - 2..0] by -1
                        stack[i + 1] = stack[i]
                        
                      stack[0] = t
                      j--
                  else
                    while j < 0
                      t = stack[0]
                      for i in [0..n] by 1
                        stack[i] = stack[i + 1]
                        
                      stack[n - 1] = t
                      j++
              
                when 34 # hflex
                  c1x = x + stack.shift()
                  c1y = y
                  c2x = c1x + stack.shift()
                  c2y = c1y + stack.shift()
                  c3x = c2x + stack.shift()
                  c3y = c2y
                  c4x = c3x + stack.shift()
                  c4y = c3y
                  c5x = c4x + stack.shift()
                  c5y = c4y
                  c6x = c5x + stack.shift()
                  c6y = c5y
                  x = c6x
                  y = c6y
              
                  path.bezierCurveTo c1x, c1y, c2x, c2y, c3x, c3y
                  path.bezierCurveTo c4x, c4y, c5x, c5y, c6x, c6y
              
                when 35 # flex
                  pts = []
                  for i in [0...6]
                    x += stack.shift()
                    y += stack.shift()
                    pts.push x, y
                
                  path.bezierCurveTo pts[0...6]...
                  path.bezierCurveTo pts[6...]...
                  stack.shift() # fd
              
                when 36 # hflex1
                  c1x = x + stack.shift()
                  c1y = y + stack.shift()
                  c2x = c1x + stack.shift()
                  c2y = c1y + stack.shift()
                  c3x = c2x + stack.shift()
                  c3y = c2y
                  c4x = c3x + stack.shift()
                  c4y = c3y
                  c5x = c4x + stack.shift()
                  c5y = c4y + stack.shift()
                  c6x = c5x + stack.shift()
                  c6y = c5y
                  x = c6x
                  y = c6y
              
                  path.bezierCurveTo c1x, c1y, c2x, c2y, c3x, c3y
                  path.bezierCurveTo c4x, c4y, c5x, c5y, c6x, c6y
              
                when 37 # flex1
                  startx = x
                  starty = y
            
                  pts = []
                  for i in [0...5]
                    x += stack.shift()
                    y += stack.shift()
                    pts.push x, y
                
                  if Math.abs(x - startx) > Math.abs(y - starty) # horizontal
                    x += stack.shift()
                    y = starty
                  else
                    x = startx
                    y += stack.shift()
                
                  pts.push x, y
                  path.bezierCurveTo pts[0...6]...
                  path.bezierCurveTo pts[6...]...
              
                else
                  throw new Error 'Unknown op: 12 ' + op
                  
            else
              throw new Error 'Unknown op: ' + op
          
        else if op < 247
          stack.push op - 139
        else if op < 251
          b1 = stream.readUInt8()
          stack.push (op - 247) * 256 + b1 + 108
        else Eif op < 255
          b1 = stream.readUInt8()
          stack.push -(op - 251) * 256 - b1 - 108
        else
          stack.push stream.readInt32BE() / 65536
            
    return path
    
module.exports = CFFGlyph