export interface TextMeasurementOptions { text: string; fontFamily: string; fontSize: number; fontWeight?: string | number; fontStyle?: string; lineHeight?: number; width?: number; letterSpacing?: number; paragraphSpacing?: number; paragraphIndent?: number; textCase?: 'uppercase' | 'lowercase' | 'capitalize' | 'none'; segments?: any[]; enableSegments?: boolean; hyphenation?: boolean; composer?: 'single-line' | 'every-line'; balanceRaggedLines?: boolean; } export function measureText(options: TextMeasurementOptions) { const { text, fontSize = 16, lineHeight = 1.2, width = 200, } = options; let textStr = String(text || ""); const rawLines = textStr.split(/\r\n|\r|\n/); // In React Native Skia, real text measurement is done via Skia Font objects, // so this is just a stub for structural layout when Skia isn't directly available. // We assume an average character width of (fontSize * 0.6) for wrapping estimation. const charWidth = fontSize * 0.6; let finalLines: { text: string, width: number }[] = []; if (width >= 100000) { finalLines = rawLines.map(line => ({ text: line, width: line.length * charWidth })); } else { const maxCharsPerLine = Math.max(1, Math.floor(width / charWidth)); rawLines.forEach(line => { let currentLine = ""; const words = line.split(" "); words.forEach(word => { if ((currentLine + word).length > maxCharsPerLine && currentLine.length > 0) { finalLines.push({ text: currentLine.trim(), width: currentLine.trim().length * charWidth }); currentLine = word + " "; } else { currentLine += word + " "; } }); if (currentLine.trim().length > 0 || words.length === 0) { finalLines.push({ text: currentLine.trim(), width: currentLine.trim().length * charWidth }); } }); } let finalLineHeight = lineHeight; if (typeof finalLineHeight === 'string') finalLineHeight = parseFloat(finalLineHeight); if (finalLineHeight > 5 && fontSize > 0) finalLineHeight = finalLineHeight / fontSize; const totalHeight = finalLines.length * finalLineHeight * fontSize; return { width: width, height: totalHeight, lineCount: finalLines.length, lines: finalLines }; }