@moondraft/import 0.1.0 → 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 (84) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +2 -2
  3. package/SOURCE.md +229 -4
  4. package/dist/adapter/convert.d.ts +24 -2
  5. package/dist/adapter/convert.js +669 -160
  6. package/dist/adapter/figma-descriptions.d.ts +7 -0
  7. package/dist/adapter/figma-descriptions.js +34 -0
  8. package/dist/adapter/figma-grids.d.ts +12 -0
  9. package/dist/adapter/figma-grids.js +175 -0
  10. package/dist/adapter/figma-handoff.d.ts +12 -0
  11. package/dist/adapter/figma-handoff.js +167 -0
  12. package/dist/adapter/figma-hyperlinks.d.ts +13 -0
  13. package/dist/adapter/figma-hyperlinks.js +77 -0
  14. package/dist/adapter/figma.d.ts +4 -1
  15. package/dist/adapter/figma.js +14 -4
  16. package/dist/adapter/file-components.d.ts +9 -3
  17. package/dist/adapter/file-components.js +122 -32
  18. package/dist/adapter/file-resources.js +316 -27
  19. package/dist/adapter/image-crop.d.ts +18 -0
  20. package/dist/adapter/image-crop.js +30 -0
  21. package/dist/adapter/link.d.ts +5 -1
  22. package/dist/adapter/link.js +308 -104
  23. package/dist/adapter/payload.js +1 -1
  24. package/dist/adapter/variables.d.ts +2 -1
  25. package/dist/adapter/variables.js +5 -2
  26. package/dist/figma.js +167 -33
  27. package/dist/index.d.ts +1 -0
  28. package/dist/index.js +1 -0
  29. package/dist/report.d.ts +28 -1
  30. package/dist/report.js +102 -29
  31. package/dist/serialize.d.ts +7 -0
  32. package/dist/serialize.js +79 -0
  33. package/dist/taco/figma-import/container/kiwiContainer.d.ts +3 -0
  34. package/dist/taco/figma-import/container/kiwiContainer.js +1 -1
  35. package/dist/taco/figma-import/import/carry.d.ts +14 -1
  36. package/dist/taco/figma-import/import/carry.js +34 -1
  37. package/dist/taco/figma-import/import/componentProps.d.ts +10 -4
  38. package/dist/taco/figma-import/import/componentProps.js +29 -13
  39. package/dist/taco/figma-import/import/componentSets.d.ts +2 -0
  40. package/dist/taco/figma-import/import/componentSets.js +1 -1
  41. package/dist/taco/figma-import/import/converters/exportSettings.d.ts +4 -0
  42. package/dist/taco/figma-import/import/converters/exportSettings.js +49 -0
  43. package/dist/taco/figma-import/import/converters/frame.js +8 -0
  44. package/dist/taco/figma-import/import/converters/gradient.d.ts +2 -0
  45. package/dist/taco/figma-import/import/converters/gradient.js +3 -1
  46. package/dist/taco/figma-import/import/converters/group.d.ts +2 -4
  47. package/dist/taco/figma-import/import/converters/group.js +6 -15
  48. package/dist/taco/figma-import/import/converters/image.d.ts +1 -1
  49. package/dist/taco/figma-import/import/converters/image.js +22 -11
  50. package/dist/taco/figma-import/import/converters/layout.d.ts +3 -4
  51. package/dist/taco/figma-import/import/converters/layout.js +40 -21
  52. package/dist/taco/figma-import/import/converters/rectangle.d.ts +1 -1
  53. package/dist/taco/figma-import/import/converters/rectangle.js +1 -1
  54. package/dist/taco/figma-import/import/converters/section.d.ts +2 -3
  55. package/dist/taco/figma-import/import/converters/section.js +3 -9
  56. package/dist/taco/figma-import/import/converters/shared.d.ts +7 -4
  57. package/dist/taco/figma-import/import/converters/shared.js +15 -5
  58. package/dist/taco/figma-import/import/converters/text.d.ts +14 -1
  59. package/dist/taco/figma-import/import/converters/text.js +143 -12
  60. package/dist/taco/figma-import/import/converters/vector.d.ts +36 -15
  61. package/dist/taco/figma-import/import/converters/vector.js +214 -73
  62. package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +19 -3
  63. package/dist/taco/figma-import/import/converters/vectorNetwork.js +147 -29
  64. package/dist/taco/figma-import/import/instances.d.ts +25 -4
  65. package/dist/taco/figma-import/import/instances.js +324 -117
  66. package/dist/taco/figma-import/import/styles.d.ts +2 -0
  67. package/dist/taco/figma-import/import/styles.js +6 -0
  68. package/dist/taco/figma-import/import/variables.d.ts +3 -2
  69. package/dist/taco/figma-import/import/variables.js +37 -30
  70. package/dist/taco/figma-import/import/variantTrees.d.ts +1 -1
  71. package/dist/taco/figma-import/import/variantTrees.js +44 -4
  72. package/dist/taco/figma-import/import/walk.d.ts +53 -5
  73. package/dist/taco/figma-import/import/walk.js +273 -106
  74. package/dist/taco/figma-import/kiwi/message.d.ts +4 -2
  75. package/dist/taco/figma-import/kiwi/message.js +13 -11
  76. package/dist/taco/model/codec.d.ts +1 -1
  77. package/dist/taco/model/codec.js +37 -7
  78. package/dist/taco/model/component-evaluation.d.ts +2 -0
  79. package/dist/taco/model/component-evaluation.js +7 -0
  80. package/dist/taco/model/instance-resolver.d.ts +6 -2
  81. package/dist/taco/model/instance-resolver.js +48 -18
  82. package/dist/taco/model/sha256.js +13 -8
  83. package/dist/taco/model/types.d.ts +23 -1
  84. package/package.json +3 -2
