Debian Codegraph @

uk/ac/babraham/FastQC/Utilities/ImageSaver/SVGGenerator.java

raw symbols SCIP
1 /**
2 * Copyright 2009- 21 Simon Andrews
3 *
4 * This file is part of SeqMonk.
5 *
6 * SeqMonk is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * SeqMonk is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with SeqMonk; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 package uk.ac.babraham.FastQC.Utilities.ImageSaver;
21
22 import java.awt.Color;
23 import java.awt.Component;
24 import java.awt.Font;
25 import java.awt.FontMetrics;
26 import java.awt.Graphics;
27 import java.awt.Image;
28 import java.awt.Rectangle;
29 import java.awt.Shape;
30 import java.awt.image.BufferedImage;
31 import java.awt.image.ImageObserver;
32 import java.text.AttributedCharacterIterator;
33
34 import javax.swing.RepaintManager;
35
36 /**
37 * SVGGenerator is an implementation of the Graphics interface which can be
38 * passed to any paint method and will generate an SVG version of the display.
39 *
40 * This is not a complete implementation of the graphics interface - just the
41 * primitive drawing parts. It doesn't handle drawing bitmaps and it doesn't
42 * implement any of the Graphics2D extensions so you should test it against
43 * any class you want to convert into SVG.
44 *
45 * Generally this class shouldn't be called directly. You should use the
46 * ImageSaver class which allows the selection of the file to save and the
47 * type of image to generate. This class will be called by ImageSaver.
48 */
49 public class SVGGenerator {
50
51 /**
52 * This class is used to generate an SVG version of displays
53 * created within the program. Although it extends Graphics
54 * so we can pass it to a paint routine it's not trying to
55 * reimplement the whole of the Graphics interface, just the
56 * bits we actually use.
57 */
58
59 private StringBuffer sb;
60
61 private Graphics g;
62 private int cWidth = 0;
63 private int cHeight = 0;
64
65 /*
66 * These are the methods we know we actually use so we need
67 * to implement them in our generator
68 */
69
70 /**
71 * This is the method used to create an SVG representation
72 * of a component. The returned string contains valid SVG
73 *
74 * @param c The component to convert
75 * @return An SVG representation of the component
76 */
77 public static String writeSVG (Component c) {
78
79 StringBuffer sb = new StringBuffer();
80
81 /*
82 * Before using our Graphics class we need to disable double
83 * buffering on the component. If we don't do this then we
84 * just get an image from the offscreen buffer to draw into
85 * our Graphics object - we never see the individual method
86 * calls to the Graphics interface.
87 *
88 * This only affects Windows/Linux where java does the buffering
89 * internally. OSX does it via the window manager so this works
90 * without changing anything.
91 */
92
93 boolean doubleBuffered = RepaintManager.currentManager(c).isDoubleBufferingEnabled();
94 if (doubleBuffered) {
95 RepaintManager.currentManager(c).setDoubleBufferingEnabled(false);
96 }
97
98 new SVGGenerator(sb,c);
99
100 if (doubleBuffered) {
101 RepaintManager.currentManager(c).setDoubleBufferingEnabled(true);
102 }
103
104 return(sb.toString());
105
106 }
107
108 /**
109 * Instantiates a new generator. Not used externally - all external calls
110 * to this class should go via the static convert to SVG method.
111 *
112 * @param c The component to convert
113 */
114 private SVGGenerator (StringBuffer sb, Component c) {
115
116 this.sb = sb;
117
118 cWidth = c.getWidth();
119 cHeight = c.getHeight();
120
121 // Some methods would be a pain to reimplement ourselves and
122 // aren't used directly by us (eg providing font metrics). We
123 // therefore create a graphics object from a BufferedImage and
124 // use that to do any difficult stuff we can't be bothered with.
125
126 BufferedImage b = new BufferedImage(c.getWidth(),c.getHeight(),BufferedImage.TYPE_INT_RGB);
127 g = b.getGraphics();
128
129
130 sb.append("<?xml version=\"1.0\" standalone=\"no\"?>\n");
131 sb.append("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
132 sb.append("<svg width=\"");
133 sb.append(c.getWidth());
134 sb.append("\" height=\"");
135 sb.append(c.getHeight());
136 sb.append("\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n");
137
138 c.paint(new SVGGraphics());
139
140 sb.append("</svg>\n");
141
142 }
143
144
145
146
147 /**
148 * The Actual SVG implementation of Graphics
149 */
150 private class SVGGraphics extends Graphics {
151
152 /**
153 * SVGGraphics is an implementation of the Graphics
154 * abstract class. When used it is required to copy
155 * itself and inherit translation and clipping properties
156 * from its parent. We don't worry about clipping since
157 * we're only going to get drawn once. We also store font
158 * and color although I'm not sure if that's necessary.
159 */
160
161 /** The current offset on the x-axis **/
162 private int translateX = 0;
163
164 /** The current offset on the y-axis **/
165 private int translateY = 0;
166
167 private Color color = Color.BLACK;
168
169 /** The font. */
170 private Font font = new Font("Default",Font.PLAIN,12);
171
172
173 /**
174 * Instantiates a new SVG graphics object.
175 */
176 public SVGGraphics () {
177 }
178
179 /**
180 * SVG Graphics has to recursively call itself. This shouldn't be
181 * called externally.
182 *
183 * @param parent The calling SVGGraphics instance
184 */
185 public SVGGraphics(SVGGraphics parent) {
186 this.translateX = parent.translateX;
187 this.translateY = parent.translateY;
188 this.color = parent.color;
189 this.font = parent.font;
190 }
191
192 /**
193 * Corrects an x value with the current translation value
194 *
195 * @param x The raw x value
196 * @return The corrected x value
197 */
198 private int correctX (int x){
199 int newX = x + translateX;
200 if (newX<0) newX=0;
201 if (newX>cWidth) newX=cWidth;
202
203 return newX;
204 }
205
206 /**
207 * Corrects a y value with the current translation value
208 *
209 * @param y The raw y value
210 * @return The corrected y value
211 */
212 private int correctY (int y) {
213 int newY = y + translateY;
214 if (newY<0) newY=0;
215 if (newY>cHeight) newY=cHeight;
216
217 return newY;
218 }
219
220 /* (non-Javadoc)
221 * @see java.awt.Graphics#drawLine(int, int, int, int)
222 */
223 @Override
224 public void drawLine(int x1, int y1, int x2, int y2) {
225 x1 = correctX(x1);
226 y1 = correctY(y1);
227 x2 = correctX(x2);
228 y2 = correctY(y2);
229
230 sb.append("<line x1=\"");
231 sb.append(x1);
232 sb.append("\" y1=\"");
233 sb.append(y1);
234 sb.append("\" x2=\"");
235 sb.append(x2);
236 sb.append("\" y2=\"");
237 sb.append(y2);
238 sb.append("\" stroke=\"rgb(");
239 appendColor();
240 sb.append(")\" stroke-width=\"1\"/>\n");
241 }
242
243 /* (non-Javadoc)
244 * @see java.awt.Graphics#drawOval(int, int, int, int)
245 */
246 @Override
247 public void drawOval(int x, int y, int width, int height) {
248
249 x = correctX(x);
250 y = correctY(y);
251
252 if (x+width > cWidth) {
253 width = cWidth-x;
254 }
255
256 if (y+height > cHeight) {
257 height = cHeight-y;
258 }
259
260 // SVG does ovals from the middle of the circle rather
261 // than the edge so we need to do some conversion.
262
263 x+= width/2;
264 y+=height/2;
265 width/=2;
266 height/=2;
267
268 sb.append("<ellipse cx=\"");
269 sb.append(x);
270 sb.append("\" cy=\"");
271 sb.append(y);
272 sb.append("\" rx=\"");
273 sb.append(width);
274 sb.append("\" ry=\"");
275 sb.append(height);
276 sb.append("\" style=\"fill:none;stroke:rgb(");
277 appendColor();
278 sb.append(");stroke-width:1\"/>\n");
279 }
280
281 /* (non-Javadoc)
282 * @see java.awt.Graphics#fillOval(int, int, int, int)
283 */
284 @Override
285 public void fillOval(int x, int y, int width, int height) {
286
287 x = correctX(x);
288 y = correctY(y);
289
290 if (x+width > cWidth) {
291 width = cWidth-x;
292 }
293
294 if (y+height > cHeight) {
295 height = cHeight-y;
296 }
297
298 // SVG does ovals from the middle of the circle rather
299 // than the edge so we need to do some conversion.
300
301 x+= width/2;
302 y+=height/2;
303 width/=2;
304 height/=2;
305
306 sb.append("<ellipse cx=\"");
307 sb.append(x);
308 sb.append("\" cy=\"");
309 sb.append(y);
310 sb.append("\" rx=\"");
311 sb.append(width);
312 sb.append("\" ry=\"");
313 sb.append(height);
314 sb.append("\" style=\"fill:rgb(");
315 appendColor();
316 sb.append(");stroke:none\"/>\n");
317 }
318
319 /* (non-Javadoc)
320 * @see java.awt.Graphics#drawString(java.lang.String, int, int)
321 */
322 @Override
323 public void drawString(String string, int x, int y) {
324
325 x = correctX(x);
326 y = correctY(y);
327
328 if (string.indexOf("&")>=0) {
329 string = "<![CDATA[" + string.replaceAll("]]>", "]]>]]><![CDATA[")+ "]]>";
330 }
331 else {
332 string = string.replaceAll(">", "&gt;");
333 string = string.replaceAll("<", "&lt;");
334 }
335
336 // This doesn't yet take the font family into account
337 // as there isn't always an easy way to map between
338 // java names and SVG names. Will look at this again
339 // later.
340
341 sb.append("<text x=\"");
342 sb.append(x);
343 sb.append("\" y=\"");
344 sb.append(y);
345 sb.append("\" fill=\"rgb(");
346 appendColor();
347 sb.append(")\" font-family=\"Arial\" font-size=\"");
348 sb.append(font.getSize());
349 if (font.isBold()) {
350 sb.append("\" font-weight=\"bold");
351 }
352 sb.append("\">");
353 sb.append(string);
354 sb.append("</text>\n");
355 }
356
357 /* (non-Javadoc)
358 * @see java.awt.Graphics#setColor(java.awt.Color)
359 */
360 public void setColor(Color color) {
361 this.color = color;
362 }
363
364 /* (non-Javadoc)
365 * @see java.awt.Graphics#setFont(java.awt.Font)
366 */
367 public void setFont(Font font) {
368 this.font = font;
369 }
370
371 /* (non-Javadoc)
372 * @see java.awt.Graphics#drawRect(int, int, int, int)
373 */
374 public void drawRect(int x, int y, int width, int height) {
375 drawRoundRect(x, y, width, height, 0, 0);
376 }
377
378 /* (non-Javadoc)
379 * @see java.awt.Graphics#drawRoundRect(int, int, int, int, int, int)
380 */
381 public void drawRoundRect(int x, int y, int width, int height, int arcWidth,int arcHeight) {
382
383 x = correctX(x);
384 y = correctY(y);
385
386 if (x+width > cWidth) {
387 width = cWidth-x;
388 }
389
390 if (y+height > cHeight) {
391 height = cHeight-y;
392 }
393
394 sb.append("<rect width=\"");
395 sb.append(width);
396 sb.append("\" height=\"");
397 sb.append(height);
398 sb.append("\"");
399 sb.append(" x=\"");
400 sb.append(x);
401 sb.append("\" y=\"");
402 sb.append(y);
403 sb.append("\" rx=\"");
404 sb.append(arcWidth);
405 sb.append("\" ry=\"");
406 sb.append(arcHeight);
407 sb.append("\" style=\"fill:none");
408 sb.append(";stroke-width:1;stroke:rgb(");
409
410 appendColor();
411
412 sb.append(")\"/>\n");
413 }
414
415 /* (non-Javadoc)
416 * @see java.awt.Graphics#fillRect(int, int, int, int)
417 */
418 public void fillRect(int x, int y, int width, int height) {
419 fillRoundRect(x, y, width, height, 0, 0);
420 }
421
422 /* (non-Javadoc)
423 * @see java.awt.Graphics#fillRoundRect(int, int, int, int, int, int)
424 */
425 public void fillRoundRect(int x, int y, int width, int height, int arcWidth,int arcHeight) {
426
427 x = correctX(x);
428 y = correctY(y);
429
430 if (x+width > cWidth) {
431 width = cWidth-x;
432 }
433
434 if (y+height > cHeight) {
435 height = cHeight-y;
436 }
437
438 sb.append("<rect width=\"");
439 sb.append(width);
440 sb.append("\" height=\"");
441 sb.append(height);
442 sb.append("\"");
443 sb.append(" x=\"");
444 sb.append(x);
445 sb.append("\" y=\"");
446 sb.append(y);
447 if (arcWidth > 0 || arcHeight > 0) {
448 sb.append("\" rx=\"");
449 sb.append(arcWidth);
450 sb.append("\" ry=\"");
451 sb.append(arcHeight);
452 }
453 sb.append("\" style=\"fill:rgb(");
454
455 appendColor();
456
457 sb.append(");stroke:none\"/>\n");
458 }
459
460 /* (non-Javadoc)
461 * @see java.awt.Graphics#toString()
462 */
463 public String toString () {
464 return "SVGGraphics";
465 }
466
467 /* (non-Javadoc)
468 * @see java.awt.Graphics#translate(int, int)
469 */
470 public void translate(int translateX, int translateY) {
471 /**
472 * It's important to note that new translations are
473 * layered on top of previous translations and do
474 * not replace them. We got this wrong previously
475 * and it made a right mess!
476 */
477
478 this.translateX += translateX;
479 this.translateY += translateY;
480 }
481
482 /* (non-Javadoc)
483 * @see java.awt.Graphics#create()
484 */
485 public Graphics create() {
486 return new SVGGraphics(this);
487 }
488
489 /* (non-Javadoc)
490 * @see java.awt.Graphics#getFont()
491 */
492 public Font getFont() {
493 return font;
494 }
495
496 /**
497 * Append color.
498 */
499 private void appendColor () {
500 sb.append(color.getRed());
501 sb.append(",");
502 sb.append(color.getGreen());
503 sb.append(",");
504 sb.append(color.getBlue());
505 }
506
507 /*
508 * The following methods are required by the graphics interface
509 * but we don't actually use them so we're just leaving them as
510 * stubs.
511 */
512
513 /* (non-Javadoc)
514 * @see java.awt.Graphics#clearRect(int, int, int, int)
515 */
516 public void clearRect(int arg0, int arg1, int arg2, int arg3) {}
517
518 /* (non-Javadoc)
519 * @see java.awt.Graphics#clipRect(int, int, int, int)
520 */
521 public void clipRect(int x, int y, int width, int height) {}
522
523 /* (non-Javadoc)
524 * @see java.awt.Graphics#copyArea(int, int, int, int, int, int)
525 */
526 public void copyArea(int arg0, int arg1, int arg2, int arg3, int arg4,int arg5) {}
527
528 /* (non-Javadoc)
529 * @see java.awt.Graphics#dispose()
530 */
531 public void dispose() {}
532
533 /* (non-Javadoc)
534 * @see java.awt.Graphics#drawArc(int, int, int, int, int, int)
535 */
536 public void drawArc(int arg0, int arg1, int arg2, int arg3, int arg4,int arg5) {}
537
538 /* (non-Javadoc)
539 * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
540 */
541 public boolean drawImage(Image img, int x, int y, ImageObserver obs) {
542 System.out.println("Height="+img.getHeight(obs)+" Width="+img.getWidth(obs));
543 return g.drawImage(img, x, y, obs);
544 }
545
546 /* (non-Javadoc)
547 * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, java.awt.Color, java.awt.image.ImageObserver)
548 */
549 public boolean drawImage(Image arg0, int arg1, int arg2, Color arg3,ImageObserver arg4) {
550 System.out.println("Draw image 2");
551 return true;
552 }
553
554 /* (non-Javadoc)
555 * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int, java.awt.image.ImageObserver)
556 */
557 public boolean drawImage(Image arg0, int arg1, int arg2, int arg3,int arg4, ImageObserver arg5) {
558 System.out.println("Draw image 3");
559 return true;
560 }
561
562 /* (non-Javadoc)
563 * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int, java.awt.Color, java.awt.image.ImageObserver)
564 */
565 public boolean drawImage(Image arg0, int arg1, int arg2, int arg3,int arg4, Color arg5, ImageObserver arg6) {
566 System.out.println("Draw image 4");
567 return true;
568 }
569
570 /* (non-Javadoc)
571 * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int, int, int, int, int, java.awt.image.ImageObserver)
572 */
573 public boolean drawImage(Image arg0, int arg1, int arg2, int arg3,int arg4, int arg5, int arg6, int arg7, int arg8, ImageObserver arg9) {
574 System.out.println("Draw image 5");
575 return true;
576 }
577
578 /* (non-Javadoc)
579 * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int, int, int, int, int, java.awt.Color, java.awt.image.ImageObserver)
580 */
581 public boolean drawImage(Image arg0, int arg1, int arg2, int arg3,int arg4, int arg5, int arg6, int arg7, int arg8, Color arg9,ImageObserver arg10) {
582 System.out.println("Draw image 6");
583 return true;
584 }
585
586 /* (non-Javadoc)
587 * @see java.awt.Graphics#drawPolygon(int[], int[], int)
588 */
589 public void drawPolygon(int[] xPoints, int[] yPoints, int pointsToUse) {
590
591 int [] correctedXPoints = new int[pointsToUse];
592 int [] correctedYPoints = new int[pointsToUse];
593
594 for (int i=0;i<pointsToUse;i++) {
595 correctedXPoints[i] = correctX(xPoints[i]);
596 correctedYPoints[i] = correctY(yPoints[i]);
597 }
598
599 sb.append("<polygon points=\"");
600
601 for (int i=0;i<pointsToUse;i++) {
602 if (i != 0) {
603 sb.append(",");
604 }
605 sb.append(correctedXPoints[i]);
606 sb.append(",");
607 sb.append(correctedYPoints[i]);
608 }
609
610 sb.append("\" style=\"stroke-width:1;stroke:rgb(");
611
612 appendColor();
613
614 sb.append(");fill:none\"/>\n");
615 }
616
617 /* (non-Javadoc)
618 * @see java.awt.Graphics#drawPolyline(int[], int[], int)
619 */
620 public void drawPolyline(int[] arg0, int[] arg1, int arg2) {
621 System.out.println("Draw polygon 2");
622 }
623
624
625 /* (non-Javadoc)
626 * @see java.awt.Graphics#drawString(java.text.AttributedCharacterIterator, int, int)
627 */
628 public void drawString(AttributedCharacterIterator arg0, int arg1, int arg2) {
629 System.out.println("Draw complex string");
630 }
631
632 /* (non-Javadoc)
633 * @see java.awt.Graphics#fillArc(int, int, int, int, int, int)
634 */
635 public void fillArc(int arg0, int arg1, int arg2, int arg3, int arg4,int arg5) {
636 System.out.println("Fill arc");
637 }
638
639
640 /* (non-Javadoc)
641 * @see java.awt.Graphics#fillPolygon(int[], int[], int)
642 */
643 public void fillPolygon(int[] xPoints, int[] yPoints, int pointsToUse) {
644
645 int [] correctedXPoints = new int[pointsToUse];
646 int [] correctedYPoints = new int[pointsToUse];
647
648 for (int i=0;i<pointsToUse;i++) {
649 correctedXPoints[i] = correctX(xPoints[i]);
650 correctedYPoints[i] = correctY(yPoints[i]);
651 }
652
653 sb.append("<polygon points=\"");
654
655 for (int i=0;i<pointsToUse;i++) {
656 if (i != 0) {
657 sb.append(",");
658 }
659 sb.append(correctedXPoints[i]);
660 sb.append(",");
661 sb.append(correctedYPoints[i]);
662 }
663
664 sb.append("\" style=\"fill:rgb(");
665
666 appendColor();
667
668 sb.append(");stroke:none\"/>\n");
669
670 }
671
672 /* (non-Javadoc)
673 * @see java.awt.Graphics#getClip()
674 */
675 public Shape getClip() {
676 return g.getClip();
677 }
678
679 /* (non-Javadoc)
680 * @see java.awt.Graphics#getClipBounds()
681 */
682 public Rectangle getClipBounds() {
683 return g.getClipBounds();
684 }
685
686 /* (non-Javadoc)
687 * @see java.awt.Graphics#getColor()
688 */
689 public Color getColor() {
690 return color;
691 }
692
693 /* (non-Javadoc)
694 * @see java.awt.Graphics#getFontMetrics(java.awt.Font)
695 */
696 public FontMetrics getFontMetrics(Font f) {
697 return g.getFontMetrics(f);
698 }
699
700 /* (non-Javadoc)
701 * @see java.awt.Graphics#setClip(java.awt.Shape)
702 */
703 public void setClip(Shape s) {}
704
705 /* (non-Javadoc)
706 * @see java.awt.Graphics#setClip(int, int, int, int)
707 */
708 public void setClip(int x, int y, int width, int height) {}
709
710 /* (non-Javadoc)
711 * @see java.awt.Graphics#setPaintMode()
712 */
713 public void setPaintMode() {}
714
715 /* (non-Javadoc)
716 * @see java.awt.Graphics#setXORMode(java.awt.Color)
717 */
718 public void setXORMode(Color arg0) {}
719 }
720
721 }