@pairdraw/pen 0.21.1 → 0.22.1

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.
@@ -24,7 +24,7 @@ import { getNodeLocalMatrix, localTransformFromWorld } from "@pairdraw/scene-gra
24
24
  import { fitEnclosingGroups } from "@pairdraw/scene-graph/group-bounds";
25
25
  import Matrix from "@pairdraw/scene-graph/matrix";
26
26
  import { canCreateSlot, claimSlotContent, createSlotProperty, slotOwner, slotScope, updateSlotProperty } from "@pairdraw/scene-graph/slots";
27
- import { isEqual as isEqual$1, omit } from "es-toolkit";
27
+ import { omit } from "es-toolkit";
28
28
  import { parseFontStyle } from "@pairdraw/scene-graph/font-style";
29
29
  import { percentLength, pixelLength } from "@pairdraw/scene-graph/text";
30
30
  import { fromUint8Array } from "js-base64";
@@ -555,10 +555,10 @@ const PROPERTY_FIELDS = {
555
555
  rotation: PLACEMENT_FIELDS,
556
556
  fill: ["fills"],
557
557
  stroke: STROKE_WRITES,
558
- strokeWidth: STROKE_WRITES,
558
+ strokeWidth: [...STROKE_WRITES, "strokeWeight"],
559
559
  strokeLinecap: ["strokes", "strokeCap"],
560
560
  strokeLinejoin: ["strokes", "strokeJoin"],
561
- strokeAlignment: ["strokes"],
561
+ strokeAlignment: ["strokes", "strokeAlign"],
562
562
  effect: ["effects"],
563
563
  cornerRadius: CORNER_FIELDS,
564
564
  layout: ["layoutMode"],
@@ -611,6 +611,8 @@ const PROPERTY_FIELDS = {
611
611
  scrollX: ["pluginData"],
612
612
  scrollY: ["pluginData"]
613
613
  };
614
+ /** Every pen field an override can name, and the scene fields it writes (see PROPERTY_FIELDS). */
615
+ const PEN_PROPERTY_FIELDS = Object.keys(PROPERTY_FIELDS);
614
616
  const isProperty = (key) => key in PROPERTY_FIELDS;
615
617
  /**
616
618
  * The fields the properties `pen` carries write, whatever their values: pen.dev keeps a property
@@ -1109,10 +1111,15 @@ function penWriteWarningText(warning) {
1109
1111
  case "display-p3-colors": 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.";
1110
1112
  case "library-definitions": 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.`;
1111
1113
  case "missing-image-fills": return `Image fills with missing image data were left out (${warning.count}); Pencil.dev files cannot hold a paint without its image.`;
1114
+ case "flattened-image-tiles": return `Tiled image fills were written as filled images (${warning.count}); Pencil.dev files cannot hold tiling.`;
1115
+ case "dropped-constraints": return `Resize constraints were left out of ${warning.count} ${warning.count === 1 ? "layer" : "layers"}; Pencil.dev files do not hold them.`;
1112
1116
  case "dropped-effects": return `Noise and texture effects were left out (${warning.count}); Pencil.dev files have no form for them.`;
1113
1117
  case "dropped-filters": return `Image adjustment filters were left out (${warning.count}); Pencil.dev files have no form for them.`;
1114
1118
  case "dropped-scroll": return `Scroll overflow was left out (${warning.count}); Pencil.dev files have no form for it.`;
1115
1119
  case "dropped-font-features": return `OpenType font features were left out (${warning.count}); Pencil.dev files have no form for them.`;
1120
+ case "dropped-layout-grids": return `Layout grids were left out (${warning.count} ${warning.count === 1 ? "frame" : "frames"}); Pencil.dev files have no form for them.`;
1121
+ case "dropped-overrides": return `Instance overrides a Pencil.dev file cannot name were left out (${warning.count}); the instance still links to its component.`;
1122
+ case "flattened-swaps": return `Instance swaps a Pencil.dev file cannot name were written as the layers they show (${warning.count}); they reopen detached from their components.`;
1116
1123
  default: throw new Error(`Unhandled pen write warning: ${String(warning)}`);
1117
1124
  }
1118
1125
  }
@@ -1280,6 +1287,7 @@ function imagePaint(state, fill) {
1280
1287
  ...paintBase(fill),
1281
1288
  url: `data:${imageMimeType(bytes)};base64,${fromUint8Array(bytes)}`
1282
1289
  };
1290
+ if (fill.imageScaleMode === "TILE") state.drops.tileImages++;
1283
1291
  if (fill.imageScaleMode === "FIT") paint.mode = "contain";
1284
1292
  if (fill.imageScaleMode === "CROP") {
1285
1293
  paint.mode = "stretch";
@@ -1395,7 +1403,6 @@ function layoutFields(state, node) {
1395
1403
  variableOr(state, node, "paddingLeft", node.paddingLeft)
1396
1404
  ];
1397
1405
  if (padding.some((value) => value !== 0)) fields.padding = padding;
1398
- if (node.clipsContent) fields.clip = true;
1399
1406
  if (node.strokesIncludedInLayout) fields.layoutIncludeStroke = true;
1400
1407
  return fields;
1401
1408
  }
@@ -1547,7 +1554,25 @@ function basicNodeFields(state, node, result) {
1547
1554
  if (theme) result.theme = theme;
1548
1555
  const fill = paintsOf(state, node, "fills", node.fills);
1549
1556
  if (fill !== void 0) result.fill = fill;
1550
- Object.assign(result, strokesOf(state, node), layoutFields(state, node), cornerFields(state, node), textFields(state, node));
1557
+ if (node.clipsContent) result.clip = true;
1558
+ Object.assign(result, strokesOf(state, node), layoutFields(state, node), cornerFields(state, node), textFields(state, node), shapeFields(node));
1559
+ }
1560
+ /**
1561
+ * The shape fields pen stores outside the paint and layout groups: an ellipse's arc, which the
1562
+ * reader rebuilds on reopen, and a polygon's point count, whose pen default (3) the reader uses
1563
+ * when the field is absent.
1564
+ */
1565
+ function shapeFields(node) {
1566
+ if (node.type === "ELLIPSE" && node.arcData) {
1567
+ const { startingAngle, endingAngle, innerRadius } = node.arcData;
1568
+ return {
1569
+ innerRadius: rounded(innerRadius),
1570
+ startAngle: rounded(-startingAngle * 180 / Math.PI),
1571
+ sweepAngle: rounded(-((endingAngle - startingAngle) * 180) / Math.PI)
1572
+ };
1573
+ }
1574
+ if ((node.type === "POLYGON" || node.type === "STAR") && node.pointCount !== 3) return { polygonCount: node.pointCount };
1575
+ return {};
1551
1576
  }
1552
1577
  /**
1553
1578
  * A layer's variable mode assignments, by the names pen writes: the collection's name and the
@@ -1563,44 +1588,43 @@ function themeFields(state, node) {
1563
1588
  return Object.keys(theme).length > 0 ? theme : void 0;
1564
1589
  }
1565
1590
  /**
1566
- * The pen fields an instance override writes: what it changes on a layer, not the placement and
1567
- * layout a reopen recalculates from the component and the boxes around it.
1591
+ * Pen override fields the writer must never store, because a reopen recalculates them from the
1592
+ * component and the boxes around it (X), or they are shape facts of a component, not a layer (Y).
1593
+ * Every other field PEN_PROPERTY_FIELDS names is writable, so the allow list follows the property
1594
+ * map instead of drifting beside it; a field missing here fails a test rather than disappearing.
1595
+ * Some names are writable in principle but never written today because their property map names no
1596
+ * recordable scene field: the icon identity fields (library, icon, weight, iconFontName,
1597
+ * iconFontFamily), text decoration (underline, strikethrough), and the plugin-only names whose map
1598
+ * is just `pluginData`. If INSTANCE_OVERRIDE_FIELDS ever gains one of those targets, add the name
1599
+ * here first.
1568
1600
  */
1569
- const OVERRIDABLE_FIELDS = /* @__PURE__ */ new Set([
1570
- "content",
1571
- "fill",
1572
- "stroke",
1573
- "strokeWidth",
1574
- "cornerRadius",
1575
- "effect",
1576
- "opacity",
1577
- "enabled",
1578
- "fontFamily",
1579
- "fontSize",
1580
- "fontWeight",
1581
- "fontStyle",
1582
- "textAlign",
1583
- "textAlignVertical",
1584
- "lineHeight",
1585
- "letterSpacing",
1586
- "href"
1601
+ const PEN_DENIED_OVERRIDE_FIELDS = /* @__PURE__ */ new Set([
1602
+ "x",
1603
+ "y",
1604
+ "rotation",
1605
+ "flipX",
1606
+ "flipY",
1607
+ "layoutPosition",
1608
+ "layout",
1609
+ "gap",
1610
+ "padding",
1611
+ "justifyContent",
1612
+ "alignItems",
1613
+ "layoutIncludeStroke",
1614
+ "viewBox",
1615
+ "geometry",
1616
+ "fillRule",
1617
+ "reusable",
1618
+ "slot",
1619
+ "placeholder",
1620
+ "ref",
1621
+ "innerRadius",
1622
+ "startAngle",
1623
+ "sweepAngle",
1624
+ "polygonCount"
1587
1625
  ]);
1588
- /**
1589
- * The path a copy sits at in its component, from the instance down: the ids of the layers it
1590
- * copies, as pen names a `descendants` override. A copy that swapped its component copies `swapOf`
1591
- * instead of the component it draws.
1592
- */
1593
- function overridePath(graph, instanceId, copyId, swapOf) {
1594
- const parts = [];
1595
- let current = graph.getNode(copyId);
1596
- while (current && current.id !== instanceId) {
1597
- const id = current.id === copyId && swapOf ? swapOf : current.componentId;
1598
- if (!id) return null;
1599
- parts.unshift(id);
1600
- current = current.parentId ? graph.getNode(current.parentId) : void 0;
1601
- }
1602
- return current ? parts.join("/") : null;
1603
- }
1626
+ /** The pen fields an instance override writes: everything the property map names but the denylist. */
1627
+ const PEN_OVERRIDE_FIELDS = PEN_PROPERTY_FIELDS.filter((field) => !PEN_DENIED_OVERRIDE_FIELDS.has(field));
1604
1628
  /**
1605
1629
  * The fields a swap override names an icon by: pen carries the glyph, not the component drawn.
1606
1630
  */
@@ -1613,58 +1637,156 @@ function iconOverride(graph, componentId) {
1613
1637
  return override;
1614
1638
  }
1615
1639
  /**
1616
- * The overrides an instance writes, and whether one of them is a swap the file cannot name. A
1617
- * layer that swapped its component draws another component than the one it copies, which pen holds
1618
- * as a glyph for an icon; any other swap is what the instance shows, not what it links to.
1640
+ * The pen fields a copy's recorded overrides name, in pen's own names. A field is written when the
1641
+ * copy records at least one scene field it maps to, so a value set back to the component's own
1642
+ * still belongs to the instance.
1619
1643
  */
1620
- function instanceDescendants(state, node) {
1621
- const overridden = [...node.instanceOverrides.descendants.keys()];
1622
- if (overridden.length === 0) return { unrepresentable: false };
1623
- const probe = {
1644
+ function overrideFieldsFor(state, copy, overrides) {
1645
+ const own = writeNode({
1624
1646
  ...state,
1625
1647
  drops: {
1626
1648
  images: 0,
1649
+ tileImages: 0,
1650
+ constraints: 0,
1627
1651
  effects: 0,
1628
1652
  filters: 0,
1629
1653
  scroll: 0,
1630
- fontFeatures: 0
1654
+ fontFeatures: 0,
1655
+ overrides: 0,
1656
+ swaps: 0,
1657
+ layoutGrids: 0
1631
1658
  }
1659
+ }, copy);
1660
+ const override = {};
1661
+ for (const field of PEN_OVERRIDE_FIELDS) {
1662
+ if (!(field in own)) continue;
1663
+ if (field === "width" || field === "height") {
1664
+ const axis = field === "width" ? "HORIZONTAL" : "VERTICAL";
1665
+ if (layoutSizing(state.graph, copy, axis) !== "FIXED") continue;
1666
+ }
1667
+ const value = own[field];
1668
+ if (!fieldsSetBy({ [field]: value }).some((key) => overrides.has(key))) continue;
1669
+ override[field] = value;
1670
+ }
1671
+ return override;
1672
+ }
1673
+ /** Adds a field map at a path, merging with one already there instead of replacing it. */
1674
+ function mergeOverride(entries, path, fields) {
1675
+ entries[path] = {
1676
+ ...entries[path],
1677
+ ...fields
1632
1678
  };
1633
- const descendants = {};
1634
- let unrepresentable = false;
1635
- for (const copyId of overridden) {
1636
- const overrides = node.instanceOverrides.descendants.get(copyId);
1637
- const swapped = overrides?.has("componentId") === true;
1638
- const swapOf = swapped ? overrides.get("sourceComponentId") : void 0;
1639
- const path = overridePath(state.graph, node.id, copyId, typeof swapOf === "string" ? swapOf : void 0);
1640
- const copy = state.graph.getNode(copyId);
1641
- if (!path || !copy) {
1642
- unrepresentable = true;
1643
- continue;
1679
+ }
1680
+ /**
1681
+ * The overrides an instance writes, and whether one of them is a swap the file cannot name.
1682
+ *
1683
+ * Three cases:
1684
+ * (i) a direct override, and a plain layer deeper than one level, is written at the authored layer id
1685
+ * from the component definition (a copy only holds the component it draws, so the definition is
1686
+ * paired with the copy list at every plain level);
1687
+ * (ii) a same-file nested instance (b1) resolves through its materialized `INSTANCE` chain to the
1688
+ * authored component, and its own overrides merge as flat `parent/child` paths;
1689
+ * (iii) an imported nested component resolves outside the file (b2 stores an authored-id map when
1690
+ * that changes), so its override is left out and counted in `drops.overrides` while the instance
1691
+ * keeps its `ref`; a swap the file cannot name as a glyph flattens the instance and is counted in
1692
+ * `drops.swaps`.
1693
+ * `copyPaths` pairs definitions with copies positionally; it skips nested `INSTANCE` copies because
1694
+ * their paths are collected by the recursion below, not because they are unnameable.
1695
+ */
1696
+ function instanceDescendants(state, node) {
1697
+ /** The authored path of every copy this instance shows, plain layers included. */
1698
+ const copyPaths = (componentId, instanceId, base) => {
1699
+ const component = state.graph.getNode(componentId);
1700
+ if (!component) return null;
1701
+ const definition = state.graph.getChildren(component.id);
1702
+ const copies = state.graph.getChildren(instanceId);
1703
+ if (definition.length !== copies.length) return null;
1704
+ const paths = /* @__PURE__ */ new Map();
1705
+ for (let index = 0; index < definition.length; index++) {
1706
+ const authored = definition[index];
1707
+ const copy = copies[index];
1708
+ const path = `${base}${authored.id}`;
1709
+ paths.set(copy.id, path);
1710
+ if (copy.type !== "INSTANCE") {
1711
+ const deeper = copyPaths(authored.id, copy.id, `${path}/`);
1712
+ if (!deeper) return null;
1713
+ for (const [id, value] of deeper) paths.set(id, value);
1714
+ }
1644
1715
  }
1645
- if (swapped) {
1646
- const icon = iconOverride(state.graph, copy.componentId);
1647
- if (!icon) {
1648
- unrepresentable = true;
1716
+ return paths;
1717
+ };
1718
+ /**
1719
+ * The authored COMPONENT behind a copy's component id. A nested ref is materialized as an
1720
+ * INSTANCE that points at the authored component, so following that chain is what makes a
1721
+ * same-file nested layer nameable; an imported component's chain ends outside the file.
1722
+ */
1723
+ const authoredComponentId = (componentId) => {
1724
+ let node = state.graph.getNode(componentId);
1725
+ for (let depth = 0; node && depth < 8; depth++) {
1726
+ if (node.type === "COMPONENT") return node.id;
1727
+ if (node.type !== "INSTANCE" || !node.componentId) return void 0;
1728
+ node = state.graph.getNode(node.componentId);
1729
+ }
1730
+ };
1731
+ const collect = (instance, componentId) => {
1732
+ const authored = authoredComponentId(componentId);
1733
+ if (!authored) return { unrepresentable: true };
1734
+ const paths = copyPaths(authored, instance.id, "");
1735
+ if (!paths) return { unrepresentable: true };
1736
+ const entries = {};
1737
+ const swapped = /* @__PURE__ */ new Set();
1738
+ let unrepresentable = false;
1739
+ for (const [copyId, overrides] of instance.instanceOverrides.descendants) {
1740
+ const path = paths.get(copyId);
1741
+ const copy = state.graph.getNode(copyId);
1742
+ if (!path || !copy) {
1743
+ state.drops.overrides++;
1649
1744
  continue;
1650
1745
  }
1651
- descendants[path] = icon;
1652
- continue;
1746
+ if (overrides.has("componentId")) {
1747
+ const icon = iconOverride(state.graph, copy.componentId);
1748
+ if (!icon) {
1749
+ unrepresentable = true;
1750
+ continue;
1751
+ }
1752
+ state.drops.overrides += copy.instanceOverrides.descendants.size;
1753
+ entries[path] = {
1754
+ ...icon,
1755
+ ...overrideFieldsFor(state, copy, overrides)
1756
+ };
1757
+ swapped.add(copyId);
1758
+ continue;
1759
+ }
1760
+ const fields = overrideFieldsFor(state, copy, overrides);
1761
+ if (Object.keys(fields).length > 0) mergeOverride(entries, path, fields);
1653
1762
  }
1654
- const master = copy.componentId ? state.graph.getNode(copy.componentId) : void 0;
1655
- if (!master) {
1656
- unrepresentable = true;
1657
- continue;
1763
+ for (const [copyId, path] of paths) {
1764
+ if (swapped.has(copyId)) continue;
1765
+ const copy = state.graph.getNode(copyId);
1766
+ if (copy?.type !== "INSTANCE" || !copy.componentId) continue;
1767
+ const nestedAuthored = authoredComponentId(copy.componentId);
1768
+ if (!nestedAuthored || !state.writtenIds.has(nestedAuthored)) {
1769
+ state.drops.overrides += copy.instanceOverrides.descendants.size;
1770
+ continue;
1771
+ }
1772
+ const deeper = collect(copy, copy.componentId);
1773
+ if (deeper.unrepresentable) {
1774
+ unrepresentable = true;
1775
+ continue;
1776
+ }
1777
+ for (const [sub, value] of Object.entries(deeper.entries ?? {})) mergeOverride(entries, `${path}/${sub}`, value);
1658
1778
  }
1659
- const own = writeNode(probe, copy);
1660
- const base = writeNode(probe, master);
1661
- const override = {};
1662
- for (const field of OVERRIDABLE_FIELDS) if (!isEqual$1(own[field], base[field])) override[field] = own[field];
1663
- if (Object.keys(override).length > 0) descendants[path] = override;
1664
- }
1779
+ return unrepresentable ? { unrepresentable } : {
1780
+ entries,
1781
+ unrepresentable
1782
+ };
1783
+ };
1784
+ if (!node.componentId) return { unrepresentable: true };
1785
+ const collected = collect(node, node.componentId);
1786
+ if (collected.unrepresentable) return { unrepresentable: true };
1665
1787
  return {
1666
- descendants: Object.keys(descendants).length > 0 ? descendants : void 0,
1667
- unrepresentable
1788
+ descendants: Object.keys(collected.entries ?? {}).length > 0 ? collected.entries : void 0,
1789
+ unrepresentable: false
1668
1790
  };
1669
1791
  }
1670
1792
  function semanticNodeFields(state, node, result) {
@@ -1681,7 +1803,10 @@ function semanticNodeFields(state, node, result) {
1681
1803
  if (node.type === "COMPONENT" || node.type === "COMPONENT_SET") result.reusable = true;
1682
1804
  if (node.type === "INSTANCE" && node.componentId && (state.writtenIds.has(node.componentId) || state.importedIds.has(node.componentId))) {
1683
1805
  const overrides = instanceDescendants(state, node);
1684
- if (overrides.unrepresentable && !state.writtenIds.has(node.componentId)) return false;
1806
+ if (overrides.unrepresentable) {
1807
+ state.drops.swaps++;
1808
+ return false;
1809
+ }
1685
1810
  result.type = "ref";
1686
1811
  result.ref = node.componentId;
1687
1812
  if (overrides.descendants) result.descendants = overrides.descendants;
@@ -1690,7 +1815,9 @@ function semanticNodeFields(state, node, result) {
1690
1815
  return false;
1691
1816
  }
1692
1817
  function writeNode(state, node) {
1818
+ if (node.horizontalConstraint !== "MIN" || node.verticalConstraint !== "MIN") state.drops.constraints++;
1693
1819
  if (node.overflowDirection !== "NONE") state.drops.scroll++;
1820
+ if (node.layoutGrids.length > 0) state.drops.layoutGrids++;
1694
1821
  if (node.fontFeatures.length > 0) state.drops.fontFeatures++;
1695
1822
  const result = {
1696
1823
  id: node.id,
@@ -1746,10 +1873,15 @@ function penDocumentFromGraph(graph, options = {}) {
1746
1873
  details: [],
1747
1874
  drops: {
1748
1875
  images: 0,
1876
+ tileImages: 0,
1877
+ constraints: 0,
1749
1878
  effects: 0,
1750
1879
  filters: 0,
1751
1880
  scroll: 0,
1752
- fontFeatures: 0
1881
+ fontFeatures: 0,
1882
+ overrides: 0,
1883
+ swaps: 0,
1884
+ layoutGrids: 0
1753
1885
  }
1754
1886
  };
1755
1887
  const pages = graph.getPages();
@@ -1820,11 +1952,19 @@ function enabledLibrariesOf(graph, state) {
1820
1952
  }
1821
1953
  /** Adds one sentence for each kind of content the written file had to leave out. */
1822
1954
  function reportDrops(state) {
1823
- const { images, effects, filters, scroll, fontFeatures } = state.drops;
1955
+ const { images, tileImages, constraints, effects, filters, scroll, fontFeatures, layoutGrids } = state.drops;
1824
1956
  if (images > 0) emit(state, {
1825
1957
  kind: "missing-image-fills",
1826
1958
  count: images
1827
1959
  });
1960
+ if (tileImages > 0) emit(state, {
1961
+ kind: "flattened-image-tiles",
1962
+ count: tileImages
1963
+ });
1964
+ if (constraints > 0) emit(state, {
1965
+ kind: "dropped-constraints",
1966
+ count: constraints
1967
+ });
1828
1968
  if (effects > 0) emit(state, {
1829
1969
  kind: "dropped-effects",
1830
1970
  count: effects
@@ -1841,6 +1981,18 @@ function reportDrops(state) {
1841
1981
  kind: "dropped-font-features",
1842
1982
  count: fontFeatures
1843
1983
  });
1984
+ if (layoutGrids > 0) emit(state, {
1985
+ kind: "dropped-layout-grids",
1986
+ count: layoutGrids
1987
+ });
1988
+ if (state.drops.overrides > 0) emit(state, {
1989
+ kind: "dropped-overrides",
1990
+ count: state.drops.overrides
1991
+ });
1992
+ if (state.drops.swaps > 0) emit(state, {
1993
+ kind: "flattened-swaps",
1994
+ count: state.drops.swaps
1995
+ });
1844
1996
  }
1845
1997
  function stringifyPenDocument(document) {
1846
1998
  return `${JSON.stringify(sortKeys(document), null, 2)}\n`;