function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/* global document */
import { Texture2D } from 'luma.gl';

var GL_TEXTURE_WRAP_S = 0x2802;
var GL_TEXTURE_WRAP_T = 0x2803;
var GL_CLAMP_TO_EDGE = 0x812f;
var MAX_CANVAS_WIDTH = 1024;
var fontSize = 64;
var padding = 4;

var charList = [];
for (var i = 32; i < 128; i++) {
  charList.push(String.fromCharCode(i));
}

function setTextStyle(ctx, fontFamily) {
  ctx.font = fontSize + 'px ' + fontFamily;
  ctx.fillStyle = '#000';
  ctx.textBaseline = 'hanging';
  ctx.textAlign = 'left';
}

export function makeFontAtlas(gl, fontFamily) {
  var _parameters;

  var canvas = document.createElement('canvas');
  var ctx = canvas.getContext('2d');
  setTextStyle(ctx, fontFamily);

  // measure texts
  var row = 0;
  var x = 0;
  var mapping = {};

  charList.forEach(function (char) {
    var _ctx$measureText = ctx.measureText(char),
        width = _ctx$measureText.width;

    if (x + width > MAX_CANVAS_WIDTH) {
      x = 0;
      row++;
    }
    mapping[char] = {
      x: x,
      y: row * (fontSize + padding),
      width: width,
      height: fontSize,
      mask: true
    };
    x += width + padding;
  });

  canvas.width = MAX_CANVAS_WIDTH;
  canvas.height = (row + 1) * (fontSize + padding);

  setTextStyle(ctx, fontFamily);
  for (var char in mapping) {
    ctx.fillText(char, mapping[char].x, mapping[char].y);
  }

  return {
    mapping: mapping,
    texture: new Texture2D(gl, {
      pixels: canvas,
      // padding is added only between the characters but not for borders
      // enforce CLAMP_TO_EDGE to avoid any artifacts.
      parameters: (_parameters = {}, _defineProperty(_parameters, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE), _defineProperty(_parameters, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE), _parameters)
    })
  };
}
//# sourceMappingURL=font-atlas.js.map