docxkeun 0.1.3 → 0.2.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 (197) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/README.md +137 -16
  3. package/dist/api/editor.d.ts +172 -5
  4. package/dist/api/editor.js +814 -24
  5. package/dist/api/headers.d.ts +51 -0
  6. package/dist/api/headers.js +133 -0
  7. package/dist/api/index.d.ts +5 -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 +390 -0
  15. package/dist/api/selection.d.ts +57 -0
  16. package/dist/api/selection.js +91 -0
  17. package/dist/charts/color.d.ts +45 -0
  18. package/dist/charts/color.js +322 -0
  19. package/dist/charts/format.d.ts +12 -0
  20. package/dist/charts/format.js +459 -0
  21. package/dist/charts/index.d.ts +11 -0
  22. package/dist/charts/index.js +8 -0
  23. package/dist/charts/model.d.ts +225 -0
  24. package/dist/charts/model.js +758 -0
  25. package/dist/charts/outline.d.ts +17 -0
  26. package/dist/charts/outline.js +214 -0
  27. package/dist/charts/render.d.ts +20 -0
  28. package/dist/charts/render.js +1772 -0
  29. package/dist/drawing/arrows.d.ts +37 -0
  30. package/dist/drawing/arrows.js +99 -0
  31. package/dist/drawing/formula.d.ts +40 -0
  32. package/dist/drawing/formula.js +159 -0
  33. package/dist/drawing/geometry.d.ts +61 -0
  34. package/dist/drawing/geometry.js +286 -0
  35. package/dist/drawing/index.d.ts +12 -0
  36. package/dist/drawing/index.js +8 -0
  37. package/dist/drawing/presets.d.ts +72 -0
  38. package/dist/drawing/presets.js +2874 -0
  39. package/dist/fonts/shaping/apply.d.ts +94 -0
  40. package/dist/fonts/shaping/apply.js +349 -0
  41. package/dist/fonts/shaping/arabic.d.ts +28 -0
  42. package/dist/fonts/shaping/arabic.js +235 -0
  43. package/dist/fonts/shaping/buffer.d.ts +53 -0
  44. package/dist/fonts/shaping/buffer.js +95 -0
  45. package/dist/fonts/shaping/gdef.d.ts +30 -0
  46. package/dist/fonts/shaping/gdef.js +66 -0
  47. package/dist/fonts/shaping/gpos.d.ts +86 -0
  48. package/dist/fonts/shaping/gpos.js +428 -0
  49. package/dist/fonts/shaping/gsub.d.ts +65 -0
  50. package/dist/fonts/shaping/gsub.js +313 -0
  51. package/dist/fonts/shaping/index.d.ts +32 -0
  52. package/dist/fonts/shaping/index.js +402 -0
  53. package/dist/fonts/shaping/indic.d.ts +6 -0
  54. package/dist/fonts/shaping/indic.js +805 -0
  55. package/dist/fonts/shaping/layout-common.d.ts +124 -0
  56. package/dist/fonts/shaping/layout-common.js +349 -0
  57. package/dist/fonts/shaping/plan.d.ts +87 -0
  58. package/dist/fonts/shaping/plan.js +152 -0
  59. package/dist/fonts/shaping/scripts.d.ts +26 -0
  60. package/dist/fonts/shaping/scripts.js +160 -0
  61. package/dist/fonts/shaping/shaper.d.ts +23 -0
  62. package/dist/fonts/shaping/shaper.js +5 -0
  63. package/dist/fonts/shaping/unicode.d.ts +34 -0
  64. package/dist/fonts/shaping/unicode.js +138 -0
  65. package/dist/images/bmp.d.ts +9 -0
  66. package/dist/images/bmp.js +234 -0
  67. package/dist/images/gif.d.ts +9 -0
  68. package/dist/images/gif.js +215 -0
  69. package/dist/images/index.d.ts +18 -0
  70. package/dist/images/index.js +63 -0
  71. package/dist/images/png-encode.d.ts +4 -0
  72. package/dist/images/png-encode.js +73 -0
  73. package/dist/images/size.d.ts +10 -0
  74. package/dist/images/size.js +83 -0
  75. package/dist/images/tiff.d.ts +14 -0
  76. package/dist/images/tiff.js +407 -0
  77. package/dist/index.d.ts +7 -2
  78. package/dist/index.js +5 -0
  79. package/dist/layout/bidi.d.ts +18 -0
  80. package/dist/layout/bidi.js +71 -0
  81. package/dist/layout/blocks.d.ts +20 -3
  82. package/dist/layout/blocks.js +88 -58
  83. package/dist/layout/cache.d.ts +31 -0
  84. package/dist/layout/cache.js +38 -0
  85. package/dist/layout/drawing.d.ts +30 -0
  86. package/dist/layout/drawing.js +120 -0
  87. package/dist/layout/engine.d.ts +3 -0
  88. package/dist/layout/engine.js +218 -11
  89. package/dist/layout/index.d.ts +1 -0
  90. package/dist/layout/index.js +1 -0
  91. package/dist/layout/lines.d.ts +63 -5
  92. package/dist/layout/lines.js +245 -20
  93. package/dist/layout/paint.d.ts +25 -3
  94. package/dist/layout/paint.js +26 -10
  95. package/dist/layout/shapes.d.ts +7 -0
  96. package/dist/layout/shapes.js +338 -0
  97. package/dist/layout/style.d.ts +4 -0
  98. package/dist/layout/style.js +2 -0
  99. package/dist/layout/types.d.ts +89 -2
  100. package/dist/math/boxes.d.ts +88 -0
  101. package/dist/math/boxes.js +166 -0
  102. package/dist/math/index.d.ts +29 -0
  103. package/dist/math/index.js +44 -0
  104. package/dist/math/layout.d.ts +13 -0
  105. package/dist/math/layout.js +771 -0
  106. package/dist/math/linear.d.ts +3 -0
  107. package/dist/math/linear.js +152 -0
  108. package/dist/math/omml.d.ts +15 -0
  109. package/dist/math/omml.js +57 -0
  110. package/dist/math/paths.d.ts +61 -0
  111. package/dist/math/paths.js +450 -0
  112. package/dist/math/symbols.d.ts +49 -0
  113. package/dist/math/symbols.js +269 -0
  114. package/dist/math/text.d.ts +53 -0
  115. package/dist/math/text.js +316 -0
  116. package/dist/metafile/canvas.d.ts +80 -0
  117. package/dist/metafile/canvas.js +515 -0
  118. package/dist/metafile/dib.d.ts +27 -0
  119. package/dist/metafile/dib.js +300 -0
  120. package/dist/metafile/emf.d.ts +31 -0
  121. package/dist/metafile/emf.js +591 -0
  122. package/dist/metafile/geometry.d.ts +49 -0
  123. package/dist/metafile/geometry.js +85 -0
  124. package/dist/metafile/index.d.ts +27 -0
  125. package/dist/metafile/index.js +34 -0
  126. package/dist/metafile/reader.d.ts +28 -0
  127. package/dist/metafile/reader.js +59 -0
  128. package/dist/metafile/state.d.ts +118 -0
  129. package/dist/metafile/state.js +172 -0
  130. package/dist/metafile/text.d.ts +17 -0
  131. package/dist/metafile/text.js +130 -0
  132. package/dist/metafile/wmf.d.ts +33 -0
  133. package/dist/metafile/wmf.js +395 -0
  134. package/dist/model/content.d.ts +92 -7
  135. package/dist/model/content.js +251 -17
  136. package/dist/model/document.d.ts +34 -1
  137. package/dist/model/document.js +80 -1
  138. package/dist/model/fields.d.ts +4 -0
  139. package/dist/model/fields.js +10 -0
  140. package/dist/model/numbering.d.ts +2 -0
  141. package/dist/model/numbering.js +22 -0
  142. package/dist/model/props.d.ts +2 -0
  143. package/dist/model/props.js +3 -0
  144. package/dist/opc/index.d.ts +1 -1
  145. package/dist/opc/package.d.ts +34 -0
  146. package/dist/opc/package.js +58 -4
  147. package/dist/pdf/export.js +97 -6
  148. package/dist/pdf/images.d.ts +7 -0
  149. package/dist/pdf/images.js +39 -2
  150. package/dist/render/activity.d.ts +138 -0
  151. package/dist/render/activity.js +794 -0
  152. package/dist/render/clipboard.d.ts +29 -0
  153. package/dist/render/clipboard.js +401 -0
  154. package/dist/render/comments.d.ts +166 -0
  155. package/dist/render/comments.js +779 -0
  156. package/dist/render/context-menu.d.ts +124 -0
  157. package/dist/render/context-menu.js +525 -0
  158. package/dist/render/editing.d.ts +146 -5
  159. package/dist/render/editing.js +678 -53
  160. package/dist/render/index.d.ts +12 -0
  161. package/dist/render/index.js +11 -0
  162. package/dist/render/navigation-panel.d.ts +76 -0
  163. package/dist/render/navigation-panel.js +431 -0
  164. package/dist/render/selection.js +5 -0
  165. package/dist/render/svg.d.ts +4 -6
  166. package/dist/render/svg.js +69 -7
  167. package/dist/render/toolbar.d.ts +83 -0
  168. package/dist/render/toolbar.js +505 -0
  169. package/dist/render/ui/comment-layout.d.ts +39 -0
  170. package/dist/render/ui/comment-layout.js +79 -0
  171. package/dist/render/ui/icons.d.ts +6 -0
  172. package/dist/render/ui/icons.js +38 -0
  173. package/dist/render/ui/menu.d.ts +38 -0
  174. package/dist/render/ui/menu.js +274 -0
  175. package/dist/render/ui/nav-model.d.ts +11 -0
  176. package/dist/render/ui/nav-model.js +34 -0
  177. package/dist/render/ui/overflow.d.ts +5 -0
  178. package/dist/render/ui/overflow.js +18 -0
  179. package/dist/render/ui/popover.d.ts +18 -0
  180. package/dist/render/ui/popover.js +109 -0
  181. package/dist/render/ui/theme.d.ts +2 -0
  182. package/dist/render/ui/theme.js +29 -0
  183. package/dist/render/undo.d.ts +76 -0
  184. package/dist/render/undo.js +144 -0
  185. package/dist/render/viewer.d.ts +100 -7
  186. package/dist/render/viewer.js +302 -92
  187. package/dist/security/policy.d.ts +3 -0
  188. package/dist/security/policy.js +6 -2
  189. package/dist/security/protection.d.ts +1 -0
  190. package/dist/security/protection.js +1 -1
  191. package/dist/text/bidi-data.d.ts +53 -0
  192. package/dist/text/bidi-data.js +318 -0
  193. package/dist/text/bidi.d.ts +37 -0
  194. package/dist/text/bidi.js +508 -0
  195. package/dist/xml/ns.d.ts +5 -0
  196. package/dist/xml/ns.js +7 -0
  197. package/package.json +9 -2
