@folijs/core 0.2.0 → 0.4.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 (136) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/README.md +1 -1
  3. package/dist/document.d.ts +10 -16
  4. package/dist/document.d.ts.map +1 -1
  5. package/dist/document.js +22 -22
  6. package/dist/document.js.map +1 -1
  7. package/dist/index.d.ts +3 -2
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +3 -2
  10. package/dist/index.js.map +1 -1
  11. package/dist/model/fragment.d.ts +5 -2
  12. package/dist/model/fragment.d.ts.map +1 -1
  13. package/dist/model/fragment.js +5 -2
  14. package/dist/model/fragment.js.map +1 -1
  15. package/dist/model/node.d.ts +1 -1
  16. package/dist/model/node.d.ts.map +1 -1
  17. package/dist/model/node.js.map +1 -1
  18. package/dist/schema.d.ts +10 -1
  19. package/dist/schema.d.ts.map +1 -1
  20. package/dist/schema.js +11 -1
  21. package/dist/schema.js.map +1 -1
  22. package/dist/state/commands.d.ts +7 -6
  23. package/dist/state/commands.d.ts.map +1 -1
  24. package/dist/state/commands.js +162 -97
  25. package/dist/state/commands.js.map +1 -1
  26. package/dist/state/comments.d.ts +47 -0
  27. package/dist/state/comments.d.ts.map +1 -0
  28. package/dist/state/comments.js +304 -0
  29. package/dist/state/comments.js.map +1 -0
  30. package/dist/state/editor.d.ts +67 -16
  31. package/dist/state/editor.d.ts.map +1 -1
  32. package/dist/state/editor.js +74 -18
  33. package/dist/state/editor.js.map +1 -1
  34. package/dist/state/format.d.ts +8 -22
  35. package/dist/state/format.d.ts.map +1 -1
  36. package/dist/state/format.js +16 -140
  37. package/dist/state/format.js.map +1 -1
  38. package/dist/state/history.d.ts +1 -1
  39. package/dist/state/history.d.ts.map +1 -1
  40. package/dist/state/history.js +3 -2
  41. package/dist/state/history.js.map +1 -1
  42. package/dist/state/index.d.ts +11 -4
  43. package/dist/state/index.d.ts.map +1 -1
  44. package/dist/state/index.js +11 -4
  45. package/dist/state/index.js.map +1 -1
  46. package/dist/state/links.d.ts.map +1 -1
  47. package/dist/state/links.js +10 -2
  48. package/dist/state/links.js.map +1 -1
  49. package/dist/state/lists.d.ts +19 -32
  50. package/dist/state/lists.d.ts.map +1 -1
  51. package/dist/state/lists.js +23 -372
  52. package/dist/state/lists.js.map +1 -1
  53. package/dist/state/mark.d.ts +28 -0
  54. package/dist/state/mark.d.ts.map +1 -0
  55. package/dist/state/mark.js +57 -0
  56. package/dist/state/mark.js.map +1 -0
  57. package/dist/state/review.d.ts +43 -0
  58. package/dist/state/review.d.ts.map +1 -0
  59. package/dist/state/review.js +282 -0
  60. package/dist/state/review.js.map +1 -0
  61. package/dist/state/state.d.ts +2 -0
  62. package/dist/state/state.d.ts.map +1 -1
  63. package/dist/state/state.js +18 -0
  64. package/dist/state/state.js.map +1 -1
  65. package/dist/state/track.d.ts +47 -0
  66. package/dist/state/track.d.ts.map +1 -0
  67. package/dist/state/track.js +534 -0
  68. package/dist/state/track.js.map +1 -0
  69. package/dist/state/word.d.ts +100 -0
  70. package/dist/state/word.d.ts.map +1 -0
  71. package/dist/state/word.js +253 -0
  72. package/dist/state/word.js.map +1 -0
  73. package/dist/state/wordclip.d.ts +43 -0
  74. package/dist/state/wordclip.d.ts.map +1 -0
  75. package/dist/state/wordclip.js +412 -0
  76. package/dist/state/wordclip.js.map +1 -0
  77. package/dist/state/wordformat.d.ts +136 -0
  78. package/dist/state/wordformat.d.ts.map +1 -0
  79. package/dist/state/wordformat.js +846 -0
  80. package/dist/state/wordformat.js.map +1 -0
  81. package/dist/v3/check.d.ts +33 -0
  82. package/dist/v3/check.d.ts.map +1 -0
  83. package/dist/v3/check.js +90 -0
  84. package/dist/v3/check.js.map +1 -0
  85. package/dist/v3/document.d.ts +217 -0
  86. package/dist/v3/document.d.ts.map +1 -0
  87. package/dist/v3/document.js +225 -0
  88. package/dist/v3/document.js.map +1 -0
  89. package/dist/v3/index.d.ts +6 -0
  90. package/dist/v3/index.d.ts.map +1 -0
  91. package/dist/v3/index.js +6 -0
  92. package/dist/v3/index.js.map +1 -0
  93. package/dist/v3/migrate.d.ts +19 -0
  94. package/dist/v3/migrate.d.ts.map +1 -0
  95. package/dist/v3/migrate.js +227 -0
  96. package/dist/v3/migrate.js.map +1 -0
  97. package/dist/v3/resolve.d.ts +129 -0
  98. package/dist/v3/resolve.d.ts.map +1 -0
  99. package/dist/v3/resolve.js +534 -0
  100. package/dist/v3/resolve.js.map +1 -0
  101. package/dist/v3/schema.d.ts +29 -0
  102. package/dist/v3/schema.d.ts.map +1 -0
  103. package/dist/v3/schema.js +431 -0
  104. package/dist/v3/schema.js.map +1 -0
  105. package/dist/v3/units.d.ts +16 -0
  106. package/dist/v3/units.d.ts.map +1 -0
  107. package/dist/v3/units.js +28 -0
  108. package/dist/v3/units.js.map +1 -0
  109. package/package.json +1 -1
  110. package/src/document.ts +26 -26
  111. package/src/index.ts +3 -6
  112. package/src/model/fragment.ts +11 -3
  113. package/src/model/node.ts +6 -1
  114. package/src/schema.ts +13 -1
  115. package/src/state/commands.ts +173 -91
  116. package/src/state/comments.ts +342 -0
  117. package/src/state/editor.ts +176 -36
  118. package/src/state/format.ts +24 -122
  119. package/src/state/history.ts +3 -2
  120. package/src/state/index.ts +73 -3
  121. package/src/state/links.ts +10 -2
  122. package/src/state/lists.ts +31 -408
  123. package/src/state/mark.ts +64 -0
  124. package/src/state/review.ts +330 -0
  125. package/src/state/state.ts +18 -0
  126. package/src/state/track.ts +602 -0
  127. package/src/state/word.ts +330 -0
  128. package/src/state/wordclip.ts +445 -0
  129. package/src/state/wordformat.ts +982 -0
  130. package/src/v3/check.ts +119 -0
  131. package/src/v3/document.ts +456 -0
  132. package/src/v3/index.ts +33 -0
  133. package/src/v3/migrate.ts +267 -0
  134. package/src/v3/resolve.ts +646 -0
  135. package/src/v3/schema.ts +482 -0
  136. package/src/v3/units.ts +34 -0