@@ -2,8 +2,7 @@ import type { FigmaNodeConverter } from "../walk.ts";
2
2
  /**
3
3
  * A Figma SECTION as a Taco frame with free layout. Sections never clip
4
4
  * their content in Figma, so `clipContent` is false and `frameMaskDisabled`
5
- * is ignored. Fields only a section has are
6
- * reported: `sectionContentsHidden` (the contents are collapsed in Figma)
7
- * and `devStatus` (the "ready for dev" mark), unless it says `NONE`.
5
+ * is ignored. `sectionContentsHidden` (collapsed contents) is reported.
6
+ * Moondraft imports section status from the original Figma nodes.
8
7
  */
9
8
  export declare const convertSection: FigmaNodeConverter;
@@ -1,23 +1,17 @@
1
- import { field, isObject } from "../fields.js";
1
+ import { field } from "../fields.js";
2
2
  import { drawnAsPlainRectangle, nodeEffects } from "./effects.js";
3
3
  import { framedBase, visualStyle } from "./shared.js";
4
4
  /**
5
5
  * A Figma SECTION as a Taco frame with free layout. Sections never clip
6
6
  * their content in Figma, so `clipContent` is false and `frameMaskDisabled`
7
- * is ignored. Fields only a section has are
8
- * reported: `sectionContentsHidden` (the contents are collapsed in Figma)
9
- * and `devStatus` (the "ready for dev" mark), unless it says `NONE`.
7
+ * is ignored. `sectionContentsHidden` (collapsed contents) is reported.
8
+ * Moondraft imports section status from the original Figma nodes.
10
9
  */
