All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.plantuml.openiconic.SvgPath Maven / Gradle / Ivy

There is a newer version: 1.2024.8
Show newest version
// THIS FILE HAS BEEN GENERATED BY A PREPROCESSOR.
package net.sourceforge.plantuml.openiconic;

import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import net.sourceforge.plantuml.klimt.UPath;
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
import net.sourceforge.plantuml.klimt.geom.XPoint2D;

public class SvgPath {

	// http://www.w3.org/TR/SVG11/paths.html#PathDataEllipticalArcCommands
	// https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths
	// http://tutorials.jenkov.com/svg/path-element.html
	// https://www.w3.org/TR/SVG/paths.html#PathDataQuadraticBezierCommands

	private List movements = new ArrayList<>();
	private List commands = new ArrayList<>();

	public SvgPath(String path) {
		// System.err.println("before=" + path);
		path = StringDecipher.decipher(path);
		// System.err.println("after=" + path);

		for (final StringTokenizer st = new StringTokenizer(path); st.hasMoreTokens();) {
			final String token = st.nextToken();

			if (token.matches("[a-zA-Z]")) {
				commands.add(new SvgCommandLetter(token));
			} else {
				commands.add(new SvgCommandNumber(token));
			}
		}
		commands = insertMissingLetter(commands);
		checkArguments(commands);
		SvgPosition last = new SvgPosition();
		SvgPosition lastMove = new SvgPosition();
		SvgPosition mirrorControlPoint = null;
		final Iterator iterator = commands.iterator();
		while (iterator.hasNext()) {
			Movement movement = new Movement(iterator);
			movement = movement.toAbsoluteUpperCase(last);

			if (movement.getLetter() == 'Z')
				last = lastMove;

			if (movement.is('S'))
				movement = movement.mutoToC(mirrorControlPoint);

			movements.add(movement);

			if (movement.getLetter() == 'M')
				lastMove = movement.lastPosition();

			if (movement.lastPosition() != null)
				last = movement.lastPosition();

			mirrorControlPoint = movement.getMirrorControlPoint();
		}
	}

	private List insertMissingLetter(List commands) {
		final List result = new ArrayList<>();
		final Iterator it = commands.iterator();
		SvgCommandLetter lastLetter = null;
		while (it.hasNext()) {
			final SvgCommand cmd = it.next();
			// System.err.println("cmd=" + cmd);
			final int nb;
			if (cmd instanceof SvgCommandNumber) {
				// System.err.println("INSERTING " + lastLetter);
				result.add(lastLetter);
				result.add(cmd);
				nb = lastLetter.argumentNumber() - 1;
			} else {
				result.add(cmd);
				lastLetter = ((SvgCommandLetter) cmd).implicit();
				nb = lastLetter.argumentNumber();
			}
			for (int i = 0; i < nb; i++) {
				final SvgCommandNumber number = (SvgCommandNumber) it.next();
				result.add(number);
			}
		}
		return result;
	}

	private void checkArguments(List commands) {
		final Iterator it = commands.iterator();
		while (it.hasNext()) {
			final SvgCommandLetter cmd = (SvgCommandLetter) it.next();
			final int nb = cmd.argumentNumber();
			for (int i = 0; i < nb; i++) {
				final SvgCommandNumber number = (SvgCommandNumber) it.next();
			}
		}
	}

	public String toSvg() {
		final StringBuilder result = new StringBuilder("");
		return result.toString();
	}

