@moondraft/import 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/SOURCE.md +115 -1
  3. package/dist/adapter/convert.d.ts +2 -0
  4. package/dist/adapter/convert.js +175 -84
  5. package/dist/adapter/figma-handoff.d.ts +24 -2
  6. package/dist/adapter/figma-handoff.js +39 -18
  7. package/dist/adapter/figma.js +2 -1
  8. package/dist/adapter/file-components.d.ts +2 -1
  9. package/dist/adapter/file-components.js +5 -2
  10. package/dist/adapter/file-resources.js +234 -25
  11. package/dist/adapter/file-typography.d.ts +6 -0
  12. package/dist/adapter/file-typography.js +44 -0
  13. package/dist/adapter/link.d.ts +7 -0
  14. package/dist/adapter/link.js +87 -24
  15. package/dist/adapter/payload.js +2 -2
  16. package/dist/adapter/variables.d.ts +1 -1
  17. package/dist/figma-file.d.ts +1 -0
  18. package/dist/figma-file.js +1 -0
  19. package/dist/figma.d.ts +1 -0
  20. package/dist/figma.js +41 -19
  21. package/dist/report.d.ts +37 -6
  22. package/dist/report.js +91 -18
  23. package/dist/taco/figma-import/figFile.js +3 -0
  24. package/dist/taco/figma-import/import/converters/effects.d.ts +3 -13
  25. package/dist/taco/figma-import/import/converters/effects.js +4 -34
  26. package/dist/taco/figma-import/import/converters/exportSettings.js +26 -0
  27. package/dist/taco/figma-import/import/converters/frame.d.ts +1 -1
  28. package/dist/taco/figma-import/import/converters/frame.js +8 -7
  29. package/dist/taco/figma-import/import/converters/grid.d.ts +12 -0
  30. package/dist/taco/figma-import/import/converters/grid.js +97 -0
  31. package/dist/taco/figma-import/import/converters/image.d.ts +1 -0
  32. package/dist/taco/figma-import/import/converters/image.js +38 -5
  33. package/dist/taco/figma-import/import/converters/layout.d.ts +5 -3
  34. package/dist/taco/figma-import/import/converters/layout.js +26 -16
  35. package/dist/taco/figma-import/import/converters/rectangle.js +2 -2
  36. package/dist/taco/figma-import/import/converters/section.js +2 -2
  37. package/dist/taco/figma-import/import/converters/shared.d.ts +2 -1
  38. package/dist/taco/figma-import/import/converters/shared.js +8 -5
  39. package/dist/taco/figma-import/import/converters/text.d.ts +24 -0
  40. package/dist/taco/figma-import/import/converters/text.js +114 -29
  41. package/dist/taco/figma-import/import/instances.d.ts +9 -3
  42. package/dist/taco/figma-import/import/instances.js +319 -43
  43. package/dist/taco/figma-import/import/types.d.ts +8 -0
  44. package/dist/taco/figma-import/import/variables.d.ts +2 -0
  45. package/dist/taco/figma-import/import/variables.js +28 -1
  46. package/dist/taco/figma-import/import/variantTrees.js +2 -0
  47. package/dist/taco/figma-import/import/walk.d.ts +9 -2
  48. package/dist/taco/figma-import/import/walk.js +69 -30
  49. package/dist/taco/figma-import/kiwi/message.js +52 -10
  50. package/dist/taco/model/codec.d.ts +1 -1
  51. package/dist/taco/model/codec.js +40 -6
  52. package/dist/taco/model/instance-resolver.js +3 -1
  53. package/dist/taco/model/types.d.ts +36 -6
  54. package/dist/taco/model/types.js +4 -0
  55. package/dist/taco/svg-import/style/computed.js +1 -0
  56. package/package.json +2 -2
@@ -1,4 +1,4 @@
1
- import { drawnAsPlainRectangle, nodeEffects } from "./effects.js";
1
+ import { nodeEffects } from "./effects.js";
2
2
  import { framedBase, visualStyle } from "./shared.js";
