[79a0317] | 1 | import { encodeSVGPath } from "./SVGPathDataEncoder";
|
---|
| 2 | import { SVGPathDataParser } from "./SVGPathDataParser";
|
---|
| 3 | import { SVGPathDataTransformer } from "./SVGPathDataTransformer";
|
---|
| 4 | import { TransformableSVG } from "./TransformableSVG";
|
---|
| 5 | import { SVGCommand } from "./types";
|
---|
| 6 |
|
---|
| 7 | export class SVGPathData extends TransformableSVG {
|
---|
| 8 | commands: SVGCommand[];
|
---|
| 9 | constructor(content: string | SVGCommand[]) {
|
---|
| 10 | super();
|
---|
| 11 | if ("string" === typeof content) {
|
---|
| 12 | this.commands = SVGPathData.parse(content);
|
---|
| 13 | } else {
|
---|
| 14 | this.commands = content;
|
---|
| 15 | }
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | encode() {
|
---|
| 19 | return SVGPathData.encode(this.commands);
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | getBounds() {
|
---|
| 23 | const boundsTransform = SVGPathDataTransformer.CALCULATE_BOUNDS();
|
---|
| 24 |
|
---|
| 25 | this.transform(boundsTransform);
|
---|
| 26 | return boundsTransform;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | transform(
|
---|
| 30 | transformFunction: (input: SVGCommand) => SVGCommand | SVGCommand[],
|
---|
| 31 | ) {
|
---|
| 32 | const newCommands = [];
|
---|
| 33 |
|
---|
| 34 | for (const command of this.commands) {
|
---|
| 35 | const transformedCommand = transformFunction(command);
|
---|
| 36 |
|
---|
| 37 | if (Array.isArray(transformedCommand)) {
|
---|
| 38 | newCommands.push(...transformedCommand);
|
---|
| 39 | } else {
|
---|
| 40 | newCommands.push(transformedCommand);
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 | this.commands = newCommands;
|
---|
| 44 | return this;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | static encode(commands: SVGCommand[]) {
|
---|
| 48 | return encodeSVGPath(commands);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | static parse(path: string) {
|
---|
| 52 | const parser = new SVGPathDataParser();
|
---|
| 53 | const commands: SVGCommand[] = [];
|
---|
| 54 | parser.parse(path, commands);
|
---|
| 55 | parser.finish(commands);
|
---|
| 56 | return commands;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | static readonly CLOSE_PATH: 1 = 1;
|
---|
| 60 | static readonly MOVE_TO: 2 = 2;
|
---|
| 61 | static readonly HORIZ_LINE_TO: 4 = 4;
|
---|
| 62 | static readonly VERT_LINE_TO: 8 = 8;
|
---|
| 63 | static readonly LINE_TO: 16 = 16;
|
---|
| 64 | static readonly CURVE_TO: 32 = 32;
|
---|
| 65 | static readonly SMOOTH_CURVE_TO: 64 = 64;
|
---|
| 66 | static readonly QUAD_TO: 128 = 128;
|
---|
| 67 | static readonly SMOOTH_QUAD_TO: 256 = 256;
|
---|
| 68 | static readonly ARC: 512 = 512;
|
---|
| 69 | static readonly LINE_COMMANDS = SVGPathData.LINE_TO | SVGPathData.HORIZ_LINE_TO | SVGPathData.VERT_LINE_TO;
|
---|
| 70 | static readonly DRAWING_COMMANDS = SVGPathData.HORIZ_LINE_TO | SVGPathData.VERT_LINE_TO | SVGPathData.LINE_TO |
|
---|
| 71 | SVGPathData.CURVE_TO | SVGPathData.SMOOTH_CURVE_TO | SVGPathData.QUAD_TO |
|
---|
| 72 | SVGPathData.SMOOTH_QUAD_TO | SVGPathData.ARC;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | export const COMMAND_ARG_COUNTS = {
|
---|
| 76 | [SVGPathData.MOVE_TO]: 2,
|
---|
| 77 | [SVGPathData.LINE_TO]: 2,
|
---|
| 78 | [SVGPathData.HORIZ_LINE_TO]: 1,
|
---|
| 79 | [SVGPathData.VERT_LINE_TO]: 1,
|
---|
| 80 | [SVGPathData.CLOSE_PATH]: 0,
|
---|
| 81 | [SVGPathData.QUAD_TO]: 4,
|
---|
| 82 | [SVGPathData.SMOOTH_QUAD_TO]: 2,
|
---|
| 83 | [SVGPathData.CURVE_TO]: 6,
|
---|
| 84 | [SVGPathData.SMOOTH_CURVE_TO]: 4,
|
---|
| 85 | [SVGPathData.ARC]: 7,
|
---|
| 86 | };
|
---|
| 87 |
|
---|
| 88 | export {encodeSVGPath} from "./SVGPathDataEncoder";
|
---|
| 89 | export {SVGPathDataParser} from "./SVGPathDataParser";
|
---|
| 90 | export {SVGPathDataTransformer} from "./SVGPathDataTransformer";
|
---|