Debian Codegraph @

freehep-graphicsio-swf/src/main/java/org/freehep/graphicsio/swf/SWFGraphics2D.java

raw symbols SCIP
1 // Copyright 2000-2006 FreeHEP
2 package org.freehep.graphicsio.swf;
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.Rectangle;
14 import java.awt.Shape;
15 import java.awt.Stroke;
16 import java.awt.TexturePaint;
17 import java.awt.font.GlyphVector;
18 import java.awt.geom.AffineTransform;
19 import java.awt.geom.Area;
20 import java.awt.geom.Rectangle2D;
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.util.Properties;
30 import java.util.Vector;
31
32 import org.freehep.graphics2d.PrintColor;
33 import org.freehep.graphics2d.VectorGraphics;
34 import org.freehep.graphics2d.font.FontEncoder;
35 import org.freehep.graphicsbase.util.UserProperties;
36 import org.freehep.graphicsbase.util.Value;
37 import org.freehep.graphicsio.AbstractVectorGraphicsIO;
38 import org.freehep.graphicsio.ImageConstants;
39 import org.freehep.graphicsio.PageConstants;
40 import org.freehep.graphicsio.swf.DefineText.Record;
41
42 /**
43 * SWF Graphics 2D driver.
44 *
45 * @author Mark Donszelmann
46 * @version $Id: freehep-graphicsio-swf/src/main/java/org/freehep/graphicsio/swf/SWFGraphics2D.java 9fd14b4f13ca 2006/11/28 00:22:04 duns $
47 */
48 public class SWFGraphics2D extends AbstractVectorGraphicsIO implements
49 SWFConstants {
50
51 private static final int SWF_VERSION = 8; // for cyclic gradients
52 private static final String rootKey = SWFGraphics2D.class.getName();
53
54 public static final String TRANSPARENT = rootKey + "."
55 + PageConstants.TRANSPARENT;
56
57 public static final String BACKGROUND = rootKey + "."
58 + PageConstants.BACKGROUND;
59
60 public static final String BACKGROUND_COLOR = rootKey + "."
61 + PageConstants.BACKGROUND_COLOR;
62
63 public static final String WRITE_IMAGES_AS = rootKey + "."
64 + ImageConstants.WRITE_IMAGES_AS;
65
66 private static final UserProperties defaultProperties = new UserProperties();
67 static {
68 defaultProperties.setProperty(TRANSPARENT, true);
69 defaultProperties.setProperty(BACKGROUND, false);
70 defaultProperties.setProperty(BACKGROUND_COLOR, Color.GRAY);
71 defaultProperties.setProperty(WRITE_IMAGES_AS, ImageConstants.SMALLEST);
72 // Seems not to work yet...
73 defaultProperties.setProperty(CLIP, false);
74 defaultProperties.setProperty(TEXT_AS_SHAPES, true);
75 }
76
77 public static Properties getDefaultProperties() {
78 return defaultProperties;
79 }
80
81 public static void setDefaultProperties(Properties newProperties) {
82 defaultProperties.setProperties(newProperties);
83 }
84
85 public final static String version = "$Revision$";
86
87 private OutputStream ros;
88
89 private SWFOutputStream os;
90
91 private Value id;
92
93 private Value depth;
94
95 private static final float frameRate = 20.0f;
96
97 private boolean compress = true;
98
99 private LineStyleArray lineStyles;
100
101 private FillStyleArray fillStyles;
102
103 // keeps the color for text, which cannot be a paint
104 private Color textColor;
105
106 // keeps the last clip which has not been written yet, its ID and depth
107 private Shape unwrittenClip = null;
108
109 private AffineTransform clipTransform = null;
110
111 private int clipID, clipDepthID, showClipID, showClipDepthID;
112
113 // for rendering shapes
114 boolean fillStroke;
115
116 // for debugging
117 private static final boolean showBounds = false;
118
119 /*
120 * ================================================================================
121 * Table of Contents: ------------------ 1. Constructors & Factory Methods
122 * 2. Document Settings 3. Header, Trailer, Multipage & Comments 3.1 Header &
123 * Trailer 3.2 MultipageDocument methods 4. Create & Dispose 5. Drawing
124 * Methods 5.1. shapes (draw/fill) 5.1.1. lines, rectangles, round
125 * rectangles 5.1.2. polylines, polygons 5.1.3. ovals, arcs 5.1.4. shapes
126 * 5.2. Images 5.3. Strings 6. Transformations 7. Clipping 8. Graphics State /
127 * Settings 8.1. stroke/linewidth 8.2. paint/color 8.3. font 8.4. rendering
128 * hints 9. Auxiliary 10. Private/Utility Methos
129 * ================================================================================
130 */
131
132 /*
133 * ================================================================================
134 * 1. Constructors & Factory Methods
135 * ================================================================================
136 */
137 public SWFGraphics2D(File file, Dimension size)
138 throws FileNotFoundException {
139 this(new FileOutputStream(file), size);
140 }
141
142 public SWFGraphics2D(File file, Component component)
143 throws FileNotFoundException {
144 this(new FileOutputStream(file), component);
145 }
146
147 public SWFGraphics2D(OutputStream os, Dimension size) {
148 super(size, true);
149 init(os);
150 }
151
152 public SWFGraphics2D(OutputStream os, Component component) {
153 super(component, true);
154 init(os);
155 }
156
157 private void init(OutputStream os) {
158 initProperties(getDefaultProperties());
159 ros = os;
160 id = new Value().set(1);
161 depth = new Value().set(1);
162 textColor = getColor();
163 fillStroke = true;
164 }
165
166 protected SWFGraphics2D(SWFGraphics2D graphics, boolean doRestoreOnDispose) {
167 super(graphics, doRestoreOnDispose);
168 // Create a graphics context from a given graphics context.
169 // This constructor is used by the system to clone a given graphics
170 // context.
171 // doRestoreOnDispose is used to call writeGraphicsRestore(),
172 // when the graphics context is being disposed off.
173 os = graphics.os;
174 id = graphics.id;
175 depth = graphics.depth;
176 lineStyles = new LineStyleArray();
177 lineStyles.add(graphics.lineStyles.get(0));
178 if (showBounds || isProperty(CLIP)) {
179 lineStyles.add(graphics.lineStyles.get(1));
180 lineStyles.add(graphics.lineStyles.get(2));
181 }
182 fillStyles = new FillStyleArray();
183 fillStyles.add(graphics.fillStyles.get(0));
184 textColor = graphics.textColor;
185 fillStroke = graphics.fillStroke;
186 }
187
188 /*
189 * ================================================================================ |
190 * 2. Document Settings
191 * ================================================================================
192 */
193
194 /*
195 * ================================================================================ |
196 * 3. Header, Trailer, Multipage & Comments
197 * ================================================================================
198 */
199 /* 3.1 Header & Trailer */
200 public void writeHeader() throws IOException {
201 os = new SWFOutputStream(new BufferedOutputStream(ros), SWF_VERSION, getSize(),
202 frameRate, compress);
203
204 String description = getCreator() + ":" + getClass().getName();
205 if (!isDeviceIndependent()) {
206 description += " " + version.substring(1, version.length() - 1);
207 }
208 // FIXME no way to write out a comment...
209 }
210
211 public void writeBackground() throws IOException {
212 if (isProperty(TRANSPARENT)) {
213 setBackground(null);
214 } else if (isProperty(BACKGROUND)) {
215 setBackground(getPropertyColor(BACKGROUND_COLOR));
216 clearRect(0.0, 0.0, getSize().width, getSize().height);
217 } else {
218 setBackground(getComponent() != null ? getComponent()
219 .getBackground() : Color.WHITE);
220 clearRect(0.0, 0.0, getSize().width, getSize().height);
221 }
222 }
223
224 public void writeTrailer() throws IOException {
225 os.writeTag(new ShowFrame());
226 os.writeTag(new End());
227 }
228
229 public void closeStream() throws IOException {
230 os.close();
231 }
232
233 /* 3.2 MultipageDocument methods */
234
235 /*
236 * ================================================================================
237 * 4. Create & Dispose
238 * ================================================================================
239 */
240
241 public Graphics create() {
242 // Create a new graphics context from the current one.
243 try {
244 // Save the current context for restore later.
245 writeGraphicsSave();
246 } catch (IOException e) {
247 handleException(e);
248 }
249 // The correct graphics context should be created.
250 return new SWFGraphics2D(this, true);
251 }
252
253 public Graphics create(double x, double y, double width, double height) {
254 // Create a new graphics context from the current one.
255 try {
256 // Save the current context for restore later.
257 writeGraphicsSave();
258 } catch (IOException e) {
259 handleException(e);
260 }
261 // The correct graphics context should be created.
262 VectorGraphics graphics = new SWFGraphics2D(this, true);
263 graphics.translate(x, y);
264 graphics.clipRect(0, 0, width, height);
265 return graphics;
266 }
267
268 protected void writeGraphicsSave() throws IOException {
269 }
270
271 protected void writeGraphicsRestore() throws IOException {
272 popStreamAndWriteClip();
273 }
274
275 /*
276 * ================================================================================ |
277 * 5. Drawing Methods
278 * ================================================================================
279 */
280 /* 5.1.4. shapes */
281
282 public void draw(Shape shape) {
283 Shape strokedShape = getStroke().createStrokedShape(shape);
284 if (fillStroke) {
285 // do this for dashed lines and non-round linejoins and linecaps
286 fill(new Area(strokedShape));
287 return;
288 }
289
290 try {
291 Rectangle2D bounds = strokedShape.getBounds2D();
292 SWFShape swfShape = createShape(shape, 1, 0, -1);
293 os.writeTag(new DefineShape4(id.getInt(), bounds, fillStyles,
294 lineStyles, swfShape));
295 os.writeTag(new PlaceObject2(id.getInt(), depth.getInt(),
296 getTransform()));
297 id.set(id.getInt() + 1);
298 depth.set(depth.getInt() + 1);
299
300 if (showBounds) {
301 SWFShape swfBounds = createShape(bounds, 2, 0, -1);
302 os.writeTag(new DefineShape4(id.getInt(), bounds, fillStyles,
303 lineStyles, swfBounds));
304 os.writeTag(new PlaceObject2(id.getInt(), depth.getInt(),
305 getTransform()));
306 id.set(id.getInt() + 1);
307 depth.set(depth.getInt() + 1);
308 }
309
310 } catch (IOException e) {
311 handleException(e);
312 }
313 }
314
315 public void fill(Shape shape) {
316 try {
317 Rectangle2D bounds = new BasicStroke().createStrokedShape(shape)
318 .getBounds2D();
319 SWFShape swfShape = createShape(shape, 0, 1, -1);
320 os.writeTag(new DefineShape4(id.getInt(), bounds, fillStyles,
321 lineStyles, swfShape));
322 os.writeTag(new PlaceObject2(id.getInt(), depth.getInt(),
323 getTransform()));
324 id.set(id.getInt() + 1);
325 depth.set(depth.getInt() + 1);
326
327 if (showBounds) {
328 SWFShape swfBounds = createShape(bounds, 2, 0, -1);
329 os.writeTag(new DefineShape4(id.getInt(), bounds, fillStyles,
330 lineStyles, swfBounds));
331 os.writeTag(new PlaceObject2(id.getInt(), depth.getInt(),
332 getTransform()));
333 id.set(id.getInt() + 1);
334 depth.set(depth.getInt() + 1);
335 }
336
337 boolean eo = SWFPathConstructor.isEvenOdd(shape);
338 if (!eo) {
339 writeWarning(getClass()
340 + ": cannot fill using non-zero winding rule, used even-odd instead.");
341 }
342 } catch (IOException e) {
343 handleException(e);
344 }
345 }
346
347 public void fillAndDraw(Shape shape, Color fillColor) {
348 try {
349 setFillColor(fillColor);
350 fill(shape);
351 setFillColor(getColor());
352 draw(shape);
353 } catch (IOException e) {
354 handleException(e);
355 }
356 }
357
358 /* 5.2. Images */
359 public void copyArea(int x, int y, int width, int height, int dx, int dy) {
360 writeWarning(getClass()
361 + ": copyArea(int, int, int, int, int, int) not implemented.");
362 // Mostly unimplemented.
363 }
364
365 protected void writeImage(RenderedImage image, AffineTransform xform,
366 Color bkg) throws IOException {
367
368 // define image
369 int imageID = id.getInt();
370 os.writeTag(getImageTag(imageID, image, bkg));
371 id.set(id.getInt() + 1);
372
373 // define shape for image
374 Shape shape = xform.createTransformedShape(new Rectangle(0, 0, image
375 .getWidth(), image.getHeight()));
376
377 AffineTransform imageTransform = new AffineTransform(TWIPS, 0, 0,
378 TWIPS, 0, 0);
379 xform.concatenate(imageTransform);
380
381 // create fill from shape
382 SWFShape imageShape = createShape(shape, 0, 0, 1);
383 FillStyleArray imageFill = new FillStyleArray();
384 imageFill.add(new FillStyle(imageID, false, xform));
385
386 LineStyleArray imageLine = new LineStyleArray();
387 Rectangle bounds = shape.getBounds();
388 os.writeTag(new DefineShape4(id.getInt(), bounds, imageFill, imageLine,
389 imageShape));
390
391 // place image
392 os.writeTag(new PlaceObject2(id.getInt(), depth.getInt(),
393 getTransform()));
394 id.set(id.getInt() + 1);
395 depth.set(depth.getInt() + 1);
396
397 if (showBounds) {
398 SWFShape swfBounds = createShape(bounds, 2, 0, -1);
399 os.writeTag(new DefineShape4(id.getInt(), bounds, fillStyles,
400 lineStyles, swfBounds));
401 os.writeTag(new PlaceObject2(id.getInt(), depth.getInt(),
402 getTransform()));
403 id.set(id.getInt() + 1);
404 depth.set(depth.getInt() + 1);
405 }
406 }
407
408 private final static Properties replaceFonts = new Properties();
409 static {
410 replaceFonts.setProperty("Symbol", "Serif");
411 replaceFonts.setProperty("ZapfDingbats", "Serif");
412 }
413
414 /* 5.3. Strings */
415 protected void writeString(String string, double x, double y)
416 throws IOException {
417 // for special fonts (Symbol, ZapfDingbats) we choose a standard font
418 // and
419 // encode using unicode.
420 String fontName = getFont().getName();
421 string = FontEncoder.getEncodedString(string, fontName);
422 fontName = replaceFonts.getProperty(fontName, null);
423
424 Font font = (fontName == null) ? getFont() : new Font(fontName,
425 getFont().getStyle(), getFont().getSize());
426 Font font1024 = font.deriveFont((float) (1024.0 / TWIPS));
427
428 GlyphVector glyphs = font1024.createGlyphVector(getFontRenderContext(),
429 string);
430 Rectangle2D bounds = font.createGlyphVector(getFontRenderContext(),
431 string).getVisualBounds();
432 bounds.setRect(bounds.getX() + x, bounds.getY() + y, bounds.getWidth(),
433 bounds.getHeight());
434
435 // define the Glyphs
436 int fontID = id.getInt();
437 DefineFont swfFont = new DefineFont(fontID);
438 id.set(id.getInt() + 1);
439
440 // define Text
441 Vector<Record> text = new Vector<Record>();
442 DefineText2.RecordType1 record1 = new DefineText2.RecordType1(fontID,
443 textColor, (int) (x * TWIPS), (int) (y * TWIPS), (int) (font
444 .getSize2D() * TWIPS));
445 DefineText2.RecordType0 record0 = new DefineText2.RecordType0();
446 text.add(record1);
447 text.add(record0);
448 int textID = id.getInt();
449 DefineText2 swfText = new DefineText2(textID, bounds,
450 new AffineTransform(), text);
451 id.set(id.getInt() + 1);
452
453 // hook font and text
454 for (int i = 0; i < glyphs.getNumGlyphs(); i++) {
455 // add filled shapes to font
456 swfFont.add(createShape(glyphs.getGlyphOutline(i), -1, 1, -1));
457
458 // add glyphs to text
459 // float advance = glyphs.getGlyphMetrics(i).getAdvance();
460 record0.add(new DefineText2.GlyphEntry(i, 0)); // (int)(advance*TWIPS)));
461 }
462
463 // write font and text
464 os.writeTag(swfFont);
465 os.writeTag(swfText);
466
467 // place String
468 os.writeTag(new PlaceObject2(textID, depth.getInt(), getTransform()));
469 depth.set(depth.getInt() + 1);
470
471 if (showBounds) {
472 SWFShape swfBounds = createShape(bounds, 2, 0, -1);
473 os.writeTag(new DefineShape4(id.getInt(), bounds, fillStyles,
474 lineStyles, swfBounds));
475 os.writeTag(new PlaceObject2(id.getInt(), depth.getInt(),
476 getTransform()));
477 id.set(id.getInt() + 1);
478 depth.set(depth.getInt() + 1);
479 }
480 }
481
482 /*
483 * ================================================================================ |
484 * 6. Transformations
485 * ================================================================================
486 */
487
488 protected void writeTransform(AffineTransform t) throws IOException {
489 // Transforms written when needed
490 }
491
492 protected void writeSetTransform(AffineTransform t) throws IOException {
493 // Transforms written when needed
494 }
495
496 /*
497 * ================================================================================ |
498 * 7. Clipping
499 * ================================================================================
500 */
501 protected void writeSetClip(Shape s) throws IOException {
502 writeClip(s);
503 }
504
505 protected void writeClip(Shape s) throws IOException {
506 // we assume we can write nested clips
507 popStreamAndWriteClip();
508
509 unwrittenClip = s;
510 clipTransform = (unwrittenClip != null) ? new AffineTransform(
511 getTransform()) : null;
512
513 if (unwrittenClip != null) {
514 // reserve IDs
515 clipID = id.getInt();
516 id.set(id.getInt() + 1);
517 clipDepthID = depth.getInt();
518 depth.set(depth.getInt() + 1);
519
520 if (isProperty(CLIP)) {
521 showClipID = id.getInt();
522 id.set(id.getInt() + 1);
523 showClipDepthID = depth.getInt();
524 depth.set(depth.getInt() + 1);
525 }
526
527 os.pushBuffer();
528 } else {
529 clipID = 0;
530 clipDepthID = 0;
531
532 if (isProperty(CLIP)) {
533 showClipID = 0;
534 showClipDepthID = 0;
535 }
536 }
537 }
538
539 /*
540 * ================================================================================ |
541 * 8. Graphics State
542 * ================================================================================
543 */
544 protected void writeStroke(Stroke stroke) throws IOException {
545 fillStroke = true;
546 if (stroke instanceof BasicStroke) {
547 BasicStroke bs = (BasicStroke) stroke;
548 setPen(bs, getColor());
549 if (bs.getLineWidth() == 0) {
550 fillStroke = false;
551 }
552 }
553 }
554
555 /* 8.2. paint/color */
556 public void setPaintMode() {
557 writeWarning(getClass() + ": setPaintMode() not implemented.");
558 // Mostly unimplemented.
559 }
560
561 public void setXORMode(Color c1) {
562 writeWarning(getClass() + ": setXORMode(Color) not implemented.");
563 // Mostly unimplemented.
564 }
565
566 protected void writePaint(Color p) throws IOException {
567 setPen((BasicStroke) getStroke(), p);
568 setFillColor(p);
569 textColor = PrintColor.createPrintColor(p);
570 }
571
572 protected void writePaint(GradientPaint p) throws IOException {
573 Gradient[] gradient = new Gradient[2];
574 gradient[0] = new Gradient(0, p.getColor1());
575 gradient[1] = new Gradient(255, p.getColor2());
576 double x0 = p.getPoint1().getX();
577 double y0 = p.getPoint1().getY();
578 double dx = p.getPoint2().getX() - x0;
579 double dy = p.getPoint2().getY() - y0;
580 double scale = p.getPoint1().distance(p.getPoint2()) * TWIPS / 32768;
581 double angle = Math.atan2(dy, dx);
582 AffineTransform transform = new AffineTransform(scale, 0, 0, scale, dx
583 / 2 + x0, dy / 2 + y0);
584 transform.rotate(angle);
585
586 int spreadMode = p.isCyclic() ? FillStyle.SPREAD_MODE_REFLECT : FillStyle.SPREAD_MODE_PAD;
587 fillStyles = new FillStyleArray();
588 fillStyles.add(new FillStyle(gradient, FillStyle.LINEAR_GRADIENT, spreadMode, FillStyle.INTERPOLATION_MODE_NORMAL_RGB, 0.0f, transform));
589
590 textColor = PrintColor.mixColor(p.getColor1(), p.getColor2());
591 }
592
593 protected void writePaint(TexturePaint p) throws IOException {
594 // define image
595 BufferedImage image = p.getImage();
596 int imageID = id.getInt();
597 os.writeTag(getImageTag(imageID, image, null));
598 id.set(id.getInt() + 1);
599
600 // setup image as fill
601 Rectangle2D a = p.getAnchorRect();
602 double sx = a.getWidth() / image.getWidth();
603 double sy = a.getHeight() / image.getHeight();
604 fillStyles = new FillStyleArray();
605 fillStyles.add(new FillStyle(imageID, true, new AffineTransform(sx
606 * TWIPS, 0, 0, sy * TWIPS, a.getX(), a.getY())));
607 textColor = PrintColor.black;
608 }
609
610 protected void writePaint(Paint p) throws IOException {
611 writeWarning(getClass() + ": writePaint(Paint) not implemented for "
612 + p.getClass());
613 // Write out the paint.
614 writePaint(Color.BLACK);
615 }
616
617 /* 8.3. font */
618 protected void writeFont(Font font) throws IOException {
619 // written when needed
620 }
621
622 /* 8.4. rendering hints */
623
624 /*
625 * ================================================================================ |
626 * 9. Auxiliary
627 * ================================================================================
628 */
629 public GraphicsConfiguration getDeviceConfiguration() {
630 writeWarning(getClass() + ": getDeviceConfiguration() not implemented.");
631 // Mostly unimplemented
632 return null;
633 }
634
635 public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
636 writeWarning(getClass()
637 + ": hit(Rectangle, Shape, boolean) not implemented.");
638 // Mostly unimplemented
639 return false;
640 }
641
642 public void writeComment(String comment) throws IOException {
643 writeWarning(getClass() + ": writeComment(String) not implemented.");
644 // Write out the comment.
645 }
646
647 public String toString() {
648 return "SWFGraphics2D";
649 }
650
651 /*
652 * Private methods
653 */
654 SWFShape createShape(Shape shape, int stroke, int fill0, int fill1)
655 throws IOException {
656 // use a resolution compatible with the current transform
657 AffineTransform t = getTransform();
658 double resolution = 0.5 / (TWIPS * Math.min(t.getScaleX(), t
659 .getScaleY()));
660
661 Vector<org.freehep.graphicsio.swf.SWFShape.Record> path = new Vector<org.freehep.graphicsio.swf.SWFShape.Record>();
662 SWFPathConstructor pc = new SWFPathConstructor(path, stroke, fill0,
663 fill1, resolution);
664 pc.addPath(shape);
665 return new SWFShape(path);
666 }
667
668 private void setPen(BasicStroke stroke, Color color) throws IOException {
669 lineStyles = new LineStyleArray();
670 lineStyles.add(new LineStyle((int) (stroke.getLineWidth() * TWIPS), getPrintColor(color)));
671
672 if (showBounds || isProperty(CLIP)) {
673 lineStyles.add(new LineStyle(TWIPS, Color.cyan));
674 lineStyles.add(new LineStyle(TWIPS, Color.orange));
675 }
676 }
677
678 private void setFillColor(Color color) throws IOException {
679 fillStyles = new FillStyleArray();
680 fillStyles.add(new FillStyle(getPrintColor(color)));
681 }
682
683 private void popStreamAndWriteClip() throws IOException {
684 if (unwrittenClip == null)
685 return;
686
687 if ((clipID == 0) || (clipDepthID == 0)) {
688 System.err
689 .println("SWFGraphics2D: internal error, invalid clipID or clipDepthID");
690 return;
691 }
692
693 // pop buffer
694 os.popBuffer();
695
696 Rectangle2D bounds = unwrittenClip.getBounds2D();
697 SWFShape clipShape = createShape(unwrittenClip, 0, 1, -1);
698 os.writeTag(new DefineShape4(clipID, bounds, fillStyles, lineStyles,
699 clipShape));
700
701 int clipDepth = depth.getInt() - 1;
702 os.writeTag(new PlaceObject2(clipID, clipDepthID, clipTransform,
703 clipDepth));
704
705 // System.out.println("Clip "+clipID+" at depth "+clipDepthID+" for
706 // clipDepth "+clipDepth+": "+unwrittenClip);
707
708 if (isProperty(CLIP)) {
709 if ((showClipID == 0) || (showClipDepthID == 0)) {
710 System.err
711 .println("SWFGraphics2D: internal error, invalid showClipID or showClipDepthID");
712 return;
713 }
714
715 SWFShape swfBounds = createShape(bounds, 3, 0, -1);
716 os.writeTag(new DefineShape4(showClipID, bounds, fillStyles,
717 lineStyles, swfBounds));
718 os.writeTag(new PlaceObject(showClipID, showClipDepthID,
719 clipTransform));
720 }
721
722 // append popped buffer;
723 os.append();
724 }
725
726 private SWFTag getImageTag(int imageID, RenderedImage image, Color bkg)
727 throws IOException {
728 String writeAs = getProperty(WRITE_IMAGES_AS);
729
730 DefineBitsLossless2 flateTag = null;
731 if (writeAs.equals(ImageConstants.ZLIB)
732 || writeAs.equals(ImageConstants.SMALLEST)) {
733 flateTag = new DefineBitsLossless2(imageID, image, bkg);
734 }
735
736 DefineBitsJPEG3 jpgTag = null;
737 if (writeAs.equals(ImageConstants.JPG)
738 || writeAs.equals(ImageConstants.SMALLEST)) {
739 jpgTag = new DefineBitsJPEG3(imageID, image, bkg, new Properties());
740 }
741
742 SWFTag imageTag;
743 if (writeAs.equals(ImageConstants.ZLIB)) {
744 imageTag = flateTag;
745 } else if (writeAs.equals(ImageConstants.JPG)) {
746 imageTag = jpgTag;
747 } else {
748 imageTag = (jpgTag.getLength() < 0.5 * flateTag.getLength()) ? (SWFTag) jpgTag
749 : (SWFTag) flateTag;
750 }
751
752 return imageTag;
753 }
754 }