Skip to content

Commit 7a5b4ba

Browse files
authored
🤖 Merge PR DefinitelyTyped#74668 [clipper-lib] Add type definitions by @kwnt-dev
1 parent b8b221f commit 7a5b4ba

File tree

5 files changed

+326
-0
lines changed

5 files changed

+326
-0
lines changed

types/clipper-lib/.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import * as ClipperLib from "clipper-lib";
2+
3+
// IntPoint
4+
const pt: ClipperLib.IntPoint = { X: 10, Y: 20 };
5+
const ptZ: ClipperLib.IntPoint = { X: 10, Y: 20, Z: 5 };
6+
7+
// Path / Paths
8+
const path: ClipperLib.Path = [
9+
{ X: 0, Y: 0 },
10+
{ X: 100, Y: 0 },
11+
{ X: 100, Y: 100 },
12+
{ X: 0, Y: 100 },
13+
];
14+
const paths: ClipperLib.Paths = [path];
15+
16+
// Enums
17+
const ct: ClipperLib.ClipType = ClipperLib.ClipType.ctUnion;
18+
const pt2: ClipperLib.PolyType = ClipperLib.PolyType.ptSubject;
19+
const pft: ClipperLib.PolyFillType = ClipperLib.PolyFillType.pftEvenOdd;
20+
const jt: ClipperLib.JoinType = ClipperLib.JoinType.jtRound;
21+
const et: ClipperLib.EndType = ClipperLib.EndType.etClosedPolygon;
22+
23+
24+
// Clipper - constructor
25+
const cpr = new ClipperLib.Clipper();
26+
const cpr2 = new ClipperLib.Clipper(
27+
ClipperLib.InitOptions.ioStrictlySimple | ClipperLib.InitOptions.ioPreserveCollinear,
28+
);
29+
30+
// Clipper - instance methods
31+
cpr.AddPath(path, ClipperLib.PolyType.ptSubject, true);
32+
cpr.AddPaths(paths, ClipperLib.PolyType.ptClip, true);
33+
const solution: ClipperLib.Paths = [];
34+
const success: boolean = cpr.Execute(ClipperLib.ClipType.ctIntersection, solution);
35+
const bounds: ClipperLib.IntRect = cpr.GetBounds();
36+
cpr.Clear();
37+
38+
// Clipper - properties
39+
cpr.PreserveCollinear = true;
40+
cpr.ReverseSolution = false;
41+
cpr.StrictlySimple = true;
42+
43+
// Clipper - static methods
44+
const area: number = ClipperLib.Clipper.Area(path);
45+
const cleaned: ClipperLib.Path = ClipperLib.Clipper.CleanPolygon(path, 1.1);
46+
const cleanedPaths: ClipperLib.Paths = ClipperLib.Clipper.CleanPolygons(paths, 1.1);
47+
const orientation: boolean = ClipperLib.Clipper.Orientation(path);
48+
const inPoly: number = ClipperLib.Clipper.PointInPolygon(pt, path);
49+
ClipperLib.Clipper.ReversePath(path);
50+
ClipperLib.Clipper.ReversePaths(paths);
51+
const simplified: ClipperLib.Paths = ClipperLib.Clipper.SimplifyPolygon(path);
52+
const simplifiedPaths: ClipperLib.Paths = ClipperLib.Clipper.SimplifyPolygons(paths);
53+
54+
// Clipper - PolyTree
55+
const polytree = new ClipperLib.PolyTree();
56+
cpr.AddPath(path, ClipperLib.PolyType.ptSubject, true);
57+
cpr.Execute(ClipperLib.ClipType.ctUnion, polytree);
58+
const closedPaths: ClipperLib.Paths = ClipperLib.Clipper.ClosedPathsFromPolyTree(polytree);
59+
const openPaths: ClipperLib.Paths = ClipperLib.Clipper.OpenPathsFromPolyTree(polytree);
60+
const allPaths: ClipperLib.Paths = ClipperLib.Clipper.PolyTreeToPaths(polytree);
61+
const total: number = polytree.Total();
62+
const first: ClipperLib.PolyNode = polytree.GetFirst();
63+
polytree.Clear();
64+
65+
// PolyNode
66+
const node = new ClipperLib.PolyNode();
67+
const childCount: number = node.ChildCount();
68+
const childs: ClipperLib.PolyNode[] = node.Childs();
69+
const contour: ClipperLib.Path = node.Contour();
70+
const next: ClipperLib.PolyNode = node.GetNext();
71+
const isHole: boolean = node.IsHole();
72+
const parent: ClipperLib.PolyNode = node.Parent();
73+
const isOpen: boolean = node.IsOpen;
74+
75+
// Clipper - Minkowski
76+
const minkDiff: ClipperLib.Paths = ClipperLib.Clipper.MinkowskiDiff(path, path);
77+
const minkSum: ClipperLib.Paths = ClipperLib.Clipper.MinkowskiSum(path, path, true);
78+
79+
// Clipper - deprecated OffsetPaths
80+
const offsetted: ClipperLib.Paths = ClipperLib.Clipper.OffsetPaths(paths, 10);
81+
82+
// ClipperOffset
83+
const co = new ClipperLib.ClipperOffset();
84+
const co2 = new ClipperLib.ClipperOffset(2.0, 0.25);
85+
co.AddPath(path, ClipperLib.JoinType.jtRound, ClipperLib.EndType.etClosedPolygon);
86+
co.AddPaths(paths, ClipperLib.JoinType.jtSquare, ClipperLib.EndType.etClosedPolygon);
87+
const offsetSolution: ClipperLib.Paths = [];
88+
co.Execute(offsetSolution, 10);
89+
co.ArcTolerance = 0.25;
90+
co.MiterLimit = 2.0;
91+
co.Clear();
92+
93+
// JS Helper functions
94+
ClipperLib.JS.ScaleUpPath(path, 100);
95+
ClipperLib.JS.ScaleUpPaths(paths, 100);
96+
ClipperLib.JS.ScaleDownPath(path, 100);
97+
ClipperLib.JS.ScaleDownPaths(paths, 100);
98+
const areaOf: number = ClipperLib.JS.AreaOfPolygon(path);
99+
const areasOf: number = ClipperLib.JS.AreaOfPolygons(paths);
100+
const boundsPath: ClipperLib.IntRect = ClipperLib.JS.BoundsOfPath(path);
101+
const boundsPaths: ClipperLib.IntRect = ClipperLib.JS.BoundsOfPaths(paths);
102+
const cloned: ClipperLib.Paths = ClipperLib.JS.Clone(paths);
103+
const cleanedJS: ClipperLib.Path = ClipperLib.JS.Clean(path, 1.0);
104+
const lightened: ClipperLib.Path = ClipperLib.JS.Lighten(path, 1.0);
105+
const perimPath: number = ClipperLib.JS.PerimeterOfPath(path, true);
106+
const perimPaths: number = ClipperLib.JS.PerimeterOfPaths(paths, true);
107+
const exPolys: ClipperLib.ExPolygons = ClipperLib.JS.PolyTreeToExPolygons(polytree);
108+
const fromEx: ClipperLib.Paths = ClipperLib.JS.ExPolygonsToPaths(exPolys);

