victory-native
Version:
A charting library for React Native with a focus on performance and customization.
55 lines (54 loc) • 2.96 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createParagraphLabelRenderer = void 0;
const react_1 = __importDefault(require("react"));
const react_native_skia_1 = require("@shopify/react-native-skia");
const DEFAULT_PARAGRAPH_LAYOUT_WIDTH = 1000000;
const normalizeDimension = (value) => Number.isFinite(value) ? Math.max(0, value !== null && value !== void 0 ? value : 0) : 0;
const createParagraphLabelRenderer = ({ paragraphStyle, textStyle, typefaceFontProvider, maxWidth, } = {}) => {
const resolvedTypefaceFontProvider = typefaceFontProvider !== null && typefaceFontProvider !== void 0 ? typefaceFontProvider : react_native_skia_1.Skia.TypefaceFontProvider.Make();
const getLayoutWidth = (args) => {
const width = typeof maxWidth === "function" ? maxWidth(args) : maxWidth;
return normalizeDimension(width !== null && width !== void 0 ? width : DEFAULT_PARAGRAPH_LAYOUT_WIDTH);
};
const buildParagraph = (args, color) => {
const builder = react_native_skia_1.Skia.ParagraphBuilder.Make(paragraphStyle !== null && paragraphStyle !== void 0 ? paragraphStyle : {}, resolvedTypefaceFontProvider);
const resolvedTextStyle = color && !(textStyle === null || textStyle === void 0 ? void 0 : textStyle.color)
? Object.assign(Object.assign({}, textStyle), { color: react_native_skia_1.Skia.Color(color) }) : textStyle;
builder.pushStyle(resolvedTextStyle !== null && resolvedTextStyle !== void 0 ? resolvedTextStyle : {});
builder.addText(args.text);
builder.pop();
const paragraph = builder.build();
const layoutWidth = getLayoutWidth(args);
paragraph.layout(layoutWidth);
return {
paragraph,
layoutWidth,
};
};
const measure = (args) => {
const { paragraph, layoutWidth } = buildParagraph(args);
const longestLine = normalizeDimension(paragraph.getLongestLine());
const maxIntrinsicWidth = normalizeDimension(paragraph.getMaxIntrinsicWidth());
const measuredWidth = Math.max(longestLine, maxIntrinsicWidth);
const height = normalizeDimension(paragraph.getHeight());
return {
width: maxWidth === undefined ? measuredWidth : layoutWidth,
height,
fontSize: normalizeDimension(textStyle === null || textStyle === void 0 ? void 0 : textStyle.fontSize),
lineHeight: height,
};
};
const render = (args) => {
const { paragraph } = buildParagraph(args, args.color);
return (<react_native_skia_1.Paragraph paragraph={paragraph} x={args.x} y={args.y} width={args.width}/>);
};
return {
measure,
render,
};
};
exports.createParagraphLabelRenderer = createParagraphLabelRenderer;