docxkeun 0.1.3 → 0.3.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.
Files changed (199) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/README.md +166 -16
  3. package/dist/api/editor.d.ts +231 -7
  4. package/dist/api/editor.js +1125 -26
  5. package/dist/api/headers.d.ts +51 -0
  6. package/dist/api/headers.js +133 -0
  7. package/dist/api/index.d.ts +6 -1
  8. package/dist/api/index.js +4 -1
  9. package/dist/api/lists.d.ts +37 -0
  10. package/dist/api/lists.js +132 -0
  11. package/dist/api/parts.js +1 -0
  12. package/dist/api/positions.js +1 -0
  13. package/dist/api/revisions.d.ts +61 -0
  14. package/dist/api/revisions.js +401 -0
  15. package/dist/api/selection.d.ts +57 -0
  16. package/dist/api/selection.js +91 -0
  17. package/dist/api/tables.d.ts +60 -0
  18. package/dist/api/tables.js +138 -0
  19. package/dist/charts/color.d.ts +45 -0
  20. package/dist/charts/color.js +322 -0
  21. package/dist/charts/format.d.ts +12 -0
  22. package/dist/charts/format.js +459 -0
  23. package/dist/charts/index.d.ts +11 -0
  24. package/dist/charts/index.js +8 -0
  25. package/dist/charts/model.d.ts +225 -0
  26. package/dist/charts/model.js +758 -0
  27. package/dist/charts/outline.d.ts +17 -0
  28. package/dist/charts/outline.js +214 -0
  29. package/dist/charts/render.d.ts +20 -0
  30. package/dist/charts/render.js +1772 -0
  31. package/dist/drawing/arrows.d.ts +37 -0
  32. package/dist/drawing/arrows.js +99 -0
  33. package/dist/drawing/formula.d.ts +40 -0
  34. package/dist/drawing/formula.js +159 -0
  35. package/dist/drawing/geometry.d.ts +61 -0
  36. package/dist/drawing/geometry.js +286 -0
  37. package/dist/drawing/index.d.ts +12 -0
  38. package/dist/drawing/index.js +8 -0
  39. package/dist/drawing/presets.d.ts +72 -0
  40. package/dist/drawing/presets.js +2874 -0
  41. package/dist/fonts/shaping/apply.d.ts +94 -0
  42. package/dist/fonts/shaping/apply.js +349 -0
  43. package/dist/fonts/shaping/arabic.d.ts +28 -0
  44. package/dist/fonts/shaping/arabic.js +235 -0
  45. package/dist/fonts/shaping/buffer.d.ts +53 -0
  46. package/dist/fonts/shaping/buffer.js +95 -0
  47. package/dist/fonts/shaping/gdef.d.ts +30 -0
  48. package/dist/fonts/shaping/gdef.js +66 -0
  49. package/dist/fonts/shaping/gpos.d.ts +86 -0
  50. package/dist/fonts/shaping/gpos.js +428 -0
  51. package/dist/fonts/shaping/gsub.d.ts +65 -0
  52. package/dist/fonts/shaping/gsub.js +313 -0
  53. package/dist/fonts/shaping/index.d.ts +32 -0
  54. package/dist/fonts/shaping/index.js +402 -0
  55. package/dist/fonts/shaping/indic.d.ts +6 -0
  56. package/dist/fonts/shaping/indic.js +805 -0
  57. package/dist/fonts/shaping/layout-common.d.ts +124 -0
  58. package/dist/fonts/shaping/layout-common.js +349 -0
  59. package/dist/fonts/shaping/plan.d.ts +87 -0
  60. package/dist/fonts/shaping/plan.js +152 -0
  61. package/dist/fonts/shaping/scripts.d.ts +26 -0
  62. package/dist/fonts/shaping/scripts.js +160 -0
  63. package/dist/fonts/shaping/shaper.d.ts +23 -0
  64. package/dist/fonts/shaping/shaper.js +5 -0
  65. package/dist/fonts/shaping/unicode.d.ts +34 -0
  66. package/dist/fonts/shaping/unicode.js +138 -0
  67. package/dist/images/bmp.d.ts +9 -0
  68. package/dist/images/bmp.js +234 -0
  69. package/dist/images/gif.d.ts +9 -0
  70. package/dist/images/gif.js +215 -0
  71. package/dist/images/index.d.ts +18 -0
  72. package/dist/images/index.js +63 -0
  73. package/dist/images/png-encode.d.ts +4 -0
  74. package/dist/images/png-encode.js +73 -0
  75. package/dist/images/size.d.ts +10 -0
  76. package/dist/images/size.js +83 -0
  77. package/dist/images/tiff.d.ts +14 -0
  78. package/dist/images/tiff.js +407 -0
  79. package/dist/index.d.ts +7 -2
  80. package/dist/index.js +5 -0
  81. package/dist/layout/bidi.d.ts +18 -0
  82. package/dist/layout/bidi.js +71 -0
  83. package/dist/layout/blocks.d.ts +20 -3
  84. package/dist/layout/blocks.js +88 -58
  85. package/dist/layout/cache.d.ts +31 -0
  86. package/dist/layout/cache.js +38 -0
  87. package/dist/layout/drawing.d.ts +30 -0
  88. package/dist/layout/drawing.js +120 -0
  89. package/dist/layout/engine.d.ts +3 -0
  90. package/dist/layout/engine.js +218 -11
  91. package/dist/layout/index.d.ts +1 -0
  92. package/dist/layout/index.js +1 -0
  93. package/dist/layout/lines.d.ts +63 -5
  94. package/dist/layout/lines.js +245 -20
  95. package/dist/layout/paint.d.ts +25 -3
  96. package/dist/layout/paint.js +26 -10
  97. package/dist/layout/shapes.d.ts +7 -0
  98. package/dist/layout/shapes.js +338 -0
  99. package/dist/layout/style.d.ts +4 -0
  100. package/dist/layout/style.js +2 -0
  101. package/dist/layout/types.d.ts +89 -2
  102. package/dist/math/boxes.d.ts +88 -0
  103. package/dist/math/boxes.js +166 -0
  104. package/dist/math/index.d.ts +29 -0
  105. package/dist/math/index.js +44 -0
  106. package/dist/math/layout.d.ts +13 -0
  107. package/dist/math/layout.js +771 -0
  108. package/dist/math/linear.d.ts +3 -0
  109. package/dist/math/linear.js +152 -0
  110. package/dist/math/omml.d.ts +15 -0
  111. package/dist/math/omml.js +57 -0
  112. package/dist/math/paths.d.ts +61 -0
  113. package/dist/math/paths.js +450 -0
  114. package/dist/math/symbols.d.ts +49 -0
  115. package/dist/math/symbols.js +269 -0
  116. package/dist/math/text.d.ts +53 -0
  117. package/dist/math/text.js +316 -0
  118. package/dist/metafile/canvas.d.ts +80 -0
  119. package/dist/metafile/canvas.js +515 -0
  120. package/dist/metafile/dib.d.ts +27 -0
  121. package/dist/metafile/dib.js +300 -0
  122. package/dist/metafile/emf.d.ts +31 -0
  123. package/dist/metafile/emf.js +591 -0
  124. package/dist/metafile/geometry.d.ts +49 -0
  125. package/dist/metafile/geometry.js +85 -0
  126. package/dist/metafile/index.d.ts +27 -0
  127. package/dist/metafile/index.js +34 -0
  128. package/dist/metafile/reader.d.ts +28 -0
  129. package/dist/metafile/reader.js +59 -0
  130. package/dist/metafile/state.d.ts +118 -0
  131. package/dist/metafile/state.js +172 -0
  132. package/dist/metafile/text.d.ts +17 -0
  133. package/dist/metafile/text.js +130 -0
  134. package/dist/metafile/wmf.d.ts +33 -0
  135. package/dist/metafile/wmf.js +395 -0
  136. package/dist/model/content.d.ts +92 -7
  137. package/dist/model/content.js +251 -17
  138. package/dist/model/document.d.ts +34 -1
  139. package/dist/model/document.js +80 -1
  140. package/dist/model/fields.d.ts +4 -0
  141. package/dist/model/fields.js +10 -0
  142. package/dist/model/numbering.d.ts +2 -0
  143. package/dist/model/numbering.js +22 -0
  144. package/dist/model/props.d.ts +2 -0
  145. package/dist/model/props.js +3 -0
  146. package/dist/opc/index.d.ts +1 -1
  147. package/dist/opc/package.d.ts +34 -0
  148. package/dist/opc/package.js +58 -4
  149. package/dist/pdf/export.js +97 -6
  150. package/dist/pdf/images.d.ts +7 -0
  151. package/dist/pdf/images.js +39 -2
  152. package/dist/render/activity.d.ts +179 -0
  153. package/dist/render/activity.js +1003 -0
  154. package/dist/render/clipboard.d.ts +29 -0
  155. package/dist/render/clipboard.js +401 -0
  156. package/dist/render/comments.d.ts +166 -0
  157. package/dist/render/comments.js +779 -0
  158. package/dist/render/context-menu.d.ts +124 -0
  159. package/dist/render/context-menu.js +525 -0
  160. package/dist/render/editing.d.ts +146 -5
  161. package/dist/render/editing.js +678 -53
  162. package/dist/render/index.d.ts +12 -0
  163. package/dist/render/index.js +11 -0
  164. package/dist/render/navigation-panel.d.ts +76 -0
  165. package/dist/render/navigation-panel.js +431 -0
  166. package/dist/render/selection.js +5 -0
  167. package/dist/render/svg.d.ts +4 -6
  168. package/dist/render/svg.js +69 -7
  169. package/dist/render/toolbar.d.ts +83 -0
  170. package/dist/render/toolbar.js +505 -0
  171. package/dist/render/ui/comment-layout.d.ts +39 -0
  172. package/dist/render/ui/comment-layout.js +79 -0
  173. package/dist/render/ui/icons.d.ts +6 -0
  174. package/dist/render/ui/icons.js +38 -0
  175. package/dist/render/ui/menu.d.ts +38 -0
  176. package/dist/render/ui/menu.js +274 -0
  177. package/dist/render/ui/nav-model.d.ts +11 -0
  178. package/dist/render/ui/nav-model.js +34 -0
  179. package/dist/render/ui/overflow.d.ts +5 -0
  180. package/dist/render/ui/overflow.js +18 -0
  181. package/dist/render/ui/popover.d.ts +18 -0
  182. package/dist/render/ui/popover.js +109 -0
  183. package/dist/render/ui/theme.d.ts +2 -0
  184. package/dist/render/ui/theme.js +29 -0
  185. package/dist/render/undo.d.ts +76 -0
  186. package/dist/render/undo.js +144 -0
  187. package/dist/render/viewer.d.ts +100 -7
  188. package/dist/render/viewer.js +304 -92
  189. package/dist/security/policy.d.ts +3 -0
  190. package/dist/security/policy.js +6 -2
  191. package/dist/security/protection.d.ts +1 -0
  192. package/dist/security/protection.js +1 -1
  193. package/dist/text/bidi-data.d.ts +53 -0
  194. package/dist/text/bidi-data.js +318 -0
  195. package/dist/text/bidi.d.ts +37 -0
  196. package/dist/text/bidi.js +508 -0
  197. package/dist/xml/ns.d.ts +5 -0
  198. package/dist/xml/ns.js +7 -0
  199. package/package.json +9 -2
