@pairdraw/pen 0.18.0 → 0.19.0-beta.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.
@@ -1,6 +1,6 @@
1
1
  import { UNRESOLVED_REF, applyAnnotation, applyCornerRadius, applyPadding, applyPenPluginData, bindIfVar, buildVarContext, createAnnotation, isAnnotation, isVarRef, mapAlignItems, mapJustifyContent, mapLayoutMode, parseSize, resolveBoolean, resolveNumber, resolveString, unresolvedRef } from "./annotations.js";
2
2
  import { withImports } from "./imports.js";
3
- import { ellipseArc, lineNetwork, pathNetwork } from "./geometry.js";
3
+ import { ellipseArc, lineNetwork, networkToSVGPath, pathNetwork } from "./geometry.js";
4
4
  import { drawIcon, insideComponent, removeUnusedIconComponents, useIconComponent } from "./icons.js";
5
5
  import { applyStroke, convertEffects, convertPaints, createImageStore, penFileSchema, strokeFields } from "./effects.js";
6
6
  import { SceneGraph, generateId } from "@pairdraw/scene-graph";
@@ -19,6 +19,7 @@ import { getNodeLocalMatrix, localTransformFromWorld } from "@pairdraw/scene-gra
19
19
  import { fitEnclosingGroups } from "@pairdraw/scene-graph/group-bounds";
20
20
  import Matrix from "@pairdraw/scene-graph/matrix";
21
21
  import { canCreateSlot, claimSlotContent, createSlotProperty, slotOwner, slotScope, updateSlotProperty } from "@pairdraw/scene-graph/slots";
22
+ import { parseFontStyle } from "@pairdraw/scene-graph/font-style";
22
23
  import { percentLength, pixelLength } from "@pairdraw/scene-graph/text";
23
24
  //#region src/library.ts
24
25
  /** The component IDs a layer and its overrides name: its `ref`s and the slots' suggestions. */