11
10
  export const convertSection = (node, walk) => {
12
11
  const where = `node ${node.id}`;
13
12
  if (field(node.change, "sectionContentsHidden", "boolean", where) === true) {
14
13
  walk.report("property-left-out", "sectionContentsHidden is not imported; the contents are shown");
15
14
  }
16
- const devStatus = node.change["devStatus"];
17
- const devType = isObject(devStatus) ? field(devStatus, "type", "string", `${where} devStatus`) : undefined;
18
- if (devStatus !== undefined && devType !== "NONE") {
19
- walk.report("property-left-out", `devStatus${devType === undefined ? "" : ` ${devType}`} is not imported`);
20
- }
21
15
  const base = framedBase(node, walk, "Section", true);
22
16
  if (base === null)
23
17
  return null;
@@ -1,6 +1,7 @@
1
1
  import { type BlendMode, type FramedNodeBase, type NodeBase, type Paint, type DashCap, type ResizeConstraints, type Stroke, type VisualStyle } from "../../../model/index.ts";
2
+ import type { StrokeCap } from "../../../model/types.ts";
2
3
  import { type FigmaObject } from "../fields.ts";
3
- import { type Box } from "./gradient.ts";
4
+ import { type PaintBounds } from "./gradient.ts";
4
5
  import type { FigmaTreeNode } from "../tree.ts";
5
6
  import type { NodeWalk } from "../walk.ts";
6
7
  /**
@@ -24,7 +25,7 @@ export declare function layerBase(node: FigmaTreeNode, walk: NodeWalk, defaultNa
24
25
  * `constraints`, or `undefined` when both are MIN (or absent), Taco's
25
26
  * default. A value Taco has no match for (such as FIXED_MIN) is reported and
26
27
  * read as MIN. They are taken off again where they do not apply: from a
27
- * group by `convertGroup`, from a child in an auto layout flow by
28
+ * group (carried in its metadata) by `convertGroup`, from a child in an auto layout flow by
28
29
  * `stackChildFields`, and from a layer in a section (directly or through
29
30
  * groups) by the walk, as Figma applies none when a section is resized.
30
31
  */
@@ -52,9 +53,11 @@ export declare function framedBase(node: FigmaTreeNode, walk: NodeWalk, defaultN
52
53
  * is the box the renderer places it on instead when that differs, such as a
53
54
  * vector region's bounds.
54
55
  */
55
- export declare function nodePaints(node: Pick<FigmaTreeNode, "id" | "change">, walk: NodeWalk, list: "fillPaints" | "strokePaints", box?: Box): Paint[];
56
+ export declare function nodePaints(node: Pick<FigmaTreeNode, "id" | "change">, walk: NodeWalk, list: "fillPaints" | "strokePaints", box?: PaintBounds, source?: readonly unknown[]): Paint[];
56
57
  /** Figma's `strokeCap` names Taco has, as path ends and dash ends alike. */
57
58
  export declare const STROKE_CAPS: Readonly<Record<string, DashCap>>;
59
+ /** Figma's vector endpoint styles, including heads that dashes cannot use. */
60
+ export declare function endCapOf(value: unknown): StrokeCap | undefined;
58
61
  /**
59
62
  * Stroke paints with the node's weight, alignment, join, miter limit and
60
63
  * dashes, gradients placed on `box` (the node's box when left out). Kiwi
@@ -63,6 +66,6 @@ export declare const STROKE_CAPS: Readonly<Record<string, DashCap>>;
63
66
  * `sides` reads Figma's separate side weights, which only rectangles and
64
67
  * frames have. A dashed stroke's dash ends follow `strokeCap`, as in Figma.
65
68
  */
66
- export declare function nodeStrokes(node: FigmaTreeNode, walk: NodeWalk, box?: Box, sides?: boolean): Stroke[];
69
+ export declare function nodeStrokes(node: FigmaTreeNode, walk: NodeWalk, box?: PaintBounds, sides?: boolean): Stroke[];
67
70
  /** Solid, gradient and image fills and strokes and corners as a Taco style; other paint types are reported and left out. */
68
71
  export declare function visualStyle(node: FigmaTreeNode, walk: NodeWalk): VisualStyle;
@@ -5,6 +5,7 @@ import { moveMark } from "../carry.js";
5
5
  import { gradientShape, onBox } from "./gradient.js";
6
6
  import { styleBackedList } from "../styles.js";
7
7
  import { sizeLimitsOf } from "./layout.js";
8
+ import { exportSettingsOf } from "./exportSettings.js";
8
9
  /**
9
10
  * A Figma blend mode enum name as a Taco blend mode: `undefined` for
10
11
  * `NORMAL` and `PASS_THROUGH` (Taco's default), `null` when Taco has no such
@@ -62,6 +63,7 @@ export function layerBase(node, walk, defaultName, effectsImported = false) {
62
63
  // Figma sets `targetAspectRatio` (width and height) when a layer's aspect ratio is locked; auto layout then keeps
63
64
  // that ratio, whatever the layer's stored size.
64
65
  const aspectRatio = aspectRatioOf(node.change, where);
66
+ const exportSettings = exportSettingsOf(node.change["exportSettings"], walk.report);
65
67
  return {
66
68
  id: walk.newId(),
67
69
  name: node.name === "" ? defaultName : node.name,
@@ -72,6 +74,7 @@ export function layerBase(node, walk, defaultName, effectsImported = false) {
72
74
  ...(mask ? { mask } : {}),
73
75
  ...(constraints ? { constraints } : {}),
74
76
  ...(aspectRatio === undefined ? {} : { aspectRatioLocked: true, aspectRatio }),
77
+ ...(exportSettings === undefined ? {} : { exportSettings }),
75
78
  ...(outline ? { metadata: { figma: { maskType: "vector" } } } : {}),
76
79
  };
77
80
  }
@@ -92,7 +95,7 @@ const VERTICAL = { MIN: "top", MAX: "bottom", STRETCH: "top-bottom", CENTER: "ce
92
95
  * `constraints`, or `undefined` when both are MIN (or absent), Taco's
93
96
  * default. A value Taco has no match for (such as FIXED_MIN) is reported and
94
97
  * read as MIN. They are taken off again where they do not apply: from a
95
- * group by `convertGroup`, from a child in an auto layout flow by
98
+ * group (carried in its metadata) by `convertGroup`, from a child in an auto layout flow by
96
99
  * `stackChildFields`, and from a layer in a section (directly or through
97
100
  * groups) by the walk, as Figma applies none when a section is resized.
98
101
  */
@@ -207,12 +210,11 @@ function gradientPaint(node, walk, paint, type, at, box) {
207
210
  * is the box the renderer places it on instead when that differs, such as a
208
211
  * vector region's bounds.
209
212
  */
210
- export function nodePaints(node, walk, list, box) {
213
+ export function nodePaints(node, walk, list, box, source = styleBackedList(node.change, list, walk.styleList, `node ${node.id}`, walk.report) ?? []) {
211
214
  const where = `node ${node.id}`;
212
215
  const what = list === "fillPaints" ? "fill" : "stroke";
213
216
  const paints = [];
214
217
  const indices = walk.paintIndices ? [] : undefined;
215
- const source = styleBackedList(node.change, list, walk.styleList, where, walk.report) ?? [];
216
218
  source.forEach((paint, index) => {
217
219
  const at = `${where} ${list}[${String(index)}]`;
218
220
  const label = `${what} ${String(index + 1)}`;
@@ -221,7 +223,7 @@ export function nodePaints(node, walk, list, box) {
221
223
  const type = field(paint, "type", "string", at);
222
224
  let base;
223
225
  if (type?.startsWith("GRADIENT_")) {
224
- const gradient = gradientPaint(node, walk, paint, type, at, box);
226
+ const gradient = gradientPaint(node, walk, paint, type, at, typeof box === "function" ? box() : box);
225
227
  if (typeof gradient === "string")
226
228
  return walk.report("property-left-out", `${label} (${type}) is left out: ${gradient}`);
227
229
  if (gradient.approximated)
@@ -229,7 +231,7 @@ export function nodePaints(node, walk, list, box) {
229
231
  base = gradient.paint;
230
232
  }
231
233
  else if (type === "IMAGE") {
232
- const image = imagePaint(paint, at, label, walk);
234
+ const image = imagePaint(paint, at, label, walk, list === "fillPaints");
233
235
  if (image === null)
234
236
  return;
235
237
  if (typeof image === "string")
@@ -277,6 +279,14 @@ const STROKE_JOINS = { MITER: "miter", BEVEL: "bevel", ROUND: "round" };
277
279
  const MAX_DASH_LENGTHS = 64;
278
280
  /** Figma's `strokeCap` names Taco has, as path ends and dash ends alike. */
279
281
  export const STROKE_CAPS = { NONE: "none", ROUND: "round", SQUARE: "square" };
282
+ const END_CAPS = {
283
+ ...STROKE_CAPS, ARROW_LINES: "line-arrow", ARROW_EQUILATERAL: "triangle-arrow",
284
+ TRIANGLE_FILLED: "reversed-triangle", CIRCLE_FILLED: "circle-arrow", DIAMOND_FILLED: "diamond-arrow",
285
+ };
286
+ /** Figma's vector endpoint styles, including heads that dashes cannot use. */
287
+ export function endCapOf(value) {
288
+ return typeof value === "string" && Object.hasOwn(END_CAPS, value) ? END_CAPS[value] : undefined;
289
+ }
280
290
  /**
281
291
  * Figma's separate side weights as Taco side widths, or `undefined` when the
282
292
  * node uses one weight. With `borderStrokeWeightsIndependent` Kiwi leaves out
@@ -1,4 +1,4 @@
1
- import type { TextStyle } from "../../../model/index.ts";
1
+ import type { TextRun, TextStyle } from "../../../model/index.ts";
2
2
  import { type FigmaObject } from "../fields.ts";
3
3
  import type { FigmaNodeConverter, NodeWalk } from "../walk.ts";
4
4
  /** The weight a Figma font style name such as "Semi Bold Italic" names (undefined when it names no known weight), and whether it is italic. */
@@ -30,6 +30,14 @@ export declare const KEPT_LINE_HEIGHT_FIELD = "tacoKeptLineHeight";
30
30
  * font size, read from the same place as the line height itself (`runBinding`).
31
31
  */
32
32
  export declare const convertText: FigmaNodeConverter;
33
+ /** Figma stores the deviations from its feature defaults in separate on and off lists. */
34
+ export declare const OPEN_TYPE_TOGGLES: readonly ["toggledOnOTFeatures", "toggledOffOTFeatures"];
35
+ /** Figma fields that specify OpenType features on a layer, range or instance override. */
36
+ export declare const OPEN_TYPE_FIELDS: string[];
37
+ /** Keeps omitted toggle lists inherited, except for tags explicitly toggled the other way. */
38
+ export declare function inheritOpenTypeToggles(base: FigmaObject | undefined, own: FigmaObject): FigmaObject;
39
+ /** An empty range hyperlink explicitly clears the layer's hyperlink. */
40
+ export declare function hyperlinkOf(value: unknown, report: NodeWalk["report"]): TextRun["hyperlink"];
33
41
  /**
34
42
  * A Figma text style node (`styleType` TEXT) as a Taco text style, read as a
35
43
  * run of a node without overrides is; a value it lacks is Figma's default.
@@ -39,3 +47,8 @@ export declare const convertText: FigmaNodeConverter;
39
47
  * layers that use the style report it.
40
48
  */
41
49
  export declare function figmaTextStyle(change: FigmaObject, name: string, report: NodeWalk["report"], where: string, layers?: readonly FigmaObject[], variableNumber?: NodeWalk["variableNumber"]): TextStyle;
50
+ /** The top visible solid fill as a colour with the paint's opacity in its alpha; other fills are reported. */
51
+ export declare function textColor(paints: readonly unknown[], report?: NodeWalk["report"], where?: string): {
52
+ value: string;
53
+ paint: FigmaObject | undefined;
54
+ };
@@ -1,5 +1,5 @@
1
- import { field, isObject } from "../fields.js";
2
- import { styleBackedList } from "../styles.js";
1
+ import { field, guidText, isObject } from "../fields.js";
2
+ import { isDetachedStyle, styleBackedList } from "../styles.js";
3
3
  import { framedBase, hexColor, readNumbers } from "./shared.js";
4
4
  const ALIGN = { LEFT: "left", CENTER: "center", RIGHT: "right", JUSTIFIED: "justify" };
5
5
  const VERTICAL_ALIGN = { TOP: "top", CENTER: "middle", BOTTOM: "bottom" };
@@ -122,6 +122,27 @@ export const convertText = (node, walk) => {
122
122
  }
123
123
  const bothAuto = textStyle !== undefined && boundLineHeight === undefined && autoLineHeight(fields) && autoLineHeight(textStyle.change);
124
124
  style = textStyle?.shared === undefined ? { overrides: own } : linkedRun(textStyle.shared, own, bothAuto);
125
+ const rangeLink = override?.["hyperlink"];
126
+ const hyperlink = hyperlinkOf(rangeLink === undefined ? change["hyperlink"] : rangeLink, report);
127
+ if (hyperlink)
128
+ style.hyperlink = hyperlink;
129
+ const paints = fields["fillPaints"];
130
+ // A range with its own paints replaces the layer's style, even if the colours match.
131
+ const source = override && ("styleIdForFill" in override || "fillPaints" in override) ? override : change;
132
+ const reference = source["styleIdForFill"];
133
+ if (reference && walk.textFillStyle && !isDetachedStyle(reference)) {
134
+ const guid = walk.textFillStyle(reference);
135
+ if (guid)
136
+ Object.assign(style, walk.plainTextPaint?.(override) ? { filePlainFillStyle: guid } : { fileFillStyle: guid });
137
+ }
138
+ const paint = Array.isArray(paints) && lookups.colors.get(paints)?.paint;
139
+ if (paint && paint["colorVar"] !== undefined && walk.textColorVariable) {
140
+ const variable = walk.textColorVariable(paint["colorVar"]);
141
+ Object.assign(style, walk.plainTextPaint?.(override) ? { filePlainTextColor: variable } : { fileTextColor: variable });
142
+ const opacity = field(paint, "opacity", "number", where) ?? 1;
143
+ if (variable && opacity !== 1 && !walk.copiedPaintAlpha?.(paint, opacity))
144
+ Object.assign(style, { filePaintOpacity: opacity });
145
+ }
125
146
  if (walk.fontSizeVariable) {
126
147
  const data = runBinding(change, override, textStyle?.change, where, "FONT_SIZE", "fontSize");
127
148
  if (data !== undefined) {
@@ -203,6 +224,11 @@ export const convertText = (node, walk) => {
203
224
  report("approximated", `${String(missingIds)} style id${missingIds === 1 ? " is" : "s are"} not in the style override table; their characters use the node's style`);
204
225
  }
205
226
  reportLeftOut(change, textData, table, report, where, nodeTextStyle?.change);
227
+ const firstLineHeight = (paragraph) => {
228
+ const id = paragraph?.runs[0] === undefined ? undefined : runIds.get(paragraph.runs[0]);
229
+ const size = sizes.get(id ?? 0) ?? { fontSize: 12, lineHeight: AUTO_LINE_HEIGHT };
230
+ return size.fontSize * size.lineHeight;
231
+ };
206
232
  const autoResize = AUTO_RESIZE[field(change, "textAutoResize", "string", where) ?? "NONE"];
207
233
  if (autoResize === undefined)
208
234
  report("approximated", `textAutoResize ${String(change["textAutoResize"])} is imported as a fixed size`);
@@ -219,6 +245,7 @@ export const convertText = (node, walk) => {
219
245
  autoResize: autoResize ?? "none",
220
246
  wrap: autoResize === "width-and-height" ? "none" : "word",
221
247
  ...(spacing > 0 ? { paragraphSpacing: spacing } : {}),
248
+ ...truncationOf(change, base.frame.height, autoResize ?? "none", firstLineHeight(paragraphs[0]), where),
222
249
  };
223
250
  // Taco draws every run at the layer's bound font size where it keeps that binding (`bindLayerVariables`), so a line
224
251
  // height from a variable is a multiple of that size instead of the stored one, and the drawn line height is the variable's.
@@ -243,6 +270,43 @@ export const convertText = (node, walk) => {
243
270
  const MAX_REPORTS = 20;
244
271
  /** The fields `runStyle` reads. */
245
272
  const STYLE_FIELDS = ["fontName", "fontSize", "lineHeight", "letterSpacing", "textDecoration", "fontVariations", "fillPaints"];
273
+ const FEATURE_SWITCHES = {
274
+ fontVariantCommonLigatures: ["liga", "clig"],
275
+ fontVariantContextualLigatures: ["calt"],
276
+ fontVariantDiscretionaryLigatures: ["dlig"],
277
+ fontVariantHistoricalLigatures: ["hlig"],
278
+ fontVariantOrdinal: ["ordn"],
279
+ fontVariantSlashedZero: ["zero"],
280
+ };
281
+ const FEATURE_CHOICES = {
282
+ fontVariantNumericFigure: { LINING: ["lnum"], OLDSTYLE: ["onum"] },
283
+ fontVariantNumericSpacing: { PROPORTIONAL: ["pnum"], TABULAR: ["tnum"] },
284
+ fontVariantNumericFraction: { DIAGONAL: ["frac"], STACKED: ["afrc"] },
285
+ fontVariantCaps: { SMALL: ["smcp"], ALL_SMALL: ["smcp", "c2sc"], PETITE: ["pcap"], ALL_PETITE: ["pcap", "c2pc"], UNICASE: ["unic"], TITLING: ["titl"] },
286
+ fontVariantPosition: { SUB: ["subs"], SUPER: ["sups"] },
287
+ };
288
+ const FEATURE_VARIANTS = [...Object.keys(FEATURE_SWITCHES), ...Object.keys(FEATURE_CHOICES)];
289
+ /** Figma stores the deviations from its feature defaults in separate on and off lists. */
290
+ export const OPEN_TYPE_TOGGLES = ["toggledOnOTFeatures", "toggledOffOTFeatures"];
291
+ /** Figma fields that specify OpenType features on a layer, range or instance override. */
292
+ export const OPEN_TYPE_FIELDS = [...FEATURE_VARIANTS, ...OPEN_TYPE_TOGGLES];
293
+ /** Keeps omitted toggle lists inherited, except for tags explicitly toggled the other way. */
294
+ export function inheritOpenTypeToggles(base, own) {
295
+ const merged = {};
296
+ for (const [index, name] of OPEN_TYPE_TOGGLES.entries()) {
297
+ const value = own[name] ?? base?.[name];
298
+ if (value === undefined)
299
+ continue;
300
+ const opposite = own[OPEN_TYPE_TOGGLES[1 - index]];
301
+ if (own[name] === undefined && Array.isArray(value) && Array.isArray(opposite) && opposite.length > 0) {
302
+ const tags = new Set(opposite);
303
+ merged[name] = value.filter(tag => !tags.has(tag));
304
+ }
305
+ else
306
+ merged[name] = value;
307
+ }
308
+ return merged;
309
+ }
246
310
  /**
247
311
  * The fields a run reads, in this order: an override entry's own, then for
248
312
  * typography the Figma text style they name (`under`), then the node's. As in
@@ -264,7 +328,14 @@ function styleFields(change, override, under) {
264
328
  if (value !== undefined)
265
329
  style[name] = value;
266
330
  }
267
- return style;
331
+ for (const name of FEATURE_VARIANTS) {
332
+ const value = override?.[name] ?? change[name] ?? under?.[name];
333
+ if (value !== undefined)
334
+ style[name] = value;
335
+ }
336
+ // A layer can set its own features alongside a shared typography style. An explicitly empty list clears that side.
337
+ const toggles = inheritOpenTypeToggles(under, change);
338
+ return { ...style, ...(override ? inheritOpenTypeToggles(toggles, override) : toggles) };
268
339
  }
269
340
  /**
270
341
  * The variable data a run's `variableField` (`LINE_HEIGHT` or `FONT_SIZE`) is bound to, read where `styleFields` reads
@@ -294,8 +365,22 @@ function boundLineHeightOf(change, override, under, where, variableNumber) {
294
365
  const lineData = runBinding(change, override, under, where, "LINE_HEIGHT", "lineHeight");
295
366
  return lineData === undefined ? undefined : variableNumber(lineData);
296
367
  }
368
+ /** An empty range hyperlink explicitly clears the layer's hyperlink. */
369
+ export function hyperlinkOf(value, report) {
370
+ if (value === undefined || (isObject(value) && Object.keys(value).length === 0))
371
+ return undefined;
372
+ if (isObject(value)) {
373
+ if (typeof value["url"] === "string" && value["url"] !== "")
374
+ return { kind: "url", url: value["url"] };
375
+ const guid = value["guid"];
376
+ if (isObject(guid) && Number.isInteger(guid["sessionID"]) && Number.isInteger(guid["localID"]))
377
+ return { kind: "target", guid: guidText(guid, "hyperlink") };
378
+ }
379
+ report("property-left-out", "invalid text hyperlink");
380
+ return undefined;
381
+ }
297
382
  /** The fields a style has that turn off when a run leaves them out, with the value that turns each off. */
298
- const STYLE_OFF = [["italic", false], ["underline", false], ["strikethrough", false], ["letterSpacing", 0], ["fontVariations", {}]];
383
+ const STYLE_OFF = [["italic", false], ["underline", false], ["strikethrough", false], ["letterSpacing", 0], ["fontVariations", {}], ["openTypeFeatures", {}]];
299
384
  /**
300
385
  * A run linked to its shared style, with the fields of `own` that differ from
301
386
  * the style's as its overrides. With `bothAuto` (the run's line height and the
@@ -412,6 +497,9 @@ function runStyle(style, lookups, report, where, boundLineHeight) {
412
497
  out.underline = true;
413
498
  if (decoration === "STRIKETHROUGH")
414
499
  out.strikethrough = true;
500
+ const features = openTypeFeatures(style, report, where);
501
+ if (features !== undefined)
502
+ out.openTypeFeatures = features;
415
503
  const axes = field(style, "fontVariations", "list", where) ?? [];
416
504
  let variations = lookups.variations.get(axes);
417
505
  if (variations === undefined) {
@@ -433,12 +521,41 @@ function runStyle(style, lookups, report, where, boundLineHeight) {
433
521
  color = textColor(paints, report, where);
434
522
  lookups.colors.set(paints, color);
435
523
  }
436
- out.color = color;
524
+ out.color = color.value;
437
525
  return out;
438
526
  }
527
+ /** Converts Figma's typography switches and then its explicit feature toggles, which take precedence. */
528
+ function openTypeFeatures(style, report, where) {
529
+ const features = new Map();
530
+ for (const [name, tags] of Object.entries(FEATURE_SWITCHES)) {
531
+ const value = field(style, name, "boolean", where);
532
+ if (value !== undefined)
533
+ for (const tag of tags)
534
+ features.set(tag, value);
535
+ }
536
+ for (const [name, choices] of Object.entries(FEATURE_CHOICES)) {
537
+ const choice = field(style, name, "string", where);
538
+ if (choice !== undefined && Object.hasOwn(choices, choice))
539
+ for (const tag of choices[choice])
540
+ features.set(tag, true);
541
+ }
542
+ for (const name of OPEN_TYPE_TOGGLES) {
543
+ for (const tag of field(style, name, "list", where) ?? []) {
544
+ if (typeof tag === "string" && tag.length === 4 && /^[ -~]+$/.test(tag))
545
+ features.set(tag.toLowerCase(), name === "toggledOnOTFeatures");
546
+ else
547
+ report("property-left-out", "an invalid OpenType feature tag is left out");
548
+ }
549
+ }
550
+ if (features.size > 256)
551
+ report("property-left-out", "OpenType feature settings beyond the first 256 tags are left out");
552
+ if (features.size > 0)
553
+ return Object.fromEntries([...features.keys()].sort().slice(0, 256).map(tag => [tag, features.get(tag)]));
554
+ }
439
555
  /** The top visible solid fill as a colour with the paint's opacity in its alpha; other fills are reported. */
440
- function textColor(paints, report, where) {
556
+ export function textColor(paints, report = () => undefined, where = "text fill") {
441
557
  let color;
558
+ let shown;
442
559
  for (let i = paints.length - 1; i >= 0; i--) {
443
560
  const paint = paints[i];
444
561
  if (!isObject(paint) || field(paint, "visible", "boolean", where) === false)
@@ -449,13 +566,14 @@ function textColor(paints, report, where) {
449
566
  report("property-left-out", `a ${type} text fill is left out: only the top solid fill is imported`);
450
567
  continue;
451
568
  }
569
+ shown = paint;
452
570
  color = hexColor({ ...channels, a: channels.a * (field(paint, "opacity", "number", where) ?? 1) });
453
571
  const blend = field(paint, "blendMode", "string", where);
454
572
  if (blend !== undefined && blend !== "NORMAL" && blend !== "PASS_THROUGH")
455
573
  report("property-left-out", `the text fill's blend mode ${blend} is left out; its colour is kept`);
456
574
  }
457
575
  // No visible fill draws no glyphs; the text keeps its layout.
458
- return color ?? "#00000000";
576
+ return { value: color ?? "#00000000", paint: shown };
459
577
  }
460
578
  /** A spacing in pixels from the Figma text style (`style`), else the node, else 0; negative values are 0. */
461
579
  /**
@@ -515,6 +633,23 @@ function bulletAdvancesOf(change, characters) {
515
633
  }
516
634
  return advances;
517
635
  }
636
+ /**
637
+ * Figma's ending truncation as `truncate` and `maxLines`. Figma reads `maxLines` only while truncation is on. A
638
+ * fixed-size box also ends at its last whole line, so its line count is its height over the first line's height (at
639
+ * least one line), or `maxLines` where that is fewer. The first line's height is Figma's own where the message lays the
640
+ * text out, else the first run's.
641
+ */
642
+ function truncationOf(change, height, autoResize, firstLineHeight, where) {
643
+ if (field(change, "textTruncation", "string", where) !== "ENDING")
644
+ return {};
645
+ const stored = Math.floor(field(change, "maxLines", "number", where) ?? 0);
646
+ const baseline = isObject(change["derivedTextData"]) && Array.isArray(change["derivedTextData"]["baselines"]) ? change["derivedTextData"]["baselines"][0] : undefined;
647
+ const derived = isObject(baseline) && typeof baseline["lineHeight"] === "number" ? baseline["lineHeight"] : undefined;
648
+ const lineHeight = derived !== undefined && derived > 0 ? derived : firstLineHeight;
649
+ const fitting = autoResize === "none" && Number.isFinite(height / lineHeight) && lineHeight > 0 ? Math.max(1, Math.floor((height + 0.01) / lineHeight)) : undefined;
650
+ const maxLines = stored > 0 ? Math.min(stored, fitting ?? stored) : fitting;
651
+ return { ...(maxLines !== undefined ? { maxLines } : {}), truncate: "ellipsis" };
652
+ }
518
653
  /** Reports the text fields Taco does not import yet, one entry for each field. */
519
654
  function reportLeftOut(change, textData, table, report, where, textStyle) {
520
655
  // The text style's case applies where the node has none of its own.
@@ -523,12 +658,8 @@ function reportLeftOut(change, textData, table, report, where, textStyle) {
523
658
  if (textCase !== undefined)
524
659
  report("property-left-out", `text case ${textCase} is not imported; the characters are kept as typed`);
525
660
  const truncation = field(change, "textTruncation", "string", where);
526
- if (truncation !== undefined && truncation !== "DISABLED")
661
+ if (truncation !== undefined && truncation !== "DISABLED" && truncation !== "ENDING")
527
662
  report("property-left-out", `text truncation ${truncation} is not imported`);
528
- if ((field(change, "maxLines", "number", where) ?? 0) > 0)
529
- report("property-left-out", "the maximum line count is not imported");
530
- if (styles.some((style) => isObject(style["hyperlink"]) && Object.keys(style["hyperlink"]).length > 0))
531
- report("property-left-out", "links are not imported");
532
663
  }
533
664
  const LIST_KIND = { ORDERED_LIST: "ordered", UNORDERED_LIST: "unordered" };
534
665
  /**
@@ -1,8 +1,9 @@
1
- import { type DashCap, type LineNode, type PathNode, type Stroke, type TransformMatrix } from "../../../model/index.ts";
1
+ import { type LineNode, type PathNode, type Stroke, type TransformMatrix } from "../../../model/index.ts";
2
+ import type { StrokeCap } from "../../../model/types.ts";
2
3
  import { type FigmaObject } from "../fields.ts";
3
4
  import type { FigmaTreeNode } from "../tree.ts";
4
5
  import type { FigmaNodeConverter, NodeWalk } from "../walk.ts";
5
- import type { Box } from "./gradient.ts";
6
+ import type { PaintBounds } from "./gradient.ts";
6
7
  /**
7
8
  * The most paths in one node's `fillGeometry` and the most drawing commands
8
9
  * in all of them together. A node over either is left out and reported.
@@ -27,12 +28,6 @@ export declare const VECTOR_GEOMETRY_LIMITS: {
27
28
  */
28
29
  export declare const VECTOR_GEOMETRY_PROBLEMS: readonly ["no-geometry", "not-a-path", "unknown-winding-rule", "blob-missing", "blob-too-short", "unknown-command", "not-finite", "draw-before-move", "too-many-paths", "too-many-commands", "import-command-limit"];
29
30
  export type VectorGeometryProblem = (typeof VECTOR_GEOMETRY_PROBLEMS)[number];
30
- /**
31
- * The most paints and gradient stops `placedFills` may copy into a VECTOR's
32
- * regions in total. Over it the node's gradients stay on one box, so a short
33
- * paint list cannot be copied into thousands of regions.
34
- */
35
- export declare const REGION_FILL_COPY_LIMIT = 100000;
36
31
  /**
37
32
  * A path node's Figma `transform` as its Taco transform (Taco's [a, b, c, d,
38
33
  * e, f] is Figma's [m00, m10, m01, m11, m02, m12]), or `null` after reporting
@@ -44,9 +39,9 @@ export declare function pathTransform(node: FigmaTreeNode, walk: NodeWalk): Tran
44
39
  * and dashes from Figma's `strokeCap`; a cap Taco has no match for is
45
40
  * reported and imported flat.
46
41
  */
47
- export declare function pathStrokes(node: FigmaTreeNode, walk: NodeWalk, box: Box | undefined): {
42
+ export declare function pathStrokes(node: FigmaTreeNode, walk: NodeWalk, box: PaintBounds | undefined): {
48
43
  strokes: Stroke[];
49
- cap: DashCap;
44
+ cap: StrokeCap;
50
45
  };
51
46
  /**
52
47
  * A Figma VECTOR as a Taco path. The shape is the node's editable vector
@@ -62,10 +57,36 @@ export declare function pathStrokes(node: FigmaTreeNode, walk: NodeWalk, box: Bo
62
57
  */
63
58
  export declare const convertVector: FigmaNodeConverter;
64
59
  /**
65
- * A Figma BOOLEAN_OPERATION as a Taco path drawn from its `fillGeometry`, the
66
- * outline Figma stores for the result; Taco has no live boolean, so the
67
- * operands are not imported (the walk reports the flattening once). Without
68
- * a `fillGeometry` outline the node is left out, as before.
60
+ * Moondraft patch 0010: how a Figma BOOLEAN_OPERATION imports. A live boolean
61
+ * group (`operation`, Moondraft's name for it) when Moondraft's boolean group
62
+ * can stand for it: an operation it has, a transform that only moves and
63
+ * turns, a node that does not fill its auto layout parent (a group cannot),
64
+ * and shown operands that are all shapes, nested boolean operations or groups
65
+ * of those (a boolean group skips any other layer, such as text, and a mask).
66
+ * Otherwise `why` says what keeps it flattened to its outline.
67
+ */
68
+ export declare function liveBooleanOf(node: FigmaTreeNode, inFlow?: boolean): {
69
+ readonly operation: string;
70
+ } | {
71
+ readonly why: string;
72
+ };
73
+ /** Moondraft patch 0010: why a boolean operation was flattened, for the walk's report line, if it says. */
74
+ export declare function whyFlattened(node: FigmaTreeNode, inFlow?: boolean): string | undefined;
75
+ /**
76
+ * A Figma BOOLEAN_OPERATION. Moondraft patch 0010: one `liveBooleanOf` passes
77
+ * is a frame holding its operands, which the walk imports as its layers; it
78
+ * carries its operation (`metadata.figma.boolean`) for Moondraft's adapter,
79
+ * which makes it a boolean group. Its frame is the operation's own, and it
80
+ * takes the constraints its operands share, as the flattened path does. Its
81
+ * look (fills, strokes, effects) is the one the outline would have. The
82
+ * outline Figma stores for the result (`fillGeometry`) is carried too, as the
83
+ * group's first result, so it draws as the flattened path did until an edit
84
+ * recuts it; without one (Figma often stores none for a nested operation) the
85
+ * renderer cuts the result from the operands, and one in an auto layout flow
86
+ * whose operands do not fill its box carries `fixedBox`, for a frame of its
87
+ * own size around the group. Any other is a Taco path drawn
88
+ * from its `fillGeometry`, its operands not imported (the walk reports the
89
+ * flattening once, with the reason), or is left out without one, as before.
69
90
  */
70
91
  export declare const convertBooleanOperation: FigmaNodeConverter;
71
92
  /**
@@ -79,6 +100,6 @@ export declare const convertBooleanOperation: FigmaNodeConverter;
79
100
  * VECTORs and LINEs. A line's stroke is drawn centred, as Figma draws the
80
101
  * stroke of an open path.
81
102
  */
82
- export declare function lineWhereItFills(path: PathNode, change: FigmaObject): PathNode | LineNode;
103
+ export declare function lineWhereItFills(path: PathNode, change: FigmaObject, endpointCaps?: Readonly<Record<string, StrokeCap>>): PathNode | LineNode;
83
104
  /** Moondraft patch (../carry.ts): a STAR or REGULAR_POLYGON as its `fillGeometry` path (as a VECTOR), its parameters carried. */
84
105
  export declare const convertStarOrPolygon: FigmaNodeConverter;