Skip to content

Commit

Permalink
feat(geom): add basic text support
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 22, 2020
1 parent 0a45ef8 commit 9d1424d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 4 deletions.
25 changes: 25 additions & 0 deletions packages/geom/src/api/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Attribs, IHiccupShape, Type } from "@thi.ng/geom-api";
import { set, Vec } from "@thi.ng/vectors";
import { copyAttribs } from "../internal/copy-attribs";

/**
* Basic stub for text elements. Currently, only a minimal set of geometry
* operations are implemented for this type, however this type implements
* {@link @this.ng/api#IToHiccup} and so is useful as wrapper for inclusion of
* text elements in {@link Group}s with other shape types.
*/
export class Text implements IHiccupShape {
constructor(public pos: Vec, public body: any, public attribs?: Attribs) {}

get type() {
return Type.TEXT;
}

copy(): Text {
return new Text(set([], this.pos), this.body, copyAttribs(this));
}

toHiccup() {
return ["text", this.attribs, this.pos, this.body];
}
}
6 changes: 6 additions & 0 deletions packages/geom/src/ctors/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Attribs } from "@thi.ng/geom-api";
import { Vec } from "@thi.ng/vectors";
import { Text } from "../api/text";

export const text = (pos: Vec, body: any, attribs?: Attribs) =>
new Text(pos, body, attribs);
2 changes: 2 additions & 0 deletions packages/geom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export * from "./api/quadratic";
export * from "./api/ray";
export * from "./api/rect";
export * from "./api/sphere";
export * from "./api/text";
export * from "./api/triangle";

export * from "./ctors/aabb";
Expand All @@ -37,6 +38,7 @@ export * from "./ctors/quad";
export * from "./ctors/quadratic";
export * from "./ctors/ray";
export * from "./ctors/rect";
export * from "./ctors/text";
export * from "./ctors/triangle";

export * from "./ops/arc-length";
Expand Down
5 changes: 4 additions & 1 deletion packages/geom/src/ops/bounds.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation1 } from "@thi.ng/defmulti";
import { AABBLike, IShape, PathSegment, PCLike, Type } from "@thi.ng/geom-api";
import { bounds as arcBounds } from "@thi.ng/geom-arc";
Expand Down Expand Up @@ -27,11 +28,11 @@ import { Line } from "../api/line";
import { Path } from "../api/path";
import { Quadratic } from "../api/quadratic";
import { Rect } from "../api/rect";
import { Text } from "../api/text";
import { aabbFromMinMax } from "../ctors/aabb";
import { rectFromMinMax } from "../ctors/rect";
import { collBounds } from "../internal/coll-bounds";
import { dispatch } from "../internal/dispatch";
import type { IObjectOf } from "@thi.ng/api";

export const bounds = defmulti<IShape, AABBLike | undefined>(dispatch);

Expand Down Expand Up @@ -84,6 +85,8 @@ bounds.addAll(<IObjectOf<Implementation1<unknown, AABBLike>>>{
rectFromMinMax(...quadraticBounds(points[0], points[1], points[2])),

[Type.RECT]: ($: IShape) => <AABBLike>$.copy(),

[Type.TEXT]: ($: Text) => new Rect(set2([], $.pos), [0, 0]),
});

bounds.isa(Type.AABB, Type.RECT);
Expand Down
3 changes: 2 additions & 1 deletion packages/geom/src/ops/centroid.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation1O, MultiFn1O } from "@thi.ng/defmulti";
import { AABBLike, IShape, PCLike, Type } from "@thi.ng/geom-api";
import {
Expand All @@ -13,7 +14,6 @@ import { Polygon } from "../api/polygon";
import { Triangle } from "../api/triangle";
import { dispatch } from "../internal/dispatch";
import { bounds } from "./bounds";
import type { IObjectOf } from "@thi.ng/api";

export const centroid: MultiFn1O<IShape, Vec, Vec | undefined> = defmulti(
dispatch
Expand Down Expand Up @@ -54,4 +54,5 @@ centroid.isa(Type.POINTS3, Type.POINTS);
centroid.isa(Type.POLYLINE, Type.POINTS);
centroid.isa(Type.QUAD, Type.POLYGON);
centroid.isa(Type.SPHERE, Type.CIRCLE);
centroid.isa(Type.TEXT, Type.CIRCLE);
centroid.isa(Type.TRIANGLE3, Type.TRIANGLE);
6 changes: 5 additions & 1 deletion packages/geom/src/ops/transform.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation2 } from "@thi.ng/defmulti";
import {
IHiccupShape,
Expand All @@ -18,6 +19,7 @@ import { Polyline } from "../api/polyline";
import { Quad } from "../api/quad";
import { Quadratic } from "../api/quadratic";
import { Rect } from "../api/rect";
import { Text } from "../api/text";
import { Triangle } from "../api/triangle";
import { copyAttribs } from "../internal/copy-attribs";
import { dispatch } from "../internal/dispatch";
Expand All @@ -27,7 +29,6 @@ import {
} from "../internal/transform-points";
import { asPath } from "./as-path";
import { asPolygon } from "./as-polygon";
import type { IObjectOf } from "@thi.ng/api";

/**
* Transforms given shape with provided matrix. Some shape types will be
Expand Down Expand Up @@ -89,6 +90,9 @@ transform.addAll(<IObjectOf<Implementation2<unknown, ReadonlyMat, IShape>>>{

[Type.RECT]: ($: Rect, mat) => transform(asPolygon($), mat),

[Type.TEXT]: ($: Text, mat) =>
new Text(mulV([], mat, $.pos!), $.body, copyAttribs($)),

[Type.TRIANGLE]: tx(Triangle),
});

Expand Down
6 changes: 5 additions & 1 deletion packages/geom/src/ops/translate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation2 } from "@thi.ng/defmulti";
import { IHiccupShape, IShape, Type } from "@thi.ng/geom-api";
import { add2, add3, ReadonlyVec, set2, set3 } from "@thi.ng/vectors";
Expand All @@ -15,11 +16,11 @@ import { Quad } from "../api/quad";
import { Ray } from "../api/ray";
import { Rect } from "../api/rect";
import { Sphere } from "../api/sphere";
import { Text } from "../api/text";
import { Triangle } from "../api/triangle";
import { copyAttribs } from "../internal/copy-attribs";
import { dispatch } from "../internal/dispatch";
import { translatedShape as tx } from "../internal/translate-points";
import type { IObjectOf } from "@thi.ng/api";

export const translate = defmulti<IShape, ReadonlyVec, IShape>(dispatch);

Expand Down Expand Up @@ -82,5 +83,8 @@ translate.addAll(<IObjectOf<Implementation2<unknown, ReadonlyVec, IShape>>>{
[Type.SPHERE]: ($: Sphere, delta) =>
new Sphere(add3([], $.pos, delta), $.r, copyAttribs($)),

[Type.TEXT]: ($: Text, delta) =>
new Text(add2([], $.pos, delta), $.body, copyAttribs($)),

[Type.TRIANGLE]: tx(Triangle),
});

0 comments on commit 9d1424d

Please sign in to comment.