types/clipper-lib/index.d.ts

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
export as namespace ClipperLib;
2+
3+
// --- Preprocessor flags ---
4+
export var use_int32: boolean;
5+
export var use_xyz: boolean;
6+
export var use_lines: boolean;
7+
export var use_deprecated: boolean;
8+
9+
// --- Types ---
10+
11+
export interface IntPoint {
12+
X: number;
13+
Y: number;
14+
Z?: number;
15+
}
16+
17+
export interface IntRect {
18+
left: number;
19+
top: number;
20+
right: number;
21+
bottom: number;
22+
}
23+
24+
export type Path = IntPoint[];
25+
export type Paths = Path[];
26+
27+
export interface ExPolygon {
28+
outer: Path;
29+
holes: Paths;
30+
}
31+
32+
export type ExPolygons = ExPolygon[];
33+
34+
// --- Enums ---
35+
36+
export enum ClipType {
37+
ctIntersection = 0,
38+
ctUnion = 1,
39+
ctDifference = 2,
40+
ctXor = 3,
41+
}
42+
43+
export enum PolyType {
44+
ptSubject = 0,
45+
ptClip = 1,
46+
}
47+
48+
export enum PolyFillType {
49+
pftEvenOdd = 0,
50+
pftNonZero = 1,
51+
pftPositive = 2,
52+
pftNegative = 3,
53+
}
54+
55+
export enum JoinType {
56+
jtSquare = 0,
57+
jtRound = 1,
58+
jtMiter = 2,
59+
}
60+
61+
export enum EndType {
62+
etClosedPolygon = 0,
63+
etClosedLine = 1,
64+
etOpenButt = 2,
65+
etOpenSquare = 3,
66+
etOpenRound = 4,
67+
}
68+
69+
export enum EndType_ {
70+
etSquare = 0,
71+
etRound = 1,
72+
etButt = 2,
73+
etSingle = 3,
74+
}
75+
76+
export enum InitOptions {
77+
ioReverseSolution = 1,
78+
ioStrictlySimple = 2,
79+
ioPreserveCollinear = 4,
80+
}
81+
82+
// --- PolyNode ---
83+
84+
export class PolyNode {
85+
IsOpen: boolean;
86+
ChildCount(): number;
87+
Childs(): PolyNode[];
88+
Contour(): Path;
89+
GetNext(): PolyNode;
90+
IsHole(): boolean;
91+
Parent(): PolyNode;
92+
}
93+
94+
// --- PolyTree ---
95+
96+
export class PolyTree extends PolyNode {
97+
Clear(): void;
98+
GetFirst(): PolyNode;
99+
Total(): number;
100+
}
101+
102+
// --- Clipper ---
103+
104+
export class Clipper {
105+
constructor(initOptions?: number);
106+
107+
PreserveCollinear: boolean;
108+
ReverseSolution: boolean;
109+
StrictlySimple: boolean;
110+
ZFillFunction:
111+
| ((bot1: IntPoint, top1: IntPoint, bot2: IntPoint, top2: IntPoint, pt: IntPoint) => void)
112+
| null;
113+
114+
AddPath(path: Path, polyType: PolyType, closed: boolean): boolean;
115+
AddPaths(paths: Paths, polyType: PolyType, closed: boolean): boolean;
116+
Clear(): void;
117+
Execute(clipType: ClipType, solution: Paths, subjFillType?: PolyFillType, clipFillType?: PolyFillType): boolean;
118+
Execute(
119+
clipType: ClipType,
120+
solution: PolyTree,
121+
subjFillType?: PolyFillType,
122+
clipFillType?: PolyFillType,
123+
): boolean;
124+
GetBounds(): IntRect;
125+
126+
static Area(poly: Path): number;
127+
static CleanPolygon(path: Path, distance?: number): Path;
128+
static CleanPolygons(polys: Paths, distance?: number): Paths;
129+
static ClosedPathsFromPolyTree(polytree: PolyTree): Paths;
130+
static MinkowskiDiff(poly1: Path, poly2: Path): Paths;
131+
static MinkowskiSum(pattern: Path, path: Path, pathIsClosed: boolean): Paths;
132+
static MinkowskiSum(pattern: Path, paths: Paths, pathIsClosed: boolean): Paths;
133+
static OffsetPaths(polys: Paths, delta: number, joinType?: JoinType, endType?: EndType_, limit?: number): Paths;
134+
static OpenPathsFromPolyTree(polytree: PolyTree): Paths;
135+
static Orientation(poly: Path): boolean;
136+
static PointInPolygon(pt: IntPoint, path: Path): number;
137+
static PolyTreeToPaths(polytree: PolyTree): Paths;
138+
static ReversePath(path: Path): void;
139+
static ReversePaths(paths: Paths): void;
140+
static SimplifyPolygon(poly: Path, fillType?: PolyFillType): Paths;
141+
static SimplifyPolygons(polys: Paths, fillType?: PolyFillType): Paths;
142+
}
143+
144+
// --- ClipperOffset ---
145+
146+
export class ClipperOffset {
147+
constructor(miterLimit?: number, arcTolerance?: number);
148+
149+
ArcTolerance: number;
150+
MiterLimit: number;
151+
152+
AddPath(path: Path, joinType: JoinType, endType: EndType): void;
153+
AddPaths(paths: Paths, joinType: JoinType, endType: EndType): void;
154+
Clear(): void;
155+
Execute(solution: Paths, delta: number): void;
156+
Execute(solution: PolyTree, delta: number): void;
157+
}
158+
159+
// --- JS Helper functions ---
160+
161+
export namespace JS {
162+
function AreaOfPolygon(poly: Path, scale?: number): number;
163+
function AreaOfPolygons(polys: Paths, scale?: number): number;
164+
function BoundsOfPath(path: Path): IntRect;
165+
function BoundsOfPaths(paths: Paths): IntRect;
166+
function Clone(paths: Paths): Paths;
167+
function Clean(path: Path, delta: number): Path;
168+
function Lighten(path: Path, tolerance: number): Path;
169+
function PerimeterOfPath(path: Path, closed: boolean, scale?: number): number;
170+
function PerimeterOfPaths(paths: Paths, closed: boolean, scale?: number): number;
171+
function ScaleDownPath(path: Path, scale: number): void;
172+
function ScaleDownPaths(paths: Paths, scale: number): void;
173+
function ScaleUpPath(path: Path, scale: number): void;
174+
function ScaleUpPaths(paths: Paths, scale: number): void;
175+
function PolyTreeToExPolygons(polytree: PolyTree): ExPolygons;
176+
function ExPolygonsToPaths(exPolygons: ExPolygons): Paths;
177+
}

types/clipper-lib/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "@types/clipper-lib",
4+
"version": "6.4.9999",
5+
"projects": [
6+
"https://github.com/junmer/clipper-lib"
7+
],
8+
"devDependencies": {
9+
"@types/clipper-lib": "workspace:."
10+
},
11+
"owners": [
12+
{
13+
"name": "kwnt-dev",
14+
"githubUsername": "kwnt-dev"
15+
}
16+
]
17+
}

types/clipper-lib/tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictFunctionTypes": true,
10+
"strictNullChecks": true,
11+
"types": [],
12+
"noEmit": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.d.ts",
17+
"clipper-lib-tests.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)