|
| 1 | +import type { TTSProgressSource } from '@/hooks/useTTS' |
| 2 | + |
| 3 | +export const computeContinuousPage = (absoluteOffset: number, textLength: number, totalPages: number): number => |
| 4 | + absoluteOffset / textLength * totalPages + 1 |
| 5 | + |
| 6 | +export const computePageTurnOffset = (pageStartOffset: number | null, pageEndOffset: number | null, lead: number): number | null => |
| 7 | + pageEndOffset === null ? null : Math.max(pageStartOffset ?? 0, pageEndOffset - lead) |
| 8 | + |
| 9 | +export const computePageEntryGuard = (pageStartOffset: number | null, pageSpan: number | null, maxGuard: number): number => |
| 10 | + pageStartOffset !== null ? Math.min(maxGuard, Math.max(12, Math.floor((pageSpan ?? maxGuard) * 0.22))) : 0 |
| 11 | + |
| 12 | +export type ShouldAdvanceParams = { |
| 13 | + absoluteOffset: number |
| 14 | + pageStartOffset: number | null |
| 15 | + pageEndOffset: number | null |
| 16 | + pageTurnOffset: number | null |
| 17 | + continuousPage: number |
| 18 | + currentPage: number |
| 19 | + source: TTSProgressSource |
| 20 | + outsidePage: boolean |
| 21 | + pageEntryGuard: number |
| 22 | +} |
| 23 | + |
| 24 | +export type ShouldAdvanceResult = { |
| 25 | + shouldAdvance: boolean |
| 26 | + reason: 'page-end-cfi' | 'progress-fallback' | 'none' |
| 27 | + beforeCurrentPageGuard: boolean |
| 28 | + insideNewPageGuard: boolean |
| 29 | +} |
| 30 | + |
| 31 | +// 完整搬移自 Reader.tsx 舊版 followTTSRange 的翻頁判斷區塊,邏輯不變。 |
| 32 | +export const shouldAdvanceTTSPage = (params: ShouldAdvanceParams): ShouldAdvanceResult => { |
| 33 | + const { absoluteOffset, pageStartOffset, pageEndOffset, pageTurnOffset, continuousPage, currentPage, source, outsidePage, pageEntryGuard } = params |
| 34 | + const distanceToPageEnd = pageTurnOffset === null ? null : pageTurnOffset - absoluteOffset |
| 35 | + const beforeCurrentPageGuard = pageStartOffset !== null && absoluteOffset < pageStartOffset - 2 |
| 36 | + const insideNewPageGuard = |
| 37 | + pageStartOffset !== null && |
| 38 | + absoluteOffset >= pageStartOffset && |
| 39 | + absoluteOffset < pageStartOffset + pageEntryGuard |
| 40 | + const progressShouldAdvance = |
| 41 | + !beforeCurrentPageGuard && |
| 42 | + !insideNewPageGuard && |
| 43 | + pageEndOffset === null && ( |
| 44 | + outsidePage || |
| 45 | + (source === 'boundary' && continuousPage >= currentPage + 1.05) || |
| 46 | + (continuousPage >= currentPage + 1.12) |
| 47 | + ) |
| 48 | + const measuredPageEndReached = |
| 49 | + !beforeCurrentPageGuard && |
| 50 | + pageEndOffset !== null && |
| 51 | + distanceToPageEnd !== null && |
| 52 | + distanceToPageEnd <= 0 |
| 53 | + |
| 54 | + if (measuredPageEndReached) return { shouldAdvance: true, reason: 'page-end-cfi', beforeCurrentPageGuard, insideNewPageGuard } |
| 55 | + if (progressShouldAdvance) return { shouldAdvance: true, reason: 'progress-fallback', beforeCurrentPageGuard, insideNewPageGuard } |
| 56 | + return { shouldAdvance: false, reason: 'none', beforeCurrentPageGuard, insideNewPageGuard } |
| 57 | +} |
| 58 | + |
| 59 | +export const computeApproxOffsetFromPercentage = ( |
| 60 | + percentage: number | undefined, |
| 61 | + currentPageIdx: number, |
| 62 | + totalPages: number, |
| 63 | + textLength: number, |
| 64 | +): number => |
| 65 | + (typeof percentage === 'number' && percentage > 0) |
| 66 | + ? Math.floor(percentage * textLength) |
| 67 | + : Math.floor(currentPageIdx / totalPages * textLength) |
| 68 | + |
| 69 | +// 在全文中搜尋 sample 文字樣本,選擇距離 approx 最近的符合位置;找不到回傳 null。 |
| 70 | +export const locateApproxOffsetByFuzzyMatch = (fullText: string, approx: number, sample: string): number | null => { |
| 71 | + if (sample.length < 3) return null |
| 72 | + const normalFull = fullText.replace(/\s+/g, '') |
| 73 | + const approxNorm = Math.round(approx / fullText.length * normalFull.length) |
| 74 | + let bestNormIdx = -1 |
| 75 | + let bestDist = Infinity |
| 76 | + let pos = 0 |
| 77 | + while (true) { |
| 78 | + const idx = normalFull.indexOf(sample, pos) |
| 79 | + if (idx < 0) break |
| 80 | + const dist = Math.abs(idx - approxNorm) |
| 81 | + if (dist < bestDist) { bestDist = dist; bestNormIdx = idx } |
| 82 | + pos = idx + 1 |
| 83 | + } |
| 84 | + if (bestNormIdx < 0) return null |
| 85 | + |
| 86 | + let normCount = 0 |
| 87 | + for (let i = 0; i < fullText.length; i++) { |
| 88 | + if (!/\s/.test(fullText[i])) { |
| 89 | + if (normCount === bestNormIdx) return i |
| 90 | + normCount++ |
| 91 | + } |
| 92 | + } |
| 93 | + return null |
| 94 | +} |
| 95 | + |
| 96 | +export const snapOffsetToWordStart = (fullText: string, approx: number): number => { |
| 97 | + let startOffset = approx |
| 98 | + while (startOffset > 0 && !/[\s\n]/.test(fullText[startOffset - 1])) startOffset-- |
| 99 | + return startOffset |
| 100 | +} |
0 commit comments