measure-text.js 1.1 KB

123456789101112131415161718192021222324
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.measureTextWidth = void 0;
  4. var tslib_1 = require("tslib");
  5. var util_1 = require("@antv/util");
  6. var context_1 = require("./context");
  7. /**
  8. * 计算文本在画布中的宽度
  9. * @param text 文本
  10. * @param font 字体
  11. */
  12. exports.measureTextWidth = (0, util_1.memoize)(function (text, font) {
  13. if (font === void 0) { font = {}; }
  14. var fontSize = font.fontSize, _a = font.fontFamily, fontFamily = _a === void 0 ? 'sans-serif' : _a, fontWeight = font.fontWeight, fontStyle = font.fontStyle, fontVariant = font.fontVariant;
  15. var ctx = (0, context_1.getCanvasContext)();
  16. // @see https://developer.mozilla.org/zh-CN/docs/Web/CSS/font
  17. ctx.font = [fontStyle, fontWeight, fontVariant, "".concat(fontSize, "px"), fontFamily].join(' ');
  18. var metrics = ctx.measureText((0, util_1.isString)(text) ? text : '');
  19. return metrics.width;
  20. }, function (text, font) {
  21. if (font === void 0) { font = {}; }
  22. return tslib_1.__spreadArray([text], (0, util_1.values)(font), true).join('');
  23. });
  24. //# sourceMappingURL=measure-text.js.map