// SPDX-License-Identifier: AGPL-3.0-only // Ported from Folia (https://github.com/chthollyphile/folia-major, v0.8.2), src/utils/lyrics/graphemeTiming.ts, // wordSegmentation.ts and textDirection.ts: a line taken apart into letters and words, each with its share of time. import type { Line, Word } from '../types'; export interface GraphemeTiming { char: string; startTime: number; endTime: number; wordIndex?: number; } const graphemeSegmenter = typeof Intl !== 'undefined' && Intl.Segmenter ? new Intl.Segmenter(undefined, { granularity: 'grapheme' }) : null; export const splitGraphemes = (text: string): string[] => { if (!text) return []; if (graphemeSegmenter) return Array.from(graphemeSegmenter.segment(text), ({ segment }) => segment); return Array.from(text); }; export const CJK_REGEX = /[一-龥぀-ヿ가-힯]/; export const isCJK = (text: string) => CJK_REGEX.test(text); const buildEvenGraphemeTimings = (text: string, startTime: number, endTime: number, wordIndex?: number): GraphemeTiming[] => { const graphemes = splitGraphemes(text); if (graphemes.length === 0) return []; const duration = Math.max(endTime - startTime, 0); const unitDuration = duration / graphemes.length; return graphemes.map((char, index) => ({ char, startTime: startTime + unitDuration * index, endTime: index === graphemes.length - 1 ? endTime : startTime + unitDuration * (index + 1), ...(typeof wordIndex === 'number' ? { wordIndex } : {}), })); }; export const buildWordGraphemeTimings = (word: Word, wordIndex?: number): GraphemeTiming[] => ( buildEvenGraphemeTimings(word.text, word.startTime, word.endTime, wordIndex) ); const findGraphemeSequence = (source: string[], target: string[], fromIndex: number): number => { if (target.length === 0) return fromIndex; for (let index = fromIndex; index <= source.length - target.length; index += 1) { let matched = true; for (let targetIndex = 0; targetIndex < target.length; targetIndex += 1) { if (source[index + targetIndex] !== target[targetIndex]) { matched = false; break; } } if (matched) return index; } return -1; }; /** Maps word timing back onto the whole displayed line, spaces and punctuation the words leave out included. */ export const buildLineGraphemeTimeline = (line: Line): GraphemeTiming[] => { const lineGraphemes = splitGraphemes(line.fullText); if (lineGraphemes.length === 0) return []; if (line.words.length === 0) return buildEvenGraphemeTimings(line.fullText, line.startTime, line.endTime); const timeline: GraphemeTiming[] = []; let cursor = 0; let lastResolvedTime = line.startTime; line.words.forEach((word, wordIndex) => { const wordGraphemes = splitGraphemes(word.text); if (wordGraphemes.length === 0) return; const matchedStart = findGraphemeSequence(lineGraphemes, wordGraphemes, cursor); const start = matchedStart >= 0 ? matchedStart : cursor; const end = Math.min(start + wordGraphemes.length, lineGraphemes.length); for (let gapIndex = cursor; gapIndex < start; gapIndex += 1) { timeline[gapIndex] = { char: lineGraphemes[gapIndex], startTime: word.startTime, endTime: word.startTime }; } const wordTimings = buildWordGraphemeTimings(word, wordIndex); for (let localIndex = 0; localIndex < end - start; localIndex += 1) { const timing = wordTimings[localIndex]; if (!timing) continue; timeline[start + localIndex] = { ...timing, char: lineGraphemes[start + localIndex] }; lastResolvedTime = Math.max(lastResolvedTime, timing.endTime); } cursor = Math.max(cursor, end); }); for (let index = 0; index < lineGraphemes.length; index += 1) { if (timeline[index]) continue; timeline[index] = { char: lineGraphemes[index], startTime: lastResolvedTime, endTime: lastResolvedTime }; } return timeline; }; export interface LyricWordSegment { segment: string; /** Offset of this segment inside the source text, in code units. */ index: number; isWordLike: boolean; } const PUNCTUATION_ONLY = /^[\s\p{P}\p{S}]+$/u; const isWordLikeText = (text: string) => !PUNCTUATION_ONLY.test(text); const wordSegmenter = typeof Intl !== 'undefined' && Intl.Segmenter ? new Intl.Segmenter(undefined, { granularity: 'word' }) : null; /** Intl.Segmenter word split; by graphemes where the runtime has no Segmenter, so offsets stay valid. */ export const segmentTextWords = (text: string): LyricWordSegment[] => { if (!text) return []; if (wordSegmenter) { return Array.from(wordSegmenter.segment(text), part => ({ segment: part.segment, index: part.index, isWordLike: part.isWordLike ?? isWordLikeText(part.segment), })); } let cursor = 0; return splitGraphemes(text).map(segment => { const part = { segment, index: cursor, isWordLike: isWordLikeText(segment) }; cursor += segment.length; return part; }); }; export type LyricTextDirection = 'ltr' | 'rtl'; const RTL_LETTER_RE = /[\p{Script=Arabic}\p{Script=Hebrew}\p{Script=Syriac}\p{Script=Thaana}\p{Script=Nko}\p{Script=Samaritan}\p{Script=Mandaic}\p{Script=Adlam}]/u; const LETTER_RE = /\p{L}/u; /** The base direction of one lyric line, from its first letter. */ export const resolveLyricTextDirection = (text: string): LyricTextDirection => { for (const char of text) { if (!LETTER_RE.test(char)) continue; return RTL_LETTER_RE.test(char) ? 'rtl' : 'ltr'; } return 'ltr'; };