@quario/layout 0.3.0 → 0.5.0

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/lib/image.js DELETED
@@ -1,58 +0,0 @@
1
- /**
2
- * An image's own size, read from its header. Sizing is a fact about the bytes
3
- * rather than anything a page can answer, so it sits here and not on the
4
- * drawing surface: the band flow asks for it while measuring, where no
5
- * document is in reach at all, and again while drawing, where one is.
6
- *
7
- * Read, never decoded — a PNG's IHDR and a JPEG's frame header carry the two
8
- * numbers the layout needs, and nothing else here looks at a pixel.
9
- *
10
- * The XLSX target reads the same two headers for its own placement, in pixels.
11
- * Whether that fact belongs on the engine's event instead — it already reads
12
- * the magic numbers to name the format — is bead `quario-b0h`.
13
- */
14
-
15
- // Points, at the conventional 96 dpi the schema names: 72/96 of a pixel.
16
- let PER_PX = 72 / 96;
17
-
18
- /** @type {(bytes: Uint8Array, at: number) => number} */
19
- let word = (bytes, at) => (bytes[at] << 8) | bytes[at + 1];
20
-
21
- /** @type {(code: number) => boolean} */
22
- let inSof = (code) => code >= 0xc0 && code <= 0xcf;
23
-
24
- /** @type {(code: number) => boolean} */
25
- let notTable = (code) => code !== 0xc4 && code !== 0xc8 && code !== 0xcc;
26
-
27
- // The frame header carries the dimensions, and it is the first SOFn marker:
28
- // every code in C0..CF except the three in that range that are not frames --
29
- // DHT, the JPG extension, and DAC.
30
- /** @type {(code: number) => boolean} */
31
- let isFrame = (code) => inSof(code) && notTable(code);
32
-
33
- /** @type {(bytes: Uint8Array) => { w: number, h: number }} */
34
- let jpegSize = (bytes) => {
35
- for (let at = 2; at + 9 < bytes.length; at += 2 + word(bytes, at + 2)) {
36
- if (bytes[at] !== 0xff) break;
37
- if (isFrame(bytes[at + 1])) return { w: word(bytes, at + 7), h: word(bytes, at + 5) };
38
- }
39
- return { w: 0, h: 0 };
40
- };
41
-
42
- /**
43
- * The image's intrinsic size in points. The format is the engine's sniff,
44
- * riding on the event, so nothing here decides it a second time.
45
- *
46
- * @param {Uint8Array} bytes The image file.
47
- * @param {string} format The engine's sniff: `"png"` or `"jpeg"`.
48
- * @returns {{ w: number, h: number }} The size, in points.
49
- */
50
- export let intrinsic = (bytes, format) => {
51
- // A PNG carries the two numbers in its IHDR at a fixed offset.
52
- let { w, h } = format === "png" ? { w: word(bytes, 18), h: word(bytes, 22) } : jpegSize(bytes);
53
- // The engine vouched for the magic numbers, not for the rest of the file:
54
- // a truncated header reaches here as a zero, and failing loudly beats
55
- // drawing an image with no size (SCHEMA.md, "Image item").
56
- if (!(w > 0 && h > 0)) throw Error("could not read the image's size from its bytes");
57
- return { w: w * PER_PX, h: h * PER_PX };
58
- };