@@ -0,0 +1,482 @@
1
+ import { type AttributeSpec, type MarkSpec, Schema } from "../model/index.js";
2
+ import {
3
+ attributes,
4
+ bool,
5
+ type Check,
6
+ color,
7
+ date,
8
+ int,
9
+ isRecord,
10
+ listOf,
11
+ nullable,
12
+ oneOf,
13
+ shape,
14
+ str,
15
+ text,
16
+ token,
17
+ xml,
18
+ } from "./check.js";
19
+
20
+ /**
21
+ * Schema v3: Word's document model ([ADR-0005](../../../../docs/decisions/ADR-0005-word-compatible-model.md)).
22
+ * Paragraphs carry Word's paragraph properties as attributes and text carries its run
23
+ * properties as marks, in Word's units (twips, half-points, eighths of a point), so values
24
+ * survive a DOCX round trip exactly. Headings are paragraphs with a heading style; lists are
25
+ * numbered paragraphs. An unset property is null (the style decides).
26
+ *
27
+ * Validation accepts what Word accepts: it rejects only what the engine cannot work with
28
+ * (wrong types, out-of-range numbers), never a property combination Word would open.
29
+ *
30
+ * Lossless: an `ext` attribute holds the element the properties were read from (`w:pPr`,
31
+ * `w:rPr`, `w:sectPr`…), verbatim. Export writes it back as it was while the modeled values
32
+ * still match it, and otherwise rewrites the modeled values into it, keeping its unknown
33
+ * children (in Word's order) and unknown attributes. `attributes` holds an element's own
34
+ * unknown attributes (`w14:paraId` on a paragraph). Opaque nodes hold whole elements Foli does
35
+ * not model yet.
36
+ *
37
+ * The editor still runs on schema v2 until rendering and editing move to v3 (8.6, 8.7); this
38
+ * schema, the v3 envelope and the v2 → v3 migration exist alongside it until then.
39
+ */
40
+
41
+ const MAX_TWIPS = 31_680; // 22 inches: Word's largest page dimension
42
+ const twips = int(-MAX_TWIPS, MAX_TWIPS);
43
+ const positiveTwips = int(0, MAX_TWIPS);
44
+ /** Line spacing: 240ths of a line ("auto") or twips. */
45
+ const lineSpacing = int(-MAX_TWIPS, MAX_TWIPS);
46
+
47
+ /** Word's paragraph justification (ST_Jc). */
48
+ export const JUSTIFICATIONS = [
49
+ "left",
50
+ "center",
51
+ "right",
52
+ "both",
53
+ "start",
54
+ "end",
55
+ "distribute",
56
+ "mediumKashida",
57
+ "highKashida",
58
+ "lowKashida",
59
+ "thaiDistribute",
60
+ "numTab",
61
+ ] as const;
62
+ export type Justification = (typeof JUSTIFICATIONS)[number];
63
+
64
+ export const LINE_RULES = ["auto", "exact", "atLeast"] as const;
65
+ export const TAB_KINDS = [
66
+ "left",
67
+ "center",
68
+ "right",
69
+ "decimal",
70
+ "bar",
71
+ "clear",
72
+ "num",
73
+ "start",
74
+ "end",
75
+ ] as const;
76
+ export const TAB_LEADERS = ["none", "dot", "hyphen", "underscore", "heavy", "middleDot"] as const;
77
+ export const SECTION_TYPES = [
78
+ "nextPage",
79
+ "continuous",
80
+ "evenPage",
81
+ "oddPage",
82
+ "nextColumn",
83
+ ] as const;
84
+ export const VERTICAL_ALIGNS = ["baseline", "superscript", "subscript"] as const;
85
+ /** Word's highlight colors (ST_HighlightColor); any other color is shading. */
86
+ export const HIGHLIGHT_COLORS = [
87
+ "black",
88
+ "blue",
89
+ "cyan",
90
+ "green",
91
+ "magenta",
92
+ "red",
93
+ "yellow",
94
+ "white",
95
+ "darkBlue",
96
+ "darkCyan",
97
+ "darkGreen",
98
+ "darkMagenta",
99
+ "darkRed",
100
+ "darkYellow",
101
+ "darkGray",
102
+ "lightGray",
103
+ "none",
104
+ ] as const;
105
+ export const THEME_FONTS = [
106
+ "majorAscii",
107
+ "majorHAnsi",
108
+ "majorEastAsia",
109
+ "majorBidi",
110
+ "minorAscii",
111
+ "minorHAnsi",
112
+ "minorEastAsia",
113
+ "minorBidi",
114
+ ] as const;
115
+
116
+ const border = shape(
117
+ {
118
+ style: token,
119
+ size: int(0, 96), // eighths of a point
120
+ space: int(0, 31), // points
121
+ color,
122
+ themeColor: token,
123
+ },
124
+ ["style"],
125
+ );
126
+
127
+ const shading = shape({ pattern: token, color, fill: color, themeFill: token }, ["pattern"]);
128
+
129
+ /** Who made a tracked change or comment, and when (ids are Word's `w:id`; authors may be empty). */
130
+ const ID = int(0, 2_147_483_647);
131
+ const revisionFields = { id: ID, author: str(), date: nullable(date) };
132
+ const revision = shape(revisionFields, ["id", "author"]);
133
+
134
+ /**
135
+ * A section's page setup (`w:sectPr`): on the last paragraph of each section, and the
136
+ * envelope's for the last. Every part is optional (Word's defaults apply); `{}` is a section
137
+ * break with Word's defaults.
138
+ */
139
+ export const checkSection: Check = shape(
140
+ {
141
+ pageSize: shape({
142
+ width: int(1, MAX_TWIPS),
143
+ height: int(1, MAX_TWIPS),
144
+ orientation: oneOf(["portrait", "landscape"]),
145
+ }),
146
+ margins: shape({
147
+ top: twips,
148
+ right: positiveTwips,
149
+ bottom: twips,
150
+ left: positiveTwips,
151
+ header: positiveTwips,
152
+ footer: positiveTwips,
153
+ gutter: positiveTwips,
154
+ }),
155
+ type: oneOf(SECTION_TYPES),
156
+ titlePage: bool,
157
+ headers: shape({ default: text(), first: text(), even: text() }),
158
+ footers: shape({ default: text(), first: text(), even: text() }),
159
+ ext: xml,
160
+ },
161
+ [],
162
+ true,
163
+ );
164
+
165
+ /** Marks that describe how text looks: what a style, a paragraph mark or a recorded change holds. */
166
+ export const RUN_PROPERTY_MARKS = [
167
+ "characterStyle",
168
+ "bold",
169
+ "italic",
170
+ "underline",
171
+ "strike",
172
+ "doubleStrike",
173
+ "caps",
174
+ "smallCaps",
175
+ "hidden",
176
+ "font",
177
+ "fontSize",
178
+ "color",
179
+ "highlight",
180
+ "shading",
181
+ "verticalAlign",
182
+ "characterSpacing",
183
+ ];
184
+
185
+ /** Run properties given as marks JSON (a style's, a paragraph mark's, a recorded change's previous). */
186
+ export const checkRunProperties: Check = (v) => {
187
+ if (!Array.isArray(v)) return "must be an array of marks";
188
+ for (const mark of v) {
189
+ if (!isRecord(mark) || !RUN_PROPERTY_MARKS.includes(mark.type as string)) {
190
+ return `${isRecord(mark) ? String(mark.type) : "a non-object"} is not a run property`;
191
+ }
192
+ }
193
+ try {
194
+ schema.nodeFromJSON({ type: "text", text: "x", marks: v });
195
+ return undefined;
196
+ } catch (error) {
197
+ return (error as Error).message;
198
+ }
199
+ };
200
+
201
+ const nullableAttr = (check: Check): AttributeSpec => ({
202
+ default: null,
203
+ validate: nullable(check),
204
+ });
205
+
206
+ /**
207
+ * Paragraph properties (`w:pPr`). The same attributes describe a style's, a numbering level's
208
+ * and the defaults' paragraph properties.
209
+ */
210
+ const paragraphProperties: Record<string, AttributeSpec> = {
211
+ style: nullableAttr(text()),
212
+ justification: nullableAttr(oneOf(JUSTIFICATIONS)),
213
+ /** In twips. With both `firstLine` and `hanging`, Word applies `hanging`. */
214
+ indent: nullableAttr(
215
+ shape({ left: twips, right: twips, firstLine: positiveTwips, hanging: positiveTwips }),
216
+ ),
217
+ spacing: nullableAttr(
218
+ shape({
219
+ before: positiveTwips,
220
+ after: positiveTwips,
221
+ line: lineSpacing,
222
+ lineRule: oneOf(LINE_RULES),
223
+ beforeAutospacing: bool,
224
+ afterAutospacing: bool,
225
+ }),
226
+ ),
227
+ keepNext: nullableAttr(bool),
228
+ keepLines: nullableAttr(bool),
229
+ widowControl: nullableAttr(bool),
230
+ pageBreakBefore: nullableAttr(bool),
231
+ contextualSpacing: nullableAttr(bool),
232
+ outlineLevel: nullableAttr(int(0, 9)),
233
+ /**
234
+ * `id` is the numbering instance (`w:numId`; 0 removes numbering), `level` 0–8 (`w:ilvl`).
235
+ * Either may be missing, as in Word: the style supplies the other (a style's numbering
236
+ * often has only `id`; a paragraph demoting a style-numbered item only `level`).
237
+ */
238
+ numbering: nullableAttr(shape({ id: ID, level: int(0, 8) })),
239
+ tabs: nullableAttr(
240
+ listOf(
241
+ shape({ position: twips, kind: oneOf(TAB_KINDS), leader: oneOf(TAB_LEADERS) }, [
242
+ "position",
243
+ "kind",
244
+ ]),
245
+ 64,
246
+ ),
247
+ ),
248
+ borders: nullableAttr(
249
+ shape({
250
+ top: border,
251
+ bottom: border,
252
+ left: border,
253
+ right: border,
254
+ between: border,
255
+ bar: border,
256
+ }),
257
+ ),
258
+ shading: nullableAttr(shading),
259
+ /** The paragraph mark's run properties (`w:pPr/w:rPr`): the mark's look, the numbering's. */
260
+ markFormat: nullableAttr(checkRunProperties),
261
+ /** The `w:pPr` these properties were read from, verbatim (see the module comment). */
262
+ ext: nullableAttr(xml),
263
+ };
264
+
265
+ /** The names of the paragraph properties (`w:pPr`) among a paragraph's attributes. */
266
+ export const PARAGRAPH_PROPERTY_KEYS: readonly string[] = Object.keys(paragraphProperties);
267
+
268
+ /** Paragraph properties as a style, a numbering level, the defaults or a recorded change give them. */
269
+ export const checkParagraphProperties: Check = (v) => {
270
+ if (!isRecord(v)) return "must be an object";
271
+ for (const [key, value] of Object.entries(v)) {
272
+ if (!Object.hasOwn(paragraphProperties, key)) return `unknown key ${key}`;
273
+ const error =
274
+ value === null
275
+ ? "must not be null (leave it out)"
276
+ : (paragraphProperties[key] as AttributeSpec).validate?.(value);
277
+ if (error) return `${key} ${error}`;
278
+ }
279
+ return undefined;
280
+ };
281
+
282
+ const markAttr = (check: Check, defaultValue?: unknown): AttributeSpec =>
283
+ defaultValue === undefined ? { validate: check } : { default: defaultValue, validate: check };
284
+
285
+ /** A run property that is on unless `value` is false (`<w:b w:val="0"/>` turns off a style's bold). */
286
+ const toggle: MarkSpec = { attrs: { value: markAttr(bool, true) } };
287
+ /**
288
+ * Bold and italic have a complex-script twin (`w:bCs`, `w:iCs`): `value` null when only the
289
+ * twin is set.
290
+ */
291
+ const toggleWithComplexScript: MarkSpec = {
292
+ attrs: {
293
+ value: markAttr(nullable(bool), true),
294
+ complexScript: markAttr(nullable(bool), null),
295
+ },
296
+ };
297
+ const reviewMark: MarkSpec = {
298
+ attrs: {
299
+ id: markAttr(ID),
300
+ author: markAttr(str()),
301
+ date: markAttr(nullable(date), null),
302
+ /** A tracked move (`w:moveFrom`, `w:moveTo`): the move's name, shared by both ends. */
303
+ move: markAttr(nullable(text()), null),
304
+ /** The change element's other attributes (`w16du:dateUtc`…). */
305
+ attributes: markAttr(nullable(attributes), null),
306
+ },
307
+ inclusive: false,
308
+ };
309
+ /** A marker's own unknown attributes (`w:displacedByCustomXml`…). */
310
+ const markerAttributes = { attributes: nullableAttr(attributes) };
311
+ const fontName = markAttr(nullable(text()), null);
312
+ const themeFont = markAttr(nullable(oneOf(THEME_FONTS)), null);
313
+ const hexByte = (v: unknown) =>
314
+ typeof v === "string" && /^[0-9A-Fa-f]{2}$/.test(v) ? undefined : "must be a hex byte";
315
+
316
+ export const schema = new Schema({
317
+ nodes: {
318
+ doc: { content: "block+" },
319
+ paragraph: {
320
+ group: "block",
321
+ content: "inline*",
322
+ attrs: {
323
+ ...paragraphProperties,
324
+ /** Ends a section: its page setup. The last section's is the envelope's `section`. */
325
+ section: nullableAttr(checkSection),
326
+ /** The paragraph mark inserted and/or deleted as tracked changes. */
327
+ revision: nullableAttr(shape({ insertion: revision, deletion: revision })),
328
+ /** A tracked formatting change: the paragraph properties before it. */
329
+ formatChange: nullableAttr(
330
+ shape({ ...revisionFields, previous: checkParagraphProperties }, [
331
+ "id",
332
+ "author",
333
+ "previous",
334
+ ]),
335
+ ),
336
+ /** `w:p`'s own unknown attributes (`w14:paraId`, revision save ids…). */
337
+ attributes: nullableAttr(attributes),
338
+ },
339
+ },
340
+ /** Block content Foli does not model yet (tables until Phase 10…), kept verbatim. */
341
+ opaqueBlock: {
342
+ group: "block",
343
+ attrs: { xml: { validate: xml }, label: nullableAttr(text()) },
344
+ },
345
+ text: { group: "inline" },
346
+ tab: { group: "inline", inline: true },
347
+ hardBreak: {
348
+ group: "inline",
349
+ inline: true,
350
+ selectable: false,
351
+ attrs: { clear: nullableAttr(oneOf(["none", "left", "right", "all"])) },
352
+ },
353
+ pageBreak: { group: "inline", inline: true, selectable: false },
354
+ columnBreak: { group: "inline", inline: true, selectable: false },
355
+ /** A bookmark's ends (`w:bookmarkStart`/`End`): what cross-references and internal links point at. */
356
+ bookmarkStart: {
357
+ group: "inline",
358
+ inline: true,
359
+ selectable: false,
360
+ attrs: { id: { validate: ID }, name: { validate: str() }, ...markerAttributes },
361
+ },
362
+ bookmarkEnd: {
363
+ group: "inline",
364
+ inline: true,
365
+ selectable: false,
366
+ attrs: { id: { validate: ID }, ...markerAttributes },
367
+ },
368
+ /** Where a comment's range starts and ends (`w:commentRangeStart`/`End`), and its reference mark. */
369
+ commentStart: {
370
+ group: "inline",
371
+ inline: true,
372
+ selectable: false,
373
+ attrs: { id: { validate: ID }, ...markerAttributes },
374
+ },
375
+ commentEnd: {
376
+ group: "inline",
377
+ inline: true,
378
+ selectable: false,
379
+ attrs: { id: { validate: ID }, ...markerAttributes },
380
+ },
381
+ commentReference: {
382
+ group: "inline",
383
+ inline: true,
384
+ attrs: { id: { validate: ID }, ...markerAttributes },
385
+ },
386
+ /** Inline content Foli does not model yet (fields, drawings, symbols…), kept verbatim. */
387
+ opaqueInline: {
388
+ group: "inline",
389
+ inline: true,
390
+ attrs: {
391
+ xml: { validate: xml },
392
+ label: nullableAttr(text()),
393
+ /**
394
+ * Content of a run (`w:drawing`, `w:fldChar`… inside `w:r`): written back inside a run
395
+ * with the node's run properties. Otherwise it stands between runs (`w:sdt`…).
396
+ */
397
+ run: { default: false, validate: bool },
398
+ },
399
+ },
400
+ },
401
+ marks: {
402
+ // Order: the canonical order of mark sets, and of nesting when rendered (outermost first).
403
+ link: {
404
+ attrs: {
405
+ /**
406
+ * The target as the file has it (`file:`, network paths…): not checked here. Whether
407
+ * a link may be opened is decided where it is opened (`isSafeHref`).
408
+ */
409
+ href: markAttr(nullable(text(8192)), null),
410
+ /** A bookmark in this document (`w:anchor`). */
411
+ anchor: markAttr(nullable(text()), null),
412
+ tooltip: markAttr(nullable(str(1024)), null),
413
+ /** `w:hyperlink`'s other attributes (`w:history`, `w:tgtFrame`…). */
414
+ attributes: markAttr(nullable(attributes), null),
415
+ },
416
+ inclusive: false,
417
+ },
418
+ insertion: reviewMark,
419
+ deletion: reviewMark,
420
+ characterStyle: { attrs: { id: markAttr(text()) } },
421
+ bold: toggleWithComplexScript,
422
+ italic: toggleWithComplexScript,
423
+ underline: {
424
+ attrs: { style: markAttr(token, "single"), color: markAttr(nullable(color), null) },
425
+ },
426
+ strike: toggle,
427
+ doubleStrike: toggle,
428
+ caps: toggle,
429
+ smallCaps: toggle,
430
+ hidden: toggle,
431
+ font: {
432
+ attrs: {
433
+ ascii: fontName,
434
+ hAnsi: fontName,
435
+ eastAsia: fontName,
436
+ cs: fontName,
437
+ asciiTheme: themeFont,
438
+ hAnsiTheme: themeFont,
439
+ eastAsiaTheme: themeFont,
440
+ csTheme: themeFont,
441
+ hint: markAttr(nullable(oneOf(["default", "eastAsia", "cs"])), null),
442
+ },
443
+ },
444
+ /** In half-points (`w:sz`; 24 = 12 pt), and for complex scripts (`w:szCs`); either may be missing. */
445
+ fontSize: {
446
+ attrs: {
447
+ value: markAttr(nullable(int(1, 3276)), null),
448
+ complexScript: markAttr(nullable(int(1, 3276)), null),
449
+ },
450
+ },
451
+ color: {
452
+ attrs: {
453
+ value: markAttr(color),
454
+ themeColor: markAttr(nullable(token), null),
455
+ themeTint: markAttr(nullable(hexByte), null),
456
+ themeShade: markAttr(nullable(hexByte), null),
457
+ },
458
+ },
459
+ highlight: { attrs: { value: markAttr(oneOf(HIGHLIGHT_COLORS)) } },
460
+ shading: {
461
+ attrs: {
462
+ pattern: markAttr(token),
463
+ color: markAttr(color, "auto"),
464
+ fill: markAttr(color, "auto"),
465
+ },
466
+ },
467
+ verticalAlign: { attrs: { value: markAttr(oneOf(VERTICAL_ALIGNS)) } },
468
+ /** Character spacing in twips (`w:spacing`), expanded or condensed. */
469
+ characterSpacing: { attrs: { value: markAttr(twips) } },
470
+ /** A tracked formatting change: the run properties before it. */
471
+ formatChange: {
472
+ attrs: {
473
+ id: markAttr(ID),
474
+ author: markAttr(str()),
475
+ date: markAttr(nullable(date), null),
476
+ previous: markAttr(checkRunProperties),
477
+ },
478
+ },
479
+ /** The `w:rPr` these marks were read from, verbatim (see the module comment). */
480
+ runExt: { attrs: { xml: markAttr(xml) } },
481
+ },
482
+ });
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Word's units, as schema v3 stores them, and conversions for the API and the view.
3
+ * twip: 1/20 point, 1/1440 inch (page geometry, indentation, spacing, tab stops);
4
+ * half-point: font sizes; eighth of a point: border widths.
5
+ */
6
+
7
+ export const TWIPS_PER_INCH = 1440;
8
+ export const TWIPS_PER_POINT = 20;
9
+
10
+ /** Millimetres to whole twips (rounding: under 0.02 mm). */
11
+ export function mmToTwips(mm: number): number {
12
+ return Math.round((mm * TWIPS_PER_INCH) / 25.4);
13
+ }
14
+
15
+ export function twipsToMm(twips: number): number {
16
+ return (twips * 25.4) / TWIPS_PER_INCH;
17
+ }
18
+
19
+ export function twipsToPt(twips: number): number {
20
+ return twips / TWIPS_PER_POINT;
21
+ }
22
+
23
+ export function ptToTwips(pt: number): number {
24
+ return Math.round(pt * TWIPS_PER_POINT);
25
+ }
26
+
27
+ /** CSS pixels (96 per inch). */
28
+ export function twipsToPx(twips: number): number {
29
+ return (twips * 96) / TWIPS_PER_INCH;
30
+ }
31
+
32
+ export function halfPointsToPt(halfPoints: number): number {
33
+ return halfPoints / 2;
34
+ }