-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathoffset.ts
More file actions
33 lines (26 loc) · 635 Bytes
/
offset.ts
File metadata and controls
33 lines (26 loc) · 635 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { Run } from '../types';
/**
* Get ligature offset by index
*
* Ex. ffi ligature
*
* glyphs: l o f f i m
* stringIndices: 0 1 2 2 2 3
* offset: 0 0 0 1 2 0
*
* @param index
* @param run - Run
* @returns Ligature offset
*/
const offset = (index: number, run: Run) => {
if (!run) return 0;
const stringIndices = run.stringIndices || [];
const value = stringIndices[index];
if (value === undefined) return 0;
let result = 0;
for (let i = index - 1; i >= 0 && stringIndices[i] === value; i -= 1) {
result += 1;
}
return result;
};
export default offset;