@vectojs/core 1.7.1 → 1.9.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.
@@ -1,6 +1,8 @@
1
1
  import {
2
+ ArabicShaper,
3
+ BidiResolver,
2
4
  LayoutWorkerManager
3
- } from "./chunk-STWPTWO4.mjs";
5
+ } from "./chunk-IESDTEJ4.mjs";
4
6
 
5
7
  // src/math/SpringPhysics.ts
6
8
  var MAX_FRAME_DT = 0.25;
@@ -370,10 +372,18 @@ var Entity = class {
370
372
  * `child.parent` is only ever `null` or `this`'s ultimate owner, so this
371
373
  * check is O(1) for the overwhelming common case (a brand-new entity).
372
374
  *
373
- * @param child - The entity to add as a child.
375
+ * Accepts multiple children in one call (`parent.add(a, b, c)`); each is
376
+ * attached in argument order with the same detach-then-attach semantics.
377
+ *
378
+ * @param children - One or more entities to add as children.
374
379
  * @returns `this` for method chaining.
375
380
  */
376
- add(child) {
381
+ add(...children) {
382
+ for (const child of children) this._addOne(child);
383
+ return this;
384
+ }
385
+ /** Attach a single child (the O(1) common path). See {@link add}. */
386
+ _addOne(child) {
377
387
  if (child.parent) child.parent.remove(child);
378
388
  child.parent = this;
379
389
  this.children.push(child);
@@ -383,7 +393,6 @@ var Entity = class {
383
393
  s.markDirty();
384
394
  child._notifyMounted();
385
395
  }
386
- return this;
387
396
  }
388
397
  /** Called once when this entity becomes attached to a live Scene. Override to react. */
389
398
  onMounted() {
@@ -415,6 +424,24 @@ var Entity = class {
415
424
  }
416
425
  return this;
417
426
  }
427
+ /**
428
+ * Assign several own properties in one call, each through its normal setter
429
+ * (so a property with a configured {@link setTransition} still animates, and
430
+ * `interactive` still flags the scene for a11y reorder). A construction-time
431
+ * ergonomic only — it is a plain `for…in` over the given object and touches
432
+ * no per-frame path.
433
+ *
434
+ * @param props - Partial set of this entity's own properties to assign.
435
+ * @returns `this` for method chaining.
436
+ * @example rect.set({ x: 40, y: 40, width: 120, fill: '#38bdf8' });
437
+ */
438
+ set(props) {
439
+ for (const key in props) {
440
+ const value = props[key];
441
+ if (value !== void 0) this[key] = value;
442
+ }
443
+ return this;
444
+ }
418
445
  /**
419
446
  * Set the local position of this entity.
420
447
  *
@@ -1459,6 +1486,205 @@ var SVGEntity = class extends Entity {
1459
1486
  }
1460
1487
  };
1461
1488
 
1489
+ // src/text/PreparedContentGrid.ts
1490
+ var nextRevision = 1;
1491
+ var graphemeSegmenter = typeof Intl !== "undefined" && "Segmenter" in Intl ? new Intl.Segmenter(void 0, { granularity: "grapheme" }) : null;
1492
+ var MARK = /\p{Mark}/u;
1493
+ var EXTENDED_PICTOGRAPHIC = /\p{Extended_Pictographic}/u;
1494
+ var REGIONAL_INDICATOR = /\p{Regional_Indicator}/u;
1495
+ var BIDI_CONTROL = /\p{Bidi_Control}/u;
1496
+ var EAST_ASIAN_WIDE = /[ᄀ-ᅟ⌚-⌛⏩-⏬⏰⏳◽-◾⺀-〾ぁ-㏿㐀-䶿一-鿿ꀀ-꓏가-힣豈-﫿︰-﹏＀-⦆¢-₩]/u;
1497
+ function codePointAt(text, index) {
1498
+ const point = text.codePointAt(index);
1499
+ if (point === void 0) return { value: "", next: index };
1500
+ const value = String.fromCodePoint(point);
1501
+ return { value, next: index + value.length };
1502
+ }
1503
+ function fallbackGraphemes(text) {
1504
+ const parts = [];
1505
+ let index = 0;
1506
+ while (index < text.length) {
1507
+ const start = index;
1508
+ let current = codePointAt(text, index);
1509
+ let segment = current.value;
1510
+ index = current.next;
1511
+ let regionalCount = REGIONAL_INDICATOR.test(segment) ? 1 : 0;
1512
+ while (index < text.length) {
1513
+ current = codePointAt(text, index);
1514
+ const point = current.value.codePointAt(0) ?? 0;
1515
+ const isVariation = point >= 65024 && point <= 65039;
1516
+ const isEmojiModifier = point >= 127995 && point <= 127999;
1517
+ const isKeycap = point === 8419;
1518
+ if (MARK.test(current.value) || isVariation || isEmojiModifier || isKeycap) {
1519
+ segment += current.value;
1520
+ index = current.next;
1521
+ continue;
1522
+ }
1523
+ if (REGIONAL_INDICATOR.test(current.value) && regionalCount === 1) {
1524
+ segment += current.value;
1525
+ index = current.next;
1526
+ regionalCount++;
1527
+ continue;
1528
+ }
1529
+ if (point === 8205) {
1530
+ segment += current.value;
1531
+ index = current.next;
1532
+ if (index < text.length) {
1533
+ current = codePointAt(text, index);
1534
+ segment += current.value;
1535
+ index = current.next;
1536
+ }
1537
+ continue;
1538
+ }
1539
+ break;
1540
+ }
1541
+ parts.push({ segment, index: start });
1542
+ }
1543
+ return parts;
1544
+ }
1545
+ function graphemes(text) {
1546
+ if (!graphemeSegmenter) return fallbackGraphemes(text);
1547
+ return Array.from(graphemeSegmenter.segment(text), (part) => ({
1548
+ segment: part.segment,
1549
+ index: part.index
1550
+ }));
1551
+ }
1552
+ function lowerBound(values, target) {
1553
+ let low = 0;
1554
+ let high = values.length;
1555
+ while (low < high) {
1556
+ const middle = low + high >>> 1;
1557
+ if (values[middle] < target) low = middle + 1;
1558
+ else high = middle;
1559
+ }
1560
+ return low;
1561
+ }
1562
+ function isWideCluster(cluster) {
1563
+ if (EXTENDED_PICTOGRAPHIC.test(cluster) || REGIONAL_INDICATOR.test(cluster)) return true;
1564
+ if (cluster.includes("\u20E3")) return true;
1565
+ if (EAST_ASIAN_WIDE.test(cluster)) return true;
1566
+ const point = cluster.codePointAt(0) ?? 0;
1567
+ return point >= 131072 && point <= 262141;
1568
+ }
1569
+ function sourceLines(source) {
1570
+ const lines = [];
1571
+ let start = 0;
1572
+ while (true) {
1573
+ let end = start;
1574
+ while (end < source.length && source[end] !== "\r" && source[end] !== "\n") end++;
1575
+ if (end === source.length) {
1576
+ lines.push({
1577
+ sourceStart: start,
1578
+ sourceEnd: end,
1579
+ nextSourceStart: end,
1580
+ text: source.slice(start)
1581
+ });
1582
+ break;
1583
+ }
1584
+ const next = source[end] === "\r" && source[end + 1] === "\n" ? end + 2 : end + 1;
1585
+ lines.push({
1586
+ sourceStart: start,
1587
+ sourceEnd: end,
1588
+ nextSourceStart: next,
1589
+ text: source.slice(start, end)
1590
+ });
1591
+ start = next;
1592
+ if (start === source.length) {
1593
+ lines.push({ sourceStart: start, sourceEnd: start, nextSourceStart: start, text: "" });
1594
+ break;
1595
+ }
1596
+ }
1597
+ return lines;
1598
+ }
1599
+ function assertPositiveFinite(value, name) {
1600
+ if (!Number.isFinite(value) || value <= 0) {
1601
+ throw new RangeError(`${name} must be a positive finite number`);
1602
+ }
1603
+ }
1604
+ function prepareContentGrid(source, options) {
1605
+ assertPositiveFinite(options.cellWidth, "cellWidth");
1606
+ assertPositiveFinite(options.lineHeight, "lineHeight");
1607
+ if (!Number.isFinite(options.baseline)) throw new RangeError("baseline must be finite");
1608
+ const tabSize = options.tabSize ?? 4;
1609
+ if (!Number.isInteger(tabSize) || tabSize <= 0) {
1610
+ throw new RangeError("tabSize must be a positive integer");
1611
+ }
1612
+ const rawLines = sourceLines(source);
1613
+ const lines = [];
1614
+ for (let lineIndex = 0; lineIndex < rawLines.length; lineIndex++) {
1615
+ const sourceLine = rawLines[lineIndex];
1616
+ const rawLine = sourceLine.text;
1617
+ const { sourceStart: lineStart, sourceEnd, nextSourceStart } = sourceLine;
1618
+ const rawCaretBoundaries = [
1619
+ 0,
1620
+ ...graphemes(rawLine).map((grapheme) => grapheme.index + grapheme.segment.length)
1621
+ ];
1622
+ const shaped = ArabicShaper.shapeArabic(rawLine);
1623
+ const shapedParts = graphemes(shaped.shapedText);
1624
+ const levels = BidiResolver.resolveLevels(shaped.shapedText);
1625
+ const cells = [];
1626
+ let column = 0;
1627
+ for (let index = 0; index < shapedParts.length; index++) {
1628
+ const part = shapedParts[index];
1629
+ const sourceOffset = shaped.indexMap[part.index] ?? part.index;
1630
+ const nextPart = shapedParts[index + 1];
1631
+ const sourceOffsetEnd = nextPart ? shaped.indexMap[nextPart.index] ?? nextPart.index : rawLine.length;
1632
+ const raw = rawLine.slice(sourceOffset, sourceOffsetEnd);
1633
+ const sourceCaretOffsets = [0];
1634
+ for (let caretIndex = lowerBound(rawCaretBoundaries, sourceOffset + 1); caretIndex < rawCaretBoundaries.length && rawCaretBoundaries[caretIndex] < sourceOffsetEnd; caretIndex++) {
1635
+ sourceCaretOffsets.push(rawCaretBoundaries[caretIndex] - sourceOffset);
1636
+ }
1637
+ if (sourceCaretOffsets.at(-1) !== sourceOffsetEnd - sourceOffset) {
1638
+ sourceCaretOffsets.push(sourceOffsetEnd - sourceOffset);
1639
+ }
1640
+ let columns;
1641
+ if (BIDI_CONTROL.test(raw)) columns = 0;
1642
+ else if (raw === " ") columns = tabSize - column % tabSize;
1643
+ else columns = isWideCluster(raw) ? 2 : 1;
1644
+ const advance = columns * options.cellWidth;
1645
+ cells.push({
1646
+ sourceStart: lineStart + sourceOffset,
1647
+ sourceEnd: lineStart + sourceOffsetEnd,
1648
+ sourceCaretOffsets: Object.freeze(sourceCaretOffsets),
1649
+ glyph: part.segment,
1650
+ x: 0,
1651
+ advance,
1652
+ level: levels[part.index] ?? 0,
1653
+ char: part.segment
1654
+ });
1655
+ column += columns;
1656
+ }
1657
+ const visualCells = [...cells];
1658
+ BidiResolver.reorderVisual(visualCells, BidiResolver.getBaseLevel(shaped.shapedText));
1659
+ let visualX = 0;
1660
+ for (const cell of visualCells) {
1661
+ cell.x = visualX;
1662
+ visualX += cell.advance;
1663
+ }
1664
+ const frozenCells = cells.map(({ char: _char, ...cell }) => Object.freeze(cell));
1665
+ lines.push(
1666
+ Object.freeze({
1667
+ sourceStart: lineStart,
1668
+ sourceEnd,
1669
+ nextSourceStart,
1670
+ width: visualX,
1671
+ cells: Object.freeze(frozenCells)
1672
+ })
1673
+ );
1674
+ }
1675
+ return Object.freeze({
1676
+ kind: "content-grid",
1677
+ revision: nextRevision++,
1678
+ source,
1679
+ font: options.font,
1680
+ cellWidth: options.cellWidth,
1681
+ lineHeight: options.lineHeight,
1682
+ baseline: options.baseline,
1683
+ tabSize,
1684
+ lines: Object.freeze(lines)
1685
+ });
1686
+ }
1687
+
1462
1688
  export {
1463
1689
  SpringPhysics,
1464
1690
  Easing,
@@ -1471,5 +1697,6 @@ export {
1471
1697
  clearCssLineBoxMetrics,
1472
1698
  MSDFFont,
1473
1699
  MSDFTextEntity,
1474
- SVGEntity
1700
+ SVGEntity,
1701
+ prepareContentGrid
1475
1702
  };
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  ArabicShaper,
3
3
  BidiResolver
4
- } from "./chunk-STWPTWO4.mjs";
4
+ } from "./chunk-IESDTEJ4.mjs";
5
5
 
6
6
  // src/layout/LayoutEngine.ts
7
7
  function computeLineSegments(top, bottom, maxWidth, exclusions) {
@@ -0,0 +1,48 @@
1
+ import { Entity, type Bounds, type BatchCircle } from '../tree/Entity';
2
+ import type { IRenderer } from '../renderer/IRenderer';
3
+ /** Construction options for {@link Circle}. */
4
+ export interface CircleOptions {
5
+ /** Radius in local units. Default `0`. */
6
+ radius?: number;
7
+ /** CSS fill color, or `null` for no fill. Default `'#38bdf8'`. */
8
+ fill?: string | null;
9
+ /** CSS stroke color, or `null` for no stroke. Default `null`. */
10
+ stroke?: string | null;
11
+ /** Stroke width in local units. Default `1`. */
12
+ strokeWidth?: number;
13
+ }
14
+ /**
15
+ * A concrete circle primitive centered on its local origin `(0, 0)`.
16
+ * Instantiate directly — no subclassing:
17
+ *
18
+ * @example
19
+ * const dot = new Circle({ radius: 24, fill: '#f97316' });
20
+ * dot.set({ x: 100, y: 100 });
21
+ * scene.add(dot);
22
+ *
23
+ * The accessibility shadow box is sized to the circle's bounding square and
24
+ * offset by `-radius` so it covers the drawn disc (whose center is the entity
25
+ * origin). A solid-fill (unstroked) Circle opts into the point-batch fast path
26
+ * via {@link getBatchCircle}; a stroked circle renders through the normal path.
27
+ */
28
+ export declare class Circle extends Entity {
29
+ fill: string | null;
30
+ stroke: string | null;
31
+ strokeWidth: number;
32
+ private _radius;
33
+ constructor(opts?: CircleOptions);
34
+ get radius(): number;
35
+ set radius(v: number);
36
+ /** Keep the a11y box (a square around the centered disc) in sync with radius. */
37
+ private syncBox;
38
+ getBounds(): Bounds;
39
+ /**
40
+ * A solid-fill, unstroked circle opts into the renderer's circle batch
41
+ * (center = entity origin, radius scaled by world scale). A stroke needs the
42
+ * exact Canvas path, so return `null` there. Read each frame, so an animated
43
+ * `fill`/`radius` still batches.
44
+ */
45
+ getBatchCircle(): BatchCircle | null;
46
+ isPointInside(globalX: number, globalY: number): boolean;
47
+ render(renderer: IRenderer): void;
48
+ }
@@ -0,0 +1,24 @@
1
+ import { Entity } from '../tree/Entity';
2
+ import type { IRenderer } from '../renderer/IRenderer';
3
+ /**
4
+ * A transform-only container: it draws nothing itself and is invisible to
5
+ * hit-testing, existing solely to compose one transform (`x`/`y`/`scale`/
6
+ * `rotation`/`opacity`) onto a set of children. Instantiate directly and pass
7
+ * children inline:
8
+ *
9
+ * @example
10
+ * const toolbar = new Group(saveBtn, undoBtn, redoBtn);
11
+ * toolbar.set({ x: 20, y: 20 });
12
+ * scene.add(toolbar);
13
+ *
14
+ * `isPointInside` returns `false` so the group never becomes the pick target;
15
+ * `Scene`'s hit-test recurses into children before testing a parent, so the
16
+ * children remain independently interactive. `render` is a no-op — children are
17
+ * drawn by the scene's normal tree walk under this group's accumulated
18
+ * transform.
19
+ */
20
+ export declare class Group extends Entity {
21
+ constructor(...children: Entity[]);
22
+ isPointInside(): boolean;
23
+ render(_renderer: IRenderer): void;
24
+ }
@@ -0,0 +1,48 @@
1
+ import { Entity, type Bounds, type BatchRect } from '../tree/Entity';
2
+ import type { IRenderer } from '../renderer/IRenderer';
3
+ /** Construction options for {@link Rect}. */
4
+ export interface RectOptions {
5
+ /** Width in local units. Default `0`. */
6
+ width?: number;
7
+ /** Height in local units. Default `0`. */
8
+ height?: number;
9
+ /** CSS fill color, or `null` for no fill. Default `'#38bdf8'`. */
10
+ fill?: string | null;
11
+ /** CSS stroke color, or `null` for no stroke. Default `null`. */
12
+ stroke?: string | null;
13
+ /** Stroke width in local units. Default `1`. */
14
+ strokeWidth?: number;
15
+ /** Corner radius in local units (uniform). Default `0` (sharp corners). */
16
+ radius?: number;
17
+ }
18
+ /**
19
+ * A concrete axis-aligned rectangle primitive drawn from its local origin
20
+ * `(0, 0)` to `(width, height)`. Instantiate directly — no subclassing:
21
+ *
22
+ * @example
23
+ * const box = new Rect({ width: 120, height: 64, fill: '#38bdf8', radius: 8 });
24
+ * box.set({ x: 40, y: 40 });
25
+ * scene.add(box);
26
+ *
27
+ * The box matches the entity's `width`/`height`, so its accessibility shadow
28
+ * node lines up with what's drawn. A solid-fill, non-rounded, unstroked Rect
29
+ * opts into the WebGL instanced-rectangle fast path via {@link getBatchRect};
30
+ * rounded or stroked rectangles render through the normal Canvas path.
31
+ */
32
+ export declare class Rect extends Entity {
33
+ fill: string | null;
34
+ stroke: string | null;
35
+ strokeWidth: number;
36
+ radius: number;
37
+ constructor(opts?: RectOptions);
38
+ getBounds(): Bounds;
39
+ /**
40
+ * Solid-fill, square-cornered, unstroked rectangles opt into the GPU
41
+ * instanced-rect batch (WebGL `pointBackend` only). Any stroke or corner
42
+ * radius needs the exact Canvas path, so return `null` to fall back to
43
+ * {@link render}. Read each frame, so an animated `fill` still batches.
44
+ */
45
+ getBatchRect(): BatchRect | null;
46
+ isPointInside(globalX: number, globalY: number): boolean;
47
+ render(renderer: IRenderer): void;
48
+ }
package/dist/index.d.ts CHANGED
@@ -10,6 +10,9 @@ export * from './tree/Scene';
10
10
  export * from './components/TextEntity';
11
11
  export * from './components/GridTextEntity';
12
12
  export * from './components/SplineEntity';
13
+ export * from './components/Rect';
14
+ export * from './components/Circle';
15
+ export * from './components/Group';
13
16
  export * from './layout/LayoutEngine';
14
17
  export * from './layout/measure';
15
18
  export * from './text/MSDFFont';
@@ -25,4 +28,5 @@ export { SVGEntity } from './text/SVGEntity';
25
28
  export * from './tree/ComputeParticleEntity';
26
29
  export * from './text/ArabicShaper';
27
30
  export * from './text/BidiResolver';
31
+ export * from './text/PreparedContentGrid';
28
32
  export * from './text/Typography';