Skip to content

🎥 Make videos programmatically with React

simplify unarcing

Jonny Burger09405cced148
diff --git a/packages/paths/src/get-bounding-box.ts b/packages/paths/src/get-bounding-box.ts
index 16e3455904..6bd9d745c6 100644
--- a/packages/paths/src/get-bounding-box.ts
+++ b/packages/paths/src/get-bounding-box.ts
@@ -1,6 +1,5 @@
+import {removeATSInstructions} from './helpers/remove-a-s-t-curves';
import type {AbsoluteInstruction, BoundingBox} from './helpers/types';
-import {removeArcInstructions} from './helpers/unarc';
-import {unshort} from './helpers/unshort';
import {normalizePath} from './normalize-path';
import {parsePath} from './parse-path';
@@ -91,13 +90,12 @@ export const getBoundingBox = (d: string): BoundingBox => {
let maxX = -Infinity;
let maxY = -Infinity;
const parsed = parsePath(normalizePath(d)) as AbsoluteInstruction[];
- const unarced = removeArcInstructions(parsed);
- const unshortened = unshort(unarced);
+ const unarced = removeATSInstructions(parsed);
let x = 0;
let y = 0;
- for (const seg of unshortened) {
+ for (const seg of unarced) {
switch (seg.type) {
case 'M':
case 'L': {
@@ -205,6 +203,7 @@ export const getBoundingBox = (d: string): BoundingBox => {
break;
default:
+ // @ts-expect-error
throw new Error(`Unknown instruction ${seg.type}`);
}
}
diff --git a/packages/paths/src/helpers/iterate.ts b/packages/paths/src/helpers/iterate.ts
index 9e43b71b21..5f6431d4ab 100644
--- a/packages/paths/src/helpers/iterate.ts
+++ b/packages/paths/src/helpers/iterate.ts
@@ -1,12 +1,13 @@
-import type {AbsoluteInstruction, UnarcedAbsoluteInstruction} from './types';
+import type {AbsoluteInstruction, ReducesAbsoluteInstruction} from './types';
-export const iterateOverSegments = <T extends UnarcedAbsoluteInstruction>({
+export const iterateOverSegments = <T extends ReducesAbsoluteInstruction>({
segments,
iterate,
}: {
segments: AbsoluteInstruction[];
iterate: (options: {
segment: AbsoluteInstruction;
+ prevSegment: AbsoluteInstruction | null;
x: number;
y: number;
}) => T[];
@@ -14,8 +15,13 @@ export const iterateOverSegments = <T extends UnarcedAbsoluteInstruction>({
let x = 0;
let y = 0;
- const newSegments = segments.map((s) => {
- const newSeg = iterate({segment: s, x, y});
+ const newSegments = segments.map((s, i) => {
+ const newSeg = iterate({
+ segment: s,
+ x,
+ y,
+ prevSegment: segments[i - 1] ?? null,
+ });
switch (s.type) {
case 'M':
case 'A':
diff --git a/packages/paths/src/helpers/unarc.ts b/packages/paths/src/helpers/remove-a-s-t-curves.ts
similarity index 70%
rename from packages/paths/src/helpers/unarc.ts
rename to packages/paths/src/helpers/remove-a-s-t-curves.ts
index 70b7f7621a..f58f78aa3e 100644
--- a/packages/paths/src/helpers/unarc.ts
+++ b/packages/paths/src/helpers/remove-a-s-t-curves.ts
@@ -1,5 +1,5 @@
import {iterateOverSegments} from './iterate';
-import type {AbsoluteInstruction, UnarcedAbsoluteInstruction} from './types';
+import type {AbsoluteInstruction, ReducesAbsoluteInstruction} from './types';
const TAU = Math.PI * 2;
@@ -129,54 +129,107 @@ function arcToCircle({
}
// Requires path to be normalized
-export const removeArcInstructions = (
+export const removeATSInstructions = (
segments: AbsoluteInstruction[]
-): UnarcedAbsoluteInstruction[] => {
- return iterateOverSegments<UnarcedAbsoluteInstruction>({
+): ReducesAbsoluteInstruction[] => {
+ let prevControlX = 0;
+ let prevControlY = 0;
+ let curControlX = 0;
+ let curControlY = 0;
+
+ return iterateOverSegments<ReducesAbsoluteInstruction>({
segments,
- iterate: ({segment, x, y}) => {
- if (segment.type !== 'A') {
- return [segment];
+ iterate: ({segment, prevSegment, x, y}) => {
+ if (segment.type === 'A') {
+ const nextX = segment.x;
+ const nextY = segment.y;
+ const new_segments = arcToCircle({
+ x1: x,
+ y1: y,
+ x2: nextX,
+ y2: nextY,
+ largeArcFlag: segment.largeArcFlag,
+ sweepFlag: segment.sweepFlag,
+ rx: segment.rx,
+ ry: segment.ry,
+ phi: segment.xAxisRotation,
+ });
+
+ // Degenerated arcs can be ignored by renderer, but should not be dropped
+ // to avoid collisions with `S A S` and so on. Replace with empty line.
+ if (new_segments.length === 0) {
+ return [
+ {
+ type: 'L',
+ x: segment.x,
+ y: segment.y,
+ },
+ ];
+ }
+
+ const result = new_segments.map((_s): ReducesAbsoluteInstruction => {
+ return {
+ type: 'C',
+ cp1x: _s[2],
+ cp1y: _s[3],
+ cp2x: _s[4],
+ cp2y: _s[5],
+ x: _s[6],
+ y: _s[7],
+ };
+ });
+ return result;
}
- const nextX = segment.x;
- const nextY = segment.y;
- const new_segments = arcToCircle({
- x1: x,
- y1: y,
- x2: nextX,
- y2: nextY,
- largeArcFlag: segment.largeArcFlag,
- sweepFlag: segment.sweepFlag,
- rx: segment.rx,
- ry: segment.ry,
- phi: segment.xAxisRotation,
- });
-
- // Degenerated arcs can be ignored by renderer, but should not be dropped
- // to avoid collisions with `S A S` and so on. Replace with empty line.
- if (new_segments.length === 0) {
+ if (segment.type === 'T') {
+ if (prevSegment && prevSegment.type === 'Q') {
+ prevControlX = prevSegment.cpx;
+ prevControlY = prevSegment.cpy;
+ } else {
+ prevControlX = 0;
+ prevControlY = 0;
+ }
+
+ curControlX = -prevControlX;
+ curControlY = -prevControlY;
+
+ return [
+ {
+ type: 'Q',
+ cpx: curControlX,
+ cpy: curControlY,
+ x: segment.x,
+ y: segment.y,
+ },
+ ];
+ }
+
+ if (segment.type === 'S') {
+ if (prevSegment && prevSegment.type === 'C') {
+ prevControlX = prevSegment.cp2x;
+ prevControlY = prevSegment.cp2y;
+ } else {
+ prevControlX = 0;
+ prevControlY = 0;
+ }
+
+ curControlX = -prevControlX;
+ curControlY = -prevControlY;
+
return [
{
- type: 'L',
+ type: 'C',
+ cp1x: curControlX,
+ cp1y: curControlY,
+ cp2x: segment.cpx,
+ cp2y: segment.cpy,
x: segment.x,
y: segment.y,
},
];
}
- const result = new_segments.map((_s): UnarcedAbsoluteInstruction => {
- return {
- type: 'C',
- cp1x: _s[2],
- cp1y: _s[3],
- cp2x: _s[4],
- cp2y: _s[5],
- x: _s[6],
- y: _s[7],
- };
- });
- return result;
+ return [segment];
},
});
};
diff --git a/packages/paths/src/helpers/types.ts b/packages/paths/src/helpers/types.ts
index 49c25e6cf0..f6f5c09684 100644
--- a/packages/paths/src/helpers/types.ts
+++ b/packages/paths/src/helpers/types.ts
@@ -26,7 +26,7 @@ export interface PointProperties {
export type BoundingBox = {x1: number; y1: number; x2: number; y2: number};
-export type UnarcedAbsoluteInstruction =
+export type ReducesAbsoluteInstruction =
| {
type: 'M';
x: number;
@@ -54,13 +54,6 @@ export type UnarcedAbsoluteInstruction =
x: number;
y: number;
}
- | {
- type: 'S';
- cpx: number;
- cpy: number;
- x: number;
- y: number;
- }
| {
type: 'Q';
cpx: number;
@@ -68,17 +61,12 @@ export type UnarcedAbsoluteInstruction =
x: number;
y: number;
}
- | {
- type: 'T';
- x: number;
- y: number;
- }
| {
type: 'Z';
};
export type AbsoluteInstruction =
- | UnarcedAbsoluteInstruction
+ | ReducesAbsoluteInstruction
| {
type: 'A';
rx: number;
@@ -88,6 +76,18 @@ export type AbsoluteInstruction =
sweepFlag: boolean;
x: number;
y: number;
+ }
+ | {
+ type: 'S';
+ cpx: number;
+ cpy: number;
+ x: number;
+ y: number;
+ }
+ | {
+ type: 'T';
+ x: number;
+ y: number;
};
export type Instruction =
diff --git a/packages/paths/src/helpers/unshort.ts b/packages/paths/src/helpers/unshort.ts
deleted file mode 100644
index 561d5aeb9a..0000000000
--- a/packages/paths/src/helpers/unshort.ts
+++ /dev/null
@@ -1,73 +0,0 @@
-import type {Instruction} from './types';
-
-// Requires that the path is already normalized
-export const unshort = function (segments: Instruction[]): Instruction[] {
- let prevControlX = 0;
- let prevControlY = 0;
- let curControlX = 0;
- let curControlY = 0;
-
- const newSegments = segments.slice(0);
-
- for (let i = 0; i < newSegments.length; i++) {
- const s = newSegments[i];
-
- // First command MUST be M|m, it's safe to skip.
- // Protect from access to [-1] for sure.
- if (!i) {
- continue;
- }
-
- if (s.type === 'T') {
- // quadratic curve
-
- const prevSegment = newSegments[i - 1];
-
- if (prevSegment.type === 'Q') {
- prevControlX = prevSegment.cpx;
- prevControlY = prevSegment.cpy;
- } else {
- prevControlX = 0;
- prevControlY = 0;
- }
-
- curControlX = -prevControlX;
- curControlY = -prevControlY;
-
- newSegments[i] = {
- type: 'Q',
- cpx: curControlX,
- cpy: curControlY,
- x: s.x,
- y: s.y,
- };
- } else if (s.type === 'S') {
- // cubic curve
-
- const prevSegment = newSegments[i - 1];
-
- if (prevSegment.type === 'C') {
- prevControlX = prevSegment.cp2x;
- prevControlY = prevSegment.cp2y;
- } else {
- prevControlX = 0;
- prevControlY = 0;
- }
-
- curControlX = -prevControlX;
- curControlY = -prevControlY;
-
- newSegments[i] = {
- type: 'C',
- cp1x: curControlX,
- cp1y: curControlY,
- cp2x: s.cpx,
- cp2y: s.cpy,
- x: s.x,
- y: s.y,
- };
- }
- }
-
- return newSegments;
-};