import { CanvasTextMetrics, TextStyle } from "pixi.js"; import { parseCommonObject } from "../util/parseCommonObject.js"; import { DEFAULT_TEXT_STYLE } from "../../../types.js"; import { toPixiTextStyle } from "../../../util/toPixiTextStyle.js"; import { mergeTextStyle } from "../../../util/mergeTextStyle.js"; import { createTextChunks, getRichTextSourceLevels, prepareRichTextSegments, } from "../text-revealing/parseTextRevealing.js"; import { createVerticalTextChunks, isVerticalWritingMode, normalizeWritingMode, } from "./verticalTextLayout.js"; import { applyBidiLayout, isRtlDirection, normalizeDirection, resolveDirectionalAlign, } from "./bidiTextLayout.js"; const RICH_TEXT_NO_WRAP_WIDTH = 100000; const getRichTextWordWrapWidth = (state, textStyle) => { if (typeof state.width === "number") { return state.width; } if ( textStyle.wordWrap && typeof textStyle.wordWrapWidth === "number" && Number.isFinite(textStyle.wordWrapWidth) && textStyle.wordWrapWidth > 0 ) { return textStyle.wordWrapWidth; } return RICH_TEXT_NO_WRAP_WIDTH; }; const getInteractionProps = (state) => ({ ...(state.hover && { hover: state.hover }), ...(state.click && { click: state.click }), ...(state.rightClick && { rightClick: state.rightClick }), ...(state.scrollUp && { scrollUp: state.scrollUp }), ...(state.scrollDown && { scrollDown: state.scrollDown }), }); // Layout facts the renderer needs but that are not part of the computed // node's public shape. const defineLayoutProperties = ( computedText, state, { layoutWidth, layoutHeight }, ) => { Object.defineProperties(computedText, { __anchorXRatio: { value: state.anchorX ?? 0, enumerable: false, }, __anchorYRatio: { value: state.anchorY ?? 0, enumerable: false, }, __fixedWidth: { value: typeof state.width === "number", enumerable: false, }, __layoutWidth: { value: layoutWidth, enumerable: false, }, __layoutHeight: { value: layoutHeight, enumerable: false, }, ...(typeof state.originX === "number" && { __explicitOriginX: { value: true, enumerable: true, }, }), ...(typeof state.originY === "number" && { __explicitOriginY: { value: true, enumerable: true, }, }), }); }; /** * Vertical text always goes through the rich layout: columns are positioned * part by part, so a plain string is treated as a single segment. Here * `height` is the wrap dimension and `width` is measured. */ const parseVerticalText = ({ state, baseTextStyle, textStyle, writingMode, }) => { const content = Array.isArray(state.content) ? state.content : [{ text: String(state.content ?? "") }]; const { chunks, width: measuredWidth, height: measuredHeight, } = createVerticalTextChunks( prepareRichTextSegments({ content, defaultTextStyle: baseTextStyle }), { columnLength: typeof state.height === "number" ? state.height : undefined, width: typeof state.width === "number" ? state.width : undefined, align: baseTextStyle.align, writingMode, }, ); const roundedMeasuredWidth = Math.round(measuredWidth); const layoutWidth = typeof state.width === "number" ? Math.round(state.width) : roundedMeasuredWidth; const layoutHeight = Math.round( typeof state.height === "number" ? state.height : measuredHeight, ); const computedText = { ...parseCommonObject({ ...state, width: layoutWidth, height: layoutHeight, }), content: chunks, writingMode, measuredWidth: roundedMeasuredWidth, textStyle: { ...textStyle, }, ...getInteractionProps(state), }; defineLayoutProperties(computedText, state, { layoutWidth, layoutHeight, }); return computedText; }; /** * @typedef {import('../../../types.js').BaseElement} BaseElement * @typedef {import('../../../types.js').TextComputedNode} TextComputedNode */ /** * Parse text object and calculate final position after anchor adjustment * @param {Object} params * @param {BaseElement} params.state - The text state to parse * @param {Array} params.parserPlugins - Array of parser plugins (not used by this parser) * @returns {TextComputedNode} */ export const parseText = ({ state }) => { const writingMode = normalizeWritingMode(state.writingMode); const direction = normalizeDirection(state.direction); // Right-to-left text is placed run by run, so it always takes the rich // layout; a plain string is one segment. Vertical text ignores `direction`. const rtl = isRtlDirection({ direction }); const contentIsRich = Array.isArray(state.content) || rtl; const baseTextStyle = mergeTextStyle(DEFAULT_TEXT_STYLE, state.textStyle); const textStyle = { ...baseTextStyle }; textStyle.lineHeight = Math.round(textStyle.fontSize * textStyle.lineHeight); if (isVerticalWritingMode({ writingMode })) { return parseVerticalText({ state, baseTextStyle, textStyle, writingMode }); } // Handle word wrap width based on element width if (typeof state.width === "number") { textStyle.wordWrapWidth = state.width; textStyle.wordWrap = true; } if (contentIsRich) { const wordWrapWidth = getRichTextWordWrapWidth(state, baseTextStyle); const content = Array.isArray(state.content) ? state.content : [{ text: String(state.content ?? "") }]; const processedContent = prepareRichTextSegments({ content, defaultTextStyle: baseTextStyle, rtl, width: typeof state.width === "number" ? state.width : undefined, }); // Use the same paragraph levels for line breaking and run placement. const sourceLevels = rtl ? getRichTextSourceLevels(processedContent, direction) : undefined; const logicalLayout = createTextChunks(processedContent, wordWrapWidth, { minimumWidth: 0, sourceLevels, }); const calculatedHeight = logicalLayout.height; const align = resolveDirectionalAlign(direction, state.textStyle?.align); const { chunks, width: calculatedWidth } = rtl ? applyBidiLayout(logicalLayout.chunks, { direction, width: typeof state.width === "number" ? state.width : undefined, align, sourceLevels, }) : logicalLayout; const roundedMeasuredWidth = Math.round(calculatedWidth); const roundedHeight = Math.round(calculatedHeight); const layoutWidth = typeof state.width === "number" ? Math.round(state.width) : roundedMeasuredWidth; let computedObj = parseCommonObject({ ...state, width: layoutWidth, height: roundedHeight, }); const computedText = { ...computedObj, content: chunks, ...(rtl && { direction }), measuredWidth: roundedMeasuredWidth, textStyle: { ...textStyle, // The alignment the lines were laid out with; right unless authored. ...(rtl && { align }), }, ...getInteractionProps(state), }; defineLayoutProperties(computedText, state, { layoutWidth, layoutHeight: roundedHeight, }); return computedText; } // Convert string content to string for measurement const contentString = String(state.content ?? ""); const { width, height } = CanvasTextMetrics.measureText( contentString, new TextStyle(toPixiTextStyle(textStyle, { includeShadow: false })), ); // Round pixel calculations const roundedMeasuredWidth = Math.round(width); const roundedHeight = Math.round(height); const layoutWidth = typeof state.width === "number" ? Math.round(state.width) : roundedMeasuredWidth; let computedObj = parseCommonObject({ ...state, width: layoutWidth, height: roundedHeight, }); const computedText = { ...computedObj, content: contentString, measuredWidth: roundedMeasuredWidth, textStyle: { ...textStyle, }, ...getInteractionProps(state), }; defineLayoutProperties(computedText, state, { layoutWidth, layoutHeight: roundedHeight, }); return computedText; };