File size: 5,697 Bytes
391a73c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/// <reference types="node" />
import { Glyph as Glyph$1 } from 'fontkit';
import { Font } from '@react-pdf/font';
export { Font } from '@react-pdf/font';

type Factor = {
    before: number;
    after: number;
    priority?: number;
    unconstrained?: boolean;
};

type Coordinate = {
    x: number;
    y: number;
};
type Rect = {
    x: number;
    y: number;
    width: number;
    height: number;
};
type Container = Rect & {
    truncateMode?: 'ellipsis';
    maxLines?: number;
    excludeRects?: Rect[];
};
type Glyph = Glyph$1;
type Position = {
    xAdvance: number;
    yAdvance: number;
    xOffset: number;
    yOffset: number;
    advanceWidth?: number;
};
type Attachment = {
    x?: number;
    y?: number;
    width: number;
    height: number;
    xOffset?: number;
    yOffset?: number;
    image: Buffer;
};
type Attributes = {
    align?: string;
    alignLastLine?: string;
    attachment?: Attachment;
    backgroundColor?: string;
    bidiLevel?: number;
    bullet?: unknown;
    characterSpacing?: number;
    color?: string;
    direction?: 'rtl' | 'ltr';
    features?: unknown[];
    fill?: boolean;
    font?: Font[];
    fontSize?: number;
    hangingPunctuation?: boolean;
    hyphenationFactor?: number;
    indent?: number;
    justificationFactor?: number;
    lineHeight?: number;
    lineSpacing?: number;
    link?: string;
    margin?: number;
    marginLeft?: number;
    marginRight?: number;
    opacity?: number;
    padding?: number;
    paddingTop?: number;
    paragraphSpacing?: number;
    scale?: number;
    script?: unknown;
    shrinkFactor?: number;
    strike?: boolean;
    strikeColor?: string;
    strikeStyle?: string;
    stroke?: boolean;
    underline?: boolean;
    underlineColor?: string;
    underlineStyle?: string;
    verticalAlign?: string;
    wordSpacing?: number;
    yOffset?: number;
};
type Run = {
    start: number;
    end: number;
    attributes: Attributes;
    /** Maps each string codepoint index to its corresponding glyph index */
    stringIndices?: number[];
    /** Maps each glyph index to its corresponding string codepoint index */
    glyphIndices?: number[];
    glyphs?: Glyph[];
    positions?: Position[];
    xAdvance?: number;
    height?: number;
    descent?: number;
};
type DecorationLine = {
    rect: Rect;
    opacity: number;
    color: string;
    style: string;
};
type AttributedString = {
    string: string;
    syllables?: string[];
    runs: Run[];
    box?: Rect;
    decorationLines?: DecorationLine[];
    overflowLeft?: number;
    overflowRight?: number;
    xAdvance?: number;
    ascent?: number;
};
type Fragment = {
    string: string;
    attributes?: Attributes;
};
type Paragraph = AttributedString[];
type LayoutOptions = {
    hyphenationCallback?: (word: string | null, fallback: (word: string | null) => string[]) => string[];
    tolerance?: number;
    hyphenationPenalty?: number;
    expandCharFactor?: Factor;
    shrinkCharFactor?: Factor;
    expandWhitespaceFactor?: Factor;
    shrinkWhitespaceFactor?: Factor;
};

declare const bidiEngine: () => (attributedString: AttributedString) => AttributedString;

/**
 * Performs Knuth & Plass line breaking algorithm
 * Fallbacks to best fit algorithm if latter not successful
 *
 * @param options - Layout options
 */
declare const linebreaker: (options: LayoutOptions) => (attributedString: AttributedString, availableWidths: number[]) => AttributedString[];

/**
 * A JustificationEngine is used by a Typesetter to perform line fragment
 * justification. This implementation is based on a description of Apple's
 * justification algorithm from a PDF in the Apple Font Tools package.
 *
 * @param options - Layout options
 */
declare const justification: (options: LayoutOptions) => (line: AttributedString) => AttributedString;

declare const fontSubstitution: () => ({ string, runs }: AttributedString) => AttributedString;

/**
 * A TextDecorationEngine is used by a Typesetter to generate
 * DecorationLines for a line fragment, including underlines
 * and strikes.
 */
declare const textDecoration: () => (line: AttributedString) => AttributedString;

/**
 * Resolves unicode script in runs, grouping equal runs together
 */
declare const scriptItemizer: () => (attributedString: AttributedString) => AttributedString;

declare const wordHyphenation: () => (word: string | null) => string[];

type Engines = {
    bidi: typeof bidiEngine;
    linebreaker: typeof linebreaker;
    justification: typeof justification;
    fontSubstitution: typeof fontSubstitution;
    scriptItemizer: typeof scriptItemizer;
    textDecoration: typeof textDecoration;
    wordHyphenation?: typeof wordHyphenation;
};

/**
 * A LayoutEngine is the main object that performs text layout.
 * It accepts an AttributedString and a Container object
 * to layout text into, and uses several helper objects to perform
 * various layout tasks. These objects can be overridden to customize
 * layout behavior.
 */
declare const layoutEngine: (engines: Engines) => (attributedString: AttributedString, container: Container, options?: LayoutOptions) => Paragraph[];

/**
 * Create attributed string from text fragments
 *
 * @param fragments - Fragments
 * @returns Attributed string
 */
declare const fromFragments: (fragments: Fragment[]) => AttributedString;

export { type Attachment, type AttributedString, type Attributes, type Container, type Coordinate, type DecorationLine, type Fragment, type Glyph, type LayoutOptions, type Paragraph, type Position, type Rect, type Run, bidiEngine as bidi, layoutEngine as default, fontSubstitution, fromFragments, justification, linebreaker, scriptItemizer, textDecoration, wordHyphenation };