"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createTextCanvas = createTextCanvas;
exports.getBase64 = getBase64;
exports.getLocation = getLocation;
exports.getUTF8CharLength = getUTF8CharLength;
exports.isEmptyValues = isEmptyValues;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
function isEmptyValues(value) {
  return value === undefined || value === null;
}
function getUTF8CharLength(str) {
  let i = 0;
  let code;
  let len = 0;
  for (; i < str.length; i++) {
    code = str.charCodeAt(i);
    if (code === 10) {
      // 回车换行问题
      len += 2;
    } else if (code < 0x007f) {
      len += 1;
    } else if (code >= 0x0080 && code <= 0x07ff) {
      len += 2;
    } else if (code >= 0x0800 && code <= 0xffff) {
      len += 3;
    }
  }
  return len;
}
function getLocation(watermark, params) {
  return {
    northwest: watermark.upperLeft(...params),
    north: watermark.northCenter(...params),
    northeast: watermark.upperRight(...params),
    west: watermark.westCenter(...params),
    center: watermark.center(...params),
    east: watermark.eastCenter(...params),
    southwest: watermark.lowerLeft(...params),
    south: watermark.southCenter(...params),
    southeast: watermark.lowerRight(...params)
  };
}
function getBase64(IMG, defaultImgSize, spc) {
  const imgWidth = IMG.naturalWidth;
  const imgHeight = IMG.naturalHeight;
  // 根据 面积处理 图片
  const pixels = defaultImgSize.width * defaultImgSize.height * spc / 1000;
  const rate = imgWidth / imgHeight;
  const oWidth = Math.sqrt(pixels * rate);
  const oHeight = Math.sqrt(pixels / rate);
  const canvas = document.createElement('canvas');
  canvas.width = oWidth; // 设置画布的宽度
  canvas.height = oHeight; // 设置画布的高度
  const ctx = canvas === null || canvas === void 0 ? void 0 : canvas.getContext('2d');
  ctx === null || ctx === void 0 ? void 0 : ctx.drawImage(IMG, 0, 0, oWidth, oHeight);
  const smBase64 = canvas.toDataURL('image/png');
  return smBase64;
}
/**
 * 数据万象给的公式计算
 * ethanfzhang@tencent.com
 * https://cloud.tencent.com/document/product/460/6951
 */
function createTextCanvas(textMsg, textFontSize, degree, alpha, fillStyle, font) {
  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');
  ctx.save();
  // 数据万象给的公式
  // 中文1汉字 = 3个utf-8字节 字节宽度 * 字体大小 * 0.46倍数
  const textWidth = getUTF8CharLength(textMsg) * textFontSize * 0.46;

  // 高度 * 1.05 倍数
  const textHeight = textFontSize * 1.05;
  // 计算角度旋转的 弧度
  const circDegree = Math.PI * degree / 180;
  // 获得需要平铺的卡片宽 高
  const imgWidth = (Math.abs(Math.cos(circDegree)) * textWidth + Math.abs(Math.sin(circDegree)) * textHeight) * 1.1;
  // 获得需要平铺的卡片 高
  const imgHeight = (Math.abs(Math.sin(circDegree)) * textWidth + Math.abs(Math.cos(circDegree)) * textHeight) * 1.1;
  // 卡片前边距
  const paddingLeft = textWidth * 0.1;

  // 卡片上边距
  // let paddingTop = textHeight * 0.9;
  canvas.width = imgWidth;
  canvas.height = imgHeight;
  ctx.globalAlpha = alpha;
  ctx.fillStyle = fillStyle;
  ctx.font = `${textFontSize}px ${font}`;
  ctx.translate(paddingLeft, textFontSize);
  ctx === null || ctx === void 0 ? void 0 : ctx.rotate(circDegree);
  ctx === null || ctx === void 0 ? void 0 : ctx.fillText(textMsg, 0, 0);
  ctx.restore();
  return ctx.canvas;
}