import { isString, memoize } from '@antv/util'; var ctx; /** * 计算文本在画布中的宽度 */ export var measureTextWidth = memoize(function (text, font) { var content = isString(text) ? text : text.style.text.toString(); var _a = font || 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 [isString(text) ? text : text.style.text.toString(), Object.values(font || getFont(text)).join()].join(''); }); export 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 }; }; export 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; } export function applyToText(node, style) { var text = textOf(node); if (text) text.attr(style); } //# sourceMappingURL=text.js.map