@mark1russell7/layout-contract 0.9.3 → 0.9.5

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.
@@ -32,6 +32,12 @@ declare function toSizedRect(rect: Rect): SizedRect;
32
32
  * rectangles that touch still touch after the rounding, and no gap or overlap of one pixel comes between them.
33
33
  */
34
34
  declare function snapRect(rect: Rect): Rect;
35
+ /**
36
+ * This function gives a rectangle with each coordinate and each size times `factor`. It changes the unit of a
37
+ * rectangle, for example from the CSS pixels of a zoomed page to the pixels of its window. It does not round:
38
+ * `snapRect` comes after it. A factor that is negative or not finite gives a `RangeError`.
39
+ */
40
+ declare function scaleRect(rect: Rect, factor: number): Rect;
35
41
  /** A value for each side of a box. */
36
42
  interface Sides {
37
43
  readonly top: number;
@@ -51,8 +57,8 @@ type Region = {
51
57
  readonly h: number;
52
58
  } | {
53
59
  readonly kind: "polygon";
54
- readonly points: readonly (readonly [number, number])[];
55
- readonly holes?: readonly (readonly (readonly [number, number])[])[];
60
+ readonly points: Ring;
61
+ readonly holes?: readonly Ring[];
56
62
  } | {
57
63
  readonly kind: "circle";
58
64
  readonly cx: number;
@@ -91,15 +97,28 @@ declare function overlapOf(a: Rect, b: Rect): {
91
97
  /**
92
98
  * This function tells if the segments `ab` and `cd` cross at one point inside both of them.
93
99
  * Segments that only touch at an end, or that are collinear, do not cross. The test has no tolerance.
100
+ * The signs are exact, so an input that is almost collinear gets the correct answer. A coordinate that is not finite gives `false`.
94
101
  */
95
102
  declare function segmentsCross(a: Point, b: Point, c: Point, d: Point): boolean;
96
103
  /**
97
104
  * This function gives the point where the segments `ab` and `cd` cross, or `undefined`.
98
105
  * It gives a point exactly when `segmentsCross` is true, so a touch at an end and collinear segments give `undefined`.
106
+ * The point comes from float cross products. For an input that is almost collinear, the point stays on `ab`.
99
107
  */
100
108
  declare function crossingPoint(a: Point, b: Point, c: Point, d: Point): Point | undefined;
101
109
  /** This function gives the shortest distance from `p` to the segment `ab`. A segment of length 0 is the point `a`. */
102
110
  declare function pointSegmentDistance(p: Point, a: Point, b: Point): number;
111
+ /** A ring of a polygon: its corners in order, as `[x, y]` pairs. The last corner connects to the first. */
112
+ type Ring = readonly (readonly [number, number])[];
113
+ /** This function tells if the point (`x`, `y`) is inside a ring, by the even-odd rule. A ring can be concave. The answer for a point on the border can be each value. */
114
+ declare function pointInRing(x: number, y: number, ring: Ring): boolean;
115
+ /**
116
+ * This function tells if the point (`x`, `y`) is inside a set of rings, by the even-odd rule over all their sides.
117
+ * Thus a point inside the outer ring and inside a hole is out. The answer for a point on a border can be each value.
118
+ */
119
+ declare function pointInRings(x: number, y: number, rings: readonly Ring[]): boolean;
120
+ /** This function gives the distance between the centers of two rectangles. */
121
+ declare function boxCenterDistance(a: Rect, b: Rect): number;
103
122
  /**
104
123
  * This function gives the index at which a point goes among the items of a list on the screen.
105
124
  * The reading order is the rows from top to bottom, and each row from left to right.
@@ -651,5 +670,5 @@ declare function minEffectOf(spec: Pick<MeasureSpec, "minEffect">, a?: number, b
651
670
  * Two equal values are a tie, also two infinite values. A value `NaN` is an error.
652
671
  */
653
672
  declare function compareMeasure(candidate: number, baseline: number, spec: Pick<MeasureSpec, "direction" | "minEffect">): Verdict;
654
- export { CorpusSplit as $, shapeKindsOf as A, overlapOf as At, LayoutInput as B, checkShapes as C, Sides as Ct, ruleLevel as D, dropIndex as Dt, itemShapeOf as E, crossingPoint as Et, LayoutBox as F, toRect as Ft, PairGap as G, PackInput as H, LayoutCell as I, toSizedRect as It, packLinks as J, Port as K, LayoutContainer as L, translate as Lt, shapeSupportProblems as M, regionArea as Mt, shapesOf as N, segmentsCross as Nt, shapeKindOf as O, grow as Ot, Item as P, snapRect as Pt, CorpusEntry as Q, LayoutEdge as R, boxArea as S, Side as St, isShapeKind as T, boundsOf as Tt, PackInputOptions as U, Obstacle as V, PackLink as W, toPackInput as X, portPoint as Y, ClaimBreak as Z, ShapeKind as _, Margin as _t, MeasureSpec as a, SPLIT_SCHEMA as at, ShapeLimit as b, Region as bt, minEffectOf as c, checkDeclared as ct, SHAPE_KINDS as d, declaredClaimOf as dt, DeclaredClaim as et, SHAPE_KIND_IDS as f, declaredCounts as ft, ShapeInput as g, splitFolds as gt, ShapeGeometry as h, splitFileName as ht, JointMeasureId as i, FamilyCell as it, shapeLevel as j, pointSegmentDistance as jt, shapeKindProblem as k, marginSides as kt, DEFAULT_SHAPES as l, checkSplit as lt, ShapeCheck as m, leaveOneSeedOut as mt, JOINT_MEASURES as n, FAMILY_ANY_KIND as nt, Verdict as o, SplitCorpus as ot, ShapeBound as p, familyOf as pt, canonicalInput as q, JOINT_MOVED_MIN as r, FamilyBy as rt, compareMeasure as s, SplitFold as st, Direction as t, Declares as tt, RuleLevel as u, corpusHashOf as ut, ShapeKindSpec as v, Point as vt, inputShapeLevel as w, SizedRect as wt, ShapeSupport as x, SIDES as xt, ShapeLevel as y, Rect as yt, LayoutGenerator as z };
655
- //# sourceMappingURL=measures-O-NfAbmJ.d.ts.map
673
+ export { CorpusSplit as $, shapeKindsOf as A, grow as At, LayoutInput as B, toRect as Bt, checkShapes as C, Side as Ct, ruleLevel as D, boxCenterDistance as Dt, itemShapeOf as E, boundsOf as Et, LayoutBox as F, pointSegmentDistance as Ft, PairGap as G, PackInput as H, translate as Ht, LayoutCell as I, regionArea as It, packLinks as J, Port as K, LayoutContainer as L, scaleRect as Lt, shapeSupportProblems as M, overlapOf as Mt, shapesOf as N, pointInRing as Nt, shapeKindOf as O, crossingPoint as Ot, Item as P, pointInRings as Pt, CorpusEntry as Q, LayoutEdge as R, segmentsCross as Rt, boxArea as S, SIDES as St, isShapeKind as T, SizedRect as Tt, PackInputOptions as U, Obstacle as V, toSizedRect as Vt, PackLink as W, toPackInput as X, portPoint as Y, ClaimBreak as Z, ShapeKind as _, Margin as _t, MeasureSpec as a, SPLIT_SCHEMA as at, ShapeLimit as b, Region as bt, minEffectOf as c, checkDeclared as ct, SHAPE_KINDS as d, declaredClaimOf as dt, DeclaredClaim as et, SHAPE_KIND_IDS as f, declaredCounts as ft, ShapeInput as g, splitFolds as gt, ShapeGeometry as h, splitFileName as ht, JointMeasureId as i, FamilyCell as it, shapeLevel as j, marginSides as jt, shapeKindProblem as k, dropIndex as kt, DEFAULT_SHAPES as l, checkSplit as lt, ShapeCheck as m, leaveOneSeedOut as mt, JOINT_MEASURES as n, FAMILY_ANY_KIND as nt, Verdict as o, SplitCorpus as ot, ShapeBound as p, familyOf as pt, canonicalInput as q, JOINT_MOVED_MIN as r, FamilyBy as rt, compareMeasure as s, SplitFold as st, Direction as t, Declares as tt, RuleLevel as u, corpusHashOf as ut, ShapeKindSpec as v, Point as vt, inputShapeLevel as w, Sides as wt, ShapeSupport as x, Ring as xt, ShapeLevel as y, Rect as yt, LayoutGenerator as z, snapRect as zt };
674
+ //# sourceMappingURL=measures-D5hOVn9j.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"measures-D5hOVn9j.d.ts","names":[],"sources":["../src/geometry.ts","../src/corpus.ts","../src/input.ts","../src/shapes.ts","../src/measures.ts"],"mappings":";KAGY;;cAGC,gBAAgB;UAEZ;WACN;WACA;;;UAIM;WACN;WACA;WACA;WACA;;;;;;UAOM;WACN;WACA;WACA;WACA;;;iBAIK,OAAO,MAAM,YAAY;;iBAKzB,YAAY,MAAM,OAAO;;;;;iBAWzB,SAAS,MAAM,OAAO;;;;;;iBAWtB,UAAU,MAAM,MAAM,iBAAiB;;UAMtC;WACN;WACA;WACA;WACA;;;;;;KAOC;WACG;WAAuB;WAAoB;WAAoB;WAAoB;;WAEnF;WACA,QAAQ;WACR,iBAAiB;;WAEjB;WAAyB;WAAqB;WAAqB;;WACnE;WAA0B;WAAqB;WAAqB;WAAqB;;;KAG5F,kBAAkB;;iBAGd,YAAY,QAAQ,qBAAqB;;iBAOzC,KAAK,MAAM,MAAM,QAAQ,qBAAqB;;iBAM9C,SAAS,QAAQ,SAAS;;;;;iBA4B1B,WAAW,QAAQ;;iBAyBnB,UAAU,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AQ;;WAER,QAAQ;;;;;;;;UASF;WACN;;WAEA;;WAEA;;;;;;;;;;;;;;;;;;;;;UAsBM;WACN,iBAAiB;WACjB;;;KAIC;;iBAGI,UAAU,MAAM,KAAK,kCAAkC,MAAM,YAAY;;KAM7E,aAAa;;;;;iBAMT,WAAW,KAAK,KAAK,4CAA4C,MAAM,YAAY;;KAOvF,aAAa,KAAK,+BAA+B,QAAQ,KAAK;;;;;iBAe1D,gBAAgB,KAAK,KAAK,sDAAsD,MAAM,WAAW,OAAO,aAAa;;;;;;;iBAWrH,qBAAqB,eAAe;;iBAwCpC,YAAY,iBAAiB,SAAS;;;;;iBAQtC,YAAY,KAAK,YAAY;;;;;;;iBAW7B,QAAQ,KAAK;;iBAQb,aAAa,OAAO,KAAK,wBAAwB;;iBAMjD,YAAY,OAAO,KAAK,wBAAwB;;iBAMhD,iBAAiB,KAAK;;iBAWtB,SAAS,KAAK,KAAK,mCAAmC;;UAKrD;;;;;;WAMN;;;;;;;;iBASK,YAAY,OAAO,YAAY,eAAe,gBAAgB,UAAS,aAAkB;;KCtQ7F;;;;;;UAOK,YAAY;WAClB,IAAI;WACJ;WACA,WAAW;;WAEX;;WAEA;;WAEA;;WAEA;;WAEA;;;;;WAKA;aAAgC;;;WAEhC;;WAEA,kBAAkB;;;;;WAKlB,QAAQ;;;KAIP;;;;;cAqBC,yBAAyB,YAAY;;cAcrC;;KAGD;;;;;;iBAOI,YAAY,MAAM,KAAK,2BAA2B,YAAO;;;;;;;iBAezD,eAAe,mBAAmB,kBAAkB,MAAM,KAAK,0CAA0C"}
package/build/text.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as MeasureSpec } from "./measures-O-NfAbmJ.js";
1
+ import { a as MeasureSpec } from "./measures-D5hOVn9j.js";
2
2
  /**
3
3
  * The advance function of a host: the width of a run of text in a font, in pixels.
4
4
  * A browser host gives `canvasAdvance(context)`. A test or a server host gives a function of a font file.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/layout-contract",
4
- "version": "0.9.3",
4
+ "version": "0.9.5",
5
5
  "description": "The shared contract of the packing and highways layout engines: the input and record formats, the turn rule, margins, tolerances and hard rules.",
6
6
  "keywords": [
7
7
  "layout",
@@ -60,6 +60,7 @@
60
60
  "vitest": "^5.0.3"
61
61
  },
62
62
  "dependencies": {
63
+ "@mark1russell7/predicates": "^0.1.1",
63
64
  "@mark1russell7/rigor-generators": "^0.1.8",
64
65
  "@mark1russell7/rigor-statistics": "^0.1.23",
65
66
  "@mark1russell7/strategy-space": "^0.0.2",
package/src/frame.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  // The frames of a layout change (`docs/FRAMES.md`): the record `layout-frame/1`, the frame at a time between two layouts, and the move between two frames.
2
2
  // A frame is a layout at one time of a change. A caller can stop at a frame and start the next layout from it.
3
3
  import type { PlacedBox } from "./balance.ts";
4
+ import { boxCenterDistance } from "./geometry.ts";
4
5
  import type { Placement } from "./output.ts";
5
6
 
6
7
  /** The schema of a frame. */
@@ -141,7 +142,7 @@ export function frameMove(a: LayoutFrame, b: LayoutFrame): number {
141
142
  for (const q of b.boxes) {
142
143
  const p = before.get(q.id);
143
144
  if (p === undefined) continue;
144
- sum += Math.hypot(q.x + q.w / 2 - (p.x + p.w / 2), q.y + q.h / 2 - (p.y + p.h / 2));
145
+ sum += boxCenterDistance(p, q);
145
146
  }
146
147
  return sum;
147
148
  }
package/src/geometry.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { orient2dSign } from "@mark1russell7/predicates";
2
+
1
3
  /** A side of a box. The y axis points down, so `top` has the smallest y. */
2
4
  export type Side = "top" | "right" | "bottom" | "left";
3
5
 
@@ -51,6 +53,16 @@ export function snapRect(rect: Rect): Rect {
51
53
  return { x, y, w: roundEdge(rect.x + rect.w) - x, h: roundEdge(rect.y + rect.h) - y };
52
54
  }
53
55
 
56
+ /**
57
+ * This function gives a rectangle with each coordinate and each size times `factor`. It changes the unit of a
58
+ * rectangle, for example from the CSS pixels of a zoomed page to the pixels of its window. It does not round:
59
+ * `snapRect` comes after it. A factor that is negative or not finite gives a `RangeError`.
60
+ */
61
+ export function scaleRect(rect: Rect, factor: number): Rect {
62
+ if (!Number.isFinite(factor) || factor < 0) throw new RangeError(`The factor of scaleRect must be a finite number of 0 or more, not ${factor}.`);
63
+ return { x: rect.x * factor, y: rect.y * factor, w: rect.w * factor, h: rect.h * factor };
64
+ }
65
+
54
66
  /** A value for each side of a box. */
55
67
  export interface Sides {
56
68
  readonly top: number;
@@ -67,8 +79,8 @@ export type Region =
67
79
  | { readonly kind: "rect"; readonly x: number; readonly y: number; readonly w: number; readonly h: number }
68
80
  | {
69
81
  readonly kind: "polygon";
70
- readonly points: readonly (readonly [number, number])[];
71
- readonly holes?: readonly (readonly (readonly [number, number])[])[];
82
+ readonly points: Ring;
83
+ readonly holes?: readonly Ring[];
72
84
  }
73
85
  | { readonly kind: "circle"; readonly cx: number; readonly cy: number; readonly r: number }
74
86
  | { readonly kind: "ellipse"; readonly cx: number; readonly cy: number; readonly rx: number; readonly ry: number };
@@ -175,27 +187,36 @@ function orient(a: Point, b: Point, c: Point): number {
175
187
  return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
176
188
  }
177
189
 
178
- /** This function tells if two numbers have opposite signs. Zero and NaN give false, so a touch at an end does not count. */
179
- function opposite(p: number, q: number): boolean {
180
- return (p < 0 && q > 0) || (p > 0 && q < 0);
190
+ /** This function gives the exact sign of the cross product of `ab` and `ac`, from `orient2dSign` of `@mark1russell7/predicates`. */
191
+ function side(a: Point, b: Point, c: Point): number {
192
+ return orient2dSign(a.x, a.y, b.x, b.y, c.x, c.y);
193
+ }
194
+
195
+ /** This function tells if each coordinate of the points is finite. */
196
+ function allFinite(...points: readonly Point[]): boolean {
197
+ return points.every((p) => Number.isFinite(p.x) && Number.isFinite(p.y));
181
198
  }
182
199
 
183
200
  /**
184
201
  * This function tells if the segments `ab` and `cd` cross at one point inside both of them.
185
202
  * Segments that only touch at an end, or that are collinear, do not cross. The test has no tolerance.
203
+ * The signs are exact, so an input that is almost collinear gets the correct answer. A coordinate that is not finite gives `false`.
186
204
  */
187
205
  export function segmentsCross(a: Point, b: Point, c: Point, d: Point): boolean {
188
- return opposite(orient(c, d, a), orient(c, d, b)) && opposite(orient(a, b, c), orient(a, b, d));
206
+ if (!allFinite(a, b, c, d)) return false;
207
+ return side(c, d, a) * side(c, d, b) === -1 && side(a, b, c) * side(a, b, d) === -1;
189
208
  }
190
209
 
191
210
  /**
192
211
  * This function gives the point where the segments `ab` and `cd` cross, or `undefined`.
193
212
  * It gives a point exactly when `segmentsCross` is true, so a touch at an end and collinear segments give `undefined`.
213
+ * The point comes from float cross products. For an input that is almost collinear, the point stays on `ab`.
194
214
  */
195
215
  export function crossingPoint(a: Point, b: Point, c: Point, d: Point): Point | undefined {
196
216
  if (!segmentsCross(a, b, c, d)) return undefined;
197
217
  const from = orient(c, d, a);
198
- const t = from / (from - orient(c, d, b));
218
+ const r = from / (from - orient(c, d, b));
219
+ const t = Number.isNaN(r) ? 0.5 : Math.min(1, Math.max(0, r));
199
220
  return { x: a.x + t * (b.x - a.x), y: a.y + t * (b.y - a.y) };
200
221
  }
201
222
 
@@ -208,6 +229,35 @@ export function pointSegmentDistance(p: Point, a: Point, b: Point): number {
208
229
  return Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy));
209
230
  }
210
231
 
232
+ /** A ring of a polygon: its corners in order, as `[x, y]` pairs. The last corner connects to the first. */
233
+ export type Ring = readonly (readonly [number, number])[];
234
+
235
+ /** This function tells if the point (`x`, `y`) is inside a ring, by the even-odd rule. A ring can be concave. The answer for a point on the border can be each value. */
236
+ export function pointInRing(x: number, y: number, ring: Ring): boolean {
237
+ return pointInRings(x, y, [ring]);
238
+ }
239
+
240
+ /**
241
+ * This function tells if the point (`x`, `y`) is inside a set of rings, by the even-odd rule over all their sides.
242
+ * Thus a point inside the outer ring and inside a hole is out. The answer for a point on a border can be each value.
243
+ */
244
+ export function pointInRings(x: number, y: number, rings: readonly Ring[]): boolean {
245
+ let inside = false;
246
+ for (const ring of rings) {
247
+ for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
248
+ const [xi, yi] = ring[i]!;
249
+ const [xj, yj] = ring[j]!;
250
+ if (yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi) inside = !inside;
251
+ }
252
+ }
253
+ return inside;
254
+ }
255
+
256
+ /** This function gives the distance between the centers of two rectangles. */
257
+ export function boxCenterDistance(a: Rect, b: Rect): number {
258
+ return Math.hypot(b.x + b.w / 2 - (a.x + a.w / 2), b.y + b.h / 2 - (a.y + a.h / 2));
259
+ }
260
+
211
261
  /**
212
262
  * This function gives the index at which a point goes among the items of a list on the screen.
213
263
  * The reading order is the rows from top to bottom, and each row from left to right.
package/src/rules.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { boundsOf, grow, marginSides, overlapOf, type Margin, type Point, type Rect, type Region } from "./geometry.ts";
1
+ import { boundsOf, grow, marginSides, overlapOf, pointInRings, type Margin, type Point, type Rect, type Region } from "./geometry.ts";
2
2
  import type { LayoutBox, LayoutContainer, LayoutInput, Obstacle, PairGap, Port } from "./input.ts";
3
3
  import { applyPack, type PackOutput } from "./output.ts";
4
4
  import { SHAPE_KIND_IDS, shapeKindProblem, type ShapeSupport } from "./shapes.ts";
@@ -532,19 +532,6 @@ function clipMid(ax: number, ay: number, bx: number, by: number, r: Rect): reado
532
532
  return [ax + t * dx, ay + t * dy];
533
533
  }
534
534
 
535
- /** The even-odd rule over the outer ring and the holes. */
536
- function pointInRings(x: number, y: number, rings: readonly (readonly (readonly [number, number])[])[]): boolean {
537
- let inside = false;
538
- for (const ring of rings) {
539
- for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
540
- const [xi, yi] = ring[i]!;
541
- const [xj, yj] = ring[j]!;
542
- if (yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi) inside = !inside;
543
- }
544
- }
545
- return inside;
546
- }
547
-
548
535
  /**
549
536
  * This function counts the pairs of a placed box and an obstacle that overlap by more than `tolerance`.
550
537
  * The box counts with its margin. A box with a shape counts as its bounding box.
@@ -1 +0,0 @@
1
- {"version":3,"file":"measures-O-NfAbmJ.d.ts","names":[],"sources":["../src/geometry.ts","../src/corpus.ts","../src/input.ts","../src/shapes.ts","../src/measures.ts"],"mappings":";KACY;;cAGC,gBAAgB;UAEZ;WACN;WACA;;;UAIM;WACN;WACA;WACA;WACA;;;;;;UAOM;WACN;WACA;WACA;WACA;;;iBAIK,OAAO,MAAM,YAAY;;iBAKzB,YAAY,MAAM,OAAO;;;;;iBAWzB,SAAS,MAAM,OAAO;;UAOrB;WACN;WACA;WACA;WACA;;;;;;KAOC;WACG;WAAuB;WAAoB;WAAoB;WAAoB;;WAEnF;WACA;WACA;;WAEA;WAAyB;WAAqB;WAAqB;;WACnE;WAA0B;WAAqB;WAAqB;WAAqB;;;KAG5F,kBAAkB;;iBAGd,YAAY,QAAQ,qBAAqB;;iBAOzC,KAAK,MAAM,MAAM,QAAQ,qBAAqB;;iBAM9C,SAAS,QAAQ,SAAS;;;;;iBA4B1B,WAAW,QAAQ;;iBAyBnB,UAAU,QAAQ,QAAQ,YAAY,aAAa;;;;;iBAoBnD,UAAU,GAAG,MAAM,GAAG;WAAkB;WAAoB;;;;;;iBAqB5D,cAAc,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG;;;;;iBAQ/C,cAAc,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,QAAQ;;iBAQvD,qBAAqB,GAAG,OAAO,GAAG,OAAO,GAAG;;;;;;;;;;;;;;;iBAsB5C,UAAU,OAAO,OAAO,kBAAkB,SAAS;;UC5NlD;;WAEN;;WAEA;;WAEA;;;UAIM;;WAEN;;WAEA;;WAEA,QAAQ,SAAS;;WAEjB,WAAW;;WAEX;;;;;;;;KASC;;;;KAGA,aAAa,SAA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