'use strict'

module.exports = textGet

var vectorizeText = require('vectorize-text')

var globals = window || process.global || {}
var __TEXT_CACHE  = globals.__TEXT_CACHE || {}
globals.__TEXT_CACHE = {}

function unwrap(mesh) {
  var cells     = mesh.cells
  var positions = mesh.positions
  var data      = new Float32Array(cells.length * 6)
  var ptr       = 0
  var shapeX    = 0
  for(var i=0; i<cells.length; ++i) {
    var tri = cells[i]
    for(var j=0; j<3; ++j) {
      var point = positions[tri[j]]
      data[ptr++] = point[0]
      data[ptr++] = point[1] + 2
      shapeX      = Math.max(point[0], shapeX)
    }
  }
  return {
    data:  data,
    shape: shapeX
  }
}

function textGet(font, text) {
  var fontcache = __TEXT_CACHE[font]
   if(!fontcache) {
     fontcache = __TEXT_CACHE[font] = {
       ' ': {
         data:   new Float32Array(0),
         shape: 0.2
       }
     }
   }
   var mesh = fontcache[text]
   if(!mesh) {
     if(text.length <= 1 || !/\d/.test(text)) {
       mesh = fontcache[text] = unwrap(vectorizeText(text, {
         triangles:     true,
         font:          font,
         textAlign:     'left',
         textBaseline:  'alphabetic'
       }))
     } else {
       var parts = text.split(/(\d|\s)/)
       var buffer = new Array(parts.length)
       var bufferSize = 0
       var shapeX = 0
       for(var i=0; i<parts.length; ++i) {
         buffer[i] = textGet(font, parts[i])
         bufferSize += buffer[i].data.length
         shapeX += buffer[i].shape
       }

       var data = new Float32Array(bufferSize)
       var ptr     = 0
       var xOffset = 0
       for(var i=0; i<buffer.length; ++i) {
         var bdata = buffer[i].data
         for(var j=0; j<bdata.length; j+=2) {
           data[ptr++] = bdata[j] + xOffset
           data[ptr++] = bdata[j+1]
         }
         xOffset += buffer[i].shape + 0.02
       }

       mesh = fontcache[text] = {
         data:  data,
         shape: xOffset
       }
     }
   }

   return mesh
}

@@ -564,17 +564,19 @@
 nt%5B1%5D + 
-2
+1.4
 %0A      s
@@ -1508,16 +1508,72 @@
 %5D.shape%0A
+         if(i%3E0) %7B%0A           shapeX += 0.02%0A         %7D%0A
        %7D
@@ -1665,17 +1665,29 @@
 ffset = 
-0
+-0.5 * shapeX
 %0A       
@@ -2021,23 +2021,22 @@
  shape: 
-xOffset
+shapeX
 %0A       

increase axes generation speed