| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class SVGGenerator { |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
private StringBuffer sb; |
| 60 |
|
| 61 |
private Graphics g; |
| 62 |
private int cWidth = 0; |
| 63 |
private int cHeight = 0; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static String writeSVG (Component c) { |
| 78 |
|
| 79 |
StringBuffer sb = new StringBuffer(); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 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 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
private SVGGenerator (StringBuffer sb, Component c) { |
| 115 |
|
| 116 |
this.sb = sb; |
| 117 |
|
| 118 |
cWidth = c.getWidth(); |
| 119 |
cHeight = c.getHeight(); |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 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 |
|
| 149 |
|
| 150 |
private class SVGGraphics extends Graphics { |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
private int translateX = 0; |
| 163 |
|
| 164 |
|
| 165 |
private int translateY = 0; |
| 166 |
|
| 167 |
private Color color = Color.BLACK; |
| 168 |
|
| 169 |
|
| 170 |
private Font font = new Font("Default",Font.PLAIN,12); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public SVGGraphics () { |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 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 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 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 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 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 |
|
| 221 |
|
| 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 |
|
| 244 |
|
| 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 |
|
| 261 |
|
| 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 |
|
| 282 |
|
| 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 |
|
| 299 |
|
| 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 |
|
| 320 |
|
| 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(">", ">"); |
| 333 |
string = string.replaceAll("<", "<"); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 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 |
|
| 358 |
|
| 359 |
|
| 360 |
public void setColor(Color color) { |
| 361 |
this.color = color; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public void setFont(Font font) { |
| 368 |
this.font = font; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
public void drawRect(int x, int y, int width, int height) { |
| 375 |
drawRoundRect(x, y, width, height, 0, 0); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 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 |
|
| 416 |
|
| 417 |
|
| 418 |
public void fillRect(int x, int y, int width, int height) { |
| 419 |
fillRoundRect(x, y, width, height, 0, 0); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 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 |
|
| 461 |
|
| 462 |
|
| 463 |
public String toString () { |
| 464 |
return "SVGGraphics"; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public void translate(int translateX, int translateY) { |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
this.translateX += translateX; |
| 479 |
this.translateY += translateY; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public Graphics create() { |
| 486 |
return new SVGGraphics(this); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public Font getFont() { |
| 493 |
return font; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 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 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
public void clearRect(int arg0, int arg1, int arg2, int arg3) {} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
public void clipRect(int x, int y, int width, int height) {} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
public void copyArea(int arg0, int arg1, int arg2, int arg3, int arg4,int arg5) {} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
public void dispose() {} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
public void drawArc(int arg0, int arg1, int arg2, int arg3, int arg4,int arg5) {} |
| 537 |
|
| 538 |
|
| 539 |
|
| 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 |
|
| 547 |
|
| 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 |
|
| 555 |
|
| 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 |
|
| 563 |
|
| 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 |
|
| 571 |
|
| 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 |
|
| 579 |
|
| 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 |
|
| 587 |
|
| 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 |
|
| 618 |
|
| 619 |
|
| 620 |
public void drawPolyline(int[] arg0, int[] arg1, int arg2) { |
| 621 |
System.out.println("Draw polygon 2"); |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
public void drawString(AttributedCharacterIterator arg0, int arg1, int arg2) { |
| 629 |
System.out.println("Draw complex string"); |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 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 |
|
| 641 |
|
| 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 |
|
| 673 |
|
| 674 |
|
| 675 |
public Shape getClip() { |
| 676 |
return g.getClip(); |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
public Rectangle getClipBounds() { |
| 683 |
return g.getClipBounds(); |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
public Color getColor() { |
| 690 |
return color; |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
public FontMetrics getFontMetrics(Font f) { |
| 697 |
return g.getFontMetrics(f); |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
public void setClip(Shape s) {} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
public void setClip(int x, int y, int width, int height) {} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
public void setPaintMode() {} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
public void setXORMode(Color arg0) {} |
| 719 |
} |
| 720 |
|
| 721 |
} |