@@ -0,0 +1,83 @@
1
+ /** Pixel size (and resolution, when the file says) of PNG, JPEG, GIF, BMP and TIFF images, from their headers. */
2
+ import { sniffImage } from './index.js';
3
+ import { metafileSize } from '../metafile/index.js';
4
+ const u16be = (b, i) => (b[i] << 8) | b[i + 1];
5
+ const u32be = (b, i) => ((b[i] << 24) >>> 0) + (b[i + 1] << 16) + (b[i + 2] << 8) + b[i + 3];
6
+ const u16le = (b, i) => b[i] | (b[i + 1] << 8);
7
+ const i32le = (b, i) => b[i] | (b[i + 1] << 8) | (b[i + 2] << 16) | (b[i + 3] << 24);
8
+ /** Format and natural size of an image, or undefined when it is not one docxkeun can place. */
9
+ export function imageInfo(bytes) {
10
+ const kind = sniffImage(bytes);
11
+ const px = (w, h, dpiX = 96, dpiY = dpiX) => w > 0 && h > 0 && kind ? { kind, width: (w * 72) / (dpiX || 96), height: (h * 72) / (dpiY || 96) } : undefined;
12
+ switch (kind) {
13
+ case 'png': {
14
+ if (bytes.length < 24)
15
+ return undefined;
16
+ let dpiX = 96;
17
+ let dpiY = 96;
18
+ // pHYs (pixels per metre), before IDAT.
19
+ for (let i = 8; i + 8 <= bytes.length;) {
20
+ const len = u32be(bytes, i);
21
+ const type = String.fromCharCode(bytes[i + 4], bytes[i + 5], bytes[i + 6], bytes[i + 7]);
22
+ if (type === 'pHYs' && bytes[i + 16] === 1) {
23
+ dpiX = Math.round(u32be(bytes, i + 8) * 0.0254) || 96;
24
+ dpiY = Math.round(u32be(bytes, i + 12) * 0.0254) || 96;
25
+ }
26
+ if (type === 'IDAT' || type === 'IEND')
27
+ break;
28
+ i += 12 + len;
29
+ }
30
+ return px(u32be(bytes, 16), u32be(bytes, 20), dpiX, dpiY);
31
+ }
32
+ case 'jpeg': {
33
+ let dpi = 96;
34
+ for (let i = 2; i + 9 < bytes.length;) {
35
+ if (bytes[i] !== 0xff)
36
+ return undefined;
37
+ const marker = bytes[i + 1];
38
+ const len = u16be(bytes, i + 2);
39
+ if (marker === 0xe0 && bytes[i + 4] === 0x4a && bytes[i + 11] === 1)
40
+ dpi = u16be(bytes, i + 12) || 96; // JFIF, dots per inch
41
+ if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc)
42
+ return px(u16be(bytes, i + 7), u16be(bytes, i + 5), dpi);
43
+ i += 2 + len;
44
+ }
45
+ return undefined;
46
+ }
47
+ case 'gif':
48
+ return px(u16le(bytes, 6), u16le(bytes, 8));
49
+ case 'bmp': {
50
+ const header = bytes[14] | (bytes[15] << 8);
51
+ if (header === 12)
52
+ return px(u16le(bytes, 18), u16le(bytes, 20));
53
+ const ppm = i32le(bytes, 38);
54
+ return px(Math.abs(i32le(bytes, 18)), Math.abs(i32le(bytes, 22)), ppm > 0 ? Math.round(ppm * 0.0254) : 96);
55
+ }
56
+ case 'tiff': {
57
+ const le = bytes[0] === 0x49;
58
+ const r16 = (i) => (le ? u16le(bytes, i) : u16be(bytes, i));
59
+ const r32 = (i) => (le ? i32le(bytes, i) >>> 0 : u32be(bytes, i));
60
+ const ifd = r32(4);
61
+ let w = 0;
62
+ let h = 0;
63
+ for (let k = 0, n = r16(ifd); k < n; k++) {
64
+ const e = ifd + 2 + k * 12;
65
+ const tag = r16(e);
66
+ const type = r16(e + 2);
67
+ const v = type === 3 ? r16(e + 8) : r32(e + 8);
68
+ if (tag === 256)
69
+ w = v;
70
+ if (tag === 257)
71
+ h = v;
72
+ }
73
+ return px(w, h);
74
+ }
75
+ case 'emf':
76
+ case 'wmf': {
77
+ const s = metafileSize(bytes);
78
+ return s ? { kind, width: s.width, height: s.height } : undefined;
79
+ }
80
+ default:
81
+ return undefined;
82
+ }
83
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Baseline TIFF decoder producing RGBA pixels from the first image (IFD) of a file. Browsers other
3
+ * than Safari cannot show TIFF and PDF has no TIFF filter, so the viewer and the PDF exporter both
4
+ * need the pixels.
5
+ *
6
+ * Supported: either byte order; strips or tiles; no compression, PackBits, LZW and Deflate, with
7
+ * horizontal-differencing prediction; WhiteIsZero, BlackIsZero, RGB and Palette images at 1, 2, 4,
8
+ * 8 or 16 bits per sample; associated or unassociated alpha; chunky or planar sample layout.
9
+ * JPEG, CCITT fax, CMYK, YCbCr and Lab images are refused with an error that says so.
10
+ */
11
+ import type { RasterImage } from '../layout/types.js';
12
+ export declare function isTiff(b: Uint8Array): boolean;
13
+ /** Decode the first image of a TIFF file. */
14
+ export declare function decodeTiff(bytes: Uint8Array): Promise<RasterImage>;
@@ -0,0 +1,407 @@
1
+ import { inflate } from '../zip/deflate.js';
2
+ /** Refuse headers that would need gigabytes of RGBA before a single pixel is read. */
3
+ const MAX_PIXELS = 1 << 27;
4
+ const TAG = {
5
+ width: 256,
6
+ height: 257,
7
+ bitsPerSample: 258,
8
+ compression: 259,
9
+ photometric: 262,
10
+ fillOrder: 266,
11
+ stripOffsets: 273,
12
+ samplesPerPixel: 277,
13
+ rowsPerStrip: 278,
14
+ stripByteCounts: 279,
15
+ planarConfiguration: 284,
16
+ predictor: 317,
17
+ colorMap: 320,
18
+ tileWidth: 322,
19
+ tileLength: 323,
20
+ tileOffsets: 324,
21
+ tileByteCounts: 325,
22
+ extraSamples: 338,
23
+ sampleFormat: 339,
24
+ };
25
+ /** Bytes per value for each TIFF field type. */
26
+ const TYPE_SIZE = { 1: 1, 2: 1, 3: 2, 4: 4, 5: 8, 6: 1, 7: 1, 8: 2, 9: 4, 10: 8, 11: 4, 12: 8 };
27
+ export function isTiff(b) {
28
+ // 42 is classic TIFF, 43 BigTIFF (recognised so that it gets a clear error rather than none).
29
+ return b.length >= 4 && ((b[0] === 0x49 && b[1] === 0x49 && (b[2] === 42 || b[2] === 43) && b[3] === 0) || (b[0] === 0x4d && b[1] === 0x4d && b[2] === 0 && (b[3] === 42 || b[3] === 43)));
30
+ }
31
+ function readIfd(view, offset, le) {
32
+ const tags = new Map();
33
+ if (offset + 2 > view.byteLength)
34
+ throw new Error('TIFF: first IFD lies outside the file');
35
+ const count = view.getUint16(offset, le);
36
+ for (let i = 0; i < count; i++) {
37
+ const e = offset + 2 + i * 12;
38
+ if (e + 12 > view.byteLength)
39
+ break;
40
+ const tag = view.getUint16(e, le);
41
+ const type = view.getUint16(e + 2, le);
42
+ const n = view.getUint32(e + 4, le);
43
+ const size = TYPE_SIZE[type];
44
+ if (!size)
45
+ continue;
46
+ const total = size * n;
47
+ const at = total <= 4 ? e + 8 : view.getUint32(e + 8, le);
48
+ if (at + total > view.byteLength)
49
+ continue;
50
+ const values = new Array(n);
51
+ for (let k = 0; k < n; k++) {
52
+ const q = at + k * size;
53
+ switch (type) {
54
+ case 3:
55
+ values[k] = view.getUint16(q, le);
56
+ break;
57
+ case 4:
58
+ values[k] = view.getUint32(q, le);
59
+ break;
60
+ case 5:
61
+ values[k] = view.getUint32(q, le) / view.getUint32(q + 4, le);
62
+ break;
63
+ case 6:
64
+ values[k] = view.getInt8(q);
65
+ break;
66
+ case 8:
67
+ values[k] = view.getInt16(q, le);
68
+ break;
69
+ case 9:
70
+ values[k] = view.getInt32(q, le);
71
+ break;
72
+ case 10:
73
+ values[k] = view.getInt32(q, le) / view.getInt32(q + 4, le);
74
+ break;
75
+ case 11:
76
+ values[k] = view.getFloat32(q, le);
77
+ break;
78
+ case 12:
79
+ values[k] = view.getFloat64(q, le);
80
+ break;
81
+ default:
82
+ values[k] = view.getUint8(q);
83
+ }
84
+ }
85
+ tags.set(tag, values);
86
+ }
87
+ return tags;
88
+ }
89
+ /** PackBits: a signed count byte, then n+1 literal bytes or one byte repeated 1-n times. */
90
+ function unpackBits(src, size) {
91
+ const out = new Uint8Array(size);
92
+ let p = 0;
93
+ let o = 0;
94
+ while (p < src.length && o < size) {
95
+ const n = (src[p++] << 24) >> 24;
96
+ if (n >= 0) {
97
+ const len = n + 1;
98
+ out.set(src.subarray(p, Math.min(p + len, src.length, p + size - o)), o);
99
+ p += len;
100
+ o += len;
101
+ }
102
+ else if (n !== -128) {
103
+ if (p >= src.length)
104
+ break;
105
+ const v = src[p++];
106
+ out.fill(v, o, Math.min(o + 1 - n, size));
107
+ o += 1 - n;
108
+ }
109
+ }
110
+ return out;
111
+ }
112
+ /**
113
+ * TIFF's LZW differs from GIF's: codes are packed MSB-first, the literal alphabet is always 8 bits,
114
+ * and the code width grows one code early ("early change"), as libtiff and every writer do.
115
+ */
116
+ function unpackLzw(src, size) {
117
+ // Pre-6.0 LZW was LSB-first; its stream starts with the clear code written that way.
118
+ if (src.length >= 2 && src[0] === 0 && (src[1] & 1) !== 0)
119
+ throw new Error('TIFF: old-style (pre-6.0) LZW is not supported');
120
+ const out = new Uint8Array(size);
121
+ const prefix = new Uint16Array(4096);
122
+ const suffix = new Uint8Array(4096);
123
+ const first = new Uint8Array(4096);
124
+ const length = new Uint16Array(4096);
125
+ for (let i = 0; i < 256; i++) {
126
+ suffix[i] = i;
127
+ first[i] = i;
128
+ length[i] = 1;
129
+ }
130
+ let codeSize = 9;
131
+ let next = 258;
132
+ let prev = -1;
133
+ let acc = 0;
134
+ let bits = 0;
135
+ let pos = 0;
136
+ let n = 0;
137
+ while (n < size) {
138
+ while (bits < codeSize) {
139
+ if (pos >= src.length)
140
+ return out;
141
+ acc = (acc << 8) | src[pos++];
142
+ bits += 8;
143
+ }
144
+ bits -= codeSize;
145
+ const code = (acc >>> bits) & ((1 << codeSize) - 1);
146
+ acc &= (1 << bits) - 1;
147
+ if (code === 256) {
148
+ codeSize = 9;
149
+ next = 258;
150
+ prev = -1;
151
+ continue;
152
+ }
153
+ if (code === 257)
154
+ break;
155
+ if (prev < 0) {
156
+ if (code > 255)
157
+ break;
158
+ out[n++] = code;
159
+ prev = code;
160
+ continue;
161
+ }
162
+ if (code > next)
163
+ break;
164
+ if (next < 4096) {
165
+ prefix[next] = prev;
166
+ first[next] = first[prev];
167
+ suffix[next] = code === next ? first[prev] : first[code];
168
+ length[next] = length[prev] + 1;
169
+ next++;
170
+ if (next >= (1 << codeSize) - 1 && codeSize < 12)
171
+ codeSize++;
172
+ }
173
+ const len = length[code];
174
+ let c = code;
175
+ for (let i = n + len - 1; i >= n; i--) {
176
+ if (i < size)
177
+ out[i] = suffix[c];
178
+ c = prefix[c];
179
+ }
180
+ n += len;
181
+ prev = code;
182
+ }
183
+ return out;
184
+ }
185
+ /** FillOrder 2 stores the first pixel in the least significant bit. */
186
+ function reverseBits(src) {
187
+ const out = new Uint8Array(src.length);
188
+ for (let i = 0; i < src.length; i++) {
189
+ let v = src[i];
190
+ v = ((v & 0xf0) >> 4) | ((v & 0x0f) << 4);
191
+ v = ((v & 0xcc) >> 2) | ((v & 0x33) << 2);
192
+ out[i] = ((v & 0xaa) >> 1) | ((v & 0x55) << 1);
193
+ }
194
+ return out;
195
+ }
196
+ /** Decompress one strip or tile; the result is at least `size` bytes so unpacking needs no bounds checks. */
197
+ async function decompress(raw, compression, size) {
198
+ if (compression === 5)
199
+ return unpackLzw(raw, size);
200
+ if (compression === 32773)
201
+ return unpackBits(raw, size);
202
+ const data = compression === 8 || compression === 32946 ? await inflate(raw, 'deflate') : raw;
203
+ if (data.length >= size)
204
+ return data;
205
+ const padded = new Uint8Array(size);
206
+ padded.set(data);
207
+ return padded;
208
+ }
209
+ function unsupportedCompression(compression) {
210
+ if (compression === 1 || compression === 5 || compression === 8 || compression === 32773 || compression === 32946)
211
+ return undefined;
212
+ if (compression === 2 || compression === 3 || compression === 4 || compression === 32771)
213
+ return 'CCITT fax-compressed TIFF is not supported';
214
+ if (compression === 6 || compression === 7)
215
+ return 'JPEG-compressed TIFF is not supported';
216
+ return `TIFF compression ${compression} is not supported`;
217
+ }
218
+ function unsupportedPhotometric(photometric) {
219
+ if (photometric <= 3)
220
+ return undefined;
221
+ const what = { 4: 'transparency-mask', 5: 'CMYK (separated)', 6: 'YCbCr', 8: 'CIE Lab', 9: 'ICC Lab', 10: 'ITU Lab', 32844: 'LogL', 32845: 'LogLuv' };
222
+ return `${what[photometric] ?? `photometric ${photometric}`} TIFF is not supported`;
223
+ }
224
+ /**
225
+ * Spread one strip or tile into the whole-image sample array. `n` is the number of samples per
226
+ * pixel stored in the chunk (all of them when chunky, one when planar) and `plane` the first one.
227
+ */
228
+ function unpack(data, samples, l, x0, y0, chunkWidth, rows, n, plane) {
229
+ const { width, height, spp, bps, le, predictor } = l;
230
+ const max = bps === 16 ? 0xffff : (1 << bps) - 1;
231
+ const rowBytes = Math.ceil((chunkWidth * n * bps) / 8);
232
+ // Prediction runs left to right, so the columns past the image edge never matter.
233
+ const visible = Math.min(chunkWidth, width - x0);
234
+ const prev = new Uint32Array(n);
235
+ for (let r = 0; r < rows; r++) {
236
+ const y = y0 + r;
237
+ if (y >= height)
238
+ break;
239
+ const start = r * rowBytes;
240
+ let out = (y * width + x0) * spp + plane;
241
+ for (let x = 0; x < visible; x++, out += spp) {
242
+ for (let s = 0; s < n; s++) {
243
+ const k = x * n + s;
244
+ let v;
245
+ if (bps === 8)
246
+ v = data[start + k];
247
+ else if (bps === 16) {
248
+ const q = start + k * 2;
249
+ v = le ? data[q] | (data[q + 1] << 8) : (data[q] << 8) | data[q + 1];
250
+ }
251
+ else {
252
+ const bit = k * bps;
253
+ v = (data[start + (bit >> 3)] >> (8 - bps - (bit & 7))) & max;
254
+ }
255
+ if (predictor === 2 && x > 0)
256
+ v = (v + prev[s]) & max;
257
+ prev[s] = v;
258
+ samples[out + s] = v;
259
+ }
260
+ }
261
+ }
262
+ }
263
+ /** Decode the first image of a TIFF file. */
264
+ export async function decodeTiff(bytes) {
265
+ if (!isTiff(bytes) || bytes.length < 8)
266
+ throw new Error('TIFF: missing II*/MM* header');
267
+ const le = bytes[0] === 0x49;
268
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
269
+ if (view.getUint16(2, le) === 43)
270
+ throw new Error('TIFF: BigTIFF files are not supported');
271
+ const tags = readIfd(view, view.getUint32(4, le), le);
272
+ const one = (tag, fallback) => tags.get(tag)?.[0] ?? fallback;
273
+ const width = one(TAG.width, 0);
274
+ const height = one(TAG.height, 0);
275
+ if (!width || !height)
276
+ throw new Error('TIFF: missing image width or length');
277
+ if (width * height > MAX_PIXELS)
278
+ throw new Error(`TIFF: ${width}x${height} is too large to decode`);
279
+ const compression = one(TAG.compression, 1);
280
+ const badCompression = unsupportedCompression(compression);
281
+ if (badCompression)
282
+ throw new Error(`TIFF: ${badCompression}`);
283
+ const spp = one(TAG.samplesPerPixel, 1);
284
+ if (spp < 1 || spp > 16)
285
+ throw new Error(`TIFF: ${spp} samples per pixel is not supported`);
286
+ const bitsList = tags.get(TAG.bitsPerSample) ?? [1];
287
+ const bps = bitsList[0] ?? 1;
288
+ if (bitsList.slice(0, spp).some((b) => b !== bps))
289
+ throw new Error('TIFF: samples with different bit depths are not supported');
290
+ if (bps !== 1 && bps !== 2 && bps !== 4 && bps !== 8 && bps !== 16)
291
+ throw new Error(`TIFF: ${bps} bits per sample is not supported`);
292
+ if ((tags.get(TAG.sampleFormat) ?? [1]).some((f) => f !== 1))
293
+ throw new Error('TIFF: only unsigned integer samples are supported');
294
+ const photometric = one(TAG.photometric, spp >= 3 ? 2 : 1);
295
+ const badPhotometric = unsupportedPhotometric(photometric);
296
+ if (badPhotometric)
297
+ throw new Error(`TIFF: ${badPhotometric}`);
298
+ const colorSamples = photometric === 2 ? 3 : 1;
299
+ if (spp < colorSamples)
300
+ throw new Error(`TIFF: ${spp} samples per pixel is too few for the photometric interpretation`);
301
+ // Extra samples marked 0 ("unspecified") are not alpha and are ignored.
302
+ const extra = spp > colorSamples ? (tags.get(TAG.extraSamples)?.[0] ?? 0) : 0;
303
+ const alpha = extra === 1 || extra === 2 ? colorSamples : -1;
304
+ const associated = extra === 1;
305
+ const planar = one(TAG.planarConfiguration, 1);
306
+ if (planar !== 1 && planar !== 2)
307
+ throw new Error(`TIFF: planar configuration ${planar} is not supported`);
308
+ const predictor = one(TAG.predictor, 1);
309
+ if (predictor === 3)
310
+ throw new Error('TIFF: the floating-point predictor is not supported');
311
+ if (predictor !== 1 && predictor !== 2)
312
+ throw new Error(`TIFF: predictor ${predictor} is not supported`);
313
+ const fillOrder = one(TAG.fillOrder, 1);
314
+ const tiled = tags.has(TAG.tileWidth);
315
+ const chunkWidth = tiled ? one(TAG.tileWidth, 0) : width;
316
+ const chunkHeight = tiled ? one(TAG.tileLength, 0) : Math.min(one(TAG.rowsPerStrip, height), height) || height;
317
+ if (!chunkWidth || !chunkHeight || chunkWidth * chunkHeight > MAX_PIXELS)
318
+ throw new Error(`TIFF: invalid tile size ${chunkWidth}x${chunkHeight}`);
319
+ const offsets = tags.get(tiled ? TAG.tileOffsets : TAG.stripOffsets) ?? [];
320
+ const counts = tags.get(tiled ? TAG.tileByteCounts : TAG.stripByteCounts) ?? [];
321
+ const across = Math.ceil(width / chunkWidth);
322
+ const perPlane = across * Math.ceil(height / chunkHeight);
323
+ const planes = planar === 2 ? spp : 1;
324
+ const n = planar === 2 ? 1 : spp;
325
+ if (offsets.length < perPlane * planes)
326
+ throw new Error(`TIFF: expected ${perPlane * planes} ${tiled ? 'tile' : 'strip'} offsets, found ${offsets.length}`);
327
+ const rowBytes = Math.ceil((chunkWidth * n * bps) / 8);
328
+ const layout = { width, height, spp, bps, le, predictor };
329
+ const samples = bps > 8 ? new Uint16Array(width * height * spp) : new Uint8Array(width * height * spp);
330
+ for (let i = 0; i < perPlane * planes; i++) {
331
+ const start = offsets[i];
332
+ if (start >= bytes.length)
333
+ continue;
334
+ const t = i % perPlane;
335
+ const x0 = (t % across) * chunkWidth;
336
+ const y0 = Math.floor(t / across) * chunkHeight;
337
+ // The last strip may be short; tiles are always stored whole.
338
+ const rows = tiled ? chunkHeight : Math.min(chunkHeight, height - y0);
339
+ const size = rows * rowBytes;
340
+ // A missing or zero byte count happens in the wild; uncompressed data has a known size.
341
+ const count = counts[i] || (compression === 1 ? size : bytes.length - start);
342
+ let raw = bytes.subarray(start, Math.min(bytes.length, start + count));
343
+ if (fillOrder === 2)
344
+ raw = reverseBits(raw);
345
+ const data = await decompress(raw, compression, size);
346
+ unpack(data, samples, layout, x0, y0, chunkWidth, rows, n, planar === 2 ? Math.floor(i / perPlane) : 0);
347
+ }
348
+ return toRgba(samples, layout, photometric, alpha, associated, tags.get(TAG.colorMap));
349
+ }
350
+ function toRgba(samples, l, photometric, alpha, associated, colorMap) {
351
+ const { width, height, spp, bps } = l;
352
+ const max = bps === 16 ? 0xffff : (1 << bps) - 1;
353
+ const scale = new Uint8Array(max + 1);
354
+ for (let v = 0; v <= max; v++)
355
+ scale[v] = Math.round((v * 255) / max);
356
+ let palette;
357
+ if (photometric === 3) {
358
+ const entries = 1 << bps;
359
+ if (!colorMap || colorMap.length < entries * 3)
360
+ throw new Error('TIFF: palette image without a colour map');
361
+ // The map should hold 16-bit values, but some writers store 8-bit ones; libtiff guesses the same way.
362
+ const wide = colorMap.some((v) => v > 255);
363
+ palette = new Uint8Array(entries * 3);
364
+ for (let i = 0; i < entries; i++) {
365
+ for (let c = 0; c < 3; c++) {
366
+ const v = colorMap[c * entries + i];
367
+ palette[i * 3 + c] = wide ? v >> 8 : v;
368
+ }
369
+ }
370
+ }
371
+ const pixels = width * height;
372
+ const rgba = new Uint8Array(pixels * 4);
373
+ // Premultiplied colour c under alpha a becomes c / a; both are in sample units, so the result is 8-bit directly.
374
+ const unpremultiply = (c, a) => (a === 0 ? 0 : Math.min(255, Math.round((c * 255) / a)));
375
+ for (let i = 0, s = 0, o = 0; i < pixels; i++, s += spp, o += 4) {
376
+ const a = alpha >= 0 ? samples[s + alpha] : max;
377
+ if (palette) {
378
+ const idx = samples[s];
379
+ rgba[o] = palette[idx * 3];
380
+ rgba[o + 1] = palette[idx * 3 + 1];
381
+ rgba[o + 2] = palette[idx * 3 + 2];
382
+ }
383
+ else if (photometric === 2) {
384
+ const r = samples[s];
385
+ const g = samples[s + 1];
386
+ const b = samples[s + 2];
387
+ if (associated) {
388
+ rgba[o] = unpremultiply(r, a);
389
+ rgba[o + 1] = unpremultiply(g, a);
390
+ rgba[o + 2] = unpremultiply(b, a);
391
+ }
392
+ else {
393
+ rgba[o] = scale[r];
394
+ rgba[o + 1] = scale[g];
395
+ rgba[o + 2] = scale[b];
396
+ }
397
+ }
398
+ else {
399
+ let g = samples[s];
400
+ if (photometric === 0)
401
+ g = max - g;
402
+ rgba[o] = rgba[o + 1] = rgba[o + 2] = associated ? unpremultiply(g, a) : scale[g];
403
+ }
404
+ rgba[o + 3] = scale[a];
405
+ }
406
+ return { width, height, rgba };
407
+ }
package/dist/index.d.ts CHANGED
@@ -3,12 +3,17 @@
3
3
  * with no runtime dependencies.