3
3
  /** A plain or rounded Figma rectangle as a Taco rectangle. */
4
4
  export const convertRectangle = (node, walk) => {
@@ -6,6 +6,6 @@ export const convertRectangle = (node, walk) => {
6
6
  if (base === null)
7
7
  return null;
8
8
  const visual = visualStyle(node, walk);
9
- const rectangle = { ...base, type: "shape", shape: "rectangle", style: { ...visual, ...nodeEffects(node, walk, drawnAsPlainRectangle(visual, false)) } };
9
+ const rectangle = { ...base, type: "shape", shape: "rectangle", style: { ...visual, ...nodeEffects(node, walk) } };
10
10
  return rectangle;
11
11
  };
@@ -1,5 +1,5 @@
1
1
  import { field } from "../fields.js";
2
- import { drawnAsPlainRectangle, nodeEffects } from "./effects.js";
2
+ import { 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
@@ -16,7 +16,7 @@ export const convertSection = (node, walk) => {
16
16
  if (base === null)
17
17
  return null;
18
18
  const visual = visualStyle(node, walk);
19
- const style = { ...visual, ...nodeEffects(node, walk, drawnAsPlainRectangle(visual, true)) };
19
+ const style = { ...visual, ...nodeEffects(node, walk) };
20
20
  const frame = {
21
21
  ...base,
22
22
  type: "frame",
@@ -7,7 +7,8 @@ import type { NodeWalk } from "../walk.ts";
7
7
  /**
8
8
  * A Figma blend mode enum name as a Taco blend mode: `undefined` for
9
9
  * `NORMAL` and `PASS_THROUGH` (Taco's default), `null` when Taco has no such
10
- * mode (`LINEAR_BURN`, `LINEAR_DODGE`).
10
+ * mode (`LINEAR_BURN`, `LINEAR_DODGE` on a shadow; layers and paints map
11
+ * them through `LAYER_BLEND_MODES`).
11
12
  */
12
13
  export declare function blendModeOf(value: string): BlendMode | undefined | null;
13
14
  export declare function readNumbers<K extends string>(object: FigmaObject, keys: readonly K[], where: string): Record<K, number> | null;
@@ -9,7 +9,8 @@ import { exportSettingsOf } from "./exportSettings.js";
9
9
  /**
10
10
  * A Figma blend mode enum name as a Taco blend mode: `undefined` for
11
11
  * `NORMAL` and `PASS_THROUGH` (Taco's default), `null` when Taco has no such
12
- * mode (`LINEAR_BURN`, `LINEAR_DODGE`).
12
+ * mode (`LINEAR_BURN`, `LINEAR_DODGE` on a shadow; layers and paints map
13
+ * them through `LAYER_BLEND_MODES`).
13
14
  */
14
15
  export function blendModeOf(value) {
15
16
  if (value === "NORMAL" || value === "PASS_THROUGH")
@@ -17,6 +18,8 @@ export function blendModeOf(value) {
17
18
  const name = value.toLowerCase().replace(/_/g, "-");
18
19
  return BLEND_MODES.includes(name) ? name : null;
19
20
  }
21
+ /** Figma's linear dodge and burn, which layers and paints take, as their CSS `plus-` names. */
22
+ const LAYER_BLEND_MODES = { LINEAR_DODGE: "plus-lighter", LINEAR_BURN: "plus-darker" };
20
23
  export function readNumbers(object, keys, where) {
21
24
  const out = {};
22
25
  for (const key of keys) {
@@ -42,7 +45,7 @@ export function hexColor(color) {
42
45
  export function layerBase(node, walk, defaultName, effectsImported = false) {
43
46
  const where = `node ${node.id}`;
44
47
  const blend = field(node.change, "blendMode", "string", where);
45
- const blendMode = blend === undefined ? undefined : blendModeOf(blend);
48
+ const blendMode = blend === undefined ? undefined : LAYER_BLEND_MODES[blend] ?? blendModeOf(blend);
46
49
  if (blendMode === null)
47
50
  walk.report("property-left-out", `blend mode ${String(blend)} has no Taco equivalent`);
48
51
  const mask = field(node.change, "mask", "boolean", where) === true;
@@ -230,12 +233,12 @@ export function nodePaints(node, walk, list, box, source = styleBackedList(node.
230
233
  walk.report("approximated", `${label}: ${gradient.approximated}`);
231
234
  base = gradient.paint;
232
235
  }
233
- else if (type === "IMAGE") {
236
+ else if (type === "IMAGE" || type === "VIDEO") {
234
237
  const image = imagePaint(paint, at, label, walk, list === "fillPaints");
235
238
  if (image === null)
236
239
  return;
237
240
  if (typeof image === "string")
238
- return walk.report("property-left-out", `${label} (IMAGE) is left out: ${image}`);
241
+ return walk.report("property-left-out", `${label} (${type}) is left out: ${image}`);
239
242
  base = image;
240
243
  }
241
244
  else {
@@ -256,7 +259,7 @@ export function nodePaints(node, walk, list, box, source = styleBackedList(node.
256
259
  }
257
260
  const visible = field(paint, "visible", "boolean", at) ?? true;
258
261
  const blend = field(paint, "blendMode", "string", at);
259
- const blendMode = blend === undefined ? undefined : blendModeOf(blend);
262
+ const blendMode = blend === undefined ? undefined : LAYER_BLEND_MODES[blend] ?? blendModeOf(blend);
260
263
  if (blendMode === null)
261
264
  walk.report("property-left-out", `${label} blend mode ${String(blend)} has no Taco equivalent`);
262
265
  const placed = {
@@ -30,12 +30,29 @@ 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
+ /** Native palette key and authored Figma field for each typography binding. */
34
+ export declare const TEXT_VARIABLE_FIELDS: {
35
+ readonly FONT_SIZE: readonly ["size", "fontSize"];
36
+ readonly FONT_FAMILY: readonly ["family", "fontName"];
37
+ readonly FONT_STYLE: readonly ["style", "fontName"];
38
+ readonly FONT_WEIGHT: readonly ["weight", "fontName"];
39
+ readonly LINE_HEIGHT: readonly ["lineHeight", "lineHeight"];
40
+ readonly LETTER_SPACING: readonly ["letterSpacing", "letterSpacing"];
41
+ };
33
42
  /** Figma stores the deviations from its feature defaults in separate on and off lists. */
34
43
  export declare const OPEN_TYPE_TOGGLES: readonly ["toggledOnOTFeatures", "toggledOffOTFeatures"];
35
44
  /** Figma fields that specify OpenType features on a layer, range or instance override. */
36
45
  export declare const OPEN_TYPE_FIELDS: string[];
37
46
  /** Keeps omitted toggle lists inherited, except for tags explicitly toggled the other way. */
38
47
  export declare function inheritOpenTypeToggles(base: FigmaObject | undefined, own: FigmaObject): FigmaObject;
48
+ /**
49
+ * The variable data a run's `variableField` (`LINE_HEIGHT` or `FONT_SIZE`) is bound to, read where `styleFields` reads
50
+ * the field it binds (`name`): an override entry's own binding, or none when the entry sets its own value; else the text
51
+ * style's (`under`), else the node's.
52
+ */
53
+ /** Override field naming the typography sources an instance override authors itself: its own variable map, and
54
+ * `textData: true` when its range table is its own. Set by `instances.ts`, read when the text converts. */
55
+ export declare const OWN_TYPOGRAPHY = "ownTypography";
39
56
  /** An empty range hyperlink explicitly clears the layer's hyperlink. */
40
57
  export declare function hyperlinkOf(value: unknown, report: NodeWalk["report"]): TextRun["hyperlink"];
41
58
  /**
@@ -52,3 +69,10 @@ export declare function textColor(paints: readonly unknown[], report?: NodeWalk[
52
69
  value: string;
53
70
  paint: FigmaObject | undefined;
54
71
  };
72
+ /**
73
+ * A field the importer adds to a text layer's Figma fields, never one Figma writes: the lines Figma laid this layer out
74
+ * in for an instance (its `derivedSymbolData` entry's `derivedTextData.baselines`), which replace the component's stored
75
+ * lines when the line limit a box holds is counted; an empty list where the instance changes how its lines are set but
76
+ * stores none, so the count follows the instance's own first line.
77
+ */
78
+ export declare const LAID_OUT_LINES_FIELD = "tacoLaidOutLines";
@@ -1,6 +1,7 @@
1
1
  import { field, guidText, isObject } from "../fields.js";
2
2
  import { isDetachedStyle, styleBackedList } from "../styles.js";
3
3
  import { framedBase, hexColor, readNumbers } from "./shared.js";
4
+ import { sizeLimitsOf } from "./layout.js";
4
5
  const ALIGN = { LEFT: "left", CENTER: "center", RIGHT: "right", JUSTIFIED: "justify" };
5
6
  const VERTICAL_ALIGN = { TOP: "top", CENTER: "middle", BOTTOM: "bottom" };
6
7
  const AUTO_RESIZE = { NONE: "none", HEIGHT: "height", WIDTH_AND_HEIGHT: "width-and-height" };
@@ -106,8 +107,15 @@ export const convertText = (node, walk) => {
106
107
  if (id !== 0 && override === undefined)
107
108
  missingIds += 1;
108
109
  const textStyle = textStyleOfEntry(override);
109
- const fields = styleFields(filled, entryFilled(override), textStyle?.change);
110
- const boundLineHeight = keptLineHeight ?? boundLineHeightOf(change, override, textStyle?.change, where, walk.variableNumber);
110
+ const instance = override?.["styleIdForText"] === undefined ? walk.instanceTypography : undefined;
111
+ let under = textStyle?.change;
112
+ if (under && instance) {
113
+ const local = Object.fromEntries(Object.values(TEXT_VARIABLE_FIELDS).filter(([, name]) => name in instance)
114
+ .map(([, name]) => [name, instance[name]]));
115
+ under = { ...under, ...local };
116
+ }
117
+ const fields = styleFields(filled, entryFilled(override), under);
118
+ const boundLineHeight = keptLineHeight ?? boundLineHeightOf(change, override, textStyle?.change, where, walk.typographyVariable ? undefined : walk.variableNumber);
111
119
  const said = [];
112
120
  const own = runStyle(fields, lookups, (kind, reason) => said.push([kind, reason]), where, boundLineHeight);
113
121
  sizes.set(id, { fontSize: own.fontSize ?? 12, lineHeight: own.lineHeight ?? AUTO_LINE_HEIGHT });
@@ -143,14 +151,40 @@ export const convertText = (node, walk) => {
143
151
  if (variable && opacity !== 1 && !walk.copiedPaintAlpha?.(paint, opacity))
144
152
  Object.assign(style, { filePaintOpacity: opacity });
145
153
  }
146
- if (walk.fontSizeVariable) {
147
- const data = runBinding(change, override, textStyle?.change, where, "FONT_SIZE", "fontSize");
148
- if (data !== undefined) {
149
- const variable = walk.fontSizeVariable(data);
150
- // An omitted style binding is reported on the definition, once for all its users.
151
- if (variable || !textStyle?.shared || data !== bindingIn(textStyle.change, "FONT_SIZE", where))
152
- Object.assign(style, { fileFontSize: variable });
154
+ if (walk.typographyVariable) {
155
+ const bindings = {};
156
+ // The bindings an instance override makes itself, even to the main's variable: Figma keeps them as overrides.
157
+ const authored = walk.instanceTypography?.[OWN_TYPOGRAPHY];
158
+ const ownBindings = {};
159
+ for (const [field, [key, name]] of Object.entries(TEXT_VARIABLE_FIELDS)) {
160
+ if (key === "lineHeight" && keptLineHeight !== undefined)
161
+ continue;
162
+ const data = runBinding(change, override, textStyle?.change, where, field, name, instance, true);
163
+ if (data === undefined)
164
+ continue;
165
+ const variable = walk.typographyVariable(field, data);
166
+ if (variable && key === "lineHeight") {
167
+ const pixels = walk.variableNumber?.(data);
168
+ const fontSize = own.fontSize ?? 12;
169
+ if (pixels !== undefined) {
170
+ sizes.set(id, { fontSize, lineHeight: pixels / fontSize });
171
+ // The stored frame was measured with this leading in the source layer's mode.
172
+ Object.assign(style, { fileLineHeightPx: pixels });
173
+ }
174
+ }
175
+ if (variable && key === "letterSpacing" && isObject(fields["letterSpacing"]) && fields["letterSpacing"]["units"] === "PERCENT")
176
+ Object.assign(style, { filePercentTracking: true });
177
+ // A failed style alias is reported once on its definition.
178
+ if (variable || !textStyle?.shared || data !== bindingIn(textStyle.change, field, where))
179
+ bindings[key] = variable;
180
+ if (variable && isObject(authored) && (bindingIn(authored, field, where) !== undefined
181
+ || authored["textData"] === true && override !== undefined && bindingIn(override, field, where) !== undefined))
182
+ ownBindings[key] = variable;
153
183
  }
184
+ if (Object.keys(bindings).length)
185
+ Object.assign(style, { fileTypography: bindings });
186
+ if (Object.keys(ownBindings).length)
187
+ Object.assign(style, { fileOwnTypography: ownBindings });
154
188
  }
155
189
  // A run that keeps its style's auto line height has the style's, which may come from another layer's font metadata.
156
190
  const keepsStyleLineHeight = bothAuto && textStyle.shared !== undefined && style.overrides?.lineHeight === undefined;
@@ -223,7 +257,7 @@ export const convertText = (node, walk) => {
223
257
  if (missingIds > 0) {
224
258
  report("approximated", `${String(missingIds)} style id${missingIds === 1 ? " is" : "s are"} not in the style override table; their characters use the node's style`);
225
259
  }
226
- reportLeftOut(change, textData, table, report, where, nodeTextStyle?.change);
260
+ reportLeftOut(change, report, where);
227
261
  const firstLineHeight = (paragraph) => {
228
262
  const id = paragraph?.runs[0] === undefined ? undefined : runIds.get(paragraph.runs[0]);
229
263
  const size = sizes.get(id ?? 0) ?? { fontSize: 12, lineHeight: AUTO_LINE_HEIGHT };
@@ -268,7 +302,16 @@ export const convertText = (node, walk) => {
268
302
  };
269
303
  /** Most distinct report reasons one text node adds; the rest are counted in one closing line. */
270
304
  const MAX_REPORTS = 20;
271
- /** The fields `runStyle` reads. */
305
+ /** Native palette key and authored Figma field for each typography binding. */
306
+ export const TEXT_VARIABLE_FIELDS = {
307
+ FONT_SIZE: ["size", "fontSize"], FONT_FAMILY: ["family", "fontName"], FONT_STYLE: ["style", "fontName"],
308
+ FONT_WEIGHT: ["weight", "fontName"], LINE_HEIGHT: ["lineHeight", "lineHeight"], LETTER_SPACING: ["letterSpacing", "letterSpacing"],
309
+ };
310
+ /** Figma's text cases, as Taco's. */
311
+ const TEXT_CASES = {
312
+ UPPER: "upper", LOWER: "lower", TITLE: "title", SMALL_CAPS: "smallCaps", SMALL_CAPS_FORCED: "smallCapsForced",
313
+ };
314
+ /** The fields read by runStyle. */
272
315
  const STYLE_FIELDS = ["fontName", "fontSize", "lineHeight", "letterSpacing", "textDecoration", "fontVariations", "fillPaints"];
273
316
  const FEATURE_SWITCHES = {
274
317
  fontVariantCommonLigatures: ["liga", "clig"],
@@ -328,7 +371,8 @@ function styleFields(change, override, under) {
328
371
  if (value !== undefined)
329
372
  style[name] = value;
330
373
  }
331
- for (const name of FEATURE_VARIANTS) {
374
+ // The layer's case beats its text style's, as for the feature switches.
375
+ for (const name of [...FEATURE_VARIANTS, "textCase"]) {
332
376
  const value = override?.[name] ?? change[name] ?? under?.[name];
333
377
  if (value !== undefined)
334
378
  style[name] = value;
@@ -342,21 +386,32 @@ function styleFields(change, override, under) {
342
386
  * the field it binds (`name`): an override entry's own binding, or none when the entry sets its own value; else the text
343
387
  * style's (`under`), else the node's.
344
388
  */
345
- function runBinding(change, override, under, where, variableField, name) {
389
+ /** Override field naming the typography sources an instance override authors itself: its own variable map, and
390
+ * `textData: true` when its range table is its own. Set by `instances.ts`, read when the text converts. */
391
+ export const OWN_TYPOGRAPHY = "ownTypography";
392
+ function runBinding(change, override, under, where, variableField, name, instance, native = false) {
346
393
  const bound = (fields) => bindingIn(fields, variableField, where);
347
394
  if (override !== undefined) {
348
395
  const own = bound(override);
349
396
  if (own !== undefined || Object.hasOwn(override, name))
350
397
  return own;
351
398
  }
352
- return under !== undefined && under[name] !== undefined ? bound(under) : bound(change);
399
+ // Figma detaches a text style when a layer field is bound, so a layer binding stored beside a linked style is stale.
400
+ // Only an instance override that sets or binds the field itself replaces the style's source.
401
+ const claimed = instance !== undefined && (instance[name] !== undefined || bound(instance) !== undefined);
402
+ if (native && override?.["styleIdForText"] === undefined && (claimed || under === undefined)) {
403
+ const local = bound(change);
404
+ if (local !== undefined || claimed)
405
+ return local;
406
+ }
407
+ return under && (under[name] !== undefined || bound(under) !== undefined) ? bound(under) : bound(change);
353
408
  }
354
409
  /** The variable data Figma fields bind `variableField` to, or `undefined`. */
355
410
  function bindingIn(fields, variableField, where) {
356
411
  const map = field(fields, "variableConsumptionMap", "object", where);
357
412
  const entry = (map && field(map, "entries", "list", `${where} variableConsumptionMap`) || [])
358
413
  .find((item) => isObject(item) && item["variableField"] === variableField);
359
- return isObject(entry) ? entry["variableData"] : undefined;
414
+ return isObject(entry) ? entry["variableData"] ?? null : undefined;
360
415
  }
361
416
  /** A run's line height in pixels from the variable it is bound to (`runBinding`), or `undefined` without one. */
362
417
  function boundLineHeightOf(change, override, under, where, variableNumber) {
@@ -380,7 +435,7 @@ export function hyperlinkOf(value, report) {
380
435
  return undefined;
381
436
  }
382
437
  /** The fields a style has that turn off when a run leaves them out, with the value that turns each off. */
383
- const STYLE_OFF = [["italic", false], ["underline", false], ["strikethrough", false], ["letterSpacing", 0], ["fontVariations", {}], ["openTypeFeatures", {}]];
438
+ const STYLE_OFF = [["italic", false], ["underline", false], ["strikethrough", false], ["letterSpacing", 0], ["fontVariations", {}], ["openTypeFeatures", {}], ["textCase", "original"]];
384
439
  /**
385
440
  * A run linked to its shared style, with the fields of `own` that differ from
386
441
  * the style's as its overrides. With `bothAuto` (the run's line height and the
@@ -500,6 +555,13 @@ function runStyle(style, lookups, report, where, boundLineHeight) {
500
555
  const features = openTypeFeatures(style, report, where);
501
556
  if (features !== undefined)
502
557
  out.openTypeFeatures = features;
558
+ const textCase = field(style, "textCase", "string", where);
559
+ if (textCase !== undefined && textCase !== "ORIGINAL") {
560
+ if (Object.hasOwn(TEXT_CASES, textCase))
561
+ out.textCase = TEXT_CASES[textCase];
562
+ else
563
+ report("property-left-out", `text case ${textCase} is not imported; the characters are kept as typed`);
564
+ }
503
565
  const axes = field(style, "fontVariations", "list", where) ?? [];
504
566
  let variations = lookups.variations.get(axes);
505
567
  if (variations === undefined) {
@@ -633,30 +695,53 @@ function bulletAdvancesOf(change, characters) {
633
695
  }
634
696
  return advances;
635
697
  }
698
+ /**
699
+ * A field the importer adds to a text layer's Figma fields, never one Figma writes: the lines Figma laid this layer out
700
+ * in for an instance (its `derivedSymbolData` entry's `derivedTextData.baselines`), which replace the component's stored
701
+ * lines when the line limit a box holds is counted; an empty list where the instance changes how its lines are set but
702
+ * stores none, so the count follows the instance's own first line.
703
+ */
704
+ export const LAID_OUT_LINES_FIELD = "tacoLaidOutLines";
636
705
  /**
637
706
  * 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.
707
+ * fixed-size box ends at its last whole line, and so does auto-height text at its maximum height, so the line count is
708
+ * the lines that fit that height (at least one line), or `maxLines` where that is fewer.
641
709
  */
642
710
  function truncationOf(change, height, autoResize, firstLineHeight, where) {
643
711
  if (field(change, "textTruncation", "string", where) !== "ENDING")
644
712
  return {};
645
713
  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;
714
+ const limit = autoResize === "none" ? height : autoResize === "height" ? sizeLimitsOf(change).maxHeight : undefined;
715
+ const fitting = limit === undefined ? undefined : linesWithin(change, limit, firstLineHeight);
650
716
  const maxLines = stored > 0 ? Math.min(stored, fitting ?? stored) : fitting;
651
717
  return { ...(maxLines !== undefined ? { maxLines } : {}), truncate: "ellipsis" };
652
718
  }
719
+ /**
720
+ * How many lines fit `height`, at least one: Figma's laid-out lines (each at its own top and height, so uneven line
721
+ * heights and paragraph spacing count) that end within it, then lines of the last one's height in the space below
722
+ * them. Without laid-out lines, every line is `firstLineHeight` tall.
723
+ */
724
+ function linesWithin(change, height, firstLineHeight) {
725
+ const own = change[LAID_OUT_LINES_FIELD];
726
+ const derived = isObject(change["derivedTextData"]) ? change["derivedTextData"]["baselines"] : undefined;
727
+ const lines = Array.isArray(own) ? own : Array.isArray(derived) ? derived : [];
728
+ let count = 0, bottom = 0, last = firstLineHeight;
729
+ for (const line of lines) {
730
+ const lineHeight = isObject(line) ? line["lineHeight"] : undefined;
731
+ if (typeof lineHeight !== "number" || !(lineHeight > 0))
732
+ break;
733
+ const top = isObject(line) && typeof line["lineY"] === "number" ? line["lineY"] : bottom;
734
+ if (top + lineHeight > height + 0.01)
735
+ return Math.max(1, count);
736
+ count += 1;
737
+ bottom = top + lineHeight;
738
+ last = lineHeight;
739
+ }
740
+ const more = (height - bottom + 0.01) / last;
741
+ return Number.isFinite(more) && last > 0 ? Math.max(1, count + Math.max(0, Math.floor(more))) : undefined;
742
+ }
653
743
  /** Reports the text fields Taco does not import yet, one entry for each field. */
654
- function reportLeftOut(change, textData, table, report, where, textStyle) {
655
- // The text style's case applies where the node has none of its own.
656
- const styles = [change, ...table.values(), ...(textStyle !== undefined && change["textCase"] === undefined ? [textStyle] : [])];
657
- const textCase = styles.map((style) => field(style, "textCase", "string", where)).find((value) => value !== undefined && value !== "ORIGINAL");
658
- if (textCase !== undefined)
659
- report("property-left-out", `text case ${textCase} is not imported; the characters are kept as typed`);
744
+ function reportLeftOut(change, report, where) {
660
745
  const truncation = field(change, "textTruncation", "string", where);
661
746
  if (truncation !== undefined && truncation !== "DISABLED" && truncation !== "ENDING")
662
747
  report("property-left-out", `text truncation ${truncation} is not imported`);
@@ -31,6 +31,9 @@ export interface StoredFigmaBox {
31
31
  readonly transform: TransformMatrix;
32
32
  /** Scale the component's path vertices from its Figma size to the size at this occurrence. */
33
33
  readonly pathScale?: readonly [number, number];
34
+ /** A text Figma measured at the size a nested instance gives it (an entry without a size): that size, if one is
35
+ * stored, wins over this box, the component's. Only its size is Figma's: its constraints place it. */
36
+ readonly inner?: true;
34
37
  }
35
38
  /** The component bindings the model drops from a layer the override is applied to, so the override must write each again. */
36
39
  export declare function bindingsDroppedBy(override: Readonly<Record<string, unknown>>): Set<string>;
@@ -40,7 +43,7 @@ export declare function bindingsDroppedBy(override: Readonly<Record<string, unkn
40
43
  * along a path are finished in file order.
41
44
  */
42
45
  export declare function unbindInnerBindings(overrides: Record<string, LayerOverride> | undefined, nested: ReadonlyMap<string, readonly Through[]>): void;
43
- /** The key an override path step names a layer by: its `overrideKey`, else its guid. */
46
+ /** The key an override path step names a layer by: its `overrideKey`, else (none, or the no-key marker) its guid. */
44
47
  export declare function overrideKeyOf(node: FigmaTreeNode): string;
45
48
  /** A slot an instance fills, with the host frame the walk made from the content frame's children. */
46
49
  export interface HostedSlotFill {
@@ -59,14 +62,17 @@ export interface NestedSwaps {
59
62
  }
60
63
  /** What finishing an instance needs from the walk. */
61
64
  export interface InstanceContext {
62
- /** Keeps the derived boxes for the native file adapter. */
63
- fileLayout?(boxes: Map<string, StoredFigmaBox>, fillOnly: boolean): void;
65
+ /** Keeps the derived boxes for the native file adapter: `boxes` (only fill sizes when `fillOnly`), and `texts`, the
66
+ * boxes Figma measured for self-sizing texts outside `boxes`. */
67
+ fileLayout?(boxes: Map<string, StoredFigmaBox>, fillOnly: boolean, texts: Map<string, StoredFigmaBox>): void;
64
68
  /** Captures an authored override before conversion to literal differences.
65
69
  * @param path - Resolved reader layer path.
66
70
  * @param change - Fields authored at this occurrence.
67
71
  * @returns Nothing.
68
72
  */
69
73
  fileOverride?(path: string, change: FigmaObject): void;
74
+ /** Authored fields at a resolved path inside a nested instance in a saved file. */
75
+ nestedTextFields?(instance: SceneNode, path: string): FigmaObject | undefined;
70
76
  /** The enclosing instance's swaps at this reader path ("" for its own assignments). */
71
77
  nestedSwaps(instance: SceneNode, path: string): NestedSwaps | undefined;
72
78
  /** Whether a path segment occurs as a node GUID or override key in the source. */