Anonymous avatar Anonymous committed 2eac26e Draft

fixed bug in draw() and obsoleted Real2String

Comments (0)

Files changed (10)

src/main/java/org/xmlcml/graphics/svg/ClosePrimitive.java

 package org.xmlcml.graphics.svg;
 
+import java.awt.geom.GeneralPath;
+
+import org.xmlcml.euclid.Real2;
+
 /**
  * supports 'Z' command
  * @author pm286
 		return TAG;
 	}
 	
+	@Override
+	public void operateOn(GeneralPath path) {
+		path.closePath();
+	}
+	
 	public String toString() {
 		return TAG;
 	}

src/main/java/org/xmlcml/graphics/svg/CubicPrimitive.java

 package org.xmlcml.graphics.svg;
 
+import java.awt.geom.GeneralPath;
+
 import org.xmlcml.euclid.Real2;
 import org.xmlcml.euclid.Real2Array;
 
 		return TAG;
 	}
 	
+	@Override
+	public void operateOn(GeneralPath path) {
+		if (coordArray != null) {	
+			Real2 coord0 = coordArray.elementAt(0);
+			Real2 coord1 = coordArray.elementAt(1);
+			Real2 coord2 = coordArray.elementAt(2);
+			path.curveTo(coord0.x, coord0.y, coord1.x, coord1.y, coord2.x, coord2.y);
+		}
+	}
+
 	public String toString() {
 		String s = TAG;
 		for (int i = 0; i < coordArray.size(); i++) {

src/main/java/org/xmlcml/graphics/svg/LinePrimitive.java

 package org.xmlcml.graphics.svg;
 
+import java.awt.geom.GeneralPath;
+
 import org.xmlcml.euclid.Real2;
 import org.xmlcml.euclid.Real2Array;
 
 		return TAG + formatCoords(coordArray.get(0));
 	}
 
+	@Override
+	public void operateOn(GeneralPath path) {
+		if (coordArray != null) {	
+			Real2 coord = coordArray.elementAt(0);
+			path.lineTo(coord.x, coord.y);
+		}
+	}
 }

src/main/java/org/xmlcml/graphics/svg/MovePrimitive.java

 package org.xmlcml.graphics.svg;
 
+import java.awt.geom.GeneralPath;
+
 import org.xmlcml.euclid.Real2;
 import org.xmlcml.euclid.Real2Array;
 
 		return TAG;
 	}
 	
+	@Override
+	public void operateOn(GeneralPath path) {
+		if (coordArray != null) {	
+			Real2 coord = coordArray.elementAt(0);
+			path.moveTo(coord.x, coord.y);
+		}
+	}
+
 	public String toString() {
 		return TAG + formatCoords(coordArray.get(0));
 	}

src/main/java/org/xmlcml/graphics/svg/QuadPrimitive.java

 package org.xmlcml.graphics.svg;
 
+import java.awt.geom.GeneralPath;
+
 import org.xmlcml.euclid.Real2;
 import org.xmlcml.euclid.Real2Array;
 
 		}
 		return s;
 	}
+	
+	@Override
+	public void operateOn(GeneralPath path) {
+		if (coordArray != null) {	
+			Real2 coord0 = coordArray.elementAt(0);
+			Real2 coord1 = coordArray.elementAt(1);
+			path.quadTo(coord0.x, coord0.y, coord1.x, coord1.y);
+		}
+	}
+
 }

src/main/java/org/xmlcml/graphics/svg/SVGPath.java

 
 package org.xmlcml.graphics.svg;
 
+import java.awt.Color;
 import java.awt.Graphics2D;
 import java.awt.Shape;
 import java.awt.geom.AffineTransform;
 import nu.xom.Attribute;
 import nu.xom.Element;
 import nu.xom.Node;
-import nu.xom.Nodes;
 
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 	}
 	
 	public void setDString(String d) {
-		this.addAttribute(new Attribute(D, d));
+		if (d != null) {
+			this.addAttribute(new Attribute(D, d));
+		}
 	}
 	
 	public String getDString() {
 //</g>
 	
 	protected void drawElement(Graphics2D g2d) {
-		GeneralPath path = createAndSetPath2D();
+		GeneralPath path = createPath2D();
+		Color stroke = g2d.getColor();
+//		g2d.setColor(this.getStroke());
+		g2d.setColor(Color.BLACK);
 		g2d.draw(path);
 	}
 
 		return 0.1;
 	}
 
+	@Deprecated
 	public GeneralPath createAndSetPath2D() {
 		String s = this.getDString().trim()+S_SPACE;
+		System.out.println(s);
 		path2 = new GeneralPath();
 		while (s.length() > 0) {
 			if (s.startsWith("M")) {
 				path2.closePath();
 				s = s.substring(1).trim();
 			} else {
-				throw new RuntimeException("Cannot create path: "+s.charAt(0));
+				throw new RuntimeException("Cannot create path: "+s);
 			}
 		}
 		return path2;
 	}
 	
+	public GeneralPath createPath2D() {
+		path2 = new GeneralPath();
+		ensurePrimitives();
+		for (SVGPathPrimitive pathPrimitive : primitiveList) {
+			pathPrimitive.operateOn(path2);
+		}
+		return path2;
+	}
+	
 	/** get tag.
 	 * @return tag
 	 */