4
4
  */
5
5
  import { DocxDocument, type OpenOptions } from './model/document.js';
6
- export { DocxDocument, DocxError, type OpenOptions, type CoreProperties } from './model/document.js';
7
- export type { Block, DocParagraph, DocTable, Inline, TextInline, Story } from './model/content.js';
6
+ export { DocxDocument, DocxError, type OpenOptions, type CoreProperties, type StoryPart } from './model/document.js';
7
+ export type { Block, DocParagraph, DocTable, Inline, TextInline, ImageInline, MathInline, ShapeInfo, DrawingContent, DrawingChild, Story } from './model/content.js';
8
8
  export { blocksToText } from './model/content.js';
9
9
  export { FontRegistry, Font, METRIC_COMPATIBLE, registerEmbeddedFonts, loadBundledFonts, BUNDLED_FONTS, type FontFace, type BundledFamily } from './fonts/index.js';
10
10
  export { layoutDocument, type LayoutOptions, type LayoutResult, type Page, type DisplayItem } from './layout/index.js';
11
11
  export { readComments, type CommentInfo } from './comments/read.js';
12
+ export { readChart, describeChart, renderChart, type ChartModel } from './charts/index.js';
13
+ export { mathToText, layoutMath, type MathLayout, type MathLayoutOptions } from './math/index.js';
14
+ export { renderMetafile, metafileKind, metafileSize } from './metafile/index.js';
15
+ export { sniffImage, decodeRaster, type ImageKind } from './images/index.js';
16
+ export { imageInfo, type ImageInfo } from './images/size.js';
12
17
  /** Open a .docx (or a password-protected one, when docxkeun/security is loaded). */
