v-regexp
Version:
JavaScript Regular Expression Parser and Visualizer.
100 lines • 3.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.plural = exports.translate = exports.elideOK = exports.getHighlightText = exports.onlyCharClass = exports.charsetEscape = exports.getCharSize = exports.getTemplateText = void 0;
var tslib_1 = require("tslib");
var constants_1 = require("../constants");
var Kit_1 = tslib_1.__importDefault(require("../Kit"));
/**
* 获取 rapheal 文本模版
* @param paper
* @param theme 主题
*/
function getTemplateText(paper, theme) {
var templateText = paper.text(-1000, -1000, 'XgfTlM|.q\nXgfTlM|.q').attr({
'font-family': theme.fontFamily,
'font-size': theme.nodeFontSize,
});
return templateText;
}
exports.getTemplateText = getTemplateText;
var charSizeCache = {};
function getCharSize(_a) {
var fontSize = _a.fontSize, _b = _a.fontBold, fontBold = _b === void 0 ? 'normal' : _b, templateText = _a.templateText;
if (charSizeCache[fontSize] && charSizeCache[fontSize][fontBold]) {
return charSizeCache[fontSize][fontBold];
}
templateText.attr({
'font-size': fontSize,
'font-weight': fontBold,
});
var box = templateText.getBBox();
charSizeCache[fontSize] = charSizeCache[fontSize] || {};
charSizeCache[fontSize][fontBold] = {
width: box.width / ((templateText.attr('text').length - 1) / 2),
height: box.height / 2,
};
return charSizeCache[fontSize][fontBold];
}
exports.getCharSize = getCharSize;
function charsetEscape(str, isRaw) {
var resultStr = Kit_1.default.toPrint(str, isRaw);
return resultStr.replace(/\[/g, '\\[').replace(/\]/g, '\\]');
}
exports.charsetEscape = charsetEscape;
function onlyCharClass(node) {
var _a, _b;
return !node.chars && !((_a = node.ranges) === null || _a === void 0 ? void 0 : _a.length) && ((_b = node.classes) === null || _b === void 0 ? void 0 : _b.length) === 1;
}
exports.onlyCharClass = onlyCharClass;
function getHighlightText(_a) {
var str = _a.str, color = _a.color, theme = _a.theme;
var colorResult = color || theme.highlightColor[str] || theme.highlightColor.defaults;
return {
type: 'text',
'font-size': theme.nodeFontSize,
'font-family': theme.fontFamily,
text: str,
fill: colorResult,
'text-anchor': theme.textAnchor,
'font-weight': theme.fontWeight,
};
}
exports.getHighlightText = getHighlightText;
function elideOK(node) {
if (Array.isArray(node)) {
for (var i = 0; i < node.length; i++) {
if (!elideOK(node[i])) {
return false;
}
}
return true;
}
if (node.type === constants_1.EMPTY_NODE) {
return true;
}
if (node.type === constants_1.GROUP_NODE && node.num === undefined) {
return elideOK(node.sub);
}
if (node.type === constants_1.CHOICE_NODE) {
return elideOK(node.branches);
}
return false;
}
exports.elideOK = elideOK;
function translate(items, dx, dy) {
items.forEach(function (item) {
if (item._translate) {
item._translate(dx, dy);
}
else {
item.x += dx;
item.y += dy;
}
});
}
exports.translate = translate;
function plural(n) {
return n + (n < 2 ? ' time' : ' times');
}
exports.plural = plural;
//# sourceMappingURL=common.js.map