001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017: package org.apache.poi.hslf.model;
018:
019: import java.awt.*;
020: import java.awt.Shape;
021: import java.awt.font.FontRenderContext;
022: import java.awt.font.GlyphVector;
023: import java.awt.image.*;
024: import java.awt.image.renderable.RenderableImage;
025: import java.awt.geom.AffineTransform;
026: import java.awt.geom.PathIterator;
027: import java.text.AttributedCharacterIterator;
028: import java.util.Map;
029: import java.util.ArrayList;
030:
031: import org.apache.poi.ddf.EscherProperties;
032: import org.apache.poi.hslf.usermodel.RichTextRun;
033: import org.apache.poi.hslf.exceptions.HSLFException;
034:
035: /**
036: * Translates Graphics2D calls into PowerPoint.
037: *
038: * @author Yegor Kozlov
039: */
040: public class PPGraphics2D extends Graphics2D {
041: //The group to write the graphics calls into.
042: private ShapeGroup group;
043:
044: private AffineTransform transform;
045: private Stroke stroke;
046: private Paint paint;
047: private Font font;
048: private Color foreground;
049: private Color background = Color.white;
050: private Shape clip;
051: int count = 0;
052:
053: /**
054: * Construct Java Graphics object which translates graphic calls in ppt drawing layer.
055: *
056: * @param group The shape group to write the graphics calls into.
057: */
058: public PPGraphics2D(ShapeGroup group) {
059: this .group = group;
060: transform = new AffineTransform();
061: }
062:
063: /**
064: * @return the shape group being used for drawing
065: */
066: public ShapeGroup getShapeGroup() {
067: return group;
068: }
069:
070: public Font getFont() {
071: return font;
072: }
073:
074: public void setFont(Font font) {
075: this .font = font;
076: }
077:
078: public Color getColor() {
079: return foreground;
080: }
081:
082: public void setColor(Color color) {
083: this .foreground = color;
084: }
085:
086: public Stroke getStroke() {
087: return stroke;
088: }
089:
090: public void setStroke(Stroke s) {
091: this .stroke = s;
092: }
093:
094: public Paint getPaint() {
095: return paint;
096: }
097:
098: public void setPaint(Paint paint) {
099: this .paint = paint;
100: if (paint instanceof Color)
101: setColor((Color) paint);
102: }
103:
104: public AffineTransform getTransform() {
105: return (AffineTransform) transform.clone();
106: }
107:
108: public void setTransform(AffineTransform trans) {
109: transform = (AffineTransform) trans.clone();
110: }
111:
112: public void draw(Shape shape) {
113: if (clip != null) {
114: java.awt.Rectangle bounds = getTransform()
115: .createTransformedShape(shape).getBounds();
116: if (bounds.width == 0)
117: bounds.width = 1;
118: if (bounds.height == 0)
119: bounds.height = 1;
120: if (!clip.getBounds().contains(bounds)) {
121: return;
122: }
123: }
124:
125: PathIterator it = shape.getPathIterator(transform);
126: double[] prev = null;
127: double[] coords = new double[6];
128: double[] first = new double[6];
129: if (!it.isDone())
130: it.currentSegment(first); //first point
131: while (!it.isDone()) {
132: int type = it.currentSegment(coords);
133: if (prev != null) {
134: Line line = new Line(group);
135: if (stroke instanceof BasicStroke) {
136: BasicStroke bs = (BasicStroke) stroke;
137: line.setLineWidth(bs.getLineWidth());
138: float[] dash = bs.getDashArray();
139: if (dash != null)
140: line.setLineDashing(Line.PEN_DASH);
141: }
142: if (getColor() != null)
143: line.setLineColor(getColor());
144: if (type == PathIterator.SEG_LINETO) {
145: line.setAnchor(new java.awt.Rectangle(
146: (int) prev[0], (int) prev[1],
147: (int) (coords[0] - prev[0]),
148: (int) (coords[1] - prev[1])));
149: } else if (type == PathIterator.SEG_CLOSE) {
150: line.setAnchor(new java.awt.Rectangle(
151: (int) coords[0], (int) coords[1],
152: (int) (first[0] - coords[0]),
153: (int) (first[1] - coords[1])));
154: }
155: group.addShape(line);
156: }
157: prev = new double[] { coords[0], coords[1] };
158: it.next();
159: }
160:
161: }
162:
163: public void drawString(String string, float x, float y) {
164: TextBox txt = new TextBox(group);
165: txt.getTextRun().supplySlideShow(
166: group.getSheet().getSlideShow());
167: txt.getTextRun().setSheet(group.getSheet());
168: txt.setText(string);
169:
170: RichTextRun rt = txt.getTextRun().getRichTextRuns()[0];
171: rt.setFontSize(font.getSize());
172: rt.setFontName(font.getFamily());
173:
174: if (getColor() != null)
175: rt.setFontColor(getColor());
176: if (font.isBold())
177: rt.setBold(true);
178: if (font.isItalic())
179: rt.setItalic(true);
180:
181: txt.setMarginBottom(0);
182: txt.setMarginTop(0);
183: txt.setMarginLeft(0);
184: txt.setMarginRight(0);
185: txt.setWordWrap(TextBox.WrapNone);
186:
187: if (!"".equals(string))
188: txt.resizeToFitText();
189: int height = (int) txt.getAnchor().getHeight();
190:
191: /*
192: In powerpoint anchor of a shape is its top left corner.
193: Java graphics sets string coordinates by the baseline of the first character
194: so we need to shift down by the height of the textbox
195: */
196: txt.moveTo((int) x, (int) (y - height));
197:
198: if (clip != null) {
199: if (!clip.getBounds().contains(txt.getAnchor())) {
200: ;//return;
201: }
202: }
203: group.addShape(txt);
204: }
205:
206: public void fill(Shape shape) {
207: if (clip != null) {
208: java.awt.Rectangle bounds = getTransform()
209: .createTransformedShape(shape).getBounds();
210: if (bounds.width == 0)
211: bounds.width = 1;
212: if (bounds.height == 0)
213: bounds.height = 1;
214: if (!clip.getBounds().contains(bounds)) {
215: return;
216: }
217: }
218: PathIterator it = shape.getPathIterator(transform);
219: ArrayList pnt = new ArrayList();
220: double[] coords = new double[6];
221: while (!it.isDone()) {
222: int type = it.currentSegment(coords);
223: if (type != PathIterator.SEG_CLOSE) {
224: pnt.add(new Point((int) coords[0], (int) coords[1]));
225: }
226: it.next();
227: }
228: int[] xPoints = new int[pnt.size()];
229: int[] yPoints = new int[pnt.size()];
230: for (int i = 0; i < pnt.size(); i++) {
231: Point p = (Point) pnt.get(i);
232: xPoints[i] = p.x;
233: yPoints[i] = p.y;
234: }
235:
236: AutoShape r = new AutoShape(ShapeTypes.Rectangle);
237: if (paint instanceof Color) {
238: Color color = (Color) paint;
239: r.setFillColor(color);
240: }
241: if (getColor() != null)
242: r.setLineColor(getColor());
243: if (stroke instanceof BasicStroke) {
244: BasicStroke bs = (BasicStroke) stroke;
245: r.setLineWidth(bs.getLineWidth());
246: float[] dash = bs.getDashArray();
247: if (dash != null)
248: r.setLineDashing(Line.PEN_DASH);
249: }
250:
251: java.awt.Rectangle bounds = transform.createTransformedShape(
252: shape).getBounds();
253: r.setAnchor(bounds);
254: group.addShape(r);
255: }
256:
257: public void translate(int x, int y) {
258: AffineTransform at = new AffineTransform();
259: at.translate(x, y);
260: transform.concatenate(at);
261: }
262:
263: public void clip(Shape shape) {
264: this .clip = transform.createTransformedShape(shape);
265: //update size of the escher group which holds the drawing
266: group.setAnchor(clip.getBounds());
267: }
268:
269: public Shape getClip() {
270: return clip;
271: }
272:
273: public void scale(double sx, double sy) {
274: AffineTransform at = new AffineTransform();
275: at.scale(sx, sy);
276: transform.concatenate(at);
277: }
278:
279: //===============================================
280: public void drawRoundRect(int x, int y, int width, int height,
281: int arcWidth, int arcHeight) {
282: throw new HSLFException("Not implemented");
283: }
284:
285: public void drawString(String str, int x, int y) {
286: throw new HSLFException("Not implemented");
287: }
288:
289: public void fillOval(int x, int y, int width, int height) {
290: throw new HSLFException("Not implemented");
291: }
292:
293: public void fillRoundRect(int x, int y, int width, int height,
294: int arcWidth, int arcHeight) {
295: throw new HSLFException("Not implemented");
296: }
297:
298: public void fillArc(int x, int y, int width, int height,
299: int startAngle, int arcAngle) {
300: throw new HSLFException("Not implemented");
301: }
302:
303: public void setPaintMode() {
304: throw new HSLFException("Not implemented");
305: }
306:
307: public void drawArc(int x, int y, int width, int height,
308: int startAngle, int arcAngle) {
309: throw new HSLFException("Not implemented");
310: }
311:
312: public void drawPolyline(int xPoints[], int yPoints[], int nPoints) {
313: throw new HSLFException("Not implemented");
314: }
315:
316: public Graphics create() {
317: throw new HSLFException("Not implemented");
318: }
319:
320: public void drawOval(int x, int y, int width, int height) {
321: AutoShape ellipse = new AutoShape(ShapeTypes.Ellipse);
322: ellipse.setAnchor(new java.awt.Rectangle(x - width / 2, y
323: - height / 2, width, height));
324: if (stroke instanceof BasicStroke) {
325: BasicStroke bs = (BasicStroke) stroke;
326: ellipse.setLineWidth(bs.getLineWidth());
327: }
328: if (getColor() != null)
329: ellipse.setLineColor(getColor());
330: if (paint instanceof Color) {
331: Color color = (Color) paint;
332: ellipse.setFillColor(color);
333: }
334:
335: group.addShape(ellipse);
336: }
337:
338: public void setXORMode(Color color1) {
339: throw new HSLFException("Not implemented");
340: }
341:
342: public boolean drawImage(Image img, int x, int y, Color bgcolor,
343: ImageObserver observer) {
344: throw new HSLFException("Not implemented");
345: }
346:
347: public boolean drawImage(Image img, int x, int y, int width,
348: int height, Color bgcolor, ImageObserver observer) {
349: throw new HSLFException("Not implemented");
350: }
351:
352: public boolean drawImage(Image img, int dx1, int dy1, int dx2,
353: int dy2, int sx1, int sy1, int sx2, int sy2,
354: ImageObserver observer) {
355: throw new HSLFException("Not implemented");
356: }
357:
358: public boolean drawImage(Image img, int dx1, int dy1, int dx2,
359: int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor,
360: ImageObserver observer) {
361: throw new HSLFException("Not implemented");
362: }
363:
364: public boolean drawImage(Image img, int x, int y,
365: ImageObserver observer) {
366: throw new HSLFException("Not implemented");
367: }
368:
369: public boolean drawImage(Image img, int x, int y, int width,
370: int height, ImageObserver observer) {
371: throw new HSLFException("Not implemented");
372: }
373:
374: public void dispose() {
375: throw new HSLFException("Not implemented");
376: }
377:
378: public void drawLine(int x1, int y1, int x2, int y2) {
379: Line line = new Line();
380: line
381: .setAnchor(new java.awt.Rectangle(x1, y1, x2 - x1, y2
382: - y1));
383: if (stroke instanceof BasicStroke) {
384: BasicStroke bs = (BasicStroke) stroke;
385: line.setLineWidth(bs.getLineWidth());
386: }
387: if (getColor() != null)
388: line.setLineColor(getColor());
389: group.addShape(line);
390: }
391:
392: public void fillPolygon(int xPoints[], int yPoints[], int nPoints) {
393: throw new HSLFException("Not implemented");
394: }
395:
396: public FontMetrics getFontMetrics(Font f) {
397: throw new HSLFException("Not implemented");
398: }
399:
400: public void fillRect(int x, int y, int width, int height) {
401: throw new HSLFException("Not implemented");
402: }
403:
404: public void drawPolygon(int xPoints[], int yPoints[], int nPoints) {
405: throw new HSLFException("Not implemented");
406: }
407:
408: public void clipRect(int x, int y, int width, int height) {
409: throw new HSLFException("Not implemented");
410: }
411:
412: public void setClip(Shape clip) {
413: throw new HSLFException("Not implemented");
414: }
415:
416: public java.awt.Rectangle getClipBounds() {
417: throw new HSLFException("Not implemented");
418: }
419:
420: public void drawString(AttributedCharacterIterator iterator, int x,
421: int y) {
422: throw new HSLFException("Not implemented");
423: }
424:
425: public void clearRect(int x, int y, int width, int height) {
426: throw new HSLFException("Not implemented");
427: }
428:
429: public void copyArea(int x, int y, int width, int height, int dx,
430: int dy) {
431: throw new HSLFException("Not implemented");
432: }
433:
434: public void setClip(int x, int y, int width, int height) {
435: throw new HSLFException("Not implemented");
436: }
437:
438: public void rotate(double d) {
439: throw new HSLFException("Not implemented");
440:
441: }
442:
443: public void rotate(double d, double d1, double d2) {
444: throw new HSLFException("Not implemented");
445: }
446:
447: public void shear(double d, double d1) {
448: throw new HSLFException("Not implemented");
449: }
450:
451: public FontRenderContext getFontRenderContext() {
452: return new FontRenderContext(transform, true, true);
453: }
454:
455: public void transform(AffineTransform affinetransform) {
456: throw new HSLFException("Not implemented");
457: }
458:
459: public void drawImage(BufferedImage bufferedimage,
460: BufferedImageOp op, int x, int y) {
461: throw new HSLFException("Not implemented");
462: }
463:
464: public void setBackground(Color c) {
465: throw new HSLFException("Not implemented");
466: }
467:
468: public void drawRenderedImage(RenderedImage renderedimage,
469: AffineTransform affinetransform) {
470: throw new HSLFException("Not implemented");
471: }
472:
473: public Color getBackground() {
474: throw new HSLFException("Not implemented");
475: }
476:
477: public void setComposite(Composite composite) {
478: throw new HSLFException("Not implemented");
479:
480: }
481:
482: public Composite getComposite() {
483: throw new HSLFException("Not implemented");
484: }
485:
486: public Object getRenderingHint(java.awt.RenderingHints.Key key) {
487: throw new HSLFException("Not implemented");
488: }
489:
490: public boolean drawImage(Image image,
491: AffineTransform affinetransform, ImageObserver imageobserver) {
492: throw new HSLFException("Not implemented");
493: }
494:
495: public void setRenderingHint(java.awt.RenderingHints.Key key,
496: Object obj) {
497: throw new HSLFException("Not implemented");
498: }
499:
500: public void drawGlyphVector(GlyphVector g, float x, float y) {
501: throw new HSLFException("Not implemented");
502:
503: }
504:
505: public GraphicsConfiguration getDeviceConfiguration() {
506: throw new HSLFException("Not implemented");
507: }
508:
509: public void addRenderingHints(Map map) {
510: throw new HSLFException("Not implemented");
511: }
512:
513: public void translate(double d, double d1) {
514:
515: throw new HSLFException("Not implemented");
516: }
517:
518: public void drawString(
519: AttributedCharacterIterator attributedcharacteriterator,
520: float x, float y) {
521: throw new HSLFException("Not implemented");
522: }
523:
524: public boolean hit(java.awt.Rectangle rectangle, Shape shape,
525: boolean flag) {
526: throw new HSLFException("Not implemented");
527: }
528:
529: public RenderingHints getRenderingHints() {
530: throw new HSLFException("Not implemented");
531: }
532:
533: public void setRenderingHints(Map map) {
534: throw new HSLFException("Not implemented");
535:
536: }
537:
538: public void drawRenderableImage(RenderableImage renderableimage,
539: AffineTransform affinetransform) {
540: throw new HSLFException("Not implemented");
541: }
542: }
|