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.
- package/CHANGELOG.md +74 -0
- package/README.md +166 -16
- package/dist/api/editor.d.ts +231 -7
- package/dist/api/editor.js +1125 -26
- package/dist/api/headers.d.ts +51 -0
- package/dist/api/headers.js +133 -0
- package/dist/api/index.d.ts +6 -1
- package/dist/api/index.js +4 -1
- package/dist/api/lists.d.ts +37 -0
- package/dist/api/lists.js +132 -0
- package/dist/api/parts.js +1 -0
- package/dist/api/positions.js +1 -0
- package/dist/api/revisions.d.ts +61 -0
- package/dist/api/revisions.js +401 -0
- package/dist/api/selection.d.ts +57 -0
- package/dist/api/selection.js +91 -0
- package/dist/api/tables.d.ts +60 -0
- package/dist/api/tables.js +138 -0
- package/dist/charts/color.d.ts +45 -0
- package/dist/charts/color.js +322 -0
- package/dist/charts/format.d.ts +12 -0
- package/dist/charts/format.js +459 -0
- package/dist/charts/index.d.ts +11 -0
- package/dist/charts/index.js +8 -0
- package/dist/charts/model.d.ts +225 -0
- package/dist/charts/model.js +758 -0
- package/dist/charts/outline.d.ts +17 -0
- package/dist/charts/outline.js +214 -0
- package/dist/charts/render.d.ts +20 -0
- package/dist/charts/render.js +1772 -0
- package/dist/drawing/arrows.d.ts +37 -0
- package/dist/drawing/arrows.js +99 -0
- package/dist/drawing/formula.d.ts +40 -0
- package/dist/drawing/formula.js +159 -0
- package/dist/drawing/geometry.d.ts +61 -0
- package/dist/drawing/geometry.js +286 -0
- package/dist/drawing/index.d.ts +12 -0
- package/dist/drawing/index.js +8 -0
- package/dist/drawing/presets.d.ts +72 -0
- package/dist/drawing/presets.js +2874 -0
- package/dist/fonts/shaping/apply.d.ts +94 -0
- package/dist/fonts/shaping/apply.js +349 -0
- package/dist/fonts/shaping/arabic.d.ts +28 -0
- package/dist/fonts/shaping/arabic.js +235 -0
- package/dist/fonts/shaping/buffer.d.ts +53 -0
- package/dist/fonts/shaping/buffer.js +95 -0
- package/dist/fonts/shaping/gdef.d.ts +30 -0
- package/dist/fonts/shaping/gdef.js +66 -0
- package/dist/fonts/shaping/gpos.d.ts +86 -0
- package/dist/fonts/shaping/gpos.js +428 -0
- package/dist/fonts/shaping/gsub.d.ts +65 -0
- package/dist/fonts/shaping/gsub.js +313 -0
- package/dist/fonts/shaping/index.d.ts +32 -0
- package/dist/fonts/shaping/index.js +402 -0
- package/dist/fonts/shaping/indic.d.ts +6 -0
- package/dist/fonts/shaping/indic.js +805 -0
- package/dist/fonts/shaping/layout-common.d.ts +124 -0
- package/dist/fonts/shaping/layout-common.js +349 -0
- package/dist/fonts/shaping/plan.d.ts +87 -0
- package/dist/fonts/shaping/plan.js +152 -0
- package/dist/fonts/shaping/scripts.d.ts +26 -0
- package/dist/fonts/shaping/scripts.js +160 -0
- package/dist/fonts/shaping/shaper.d.ts +23 -0
- package/dist/fonts/shaping/shaper.js +5 -0
- package/dist/fonts/shaping/unicode.d.ts +34 -0
- package/dist/fonts/shaping/unicode.js +138 -0
- package/dist/images/bmp.d.ts +9 -0
- package/dist/images/bmp.js +234 -0
- package/dist/images/gif.d.ts +9 -0
- package/dist/images/gif.js +215 -0
- package/dist/images/index.d.ts +18 -0
- package/dist/images/index.js +63 -0
- package/dist/images/png-encode.d.ts +4 -0
- package/dist/images/png-encode.js +73 -0
- package/dist/images/size.d.ts +10 -0
- package/dist/images/size.js +83 -0
- package/dist/images/tiff.d.ts +14 -0
- package/dist/images/tiff.js +407 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +5 -0
- package/dist/layout/bidi.d.ts +18 -0
- package/dist/layout/bidi.js +71 -0
- package/dist/layout/blocks.d.ts +20 -3
- package/dist/layout/blocks.js +88 -58
- package/dist/layout/cache.d.ts +31 -0
- package/dist/layout/cache.js +38 -0
- package/dist/layout/drawing.d.ts +30 -0
- package/dist/layout/drawing.js +120 -0
- package/dist/layout/engine.d.ts +3 -0
- package/dist/layout/engine.js +218 -11
- package/dist/layout/index.d.ts +1 -0
- package/dist/layout/index.js +1 -0
- package/dist/layout/lines.d.ts +63 -5
- package/dist/layout/lines.js +245 -20
- package/dist/layout/paint.d.ts +25 -3
- package/dist/layout/paint.js +26 -10
- package/dist/layout/shapes.d.ts +7 -0
- package/dist/layout/shapes.js +338 -0
- package/dist/layout/style.d.ts +4 -0
- package/dist/layout/style.js +2 -0
- package/dist/layout/types.d.ts +89 -2
- package/dist/math/boxes.d.ts +88 -0
- package/dist/math/boxes.js +166 -0
- package/dist/math/index.d.ts +29 -0
- package/dist/math/index.js +44 -0
- package/dist/math/layout.d.ts +13 -0
- package/dist/math/layout.js +771 -0
- package/dist/math/linear.d.ts +3 -0
- package/dist/math/linear.js +152 -0
- package/dist/math/omml.d.ts +15 -0
- package/dist/math/omml.js +57 -0
- package/dist/math/paths.d.ts +61 -0
- package/dist/math/paths.js +450 -0
- package/dist/math/symbols.d.ts +49 -0
- package/dist/math/symbols.js +269 -0
- package/dist/math/text.d.ts +53 -0
- package/dist/math/text.js +316 -0
- package/dist/metafile/canvas.d.ts +80 -0
- package/dist/metafile/canvas.js +515 -0
- package/dist/metafile/dib.d.ts +27 -0
- package/dist/metafile/dib.js +300 -0
- package/dist/metafile/emf.d.ts +31 -0
- package/dist/metafile/emf.js +591 -0
- package/dist/metafile/geometry.d.ts +49 -0
- package/dist/metafile/geometry.js +85 -0
- package/dist/metafile/index.d.ts +27 -0
- package/dist/metafile/index.js +34 -0
- package/dist/metafile/reader.d.ts +28 -0
- package/dist/metafile/reader.js +59 -0
- package/dist/metafile/state.d.ts +118 -0
- package/dist/metafile/state.js +172 -0
- package/dist/metafile/text.d.ts +17 -0
- package/dist/metafile/text.js +130 -0
- package/dist/metafile/wmf.d.ts +33 -0
- package/dist/metafile/wmf.js +395 -0
- package/dist/model/content.d.ts +92 -7
- package/dist/model/content.js +251 -17
- package/dist/model/document.d.ts +34 -1
- package/dist/model/document.js +80 -1
- package/dist/model/fields.d.ts +4 -0
- package/dist/model/fields.js +10 -0
- package/dist/model/numbering.d.ts +2 -0
- package/dist/model/numbering.js +22 -0
- package/dist/model/props.d.ts +2 -0
- package/dist/model/props.js +3 -0
- package/dist/opc/index.d.ts +1 -1
- package/dist/opc/package.d.ts +34 -0
- package/dist/opc/package.js +58 -4
- package/dist/pdf/export.js +97 -6
- package/dist/pdf/images.d.ts +7 -0
- package/dist/pdf/images.js +39 -2
- package/dist/render/activity.d.ts +179 -0
- package/dist/render/activity.js +1003 -0
- package/dist/render/clipboard.d.ts +29 -0
- package/dist/render/clipboard.js +401 -0
- package/dist/render/comments.d.ts +166 -0
- package/dist/render/comments.js +779 -0
- package/dist/render/context-menu.d.ts +124 -0
- package/dist/render/context-menu.js +525 -0
- package/dist/render/editing.d.ts +146 -5
- package/dist/render/editing.js +678 -53
- package/dist/render/index.d.ts +12 -0
- package/dist/render/index.js +11 -0
- package/dist/render/navigation-panel.d.ts +76 -0
- package/dist/render/navigation-panel.js +431 -0
- package/dist/render/selection.js +5 -0
- package/dist/render/svg.d.ts +4 -6
- package/dist/render/svg.js +69 -7
- package/dist/render/toolbar.d.ts +83 -0
- package/dist/render/toolbar.js +505 -0
- package/dist/render/ui/comment-layout.d.ts +39 -0
- package/dist/render/ui/comment-layout.js +79 -0
- package/dist/render/ui/icons.d.ts +6 -0
- package/dist/render/ui/icons.js +38 -0
- package/dist/render/ui/menu.d.ts +38 -0
- package/dist/render/ui/menu.js +274 -0
- package/dist/render/ui/nav-model.d.ts +11 -0
- package/dist/render/ui/nav-model.js +34 -0
- package/dist/render/ui/overflow.d.ts +5 -0
- package/dist/render/ui/overflow.js +18 -0
- package/dist/render/ui/popover.d.ts +18 -0
- package/dist/render/ui/popover.js +109 -0
- package/dist/render/ui/theme.d.ts +2 -0
- package/dist/render/ui/theme.js +29 -0
- package/dist/render/undo.d.ts +76 -0
- package/dist/render/undo.js +144 -0
- package/dist/render/viewer.d.ts +100 -7
- package/dist/render/viewer.js +304 -92
- package/dist/security/policy.d.ts +3 -0
- package/dist/security/policy.js +6 -2
- package/dist/security/protection.d.ts +1 -0
- package/dist/security/protection.js +1 -1
- package/dist/text/bidi-data.d.ts +53 -0
- package/dist/text/bidi-data.js +318 -0
- package/dist/text/bidi.d.ts +37 -0
- package/dist/text/bidi.js +508 -0
- package/dist/xml/ns.d.ts +5 -0
- package/dist/xml/ns.js +7 -0
- package/package.json +9 -2
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Line end decorations (`a:headEnd` / `a:tailEnd`, ECMA-376 §20.1.2.2.15 and §20.1.2.2.37): the
|
|
3
|
+
* arrowheads, diamonds and dots drawn where a line or open path stops.
|
|
4
|
+
*
|
|
5
|
+
* The spec only names the kinds (triangle, stealth, diamond, oval, arrow) and three relative sizes;
|
|
6
|
+
* the proportions here follow what Office draws: each size is a multiple of the line width
|
|
7
|
+
* (sm 2×, med 3×, lg 5×), measured against a floor so that heads on hairlines stay visible.
|
|
8
|
+
*/
|
|
9
|
+
import type { PathCommand } from '../layout/types.js';
|
|
10
|
+
import type { XmlElement } from '../xml/dom.js';
|
|
11
|
+
export type LineEndType = 'none' | 'triangle' | 'stealth' | 'diamond' | 'oval' | 'arrow';
|
|
12
|
+
export type LineEndSize = 'sm' | 'med' | 'lg';
|
|
13
|
+
export interface LineEndSpec {
|
|
14
|
+
type: LineEndType;
|
|
15
|
+
w: LineEndSize;
|
|
16
|
+
len: LineEndSize;
|
|
17
|
+
}
|
|
18
|
+
export interface LineEndShape {
|
|
19
|
+
/** The decoration in the same coordinates as `tip`; empty for `none`. */
|
|
20
|
+
commands: PathCommand[];
|
|
21
|
+
/** Filled with the line colour (true) or stroked with the line's pen (the open `arrow`). */
|
|
22
|
+
filled: boolean;
|
|
23
|
+
/** How far to pull the line's end back from `tip` so its butt end hides inside the decoration. */
|
|
24
|
+
inset: number;
|
|
25
|
+
}
|
|
26
|
+
interface Point {
|
|
27
|
+
x: number;
|
|
28
|
+
y: number;
|
|
29
|
+
}
|
|
30
|
+
/** Reads an `a:headEnd` or `a:tailEnd`; undefined when absent or `type="none"`. */
|
|
31
|
+
export declare function readLineEnd(el: XmlElement | undefined): LineEndSpec | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* The decoration for a line ending at `tip` and arriving from `from` (the previous point on the
|
|
34
|
+
* line, which gives the direction). `w` and `len` are the spec's sm/med/lg sizes.
|
|
35
|
+
*/
|
|
36
|
+
export declare function lineEnd(type: string, w: string | undefined, len: string | undefined, tip: Point, from: Point, lineWidth: number): LineEndShape;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { attr } from '../xml/dom.js';
|
|
2
|
+
const TYPES = new Set(['none', 'triangle', 'stealth', 'diamond', 'oval', 'arrow']);
|
|
3
|
+
const FACTOR = { sm: 2, med: 3, lg: 5 };
|
|
4
|
+
/**
|
|
5
|
+
* Below about 2pt Office stops shrinking heads with the line; LibreOffice uses the same floor
|
|
6
|
+
* (0.7 mm) when it imports these files.
|
|
7
|
+
*/
|
|
8
|
+
const MIN_BASE_WIDTH = 2;
|
|
9
|
+
/** Where the stealth head's back notch sits, as a fraction of its length from the tip. */
|
|
10
|
+
const STEALTH_NOTCH = 2 / 3;
|
|
11
|
+
/** Cubic Bézier circle constant: control distance for a quarter arc of a unit circle. */
|
|
12
|
+
const KAPPA = 0.5522847498307936;
|
|
13
|
+
function size(v) {
|
|
14
|
+
return v === 'sm' || v === 'lg' ? v : 'med';
|
|
15
|
+
}
|
|
16
|
+
/** Reads an `a:headEnd` or `a:tailEnd`; undefined when absent or `type="none"`. */
|
|
17
|
+
export function readLineEnd(el) {
|
|
18
|
+
if (!el)
|
|
19
|
+
return undefined;
|
|
20
|
+
const type = attr(el, 'type') ?? 'none';
|
|
21
|
+
if (!TYPES.has(type) || type === 'none')
|
|
22
|
+
return undefined;
|
|
23
|
+
return { type: type, w: size(attr(el, 'w')), len: size(attr(el, 'len')) };
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The decoration for a line ending at `tip` and arriving from `from` (the previous point on the
|
|
27
|
+
* line, which gives the direction). `w` and `len` are the spec's sm/med/lg sizes.
|
|
28
|
+
*/
|
|
29
|
+
export function lineEnd(type, w, len, tip, from, lineWidth) {
|
|
30
|
+
const none = { commands: [], filled: false, inset: 0 };
|
|
31
|
+
if (!TYPES.has(type) || type === 'none')
|
|
32
|
+
return none;
|
|
33
|
+
const lw = Math.max(0, lineWidth);
|
|
34
|
+
const base = Math.max(lw, MIN_BASE_WIDTH);
|
|
35
|
+
const W = FACTOR[size(w)] * base;
|
|
36
|
+
const L = FACTOR[size(len)] * base;
|
|
37
|
+
// Unit vector along the line toward the tip, and its normal; a degenerate line points right.
|
|
38
|
+
let dx = tip.x - from.x;
|
|
39
|
+
let dy = tip.y - from.y;
|
|
40
|
+
const d = Math.hypot(dx, dy);
|
|
41
|
+
if (d > 0) {
|
|
42
|
+
dx /= d;
|
|
43
|
+
dy /= d;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
dx = 1;
|
|
47
|
+
dy = 0;
|
|
48
|
+
}
|
|
49
|
+
const nx = -dy;
|
|
50
|
+
const ny = dx;
|
|
51
|
+
/** A point `along` the line from the tip (negative = back toward `from`) and `across` it. */
|
|
52
|
+
const at = (along, across) => ({ x: tip.x + dx * along + nx * across, y: tip.y + dy * along + ny * across });
|
|
53
|
+
const poly = (pts, close) => {
|
|
54
|
+
const out = pts.map((p, i) => ({ op: i === 0 ? 'M' : 'L', x: p.x, y: p.y }));
|
|
55
|
+
if (close)
|
|
56
|
+
out.push({ op: 'Z' });
|
|
57
|
+
return out;
|
|
58
|
+
};
|
|
59
|
+
switch (type) {
|
|
60
|
+
case 'triangle':
|
|
61
|
+
// Stop the line half a pen width inside the base so no seam shows; the base is at least
|
|
62
|
+
// twice the pen width, so the line's corners stay inside the head.
|
|
63
|
+
return { commands: poly([at(0, 0), at(-L, W / 2), at(-L, -W / 2)], true), filled: true, inset: Math.max(0, L - lw / 2) };
|
|
64
|
+
case 'stealth':
|
|
65
|
+
return {
|
|
66
|
+
commands: poly([at(0, 0), at(-L, W / 2), at(-L * STEALTH_NOTCH, 0), at(-L, -W / 2)], true),
|
|
67
|
+
filled: true,
|
|
68
|
+
inset: L * STEALTH_NOTCH,
|
|
69
|
+
};
|
|
70
|
+
case 'diamond':
|
|
71
|
+
// Diamonds and ovals are centred on the end point and cover it, so the line keeps its length.
|
|
72
|
+
return { commands: poly([at(L / 2, 0), at(0, W / 2), at(-L / 2, 0), at(0, -W / 2)], true), filled: true, inset: 0 };
|
|
73
|
+
case 'oval': {
|
|
74
|
+
const a = L / 2;
|
|
75
|
+
const b = W / 2;
|
|
76
|
+
const k = KAPPA;
|
|
77
|
+
const p = (along, across) => at(along, across);
|
|
78
|
+
const c = (c1, c2, e) => ({ op: 'C', x1: c1.x, y1: c1.y, x2: c2.x, y2: c2.y, x: e.x, y: e.y });
|
|
79
|
+
const start = p(a, 0);
|
|
80
|
+
return {
|
|
81
|
+
commands: [
|
|
82
|
+
{ op: 'M', x: start.x, y: start.y },
|
|
83
|
+
c(p(a, k * b), p(k * a, b), p(0, b)),
|
|
84
|
+
c(p(-k * a, b), p(-a, k * b), p(-a, 0)),
|
|
85
|
+
c(p(-a, -k * b), p(-k * a, -b), p(0, -b)),
|
|
86
|
+
c(p(k * a, -b), p(a, -k * b), p(a, 0)),
|
|
87
|
+
{ op: 'Z' },
|
|
88
|
+
],
|
|
89
|
+
filled: true,
|
|
90
|
+
inset: 0,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
case 'arrow':
|
|
94
|
+
// An open chevron stroked with the line's own pen; the line runs into its joint.
|
|
95
|
+
return { commands: poly([at(-L, W / 2), at(0, 0), at(-L, -W / 2)], false), filled: false, inset: Math.min(lw / 2, L) };
|
|
96
|
+
default:
|
|
97
|
+
return none;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DrawingML shape guide formulas (ECMA-376 §20.1.9.11).
|
|
3
|
+
*
|
|
4
|
+
* Preset and custom shapes describe their geometry as an ordered list of named guides, each one
|
|
5
|
+
* operator applied to numeric literals or to earlier guides. This module evaluates them. Angles are
|
|
6
|
+
* in 60000ths of a degree, as in the files, and lengths are in whatever unit the caller chose for
|
|
7
|
+
* `w` and `h` (the formulas are unit-agnostic as long as every length shares that unit).
|
|
8
|
+
*/
|
|
9
|
+
/** Guide values by name, in evaluation order. */
|
|
10
|
+
export type Guides = Map<string, number>;
|
|
11
|
+
/** A named formula: an `a:gd` element. */
|
|
12
|
+
export interface GuideDef {
|
|
13
|
+
name: string;
|
|
14
|
+
fmla: string;
|
|
15
|
+
}
|
|
16
|
+
/** 60000ths of a degree → radians. */
|
|
17
|
+
export declare function angleToRadians(a: number): number;
|
|
18
|
+
/** Radians → 60000ths of a degree. */
|
|
19
|
+
export declare function radiansToAngle(r: number): number;
|
|
20
|
+
/**
|
|
21
|
+
* The value of one formula argument: a guide name or a numeric literal. Guides are looked up first
|
|
22
|
+
* because some preset guide names start with a digit (`2a1`, `3cd4`) and would otherwise half-parse
|
|
23
|
+
* as numbers. Unknown names count as 0 rather than throwing, so one bad guide in a document cannot
|
|
24
|
+
* stop the rest of the page from drawing.
|
|
25
|
+
*/
|
|
26
|
+
export declare function resolveValue(token: string, guides: Guides): number;
|
|
27
|
+
/**
|
|
28
|
+
* The guides every shape gets for free: the shape box, its fractions, and common angles. The spec
|
|
29
|
+
* lists a fixed set (wd2 … wd32 and so on); a few extra divisors are defined too since they cost
|
|
30
|
+
* nothing and some producers use them.
|
|
31
|
+
*/
|
|
32
|
+
export declare function builtinGuides(w: number, h: number): Guides;
|
|
33
|
+
/** Evaluates one formula such as `+- r 0 x1` against the guides defined so far. */
|
|
34
|
+
export declare function evaluateFormula(fmla: string, guides: Guides): number;
|
|
35
|
+
/**
|
|
36
|
+
* Evaluates a shape's guides: the built-ins for a `w`×`h` box, then the adjust values (`avLst`,
|
|
37
|
+
* where a value from `adjust` replaces the default formula), then `gdLst` in document order so
|
|
38
|
+
* each guide can use the ones before it.
|
|
39
|
+
*/
|
|
40
|
+
export declare function evaluateGuides(w: number, h: number, avLst: readonly GuideDef[], gdLst: readonly GuideDef[], adjust?: Readonly<Record<string, number>>): Guides;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DrawingML shape guide formulas (ECMA-376 §20.1.9.11).
|
|
3
|
+
*
|
|
4
|
+
* Preset and custom shapes describe their geometry as an ordered list of named guides, each one
|
|
5
|
+
* operator applied to numeric literals or to earlier guides. This module evaluates them. Angles are
|
|
6
|
+
* in 60000ths of a degree, as in the files, and lengths are in whatever unit the caller chose for
|
|
7
|
+
* `w` and `h` (the formulas are unit-agnostic as long as every length shares that unit).
|
|
8
|
+
*/
|
|
9
|
+
const RAD_PER_UNIT = Math.PI / (180 * 60000);
|
|
10
|
+
/** 60000ths of a degree → radians. */
|
|
11
|
+
export function angleToRadians(a) {
|
|
12
|
+
return a * RAD_PER_UNIT;
|
|
13
|
+
}
|
|
14
|
+
/** Radians → 60000ths of a degree. */
|
|
15
|
+
export function radiansToAngle(r) {
|
|
16
|
+
return r / RAD_PER_UNIT;
|
|
17
|
+
}
|
|
18
|
+
const NUMBER = /^[-+]?(\d+\.?\d*|\.\d+)(e[-+]?\d+)?$/i;
|
|
19
|
+
/**
|
|
20
|
+
* The value of one formula argument: a guide name or a numeric literal. Guides are looked up first
|
|
21
|
+
* because some preset guide names start with a digit (`2a1`, `3cd4`) and would otherwise half-parse
|
|
22
|
+
* as numbers. Unknown names count as 0 rather than throwing, so one bad guide in a document cannot
|
|
23
|
+
* stop the rest of the page from drawing.
|
|
24
|
+
*/
|
|
25
|
+
export function resolveValue(token, guides) {
|
|
26
|
+
const v = guides.get(token);
|
|
27
|
+
if (v !== undefined)
|
|
28
|
+
return v;
|
|
29
|
+
if (NUMBER.test(token))
|
|
30
|
+
return Number(token);
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The guides every shape gets for free: the shape box, its fractions, and common angles. The spec
|
|
35
|
+
* lists a fixed set (wd2 … wd32 and so on); a few extra divisors are defined too since they cost
|
|
36
|
+
* nothing and some producers use them.
|
|
37
|
+
*/
|
|
38
|
+
export function builtinGuides(w, h) {
|
|
39
|
+
const ss = Math.min(w, h);
|
|
40
|
+
const ls = Math.max(w, h);
|
|
41
|
+
const g = new Map([
|
|
42
|
+
['l', 0],
|
|
43
|
+
['t', 0],
|
|
44
|
+
['r', w],
|
|
45
|
+
['b', h],
|
|
46
|
+
['w', w],
|
|
47
|
+
['h', h],
|
|
48
|
+
['hc', w / 2],
|
|
49
|
+
['vc', h / 2],
|
|
50
|
+
['ss', ss],
|
|
51
|
+
['ls', ls],
|
|
52
|
+
['cd2', 10800000],
|
|
53
|
+
['cd4', 5400000],
|
|
54
|
+
['cd8', 2700000],
|
|
55
|
+
['3cd4', 16200000],
|
|
56
|
+
['3cd8', 8100000],
|
|
57
|
+
['5cd8', 13500000],
|
|
58
|
+
['7cd8', 18900000],
|
|
59
|
+
]);
|
|
60
|
+
for (const d of [2, 3, 4, 5, 6, 8, 10, 12, 16, 32]) {
|
|
61
|
+
g.set(`wd${d}`, w / d);
|
|
62
|
+
g.set(`hd${d}`, h / d);
|
|
63
|
+
g.set(`ssd${d}`, ss / d);
|
|
64
|
+
}
|
|
65
|
+
return g;
|
|
66
|
+
}
|
|
67
|
+
/** x·y/z and (x+y)/z with a zero divisor give 0: a collapsed (zero-size) shape must still evaluate. */
|
|
68
|
+
function div(n, d) {
|
|
69
|
+
return d === 0 ? 0 : n / d;
|
|
70
|
+
}
|
|
71
|
+
/** Evaluates one formula such as `+- r 0 x1` against the guides defined so far. */
|
|
72
|
+
export function evaluateFormula(fmla, guides) {
|
|
73
|
+
const parts = fmla.trim().split(/\s+/);
|
|
74
|
+
const op = parts[0] ?? '';
|
|
75
|
+
const arg = (i) => {
|
|
76
|
+
const t = parts[i + 1];
|
|
77
|
+
return t === undefined ? 0 : resolveValue(t, guides);
|
|
78
|
+
};
|
|
79
|
+
const x = arg(0);
|
|
80
|
+
let v;
|
|
81
|
+
switch (op) {
|
|
82
|
+
case '*/':
|
|
83
|
+
v = div(x * arg(1), arg(2));
|
|
84
|
+
break;
|
|
85
|
+
case '+-':
|
|
86
|
+
v = x + arg(1) - arg(2);
|
|
87
|
+
break;
|
|
88
|
+
case '+/':
|
|
89
|
+
v = div(x + arg(1), arg(2));
|
|
90
|
+
break;
|
|
91
|
+
case '?:':
|
|
92
|
+
v = x > 0 ? arg(1) : arg(2);
|
|
93
|
+
break;
|
|
94
|
+
case 'abs':
|
|
95
|
+
v = Math.abs(x);
|
|
96
|
+
break;
|
|
97
|
+
case 'at2':
|
|
98
|
+
// Arguments are (x, y); the result is the angle of that vector.
|
|
99
|
+
v = radiansToAngle(Math.atan2(arg(1), x));
|
|
100
|
+
break;
|
|
101
|
+
case 'cat2':
|
|
102
|
+
v = x * Math.cos(Math.atan2(arg(2), arg(1)));
|
|
103
|
+
break;
|
|
104
|
+
case 'sat2':
|
|
105
|
+
v = x * Math.sin(Math.atan2(arg(2), arg(1)));
|
|
106
|
+
break;
|
|
107
|
+
case 'cos':
|
|
108
|
+
v = x * Math.cos(angleToRadians(arg(1)));
|
|
109
|
+
break;
|
|
110
|
+
case 'sin':
|
|
111
|
+
v = x * Math.sin(angleToRadians(arg(1)));
|
|
112
|
+
break;
|
|
113
|
+
case 'tan':
|
|
114
|
+
v = x * Math.tan(angleToRadians(arg(1)));
|
|
115
|
+
break;
|
|
116
|
+
case 'max':
|
|
117
|
+
v = Math.max(x, arg(1));
|
|
118
|
+
break;
|
|
119
|
+
case 'min':
|
|
120
|
+
v = Math.min(x, arg(1));
|
|
121
|
+
break;
|
|
122
|
+
case 'mod': {
|
|
123
|
+
const y = arg(1);
|
|
124
|
+
const z = arg(2);
|
|
125
|
+
v = Math.sqrt(x * x + y * y + z * z);
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
case 'pin': {
|
|
129
|
+
const y = arg(1);
|
|
130
|
+
const z = arg(2);
|
|
131
|
+
v = y < x ? x : y > z ? z : y;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case 'sqrt':
|
|
135
|
+
v = Math.sqrt(Math.max(0, x));
|
|
136
|
+
break;
|
|
137
|
+
case 'val':
|
|
138
|
+
v = x;
|
|
139
|
+
break;
|
|
140
|
+
default:
|
|
141
|
+
v = 0;
|
|
142
|
+
}
|
|
143
|
+
return Number.isFinite(v) ? v : 0;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Evaluates a shape's guides: the built-ins for a `w`×`h` box, then the adjust values (`avLst`,
|
|
147
|
+
* where a value from `adjust` replaces the default formula), then `gdLst` in document order so
|
|
148
|
+
* each guide can use the ones before it.
|
|
149
|
+
*/
|
|
150
|
+
export function evaluateGuides(w, h, avLst, gdLst, adjust = {}) {
|
|
151
|
+
const g = builtinGuides(w, h);
|
|
152
|
+
for (const gd of avLst) {
|
|
153
|
+
const override = adjust[gd.name];
|
|
154
|
+
g.set(gd.name, override !== undefined && Number.isFinite(override) ? override : evaluateFormula(gd.fmla, g));
|
|
155
|
+
}
|
|
156
|
+
for (const gd of gdLst)
|
|
157
|
+
g.set(gd.name, evaluateFormula(gd.fmla, g));
|
|
158
|
+
return g;
|
|
159
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns DrawingML shape geometry (`a:prstGeom` and `a:custGeom`) into vector paths in shape-local
|
|
3
|
+
* points: origin at the shape's top-left, x right, y down, spanning 0..w and 0..h.
|
|
4
|
+
*
|
|
5
|
+
* Guides are evaluated in EMU (12700 per point) because a custom geometry's literal coordinates are
|
|
6
|
+
* EMU when its path has no `w`/`h` of its own; presets are unit-free so the choice does not affect
|
|
7
|
+
* them. Arcs become cubic Béziers so every renderer (SVG, PDF) only needs M/L/C/Z.
|
|
8
|
+
*/
|
|
9
|
+
import type { PathCommand } from '../layout/types.js';
|
|
10
|
+
import type { XmlElement } from '../xml/dom.js';
|
|
11
|
+
import type { PathFill, ShapeDefinition } from './presets.js';
|
|
12
|
+
export type { PathFill } from './presets.js';
|
|
13
|
+
/** One drawn path: how to fill it (relative to the shape fill) and whether to stroke it. */
|
|
14
|
+
export interface ShapePath {
|
|
15
|
+
commands: PathCommand[];
|
|
16
|
+
fill: PathFill;
|
|
17
|
+
stroke: boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface ShapeGeometry {
|
|
20
|
+
paths: ShapePath[];
|
|
21
|
+
/** Where the shape's text goes, in shape-local points. */
|
|
22
|
+
textRect: {
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
25
|
+
w: number;
|
|
26
|
+
h: number;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Evaluates a parsed definition for a `w`×`h` box given in geometry units, of which there are
|
|
31
|
+
* `unitsPerPoint` to an output point.
|
|
32
|
+
*/
|
|
33
|
+
export declare function evaluateDefinition(def: ShapeDefinition, w: number, h: number, adjust?: Readonly<Record<string, number>>, unitsPerPoint?: number): ShapeGeometry;
|
|
34
|
+
/**
|
|
35
|
+
* The geometry of a preset shape (`a:prstGeom prst="…"`) sized `w`×`h` points, or undefined when
|
|
36
|
+
* the preset is not known. `adjust` holds the shape's `a:avLst` overrides (see readAdjustments).
|
|
37
|
+
*/
|
|
38
|
+
export declare function presetGeometry(name: string, w: number, h: number, adjust?: Record<string, number>): ShapeGeometry | undefined;
|
|
39
|
+
/** The adjust values of an `a:prstGeom` (its `a:avLst/a:gd`, normally `fmla="val N"`). */
|
|
40
|
+
export declare function readAdjustments(prstGeom: XmlElement): Record<string, number>;
|
|
41
|
+
/** Parses an `a:custGeom` element into a definition (guides, text rectangle and paths). */
|
|
42
|
+
export declare function parseCustomGeometry(custGeom: XmlElement): ShapeDefinition;
|
|
43
|
+
/** The geometry of an `a:custGeom` for a shape sized `w`×`h` points. */
|
|
44
|
+
export declare function customGeometry(custGeom: XmlElement, w: number, h: number): ShapeGeometry;
|
|
45
|
+
/**
|
|
46
|
+
* `a:arcTo` from the current point (x0, y0): an arc of the ellipse with radii wR, hR, starting at
|
|
47
|
+
* angle stAng and sweeping swAng (60000ths of a degree, clockwise with y down). The angles are
|
|
48
|
+
* directions from the ellipse centre (the preset guides compute start points that way, e.g. `arc`
|
|
49
|
+
* and `moon`), so they are converted to the ellipse's parametric angles before drawing. The arc is
|
|
50
|
+
* emitted as cubic Béziers of at most 90° each, through `emit`; returns the end point.
|
|
51
|
+
*/
|
|
52
|
+
export declare function arcToBeziers(x0: number, y0: number, wR: number, hR: number, stAng: number, swAng: number, emit: (x1: number, y1: number, x2: number, y2: number, x: number, y: number) => void): {
|
|
53
|
+
x: number;
|
|
54
|
+
y: number;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* The colour a path with a `lighten`/`darken` fill mode is painted, given the shape's fill colour
|
|
58
|
+
* as RRGGBB: darken scales toward black (60%, darkenLess 80%), lighten mixes toward white (40%,
|
|
59
|
+
* lightenLess 20%), which is how PowerPoint shades the faces of 3-D-looking presets like `cube`.
|
|
60
|
+
*/
|
|
61
|
+
export declare function shadeFill(color: string, mode: PathFill): string;
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { attr, elements, firstChild } from '../xml/dom.js';
|
|
2
|
+
import { angleToRadians, evaluateFormula, evaluateGuides, resolveValue } from './formula.js';
|
|
3
|
+
import { presetDefinition } from './presets.js';
|
|
4
|
+
const EMU_PER_PT = 12700;
|
|
5
|
+
/**
|
|
6
|
+
* Evaluates a parsed definition for a `w`×`h` box given in geometry units, of which there are
|
|
7
|
+
* `unitsPerPoint` to an output point.
|
|
8
|
+
*/
|
|
9
|
+
export function evaluateDefinition(def, w, h, adjust = {}, unitsPerPoint = 1) {
|
|
10
|
+
const g = evaluateGuides(w, h, def.avLst, def.gdLst, adjust);
|
|
11
|
+
const paths = def.pathLst.map((p) => evaluatePath(p, g, w, h, unitsPerPoint));
|
|
12
|
+
const tr = def.textRect;
|
|
13
|
+
let textRect = { x: 0, y: 0, w: w / unitsPerPoint, h: h / unitsPerPoint };
|
|
14
|
+
if (tr) {
|
|
15
|
+
const l = resolveValue(tr.l, g) / unitsPerPoint;
|
|
16
|
+
const t = resolveValue(tr.t, g) / unitsPerPoint;
|
|
17
|
+
const r = resolveValue(tr.r, g) / unitsPerPoint;
|
|
18
|
+
const b = resolveValue(tr.b, g) / unitsPerPoint;
|
|
19
|
+
textRect = { x: l, y: t, w: r - l, h: b - t };
|
|
20
|
+
}
|
|
21
|
+
return { paths, textRect };
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The geometry of a preset shape (`a:prstGeom prst="…"`) sized `w`×`h` points, or undefined when
|
|
25
|
+
* the preset is not known. `adjust` holds the shape's `a:avLst` overrides (see readAdjustments).
|
|
26
|
+
*/
|
|
27
|
+
export function presetGeometry(name, w, h, adjust) {
|
|
28
|
+
const def = presetDefinition(name);
|
|
29
|
+
if (!def)
|
|
30
|
+
return undefined;
|
|
31
|
+
return evaluateDefinition(def, w * EMU_PER_PT, h * EMU_PER_PT, normaliseAdjust(def, adjust), EMU_PER_PT);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Some producers write a single handle as `adj` for shapes whose handle is called `adj1` (and the
|
|
35
|
+
* other way round); match it to the name the shape actually has rather than dropping it.
|
|
36
|
+
*/
|
|
37
|
+
function normaliseAdjust(def, adjust) {
|
|
38
|
+
if (!adjust)
|
|
39
|
+
return {};
|
|
40
|
+
const names = new Set(def.avLst.map((g) => g.name));
|
|
41
|
+
const out = {};
|
|
42
|
+
for (const [k, v] of Object.entries(adjust)) {
|
|
43
|
+
if (names.has(k))
|
|
44
|
+
out[k] = v;
|
|
45
|
+
else if (k === 'adj' && names.has('adj1') && adjust['adj1'] === undefined)
|
|
46
|
+
out['adj1'] = v;
|
|
47
|
+
else if (k === 'adj1' && names.has('adj') && adjust['adj'] === undefined)
|
|
48
|
+
out['adj'] = v;
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
/** The adjust values of an `a:prstGeom` (its `a:avLst/a:gd`, normally `fmla="val N"`). */
|
|
53
|
+
export function readAdjustments(prstGeom) {
|
|
54
|
+
const out = {};
|
|
55
|
+
const av = firstChild(prstGeom, 'a:avLst');
|
|
56
|
+
if (!av)
|
|
57
|
+
return out;
|
|
58
|
+
for (const gd of elements(av, 'a:gd')) {
|
|
59
|
+
const name = attr(gd, 'name');
|
|
60
|
+
const fmla = attr(gd, 'fmla');
|
|
61
|
+
if (!name || !fmla)
|
|
62
|
+
continue;
|
|
63
|
+
// Adjust values are formulas too; anything but `val` is rare, and with no guides in scope the
|
|
64
|
+
// evaluator still handles constant forms.
|
|
65
|
+
out[name] = evaluateFormula(fmla, new Map());
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
/** Parses an `a:custGeom` element into a definition (guides, text rectangle and paths). */
|
|
70
|
+
export function parseCustomGeometry(custGeom) {
|
|
71
|
+
const guides = (list) => list ? elements(list, 'a:gd').map((gd) => ({ name: attr(gd, 'name') ?? '', fmla: attr(gd, 'fmla') ?? 'val 0' })) : [];
|
|
72
|
+
const def = {
|
|
73
|
+
avLst: guides(firstChild(custGeom, 'a:avLst')),
|
|
74
|
+
gdLst: guides(firstChild(custGeom, 'a:gdLst')),
|
|
75
|
+
pathLst: [],
|
|
76
|
+
};
|
|
77
|
+
const rect = firstChild(custGeom, 'a:rect');
|
|
78
|
+
if (rect)
|
|
79
|
+
def.textRect = { l: attr(rect, 'l') ?? 'l', t: attr(rect, 't') ?? 't', r: attr(rect, 'r') ?? 'r', b: attr(rect, 'b') ?? 'b' };
|
|
80
|
+
const pathLst = firstChild(custGeom, 'a:pathLst');
|
|
81
|
+
for (const p of pathLst ? elements(pathLst, 'a:path') : [])
|
|
82
|
+
def.pathLst.push(parsePathElement(p));
|
|
83
|
+
return def;
|
|
84
|
+
}
|
|
85
|
+
const FILL_MODES = new Set(['norm', 'none', 'lighten', 'lightenLess', 'darken', 'darkenLess']);
|
|
86
|
+
function xsdBool(v, dflt) {
|
|
87
|
+
if (v === undefined)
|
|
88
|
+
return dflt;
|
|
89
|
+
return v === 'true' || v === '1';
|
|
90
|
+
}
|
|
91
|
+
function parsePathElement(p) {
|
|
92
|
+
const fill = attr(p, 'fill') ?? 'norm';
|
|
93
|
+
const def = {
|
|
94
|
+
w: Number(attr(p, 'w') ?? 0) || 0,
|
|
95
|
+
h: Number(attr(p, 'h') ?? 0) || 0,
|
|
96
|
+
fill: FILL_MODES.has(fill) ? fill : 'norm',
|
|
97
|
+
stroke: xsdBool(attr(p, 'stroke'), true),
|
|
98
|
+
extrusionOk: xsdBool(attr(p, 'extrusionOk'), true),
|
|
99
|
+
commands: [],
|
|
100
|
+
};
|
|
101
|
+
const pts = (el) => elements(el, 'a:pt').map((pt) => ({ x: attr(pt, 'x') ?? '0', y: attr(pt, 'y') ?? '0' }));
|
|
102
|
+
for (const c of elements(p)) {
|
|
103
|
+
switch (c.name) {
|
|
104
|
+
case 'a:moveTo':
|
|
105
|
+
case 'a:lnTo': {
|
|
106
|
+
const pt = pts(c)[0];
|
|
107
|
+
if (pt)
|
|
108
|
+
def.commands.push({ op: c.name === 'a:moveTo' ? 'moveTo' : 'lnTo', pt });
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
case 'a:arcTo':
|
|
112
|
+
def.commands.push({ op: 'arcTo', wR: attr(c, 'wR') ?? '0', hR: attr(c, 'hR') ?? '0', stAng: attr(c, 'stAng') ?? '0', swAng: attr(c, 'swAng') ?? '0' });
|
|
113
|
+
break;
|
|
114
|
+
case 'a:quadBezTo': {
|
|
115
|
+
const [a, b] = pts(c);
|
|
116
|
+
if (a && b)
|
|
117
|
+
def.commands.push({ op: 'quadBezTo', pts: [a, b] });
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case 'a:cubicBezTo': {
|
|
121
|
+
const [a, b, d] = pts(c);
|
|
122
|
+
if (a && b && d)
|
|
123
|
+
def.commands.push({ op: 'cubicBezTo', pts: [a, b, d] });
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
case 'a:close':
|
|
127
|
+
def.commands.push({ op: 'close' });
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return def;
|
|
132
|
+
}
|
|
133
|
+
/** The geometry of an `a:custGeom` for a shape sized `w`×`h` points. */
|
|
134
|
+
export function customGeometry(custGeom, w, h) {
|
|
135
|
+
return evaluateDefinition(parseCustomGeometry(custGeom), w * EMU_PER_PT, h * EMU_PER_PT, {}, EMU_PER_PT);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Evaluates one path. Coordinates are worked out in the path's own space (its `w`/`h`, or the
|
|
139
|
+
* shape's when those are 0) and scaled into the shape at output, so an arc's radii and angles are
|
|
140
|
+
* interpreted where the spec defines them.
|
|
141
|
+
*/
|
|
142
|
+
function evaluatePath(def, g, w, h, unitsPerPoint) {
|
|
143
|
+
const sx = (def.w > 0 ? w / def.w : 1) / unitsPerPoint;
|
|
144
|
+
const sy = (def.h > 0 ? h / def.h : 1) / unitsPerPoint;
|
|
145
|
+
const v = (s) => resolveValue(s, g);
|
|
146
|
+
const out = [];
|
|
147
|
+
let cx = 0;
|
|
148
|
+
let cy = 0;
|
|
149
|
+
let startX = 0;
|
|
150
|
+
let startY = 0;
|
|
151
|
+
let open = false;
|
|
152
|
+
const ensureOpen = () => {
|
|
153
|
+
// PDF needs a current point before a segment; the spec starts at the subpath start after close.
|
|
154
|
+
if (open)
|
|
155
|
+
return;
|
|
156
|
+
out.push({ op: 'M', x: cx * sx, y: cy * sy });
|
|
157
|
+
startX = cx;
|
|
158
|
+
startY = cy;
|
|
159
|
+
open = true;
|
|
160
|
+
};
|
|
161
|
+
for (const c of def.commands) {
|
|
162
|
+
switch (c.op) {
|
|
163
|
+
case 'moveTo':
|
|
164
|
+
cx = v(c.pt.x);
|
|
165
|
+
cy = v(c.pt.y);
|
|
166
|
+
out.push({ op: 'M', x: cx * sx, y: cy * sy });
|
|
167
|
+
startX = cx;
|
|
168
|
+
startY = cy;
|
|
169
|
+
open = true;
|
|
170
|
+
break;
|
|
171
|
+
case 'lnTo':
|
|
172
|
+
ensureOpen();
|
|
173
|
+
cx = v(c.pt.x);
|
|
174
|
+
cy = v(c.pt.y);
|
|
175
|
+
out.push({ op: 'L', x: cx * sx, y: cy * sy });
|
|
176
|
+
break;
|
|
177
|
+
case 'arcTo': {
|
|
178
|
+
ensureOpen();
|
|
179
|
+
const end = arcToBeziers(cx, cy, v(c.wR), v(c.hR), v(c.stAng), v(c.swAng), (x1, y1, x2, y2, x, y) => {
|
|
180
|
+
out.push({ op: 'C', x1: x1 * sx, y1: y1 * sy, x2: x2 * sx, y2: y2 * sy, x: x * sx, y: y * sy });
|
|
181
|
+
});
|
|
182
|
+
cx = end.x;
|
|
183
|
+
cy = end.y;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
case 'quadBezTo': {
|
|
187
|
+
ensureOpen();
|
|
188
|
+
const [q, p] = c.pts;
|
|
189
|
+
const qx = v(q.x);
|
|
190
|
+
const qy = v(q.y);
|
|
191
|
+
const px = v(p.x);
|
|
192
|
+
const py = v(p.y);
|
|
193
|
+
// A quadratic is the cubic whose controls sit 2/3 of the way to the quadratic control.
|
|
194
|
+
out.push({
|
|
195
|
+
op: 'C',
|
|
196
|
+
x1: (cx + (2 / 3) * (qx - cx)) * sx,
|
|
197
|
+
y1: (cy + (2 / 3) * (qy - cy)) * sy,
|
|
198
|
+
x2: (px + (2 / 3) * (qx - px)) * sx,
|
|
199
|
+
y2: (py + (2 / 3) * (qy - py)) * sy,
|
|
200
|
+
x: px * sx,
|
|
201
|
+
y: py * sy,
|
|
202
|
+
});
|
|
203
|
+
cx = px;
|
|
204
|
+
cy = py;
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
case 'cubicBezTo': {
|
|
208
|
+
ensureOpen();
|
|
209
|
+
const [a, b, p] = c.pts;
|
|
210
|
+
cx = v(p.x);
|
|
211
|
+
cy = v(p.y);
|
|
212
|
+
out.push({ op: 'C', x1: v(a.x) * sx, y1: v(a.y) * sy, x2: v(b.x) * sx, y2: v(b.y) * sy, x: cx * sx, y: cy * sy });
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
case 'close':
|
|
216
|
+
if (open) {
|
|
217
|
+
out.push({ op: 'Z' });
|
|
218
|
+
open = false;
|
|
219
|
+
cx = startX;
|
|
220
|
+
cy = startY;
|
|
221
|
+
}
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return { commands: out, fill: def.fill, stroke: def.stroke };
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* `a:arcTo` from the current point (x0, y0): an arc of the ellipse with radii wR, hR, starting at
|
|
229
|
+
* angle stAng and sweeping swAng (60000ths of a degree, clockwise with y down). The angles are
|
|
230
|
+
* directions from the ellipse centre (the preset guides compute start points that way, e.g. `arc`
|
|
231
|
+
* and `moon`), so they are converted to the ellipse's parametric angles before drawing. The arc is
|
|
232
|
+
* emitted as cubic Béziers of at most 90° each, through `emit`; returns the end point.
|
|
233
|
+
*/
|
|
234
|
+
export function arcToBeziers(x0, y0, wR, hR, stAng, swAng, emit) {
|
|
235
|
+
if ((wR === 0 && hR === 0) || swAng === 0)
|
|
236
|
+
return { x: x0, y: y0 };
|
|
237
|
+
const st = angleToRadians(stAng);
|
|
238
|
+
const sw = angleToRadians(swAng);
|
|
239
|
+
const param = (a) => Math.atan2(wR * Math.sin(a), hR * Math.cos(a));
|
|
240
|
+
const t0 = param(st);
|
|
241
|
+
let dt = param(st + sw) - t0;
|
|
242
|
+
// The visual→parametric map keeps whole turns, so pick the parametric sweep nearest the visual one.
|
|
243
|
+
dt += 2 * Math.PI * Math.round((sw - dt) / (2 * Math.PI));
|
|
244
|
+
const cxe = x0 - wR * Math.cos(t0);
|
|
245
|
+
const cye = y0 - hR * Math.sin(t0);
|
|
246
|
+
const n = Math.max(1, Math.ceil(Math.abs(dt) / (Math.PI / 2) - 1e-9));
|
|
247
|
+
const step = dt / n;
|
|
248
|
+
const k = (4 / 3) * Math.tan(step / 4);
|
|
249
|
+
let px = x0;
|
|
250
|
+
let py = y0;
|
|
251
|
+
for (let i = 0; i < n; i++) {
|
|
252
|
+
const a1 = t0 + i * step;
|
|
253
|
+
const a2 = a1 + step;
|
|
254
|
+
const c1 = Math.cos(a1);
|
|
255
|
+
const s1 = Math.sin(a1);
|
|
256
|
+
const c2 = Math.cos(a2);
|
|
257
|
+
const s2 = Math.sin(a2);
|
|
258
|
+
const ex = cxe + wR * c2;
|
|
259
|
+
const ey = cye + hR * s2;
|
|
260
|
+
emit(px - k * wR * s1, py + k * hR * c1, ex + k * wR * s2, ey - k * hR * c2, ex, ey);
|
|
261
|
+
px = ex;
|
|
262
|
+
py = ey;
|
|
263
|
+
}
|
|
264
|
+
return { x: px, y: py };
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* The colour a path with a `lighten`/`darken` fill mode is painted, given the shape's fill colour
|
|
268
|
+
* as RRGGBB: darken scales toward black (60%, darkenLess 80%), lighten mixes toward white (40%,
|
|
269
|
+
* lightenLess 20%), which is how PowerPoint shades the faces of 3-D-looking presets like `cube`.
|
|
270
|
+
*/
|
|
271
|
+
export function shadeFill(color, mode) {
|
|
272
|
+
const hex = color.replace(/^#/, '');
|
|
273
|
+
if (!/^[0-9a-fA-F]{6}$/.test(hex))
|
|
274
|
+
return color;
|
|
275
|
+
if (mode === 'norm' || mode === 'none')
|
|
276
|
+
return hex.toUpperCase();
|
|
277
|
+
// Darkening scales each channel; lightening moves it that fraction of the way to 255.
|
|
278
|
+
const darken = mode === 'darken' ? 0.6 : mode === 'darkenLess' ? 0.8 : 1;
|
|
279
|
+
const lighten = mode === 'lighten' ? 0.4 : mode === 'lightenLess' ? 0.2 : 0;
|
|
280
|
+
return [0, 2, 4]
|
|
281
|
+
.map((i) => Number.parseInt(hex.slice(i, i + 2), 16))
|
|
282
|
+
.map((c) => c * darken + (255 - c) * lighten)
|
|
283
|
+
.map((c) => Math.round(Math.min(255, Math.max(0, c))).toString(16).padStart(2, '0'))
|
|
284
|
+
.join('')
|
|
285
|
+
.toUpperCase();
|
|
286
|
+
}
|