13
18
  export declare function openDocx(input: Uint8Array | ArrayBuffer | Blob, options?: OpenOptions): Promise<DocxDocument>;
14
19
  /** A new, empty document with Word's default styles. */
package/dist/index.js CHANGED
@@ -8,6 +8,11 @@ export { blocksToText } from './model/content.js';
8
8
  export { FontRegistry, Font, METRIC_COMPATIBLE, registerEmbeddedFonts, loadBundledFonts, BUNDLED_FONTS } from './fonts/index.js';
9
9
  export { layoutDocument } from './layout/index.js';
10
10
  export { readComments } from './comments/read.js';
11
+ export { readChart, describeChart, renderChart } from './charts/index.js';
12
+ export { mathToText, layoutMath } from './math/index.js';
13
+ export { renderMetafile, metafileKind, metafileSize } from './metafile/index.js';
14
+ export { sniffImage, decodeRaster } from './images/index.js';
15
+ export { imageInfo } from './images/size.js';
11
16
  /** Open a .docx (or a password-protected one, when docxkeun/security is loaded). */
12
17
  export function openDocx(input, options) {
13
18
  return DocxDocument.open(input, options);
@@ -0,0 +1,18 @@
1
+ import type { Atom } from './lines.js';
2
+ export interface BidiInfo {
3
+ /** One stand-in string for the paragraph's atoms. */
4
+ text: string;
5
+ /** Resolved embedding levels, per UTF-16 code unit of `text`. */
6
+ levels: Uint8Array;
7
+ /** Where each atom starts in `text` (and the end, at index atoms.length). */
8
+ offsets: number[];
9
+ paragraphLevel: 0 | 1;
10
+ }
11
+ /** Resolve levels for a paragraph's atoms; nothing to do (undefined) for plain left-to-right text. */
12
+ export declare function analyzeBidi(atoms: Atom[], rtlParagraph: boolean): BidiInfo | undefined;
13
+ export declare function bidiOf(atoms: Atom[]): BidiInfo | undefined;
14
+ /** Atom indices of a line in visual order (left to right on the page), with each atom's level. */
15
+ export declare function visualAtoms(info: BidiInfo, start: number, end: number): Array<{
16
+ atom: number;
17
+ level: number;
18
+ }>;
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Right-to-left text in line layout. Paragraphs are laid out in logical order, as if left to
3
+ * right from the paragraph's start edge (Word's w:ind and w:jc are start/end values in a
4
+ * right-to-left paragraph); each finished line is then put in visual order with the Unicode
5
+ * bidi algorithm, and a right-to-left paragraph's lines are mirrored onto the page.
6
+ */
7
+ import { lineLevels, resolveLevels } from '../text/bidi.js';
8
+ import { visualOrder } from '../text/bidi.js';
9
+ const infos = new WeakMap();
10
+ const RTL = /[֐-ࣿיִ-﷿ﹰ-ﻼ\u{10800}-\u{10fff}\u{1e800}-\u{1efff}‏‫‮⁧]/u;
11
+ /** Text standing for an atom in the bidi algorithm. */
12
+ function standIn(a) {
13
+ switch (a.kind) {
14
+ case 'char':
15
+ return a.ch;
16
+ case 'tab':
17
+ return '\t';
18
+ case 'break':
19
+ return '
';
20
+ case 'image':
21
+ case 'object':
22
+ case 'field':
23
+ return '';
24
+ default:
25
+ // Bookmarks, comment marks and anchors of floating objects do not take part.
26
+ return '​';
27
+ }
28
+ }
29
+ /** Resolve levels for a paragraph's atoms; nothing to do (undefined) for plain left-to-right text. */
30
+ export function analyzeBidi(atoms, rtlParagraph) {
31
+ let text = '';
32
+ const offsets = [];
33
+ let anyRtl = rtlParagraph;
34
+ for (const a of atoms) {
35
+ offsets.push(text.length);
36
+ const s = standIn(a);
37
+ if (!anyRtl && a.kind === 'char' && RTL.test(s))
38
+ anyRtl = true;
39
+ text += s;
40
+ }
41
+ offsets.push(text.length);
42
+ if (!anyRtl)
43
+ return undefined;
44
+ const { levels, paragraphLevel } = resolveLevels(text, rtlParagraph ? 1 : 0);
45
+ const info = { text, levels, offsets, paragraphLevel };
46
+ infos.set(atoms, info);
47
+ return info;
48
+ }
49
+ export function bidiOf(atoms) {
50
+ return infos.get(atoms);
51
+ }
52
+ /** Atom indices of a line in visual order (left to right on the page), with each atom's level. */
53
+ export function visualAtoms(info, start, end) {
54
+ const s = info.offsets[start];
55
+ const e = info.offsets[end];
56
+ const levels = lineLevels(info.text, info.levels, info.paragraphLevel, s, e);
57
+ const owner = new Int32Array(e - s);
58
+ for (let i = start, k = s; i < end; i++)
59
+ for (; k < info.offsets[i + 1]; k++)
60
+ owner[k - s] = i;
61
+ const out = [];
62
+ const seen = new Set();
63
+ for (const k of visualOrder(levels, s, e)) {
64
+ const atom = owner[k - s];
65
+ if (seen.has(atom))
66
+ continue;
67
+ seen.add(atom);
68
+ out.push({ atom, level: levels[k] ?? 0 });
69
+ }
70
+ return out;
71
+ }
@@ -2,11 +2,12 @@
2
2
  * Blocks → boxes. A box is an unbreakable vertical slice (a line, some spacing, a table row)
