| 1 |
// Copyright 2000-2007 FreeHEP |
| 2 |
package org.freehep.graphicsio.emf; |
| 3 |
|
| 4 |
import java.awt.BasicStroke; |
| 5 |
import java.awt.Color; |
| 6 |
import java.awt.Component; |
| 7 |
import java.awt.Dimension; |
| 8 |
import java.awt.Font; |
| 9 |
import java.awt.GradientPaint; |
| 10 |
import java.awt.Graphics; |
| 11 |
import java.awt.GraphicsConfiguration; |
| 12 |
import java.awt.Paint; |
| 13 |
import java.awt.Point; |
| 14 |
import java.awt.Rectangle; |
| 15 |
import java.awt.Shape; |
| 16 |
import java.awt.Stroke; |
| 17 |
import java.awt.TexturePaint; |
| 18 |
import java.awt.Toolkit; |
| 19 |
import java.awt.geom.AffineTransform; |
| 20 |
import java.awt.geom.GeneralPath; |
| 21 |
import java.awt.image.BufferedImage; |
| 22 |
import java.awt.image.RenderedImage; |
| 23 |
import java.io.BufferedOutputStream; |
| 24 |
import java.io.File; |
| 25 |
import java.io.FileNotFoundException; |
| 26 |
import java.io.FileOutputStream; |
| 27 |
import java.io.IOException; |
| 28 |
import java.io.OutputStream; |
| 29 |
import java.text.AttributedCharacterIterator.Attribute; |
| 30 |
import java.util.HashMap; |
| 31 |
import java.util.Map; |
| 32 |
import java.util.Properties; |
| 33 |
|
| 34 |
import org.freehep.graphics2d.PrintColor; |
| 35 |
import org.freehep.graphics2d.VectorGraphics; |
| 36 |
import org.freehep.graphics2d.font.FontEncoder; |
| 37 |
import org.freehep.graphics2d.font.FontUtilities; |
| 38 |
import org.freehep.graphicsbase.util.UserProperties; |
| 39 |
import org.freehep.graphicsbase.util.images.ImageUtilities; |
| 40 |
import org.freehep.graphicsio.AbstractVectorGraphicsIO; |
| 41 |
import org.freehep.graphicsio.PageConstants; |
| 42 |
import org.freehep.graphicsio.emf.gdi.AlphaBlend; |
| 43 |
import org.freehep.graphicsio.emf.gdi.BeginPath; |
| 44 |
import org.freehep.graphicsio.emf.gdi.CreateBrushIndirect; |
| 45 |
import org.freehep.graphicsio.emf.gdi.DeleteObject; |
| 46 |
import org.freehep.graphicsio.emf.gdi.EOF; |
| 47 |
import org.freehep.graphicsio.emf.gdi.EndPath; |
| 48 |
import org.freehep.graphicsio.emf.gdi.ExtCreateFontIndirectW; |
| 49 |
import org.freehep.graphicsio.emf.gdi.ExtCreatePen; |
| 50 |
import org.freehep.graphicsio.emf.gdi.ExtLogFontW; |
| 51 |
import org.freehep.graphicsio.emf.gdi.ExtLogPen; |
| 52 |
import org.freehep.graphicsio.emf.gdi.ExtTextOutW; |
| 53 |
import org.freehep.graphicsio.emf.gdi.FillPath; |
| 54 |
import org.freehep.graphicsio.emf.gdi.LogBrush32; |
| 55 |
import org.freehep.graphicsio.emf.gdi.ModifyWorldTransform; |
| 56 |
import org.freehep.graphicsio.emf.gdi.RestoreDC; |
| 57 |
import org.freehep.graphicsio.emf.gdi.SaveDC; |
| 58 |
import org.freehep.graphicsio.emf.gdi.SelectClipPath; |
| 59 |
import org.freehep.graphicsio.emf.gdi.SelectObject; |
| 60 |
import org.freehep.graphicsio.emf.gdi.SetBkMode; |
| 61 |
import org.freehep.graphicsio.emf.gdi.SetMapMode; |
| 62 |
import org.freehep.graphicsio.emf.gdi.SetMiterLimit; |
| 63 |
import org.freehep.graphicsio.emf.gdi.SetPolyFillMode; |
| 64 |
import org.freehep.graphicsio.emf.gdi.SetTextAlign; |
| 65 |
import org.freehep.graphicsio.emf.gdi.SetTextColor; |
| 66 |
import org.freehep.graphicsio.emf.gdi.SetViewportExtEx; |
| 67 |
import org.freehep.graphicsio.emf.gdi.SetViewportOrgEx; |
| 68 |
import org.freehep.graphicsio.emf.gdi.SetWindowExtEx; |
| 69 |
import org.freehep.graphicsio.emf.gdi.SetWindowOrgEx; |
| 70 |
import org.freehep.graphicsio.emf.gdi.SetWorldTransform; |
| 71 |
import org.freehep.graphicsio.emf.gdi.StrokeAndFillPath; |
| 72 |
import org.freehep.graphicsio.emf.gdi.StrokePath; |
| 73 |
import org.freehep.graphicsio.emf.gdi.TextW; |
| 74 |
import org.freehep.graphicsio.font.FontTable; |
| 75 |
|
| 76 |
/** |
| 77 |
* Enhanced Metafile Format Graphics 2D driver. |
| 78 |
* |
| 79 |
* @author Mark Donszelmann |
| 80 |
* @version $Id: freehep-graphicsio-emf/src/main/java/org/freehep/graphicsio/emf/EMFGraphics2D.java 59372df5e0d9 2007/02/06 21:11:19 duns $ |
| 81 |
*/ |
| 82 |
public class EMFGraphics2D extends AbstractVectorGraphicsIO implements |
| 83 |
EMFConstants { |
| 84 |
public static final String version = "$Revision$"; |
| 85 |
|
| 86 |
private EMFHandleManager handleManager; |
| 87 |
|
| 88 |
private int penHandle; |
| 89 |
|
| 90 |
private int brushHandle; |
| 91 |
|
| 92 |
private Rectangle imageBounds; |
| 93 |
|
| 94 |
private OutputStream ros; |
| 95 |
|
| 96 |
private EMFOutputStream os; |
| 97 |
|
| 98 |
private Color textColor = null; |
| 99 |
|
| 100 |
private Color penColor = null; |
| 101 |
|
| 102 |
private Color brushColor = null; |
| 103 |
|
| 104 |
private Map<Font, Integer> fontTable; // java fonts |
| 105 |
|
| 106 |
private Map<Font, Font> unitFontTable; // windows fonts |
| 107 |
|
| 108 |
private EMFPathConstructor pathConstructor; |
| 109 |
|
| 110 |
private boolean evenOdd; |
| 111 |
|
| 112 |
private static final Rectangle dummy = new Rectangle(0, 0, 0, 0); |
| 113 |
|
| 114 |
/* |
| 115 |
* ================================================================================ |
| 116 |
* Table of Contents: ------------------ 1. Constructors & Factory Methods |
| 117 |
* 2. Document Settings 3. Header, Trailer, Multipage & Comments 3.1 Header & |
| 118 |
* Trailer 3.2 MultipageDocument methods 4. Create & Dispose 5. Drawing |
| 119 |
* Methods 5.1. shapes (draw/fill) 5.1.1. lines, rectangles, round |
| 120 |
* rectangles 5.1.2. polylines, polygons 5.1.3. ovals, arcs 5.1.4. shapes |
| 121 |
* 5.2. Images 5.3. Strings 6. Transformations 7. Clipping 8. Graphics State / |
| 122 |
* Settings 8.1. stroke/linewidth 8.2. paint/color 8.3. font 8.4. rendering |
| 123 |
* hints 9. Auxiliary 10. Private/Utility Methos |
| 124 |
* ================================================================================ |
| 125 |
*/ |
| 126 |
private static final String rootKey = EMFGraphics2D.class.getName(); |
| 127 |
|
| 128 |
public static final String TRANSPARENT = rootKey + "." |
| 129 |
+ PageConstants.TRANSPARENT; |
| 130 |
|
| 131 |
public static final String BACKGROUND = rootKey + "." |
| 132 |
+ PageConstants.BACKGROUND; |
| 133 |
|
| 134 |
public static final String BACKGROUND_COLOR = rootKey + "." |
| 135 |
+ PageConstants.BACKGROUND_COLOR; |
| 136 |
|
| 137 |
private static final UserProperties defaultProperties = new UserProperties(); |
| 138 |
static { |
| 139 |
defaultProperties.setProperty(TRANSPARENT, true); |
| 140 |
defaultProperties.setProperty(BACKGROUND, false); |
| 141 |
defaultProperties.setProperty(BACKGROUND_COLOR, Color.GRAY); |
| 142 |
defaultProperties.setProperty(CLIP, true); |
| 143 |
// NOTE: using TEXT_AS_SHAPES makes the text shapes quite unreadable. |
| 144 |
defaultProperties.setProperty(TEXT_AS_SHAPES, false); |
| 145 |
} |
| 146 |
|
| 147 |
public static Properties getDefaultProperties() { |
| 148 |
return defaultProperties; |
| 149 |
} |
| 150 |
|
| 151 |
public static void setDefaultProperties(Properties newProperties) { |
| 152 |
defaultProperties.setProperties(newProperties); |
| 153 |
} |
| 154 |
|
| 155 |
/* |
| 156 |
* ================================================================================ |
| 157 |
* 1. Constructors & Factory Methods |
| 158 |
* ================================================================================ |
| 159 |
*/ |
| 160 |
public EMFGraphics2D(File file, Dimension size) |
| 161 |
throws FileNotFoundException { |
| 162 |
this(new FileOutputStream(file), size); |
| 163 |
} |
| 164 |
|
| 165 |
public EMFGraphics2D(File file, Component component) |
| 166 |
throws FileNotFoundException { |
| 167 |
this(new FileOutputStream(file), component); |
| 168 |
} |
| 169 |
|
| 170 |
public EMFGraphics2D(OutputStream os, Dimension size) { |
| 171 |
super(size, false); |
| 172 |
this.imageBounds = new Rectangle(0, 0, size.width, size.height); |
| 173 |
init(os); |
| 174 |
} |
| 175 |
|
| 176 |
public EMFGraphics2D(OutputStream os, Component component) { |
| 177 |
super(component, false); |
| 178 |
this.imageBounds = new Rectangle(0, 0, getSize().width, |
| 179 |
getSize().height); |
| 180 |
init(os); |
| 181 |
} |
| 182 |
|
| 183 |
private void init(OutputStream os) { |
| 184 |
fontTable = new HashMap<Font, Integer>(); |
| 185 |
unitFontTable = new HashMap<Font, Font>(); |
| 186 |
evenOdd = false; |
| 187 |
|
| 188 |
handleManager = new EMFHandleManager(); |
| 189 |
ros = os; |
| 190 |
initProperties(defaultProperties); |
| 191 |
} |
| 192 |
|
| 193 |
protected EMFGraphics2D(EMFGraphics2D graphics, boolean doRestoreOnDispose) { |
| 194 |
super(graphics, doRestoreOnDispose); |
| 195 |
// Create a graphics context from a given graphics context. |
| 196 |
// This constructor is used by the system to clone a given graphics |
| 197 |
// context. |
| 198 |
// doRestoreOnDispose is used to call writeGraphicsRestore(), |
| 199 |
// when the graphics context is being disposed off. |
| 200 |
os = graphics.os; |
| 201 |
imageBounds = graphics.imageBounds; |
| 202 |
handleManager = graphics.handleManager; |
| 203 |
fontTable = graphics.fontTable; |
| 204 |
unitFontTable = graphics.unitFontTable; |
| 205 |
pathConstructor = graphics.pathConstructor; |
| 206 |
evenOdd = graphics.evenOdd; |
| 207 |
textColor = graphics.textColor; |
| 208 |
penColor = graphics.penColor; |
| 209 |
brushColor = graphics.brushColor; |
| 210 |
} |
| 211 |
|
| 212 |
/* |
| 213 |
* ================================================================================ | |
| 214 |
* 2. Document Settings |
| 215 |
* ================================================================================ |
| 216 |
*/ |
| 217 |
|
| 218 |
/* |
| 219 |
* ================================================================================ | |
| 220 |
* 3. Header, Trailer, Multipage & Comments |
| 221 |
* ================================================================================ |
| 222 |
*/ |
| 223 |
/* 3.1 Header & Trailer */ |
| 224 |
public void writeHeader() throws IOException { |
| 225 |
ros = new BufferedOutputStream(ros); |
| 226 |
Dimension device = isDeviceIndependent() ? new Dimension(1024, 768) |
| 227 |
: Toolkit.getDefaultToolkit().getScreenSize(); |
| 228 |
String producer = getClass().getName(); |
| 229 |
if (!isDeviceIndependent()) { |
| 230 |
producer += " " + version.substring(1, version.length() - 1); |
| 231 |
} |
| 232 |
os = new EMFOutputStream(ros, imageBounds, handleManager, getCreator(), |
| 233 |
producer, device); |
| 234 |
pathConstructor = new EMFPathConstructor(os, imageBounds); |
| 235 |
|
| 236 |
Point orig = new Point(imageBounds.x, imageBounds.y); |
| 237 |
Dimension size = new Dimension(imageBounds.width, imageBounds.height); |
| 238 |
|
| 239 |
os.writeTag(new SetMapMode(MM_ANISOTROPIC)); |
| 240 |
os.writeTag(new SetWindowOrgEx(orig)); |
| 241 |
os.writeTag(new SetWindowExtEx(size)); |
| 242 |
os.writeTag(new SetViewportOrgEx(orig)); |
| 243 |
os.writeTag(new SetViewportExtEx(size)); |
| 244 |
os.writeTag(new SetTextAlign(TA_BASELINE)); |
| 245 |
os.writeTag(new SetTextColor(getColor())); |
| 246 |
os.writeTag(new SetPolyFillMode(EMFConstants.WINDING)); |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
public void writeGraphicsState() throws IOException { |
| 251 |
super.writeGraphicsState(); |
| 252 |
// write a special matrix here to scale all written coordinates by a |
| 253 |
// factor of TWIPS |
| 254 |
AffineTransform n = AffineTransform.getScaleInstance(1.0 / TWIPS, |
| 255 |
1.0 / TWIPS); |
| 256 |
os.writeTag(new SetWorldTransform(n)); |
| 257 |
} |
| 258 |
|
| 259 |
public void writeBackground() throws IOException { |
| 260 |
if (isProperty(TRANSPARENT)) { |
| 261 |
setBackground(null); |
| 262 |
os.writeTag(new SetBkMode(BKG_TRANSPARENT)); |
| 263 |
} else if (isProperty(BACKGROUND)) { |
| 264 |
os.writeTag(new SetBkMode(BKG_OPAQUE)); |
| 265 |
setBackground(getPropertyColor(BACKGROUND_COLOR)); |
| 266 |
clearRect(0.0, 0.0, getSize().width, getSize().height); |
| 267 |
} else { |
| 268 |
os.writeTag(new SetBkMode(BKG_OPAQUE)); |
| 269 |
setBackground(getComponent() != null ? getComponent() |
| 270 |
.getBackground() : Color.WHITE); |
| 271 |
clearRect(0.0, 0.0, getSize().width, getSize().height); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
public void writeTrailer() throws IOException { |
| 276 |
// delete any remaining objects |
| 277 |
for (;;) { |
| 278 |
int handle = handleManager.highestHandleInUse(); |
| 279 |
if (handle < 0) |
| 280 |
break; |
| 281 |
os.writeTag(new DeleteObject(handle)); |
| 282 |
handleManager.freeHandle(handle); |
| 283 |
} |
| 284 |
os.writeTag(new EOF()); |
| 285 |
} |
| 286 |
|
| 287 |
public void closeStream() throws IOException { |
| 288 |
os.close(); |
| 289 |
} |
| 290 |
|
| 291 |
/* 3.2 MultipageDocument methods */ |
| 292 |
|
| 293 |
/* |
| 294 |
* ================================================================================ |
| 295 |
* 4. Create & Dispose |
| 296 |
* ================================================================================ |
| 297 |
*/ |
| 298 |
|
| 299 |
public Graphics create() { |
| 300 |
// Create a new graphics context from the current one. |
| 301 |
try { |
| 302 |
// Save the current context for restore later. |
| 303 |
writeGraphicsSave(); |
| 304 |
} catch (IOException e) { |
| 305 |
handleException(e); |
| 306 |
} |
| 307 |
// The correct graphics context should be created. |
| 308 |
return new EMFGraphics2D(this, true); |
| 309 |
} |
| 310 |
|
| 311 |
public Graphics create(double x, double y, double width, double height) { |
| 312 |
// Create a new graphics context from the current one. |
| 313 |
try { |
| 314 |
// Save the current context for restore later. |
| 315 |
writeGraphicsSave(); |
| 316 |
} catch (IOException e) { |
| 317 |
handleException(e); |
| 318 |
} |
| 319 |
// The correct graphics context should be created. |
| 320 |
VectorGraphics graphics = new EMFGraphics2D(this, true); |
| 321 |
graphics.translate(x, y); |
| 322 |
graphics.clipRect(0, 0, width, height); |
| 323 |
return graphics; |
| 324 |
} |
| 325 |
|
| 326 |
protected void writeGraphicsSave() throws IOException { |
| 327 |
os.writeTag(new SaveDC()); |
| 328 |
} |
| 329 |
|
| 330 |
protected void writeGraphicsRestore() throws IOException { |
| 331 |
if (penHandle != 0) |
| 332 |
os.writeTag(new DeleteObject(handleManager.freeHandle(penHandle))); |
| 333 |
if (brushHandle != 0) |
| 334 |
os |
| 335 |
.writeTag(new DeleteObject(handleManager |
| 336 |
.freeHandle(brushHandle))); |
| 337 |
os.writeTag(new RestoreDC()); |
| 338 |
} |
| 339 |
|
| 340 |
/* |
| 341 |
* ================================================================================ | |
| 342 |
* 5. Drawing Methods |
| 343 |
* ================================================================================ |
| 344 |
*/ |
| 345 |
/* 5.1.4. shapes */ |
| 346 |
public void draw(Shape shape) { |
| 347 |
try { |
| 348 |
if (getStroke() instanceof BasicStroke) { |
| 349 |
writePen((BasicStroke) getStroke(), getColor()); |
| 350 |
writePath(shape); |
| 351 |
os.writeTag(new StrokePath(imageBounds)); |
| 352 |
} else { |
| 353 |
writeBrush(getColor()); |
| 354 |
writePath(getStroke().createStrokedShape(shape)); |
| 355 |
os.writeTag(new FillPath(imageBounds)); |
| 356 |
} |
| 357 |
} catch (IOException e) { |
| 358 |
handleException(e); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
public void fill(Shape shape) { |
| 363 |
try { |
| 364 |
if (getPaint() instanceof Color) { |
| 365 |
writeBrush(getColor()); |
| 366 |
writePath(shape); |
| 367 |
os.writeTag(new FillPath(imageBounds)); |
| 368 |
} else { |
| 369 |
// draw paint as image |
| 370 |
fill(shape, getPaint()); |
| 371 |
} |
| 372 |
} catch (IOException e) { |
| 373 |
handleException(e); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
public void fillAndDraw(Shape shape, Color fillColor) { |
| 378 |
try { |
| 379 |
if (getPaint() instanceof Color) { |
| 380 |
writePen((BasicStroke) getStroke(), getColor()); |
| 381 |
writeBrush(fillColor); |
| 382 |
writePath(shape); |
| 383 |
os.writeTag(new StrokeAndFillPath(imageBounds)); |
| 384 |
} else { |
| 385 |
// draw paint as image |
| 386 |
fill(shape, getPaint()); |
| 387 |
// draw shape |
| 388 |
draw(shape); |
| 389 |
} |
| 390 |
} catch (IOException e) { |
| 391 |
handleException(e); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
/* 5.2. Images */ |
| 396 |
public void copyArea(int x, int y, int width, int height, int dx, int dy) { |
| 397 |
writeWarning(getClass() |
| 398 |
+ ": copyArea(int, int, int, int, int, int) not implemented."); |
| 399 |
// Mostly unimplemented. |
| 400 |
} |
| 401 |
|
| 402 |
// NOTE: does not use writeGraphicsSave and writeGraphicsRestore since these |
| 403 |
// delete pen and brush |
| 404 |
protected void writeImage(RenderedImage image, AffineTransform xform, |
| 405 |
Color bkg) throws IOException { |
| 406 |
os.writeTag(new SaveDC()); |
| 407 |
|
| 408 |
AffineTransform imageTransform = new AffineTransform( |
| 409 |
1.0, 0.0, 0.0, -1.0, 0.0, image.getHeight()); |
| 410 |
imageTransform.preConcatenate(xform); |
| 411 |
writeTransform(imageTransform); |
| 412 |
|
| 413 |
BufferedImage bufferedImage = ImageUtilities.createBufferedImage( |
| 414 |
image, null, null); |
| 415 |
AlphaBlend alphaBlend = new AlphaBlend( |
| 416 |
imageBounds, |
| 417 |
toUnit(0), |
| 418 |
toUnit(0), |
| 419 |
toUnit(image.getWidth()), |
| 420 |
toUnit(image.getHeight()), |
| 421 |
new AffineTransform(), |
| 422 |
bufferedImage, |
| 423 |
bkg); |
| 424 |
|
| 425 |
os.writeTag(alphaBlend); |
| 426 |
os.writeTag(new RestoreDC()); |
| 427 |
} |
| 428 |
|
| 429 |
/* 5.3. Strings */ |
| 430 |
public void writeString(String string, double x, double y) |
| 431 |
throws IOException { |
| 432 |
|
| 433 |
Color color; |
| 434 |
Paint paint = getPaint(); |
| 435 |
if (paint instanceof Color) { |
| 436 |
color = (Color) paint; |
| 437 |
} else if (paint instanceof GradientPaint) { |
| 438 |
GradientPaint gp = (GradientPaint) paint; |
| 439 |
color = PrintColor.mixColor(gp.getColor1(), gp.getColor2()); |
| 440 |
} else { |
| 441 |
Color bkg = getBackground(); |
| 442 |
if (bkg == null) { |
| 443 |
color = Color.BLACK; |
| 444 |
} else { |
| 445 |
color = PrintColor.invert(bkg); |
| 446 |
} |
| 447 |
} |
| 448 |
if (!color.equals(textColor)) { |
| 449 |
textColor = color; |
| 450 |
os.writeTag(new SetTextColor(textColor)); |
| 451 |
} |
| 452 |
|
| 453 |
// dialog.bold -> Dialog with TextAttribute.WEIGHT_BOLD |
| 454 |
Map<Attribute, Object> attributes = FontUtilities.getAttributes(getFont()); |
| 455 |
FontTable.normalize(attributes); |
| 456 |
Font font = new Font(attributes); |
| 457 |
|
| 458 |
Font unitFont = unitFontTable.get(font); |
| 459 |
|
| 460 |
Integer fontIndex = fontTable.get(font); |
| 461 |
if (fontIndex == null) { |
| 462 |
// for special fonts (Symbol, ZapfDingbats) we choose a standard |
| 463 |
// font and |
| 464 |
// encode using unicode. |
| 465 |
String fontName = font.getName(); |
| 466 |
string = FontEncoder.getEncodedString(string, fontName); |
| 467 |
|
| 468 |
String windowsFontName = FontUtilities |
| 469 |
.getWindowsFontName(fontName); |
| 470 |
|
| 471 |
unitFont = new Font(windowsFontName, font.getStyle(), font |
| 472 |
.getSize()); |
| 473 |
unitFont = unitFont.deriveFont(font.getSize2D() |
| 474 |
* UNITS_PER_PIXEL * TWIPS); |
| 475 |
unitFontTable.put(font, unitFont); |
| 476 |
|
| 477 |
ExtLogFontW logFontW = new ExtLogFontW(unitFont); |
| 478 |
int handle = handleManager.getHandle(); |
| 479 |
os.writeTag(new ExtCreateFontIndirectW(handle, logFontW)); |
| 480 |
|
| 481 |
fontIndex = new Integer(handle); |
| 482 |
fontTable.put(font, fontIndex); |
| 483 |
} |
| 484 |
os.writeTag(new SelectObject(fontIndex.intValue())); |
| 485 |
|
| 486 |
int[] widths = new int[string.length()]; |
| 487 |
for (int i = 0; i < widths.length; i++) { |
| 488 |
double w = unitFont.getStringBounds(string, i, i + 1, |
| 489 |
getFontRenderContext()).getWidth(); |
| 490 |
widths[i] = (int) w; |
| 491 |
} |
| 492 |
|
| 493 |
// font transformation sould _not_ transform string position |
| 494 |
translate(x, y); |
| 495 |
|
| 496 |
// apply font transformation |
| 497 |
AffineTransform t = font.getTransform(); |
| 498 |
if (!t.isIdentity()) { |
| 499 |
writeGraphicsSave(); |
| 500 |
writeTransform(t); |
| 501 |
} |
| 502 |
|
| 503 |
TextW text = new TextW(new Point(0, 0), string, 0, dummy, widths); |
| 504 |
os.writeTag(new ExtTextOutW(imageBounds, EMFConstants.GM_ADVANCED, 1, 1, text)); |
| 505 |
|
| 506 |
// revert font transformation |
| 507 |
if (!t.isIdentity()) { |
| 508 |
writeGraphicsRestore(); |
| 509 |
} |
| 510 |
|
| 511 |
// translation for string position. |
| 512 |
translate(-x, -y); |
| 513 |
} |
| 514 |
|
| 515 |
/* |
| 516 |
* ================================================================================ | |
| 517 |
* 6. Transformations |
| 518 |
* ================================================================================ |
| 519 |
*/ |
| 520 |
protected void writeTransform(AffineTransform t) throws IOException { |
| 521 |
AffineTransform n = new AffineTransform(t.getScaleX(), t.getShearY(), t |
| 522 |
.getShearX(), t.getScaleY(), t.getTranslateX() |
| 523 |
* UNITS_PER_PIXEL * TWIPS, t.getTranslateY() * UNITS_PER_PIXEL |
| 524 |
* TWIPS); |
| 525 |
os.writeTag(new ModifyWorldTransform(n, EMFConstants.MWT_LEFTMULTIPLY)); |
| 526 |
} |
| 527 |
|
| 528 |
protected void writeSetTransform(AffineTransform t) throws IOException { |
| 529 |
// write a special matrix here to scale all written coordinates by a factor of TWIPS |
| 530 |
AffineTransform n = AffineTransform.getScaleInstance(1.0/TWIPS, 1.0/TWIPS); |
| 531 |
os.writeTag(new SetWorldTransform(n)); |
| 532 |
// apply transform |
| 533 |
writeTransform(t); |
| 534 |
} |
| 535 |
|
| 536 |
/* |
| 537 |
* ================================================================================ | |
| 538 |
* 7. Clipping |
| 539 |
* ================================================================================ |
| 540 |
*/ |
| 541 |
protected void writeSetClip(Shape s) throws IOException { |
| 542 |
if (!isProperty(CLIP)) { |
| 543 |
return; |
| 544 |
} |
| 545 |
|
| 546 |
// if s == null the clip is reset to the imageBounds |
| 547 |
if (s == null && imageBounds != null) { |
| 548 |
s = new Rectangle(imageBounds); |
| 549 |
AffineTransform at = getTransform(); |
| 550 |
if (at != null) { |
| 551 |
s = at.createTransformedShape(s); |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
writePath(s); |
| 556 |
os.writeTag(new SelectClipPath(EMFConstants.RGN_COPY)); |
| 557 |
} |
| 558 |
|
| 559 |
protected void writeClip(Shape s) throws IOException { |
| 560 |
|
| 561 |
if (s == null || !isProperty(CLIP)) { |
| 562 |
return; |
| 563 |
} |
| 564 |
|
| 565 |
writePath(s); |
| 566 |
os.writeTag(new SelectClipPath(EMFConstants.RGN_AND)); |
| 567 |
} |
| 568 |
|
| 569 |
/* |
| 570 |
* ================================================================================ | |
| 571 |
* 8. Graphics State |
| 572 |
* ================================================================================ |
| 573 |
*/ |
| 574 |
public void writeStroke(Stroke stroke) throws IOException { |
| 575 |
if (stroke instanceof BasicStroke) { |
| 576 |
writePen((BasicStroke) stroke, getColor()); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
/* 8.2. paint/color */ |
| 581 |
public void setPaintMode() { |
| 582 |
writeWarning(getClass() + ": setPaintMode() not implemented."); |
| 583 |
// Mostly unimplemented. |
| 584 |
} |
| 585 |
|
| 586 |
public void setXORMode(Color c1) { |
| 587 |
writeWarning(getClass() + ": setXORMode(Color) not implemented."); |
| 588 |
// Mostly unimplemented. |
| 589 |
} |
| 590 |
|
| 591 |
protected void writePaint(Color p) throws IOException { |
| 592 |
// all color setting delayed |
| 593 |
} |
| 594 |
|
| 595 |
protected void writePaint(GradientPaint p) throws IOException { |
| 596 |
// all paint setting delayed |
| 597 |
} |
| 598 |
|
| 599 |
protected void writePaint(TexturePaint p) throws IOException { |
| 600 |
// all paint setting delayed |
| 601 |
} |
| 602 |
|
| 603 |
protected void writePaint(Paint p) throws IOException { |
| 604 |
// all paint setting delayed |
| 605 |
} |
| 606 |
|
| 607 |
/* 8.3. font */ |
| 608 |
protected void writeFont(Font font) throws IOException { |
| 609 |
// written when needed |
| 610 |
} |
| 611 |
|
| 612 |
/* 8.4. rendering hints */ |
| 613 |
|
| 614 |
/* |
| 615 |
* ================================================================================ | |
| 616 |
* 9. Auxiliary |
| 617 |
* ================================================================================ |
| 618 |
*/ |
| 619 |
public GraphicsConfiguration getDeviceConfiguration() { |
| 620 |
writeWarning(getClass() + ": getDeviceConfiguration() not implemented."); |
| 621 |
// Mostly unimplemented |
| 622 |
return null; |
| 623 |
} |
| 624 |
|
| 625 |
public void writeComment(String comment) throws IOException { |
| 626 |
writeWarning(getClass() + ": writeComment(String) not implemented."); |
| 627 |
// Write out the comment. |
| 628 |
} |
| 629 |
|
| 630 |
public String toString() { |
| 631 |
return "EMFGraphics2D"; |
| 632 |
} |
| 633 |
|
| 634 |
/** |
| 635 |
* Implementation of createShape makes sure that the points are different by |
| 636 |
* at least one Unit. |
| 637 |
*/ |
| 638 |
protected Shape createShape(double[] xPoints, double[] yPoints, |
| 639 |
int nPoints, boolean close) { |
| 640 |
GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD); |
| 641 |
if (nPoints > 0) { |
| 642 |
path.moveTo((float) xPoints[0], (float) yPoints[0]); |
| 643 |
double lastX = xPoints[0]; |
| 644 |
double lastY = yPoints[0]; |
| 645 |
if (close && (Math.abs(xPoints[nPoints - 1] - lastX) < 1) |
| 646 |
&& (Math.abs(yPoints[nPoints - 1] - lastY) < 1)) { |
| 647 |
nPoints--; |
| 648 |
} |
| 649 |
for (int i = 1; i < nPoints; i++) { |
| 650 |
if ((Math.abs(xPoints[i] - lastX) > 1) |
| 651 |
|| (Math.abs(yPoints[i] - lastY) > 1)) { |
| 652 |
path.lineTo((float) xPoints[i], (float) yPoints[i]); |
| 653 |
lastX = xPoints[i]; |
| 654 |
lastY = yPoints[i]; |
| 655 |
} |
| 656 |
} |
| 657 |
if (close) |
| 658 |
path.closePath(); |
| 659 |
} |
| 660 |
return path; |
| 661 |
} |
| 662 |
|
| 663 |
/* |
| 664 |
* Private methods |
| 665 |
*/ |
| 666 |
private boolean writePath(Shape shape) throws IOException { |
| 667 |
boolean eo = EMFPathConstructor.isEvenOdd(shape); |
| 668 |
if (eo != evenOdd) { |
| 669 |
evenOdd = eo; |
| 670 |
os.writeTag(new SetPolyFillMode((evenOdd) ? EMFConstants.ALTERNATE |
| 671 |
: EMFConstants.WINDING)); |
| 672 |
} |
| 673 |
os.writeTag(new BeginPath()); |
| 674 |
pathConstructor.addPath(shape); |
| 675 |
os.writeTag(new EndPath()); |
| 676 |
return evenOdd; |
| 677 |
} |
| 678 |
|
| 679 |
private void writePen(BasicStroke stroke, Color color) throws IOException { |
| 680 |
if (color.equals(penColor) && stroke.equals(getStroke())) |
| 681 |
return; |
| 682 |
penColor = color; |
| 683 |
|
| 684 |
int style = EMFConstants.PS_GEOMETRIC; |
| 685 |
|
| 686 |
switch (stroke.getEndCap()) { |
| 687 |
case BasicStroke.CAP_BUTT: |
| 688 |
style |= EMFConstants.PS_ENDCAP_FLAT; |
| 689 |
break; |
| 690 |
case BasicStroke.CAP_ROUND: |
| 691 |
style |= EMFConstants.PS_ENDCAP_ROUND; |
| 692 |
break; |
| 693 |
case BasicStroke.CAP_SQUARE: |
| 694 |
style |= EMFConstants.PS_ENDCAP_SQUARE; |
| 695 |
break; |
| 696 |
} |
| 697 |
|
| 698 |
switch (stroke.getLineJoin()) { |
| 699 |
case BasicStroke.JOIN_MITER: |
| 700 |
style |= EMFConstants.PS_JOIN_MITER; |
| 701 |
break; |
| 702 |
case BasicStroke.JOIN_ROUND: |
| 703 |
style |= EMFConstants.PS_JOIN_ROUND; |
| 704 |
break; |
| 705 |
case BasicStroke.JOIN_BEVEL: |
| 706 |
style |= EMFConstants.PS_JOIN_BEVEL; |
| 707 |
break; |
| 708 |
} |
| 709 |
|
| 710 |
// FIXME int conversion |
| 711 |
// FIXME phase ignored |
| 712 |
float[] dashArray = stroke.getDashArray(); |
| 713 |
int[] dash = new int[(dashArray != null) ? dashArray.length : 0]; |
| 714 |
style |= (dash.length == 0) ? EMFConstants.PS_SOLID |
| 715 |
: EMFConstants.PS_USERSTYLE; |
| 716 |
for (int i = 0; i < dash.length; i++) { |
| 717 |
dash[i] = toUnit(dashArray[i]); |
| 718 |
} |
| 719 |
|
| 720 |
int brushStyle = (color.getAlpha() == 0) ? EMFConstants.BS_NULL |
| 721 |
: EMFConstants.BS_SOLID; |
| 722 |
|
| 723 |
ExtLogPen pen = new ExtLogPen(style, toUnit(stroke.getLineWidth()), |
| 724 |
brushStyle, getPrintColor(color), 0, dash); |
| 725 |
if (penHandle != 0) { |
| 726 |
os.writeTag(new DeleteObject(penHandle)); |
| 727 |
} else { |
| 728 |
penHandle = handleManager.getHandle(); |
| 729 |
} |
| 730 |
os.writeTag(new ExtCreatePen(penHandle, pen)); |
| 731 |
os.writeTag(new SelectObject(penHandle)); |
| 732 |
|
| 733 |
if (!(getStroke() instanceof BasicStroke) |
| 734 |
|| (((BasicStroke) getStroke()).getMiterLimit() != stroke |
| 735 |
.getMiterLimit())) { |
| 736 |
os.writeTag(new SetMiterLimit(toUnit(stroke.getMiterLimit()))); |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
private void writeBrush(Color color) throws IOException { |
| 741 |
if (color.equals(brushColor)) |
| 742 |
return; |
| 743 |
brushColor = color; |
| 744 |
|
| 745 |
int brushStyle = (color.getAlpha() == 0) ? EMFConstants.BS_NULL |
| 746 |
: EMFConstants.BS_SOLID; |
| 747 |
|
| 748 |
LogBrush32 brush = new LogBrush32(brushStyle, getPrintColor(color), 0); |
| 749 |
if (brushHandle != 0) { |
| 750 |
os.writeTag(new DeleteObject(brushHandle)); |
| 751 |
} else { |
| 752 |
brushHandle = handleManager.getHandle(); |
| 753 |
} |
| 754 |
os.writeTag(new CreateBrushIndirect(brushHandle, brush)); |
| 755 |
os.writeTag(new SelectObject(brushHandle)); |
| 756 |
} |
| 757 |
|
| 758 |
private int toUnit(double d) { |
| 759 |
return (int) Math.floor(d * UNITS_PER_PIXEL * TWIPS); |
| 760 |
} |
| 761 |
} |