import startCoord from "./startCoord";

// 绘制文字
function drawText() {
  var fillStyle = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var _ref = arguments.length > 1 ? arguments[1] : undefined,
    ctx = _ref.ctx,
    width = _ref.width,
    height = _ref.height;
  if (!(fillStyle !== null && fillStyle !== void 0 && fillStyle.text)) {
    console.log('SeedsUI Error:手写签名drawFont缺少文字');
    return;
  }
  var fontSize = fillStyle.fontSize || 15;
  var fontFamily = fillStyle.fontFamily || 'microsoft yahei';
  var fontPosition = fillStyle.position || 'bottom center';
  var fontStyle = fillStyle.color || 'rgba(0, 0, 0, 1)';
  if (isNaN(fontSize)) {
    console.log('SeedsUI Error:文字大小请输入数字类型');
    return;
  }
  var targetHeight = fontSize;
  var targetWidth = fillStyle.text.length * targetHeight;

  // 起始绘制的xy坐标
  var coord = startCoord(targetWidth, targetHeight, fontPosition, {
    ctx: ctx,
    width: width,
    height: height
  });

  // 计算的位置中文字的中线所以需要往下移动一些绘制
  var calcY = coord.y === 0 ? Number(coord.y) + Number(targetHeight) : Number(coord.y) + Number(targetHeight) - 5; // 文字垂直位置有整个高度的偏差

  // 写字
  ctx.font = fontSize + 'px ' + fontFamily;
  ctx.fillStyle = fontStyle;
  ctx.fillText(fillStyle.text, coord.x, calcY);
}
export default drawText;