@@ -0,0 +1,234 @@
1
+ /** Refuse headers that would need gigabytes of RGBA before a single pixel is read. */
2
+ const MAX_PIXELS = 1 << 27;
3
+ const BI_RGB = 0;
4
+ const BI_RLE8 = 1;
5
+ const BI_RLE4 = 2;
6
+ const BI_BITFIELDS = 3;
7
+ const BI_ALPHABITFIELDS = 6;
8
+ export function isBmp(b) {
9
+ if (b.length < 18 || b[0] !== 0x42 || b[1] !== 0x4d)
10
+ return false;
11
+ // "BM" alone is too weak a signature (text can start with it), so check the DIB header size too.
12
+ const size = (b[14] | (b[15] << 8) | (b[16] << 16) | (b[17] << 24)) >>> 0;
13
+ return size === 12 || (size >= 16 && size <= 64) || size === 108 || size === 124;
14
+ }
15
+ function channel(mask) {
16
+ mask >>>= 0;
17
+ if (!mask)
18
+ return { mask: 0, shift: 0, max: 0 };
19
+ let shift = 0;
20
+ while (!((mask >>> shift) & 1))
21
+ shift++;
22
+ return { mask, shift, max: mask >>> shift };
23
+ }
24
+ /** Scale a bitfield to 8 bits; a 5-bit 31 must become 255, not 248. */
25
+ function extract(px, c) {
26
+ if (!c.max)
27
+ return 0;
28
+ const v = (px & c.mask) >>> c.shift;
29
+ return c.max === 255 ? v : Math.round((v * 255) / c.max);
30
+ }
31
+ /** Decode a .bmp file (with its 14-byte BITMAPFILEHEADER). */
32
+ export function decodeBmp(bytes) {
33
+ if (bytes.length < 26 || bytes[0] !== 0x42 || bytes[1] !== 0x4d)
34
+ throw new Error('BMP: missing "BM" signature');
35
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
36
+ const dataOffset = view.getUint32(10, true);
37
+ const headerSize = view.getUint32(14, true);
38
+ if (headerSize < 12 || 14 + headerSize > bytes.length)
39
+ throw new Error('BMP: truncated header');
40
+ let width;
41
+ let height;
42
+ let bpp;
43
+ let compression = BI_RGB;
44
+ let colorsUsed = 0;
45
+ let topDown = false;
46
+ let entrySize = 4;
47
+ if (headerSize === 12) {
48
+ width = view.getUint16(18, true);
49
+ height = view.getUint16(20, true);
50
+ bpp = view.getUint16(24, true);
51
+ entrySize = 3;
52
+ }
53
+ else {
54
+ width = view.getInt32(18, true);
55
+ const h = view.getInt32(22, true);
56
+ bpp = view.getUint16(28, true);
57
+ // OS/2 2.x headers may stop early; missing fields are zero.
58
+ if (headerSize >= 20)
59
+ compression = view.getUint32(30, true);
60
+ if (headerSize >= 36)
61
+ colorsUsed = view.getUint32(46, true);
62
+ topDown = h < 0;
63
+ height = Math.abs(h);
64
+ }
65
+ // OS/2 2.x reuses compression 3 and 4 for Huffman 1D and RLE24, which Windows never wrote.
66
+ const os2 = headerSize > 12 && headerSize <= 64 && headerSize !== 40 && headerSize !== 52 && headerSize !== 56;
67
+ if (width <= 0 || height <= 0)
68
+ throw new Error(`BMP: invalid size ${width}x${height}`);
69
+ if (width * height > MAX_PIXELS)
70
+ throw new Error(`BMP: ${width}x${height} is too large to decode`);
71
+ if (![1, 2, 4, 8, 16, 24, 32].includes(bpp))
72
+ throw new Error(`BMP: ${bpp} bits per pixel is not supported`);
73
+ const bitfields = !os2 && (compression === BI_BITFIELDS || compression === BI_ALPHABITFIELDS);
74
+ if (compression === BI_RLE8 && bpp !== 8)
75
+ throw new Error('BMP: RLE8 needs 8 bits per pixel');
76
+ if (compression === BI_RLE4 && bpp !== 4)
77
+ throw new Error('BMP: RLE4 needs 4 bits per pixel');
78
+ if (bitfields && bpp !== 16 && bpp !== 32)
79
+ throw new Error(`BMP: bitfields need 16 or 32 bits per pixel, not ${bpp}`);
80
+ if (compression > BI_RLE4 && !bitfields) {
81
+ const what = os2 && compression === 3 ? 'OS/2 Huffman' : os2 && compression === 4 ? 'OS/2 RLE24' : compression === 4 ? 'embedded JPEG' : compression === 5 ? 'embedded PNG' : `compression ${compression}`;
82
+ throw new Error(`BMP: ${what} is not supported`);
83
+ }
84
+ let red = channel(0x7c00);
85
+ let green = channel(0x03e0);
86
+ let blue = channel(0x001f);
87
+ let alphaChannel;
88
+ if (bitfields) {
89
+ // The masks sit right after the 40-byte core, inside a V2+ header or following a 40-byte one.
90
+ const alphaInHeader = headerSize >= 56 || compression === BI_ALPHABITFIELDS;
91
+ if (54 + (alphaInHeader ? 16 : 12) > bytes.length)
92
+ throw new Error('BMP: truncated bitfield masks');
93
+ red = channel(view.getUint32(54, true));
94
+ green = channel(view.getUint32(58, true));
95
+ blue = channel(view.getUint32(62, true));
96
+ const alphaMask = alphaInHeader ? view.getUint32(66, true) : 0;
97
+ if (alphaMask)
98
+ alphaChannel = channel(alphaMask);
99
+ }
100
+ const paletteOffset = 14 + headerSize + (headerSize === 40 && compression === BI_BITFIELDS ? 12 : headerSize === 40 && compression === BI_ALPHABITFIELDS ? 16 : 0);
101
+ let paletteCount = 0;
102
+ if (bpp <= 8) {
103
+ paletteCount = colorsUsed > 0 && colorsUsed < 1 << bpp ? colorsUsed : 1 << bpp;
104
+ // Some writers store fewer entries than they declare; the pixel data offset tells.
105
+ if (dataOffset > paletteOffset)
106
+ paletteCount = Math.min(paletteCount, Math.floor((dataOffset - paletteOffset) / entrySize));
107
+ paletteCount = Math.max(0, Math.min(paletteCount, Math.floor((bytes.length - paletteOffset) / entrySize)));
108
+ }
109
+ const palette = new Uint8Array(256 * 3);
110
+ for (let i = 0; i < paletteCount; i++) {
111
+ const q = paletteOffset + i * entrySize;
112
+ palette[i * 3] = bytes[q + 2];
113
+ palette[i * 3 + 1] = bytes[q + 1];
114
+ palette[i * 3 + 2] = bytes[q];
115
+ }
116
+ const pixelOffset = dataOffset > 0 && dataOffset < bytes.length ? dataOffset : paletteOffset + paletteCount * entrySize;
117
+ const rgba = new Uint8Array(width * height * 4);
118
+ if (compression === BI_RLE8 || compression === BI_RLE4) {
119
+ decodeRle(bytes, pixelOffset, width, height, topDown, palette, compression === BI_RLE4, rgba);
120
+ return { width, height, rgba };
121
+ }
122
+ const stride = Math.floor((bpp * width + 31) / 32) * 4;
123
+ const rowBytes = Math.ceil((bpp * width) / 8);
124
+ // 32-bit BI_RGB has an unused byte that many writers fill with alpha and many leave at zero.
125
+ const alphaByte = bpp === 32 && !bitfields;
126
+ const hasAlpha = alphaByte || alphaChannel !== undefined;
127
+ let anyAlpha = false;
128
+ let decodedRows = 0;
129
+ const indexMask = (1 << bpp) - 1;
130
+ for (let row = 0; row < height; row++) {
131
+ const p = pixelOffset + row * stride;
132
+ if (p + rowBytes > bytes.length)
133
+ break;
134
+ const y = topDown ? row : height - 1 - row;
135
+ let o = y * width * 4;
136
+ for (let x = 0; x < width; x++, o += 4) {
137
+ let a = 255;
138
+ if (bpp <= 8) {
139
+ const bit = x * bpp;
140
+ const idx = (bytes[p + (bit >> 3)] >> (8 - bpp - (bit & 7))) & indexMask;
141
+ rgba[o] = palette[idx * 3];
142
+ rgba[o + 1] = palette[idx * 3 + 1];
143
+ rgba[o + 2] = palette[idx * 3 + 2];
144
+ }
145
+ else if (bpp === 24) {
146
+ const q = p + x * 3;
147
+ rgba[o] = bytes[q + 2];
148
+ rgba[o + 1] = bytes[q + 1];
149
+ rgba[o + 2] = bytes[q];
150
+ }
151
+ else if (alphaByte) {
152
+ const q = p + x * 4;
153
+ rgba[o] = bytes[q + 2];
154
+ rgba[o + 1] = bytes[q + 1];
155
+ rgba[o + 2] = bytes[q];
156
+ a = bytes[q + 3];
157
+ }
158
+ else {
159
+ const px = bpp === 16 ? view.getUint16(p + x * 2, true) : view.getUint32(p + x * 4, true);
160
+ rgba[o] = extract(px, red);
161
+ rgba[o + 1] = extract(px, green);
162
+ rgba[o + 2] = extract(px, blue);
163
+ if (alphaChannel)
164
+ a = extract(px, alphaChannel);
165
+ }
166
+ if (a)
167
+ anyAlpha = true;
168
+ rgba[o + 3] = a;
169
+ }
170
+ decodedRows++;
171
+ }
172
+ // An alpha channel that is zero everywhere was never meant as alpha: show the image opaque.
173
+ if (hasAlpha && !anyAlpha) {
174
+ for (let row = 0; row < decodedRows; row++) {
175
+ const y = topDown ? row : height - 1 - row;
176
+ for (let x = 0; x < width; x++)
177
+ rgba[(y * width + x) * 4 + 3] = 255;
178
+ }
179
+ }
180
+ return { width, height, rgba };
181
+ }
182
+ /**
183
+ * BI_RLE8 / BI_RLE4. Pixels the stream skips (with a delta, an early end of line or end of
184
+ * bitmap) stay transparent, as browsers show them.
185
+ */
186
+ function decodeRle(bytes, start, width, height, topDown, palette, four, rgba) {
187
+ let x = 0;
188
+ let row = 0;
189
+ let p = start;
190
+ const put = (idx) => {
191
+ if (x < width && row < height) {
192
+ const y = topDown ? row : height - 1 - row;
193
+ const o = (y * width + x) * 4;
194
+ rgba[o] = palette[idx * 3];
195
+ rgba[o + 1] = palette[idx * 3 + 1];
196
+ rgba[o + 2] = palette[idx * 3 + 2];
197
+ rgba[o + 3] = 255;
198
+ }
199
+ x++;
200
+ };
201
+ while (p + 1 < bytes.length && row < height) {
202
+ const n = bytes[p++];
203
+ const c = bytes[p++];
204
+ if (n > 0) {
205
+ // Encoded run: in RLE4 the two nibbles of c alternate.
206
+ for (let i = 0; i < n; i++)
207
+ put(four ? (i & 1 ? c & 15 : c >> 4) : c);
208
+ }
209
+ else if (c === 0) {
210
+ x = 0;
211
+ row++;
212
+ }
213
+ else if (c === 1) {
214
+ break;
215
+ }
216
+ else if (c === 2) {
217
+ if (p + 1 >= bytes.length)
218
+ break;
219
+ x += bytes[p++];
220
+ row += bytes[p++];
221
+ }
222
+ else {
223
+ // Absolute run of c pixels, padded to a 16-bit boundary.
224
+ const count = four ? (c + 1) >> 1 : c;
225
+ if (p + count > bytes.length)
226
+ break;
227
+ for (let i = 0; i < c; i++) {
228
+ const v = bytes[p + (four ? i >> 1 : i)];
229
+ put(four ? (i & 1 ? v & 15 : v >> 4) : v);
230
+ }
231
+ p += count + (count & 1);
232
+ }
233
+ }
234
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * GIF decoder (GIF87a and GIF89a) producing RGBA pixels. PDF pages are static, so only the first
3
+ * frame is decoded. It is composited onto a transparent canvas of the logical screen size; the
4
+ * background colour index is ignored, as browsers ignore it.
5
+ */
6
+ import type { RasterImage } from '../layout/types.js';
7
+ export declare function isGif(b: Uint8Array): boolean;
8
+ /** Decode the first frame of a GIF. */
9
+ export declare function decodeGif(bytes: Uint8Array): RasterImage;
@@ -0,0 +1,215 @@
1
+ import { concatBytes } from '../util/bytes.js';
2
+ /** Refuse headers that would need gigabytes of RGBA before a single pixel is read. */
3
+ const MAX_PIXELS = 1 << 27;
4
+ export function isGif(b) {
5
+ return b.length >= 6 && b[0] === 0x47 && b[1] === 0x49 && b[2] === 0x46 && b[3] === 0x38 && (b[4] === 0x37 || b[4] === 0x39) && b[5] === 0x61;
6
+ }
7
+ /**
8
+ * GIF's variable-width LZW: codes are packed LSB-first, the width grows as soon as the next free
9
+ * code no longer fits, and stays at 12 bits once the table is full until a clear code arrives
10
+ * (encoders may defer the clear). Returns how many indices were written, so a truncated or damaged
11
+ * stream still yields the pixels before the damage.
12
+ */
13
+ function lzwDecode(data, minCodeSize, out) {
14
+ const clear = 1 << minCodeSize;
15
+ const end = clear + 1;
16
+ const prefix = new Uint16Array(4096);
17
+ const suffix = new Uint8Array(4096);
18
+ const first = new Uint8Array(4096);
19
+ const length = new Uint16Array(4096);
20
+ for (let i = 0; i < clear; i++) {
21
+ suffix[i] = i;
22
+ first[i] = i;
23
+ length[i] = 1;
24
+ }
25
+ let codeSize = minCodeSize + 1;
26
+ let next = clear + 2;
27
+ let prev = -1;
28
+ let acc = 0;
29
+ let bits = 0;
30
+ let pos = 0;
31
+ let n = 0;
32
+ while (n < out.length) {
33
+ while (bits < codeSize) {
34
+ if (pos >= data.length)
35
+ return n;
36
+ acc |= data[pos++] << bits;
37
+ bits += 8;
38
+ }
39
+ const code = acc & ((1 << codeSize) - 1);
40
+ acc >>>= codeSize;
41
+ bits -= codeSize;
42
+ if (code === clear) {
43
+ codeSize = minCodeSize + 1;
44
+ next = clear + 2;
45
+ prev = -1;
46
+ continue;
47
+ }
48
+ if (code === end)
49
+ break;
50
+ if (prev < 0) {
51
+ // The first code after a clear must be a literal; anything else means corrupt data.
52
+ if (code > clear)
53
+ break;
54
+ out[n++] = code;
55
+ prev = code;
56
+ continue;
57
+ }
58
+ if (code > next)
59
+ break;
60
+ if (next < 4096) {
61
+ // code === next is the "KwKwK" case: the string is prev's plus prev's first byte.
62
+ prefix[next] = prev;
63
+ first[next] = first[prev];
64
+ suffix[next] = code === next ? first[prev] : first[code];
65
+ length[next] = length[prev] + 1;
66
+ next++;
67
+ if (next === 1 << codeSize && codeSize < 12)
68
+ codeSize++;
69
+ }
70
+ const len = length[code];
71
+ let c = code;
72
+ for (let i = n + len - 1; i >= n; i--) {
73
+ if (i < out.length)
74
+ out[i] = suffix[c];
75
+ c = prefix[c];
76
+ }
77
+ n += len;
78
+ prev = code;
79
+ }
80
+ return Math.min(n, out.length);
81
+ }
82
+ /** Row order of an interlaced image: every 8th row from 0, every 8th from 4, every 4th from 2, every 2nd from 1. */
83
+ function interlacedRows(height) {
84
+ const rows = new Uint32Array(height);
85
+ let i = 0;
86
+ for (const [start, step] of [
87
+ [0, 8],
88
+ [4, 8],
89
+ [2, 4],
90
+ [1, 2],
91
+ ]) {
92
+ for (let y = start; y < height; y += step)
93
+ rows[i++] = y;
94
+ }
95
+ return rows;
96
+ }
97
+ function skipSubBlocks(b, p) {
98
+ while (p < b.length) {
99
+ const n = b[p++];
100
+ if (n === 0)
101
+ break;
102
+ p += n;
103
+ }
104
+ return p;
105
+ }
106
+ /** Decode the first frame of a GIF. */
107
+ export function decodeGif(bytes) {
108
+ if (!isGif(bytes))
109
+ throw new Error('GIF: missing GIF87a/GIF89a signature');
110
+ if (bytes.length < 13)
111
+ throw new Error('GIF: truncated header');
112
+ const u16 = (p) => bytes[p] | (bytes[p + 1] << 8);
113
+ const screenWidth = u16(6);
114
+ const screenHeight = u16(8);
115
+ const flags = bytes[10];
116
+ let p = 13;
117
+ let globalTable;
118
+ if (flags & 0x80) {
119
+ const size = 3 << ((flags & 7) + 1);
120
+ globalTable = bytes.subarray(p, p + size);
121
+ p += size;
122
+ }
123
+ let transparent = -1;
124
+ while (p < bytes.length) {
125
+ const block = bytes[p++];
126
+ if (block === 0x2c)
127
+ return decodeFrame(bytes, p, screenWidth, screenHeight, globalTable, transparent);
128
+ if (block === 0x21) {
129
+ const label = bytes[p++];
130
+ if (label === 0xf9 && (bytes[p] ?? 0) >= 4)
131
+ transparent = (bytes[p + 1] ?? 0) & 1 ? (bytes[p + 4] ?? -1) : -1;
132
+ // A Graphic Control Extension applies to the next graphic block only, which may be plain text.
133
+ else if (label === 0x01)
134
+ transparent = -1;
135
+ p = skipSubBlocks(bytes, p);
136
+ continue;
137
+ }
138
+ if (block === 0x3b)
139
+ break;
140
+ throw new Error(`GIF: unexpected block 0x${block.toString(16)} at offset ${p - 1}`);
141
+ }
142
+ throw new Error('GIF: no image in file');
143
+ }
144
+ function decodeFrame(bytes, p, screenWidth, screenHeight, globalTable, transparent) {
145
+ if (p + 9 > bytes.length)
146
+ throw new Error('GIF: truncated image descriptor');
147
+ const u16 = (q) => bytes[q] | (bytes[q + 1] << 8);
148
+ let left = u16(p);
149
+ let top = u16(p + 2);
150
+ const w = u16(p + 4);
151
+ const h = u16(p + 6);
152
+ const flags = bytes[p + 8];
153
+ p += 9;
154
+ let table = globalTable;
155
+ if (flags & 0x80) {
156
+ const size = 3 << ((flags & 7) + 1);
157
+ table = bytes.subarray(p, p + size);
158
+ p += size;
159
+ }
160
+ const interlaced = (flags & 0x40) !== 0;
161
+ const minCodeSize = bytes[p++];
162
+ if (minCodeSize === undefined || minCodeSize < 2 || minCodeSize > 8)
163
+ throw new Error(`GIF: invalid LZW code size ${minCodeSize}`);
164
+ const chunks = [];
165
+ while (p < bytes.length) {
166
+ const n = bytes[p++];
167
+ if (n === 0)
168
+ break;
169
+ chunks.push(bytes.subarray(p, p + n));
170
+ p += n;
171
+ }
172
+ // Browsers grow a logical screen that is too small for the first frame (or zero) to fit it,
173
+ // and place the frame at the origin; files written that way are common enough to follow suit.
174
+ let width = screenWidth;
175
+ let height = screenHeight;
176
+ if (w > width || h > height) {
177
+ width = Math.max(width, w);
178
+ height = Math.max(height, h);
179
+ left = 0;
180
+ top = 0;
181
+ }
182
+ if (!width || !height)
183
+ throw new Error('GIF: image has no size');
184
+ if (width * height > MAX_PIXELS)
185
+ throw new Error(`GIF: ${width}x${height} is too large to decode`);
186
+ const rgba = new Uint8Array(width * height * 4);
187
+ if (!w || !h)
188
+ return { width, height, rgba };
189
+ const indices = new Uint8Array(w * h);
190
+ const count = lzwDecode(concatBytes(chunks), minCodeSize, indices);
191
+ const rows = interlaced ? interlacedRows(h) : undefined;
192
+ // Without any colour table the indices are shown as grey levels.
193
+ const colors = table ? Math.floor(table.length / 3) : 256;
194
+ for (let i = 0; i < count; i++) {
195
+ const idx = indices[i];
196
+ if (idx === transparent || idx >= colors)
197
+ continue;
198
+ const r = (i / w) | 0;
199
+ const x = left + i - r * w;
200
+ const y = top + (rows ? rows[r] : r);
201
+ if (x >= width || y >= height)
202
+ continue;
203
+ const o = (y * width + x) * 4;
204
+ if (table) {
205
+ rgba[o] = table[idx * 3];
206
+ rgba[o + 1] = table[idx * 3 + 1];
207
+ rgba[o + 2] = table[idx * 3 + 2];
208
+ }
209
+ else {
210
+ rgba[o] = rgba[o + 1] = rgba[o + 2] = idx;
211
+ }
212
+ rgba[o + 3] = 255;
213
+ }
214
+ return { width, height, rgba };
215
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Image format detection and decoding for the bitmaps a .docx may embed. Word keeps the original
3
+ * bytes of an inserted picture, and the part's file extension or content type is not reliable, so
4
+ * the format is sniffed from the bytes. GIF, BMP and TIFF are decoded to RGBA here because PDF
5
+ * cannot embed them as they are (and most browsers cannot show TIFF).
6
+ */
7
+ import type { RasterImage } from '../layout/types.js';
8
+ import { decodeBmp } from './bmp.js';
9
+ import { decodeGif } from './gif.js';
10
+ import { decodeTiff } from './tiff.js';
11
+ export { decodeBmp, decodeGif, decodeTiff };
12
+ export type ImageKind = 'png' | 'jpeg' | 'gif' | 'bmp' | 'tiff' | 'emf' | 'wmf' | 'svg';
13
+ /** Identify an image from its leading bytes. */
14
+ export declare function sniffImage(bytes: Uint8Array): ImageKind | undefined;
15
+ /** Whether every current browser can show this kind of image in an <img> as it is. */
16
+ export declare function browserShows(kind: ImageKind | undefined): boolean;
17
+ /** Decode a GIF (first frame), BMP or TIFF (first page) to RGBA; throws for anything else. */
18
+ export declare function decodeRaster(bytes: Uint8Array): Promise<RasterImage>;
@@ -0,0 +1,63 @@
1
+ import { decodeBmp, isBmp } from './bmp.js';
2
+ import { decodeGif, isGif } from './gif.js';
3
+ import { decodeTiff, isTiff } from './tiff.js';
4
+ export { decodeBmp, decodeGif, decodeTiff };
5
+ function isEmf(b) {
6
+ // EMR_HEADER (record type 1) first, with the " EMF" signature in its dSignature field.
7
+ return b.length >= 44 && b[0] === 1 && b[1] === 0 && b[2] === 0 && b[3] === 0 && b[40] === 0x20 && b[41] === 0x45 && b[42] === 0x4d && b[43] === 0x46;
8
+ }
9
+ function isWmf(b) {
10
+ if (b.length >= 4 && b[0] === 0xd7 && b[1] === 0xcd && b[2] === 0xc6 && b[3] === 0x9a)
11
+ return true; // placeable (Aldus) header
12
+ if (b.length < 6)
13
+ return false;
14
+ const type = b[0] | (b[1] << 8);
15
+ const headerWords = b[2] | (b[3] << 8);
16
+ const version = b[4] | (b[5] << 8);
17
+ return (type === 1 || type === 2) && headerWords === 9 && (version === 0x0100 || version === 0x0300);
18
+ }
19
+ function isSvg(b) {
20
+ let text = '';
21
+ const n = Math.min(b.length, 4096);
22
+ for (let i = 0; i < n; i++)
23
+ text += String.fromCharCode(b[i]);
24
+ // Allow a UTF-8 BOM, an XML declaration, comments and a doctype before the root element.
25
+ text = text.replace(/^\xef\xbb\xbf/, '').trimStart();
26
+ return text.startsWith('<') && /<svg[\s/>]/i.test(text);
27
+ }
28
+ /** Identify an image from its leading bytes. */
29
+ export function sniffImage(bytes) {
30
+ const b = bytes;
31
+ if (b.length >= 4 && b[0] === 0x89 && b[1] === 0x50 && b[2] === 0x4e && b[3] === 0x47)
32
+ return 'png';
33
+ if (b.length >= 3 && b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff)
34
+ return 'jpeg';
35
+ if (isGif(b))
36
+ return 'gif';
37
+ if (isBmp(b))
38
+ return 'bmp';
39
+ if (isTiff(b))
40
+ return 'tiff';
41
+ if (isEmf(b))
42
+ return 'emf';
43
+ if (isWmf(b))
44
+ return 'wmf';
45
+ if (isSvg(b))
46
+ return 'svg';
47
+ return undefined;
48
+ }
49
+ /** Whether every current browser can show this kind of image in an <img> as it is. */
50
+ export function browserShows(kind) {
51
+ return kind === 'png' || kind === 'jpeg' || kind === 'gif' || kind === 'bmp' || kind === 'svg';
52
+ }
53
+ /** Decode a GIF (first frame), BMP or TIFF (first page) to RGBA; throws for anything else. */
54
+ export async function decodeRaster(bytes) {
55
+ const kind = sniffImage(bytes);
56
+ if (kind === 'gif')
57
+ return decodeGif(bytes);
58
+ if (kind === 'bmp')
59
+ return decodeBmp(bytes);
60
+ if (kind === 'tiff')
61
+ return decodeTiff(bytes);
62
+ throw new Error(`Cannot decode ${kind ? kind.toUpperCase() : 'unrecognised'} images to pixels`);
63
+ }
@@ -0,0 +1,4 @@
1
+ import type { RasterImage } from '../layout/types.js';
2
+ export declare function encodePng(image: RasterImage): Uint8Array;
3
+ /** A data: URL for the image (for SVG <image href>). */
4
+ export declare function pngDataUrl(image: RasterImage): string;
@@ -0,0 +1,73 @@
1
+ /**
2
+ * A synchronous PNG encoder for decoded pixels (RGBA). It stores the image data uncompressed
3
+ * (deflate "stored" blocks), which every PNG reader accepts, so it needs no compressor and can run
4
+ * while an SVG page is being built. Used for bitmaps that browsers cannot show as they are.
5
+ */
6
+ import { crc32 } from '../util/crc32.js';
7
+ import { base64Encode } from '../util/text.js';
8
+ function adler32(data) {
9
+ let a = 1;
10
+ let b = 0;
11
+ for (let i = 0; i < data.length; i++) {
12
+ a = (a + data[i]) % 65521;
13
+ b = (b + a) % 65521;
14
+ }
15
+ return ((b << 16) | a) >>> 0;
16
+ }
17
+ function chunk(type, data) {
18
+ const out = new Uint8Array(12 + data.length);
19
+ const view = new DataView(out.buffer);
20
+ view.setUint32(0, data.length);
21
+ for (let i = 0; i < 4; i++)
22
+ out[4 + i] = type.charCodeAt(i);
23
+ out.set(data, 8);
24
+ view.setUint32(8 + data.length, crc32(out.subarray(4, 8 + data.length)));
25
+ return out;
26
+ }
27
+ /** zlib stream made of stored (uncompressed) deflate blocks. */
28
+ function zlibStored(data) {
29
+ const blocks = Math.max(1, Math.ceil(data.length / 65535));
30
+ const out = new Uint8Array(2 + blocks * 5 + data.length + 4);
31
+ out[0] = 0x78;
32
+ out[1] = 0x01;
33
+ let o = 2;
34
+ for (let i = 0; i < blocks; i++) {
35
+ const start = i * 65535;
36
+ const len = Math.min(65535, data.length - start);
37
+ out[o++] = i === blocks - 1 ? 1 : 0;
38
+ out[o++] = len & 0xff;
39
+ out[o++] = len >>> 8;
40
+ out[o++] = ~len & 0xff;
41
+ out[o++] = (~len >>> 8) & 0xff;
42
+ out.set(data.subarray(start, start + len), o);
43
+ o += len;
44
+ }
45
+ new DataView(out.buffer).setUint32(o, adler32(data));
46
+ return out;
47
+ }
48
+ export function encodePng(image) {
49
+ const { width, height, rgba } = image;
50
+ const raw = new Uint8Array(height * (width * 4 + 1));
51
+ for (let y = 0; y < height; y++) {
52
+ raw[y * (width * 4 + 1)] = 0; // filter: none
53
+ raw.set(rgba.subarray(y * width * 4, (y + 1) * width * 4), y * (width * 4 + 1) + 1);
54
+ }
55
+ const ihdr = new Uint8Array(13);
56
+ const v = new DataView(ihdr.buffer);
57
+ v.setUint32(0, width);
58
+ v.setUint32(4, height);
59
+ ihdr[8] = 8; // bit depth
60
+ ihdr[9] = 6; // RGBA
61
+ const parts = [new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk('IHDR', ihdr), chunk('IDAT', zlibStored(raw)), chunk('IEND', new Uint8Array(0))];
62
+ const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
63
+ let o = 0;
64
+ for (const p of parts) {
65
+ out.set(p, o);
66
+ o += p.length;
67
+ }
68
+ return out;
69
+ }
70
+ /** A data: URL for the image (for SVG <image href>). */
71
+ export function pngDataUrl(image) {
72
+ return `data:image/png;base64,${base64Encode(encodePng(image))}`;
73
+ }
@@ -0,0 +1,10 @@
1
+ /** Pixel size (and resolution, when the file says) of PNG, JPEG, GIF, BMP and TIFF images, from their headers. */
2
+ import { type ImageKind } from './index.js';
3
+ export interface ImageInfo {
4
+ kind: ImageKind;
5
+ /** Natural size in points (pixels at the image's DPI, 96 when unknown). */
6
+ width: number;
7
+ height: number;
8
+ }
9
+ /** Format and natural size of an image, or undefined when it is not one docxkeun can place. */
10
+ export declare function imageInfo(bytes: Uint8Array): ImageInfo | undefined;