src/main/java/org/xmlcml/graphics/svg/SVGPathPrimitive.java

 package org.xmlcml.graphics.svg;
 
+import java.awt.geom.GeneralPath;
 import java.util.ArrayList;
 import java.util.List;
 
 	
 	public static List<SVGPathPrimitive> parseDString(String d) {
 		List<SVGPathPrimitive> primitiveList = new ArrayList<SVGPathPrimitive>();
+		if (d == null) {
+			return primitiveList;
+		}
 		List<String> tokenList = extractTokenList(d);
 		int itok = 0;
 		Real2 lastXY = null;
 				if (numberStart == -1) {
 					numberStart = i;
 				}
-			} else if ("mMcClLzZ".indexOf(c) != -1) {
+			} else if ("mMcClLqQzZ".indexOf(c) != -1) {
 				addCurrentNumber(d, tokenList, numberStart, i);
 				tokenList.add(""+c);
 				numberStart = -1;
 		Real2Array coordArray = getCoordArray();
 		return (coordArray) == null ? null : coordArray.getLastElement();
 	}
+
+	public abstract void operateOn(GeneralPath path2);
 }

src/main/java/org/xmlcml/graphics/svg/UnknownPrimitive.java

 package org.xmlcml.graphics.svg;
 
+import java.awt.geom.GeneralPath;
+
 public class UnknownPrimitive extends SVGPathPrimitive {
 
 	private String TAG = "?";
 		return TAG;
 	}
 
+	@Override
+	public void operateOn(GeneralPath path) {
+		throw new RuntimeException("Cannot create path");
+	}
+
+	
 }

src/test/java/org/xmlcml/graphics/svg/GraphicsTestFramework.java

+package org.xmlcml.graphics.svg;
+
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+
+/**main purpose is to provide a simple area for visual inspection of 
+ * drawing to Graphics (drawElement())
+ * @author pm286
+ *
+ */
+public class GraphicsTestFramework {
+
+	public static void main(String[] args) {
+		JFrame frame = new JFrame();
+		frame.getContentPane().add(new MyPanel());
+		frame.setSize(500, 500);
+		frame.show();
+	} 
+}
+
+class MyPanel extends JPanel {
+
+	private SVGPath path;
+	public MyPanel() {
+		path = new SVGPath("M100 200L250,300C100 290 240 110 400 230L110 20 Z M 30 40 L 70 20");
+	}
+	@Override
+	public void paint(Graphics g) {
+		g.drawString("foo", 100, 200 );
+		path.draw((Graphics2D) g);
+	}
+}

src/test/java/org/xmlcml/graphics/svg/SVGPathTest.java

 package org.xmlcml.graphics.svg;
 
 import java.awt.geom.GeneralPath;
+import java.util.List;
 
 import junit.framework.Assert;
 
 		Assert.assertEquals("format", "M219.758 604.535 L229.242 604.535", path.getDString().trim());
 	}
 
+	@Test
+	public void testPrimitives1() {
+		SVGPath svgPath = new SVGPath("M100 200L250,300");
+		List<SVGPathPrimitive> primitives = svgPath.ensurePrimitives();
+		Assert.assertEquals("prim", 2, primitives.size());
+		Assert.assertTrue("prim", primitives.get(0) instanceof MovePrimitive);
+		Assert.assertEquals("prim", 1, primitives.get(0).getCoordArray().size());
+		Assert.assertTrue("prim", primitives.get(1) instanceof LinePrimitive);
+		Assert.assertEquals("prim", 1, primitives.get(1).getCoordArray().size());
+	}
+	
+	@Test
+	public void testPrimitives2() {
+		SVGPath svgPath = new SVGPath("M100 200 L250,300 C100 290 240 110 400 230 Z");
+		List<SVGPathPrimitive> primitives = svgPath.ensurePrimitives();
+		Assert.assertEquals("prim", 4, primitives.size());
+		Assert.assertTrue("prim", primitives.get(0) instanceof MovePrimitive);
+		Assert.assertTrue("prim", primitives.get(1) instanceof LinePrimitive);
+		Assert.assertTrue("prim", primitives.get(2) instanceof CubicPrimitive);
+		Assert.assertEquals("prim", 3, primitives.get(2).getCoordArray().size());
+		Assert.assertTrue("prim", primitives.get(3) instanceof ClosePrimitive);
+		Assert.assertNull("prim", primitives.get(3).getCoordArray());
+
+	}
+	
 
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.