3
3
  * with keep rules; the paginator stacks boxes into columns and pages.
4
4
  */
5
- import { type Block, type DocParagraph, type DocTable, type NoteRefInline } from '../model/content.js';
6
- import type { NumberingCounter } from '../model/numbering.js';
5
+ import { type Block, type DocParagraph, type DocTable, type ImageInline, type NoteRefInline } from '../model/content.js';
6
+ import { type NumberingCounter } from '../model/numbering.js';
7
7
  import { type ParagraphBorders } from '../model/props.js';
8
8
  import type { Theme } from '../model/theme.js';
9
- import { type Line, type RangeTracker } from './lines.js';
9
+ import { type LayoutCache } from './cache.js';
10
+ import { type Line, type LineGeometry, type RangeTracker } from './lines.js';
10
11
  import { type Painter } from './paint.js';
11
12
  import type { FontProvider } from './types.js';
12
13
  export interface NoteRef {
@@ -28,8 +29,22 @@ export interface Box {
28
29
  split?(avail: number): [Box, Box] | undefined;
29
30
  /** Table header rows to repeat when this box starts a new page. */
30
31
  repeatHeader?: Box[];
32
+ /** On a paragraph's first box: what pagination needs to wrap it around floating objects. */
33
+ paragraph?: ParagraphMeta;
31
34
  paint(p: Painter, x: number, y: number): void;
32
35
  }
36
+ export interface ParagraphMeta {
37
+ /** How many boxes (from this one) belong to the paragraph. */
38
+ count: number;
39
+ /** Height of the boxes before the first line (spacing before, top border). */
40
+ textOffset: number;
41
+ /** Floating objects anchored in the paragraph. */
42
+ floats: ImageInline[];
43
+ /** The paragraph's boxes again, with lines broken around floats (`bounds` relative to the first line's top). */
44
+ reflow(bounds: NonNullable<LineGeometry['bounds']>): Box[];
45
+ /** Already laid out around this page's floats. */
46
+ wrapped?: boolean;
47
+ }
33
48
  export interface BlockContext {
34
49
  fonts: FontProvider;
35
50
  theme: Theme;
@@ -37,6 +52,8 @@ export interface BlockContext {
37
52
  counter: NumberingCounter;
38
53
  defaultTab: number;
39
54
  noteLabel(inline: NoteRefInline): string;
55
+ /** Paragraph layout kept from earlier runs. */
56
+ cache?: LayoutCache;
40
57
  }
41
58
  interface ParagraphState {
42
59
  styleId?: string;