victory-native
Version:
A charting library for React Native with a focus on performance and customization.
43 lines (42 loc) • 1.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMaxTextLayout = exports.getTextLayout = exports.getTextLines = void 0;
const getTextLines = (text) => text.split(/\r\n|\r|\n/);
exports.getTextLines = getTextLines;
const getTextLayout = (text, font) => {
var _a;
const lines = (0, exports.getTextLines)(text);
const fontSize = (_a = font === null || font === void 0 ? void 0 : font.getSize()) !== null && _a !== void 0 ? _a : 0;
const lineHeight = fontSize;
const width = Math.max(0, ...lines.map((line) => {
var _a, _b;
if (!font)
return 0;
const glyphIDs = font.getGlyphIDs(line);
const widths = (_b = (_a = font.getGlyphWidths) === null || _a === void 0 ? void 0 : _a.call(font, glyphIDs)) !== null && _b !== void 0 ? _b : [];
return widths.reduce((sum, value) => sum + value, 0);
}));
return {
lines,
width,
height: lines.length * lineHeight,
fontSize,
lineHeight,
};
};
exports.getTextLayout = getTextLayout;
const getMaxTextLayout = (labels, font) => {
var _a;
const fontSize = (_a = font === null || font === void 0 ? void 0 : font.getSize()) !== null && _a !== void 0 ? _a : 0;
const lineHeight = fontSize;
return labels.reduce((max, label) => {
const layout = (0, exports.getTextLayout)(label, font);
return {
width: Math.max(max.width, layout.width),
height: Math.max(max.height, layout.height),
fontSize: layout.fontSize,
lineHeight: layout.lineHeight,
};
}, { width: 0, height: 0, fontSize, lineHeight });
};
exports.getMaxTextLayout = getMaxTextLayout;