@aboutgeo/pdiiif 0.0.0-stage → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +169 -2
- package/dist/__tests__/parser.test.d.ts +1 -0
- package/dist/__tests__/pdf.spec.d.ts +1 -0
- package/dist/__tests__/region.test.d.ts +1 -0
- package/dist/__tests__/setupJest.d.ts +0 -0
- package/dist/assets/optimization.worker-C8u4PFfM.js +886 -0
- package/dist/assets/optimization.worker-C8u4PFfM.js.map +1 -0
- package/dist/convert.d.ts +223 -0
- package/dist/download.d.ts +115 -0
- package/dist/iiif.d.ts +110 -0
- package/dist/index.cjs +65 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.mjs +11137 -0
- package/dist/index.mjs.map +1 -0
- package/dist/io.d.ts +75 -0
- package/dist/log.d.ts +21 -0
- package/dist/metrics.d.ts +10 -0
- package/dist/ocr.d.ts +36 -0
- package/dist/optimization.d.ts +48 -0
- package/dist/optimization.worker.d.ts +1 -0
- package/dist/pdf/annos-text.d.ts +19 -0
- package/dist/pdf/annos.d.ts +4 -0
- package/dist/pdf/common.d.ts +48 -0
- package/dist/pdf/generator.d.ts +197 -0
- package/dist/pdf/image.d.ts +34 -0
- package/dist/pdf/parser.d.ts +131 -0
- package/dist/pdf/pkzip.d.ts +23 -0
- package/dist/pdf/png.d.ts +34 -0
- package/dist/pdf/util.d.ts +18 -0
- package/dist/res/licenses.d.ts +7 -0
- package/dist/res/srgbColorspace.d.ts +2 -0
- package/dist/util.d.ts +8 -0
- package/dist/version.d.ts +2 -0
- package/package.json +149 -4
- package/src/__tests__/fixtures/deep_toc.json +2953 -0
- package/src/__tests__/fixtures/flat_toc.json +2917 -0
- package/src/__tests__/fixtures/physdim.json +294 -0
- package/src/__tests__/fixtures/region_target.json +58 -0
- package/src/__tests__/fixtures/wunder.jpg +0 -0
- package/src/__tests__/parser.test.ts +42 -0
- package/src/__tests__/pdf.spec.ts +216 -0
- package/src/__tests__/region.test.ts +194 -0
- package/src/__tests__/setupJest.ts +2 -0
- package/src/convert.ts +1098 -0
- package/src/download.ts +703 -0
- package/src/global.d.ts +10 -0
- package/src/iiif.ts +605 -0
- package/src/index.ts +7 -0
- package/src/io.ts +243 -0
- package/src/log.ts +51 -0
- package/src/metrics.ts +47 -0
- package/src/ocr.ts +176 -0
- package/src/optimization.ts +201 -0
- package/src/optimization.worker.ts +118 -0
- package/src/pdf/annos-text.ts +382 -0
- package/src/pdf/annos.ts +240 -0
- package/src/pdf/common.ts +158 -0
- package/src/pdf/generator.ts +1665 -0
- package/src/pdf/image.ts +340 -0
- package/src/pdf/parser.ts +811 -0
- package/src/pdf/pkzip.ts +228 -0
- package/src/pdf/png.ts +397 -0
- package/src/pdf/util.ts +111 -0
- package/src/res/licenses.ts +266 -0
- package/src/res/srgbColorspace.ts +405 -0
- package/src/types.d.ts +6 -0
- package/src/util.ts +55 -0
- package/src/version.ts +1 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import encode, { init as initMozJpegEnc } from '@jsquash/jpeg/encode';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
BrowserEncoderParams,
|
|
5
|
+
InitializationRequest,
|
|
6
|
+
MOZJPEG_DEFAULT_PARAMS,
|
|
7
|
+
MozJPEGParams,
|
|
8
|
+
OptimizationError,
|
|
9
|
+
OptimizationRequest,
|
|
10
|
+
OptimizedImage,
|
|
11
|
+
TaskDescription,
|
|
12
|
+
type WorkerMessage,
|
|
13
|
+
} from './optimization';
|
|
14
|
+
|
|
15
|
+
let mozjpegInitialized = false;
|
|
16
|
+
const canvas = new OffscreenCanvas(1, 1);
|
|
17
|
+
|
|
18
|
+
self.onmessage = async function (
|
|
19
|
+
evt: MessageEvent<TaskDescription<OptimizationRequest | InitializationRequest>>
|
|
20
|
+
) {
|
|
21
|
+
if ('mozjpegWasm' in evt.data.data) {
|
|
22
|
+
console.debug(`worker: received MozJPEG WASM module`);
|
|
23
|
+
const module = await WebAssembly.compile(evt.data.data.mozjpegWasm);
|
|
24
|
+
await initMozJpegEnc(module);
|
|
25
|
+
mozjpegInitialized = true;
|
|
26
|
+
console.debug(`worker: initialized MozJPEG encoder`);
|
|
27
|
+
self.postMessage({
|
|
28
|
+
input: evt.data,
|
|
29
|
+
result: {},
|
|
30
|
+
});
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const params = evt.data.data;
|
|
35
|
+
console.debug(`worker: received optimization task`, evt.data.id, params);
|
|
36
|
+
let optimizedData: Uint8Array;
|
|
37
|
+
try {
|
|
38
|
+
if (params.method === 'mozjpeg') {
|
|
39
|
+
if (!mozjpegInitialized) {
|
|
40
|
+
throw new Error('MozJPEG encoder not initialized');
|
|
41
|
+
}
|
|
42
|
+
optimizedData = await reencodeWithMozJPEG(params);
|
|
43
|
+
} else if (params.method === 'browser') {
|
|
44
|
+
optimizedData = await reencodeWithBrowserEncoder(params);
|
|
45
|
+
} else {
|
|
46
|
+
throw new Error('Unsupported optimization method');
|
|
47
|
+
}
|
|
48
|
+
} catch (err) {
|
|
49
|
+
console.error(`Failed to optimize image: ${err}`, evt.data.id, err);
|
|
50
|
+
self.postMessage(
|
|
51
|
+
new OptimizationError(
|
|
52
|
+
`Failed to optimize image: ${err}`,
|
|
53
|
+
evt.data.id,
|
|
54
|
+
err
|
|
55
|
+
)
|
|
56
|
+
);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const result = {
|
|
60
|
+
input: evt.data,
|
|
61
|
+
result: {
|
|
62
|
+
jpegData: optimizedData,
|
|
63
|
+
sizeFactor: optimizedData.length / params.imageData.length,
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
self.postMessage(result);
|
|
67
|
+
console.debug(`worker optimized image with factor ${result.result.sizeFactor}`);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
function loadImageToCanvas(
|
|
71
|
+
imageData: Uint8Array,
|
|
72
|
+
imageFormat: string
|
|
73
|
+
): Promise<void> {
|
|
74
|
+
return new Promise(async (resolve, reject) => {
|
|
75
|
+
try {
|
|
76
|
+
const bitmap = await createImageBitmap(
|
|
77
|
+
new Blob([imageData], { type: imageFormat })
|
|
78
|
+
);
|
|
79
|
+
canvas.width = bitmap.width;
|
|
80
|
+
canvas.height = bitmap.height;
|
|
81
|
+
const ctx = canvas.getContext('2d');
|
|
82
|
+
if (!ctx) {
|
|
83
|
+
reject(new Error('Could not get 2D context'));
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
ctx.drawImage(bitmap, 0, 0);
|
|
87
|
+
resolve();
|
|
88
|
+
} catch (err) {
|
|
89
|
+
reject(err);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function reencodeWithMozJPEG(
|
|
96
|
+
params: OptimizationRequest & MozJPEGParams
|
|
97
|
+
): Promise<Uint8Array> {
|
|
98
|
+
await loadImageToCanvas(params.imageData, params.imageFormat);
|
|
99
|
+
const pixelData = canvas
|
|
100
|
+
.getContext('2d')!
|
|
101
|
+
.getImageData(0, 0, canvas.width, canvas.height);
|
|
102
|
+
const encoded = await encode(pixelData, {
|
|
103
|
+
...MOZJPEG_DEFAULT_PARAMS,
|
|
104
|
+
...params,
|
|
105
|
+
});
|
|
106
|
+
return new Uint8Array(encoded);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function reencodeWithBrowserEncoder(
|
|
110
|
+
params: OptimizationRequest & BrowserEncoderParams
|
|
111
|
+
): Promise<Uint8Array> {
|
|
112
|
+
await loadImageToCanvas(params.imageData, params.imageFormat);
|
|
113
|
+
const encoded = await canvas.convertToBlob({
|
|
114
|
+
type: 'image/jpeg',
|
|
115
|
+
quality: params.quality / 100,
|
|
116
|
+
});
|
|
117
|
+
return new Uint8Array(await encoded.arrayBuffer());
|
|
118
|
+
}
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import { Annotation } from '../iiif.js';
|
|
2
|
+
import { SupportedSelector } from '@iiif/helpers';
|
|
3
|
+
import Color from 'color';
|
|
4
|
+
import { SAXParser, SaxEventType, Tag, Text } from 'sax-wasm';
|
|
5
|
+
import { saxParserWasm } from '../util.js';
|
|
6
|
+
import { textEncoder } from './util.js';
|
|
7
|
+
|
|
8
|
+
export type AnnotationFont = 'Helvetica' | 'Times-Roman' | 'Courier';
|
|
9
|
+
|
|
10
|
+
const STANDARD_FONT_FACES: Record<
|
|
11
|
+
AnnotationFont,
|
|
12
|
+
[string, string, string, string]
|
|
13
|
+
> = {
|
|
14
|
+
Helvetica: [
|
|
15
|
+
'Helvetica',
|
|
16
|
+
'Helvetica-Bold',
|
|
17
|
+
'Helvetica-Oblique',
|
|
18
|
+
'Helvetica-BoldOblique',
|
|
19
|
+
],
|
|
20
|
+
'Times-Roman': [
|
|
21
|
+
'Times-Roman',
|
|
22
|
+
'Times-Bold',
|
|
23
|
+
'Times-Italic',
|
|
24
|
+
'Times-BoldItalic',
|
|
25
|
+
],
|
|
26
|
+
Courier: [
|
|
27
|
+
'Courier',
|
|
28
|
+
'Courier-Bold',
|
|
29
|
+
'Courier-Oblique',
|
|
30
|
+
'Courier-BoldOblique',
|
|
31
|
+
],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export function annotationFontResourceName(
|
|
35
|
+
font: AnnotationFont,
|
|
36
|
+
bold: boolean,
|
|
37
|
+
italic: boolean
|
|
38
|
+
): string {
|
|
39
|
+
const suffix = bold
|
|
40
|
+
? italic
|
|
41
|
+
? 'BoldItalic'
|
|
42
|
+
: 'Bold'
|
|
43
|
+
: italic
|
|
44
|
+
? 'Italic'
|
|
45
|
+
: 'Regular';
|
|
46
|
+
return `annFont${font.replace(/[^a-zA-Z0-9]/g, '')}${suffix}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function annotationFontFaces(
|
|
50
|
+
font: AnnotationFont
|
|
51
|
+
): Array<{ resourceName: string; baseFont: string }> {
|
|
52
|
+
return STANDARD_FONT_FACES[font].map((baseFont, idx) => ({
|
|
53
|
+
resourceName: annotationFontResourceName(font, idx % 2 === 1, idx >= 2),
|
|
54
|
+
baseFont,
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface AnnotationTextRun {
|
|
59
|
+
text: string;
|
|
60
|
+
bold: boolean;
|
|
61
|
+
italic: boolean;
|
|
62
|
+
underline: boolean;
|
|
63
|
+
color?: [number, number, number];
|
|
64
|
+
lineBreak?: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface AnnotationTextStyle {
|
|
68
|
+
bold: boolean;
|
|
69
|
+
italic: boolean;
|
|
70
|
+
underline: boolean;
|
|
71
|
+
color?: [number, number, number];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function htmlToAnnotationText(
|
|
75
|
+
html: string
|
|
76
|
+
): Promise<AnnotationTextRun[]> {
|
|
77
|
+
if (!saxParserWasm) {
|
|
78
|
+
throw new Error('SAX parser WebAssembly has not been initialized');
|
|
79
|
+
}
|
|
80
|
+
const parser = new SAXParser(
|
|
81
|
+
SaxEventType.Text | SaxEventType.OpenTag | SaxEventType.CloseTag
|
|
82
|
+
);
|
|
83
|
+
await parser.prepareWasm(saxParserWasm);
|
|
84
|
+
const baseStyle: AnnotationTextStyle = {
|
|
85
|
+
bold: false,
|
|
86
|
+
italic: false,
|
|
87
|
+
underline: false,
|
|
88
|
+
};
|
|
89
|
+
const stack: Array<{ name: string; style: AnnotationTextStyle }> = [
|
|
90
|
+
{ name: '', style: baseStyle },
|
|
91
|
+
];
|
|
92
|
+
const runs: AnnotationTextRun[] = [];
|
|
93
|
+
const addBreak = () => {
|
|
94
|
+
if (runs.length > 0 && !runs[runs.length - 1].lineBreak) {
|
|
95
|
+
runs.push({
|
|
96
|
+
text: '',
|
|
97
|
+
...stack[stack.length - 1].style,
|
|
98
|
+
lineBreak: true,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
parser.eventHandler = (event, data) => {
|
|
103
|
+
if (event === SaxEventType.Text) {
|
|
104
|
+
const text = (data as Text).value.replace(/\s+/g, ' ');
|
|
105
|
+
if (text) {
|
|
106
|
+
const style = stack[stack.length - 1].style;
|
|
107
|
+
runs.push({ text, ...style });
|
|
108
|
+
}
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const tag = data as Tag;
|
|
112
|
+
const name = tag.name.toLowerCase();
|
|
113
|
+
if (event === SaxEventType.OpenTag) {
|
|
114
|
+
if (name === 'br') {
|
|
115
|
+
addBreak();
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (['p', 'div', 'li'].includes(name)) {
|
|
119
|
+
addBreak();
|
|
120
|
+
}
|
|
121
|
+
if (!tag.selfClosing) {
|
|
122
|
+
stack.push({
|
|
123
|
+
name,
|
|
124
|
+
style: styleForTag(tag, stack[stack.length - 1].style),
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
let stackIdx = -1;
|
|
130
|
+
for (let idx = stack.length - 1; idx > 0; idx--) {
|
|
131
|
+
if (stack[idx].name === name) {
|
|
132
|
+
stackIdx = idx;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (stackIdx > 0) {
|
|
137
|
+
stack.splice(stackIdx);
|
|
138
|
+
}
|
|
139
|
+
if (['p', 'div', 'li'].includes(name)) {
|
|
140
|
+
addBreak();
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
parser.write(textEncoder.encode(html));
|
|
144
|
+
parser.end();
|
|
145
|
+
while (runs[0]?.lineBreak) {
|
|
146
|
+
runs.shift();
|
|
147
|
+
}
|
|
148
|
+
while (runs[runs.length - 1]?.lineBreak) {
|
|
149
|
+
runs.pop();
|
|
150
|
+
}
|
|
151
|
+
if (runs.length > 0) {
|
|
152
|
+
runs[0].text = runs[0].text.trimStart();
|
|
153
|
+
runs[runs.length - 1].text = runs[runs.length - 1].text.trimEnd();
|
|
154
|
+
}
|
|
155
|
+
return runs.filter((run) => run.lineBreak || run.text.length > 0);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function styleForTag(tag: Tag, parent: AnnotationTextStyle): AnnotationTextStyle {
|
|
159
|
+
const style = { ...parent };
|
|
160
|
+
const name = tag.name.toLowerCase();
|
|
161
|
+
if (name === 'b' || name === 'strong') {
|
|
162
|
+
style.bold = true;
|
|
163
|
+
} else if (name === 'i' || name === 'em') {
|
|
164
|
+
style.italic = true;
|
|
165
|
+
} else if (name === 'u') {
|
|
166
|
+
style.underline = true;
|
|
167
|
+
}
|
|
168
|
+
const styleAttr = tag.attributes.find(
|
|
169
|
+
(attr) => attr.name.value.toLowerCase() === 'style'
|
|
170
|
+
)?.value.value;
|
|
171
|
+
if (!styleAttr) {
|
|
172
|
+
return style;
|
|
173
|
+
}
|
|
174
|
+
for (const rule of styleAttr.split(';')) {
|
|
175
|
+
const [rawName, ...rawValue] = rule.split(':');
|
|
176
|
+
const property = rawName?.trim().toLowerCase();
|
|
177
|
+
const value = rawValue.join(':').trim();
|
|
178
|
+
if (property === 'font-weight') {
|
|
179
|
+
style.bold = value === 'bold' || Number.parseInt(value, 10) >= 600;
|
|
180
|
+
} else if (property === 'font-style') {
|
|
181
|
+
style.italic = value === 'italic' || value === 'oblique';
|
|
182
|
+
} else if (property === 'text-decoration') {
|
|
183
|
+
style.underline = value.split(/\s+/).includes('underline');
|
|
184
|
+
} else if (property === 'color') {
|
|
185
|
+
try {
|
|
186
|
+
const [r, g, b] = Color(value).rgb().array();
|
|
187
|
+
style.color = [r / 255, g / 255, b / 255];
|
|
188
|
+
} catch {
|
|
189
|
+
// Ignore unsupported CSS colors and retain the inherited color.
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return style;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function winAnsiHexString(value: string): string {
|
|
197
|
+
const extensions: Record<string, number> = {
|
|
198
|
+
'€': 0x80,
|
|
199
|
+
'‚': 0x82,
|
|
200
|
+
'ƒ': 0x83,
|
|
201
|
+
'„': 0x84,
|
|
202
|
+
'…': 0x85,
|
|
203
|
+
'†': 0x86,
|
|
204
|
+
'‡': 0x87,
|
|
205
|
+
'ˆ': 0x88,
|
|
206
|
+
'‰': 0x89,
|
|
207
|
+
'Š': 0x8a,
|
|
208
|
+
'‹': 0x8b,
|
|
209
|
+
'Œ': 0x8c,
|
|
210
|
+
'Ž': 0x8e,
|
|
211
|
+
'‘': 0x91,
|
|
212
|
+
'’': 0x92,
|
|
213
|
+
'“': 0x93,
|
|
214
|
+
'”': 0x94,
|
|
215
|
+
'•': 0x95,
|
|
216
|
+
'–': 0x96,
|
|
217
|
+
'—': 0x97,
|
|
218
|
+
'˜': 0x98,
|
|
219
|
+
'™': 0x99,
|
|
220
|
+
'š': 0x9a,
|
|
221
|
+
'›': 0x9b,
|
|
222
|
+
'œ': 0x9c,
|
|
223
|
+
'ž': 0x9e,
|
|
224
|
+
'Ÿ': 0x9f,
|
|
225
|
+
};
|
|
226
|
+
const bytes = Array.from(value, (character) => {
|
|
227
|
+
const codePoint = character.codePointAt(0) ?? 0x3f;
|
|
228
|
+
const isWinAnsiCharacter =
|
|
229
|
+
(codePoint >= 0x20 && codePoint <= 0x7e) ||
|
|
230
|
+
(codePoint >= 0xa0 && codePoint <= 0xff);
|
|
231
|
+
return extensions[character] ?? (isWinAnsiCharacter ? codePoint : 0x3f);
|
|
232
|
+
});
|
|
233
|
+
return `<${bytes.map((byte) => byte.toString(16).padStart(2, '0')).join('')}>`;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function annotationTextWidth(
|
|
237
|
+
text: string,
|
|
238
|
+
font: AnnotationFont,
|
|
239
|
+
fontSize: number
|
|
240
|
+
): number {
|
|
241
|
+
const narrow = ' !.,:;|ijlItfr\'';
|
|
242
|
+
const wide = 'MW@%&';
|
|
243
|
+
return (
|
|
244
|
+
Array.from(text).reduce((width, character) => {
|
|
245
|
+
if (font === 'Courier') {
|
|
246
|
+
return width + 0.6;
|
|
247
|
+
}
|
|
248
|
+
if (character === ' ') {
|
|
249
|
+
return width + 0.28;
|
|
250
|
+
}
|
|
251
|
+
if (narrow.includes(character)) {
|
|
252
|
+
return width + 0.28;
|
|
253
|
+
}
|
|
254
|
+
if (wide.includes(character)) {
|
|
255
|
+
return width + 0.82;
|
|
256
|
+
}
|
|
257
|
+
if (/[A-Z0-9]/.test(character)) {
|
|
258
|
+
return width + 0.62;
|
|
259
|
+
}
|
|
260
|
+
return width + 0.52;
|
|
261
|
+
}, 0) * fontSize
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function annotationSelectors(anno: Annotation): SupportedSelector[] {
|
|
266
|
+
if (anno.target.selector) {
|
|
267
|
+
return [anno.target.selector];
|
|
268
|
+
}
|
|
269
|
+
return anno.target.selectors ?? [];
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Create page-content instructions for an annotation rendered as visible text. */
|
|
273
|
+
export async function exportPdfText(
|
|
274
|
+
anno: Annotation,
|
|
275
|
+
unitScale: number,
|
|
276
|
+
pageHeight: number,
|
|
277
|
+
pageWidth: number,
|
|
278
|
+
font: AnnotationFont
|
|
279
|
+
): Promise<string[]> {
|
|
280
|
+
const markupRuns = await htmlToAnnotationText(anno.markup);
|
|
281
|
+
const contentOps: string[] = [];
|
|
282
|
+
const fontSize = 10;
|
|
283
|
+
const lineHeight = 12;
|
|
284
|
+
const padding = 2;
|
|
285
|
+
for (const selector of annotationSelectors(anno)) {
|
|
286
|
+
const spatial = selector.spatial;
|
|
287
|
+
if (
|
|
288
|
+
!spatial ||
|
|
289
|
+
spatial.width === undefined ||
|
|
290
|
+
spatial.height === undefined ||
|
|
291
|
+
spatial.width <= 0 ||
|
|
292
|
+
spatial.height <= 0
|
|
293
|
+
) {
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
const scaleX = spatial.unit === 'percent' ? pageWidth / 100 : 1;
|
|
297
|
+
const scaleY = spatial.unit === 'percent' ? pageHeight / 100 : 1;
|
|
298
|
+
const x = spatial.x * scaleX * unitScale;
|
|
299
|
+
const y = spatial.y * scaleY * unitScale;
|
|
300
|
+
const width = spatial.width * scaleX * unitScale;
|
|
301
|
+
const height = spatial.height * scaleY * unitScale;
|
|
302
|
+
const maxWidth = Math.max(0, width - padding * 2);
|
|
303
|
+
const lines: AnnotationTextRun[][] = [[]];
|
|
304
|
+
let lineWidth = 0;
|
|
305
|
+
const addLine = () => {
|
|
306
|
+
if (lines[lines.length - 1].length > 0) {
|
|
307
|
+
lines.push([]);
|
|
308
|
+
lineWidth = 0;
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
for (const run of markupRuns) {
|
|
312
|
+
if (run.lineBreak) {
|
|
313
|
+
addLine();
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
const tokens = run.text.match(/\s+|[^\s]+/g) ?? [];
|
|
317
|
+
for (const token of tokens) {
|
|
318
|
+
const isWhitespace = /^\s+$/.test(token);
|
|
319
|
+
const tokenWidth = annotationTextWidth(token, font, fontSize);
|
|
320
|
+
if (isWhitespace && lines[lines.length - 1].length === 0) {
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
if (!isWhitespace && lineWidth + tokenWidth > maxWidth && lineWidth > 0) {
|
|
324
|
+
addLine();
|
|
325
|
+
}
|
|
326
|
+
if (isWhitespace && lineWidth + tokenWidth > maxWidth) {
|
|
327
|
+
addLine();
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
lines[lines.length - 1].push({ ...run, text: token });
|
|
331
|
+
lineWidth += tokenWidth;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
const top = pageHeight * unitScale - y - padding - fontSize;
|
|
335
|
+
const bottom = pageHeight * unitScale - y - height + padding;
|
|
336
|
+
for (const [lineIdx, line] of lines.entries()) {
|
|
337
|
+
const baseline = top - lineIdx * lineHeight;
|
|
338
|
+
if (baseline < bottom) {
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
if (line.length === 0) {
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
const startX = x + padding;
|
|
345
|
+
contentOps.push(`BT 1 0 0 1 ${startX} ${baseline} Tm`);
|
|
346
|
+
let underlineX = startX;
|
|
347
|
+
const underlines: Array<{
|
|
348
|
+
x1: number;
|
|
349
|
+
x2: number;
|
|
350
|
+
color?: [number, number, number];
|
|
351
|
+
}> = [];
|
|
352
|
+
for (const run of line) {
|
|
353
|
+
const resourceName = annotationFontResourceName(
|
|
354
|
+
font,
|
|
355
|
+
run.bold,
|
|
356
|
+
run.italic
|
|
357
|
+
);
|
|
358
|
+
contentOps.push(`/${resourceName} ${fontSize} Tf`);
|
|
359
|
+
const color = run.color ?? [0, 0, 0];
|
|
360
|
+
contentOps.push(`${color.join(' ')} rg`);
|
|
361
|
+
contentOps.push(`${winAnsiHexString(run.text)} Tj`);
|
|
362
|
+
const runWidth = annotationTextWidth(run.text, font, fontSize);
|
|
363
|
+
if (run.underline) {
|
|
364
|
+
underlines.push({
|
|
365
|
+
x1: underlineX,
|
|
366
|
+
x2: underlineX + runWidth,
|
|
367
|
+
color: run.color,
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
underlineX += runWidth;
|
|
371
|
+
}
|
|
372
|
+
contentOps.push('ET');
|
|
373
|
+
for (const underline of underlines) {
|
|
374
|
+
const color = underline.color ?? [0, 0, 0];
|
|
375
|
+
contentOps.push(
|
|
376
|
+
`q ${color.join(' ')} RG 0.5 w ${underline.x1} ${baseline - 1.5} m ${underline.x2} ${baseline - 1.5} l S Q`
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return contentOps;
|
|
382
|
+
}
|