1 /* 2 * Copyright (c) 2016 Helmut Neemann 3 * Use of this source code is governed by the GPL v3 license 4 * that can be found in the LICENSE file. 5 */ 6 package de.neemann.digital.draw.graphics; 7 8 import java.io.Closeable; 9 import java.io.IOException; 10 11 /** 12 * Interface used to draw the circuit. 13 * There are implementations to draw on a {@link java.awt.Graphics2D} instance ({@link GraphicSwing}) but also 14 * implementations which create export formats like SVG ({@link GraphicSVG}). 15 */ 16 public abstract class Graphic implements Closeable { 17 18 /** 19 * The available flags 20 */ 21 public enum Flag {noShapeFilling, smallIO, hideTest, noPinMarker, thinnerLines, tiny} 22 23 /** 24 * Sets the bounding box of the future usage of this instance 25 * Instances that create a file will use this bounding box th write a header. 26 * So this method needs to be called before a draw-Method is called. 27 * 28 * @param min upper left corner 29 * @param max lower right corner 30 * @return this for chained calls 31 */ setBoundingBox(VectorInterface min, VectorInterface max)32 public Graphic setBoundingBox(VectorInterface min, VectorInterface max) { 33 return this; 34 } 35 36 /** 37 * Draws a line 38 * 39 * @param p1 first point 40 * @param p2 second point 41 * @param style the line style 42 */ drawLine(VectorInterface p1, VectorInterface p2, Style style)43 public abstract void drawLine(VectorInterface p1, VectorInterface p2, Style style); 44 45 /** 46 * Draws a polygon 47 * 48 * @param p the polygon to draw 49 * @param style the style 50 */ drawPolygon(Polygon p, Style style)51 public abstract void drawPolygon(Polygon p, Style style); 52 53 /** 54 * Draws a circle 55 * 56 * @param p1 upper left corner of outer rectangle containing the circle 57 * @param p2 lower right corner of outer rectangle containing the circle 58 * @param style the style 59 */ drawCircle(VectorInterface p1, VectorInterface p2, Style style)60 public abstract void drawCircle(VectorInterface p1, VectorInterface p2, Style style); 61 62 /** 63 * Draws text 64 * 65 * @param p1 point to draw the text 66 * @param p2 point at the left of p1, is used to determine the correct orientation of the text after transforming coordinates 67 * @param p3 point at the top of p1, is used to determine the correct orientation of the text after transforming coordinates 68 * @param text the text 69 * @param orientation the text orientation 70 * @param style the text style 71 */ drawText(VectorInterface p1, VectorInterface p2, VectorInterface p3, String text, Orientation orientation, Style style)72 public abstract void drawText(VectorInterface p1, VectorInterface p2, VectorInterface p3, String text, Orientation orientation, Style style); 73 74 /** 75 * Draws text 76 * 77 * @param p1 point to draw the text 78 * @param p2 point at the left of p1, is used to determine the correct orientation of the text after transforming coordinates 79 * @param text the text 80 * @param orientation the text orientation 81 * @param style the text style 82 */ drawText(VectorInterface p1, VectorInterface p2, String text, Orientation orientation, Style style)83 public final void drawText(VectorInterface p1, VectorInterface p2, String text, Orientation orientation, Style style) { 84 VectorInterface d = p2.sub(p1).toFloat().getOrthogonal(); 85 drawText(p1, p2, p1.add(d), text, orientation, style); 86 } 87 88 /** 89 * Helper to draw a horizontal left to right text 90 * 91 * @param pos the text position 92 * @param text the text 93 * @param orientation the text orientation 94 * @param style the text style 95 */ drawText(VectorInterface pos, String text, Orientation orientation, Style style)96 public final void drawText(VectorInterface pos, String text, Orientation orientation, Style style) { 97 drawText(pos, pos.add(new Vector(1, 0)), text, orientation, style); 98 } 99 100 /** 101 * opens a new group, used to create SVG grouping 102 */ openGroup()103 public void openGroup() { 104 } 105 106 /** 107 * closes a group, used to create SVG grouping 108 */ closeGroup()109 public void closeGroup() { 110 } 111 112 /** 113 * Returns true if the given flag is set 114 * 115 * @param flag the flag 116 * @return true if the given flag is set 117 */ isFlagSet(Flag flag)118 public boolean isFlagSet(Flag flag) { 119 return false; 120 } 121 122 /** 123 * closes the graphics instance 124 * 125 * @throws IOException IOException 126 */ close()127 public void close() throws IOException { 128 } 129 } 130