@pairdraw/pen 0.21.1 → 0.22.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/dist/index.d.ts CHANGED
@@ -634,6 +634,12 @@ type PenWriteWarning = {
634
634
  } | {
635
635
  kind: 'missing-image-fills';
636
636
  count: number;
637
+ } | {
638
+ kind: 'flattened-image-tiles';
639
+ count: number;
640
+ } | {
641
+ kind: 'dropped-constraints';
642
+ count: number;
637
643
  } | {
638
644
  kind: 'dropped-effects';
639
645
  count: number;
@@ -646,6 +652,15 @@ type PenWriteWarning = {
646
652
  } | {
647
653
  kind: 'dropped-font-features';
648
654
  count: number;
655
+ } | {
656
+ kind: 'dropped-layout-grids';
657
+ count: number;
658
+ } | {
659
+ kind: 'dropped-overrides';
660
+ count: number;
661
+ } | {
662
+ kind: 'flattened-swaps';
663
+ count: number;
649
664
  };
650
665
  /** The English sentence a write warning has always been reported as. */
651
666
  declare function penWriteWarningText(warning: PenWriteWarning): string;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/schema.ts","../src/assets.ts","../src/imports.ts","../src/nodes.ts","../src/library.ts","../src/plugin-data.ts","../src/read.ts","../src/write.ts"],"mappings":";;;;;;cA6KM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA4FM,aAAa,EAAE,YAAY,EAAE,oBAAoB;;UAG5C,oBAAoB;EACnC;EACA;EACA,WAAW;EACX,cAAc,eAAe;GAC5B;;UAGc,gBAAgB;EAC/B;EACA;;;cA8CI,YAAU,EAAA;;;;cAwDV,mBAAiB,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA2CX,cAAc,EAAE,mBAAmB;KACnC,UAAU,EAAE,mBAAmB;;;;iBC7Y3B,SAAS,iBAAiB;;iBAM1B,YACd,iBACA,6BACC;;UAKc;EACf;EACA,UAAU;;;;;;iBAOI,oBAAoB;;iBAMpB,UAAU;;;;;iBAWV,sBAAsB;EAAgB;EAAe;;;UASpD;;EAEf,OAAO;;;;;EAKP;;;;;EAKA;;;;;;iBAsHc,UAAU,KAAK,cAAc;;;;UCrM5B;;EAEf;;EAEA,KAAK;;;KAIK,aAAa,oBAAoB;;;;;;;iBAqH7B,iBACd,eACA,cACA,KAAK,aACL,SAAS,aACR;;;;UCtGc;;;;;;EAMf,KACE,OAAO,YACP,iBACA,cACA,cACA,OAAO,OACP,UAAU;;;;;;;;;iBC8BE,gBAAgB,KAAK,aAAa,SAAS,aAAa;;;;;;;;cCpE3D;;;;UCUI;;;;;EAKf,QAAQ,OAAO;;EAEf,SAAS,oBAAoB;;;;;EAK7B,UAAU;;EAEV,QAAQ;;;;;;EAMR,QAAQ,oBAAoB;;;;;;;EAO5B,UAAU;;EAEV,mBAAmB;;EAEnB,mBAAmB;;;iBAgDL,iBAAiB,eAAe;;iBAShC,cAAc,KAAK,aAAa,UAAS,iBAAsB;iBA8D/D,aAAa,cAAc,UAAS,iBAAsB;iBAIpD,YAAY,MAAM,MAAM,UAAU,iBAAiB,QAAQ;;;;;;;KClFrE;EACN;EAAsB;EAAe;;EACrC;;EACA;EAA6B;;EAC7B;EAA6B;;EAC7B;EAAyB;;EACzB;EAAyB;;EACzB;EAAwB;;EACxB;EAA+B;;;iBAGrB,oBAAoB,SAAS;;UAgC5B;;EAEf;;EAEA,iBAAiB;;iBA4sBH,qBACd,OAAO,YACP,UAAS,kBACR;iBAyGa,qBAAqB,UAAU;iBAI/B,iBACd,QAAQ,cAAc,YACtB,UAAS"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/schema.ts","../src/assets.ts","../src/imports.ts","../src/nodes.ts","../src/library.ts","../src/plugin-data.ts","../src/read.ts","../src/write.ts"],"mappings":";;;;;;cA6KM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA4FM,aAAa,EAAE,YAAY,EAAE,oBAAoB;;UAG5C,oBAAoB;EACnC;EACA;EACA,WAAW;EACX,cAAc,eAAe;GAC5B;;UAGc,gBAAgB;EAC/B;EACA;;;cA8CI,YAAU,EAAA;;;;cAwDV,mBAAiB,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA2CX,cAAc,EAAE,mBAAmB;KACnC,UAAU,EAAE,mBAAmB;;;;iBC7Y3B,SAAS,iBAAiB;;iBAM1B,YACd,iBACA,6BACC;;UAKc;EACf;EACA,UAAU;;;;;;iBAOI,oBAAoB;;iBAMpB,UAAU;;;;;iBAWV,sBAAsB;EAAgB;EAAe;;;UASpD;;EAEf,OAAO;;;;;EAKP;;;;;EAKA;;;;;;iBAsHc,UAAU,KAAK,cAAc;;;;UCrM5B;;EAEf;;EAEA,KAAK;;;KAIK,aAAa,oBAAoB;;;;;;;iBAqH7B,iBACd,eACA,cACA,KAAK,aACL,SAAS,aACR;;;;UCtGc;;;;;;EAMf,KACE,OAAO,YACP,iBACA,cACA,cACA,OAAO,OACP,UAAU;;;;;;;;;iBC8BE,gBAAgB,KAAK,aAAa,SAAS,aAAa;;;;;;;;cCpE3D;;;;UCUI;;;;;EAKf,QAAQ,OAAO;;EAEf,SAAS,oBAAoB;;;;;EAK7B,UAAU;;EAEV,QAAQ;;;;;;EAMR,QAAQ,oBAAoB;;;;;;;EAO5B,UAAU;;EAEV,mBAAmB;;EAEnB,mBAAmB;;;iBAgDL,iBAAiB,eAAe;;iBAShC,cAAc,KAAK,aAAa,UAAS,iBAAsB;iBA8D/D,aAAa,cAAc,UAAS,iBAAsB;iBAIpD,YAAY,MAAM,MAAM,UAAU,iBAAiB,QAAQ;;;;;;;KCxErE;EACN;EAAsB;EAAe;;EACrC;;EACA;EAA6B;;EAC7B;EAA6B;;EAC7B;EAA+B;;EAC/B;EAA6B;;EAC7B;EAAyB;;EACzB;EAAyB;;EACzB;EAAwB;;EACxB;EAA+B;;EAC/B;EAA8B;;EAC9B;EAA2B;;EAC3B;EAAyB;;;iBAGf,oBAAoB,SAAS;;UA0C5B;;EAEf;;EAEA,iBAAiB;;iBAq2BH,qBACd,OAAO,YACP,UAAS,kBACR;iBA2Ha,qBAAqB,UAAU;iBAI/B,iBACd,QAAQ,cAAc,YACtB,UAAS"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pairdraw/pen",
