"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyToText = exports.textOf = exports.getFont = exports.measureTextWidth = void 0; var util_1 = require("@antv/util"); var ctx; /** * 计算文本在画布中的宽度 */ exports.measureTextWidth = (0, util_1.memoize)(function (text, font) { var content = (0, util_1.isString)(text) ? text : text.style.text.toString(); var _a = font || (0, exports.getFont)(text), fontSize = _a.fontSize, fontFamily = _a.fontFamily, fontWeight = _a.fontWeight, fontStyle = _a.fontStyle, fontVariant = _a.fontVariant; if (!ctx) { ctx = document.createElement('canvas').getContext('2d'); } ctx.font = [fontStyle, fontVariant, fontWeight, "".concat(fontSize, "px"), fontFamily].join(' '); return ctx.measureText(content).width; }, function (text, font) { return [(0, util_1.isString)(text) ? text : text.style.text.toString(), Object.values(font || (0, exports.getFont)(text)).join()].join(''); }); var getFont = function (textShape) { var fontFamily = textShape.style.fontFamily || 'sans-serif'; var fontWeight = textShape.style.fontWeight || 'normal'; var fontStyle = textShape.style.fontStyle || 'normal'; var fontVariant = textShape.style.fontVariant; var fontSize = textShape.style.fontSize; fontSize = typeof fontSize === 'object' ? fontSize.value : fontSize; return { fontSize: fontSize, fontFamily: fontFamily, fontWeight: fontWeight, fontStyle: fontStyle, fontVariant: fontVariant }; }; exports.getFont = getFont; function textOf(node) { if (node.nodeName === 'text') { return node; } if (node.nodeName === 'g' && node.children.length === 1 && node.children[0].nodeName === 'text') { return node.children[0]; } return null; } exports.textOf = textOf; function applyToText(node, style) { var text = textOf(node); if (text) text.attr(style); } exports.applyToText = applyToText; //# sourceMappingURL=text.js.map