@@ -364,7 +365,7 @@ const TEXT_GROWTH = {
364
365
  * as pen.dev shows it. pen.dev 2.21 draws no underline, strikethrough, or text stroke, so those
365
366
  * are left out to match it. An `href` is kept as a link, drawn without an underline.
366
367
  */
367
- function applyTextProps(node, pen, ctx) {
368
+ function applyTextProps(node, pen, ctx, fonts) {
368
369
  const isIconFont = pen.type === "icon_font";
369
370
  node.text = isIconFont ? pen.iconFontName ?? "" : pen.content ?? "";
370
371
  node.fontFamily = isIconFont ? pen.iconFontFamily ?? "Material Symbols Sharp" : resolveString(pen.fontFamily, ctx) ?? "Inter";
@@ -375,6 +376,17 @@ function applyTextProps(node, pen, ctx) {
375
376
  node.fontWeight = fontWeight(weight, ctx);
376
377
  bindIfVar(node, "fontWeight", weight, ctx);
377
378
  node.italic = resolveString(pen.fontStyle, ctx)?.toLowerCase() === "italic";
379
+ const bundled = isIconFont ? void 0 : fonts.get(node.fontFamily);
380
+ if (bundled) {
381
+ const { weight, italic } = parseFontStyle(bundled.style);
382
+ Object.assign(node, {
383
+ fontFamily: bundled.family,
384
+ fontStyle: bundled.style,
385
+ fontWeight: weight,
386
+ italic
387
+ });
388
+ delete node.boundVariables.fontFamily;
389
+ }
378
390
  node.textAlignHorizontal = TEXT_ALIGN[pen.textAlign ?? "left"] ?? "LEFT";
379
391
  node.textAlignVertical = TEXT_ALIGN_VERTICAL[pen.textAlignVertical ?? "top"] ?? "TOP";
380
392
  if (pen.lineHeight !== void 0) node.lineHeight = percentLength(resolveNumber(pen.lineHeight, ctx) * 100);
@@ -459,6 +471,7 @@ function applyAutoLayout(node, layoutMode, pen, widthSizing, heightSizing, ctx)
459
471
  node.primaryAxisSizing = own(widthSizing);
460
472
  node.counterAxisSizing = own(heightSizing);
461
473
  }
474
+ if (node.primaryAxisAlign === "SPACE_BETWEEN" && node.primaryAxisSizing === "HUG") node.primaryAxisAlign = "MIN";
462
475
  }
463
476
  const PLACEMENT_FIELDS = [
464
477
  "x",
@@ -634,7 +647,7 @@ function applyPenProperties(node, pen, build) {
634
647
  node.effects = normalizeEffects(convertEffects(pen.effect, ctx, build.shaders));
635
648
  applyCornerRadius(node, pen.cornerRadius, ctx);
636
649
  applyPadding(node, pen.padding, ctx);
637
- if (isTextLike) applyTextProps(node, pen, ctx);
650
+ if (isTextLike) applyTextProps(node, pen, ctx, build.fonts);
638
651
  if (pen.type === "icon") drawIconLayer(node, pen, build);
639
652
  if (pen.type === "ellipse") node.arcData = ellipseArc(pen, ctx);
640
653
  if (pen.type === "polygon") node.pointCount = Math.max(3, resolveNumber(pen.polygonCount, ctx, 3));
@@ -717,11 +730,16 @@ function sources(graph, node) {
717
730
  for (let id = node.componentId; id && !found.has(id); id = graph.getNode(id)?.componentId ?? null) found.add(id);
718
731
  return found;
719
732
  }
733
+ /** The pen.dev layer a layer was built from, which a component swap stops naming. */
734
+ function penLayerId(build, node) {
735
+ return build.effective.get(node.id)?.id;
736
+ }
720
737
  /** The copy in `root`'s layers made from the pen.dev layer `sourceId`, at any depth. */
721
- function findCopy(graph, root, sourceId) {
738
+ function findCopy(build, root, sourceId) {
739
+ const { graph } = build;
722
740
  const queue = graph.getChildren(root.id);
723
741
  for (const node of queue) {
724
- if (sources(graph, node).has(sourceId)) return node;
742
+ if (penLayerId(build, node) === sourceId || sources(graph, node).has(sourceId)) return node;
725
743
  queue.push(...graph.getChildren(node.id));
726
744
  }
727
745
  }
@@ -729,9 +747,9 @@ function findCopy(graph, root, sourceId) {
729
747
  * The layer an override's key names: pen.dev IDs separated by `/`, each one inside the copy of the
730
748
  * instance the one before it names.
731
749
  */
732
- function resolvePath(graph, instance, path) {
750
+ function resolvePath(build, instance, path) {
733
751
  let current = instance;
734
- for (const id of path.split("/")) current = current ? findCopy(graph, current, id) : void 0;
752
+ for (const id of path.split("/")) current = current ? findCopy(build, current, id) : void 0;
735
753
  return current;
736
754
  }
737
755
  /** Runs `apply` in the merged theme of a layer's ancestors, as the layer was read in. */
@@ -831,12 +849,14 @@ function overrideLayer(build, target, override, instance) {
831
849
  inAncestorThemes(build, target, () => {
832
850
  const leave = merged.theme ? ctx.enterTheme(merged.theme) : void 0;
833
851
  try {
834
- applyPenProperties(target, merged, build);
852
+ if (children) for (const childId of target.childIds) graph.deleteNode(childId);
853
+ applyPenProperties(target, children ? {
854
+ ...merged,
855
+ children
856
+ } : merged, build);
835
857
  recordChangedFields(graph, target, before, fields);
836
858
  if (children) {
837
859
  const fillsSlot = instance !== void 0 && claimSlot(build, instance, target);
838
- const replaced = target.childIds;
839
- for (const childId of replaced) graph.deleteNode(childId);
840
860
  for (const child of children) createSceneNode(child, target.id, build);
841
861
  if (!fillsSlot) build.replaced.add(target.id);
842
862
  }
@@ -861,7 +881,7 @@ function detachReplacingInstances(build) {
861
881
  /** Gives an instance's copies their overrides; a key with a `type` replaces the layer. */
862
882
  function applyDescendants(build, instance, descendants, populate) {
863
883
  for (const [path, override] of Object.entries(descendants ?? {})) {
864
- const target = resolvePath(build.graph, instance, path);
884
+ const target = resolvePath(build, instance, path);
865
885
  if (!target) continue;
866
886
  if (override.type) replaceLayer(build, target, override, populate);
867
887
  else overrideLayer(build, target, override, instance);
@@ -902,6 +922,7 @@ function populateInstances(build) {
902
922
  recordOwnFields(graph, instance, ref);
903
923
  inheritCopyFields(build, instance);
904
924
  applyDescendants(build, instance, ref.descendants, populate);
925
+ if (ref.children) overrideLayer(build, instance, { children: ref.children });
905
926
  detachReplacingInstances(build);
906
927
  }
907
928
  for (const id of instances.keys()) populate(id);
@@ -962,6 +983,7 @@ function buildPenGraph(doc, options = {}) {
962
983
  images: createImageStore(graph, options.images),
963
984
  shaders: options.shaders,
964
985
  icons: options.icons,
986
+ fonts: options.fonts ?? /* @__PURE__ */ new Map(),
965
987
  components: /* @__PURE__ */ new Map(),
966
988
  effective: /* @__PURE__ */ new Map(),
967
989
  instances: /* @__PURE__ */ new Map(),
@@ -1423,10 +1445,25 @@ function basicNodeFields(state, node, result) {
1423
1445
  if (node.rotation !== 0) result.rotation = rounded(node.flipX !== node.flipY ? node.rotation : -node.rotation);
1424
1446
  if (node.flipX) result.flipX = true;
1425
1447
  if (node.flipY) result.flipY = true;
1448
+ const theme = themeFields(state, node);
1449
+ if (theme) result.theme = theme;
1426
1450
  const fill = paintsOf(state, node, "fills", node.fills);
1427
1451
  if (fill !== void 0) result.fill = fill;
1428
1452
  Object.assign(result, strokesOf(state, node), layoutFields(state, node), cornerFields(node), textFields(state, node));
1429
1453
  }
1454
+ /**
1455
+ * A layer's variable mode assignments, by the names pen writes: the collection's name and the
1456
+ * mode's. A mode the document no longer holds is left out.
1457
+ */
1458
+ function themeFields(state, node) {
1459
+ const theme = {};
1460
+ for (const [collectionId, modeId] of Object.entries(node.variableModes)) {
1461
+ const collection = state.collections.get(collectionId);
1462
+ const mode = collection?.modes.find((candidate) => candidate.modeId === modeId);
1463
+ if (collection && mode) theme[collection.name] = mode.name;
1464
+ }
1465
+ return Object.keys(theme).length > 0 ? theme : void 0;
1466
+ }
1430
1467
  function semanticNodeFields(state, node, result) {
1431
1468
  if (node.type === "TEXT" && node.textAutoResize === "WIDTH_AND_HEIGHT" && layoutSizing(state.graph, node, "HORIZONTAL") === "HUG") result.width = "fit_content";
1432
1469
  if (node.type === "STICKY") result.content = stickySublayers(state.graph, node)?.body.text ?? "";
@@ -1448,6 +1485,19 @@ function writeNode(state, node) {
1448
1485
  };
1449
1486
  basicNodeFields(state, node, result);
1450
1487
  if (semanticNodeFields(state, node, result)) return result;
1488
+ if (node.type === "VECTOR" && node.vectorNetwork && node.vectorNetwork.segments.length > 0) {
1489
+ const geometry = networkToSVGPath(node.vectorNetwork);
1490
+ if (geometry) {
1491
+ result.geometry = geometry;
1492
+ result.viewBox = [
1493
+ 0,
1494
+ 0,
1495
+ rounded(node.width),
1496
+ rounded(node.height)
1497
+ ];
1498
+ if (node.vectorNetwork.regions[0]?.windingRule === "EVENODD") result.fillRule = "evenodd";
1499
+ }
1500
+ }
1451
1501
  const children = node.type === "STICKY" ? [] : state.graph.getChildren(node.id).filter((child) => !child.internalOnly).map((child) => writeNode(state, child));
1452
1502
  if (children.length > 0) result.children = children;
1453
1503
  return result;