@pairdraw/pen 0.21.0 → 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/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,
@@ -76,19 +76,94 @@ type PenWriteState = {
76
76
  importedIds: ReadonlySet<string>
77
77
  /** What the file cannot hold, reported once writing leaves it out. */
78
78
  warnings: string[]
79
+ /** The same facts as kinds, for a host that shows them in its own language. */
80
+ details: PenWriteWarning[]
79
81
  drops: {
80
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
81
87
  effects: number
82
88
  filters: number
83
89
  scroll: number
84
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
97
+ }
98
+ }
99
+
100
+ /**
101
+ * What a written `.pen` file could not hold, as facts rather than a sentence: the app shows these
102
+ * in the reader's language, while the CLI and MCP print `penWriteWarningText` of each.
103
+ */
104
+ export type PenWriteWarning =
105
+ | { kind: 'merged-pages'; pages: number; name: string }
106
+ | { kind: 'display-p3-colors' }
107
+ | { kind: 'library-definitions'; count: number }
108
+ | { kind: 'missing-image-fills'; count: number }
109
+ | { kind: 'flattened-image-tiles'; count: number }
110
+ | { kind: 'dropped-constraints'; count: number }
111
+ | { kind: 'dropped-effects'; count: number }
112
+ | { kind: 'dropped-filters'; count: number }
113
+ | { kind: 'dropped-scroll'; count: number }
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 }
118
+
119
+ /** The English sentence a write warning has always been reported as. */
120
+ export function penWriteWarningText(warning: PenWriteWarning): string {
121
+ switch (warning.kind) {
122
+ case 'merged-pages':
123
+ return `Pencil.dev files hold one page; ${warning.pages} pages were merged into “${warning.name}”.`
124
+ case 'display-p3-colors':
125
+ return 'Colors are written as sRGB hex, the only form a Pencil.dev file holds; a Display P3 color outside sRGB comes out as the nearest sRGB color.'
126
+ case 'library-definitions':
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.`
128
+ case 'missing-image-fills':
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.`
134
+ case 'dropped-effects':
135
+ return `Noise and texture effects were left out (${warning.count}); Pencil.dev files have no form for them.`
136
+ case 'dropped-filters':
137
+ return `Image adjustment filters were left out (${warning.count}); Pencil.dev files have no form for them.`
138
+ case 'dropped-scroll':
139
+ return `Scroll overflow was left out (${warning.count}); Pencil.dev files have no form for it.`
140
+ case 'dropped-font-features':
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.`
148
+ default: {
149
+ const unhandled: never = warning
150
+ throw new Error(`Unhandled pen write warning: ${String(unhandled)}`)
151
+ }
85
152
  }
86
153
  }
87
154
 
155
+ /** Records a warning as its sentence and its kind, in the order it was found. */
156
+ function emit(state: PenWriteState, warning: PenWriteWarning): void {
157
+ state.details.push(warning)
158
+ state.warnings.push(penWriteWarningText(warning))
159
+ }
160
+
88
161
  /** What a caller learns about content a written `.pen` file cannot hold. */
89
162
  export interface PenWriteOptions {
90
163
  /** Collects one sentence per kind of content left out, in place of silently losing it. */
91
164
  warnings?: string[]
165
+ /** Collects the same facts as kinds, for a host that shows them in its own language. */
166
+ warningDetails?: PenWriteWarning[]
92
167
  }
93
168
 
94
169
  /** Snaps matrix values that are within floating-point noise of an integer. */
@@ -222,6 +297,7 @@ function imagePaint(state: PenWriteState, fill: Fill): PenPaint | null {
222
297
  ...paintBase(fill),
223
298
  url: `data:${imageMimeType(bytes)};base64,${fromUint8Array(bytes)}`
224
299
  }
300
+ if (fill.imageScaleMode === 'TILE') state.drops.tileImages++
225
301
  if (fill.imageScaleMode === 'FIT') paint.mode = 'contain'
226
302
  if (fill.imageScaleMode === 'CROP') {
227
303
  paint.mode = 'stretch'
@@ -363,7 +439,6 @@ function layoutFields(state: PenWriteState, node: SceneNode): Partial<PenNode> {
363
439
  variableOr(state, node, 'paddingLeft', node.paddingLeft)
364
440
  ]
365
441
  if (padding.some((value) => value !== 0)) fields.padding = padding
366
- if (node.clipsContent) fields.clip = true
367
442
  if (node.strokesIncludedInLayout) fields.layoutIncludeStroke = true
368
443
  return fields
369
444
  }
@@ -579,14 +654,38 @@ function basicNodeFields(state: PenWriteState, node: SceneNode, result: PenNode)
579
654
  if (theme) result.theme = theme
580
655
  const fill = paintsOf(state, node, 'fills', node.fills)
581
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
582
660
  Object.assign(
583
661
  result,
584
662
  strokesOf(state, node),
585
663
  layoutFields(state, node),
586
664
  cornerFields(state, node),
587
- textFields(state, node)
665
+ textFields(state, node),
666
+ shapeFields(node)
588
667
  )
589
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
+ }
590
689
  /**
591
690
  * A layer's variable mode assignments, by the names pen writes: the collection's name and the
592
691
  * mode's. A mode the document no longer holds is left out.
@@ -602,50 +701,46 @@ function themeFields(state: PenWriteState, node: SceneNode): Record<string, stri
602
701
  }
603
702
 
604
703
  /**
605
- * The pen fields an instance override writes: what it changes on a layer, not the placement and
606
- * 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.
607
713
  */
608
- const OVERRIDABLE_FIELDS: ReadonlySet<string> = new Set([
609
- 'content',
610
- 'fill',
611
- 'stroke',
612
- 'strokeWidth',
613
- 'cornerRadius',
614
- 'effect',
615
- 'opacity',
616
- 'enabled',
617
- 'fontFamily',
618
- 'fontSize',
619
- 'fontWeight',
620
- 'fontStyle',
621
- 'textAlign',
622
- 'textAlignVertical',
623
- 'lineHeight',
624
- 'letterSpacing',
625
- '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'
626
738
  ])
627
739
 
628
- /**
629
- * The path a copy sits at in its component, from the instance down: the ids of the layers it
630
- * copies, as pen names a `descendants` override. A copy that swapped its component copies `swapOf`
631
- * instead of the component it draws.
632
- */
633
- function overridePath(
634
- graph: SceneGraph,
635
- instanceId: string,
636
- copyId: string,
637
- swapOf?: string
638
- ): string | null {
639
- const parts: string[] = []
640
- let current = graph.getNode(copyId)
641
- while (current && current.id !== instanceId) {
642
- const id = current.id === copyId && swapOf ? swapOf : current.componentId
643
- if (!id) return null
644
- parts.unshift(id)
645
- current = current.parentId ? graph.getNode(current.parentId) : undefined
646
- }
647
- return current ? parts.join('/') : null
648
- }
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
+ )
649
744
 
650
745
  /**
651
746
  * The fields a swap override names an icon by: pen carries the glyph, not the component drawn.
@@ -660,63 +755,190 @@ function iconOverride(graph: SceneGraph, componentId: string | null): PenOverrid
660
755
  }
661
756
 
662
757
  /**
663
- * The overrides an instance writes, and whether one of them is a swap the file cannot name. A
664
- * layer that swapped its component draws another component than the one it copies, which pen holds
665
- * 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.
666
822
  */
667
823
  function instanceDescendants(
668
824
  state: PenWriteState,
669
825
  node: SceneNode
670
826
  ): { descendants?: Record<string, PenOverride>; unrepresentable: boolean } {
671
- const overridden = [...node.instanceOverrides.descendants.keys()]
672
- if (overridden.length === 0) return { unrepresentable: false }
673
- // Comparing writes fields the moment a document writes them, but their drops are counted once.
674
- const probe: PenWriteState = {
675
- ...state,
676
- drops: { images: 0, effects: 0, filters: 0, scroll: 0, fontFeatures: 0 }
677
- }
678
- const descendants: Record<string, PenOverride> = {}
679
- let unrepresentable = false
680
- for (const copyId of overridden) {
681
- const overrides = node.instanceOverrides.descendants.get(copyId)
682
- const swapped = overrides?.has('componentId') === true
683
- const swapOf = swapped ? overrides.get('sourceComponentId') : undefined
684
- const path = overridePath(
685
- state.graph,
686
- node.id,
687
- copyId,
688
- typeof swapOf === 'string' ? swapOf : undefined
689
- )
690
- const copy = state.graph.getNode(copyId)
691
- if (!path || !copy) {
692
- unrepresentable = true
693
- 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
+ }
694
850
  }
695
- if (swapped) {
696
- const icon = iconOverride(state.graph, copy.componentId)
697
- if (!icon) {
698
- 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++
699
890
  continue
700
891
  }
701
- descendants[path] = icon
702
- continue
703
- }
704
- const master = copy.componentId ? state.graph.getNode(copy.componentId) : undefined
705
- if (!master) {
706
- unrepresentable = true
707
- 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)
708
909
  }
709
- const own = writeNode(probe, copy)
710
- const base = writeNode(probe, master)
711
- const override: PenOverride = {}
712
- for (const field of OVERRIDABLE_FIELDS) {
713
- 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
+ }
714
932
  }
715
- if (Object.keys(override).length > 0) descendants[path] = override
933
+ return unrepresentable ? { unrepresentable } : { entries, unrepresentable }
716
934
  }
935
+
936
+ if (!node.componentId) return { unrepresentable: true }
937
+ const collected = collect(node, node.componentId)
938
+ if (collected.unrepresentable) return { unrepresentable: true }
717
939
  return {
718
- descendants: Object.keys(descendants).length > 0 ? descendants : undefined,
719
- unrepresentable
940
+ descendants: Object.keys(collected.entries ?? {}).length > 0 ? collected.entries : undefined,
941
+ unrepresentable: false
720
942
  }
721
943
  }
722
944
 
@@ -745,9 +967,12 @@ function semanticNodeFields(state: PenWriteState, node: SceneNode, result: PenNo
745
967
  (state.writtenIds.has(node.componentId) || state.importedIds.has(node.componentId))
746
968
  ) {
747
969
  const overrides = instanceDescendants(state, node)
748
- // The file names a swap through the library it imports; one it cannot name is written as the
749
- // layers the instance draws, which keeps what it shows, as before the library work.
750
- 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
+ }
751
976
  result.type = 'ref'
752
977
  result.ref = node.componentId
753
978
  if (overrides.descendants) result.descendants = overrides.descendants
@@ -756,7 +981,10 @@ function semanticNodeFields(state: PenWriteState, node: SceneNode, result: PenNo
756
981
  return false
757
982
  }
758
983
  function writeNode(state: PenWriteState, node: SceneNode): PenNode {
984
+ if (node.horizontalConstraint !== 'MIN' || node.verticalConstraint !== 'MIN')
985
+ state.drops.constraints++
759
986
  if (node.overflowDirection !== 'NONE') state.drops.scroll++
987
+ if (node.layoutGrids.length > 0) state.drops.layoutGrids++
760
988
  if (node.fontFeatures.length > 0) state.drops.fontFeatures++
761
989
  const result: PenNode = { id: node.id, type: nodeType(node) }
762
990
  if (node.status !== null) result.status = node.status
@@ -822,7 +1050,19 @@ export function penDocumentFromGraph(
822
1050
  writtenIds: writtenNodeIds(graph),
823
1051
  importedIds: importedComponentIds(graph, imports ?? {}),
824
1052
  warnings: options.warnings ?? [],
825
- drops: { images: 0, effects: 0, filters: 0, scroll: 0, fontFeatures: 0 }
1053
+ details: [],
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
+ }
826
1066
  }
827
1067
  const pages = graph.getPages()
828
1068
  const children = pages.flatMap((page) =>
@@ -875,16 +1115,13 @@ export function penDocumentFromGraph(
875
1115
  if (publication) document.sourceLibraryPublication = publication
876
1116
  if (pages.length > 1) {
877
1117
  const name = document.name ?? 'Page 1'
878
- state.warnings.push(
879
- `Pencil.dev files hold one page; ${pages.length} pages were merged into “${name}”.`
880
- )
1118
+ emit(state, { kind: 'merged-pages', pages: pages.length, name })
881
1119
  }
882
1120
  if (graph.documentColorSpace === 'display-p3') {
883
- state.warnings.push(
884
- 'Colors are written as sRGB hex, the only form a Pencil.dev file holds; a Display P3 color outside sRGB comes out as the nearest sRGB color.'
885
- )
1121
+ emit(state, { kind: 'display-p3-colors' })
886
1122
  }
887
1123
  reportDrops(state)
1124
+ options.warningDetails?.push(...state.details)
888
1125
  return document
889
1126
  }
890
1127
 
@@ -900,36 +1137,25 @@ function enabledLibrariesOf(
900
1137
  const bindings = [...graph.enabledLibraries.values()]
901
1138
  if (bindings.length === 0) return null
902
1139
  const enabled = bindings.filter((binding) => binding.enabled).length
903
- if (enabled > 0)
904
- state.warnings.push(
905
- `Library components came from ${enabled} enabled ${enabled === 1 ? 'library' : 'libraries'}, whose definitions Pencil.dev files do not hold; re-enable those libraries after reopening to restore them.`
906
- )
1140
+ if (enabled > 0) emit(state, { kind: 'library-definitions', count: enabled })
907
1141
  return bindings
908
1142
  }
909
1143
 
910
1144
  /** Adds one sentence for each kind of content the written file had to leave out. */
911
1145
  function reportDrops(state: PenWriteState): void {
912
- const { images, effects, filters, scroll, fontFeatures } = state.drops
913
- if (images > 0)
914
- state.warnings.push(
915
- `Image fills with missing image data were left out (${images}); Pencil.dev files cannot hold a paint without its image.`
916
- )
917
- if (effects > 0)
918
- state.warnings.push(
919
- `Noise and texture effects were left out (${effects}); Pencil.dev files have no form for them.`
920
- )
921
- if (filters > 0)
922
- state.warnings.push(
923
- `Image adjustment filters were left out (${filters}); Pencil.dev files have no form for them.`
924
- )
925
- if (scroll > 0)
926
- state.warnings.push(
927
- `Scroll overflow was left out (${scroll}); Pencil.dev files have no form for it.`
928
- )
929
- if (fontFeatures > 0)
930
- state.warnings.push(
931
- `OpenType font features were left out (${fontFeatures}); Pencil.dev files have no form for them.`
932
- )
1146
+ const { images, tileImages, constraints, effects, filters, scroll, fontFeatures, layoutGrids } =
1147
+ state.drops
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 })
1151
+ if (effects > 0) emit(state, { kind: 'dropped-effects', count: effects })
1152
+ if (filters > 0) emit(state, { kind: 'dropped-filters', count: filters })
1153
+ if (scroll > 0) emit(state, { kind: 'dropped-scroll', count: scroll })
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 })
933
1159
  }
934
1160
 
935
1161
  export function stringifyPenDocument(document: PenDocument): string {