1 17 18 19 20 package org.apache.fop.render.java2d; 21 22 import java.awt.BasicStroke ; 23 import java.awt.Color ; 24 import java.awt.Graphics2D ; 25 import java.awt.Paint ; 26 import java.awt.Shape ; 27 import java.awt.geom.AffineTransform ; 28 import java.awt.geom.Area ; 29 import java.awt.geom.GeneralPath ; 30 31 import org.apache.fop.fo.Constants; 32 import org.apache.fop.fonts.FontInfo; 33 34 40 public class Java2DGraphicsState { 41 42 43 private Graphics2D currentGraphics; 44 45 private BasicStroke currentStroke; 46 47 private float currentStrokeWidth; 48 49 private int currentStrokeStyle; 50 51 52 private FontInfo fontInfo; 53 54 55 private AffineTransform initialTransform; 56 57 63 public Java2DGraphicsState(Graphics2D graphics, FontInfo fontInfo, 64 AffineTransform at) { 65 this.fontInfo = fontInfo; 66 this.currentGraphics = graphics; 67 this.initialTransform = at; 68 currentGraphics.setTransform(at); 69 } 70 71 75 public Java2DGraphicsState(Java2DGraphicsState org) { 76 this.currentGraphics = (Graphics2D )org.currentGraphics.create(); 77 this.fontInfo = org.fontInfo; 78 this.initialTransform = org.initialTransform; 79 this.currentStroke = org.currentStroke; 80 this.currentStrokeStyle = org.currentStrokeStyle; 81 this.currentStrokeWidth = org.currentStrokeWidth; 82 } 83 84 87 public Graphics2D getGraph() { 88 return currentGraphics; 89 } 90 91 92 public void dispose() { 93 this.currentGraphics.dispose(); 94 this.currentGraphics = null; 95 96 } 97 98 105 public boolean updateColor(Color col) { 106 if (!col.equals(getGraph().getColor())) { 107 getGraph().setColor(col); 108 return true; 109 } else { 110 return false; 111 } 112 } 113 114 117 public java.awt.Color getColor() { 118 return currentGraphics.getColor(); 119 } 120 121 129 public boolean updateFont(String name, int size) { 130 131 FontMetricsMapper mapper = (FontMetricsMapper)fontInfo.getMetricsFor(name); 132 boolean updateName = (!mapper.getFontName().equals( 133 getGraph().getFont().getFontName())); 134 boolean updateSize = (size != (getGraph().getFont().getSize() * 1000)); 135 136 if (updateName || updateSize) { 137 java.awt.Font font = mapper.getFont(size); 139 140 currentGraphics.setFont(font); 141 return true; 142 } else { 143 return false; 144 } 145 } 146 147 148 public java.awt.Font getFont() { 149 return currentGraphics.getFont(); 150 } 151 152 160 public boolean updateStroke(float width, int style) { 161 162 boolean update = false; 163 164 if ((width != currentStrokeWidth) || (style != currentStrokeStyle)) { 166 167 update = true; 168 169 switch (style) { 170 case Constants.EN_DOTTED: 171 172 currentStroke = new BasicStroke (width, BasicStroke.CAP_ROUND, 173 BasicStroke.JOIN_BEVEL, 0f, new float[] {0, 2 * width}, width); 174 currentGraphics.setStroke(currentStroke); 175 176 currentStrokeWidth = width; 177 currentStrokeStyle = style; 178 179 break; 180 181 case Constants.EN_DASHED: 182 183 currentStroke = new BasicStroke (width, BasicStroke.CAP_BUTT, 184 BasicStroke.JOIN_BEVEL, 0f, new float[] {8f, 2f}, 0f); 185 currentGraphics.setStroke(currentStroke); 186 187 currentStrokeWidth = width; 188 currentStrokeStyle = style; 189 190 break; 191 192 default: 194 currentStroke = new BasicStroke (width); 195 currentGraphics.setStroke(currentStroke); 196 197 currentStrokeWidth = width; 198 currentStrokeStyle = style; 199 200 break; 201 } 202 } 203 204 return update; 205 } 206 207 208 public BasicStroke getStroke() { 209 return (BasicStroke ) currentGraphics.getStroke(); 210 } 211 212 219 public boolean updatePaint(Paint p) { 220 if (getGraph().getPaint() == null) { 221 if (p != null) { 222 getGraph().setPaint(p); 223 return true; 224 } 225 } else if (p.equals(getGraph().getPaint())) { 226 getGraph().setPaint(p); 227 return true; 228 } 229 return false; 230 } 231 232 239 public boolean updateClip(Shape cl) { 240 if (getGraph().getClip() != null) { 241 Area newClip = new Area (getGraph().getClip()); 242 newClip.intersect(new Area (cl)); 243 getGraph().setClip(new GeneralPath (newClip)); 244 } else { 245 getGraph().setClip(cl); 246 } 247 return true; } 249 250 257 public void transform(AffineTransform tf) { 258 getGraph().transform(tf); 259 } 260 261 267 public AffineTransform getTransform() { 268 return getGraph().getTransform(); 269 } 270 271 272 public String toString() { 273 String s = "AWTGraphicsState " + currentGraphics.toString() 274 + ", Stroke (width: " + currentStrokeWidth + " style: " 275 + currentStrokeStyle + "), " + getTransform(); 276 return s; 277 } 278 279 } 280 | Popular Tags |