@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,194 @@
|
|
|
1
|
+
import { PDFDocument, PDFRawStream, decodePDFRawStream } from 'pdf-lib';
|
|
2
|
+
import fetchMock from 'jest-fetch-mock';
|
|
3
|
+
import tmp from 'tmp';
|
|
4
|
+
|
|
5
|
+
import fs from 'fs';
|
|
6
|
+
import path from 'path';
|
|
7
|
+
|
|
8
|
+
import { fetchCanvasData, getCropRect } from '../download';
|
|
9
|
+
import {
|
|
10
|
+
getCanvasInfo,
|
|
11
|
+
getImageInfos,
|
|
12
|
+
getRegionFromSelector,
|
|
13
|
+
vault,
|
|
14
|
+
} from '../iiif';
|
|
15
|
+
import { NodeWriter } from '../io';
|
|
16
|
+
import PDFGenerator from '../pdf/generator';
|
|
17
|
+
import type { CanvasNormalized } from '@iiif/presentation-3-normalized';
|
|
18
|
+
|
|
19
|
+
const FIXTURES = path.resolve(__dirname, './fixtures');
|
|
20
|
+
|
|
21
|
+
/** Two images on one canvas, the second one with a region (Level 0 services). */
|
|
22
|
+
async function loadCanvas(): Promise<CanvasNormalized> {
|
|
23
|
+
const manifest = JSON.parse(
|
|
24
|
+
await fs.promises.readFile(path.join(FIXTURES, 'region_target.json'), 'utf8')
|
|
25
|
+
);
|
|
26
|
+
const loaded = await vault.loadManifest(manifest.id, manifest);
|
|
27
|
+
if (!loaded) {
|
|
28
|
+
throw new Error('Could not load the manifest');
|
|
29
|
+
}
|
|
30
|
+
return vault.get<CanvasNormalized>(loaded.items[0]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
describe('getCropRect', () => {
|
|
34
|
+
it('should convert regions to coordinates relative to the image', () => {
|
|
35
|
+
expect(getCropRect('1768,2423,1768,2080', 3536, 4999)).toEqual({
|
|
36
|
+
x: 0.5,
|
|
37
|
+
y: 2423 / 4999,
|
|
38
|
+
width: 0.5,
|
|
39
|
+
height: 2080 / 4999,
|
|
40
|
+
});
|
|
41
|
+
expect(getCropRect('pct:10,20,50,25')).toEqual({
|
|
42
|
+
x: 0.1,
|
|
43
|
+
y: 0.2,
|
|
44
|
+
width: 0.5,
|
|
45
|
+
height: 0.25,
|
|
46
|
+
});
|
|
47
|
+
expect(getCropRect('square', 200, 100)).toEqual({
|
|
48
|
+
x: 0.25,
|
|
49
|
+
y: 0,
|
|
50
|
+
width: 0.5,
|
|
51
|
+
height: 1,
|
|
52
|
+
});
|
|
53
|
+
// Only the part inside of the image counts
|
|
54
|
+
expect(getCropRect('50,50,100,100', 100, 100)).toEqual({
|
|
55
|
+
x: 0.5,
|
|
56
|
+
y: 0.5,
|
|
57
|
+
width: 0.5,
|
|
58
|
+
height: 0.5,
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should return nothing if the whole image is selected', () => {
|
|
63
|
+
expect(getCropRect('full', 100, 100)).toBeUndefined();
|
|
64
|
+
expect(getCropRect('0,0,150,150', 100, 100)).toBeUndefined();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should fail for malformed regions or regions outside of the image', () => {
|
|
68
|
+
expect(() => getCropRect('200,200,50,50', 100, 100)).toThrow(/outside/);
|
|
69
|
+
expect(() => getCropRect('10,10,50', 100, 100)).toThrow(/four numbers/);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('getRegionFromSelector', () => {
|
|
74
|
+
it('should read ImageApiSelectors and media fragments', () => {
|
|
75
|
+
expect(
|
|
76
|
+
getRegionFromSelector({ type: 'ImageApiSelector', region: '1,2,3,4' })
|
|
77
|
+
).toBe('1,2,3,4');
|
|
78
|
+
expect(
|
|
79
|
+
getRegionFromSelector({ type: 'FragmentSelector', value: 'xywh=1,2,3,4' })
|
|
80
|
+
).toBe('1,2,3,4');
|
|
81
|
+
expect(
|
|
82
|
+
getRegionFromSelector({ type: 'ImageApiSelector', region: 'full' })
|
|
83
|
+
).toBeUndefined();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('Images with a region', () => {
|
|
88
|
+
it('should place images as given by the target and find the region', async () => {
|
|
89
|
+
const images = getImageInfos(await loadCanvas());
|
|
90
|
+
expect(images).toHaveLength(2);
|
|
91
|
+
expect(images[0]).toMatchObject({ x: 8, y: 1, width: 575, height: 903 });
|
|
92
|
+
expect(images[0].region).toBeUndefined();
|
|
93
|
+
expect(images[1]).toMatchObject({
|
|
94
|
+
x: 587,
|
|
95
|
+
y: 226,
|
|
96
|
+
width: 448,
|
|
97
|
+
height: 1396,
|
|
98
|
+
region: '224,156,369,1149',
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should fetch the full image (also for Level 0) and attach the crop', async () => {
|
|
103
|
+
fetchMock.resetMocks();
|
|
104
|
+
const imgBuf = await fs.promises.readFile(path.join(FIXTURES, 'wunder.jpg'));
|
|
105
|
+
fetchMock.mockResponse(async (req) =>
|
|
106
|
+
req.url.endsWith('/info.json')
|
|
107
|
+
? JSON.stringify({
|
|
108
|
+
'@context': 'http://iiif.io/api/image/2/context.json',
|
|
109
|
+
'@id': req.url.replace('/info.json', ''),
|
|
110
|
+
profile: 'http://iiif.io/api/image/2/level0.json',
|
|
111
|
+
width: 1041,
|
|
112
|
+
height: 1634,
|
|
113
|
+
})
|
|
114
|
+
: { body: imgBuf as any, headers: { 'Content-Type': 'image/jpeg' } }
|
|
115
|
+
);
|
|
116
|
+
const canvas = await loadCanvas();
|
|
117
|
+
const data = await fetchCanvasData(canvas, getCanvasInfo(canvas).images, {});
|
|
118
|
+
|
|
119
|
+
expect(data?.imageFailures).toHaveLength(0);
|
|
120
|
+
expect(data?.images).toHaveLength(2);
|
|
121
|
+
expect(data?.images[0].crop).toBeUndefined();
|
|
122
|
+
expect(data?.images[1].crop).toEqual({
|
|
123
|
+
x: 224 / 1041,
|
|
124
|
+
y: 156 / 1634,
|
|
125
|
+
width: 369 / 1041,
|
|
126
|
+
height: 1149 / 1634,
|
|
127
|
+
});
|
|
128
|
+
// No region requests, Level 0 can't do them
|
|
129
|
+
const imageUrls = fetchMock.mock.calls
|
|
130
|
+
.map(([url]) => String(typeof url === 'string' ? url : (url as Request).url))
|
|
131
|
+
.filter((url) => url.endsWith('.jpg'));
|
|
132
|
+
expect(imageUrls.every((url) => url.includes('/full/'))).toBe(true);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe('Painting a cropped image into the PDF', () => {
|
|
137
|
+
it('should clip to the target and place the full image so that the crop fills it', async () => {
|
|
138
|
+
const pdfPath = tmp.tmpNameSync({ prefix: 'pdiiif-test-', postfix: '.pdf' });
|
|
139
|
+
const canvasId = 'http://some.fixture/canvas';
|
|
140
|
+
const pdfgen = new PDFGenerator({
|
|
141
|
+
writer: new NodeWriter(fs.createWriteStream(pdfPath)),
|
|
142
|
+
metadata: {},
|
|
143
|
+
langPref: ['en'],
|
|
144
|
+
canvasInfos: [
|
|
145
|
+
{
|
|
146
|
+
canvas: { id: canvasId, type: 'Canvas' },
|
|
147
|
+
images: [
|
|
148
|
+
{ resource: { id: 'foo', type: 'Image' }, x: 0, y: 0, width: 290, height: 400 },
|
|
149
|
+
],
|
|
150
|
+
numAnnotations: 0,
|
|
151
|
+
},
|
|
152
|
+
],
|
|
153
|
+
outline: [],
|
|
154
|
+
pageLabels: ['1'],
|
|
155
|
+
});
|
|
156
|
+
await pdfgen.setup();
|
|
157
|
+
const imgBuf = await fs.promises.readFile(path.join(FIXTURES, 'wunder.jpg'));
|
|
158
|
+
await pdfgen.renderPage(
|
|
159
|
+
canvasId,
|
|
160
|
+
{ width: 290, height: 400 },
|
|
161
|
+
[
|
|
162
|
+
{
|
|
163
|
+
resource: { id: 'someid', type: 'Image' },
|
|
164
|
+
width: 290,
|
|
165
|
+
height: 400,
|
|
166
|
+
x: 0,
|
|
167
|
+
y: 0,
|
|
168
|
+
data: imgBuf,
|
|
169
|
+
numBytes: imgBuf.length,
|
|
170
|
+
corsAvailable: true,
|
|
171
|
+
format: 'jpeg',
|
|
172
|
+
// Right half of the image, starting a quarter from the top
|
|
173
|
+
crop: { x: 0.5, y: 0.25, width: 0.5, height: 0.5 },
|
|
174
|
+
},
|
|
175
|
+
],
|
|
176
|
+
[],
|
|
177
|
+
undefined,
|
|
178
|
+
72
|
|
179
|
+
);
|
|
180
|
+
await pdfgen.end();
|
|
181
|
+
|
|
182
|
+
const pdfData = await fs.promises.readFile(pdfPath);
|
|
183
|
+
fs.unlinkSync(pdfPath);
|
|
184
|
+
const parsed = await PDFDocument.load(pdfData.buffer.slice(0) as ArrayBuffer, {
|
|
185
|
+
throwOnInvalidObject: false,
|
|
186
|
+
});
|
|
187
|
+
const contents = parsed.getPage(0).node.Contents() as PDFRawStream;
|
|
188
|
+
const content = Buffer.from(decodePDFRawStream(contents).decode()).toString('latin1');
|
|
189
|
+
// The full image is twice the size of the page, shifted left by one page
|
|
190
|
+
// width and down by a quarter of the image height
|
|
191
|
+
expect(content).toContain('q 0 0 290 400 re W n');
|
|
192
|
+
expect(content).toContain('580 0 0 800 -290 -200 cm');
|
|
193
|
+
});
|
|
194
|
+
});
|