3
- "version": "0.21.1",
3
+ "version": "0.22.0",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist",
@@ -15,7 +15,7 @@
15
15
  "valibot": "^1.5.0"
16
16
  },
17
17
  "peerDependencies": {
18
- "@pairdraw/scene-graph": "^0.21.1"
18
+ "@pairdraw/scene-graph": "^0.22.0"
19
19
  },
20
20
  "exports": {
21
21
  ".": {
package/src/nodes.ts CHANGED
@@ -213,10 +213,10 @@ const PROPERTY_FIELDS = {
213
213
  rotation: PLACEMENT_FIELDS,
214
214
  fill: ['fills'],
215
215
  stroke: STROKE_WRITES,
216
- strokeWidth: STROKE_WRITES,
216
+ strokeWidth: [...STROKE_WRITES, 'strokeWeight'],
217
217
  strokeLinecap: ['strokes', 'strokeCap'],
218
218
  strokeLinejoin: ['strokes', 'strokeJoin'],
219
- strokeAlignment: ['strokes'],
219
+ strokeAlignment: ['strokes', 'strokeAlign'],
220
220
  effect: ['effects'],
221
221
  cornerRadius: CORNER_FIELDS,
222
222
  layout: ['layoutMode'],
@@ -265,6 +265,9 @@ const PROPERTY_FIELDS = {
265
265
  scrollY: ['pluginData']
266
266
  } satisfies Record<keyof NodeFields, readonly (keyof SceneNode)[]>
267
267
 
268
+ /** Every pen field an override can name, and the scene fields it writes (see PROPERTY_FIELDS). */
269
+ export const PEN_PROPERTY_FIELDS = Object.keys(PROPERTY_FIELDS) as (keyof typeof PROPERTY_FIELDS)[]
270
+
268
271
  const isProperty = (key: string): key is keyof typeof PROPERTY_FIELDS => key in PROPERTY_FIELDS
269
272
 
270
273
  /**
package/src/write.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { isEqual } from 'es-toolkit'
2
1
  import { fromUint8Array } from 'js-base64'
3
2
 
3
+ import { SceneGraph as SceneGraphValue } from '@pairdraw/scene-graph'
4
4
  import type {
5
5
  BlendMode,
6
6
  EnabledLibraryBinding,
@@ -16,7 +16,6 @@ import type {
16
16
  VariableCollection,
17
17
  VariableValue
18
18
  } from '@pairdraw/scene-graph'
19
- import { SceneGraph as SceneGraphValue } from '@pairdraw/scene-graph'
20
19
  import { colorToHex, colorToHex8 } from '@pairdraw/scene-graph/color'
21
20
  import { getNodeLocalMatrix } from '@pairdraw/scene-graph/coordinate'
22
21
  import { readDesignMemory } from '@pairdraw/scene-graph/design-memory'
@@ -32,6 +31,7 @@ import { readPenImports, readSourceLibraryPublication } from './document-librari
32
31
  import { networkToSVGPath } from './geometry'
33
32
  import { penCropTransform } from './images'
34
33
  import { importedComponentIds } from './library'
34
+ import { PEN_PROPERTY_FIELDS, fieldsSetBy } from './nodes'
35
35
  import { PEN_PLUGIN_DATA_NAMESPACE, readIconOrigin } from './plugin-data'
36
36
  import type {
37
37
  PenDocument,
@@ -80,10 +80,20 @@ type PenWriteState = {
80
80
  details: PenWriteWarning[]
81
81
  drops: {
82
82
  images: number
83
+ /** Image fills whose TILE mode the file cannot hold; they are written as filled images. */
84
+ tileImages: number
85
+ /** Layers whose resize constraints the file cannot hold. */
86
+ constraints: number
83
87
  effects: number
84
88
  filters: number
85
89
  scroll: number
86
90
  fontFeatures: number
91
+ /** Frames whose layout grids the file could not hold. */
92
+ layoutGrids: number
93
+ /** Overrides inside a nested instance the file cannot name; the instance keeps its link. */
94
+ overrides: number
95
+ /** Instance swaps the file cannot name, written as the layers they show. */
96
+ swaps: number
87
97
  }
88
98
  }
89
99
 
@@ -96,10 +106,15 @@ export type PenWriteWarning =
96
106
  | { kind: 'display-p3-colors' }
97
107
  | { kind: 'library-definitions'; count: number }
98
108
  | { kind: 'missing-image-fills'; count: number }
109
+ | { kind: 'flattened-image-tiles'; count: number }
110
+ | { kind: 'dropped-constraints'; count: number }
99
111
  | { kind: 'dropped-effects'; count: number }
100
112
  | { kind: 'dropped-filters'; count: number }
101
113
  | { kind: 'dropped-scroll'; count: number }
102
114
  | { kind: 'dropped-font-features'; count: number }
115
+ | { kind: 'dropped-layout-grids'; count: number }
116
+ | { kind: 'dropped-overrides'; count: number }
117
+ | { kind: 'flattened-swaps'; count: number }
103
118
 
104
119
  /** The English sentence a write warning has always been reported as. */
105
120
  export function penWriteWarningText(warning: PenWriteWarning): string {
@@ -112,6 +127,10 @@ export function penWriteWarningText(warning: PenWriteWarning): string {
112
127
  return `Library components came from ${warning.count} enabled ${warning.count === 1 ? 'library' : 'libraries'}, whose definitions Pencil.dev files do not hold; re-enable those libraries after reopening to restore them.`
113
128
  case 'missing-image-fills':
114
129
  return `Image fills with missing image data were left out (${warning.count}); Pencil.dev files cannot hold a paint without its image.`
130
+ case 'flattened-image-tiles':
131
+ return `Tiled image fills were written as filled images (${warning.count}); Pencil.dev files cannot hold tiling.`
132
+ case 'dropped-constraints':
133
+ return `Resize constraints were left out of ${warning.count} ${warning.count === 1 ? 'layer' : 'layers'}; Pencil.dev files do not hold them.`
115
134
  case 'dropped-effects':
116
135
  return `Noise and texture effects were left out (${warning.count}); Pencil.dev files have no form for them.`
117
136
  case 'dropped-filters':
@@ -120,6 +139,12 @@ export function penWriteWarningText(warning: PenWriteWarning): string {
120
139
  return `Scroll overflow was left out (${warning.count}); Pencil.dev files have no form for it.`
121
140
  case 'dropped-font-features':
122
141
  return `OpenType font features were left out (${warning.count}); Pencil.dev files have no form for them.`
142
+ case 'dropped-layout-grids':
143
+ return `Layout grids were left out (${warning.count} ${warning.count === 1 ? 'frame' : 'frames'}); Pencil.dev files have no form for them.`
144
+ case 'dropped-overrides':
145
+ return `Instance overrides a Pencil.dev file cannot name were left out (${warning.count}); the instance still links to its component.`
146
+ case 'flattened-swaps':
147
+ return `Instance swaps a Pencil.dev file cannot name were written as the layers they show (${warning.count}); they reopen detached from their components.`
123
148
  default: {
124
149
  const unhandled: never = warning
125
150
  throw new Error(`Unhandled pen write warning: ${String(unhandled)}`)
@@ -272,6 +297,7 @@ function imagePaint(state: PenWriteState, fill: Fill): PenPaint | null {
272
297
  ...paintBase(fill),
273
298
  url: `data:${imageMimeType(bytes)};base64,${fromUint8Array(bytes)}`
274
299
  }
300
+ if (fill.imageScaleMode === 'TILE') state.drops.tileImages++
275
301
  if (fill.imageScaleMode === 'FIT') paint.mode = 'contain'
276
302
  if (fill.imageScaleMode === 'CROP') {
277
303
  paint.mode = 'stretch'
@@ -413,7 +439,6 @@ function layoutFields(state: PenWriteState, node: SceneNode): Partial<PenNode> {
413
439
  variableOr(state, node, 'paddingLeft', node.paddingLeft)
414
440
  ]
415
441
  if (padding.some((value) => value !== 0)) fields.padding = padding
416
- if (node.clipsContent) fields.clip = true
417
442
  if (node.strokesIncludedInLayout) fields.layoutIncludeStroke = true
418
443
  return fields
419
444
  }
@@ -629,14 +654,38 @@ function basicNodeFields(state: PenWriteState, node: SceneNode, result: PenNode)
629
654
  if (theme) result.theme = theme
630
655
  const fill = paintsOf(state, node, 'fills', node.fills)
631
656
  if (fill !== undefined) result.fill = fill
657
+ // Clip is a fact about the node, not about auto layout: a frame that does not lay out its
658
+ // children still clips them, and writes it outside layoutFields.
659
+ if (node.clipsContent) result.clip = true
632
660
  Object.assign(
633
661
  result,
634
662
  strokesOf(state, node),
635
663
  layoutFields(state, node),
636
664
  cornerFields(state, node),
637
- textFields(state, node)
665
+ textFields(state, node),
666
+ shapeFields(node)
638
667
  )
639
668
  }
669
+
670
+ /**
671
+ * The shape fields pen stores outside the paint and layout groups: an ellipse's arc, which the
672
+ * reader rebuilds on reopen, and a polygon's point count, whose pen default (3) the reader uses
673
+ * when the field is absent.
674
+ */
675
+ function shapeFields(node: SceneNode): Partial<PenNode> {
676
+ if (node.type === 'ELLIPSE' && node.arcData) {
677
+ const { startingAngle, endingAngle, innerRadius } = node.arcData
678
+ return {
679
+ innerRadius: rounded(innerRadius),
680
+ startAngle: rounded((-startingAngle * 180) / Math.PI),
681
+ sweepAngle: rounded(-((endingAngle - startingAngle) * 180) / Math.PI)
682
+ }
683
+ }
684
+ if ((node.type === 'POLYGON' || node.type === 'STAR') && node.pointCount !== 3) {
685
+ return { polygonCount: node.pointCount }
686
+ }
687
+ return {}
688
+ }
640
689
  /**
641
690
  * A layer's variable mode assignments, by the names pen writes: the collection's name and the
642
691
  * mode's. A mode the document no longer holds is left out.
@@ -652,50 +701,46 @@ function themeFields(state: PenWriteState, node: SceneNode): Record<string, stri
652
701
  }
653
702
 
654
703
  /**
655
- * The pen fields an instance override writes: what it changes on a layer, not the placement and
656
- * layout a reopen recalculates from the component and the boxes around it.
704
+ * Pen override fields the writer must never store, because a reopen recalculates them from the
705
+ * component and the boxes around it (X), or they are shape facts of a component, not a layer (Y).
706
+ * Every other field PEN_PROPERTY_FIELDS names is writable, so the allow list follows the property
707
+ * map instead of drifting beside it; a field missing here fails a test rather than disappearing.
708
+ * Some names are writable in principle but never written today because their property map names no
709
+ * recordable scene field: the icon identity fields (library, icon, weight, iconFontName,
710
+ * iconFontFamily), text decoration (underline, strikethrough), and the plugin-only names whose map
711
+ * is just `pluginData`. If INSTANCE_OVERRIDE_FIELDS ever gains one of those targets, add the name
712
+ * here first.
657
713
  */
658
- const OVERRIDABLE_FIELDS: ReadonlySet<string> = new Set([
659
- 'content',
660
- 'fill',
661
- 'stroke',
662
- 'strokeWidth',
663
- 'cornerRadius',
664
- 'effect',
665
- 'opacity',
666
- 'enabled',
667
- 'fontFamily',
668
- 'fontSize',
669
- 'fontWeight',
670
- 'fontStyle',
671
- 'textAlign',
672
- 'textAlignVertical',
673
- 'lineHeight',
674
- 'letterSpacing',
675
- 'href'
714
+ export const PEN_DENIED_OVERRIDE_FIELDS: ReadonlySet<string> = new Set([
715
+ 'x',
716
+ 'y',
717
+ 'rotation',
718
+ 'flipX',
719
+ 'flipY',
720
+ 'layoutPosition',
721
+ 'layout',
722
+ 'gap',
723
+ 'padding',
724
+ 'justifyContent',
725
+ 'alignItems',
726
+ 'layoutIncludeStroke',
727
+ 'viewBox',
728
+ 'geometry',
729
+ 'fillRule',
730
+ 'reusable',
731
+ 'slot',
732
+ 'placeholder',
733
+ 'ref',
734
+ 'innerRadius',
735
+ 'startAngle',
736
+ 'sweepAngle',
737
+ 'polygonCount'
676
738
  ])
677
739
 
678
- /**
679
- * The path a copy sits at in its component, from the instance down: the ids of the layers it
680
- * copies, as pen names a `descendants` override. A copy that swapped its component copies `swapOf`
681
- * instead of the component it draws.
682
- */
683
- function overridePath(
684
- graph: SceneGraph,
685
- instanceId: string,
686
- copyId: string,
687
- swapOf?: string
688
- ): string | null {
689
- const parts: string[] = []
690
- let current = graph.getNode(copyId)
691
- while (current && current.id !== instanceId) {
692
- const id = current.id === copyId && swapOf ? swapOf : current.componentId
693
- if (!id) return null
694
- parts.unshift(id)
695
- current = current.parentId ? graph.getNode(current.parentId) : undefined
696
- }
697
- return current ? parts.join('/') : null
698
- }
740
+ /** The pen fields an instance override writes: everything the property map names but the denylist. */
741
+ export const PEN_OVERRIDE_FIELDS: readonly string[] = PEN_PROPERTY_FIELDS.filter(
742
+ (field) => !PEN_DENIED_OVERRIDE_FIELDS.has(field)
743
+ )
699
744
 
700
745
  /**
701
746
  * The fields a swap override names an icon by: pen carries the glyph, not the component drawn.
@@ -710,63 +755,190 @@ function iconOverride(graph: SceneGraph, componentId: string | null): PenOverrid
710
755
  }
711
756
 
712
757
  /**
713
- * The overrides an instance writes, and whether one of them is a swap the file cannot name. A
714
- * layer that swapped its component draws another component than the one it copies, which pen holds
715
- * as a glyph for an icon; any other swap is what the instance shows, not what it links to.
758
+ * The pen fields a copy's recorded overrides name, in pen's own names. A field is written when the
759
+ * copy records at least one scene field it maps to, so a value set back to the component's own
760
+ * still belongs to the instance.
761
+ */
762
+ function overrideFieldsFor(
763
+ state: PenWriteState,
764
+ copy: SceneNode,
765
+ overrides: ReadonlyMap<string, unknown>
766
+ ): PenOverride {
767
+ const probe: PenWriteState = {
768
+ ...state,
769
+ drops: {
770
+ images: 0,
771
+ tileImages: 0,
772
+ constraints: 0,
773
+ effects: 0,
774
+ filters: 0,
775
+ scroll: 0,
776
+ fontFeatures: 0,
777
+ overrides: 0,
778
+ swaps: 0,
779
+ layoutGrids: 0
780
+ }
781
+ }
782
+ const own = writeNode(probe, copy) as PenNode & Record<string, unknown>
783
+ const override: PenOverride = {}
784
+ for (const field of PEN_OVERRIDE_FIELDS) {
785
+ if (!(field in own)) continue
786
+ // A width the layout fills or hugs is recalculated on reopen; only a fixed one is a fact.
787
+ if (field === 'width' || field === 'height') {
788
+ const axis = field === 'width' ? 'HORIZONTAL' : 'VERTICAL'
789
+ if (layoutSizing(state.graph, copy, axis) !== 'FIXED') continue
790
+ }
791
+ const value = own[field]
792
+ if (!fieldsSetBy({ [field]: value }).some((key) => overrides.has(key))) continue
793
+ override[field] = value
794
+ }
795
+ return override
796
+ }
797
+
798
+ /** Adds a field map at a path, merging with one already there instead of replacing it. */
799
+ function mergeOverride(
800
+ entries: Record<string, PenOverride>,
801
+ path: string,
802
+ fields: PenOverride
803
+ ): void {
804
+ entries[path] = { ...entries[path], ...fields }
805
+ }
806
+
807
+ /**
808
+ * The overrides an instance writes, and whether one of them is a swap the file cannot name.
809
+ *
810
+ * Three cases:
811
+ * (i) a direct override, and a plain layer deeper than one level, is written at the authored layer id
812
+ * from the component definition (a copy only holds the component it draws, so the definition is
813
+ * paired with the copy list at every plain level);
814
+ * (ii) a same-file nested instance (b1) resolves through its materialized `INSTANCE` chain to the
815
+ * authored component, and its own overrides merge as flat `parent/child` paths;
816
+ * (iii) an imported nested component resolves outside the file (b2 stores an authored-id map when
817
+ * that changes), so its override is left out and counted in `drops.overrides` while the instance
818
+ * keeps its `ref`; a swap the file cannot name as a glyph flattens the instance and is counted in
819
+ * `drops.swaps`.
820
+ * `copyPaths` pairs definitions with copies positionally; it skips nested `INSTANCE` copies because
821
+ * their paths are collected by the recursion below, not because they are unnameable.
716
822
  */
717
823
  function instanceDescendants(
718
824
  state: PenWriteState,
719
825
  node: SceneNode
720
826
  ): { descendants?: Record<string, PenOverride>; unrepresentable: boolean } {
721
- const overridden = [...node.instanceOverrides.descendants.keys()]
722
- if (overridden.length === 0) return { unrepresentable: false }
723
- // Comparing writes fields the moment a document writes them, but their drops are counted once.
724
- const probe: PenWriteState = {
725
- ...state,
726
- drops: { images: 0, effects: 0, filters: 0, scroll: 0, fontFeatures: 0 }
727
- }
728
- const descendants: Record<string, PenOverride> = {}
729
- let unrepresentable = false
730
- for (const copyId of overridden) {
731
- const overrides = node.instanceOverrides.descendants.get(copyId)
732
- const swapped = overrides?.has('componentId') === true
733
- const swapOf = swapped ? overrides.get('sourceComponentId') : undefined
734
- const path = overridePath(
735
- state.graph,
736
- node.id,
737
- copyId,
738
- typeof swapOf === 'string' ? swapOf : undefined
739
- )
740
- const copy = state.graph.getNode(copyId)
741
- if (!path || !copy) {
742
- unrepresentable = true
743
- continue
827
+ /** The authored path of every copy this instance shows, plain layers included. */
828
+ const copyPaths = (
829
+ componentId: string,
830
+ instanceId: string,
831
+ base: string
832
+ ): Map<string, string> | null => {
833
+ const component = state.graph.getNode(componentId)
834
+ if (!component) return null
835
+ const definition = state.graph.getChildren(component.id)
836
+ const copies = state.graph.getChildren(instanceId)
837
+ if (definition.length !== copies.length) return null
838
+ const paths = new Map<string, string>()
839
+ for (let index = 0; index < definition.length; index++) {
840
+ const authored = definition[index]
841
+ const copy = copies[index]
842
+ const path = `${base}${authored.id}`
843
+ paths.set(copy.id, path)
844
+ // A nested instance is path-mapped by its own collection; only plain layers recurse here.
845
+ if (copy.type !== 'INSTANCE') {
846
+ const deeper = copyPaths(authored.id, copy.id, `${path}/`)
847
+ if (!deeper) return null
848
+ for (const [id, value] of deeper) paths.set(id, value)
849
+ }
744
850
  }
745
- if (swapped) {
746
- const icon = iconOverride(state.graph, copy.componentId)
747
- if (!icon) {
748
- unrepresentable = true
851
+ return paths
852
+ }
853
+
854
+ /**
855
+ * The authored COMPONENT behind a copy's component id. A nested ref is materialized as an
856
+ * INSTANCE that points at the authored component, so following that chain is what makes a
857
+ * same-file nested layer nameable; an imported component's chain ends outside the file.
858
+ */
859
+ const authoredComponentId = (componentId: string): string | undefined => {
860
+ // An imported clone is itself a COMPONENT, so this returning a node is not proof of authorship:
861
+ // `writtenIds` excludes internal-only clones and the caller checks that. The depth cap keeps a
862
+ // malicious or cyclic chain bounded; a deeper chain is simply counted as left out.
863
+ let node: SceneNode | undefined = state.graph.getNode(componentId)
864
+ for (let depth = 0; node && depth < 8; depth++) {
865
+ if (node.type === 'COMPONENT') return node.id
866
+ if (node.type !== 'INSTANCE' || !node.componentId) return undefined
867
+ node = state.graph.getNode(node.componentId)
868
+ }
869
+ return undefined
870
+ }
871
+
872
+ const collect = (
873
+ instance: SceneNode,
874
+ componentId: string
875
+ ): { entries?: Record<string, PenOverride>; unrepresentable: boolean } => {
876
+ const authored = authoredComponentId(componentId)
877
+ if (!authored) return { unrepresentable: true }
878
+ const paths = copyPaths(authored, instance.id, '')
879
+ if (!paths) return { unrepresentable: true }
880
+ const entries: Record<string, PenOverride> = {}
881
+ const swapped = new Set<string>()
882
+ let unrepresentable = false
883
+ for (const [copyId, overrides] of instance.instanceOverrides.descendants) {
884
+ const path = paths.get(copyId)
885
+ const copy = state.graph.getNode(copyId)
886
+ if (!path || !copy) {
887
+ // Recorded on a layer of a nested instance, whose materialized ids the file does not
888
+ // hold: the link survives, this one override is left out and counted alone.
889
+ state.drops.overrides++
749
890
  continue
750
891
  }
751
- descendants[path] = icon
752
- continue
753
- }
754
- const master = copy.componentId ? state.graph.getNode(copy.componentId) : undefined
755
- if (!master) {
756
- unrepresentable = true
757
- continue
892
+ if (overrides.has('componentId')) {
893
+ // A nested instance's component is materialized under an id the file does not hold, so a
894
+ // component swap cannot name its target; only a swap pen carries as a glyph is writable.
895
+ const icon = iconOverride(state.graph, copy.componentId)
896
+ if (!icon) {
897
+ unrepresentable = true
898
+ continue
899
+ }
900
+ // A glyph carrying a colour records fields on the icon copy itself; the swap still names
901
+ // the glyph, so the fields it can write ride along and only unnameable deeper ones count.
902
+ state.drops.overrides += copy.instanceOverrides.descendants.size
903
+ entries[path] = { ...icon, ...overrideFieldsFor(state, copy, overrides) }
904
+ swapped.add(copyId)
905
+ continue
906
+ }
907
+ const fields = overrideFieldsFor(state, copy, overrides)
908
+ if (Object.keys(fields).length > 0) mergeOverride(entries, path, fields)
758
909
  }
759
- const own = writeNode(probe, copy)
760
- const base = writeNode(probe, master)
761
- const override: PenOverride = {}
762
- for (const field of OVERRIDABLE_FIELDS) {
763
- if (!isEqual(own[field], base[field])) override[field] = own[field]
910
+ // An override recorded inside a nested instance names a layer of a materialized clone whose
911
+ // ids are generated, not the authored ones the file holds, so it has no writable path: the
912
+ // instance falls back to the layers it shows instead of writing a path that names nothing.
913
+ // b1: a same-file nested instance resolves to an authored component whose children pair with
914
+ // the nested copy's positionally, so its own overrides merge as flat `parent/child` paths.
915
+ for (const [copyId, path] of paths) {
916
+ if (swapped.has(copyId)) continue
917
+ const copy = state.graph.getNode(copyId)
918
+ if (copy?.type !== 'INSTANCE' || !copy.componentId) continue
919
+ const nestedAuthored = authoredComponentId(copy.componentId)
920
+ if (!nestedAuthored || !state.writtenIds.has(nestedAuthored)) {
921
+ state.drops.overrides += copy.instanceOverrides.descendants.size
922
+ continue
923
+ }
924
+ const deeper = collect(copy, copy.componentId)
925
+ if (deeper.unrepresentable) {
926
+ unrepresentable = true
927
+ continue
928
+ }
929
+ for (const [sub, value] of Object.entries(deeper.entries ?? {})) {
930
+ mergeOverride(entries, `${path}/${sub}`, value)
931
+ }
764
932
  }
765
- if (Object.keys(override).length > 0) descendants[path] = override
933
+ return unrepresentable ? { unrepresentable } : { entries, unrepresentable }
766
934
  }
935
+
936
+ if (!node.componentId) return { unrepresentable: true }
937
+ const collected = collect(node, node.componentId)
938
+ if (collected.unrepresentable) return { unrepresentable: true }
767
939
  return {
768
- descendants: Object.keys(descendants).length > 0 ? descendants : undefined,
769
- unrepresentable
940
+ descendants: Object.keys(collected.entries ?? {}).length > 0 ? collected.entries : undefined,
941
+ unrepresentable: false
770
942
  }
771
943
  }
772
944
 
@@ -795,9 +967,12 @@ function semanticNodeFields(state: PenWriteState, node: SceneNode, result: PenNo
795
967
  (state.writtenIds.has(node.componentId) || state.importedIds.has(node.componentId))
796
968
  ) {
797
969
  const overrides = instanceDescendants(state, node)
798
- // The file names a swap through the library it imports; one it cannot name is written as the
799
- // layers the instance draws, which keeps what it shows, as before the library work.
800
- if (overrides.unrepresentable && !state.writtenIds.has(node.componentId)) return false
970
+ // A swap or nested path the file cannot name is written as the layers the instance draws: the
971
+ // appearance survives, the link does not, and the warning says so once.
972
+ if (overrides.unrepresentable) {
973
+ state.drops.swaps++
974
+ return false
975
+ }
801
976
  result.type = 'ref'
802
977
  result.ref = node.componentId
803
978
  if (overrides.descendants) result.descendants = overrides.descendants
@@ -806,7 +981,10 @@ function semanticNodeFields(state: PenWriteState, node: SceneNode, result: PenNo
806
981
  return false
807
982
  }
808
983
  function writeNode(state: PenWriteState, node: SceneNode): PenNode {
984
+ if (node.horizontalConstraint !== 'MIN' || node.verticalConstraint !== 'MIN')
985
+ state.drops.constraints++
809
986
  if (node.overflowDirection !== 'NONE') state.drops.scroll++
987
+ if (node.layoutGrids.length > 0) state.drops.layoutGrids++
810
988
  if (node.fontFeatures.length > 0) state.drops.fontFeatures++
811
989
  const result: PenNode = { id: node.id, type: nodeType(node) }
812
990
  if (node.status !== null) result.status = node.status
@@ -873,7 +1051,18 @@ export function penDocumentFromGraph(
873
1051
  importedIds: importedComponentIds(graph, imports ?? {}),
874
1052
  warnings: options.warnings ?? [],
875
1053
  details: [],
876
- drops: { images: 0, effects: 0, filters: 0, scroll: 0, fontFeatures: 0 }
1054
+ drops: {
1055
+ images: 0,
1056
+ tileImages: 0,
1057
+ constraints: 0,
1058
+ effects: 0,
1059
+ filters: 0,
1060
+ scroll: 0,
1061
+ fontFeatures: 0,
1062
+ overrides: 0,
1063
+ swaps: 0,
1064
+ layoutGrids: 0
1065
+ }
877
1066
  }
878
1067
  const pages = graph.getPages()
879
1068
  const children = pages.flatMap((page) =>
@@ -954,12 +1143,19 @@ function enabledLibrariesOf(
954
1143
 
955
1144
  /** Adds one sentence for each kind of content the written file had to leave out. */
956
1145
  function reportDrops(state: PenWriteState): void {
957
- const { images, effects, filters, scroll, fontFeatures } = state.drops
1146
+ const { images, tileImages, constraints, effects, filters, scroll, fontFeatures, layoutGrids } =
1147
+ state.drops
958
1148
  if (images > 0) emit(state, { kind: 'missing-image-fills', count: images })
1149
+ if (tileImages > 0) emit(state, { kind: 'flattened-image-tiles', count: tileImages })
1150
+ if (constraints > 0) emit(state, { kind: 'dropped-constraints', count: constraints })
959
1151
  if (effects > 0) emit(state, { kind: 'dropped-effects', count: effects })
960
1152
  if (filters > 0) emit(state, { kind: 'dropped-filters', count: filters })
961
1153
  if (scroll > 0) emit(state, { kind: 'dropped-scroll', count: scroll })
962
1154
  if (fontFeatures > 0) emit(state, { kind: 'dropped-font-features', count: fontFeatures })
1155
+ if (layoutGrids > 0) emit(state, { kind: 'dropped-layout-grids', count: layoutGrids })
1156
+ if (state.drops.overrides > 0)
1157
+ emit(state, { kind: 'dropped-overrides', count: state.drops.overrides })
1158
+ if (state.drops.swaps > 0) emit(state, { kind: 'flattened-swaps', count: state.drops.swaps })
963
1159
  }
964
1160
 
965
1161
  export function stringifyPenDocument(document: PenDocument): string {