|
1 | 1 | import scale from '../run/scale'; |
2 | 2 | import resolveStringIndices from '../string-indices/resolve'; |
3 | 3 | import resolveGlyphIndices from '../glyph-indices/resolve'; |
4 | | -import { AttributedString, Position, Run } from '../types'; |
| 4 | +import { AttributedString, Glyph, Position, Run } from '../types'; |
| 5 | + |
| 6 | +const codePointsFromString = (string: string): number[] => { |
| 7 | + const result: number[] = []; |
| 8 | + |
| 9 | + for (const char of string) { |
| 10 | + const codePoint = char.codePointAt(0); |
| 11 | + |
| 12 | + if (codePoint !== undefined) result.push(codePoint); |
| 13 | + } |
| 14 | + |
| 15 | + return result; |
| 16 | +}; |
| 17 | + |
| 18 | +const sequenceStartsAt = ( |
| 19 | + values: number[], |
| 20 | + sequence: number[], |
| 21 | + start: number, |
| 22 | +) => sequence.every((value, index) => values[start + index] === value); |
| 23 | + |
| 24 | +const findSequence = (values: number[], sequence: number[], start: number) => { |
| 25 | + if (sequence.length === 0) return start; |
| 26 | + |
| 27 | + for (let i = start; i <= values.length - sequence.length; i += 1) { |
| 28 | + if (sequenceStartsAt(values, sequence, i)) return i; |
| 29 | + } |
| 30 | + |
| 31 | + return -1; |
| 32 | +}; |
| 33 | + |
| 34 | +const cloneGlyph = (glyph: Glyph, codePoints: number[]): Glyph => |
| 35 | + Object.assign(Object.create(Object.getPrototypeOf(glyph)), glyph, { |
| 36 | + codePoints, |
| 37 | + }); |
| 38 | + |
| 39 | +const assignPendingCodePoints = ( |
| 40 | + glyphs: Glyph[], |
| 41 | + pendingGlyphs: number[], |
| 42 | + codePoints: number[], |
| 43 | +) => { |
| 44 | + if (pendingGlyphs.length === 0 || codePoints.length === 0) return; |
| 45 | + |
| 46 | + let codePointIndex = 0; |
| 47 | + |
| 48 | + for (let i = 0; i < pendingGlyphs.length; i += 1) { |
| 49 | + const remainingGlyphs = pendingGlyphs.length - i; |
| 50 | + const remainingCodePoints = codePoints.length - codePointIndex; |
| 51 | + const length = Math.max(1, remainingCodePoints - remainingGlyphs + 1); |
| 52 | + const glyphIndex = pendingGlyphs[i]; |
| 53 | + |
| 54 | + glyphs[glyphIndex] = cloneGlyph( |
| 55 | + glyphs[glyphIndex], |
| 56 | + codePoints.slice(codePointIndex, codePointIndex + length), |
| 57 | + ); |
| 58 | + |
| 59 | + codePointIndex += length; |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +const normalizeGlyphCodePoints = (glyphs: Glyph[], string: string): Glyph[] => { |
| 64 | + const codePoints = codePointsFromString(string); |
| 65 | + const result = [...glyphs]; |
| 66 | + const pendingGlyphs: number[] = []; |
| 67 | + let isAligned = true; |
| 68 | + let cursor = 0; |
| 69 | + |
| 70 | + for (let i = 0; i < glyphs.length; i += 1) { |
| 71 | + const glyph = glyphs[i]; |
| 72 | + const glyphCodePoints = glyph.codePoints || []; |
| 73 | + |
| 74 | + if (!isAligned) continue; |
| 75 | + |
| 76 | + if (glyphCodePoints.length === 0) { |
| 77 | + pendingGlyphs.push(i); |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + if (pendingGlyphs.length > 0) { |
| 82 | + const foundIndex = findSequence(codePoints, glyphCodePoints, cursor); |
| 83 | + |
| 84 | + if (foundIndex < cursor) { |
| 85 | + pendingGlyphs.length = 0; |
| 86 | + isAligned = false; |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + assignPendingCodePoints( |
| 91 | + result, |
| 92 | + pendingGlyphs, |
| 93 | + codePoints.slice(cursor, foundIndex), |
| 94 | + ); |
| 95 | + |
| 96 | + pendingGlyphs.length = 0; |
| 97 | + cursor = foundIndex + glyphCodePoints.length; |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + if (sequenceStartsAt(codePoints, glyphCodePoints, cursor)) { |
| 102 | + cursor += glyphCodePoints.length; |
| 103 | + } else { |
| 104 | + isAligned = false; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (isAligned) { |
| 109 | + assignPendingCodePoints(result, pendingGlyphs, codePoints.slice(cursor)); |
| 110 | + } |
| 111 | + |
| 112 | + return result; |
| 113 | +}; |
5 | 114 |
|
6 | 115 | const getCharacterSpacing = (run: Run) => { |
7 | 116 | return run.attributes?.characterSpacing || 0; |
@@ -67,16 +176,17 @@ const layoutRun = (string: string) => { |
67 | 176 | 'ltr', |
68 | 177 | ); |
69 | 178 |
|
| 179 | + const glyphs = normalizeGlyphCodePoints(glyphRun.glyphs, runString); |
70 | 180 | const positions = scalePositions(run, glyphRun.positions); |
71 | | - const stringIndices = resolveStringIndices(glyphRun.glyphs); |
72 | | - const glyphIndices = resolveGlyphIndices(glyphRun.glyphs); |
| 181 | + const stringIndices = resolveStringIndices(glyphs); |
| 182 | + const glyphIndices = resolveGlyphIndices(glyphs); |
73 | 183 |
|
74 | 184 | const result: Run = { |
75 | 185 | ...run, |
76 | 186 | positions, |
77 | 187 | stringIndices, |
78 | 188 | glyphIndices, |
79 | | - glyphs: glyphRun.glyphs, |
| 189 | + glyphs, |
80 | 190 | }; |
81 | 191 |
|
82 | 192 | return result; |
|
0 commit comments