	private UPath toUPath(double factorx, double factory) {
		final UPath result = UPath.none();
		Movement previous = null;
		for (Movement move : movements) {
			final char letter = move.getLetter();
			final SvgPosition position = move.lastPosition();
			if (letter == 'M') {
				result.moveTo(position.getXDouble() * factorx, position.getYDouble() * factory);
			} else if (letter == 'C') {
				final SvgPosition ctl1 = move.getSvgPosition(0);
				final SvgPosition ctl2 = move.getSvgPosition(2);
				result.cubicTo(ctl1.getXDouble() * factorx, ctl1.getYDouble() * factory, ctl2.getXDouble() * factorx,
						ctl2.getYDouble() * factory, position.getXDouble() * factorx, position.getYDouble() * factory);
			} else if (letter == 'Q') {
				final SvgPosition ctl = move.getSvgPosition(0);
				result.cubicTo(ctl.getXDouble() * factorx, ctl.getYDouble() * factory, ctl.getXDouble() * factorx,
						ctl.getYDouble() * factory, position.getXDouble() * factorx, position.getYDouble() * factory);
			} else if (letter == 'T') {
				if (previous.getLetter() != 'Q')
					throw new IllegalArgumentException();
				// https://stackoverflow.com/questions/5287559/calculating-control-points-for-a-shorthand-smooth-svg-path-bezier-curve
				final SvgPosition lastCtl = previous.getSvgPosition(0);
				final SvgPosition lastP = previous.lastPosition();
				final SvgPosition ctl = lastP.getMirror(lastCtl);
				result.cubicTo(ctl.getXDouble() * factorx, ctl.getYDouble() * factory, ctl.getXDouble() * factorx,
						ctl.getYDouble() * factory, position.getXDouble() * factorx, position.getYDouble() * factory);
			} else if (letter == 'L') {
				result.lineTo(position.getXDouble() * factorx, position.getYDouble() * factory);
			} else if (letter == 'A') {
				final double rx = move.getArgument(0);
				final double ry = move.getArgument(1);
				final double x_axis_rotation = move.getArgument(2);
				final double large_arc_flag = move.getArgument(3);
				final double sweep_flag = move.getArgument(4);
				result.arcTo(rx * factorx, ry * factory, x_axis_rotation, large_arc_flag, sweep_flag,
						position.getXDouble() * factorx, position.getYDouble() * factory);
			} else if (letter == 'Z') {
				result.closePath();
			} else {
				throw new UnsupportedOperationException("letter " + letter);
			}
		}
		result.setOpenIconic(true);
		return result;
	}

	private UPath toUPath(AffineTransform at) {
		final UPath result = UPath.none();
		Movement previous = null;
		for (Movement move : movements) {
			final char letter = move.getLetter();
			final SvgPosition position = move.lastPosition();
			if (letter == 'M') {
				result.moveTo(position.affine(at));
			} else if (letter == 'C') {
				final SvgPosition ctl1 = move.getSvgPosition(0);
				final SvgPosition ctl2 = move.getSvgPosition(2);
				result.cubicTo(ctl1.affine(at), ctl2.affine(at), position.affine(at));
			} else if (letter == 'Q') {
				final SvgPosition ctl = move.getSvgPosition(0);
				result.cubicTo(ctl.affine(at), ctl.affine(at), position.affine(at));
			} else if (letter == 'T') {
				if (previous.getLetter() != 'Q')
					throw new IllegalArgumentException();
				// https://stackoverflow.com/questions/5287559/calculating-control-points-for-a-shorthand-smooth-svg-path-bezier-curve
				final SvgPosition lastCtl = previous.getSvgPosition(0);
				final SvgPosition lastP = previous.lastPosition();
				final SvgPosition ctl = lastP.getMirror(lastCtl);
				result.cubicTo(ctl.affine(at), ctl.affine(at), position.affine(at));
			} else if (letter == 'L') {
				result.lineTo(position.affine(at));
			} else if (letter == 'A') {
				final double rx = move.getArgument(0);
				final double ry = move.getArgument(1);
				final double x_axis_rotation = move.getArgument(2);
				final double large_arc_flag = move.getArgument(3);
				final double sweep_flag = move.getArgument(4);
				final XPoint2D tmp = position.affine(at);
				result.arcTo(rx * at.getScaleX(), ry * at.getScaleY(), x_axis_rotation, large_arc_flag, sweep_flag,
						tmp.getX(), tmp.getY());
			} else if (letter == 'Z') {
				result.closePath();
			} else {
				throw new UnsupportedOperationException("letter " + letter);
			}
			previous = move;
		}
		result.setOpenIconic(true);
		return result;
	}

	public void drawMe(UGraphic ug, double factor) {
		final UPath path = toUPath(factor, factor);
		ug.draw(path);
	}

	public void drawMe(UGraphic ug, AffineTransform at) {
		final UPath path = toUPath(at);
		ug.draw(path);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy