Debian Codegraph @

src/test/java/org/jfree/pdf/TestGraphics2D.java

raw
1 /* =====================================================================
2 * JFreePDF : a fast, light-weight PDF library for the Java(tm) platform
3 * =====================================================================
4 *
5 * (C)opyright 2013-2022, by David Gilbert. All rights reserved.
6 *
7 * https://github.com/jfree/orsonpdf
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
23 * Other names may be trademarks of their respective owners.]
24 *
25 * If you do not wish to be bound by the terms of the GPL, an alternative
26 * runtime license is available to JFree sponsors:
27 *
28 * https://github.com/sponsors/jfree
29 *
30 */
31
32 package org.jfree.pdf;
33
34 import java.awt.BasicStroke;
35 import java.awt.Color;
36 import java.awt.Font;
37 import java.awt.FontMetrics;
38 import java.awt.GradientPaint;
39 import java.awt.Graphics2D;
40 import java.awt.Image;
41 import java.awt.Rectangle;
42 import java.awt.RenderingHints;
43 import java.awt.Shape;
44 import java.awt.geom.AffineTransform;
45 import java.awt.geom.Line2D;
46 import java.awt.geom.Point2D;
47 import java.awt.geom.Rectangle2D;
48 import java.awt.image.BufferedImage;
49 import org.junit.jupiter.api.BeforeEach;
50 import org.junit.jupiter.api.Test;
51 import org.junit.jupiter.api.Disabled;
52
53 import static org.junit.jupiter.api.Assertions.*;
54
55 /**
56 * Some tests for a Graphics2D implementation. All tests should pass with the
57 * Graphics2D instance from a BufferedImage (which we can treat as a reference
58 * implementation).
59 */
60 public class TestGraphics2D {
61
62 private Graphics2D g2;
63
64 @BeforeEach
65 public void setUp() {
66 // to test a reference implementation, use this Graphics2D from a
67 // BufferedImage in the JDK
68 // BufferedImage img = new BufferedImage(10, 20, BufferedImage.TYPE_INT_ARGB);
69 // this.g2 = img.createGraphics();
70
71 // Test PDFGraphics2D...
72 PDFDocument pdfDoc = new PDFDocument();
73 Page page = pdfDoc.createPage(new Rectangle(0, 0, 300, 200));
74 this.g2 = page.getGraphics2D();
75 }
76
77 /**
78 * Checks that the default transform is an identity transform.
79 */
80 @Test
81 public void checkDefaultTransform() {
82 assertEquals(new AffineTransform(), g2.getTransform());
83 }
84
85 /**
86 * Modifying the transform returned by the Graphics2D should not affect
87 * the state of the Graphics2D. In order for that to happen, the method
88 * should be returning a copy of the actual transform object.
89 */
90 @Test
91 public void checkGetTransformSafety() {
92 AffineTransform t = g2.getTransform();
93 t.rotate(Math.PI);
94 assertNotEquals(t, g2.getTransform());
95 assertEquals(new AffineTransform(), g2.getTransform());
96 }
97
98 /**
99 * A basic check that setTransform() does indeed update the transform.
100 */
101 @Test
102 public void setTransform() {
103 AffineTransform t = new AffineTransform(1, 2, 3, 4, 5, 6);
104 g2.setTransform(t);
105 assertEquals(t, g2.getTransform());
106
107 t.setTransform(6, 5, 4, 3, 2, 1);
108 g2.setTransform(t);
109 assertEquals(t, g2.getTransform());
110
111 // in spite of the docs saying that null is accepted this gives
112 // a NullPointerException with SunGraphics2D.
113 // g2.setTransform(null);
114 // assertEquals(new AffineTransform(), g2.getTransform());
115 }
116
117 /**
118 * When calling setTransform() the caller passes in an AffineTransform
119 * instance. If the caller retains a reference to the AffineTransform
120 * and subsequently modifies it, we don't want the Graphics2D object to
121 * be affected...so it should be making an internal copy of the
122 * AffineTransform.
123 */
124 @Test
125 public void checkSetTransformSafety() {
126 AffineTransform t = AffineTransform.getTranslateInstance(1.0, 2.0);
127 g2.setTransform(t);
128 assertEquals(t, g2.getTransform());
129 t.setToRotation(Math.PI);
130 assertNotEquals(t, g2.getTransform());
131 }
132
133 @Test
134 public void checkSetNonInvertibleTransform() {
135 AffineTransform t = AffineTransform.getScaleInstance(0.0, 0.0);
136 g2.setTransform(t);
137 assertEquals(t, g2.getTransform());
138
139 // after setting the clip, we cannot retrieve it while the transform
140 // is non-invertible...
141 Rectangle2D clip = new Rectangle2D.Double(1, 2, 3, 4);
142 g2.setClip(clip);
143 assertNull(g2.getClip());
144
145 g2.setTransform(new AffineTransform());
146 assertEquals(new Rectangle2D.Double(0, 0, 0, 0),
147 g2.getClip().getBounds2D());
148 }
149
150 /**
151 * A check for a call to transform() with a rotation, that follows a
152 * translation.
153 */
154 @Test
155 public void checkTransform() {
156 AffineTransform t = new AffineTransform();
157 this.g2.setTransform(t);
158 this.g2.translate(30, 30);
159 AffineTransform rt = AffineTransform.getRotateInstance(Math.PI / 2.0,
160 300, 200);
161 this.g2.transform(rt);
162 t = this.g2.getTransform();
163 assertEquals(0, t.getScaleX(), EPSILON);
164 assertEquals(0, t.getScaleY(), EPSILON);
165 assertEquals(-1.0, t.getShearX(), EPSILON);
166 assertEquals(1.0, t.getShearY(), EPSILON);
167 assertEquals(530.0, t.getTranslateX(), EPSILON);
168 assertEquals(-70, t.getTranslateY(), EPSILON);
169 }
170
171 @Test
172 public void checkTransformNull() {
173 try {
174 this.g2.transform(null);
175 fail("Expected a NullPointerException.");
176 } catch (NullPointerException e) {
177 // this exception is expected
178 }
179 }
180
181 /**
182 * Basic checks for the scale(x, y) method.
183 */
184 @Test
185 public void scale() {
186 g2.scale(0.5, 2.0);
187 assertEquals(AffineTransform.getScaleInstance(0.5, 2.0),
188 g2.getTransform());
189 g2.scale(2.0, -1.0);
190 assertEquals(AffineTransform.getScaleInstance(1.0, -2.0),
191 g2.getTransform());
192 }
193
194 /**
195 * Checks that a call to scale(x, y) on top of an existing translation
196 * gives the correct values.
197 */
198 @Test
199 public void translateFollowedByScale() {
200 g2.translate(2, 3);
201 assertEquals(AffineTransform.getTranslateInstance(2.0, 3.0),
202 g2.getTransform());
203 g2.scale(10, 20);
204 assertEquals(new AffineTransform(10.0, 0.0, 0.0, 20.0, 2.0, 3.0),
205 g2.getTransform());
206 }
207
208 /**
209 * Checks that a call to translate(x, y) on top of an existing scale
210 * gives the correct values.
211 */
212 @Test
213 public void scaleFollowedByTranslate() {
214 g2.scale(2, 2);
215 assertEquals(AffineTransform.getScaleInstance(2.0, 2.0),
216 g2.getTransform());
217 g2.translate(10, 20);
218 assertEquals(new AffineTransform(2.0, 0.0, 0.0, 2.0, 20.0, 40.0),
219 g2.getTransform());
220 }
221
222 private static final double EPSILON = 0.000000001;
223
224 @Test
225 public void scaleFollowedByRotate() {
226 g2.scale(2, 2);
227 assertEquals(AffineTransform.getScaleInstance(2.0, 2.0),
228 g2.getTransform());
229 g2.rotate(Math.PI / 3);
230 AffineTransform t = g2.getTransform();
231 assertEquals(1.0, t.getScaleX(), EPSILON);
232 assertEquals(1.0, t.getScaleY(), EPSILON);
233 assertEquals(-1.7320508075688772, t.getShearX(), EPSILON);
234 assertEquals(1.7320508075688772, t.getShearY(), EPSILON);
235 assertEquals(0.0, t.getTranslateX(), EPSILON);
236 assertEquals(0.0, t.getTranslateY(), EPSILON);
237 }
238
239 @Test
240 public void rotateFollowedByScale() {
241 g2.rotate(Math.PI);
242 assertEquals(AffineTransform.getRotateInstance(Math.PI),
243 g2.getTransform());
244 g2.scale(2.0, 2.0);
245 assertEquals(new AffineTransform(-2.0, 0.0, 0.0, -2.0, 0.0, 0.0),
246 g2.getTransform());
247 }
248
249 /**
250 * Checks that the getClip() method returns a different object than what
251 * was passed to setClip(), and that multiple calls to getClip() return
252 * a new object each time.
253 */
254 @Test
255 public void checkGetClipSafety() {
256 Rectangle2D r = new Rectangle2D.Double(0, 0, 1, 1);
257 this.g2.setClip(r);
258 Shape s = this.g2.getClip();
259 assertNotSame(r, s);
260 Shape s2 = this.g2.getClip();
261 assertNotSame(s, s2);
262 }
263
264 /**
265 * The default user clip should be {@code null}.
266 */
267 @Test
268 public void checkDefaultClip() {
269 assertNull(g2.getClip(), "Default user clip should be null.");
270 }
271
272 /**
273 * Checks that getClipBounds() is returning an integer approximation of
274 * the bounds.
275 */
276 @Test
277 public void checkGetClipBounds() {
278 Rectangle2D r = new Rectangle2D.Double(0.25, 0.25, 0.5, 0.5);
279 this.g2.setClip(r);
280 assertEquals(new Rectangle(0, 0, 1, 1), this.g2.getClipBounds());
281 }
282
283 /**
284 * Checks that getClipBounds() returns {@code null} when the clip is
285 * {@code null}.
286 */
287 @Test
288 public void checkGetClipBoundsWhenClipIsNull() {
289 this.g2.setClip(null);
290 assertNull(this.g2.getClipBounds());
291 }
292
293 /**
294 * Simple check that the clip() methods creates an intersection with the
295 * existing clip region.
296 */
297 @Test
298 public void checkClip() {
299 Rectangle2D r = new Rectangle2D.Double(1.0, 1.0, 3.0, 3.0);
300 this.g2.setClip(r);
301 this.g2.clip(new Rectangle2D.Double(0.0, 0.0, 2.0, 2.0));
302 assertEquals(new Rectangle2D.Double(1.0, 1.0, 1.0, 1.0),
303 this.g2.getClip().getBounds2D());
304 }
305
306 @Test
307 public void checkNonIntersectingClip() {
308 Rectangle2D r = new Rectangle2D.Double(1.0, 1.0, 3.0, 3.0);
309 this.g2.setClip(r);
310 this.g2.clip(new Rectangle2D.Double(5.0, 5.0, 1.0, 1.0));
311 assertTrue(this.g2.getClip().getBounds2D().isEmpty());
312 }
313
314 /**
315 * After applying a scale transformation, getClip() will return a
316 * modified clip.
317 */
318 @Test
319 public void checkClipAfterScaling() {
320 Rectangle2D r = new Rectangle2D.Double(1, 2, 3, 0.5);
321 this.g2.setClip(r);
322 assertEquals(r, this.g2.getClip().getBounds2D());
323 this.g2.scale(0.5, 2.0);
324 assertEquals(new Rectangle2D.Double(2, 1, 6, 0.25),
325 this.g2.getClip().getBounds2D());
326
327 // check that we get a good intersection when clipping after the
328 // scaling has been done...
329 r = new Rectangle2D.Double(3, 0, 2, 2);
330 this.g2.clip(r);
331 assertEquals(new Rectangle2D.Double(3, 1, 2, 0.25),
332 this.g2.getClip().getBounds2D());
333 }
334
335 /**
336 * Translating will change the existing clip.
337 */
338 @Test
339 public void checkClipAfterTranslate() {
340 Rectangle2D clip = new Rectangle2D.Double(0.0, 0.0, 1.0, 1.0);
341 this.g2.setClip(clip);
342 assertEquals(clip, this.g2.getClip().getBounds2D());
343 this.g2.translate(1.0, 2.0);
344 assertEquals(new Rectangle(-1, -2, 1 ,1),
345 this.g2.getClip().getBounds2D());
346 }
347
348 @Test
349 public void checkSetClipAfterTranslate() {
350 this.g2.translate(1.0, 2.0);
351 this.g2.setClip(0, 0, 1, 1);
352 assertEquals(new Rectangle(0, 0, 1, 1), this.g2.getClip().getBounds());
353 this.g2.translate(1.0, 2.0);
354 assertEquals(new Rectangle(-1, -2, 1, 1), this.g2.getClip().getBounds());
355 }
356
357 /**
358 * Transforming will change the reported clipping shape.
359 */
360 @Test
361 public void checkClipAfterTransform() {
362 Rectangle2D clip = new Rectangle2D.Double(0, 0, 1, 1);
363 this.g2.setClip(clip);
364 assertEquals(clip, this.g2.getClip().getBounds2D());
365
366 this.g2.transform(AffineTransform.getRotateInstance(Math.PI));
367 assertEquals(new Rectangle(-1, -1, 1 ,1),
368 this.g2.getClip().getBounds2D());
369
370 this.g2.setTransform(new AffineTransform());
371 assertEquals(clip, this.g2.getClip().getBounds2D());
372 }
373
374 /**
375 * Clipping with a line makes no sense, but the API allows it so we should
376 * not fail. In fact, running with a JDK Graphics2D (from a BufferedImage)
377 * it seems that the bounding rectangle of the line is used for clipping...
378 * does that make sense? Switching off the test for now.
379 */
380 @Test
381 @Disabled
382 public void checkClipWithLine2D() {
383 Rectangle2D r = new Rectangle2D.Double(1.0, 1.0, 3.0, 3.0);
384 this.g2.setClip(r);
385 this.g2.clip(new Line2D.Double(1.0, 2.0, 3.0, 4.0));
386 //assertEquals(new Rectangle2D.Double(1.0, 2.0, 2.0, 2.0),
387 // this.g2.getClip().getBounds2D());
388 //assertTrue(this.g2.getClip().getBounds2D().isEmpty());
389 }
390
391 /**
392 * Clipping with a null argument is "not recommended" according to the
393 * latest API docs (https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6206189).
394 */
395 @Test
396 public void checkClipWithNullArgument() {
397
398 // when there is a current clip set, a null pointer exception is expected
399 this.g2.setClip(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
400 Exception exception = assertThrows(NullPointerException.class, () -> this.g2.clip(null));
401
402 this.g2.setClip(null);
403 try {
404 this.g2.clip(null);
405 } catch (Exception e) {
406 fail("No exception expected.");
407 }
408 }
409
410 /**
411 * A simple check for a call to clipRect().
412 */
413 @Test
414 public void checkClipRect() {
415 Rectangle2D clip = new Rectangle2D.Double(0, 0, 5, 5);
416 this.g2.setClip(clip);
417
418 this.g2.clipRect(2, 1, 4, 2);
419 assertEquals(new Rectangle(2, 1, 3, 2),
420 g2.getClip().getBounds2D());
421 }
422
423 @Test
424 public void checkClipRectParams() {
425 Rectangle2D clip = new Rectangle2D.Double(0, 0, 5, 5);
426 this.g2.setClip(clip);
427
428 // negative width
429 this.g2.clipRect(2, 1, -4, 2);
430 assertTrue(this.g2.getClip().getBounds2D().isEmpty());
431
432 // negative height
433 this.g2.setClip(clip);
434 this.g2.clipRect(2, 1, 4, -2);
435 assertTrue(this.g2.getClip().getBounds2D().isEmpty());
436 }
437
438 @Test
439 public void checkDrawStringWithNullString() {
440 try {
441 g2.drawString((String) null, 1, 2);
442 fail("There should be a NullPointerException.");
443 } catch (NullPointerException e) {
444 // this exception is expected
445 }
446 try {
447 g2.drawString((String) null, 1.0f, 2.0f);
448 fail("There should be a NullPointerException.");
449 } catch (NullPointerException e) {
450 // this exception is expected
451 }
452 }
453
454 @Test
455 public void checkDrawStringWithEmptyString() {
456 // this should not cause any exception
457 g2.setRenderingHint(PDFHints.KEY_DRAW_STRING_TYPE, PDFHints.VALUE_DRAW_STRING_TYPE_VECTOR);
458 g2.drawString("", 1, 2);
459 g2.setRenderingHint(PDFHints.KEY_DRAW_STRING_TYPE, null);
460 g2.drawString("", 1, 2);
461 }
462
463 /**
464 * Some checks for the create() method.
465 */
466 @Test
467 public void checkCreate() {
468 this.g2.setClip(new Rectangle(1, 2, 3, 4));
469 Graphics2D copy = (Graphics2D) g2.create();
470 assertEquals(copy.getBackground(), g2.getBackground());
471 assertEquals(copy.getClip().getBounds2D(),
472 g2.getClip().getBounds2D());
473 assertEquals(copy.getColor(), g2.getColor());
474 assertEquals(copy.getComposite(), g2.getComposite());
475 assertEquals(copy.getFont(), g2.getFont());
476 assertEquals(copy.getRenderingHints(), g2.getRenderingHints());
477 assertEquals(copy.getStroke(), g2.getStroke());
478 assertEquals(copy.getTransform(), g2.getTransform());
479 }
480
481 /**
482 * The setPaint() method allows a very minor state leakage in the sense
483 * that it is possible to modify a GradientPaint externally after a call
484 * to the setPaint() method and have it impact the state of the
485 * Graphics2D implementation. Avoiding this would require cloning the
486 * Paint object, but there is no good way to do that for an arbitrary
487 * Paint instance.
488 */
489 @Test
490 public void checkSetPaintSafety() {
491 Point2D pt1 = new Point2D.Double(1.0, 2.0);
492 Point2D pt2 = new Point2D.Double(3.0, 4.0);
493 GradientPaint gp = new GradientPaint(pt1, Color.RED, pt2, Color.BLUE);
494 this.g2.setPaint(gp);
495 assertEquals(gp, this.g2.getPaint());
496 assertSame(gp, this.g2.getPaint());
497 pt1.setLocation(7.0, 7.0);
498 assertEquals(gp, this.g2.getPaint());
499 }
500
501 /**
502 * According to the Javadocs, setting the paint to null should have no
503 * impact on the current paint (that is, the call is silently ignored).
504 */
505 @Test
506 public void checkSetPaintNull() {
507 this.g2.setPaint(Color.RED);
508 // this next call should have no impact
509 this.g2.setPaint(null);
510 assertEquals(Color.RED, this.g2.getPaint());
511 }
512
513 /**
514 * Passing a Color to setPaint() also updates the color, but not the
515 * background color.
516 */
517 @Test
518 public void checkSetPaintAlsoUpdatesColorButNotBackground() {
519 Color existingBackground = this.g2.getBackground();
520 this.g2.setPaint(Color.MAGENTA);
521 assertEquals(Color.MAGENTA, this.g2.getPaint());
522 assertEquals(Color.MAGENTA, this.g2.getColor());
523 assertEquals(existingBackground, this.g2.getBackground());
524 }
525
526 @Test
527 public void checkSetColorAlsoUpdatesPaint() {
528 this.g2.setColor(Color.MAGENTA);
529 assertEquals(Color.MAGENTA, this.g2.getPaint());
530 assertEquals(Color.MAGENTA, this.g2.getColor());
531 }
532
533 /**
534 * The behaviour of the reference implementation has been observed as
535 * ignoring null. This matches the documented behaviour of the
536 * setPaint() method.
537 */
538 @Test
539 public void checkSetColorNull() {
540 this.g2.setColor(Color.RED);
541 this.g2.setColor(null);
542 assertEquals(Color.RED, this.g2.getColor());
543 }
544
545 /**
546 * Setting the background color does not change the color or paint.
547 */
548 @Test
549 public void checkSetBackground() {
550 this.g2.setBackground(Color.CYAN);
551 assertEquals(Color.CYAN, this.g2.getBackground());
552 assertNotEquals(Color.CYAN, this.g2.getColor());
553 assertNotEquals(Color.CYAN, this.g2.getPaint());
554 }
555
556 /**
557 * The behaviour of the reference implementation has been observed as
558 * allowing null (this is inconsistent with the behaviour of setColor()).
559 */
560 @Test
561 public void checkSetBackgroundNull() {
562 this.g2.setBackground(Color.RED);
563 this.g2.setBackground(null);
564 assertNull(this.g2.getBackground());
565 }
566
567 /**
568 * Since the setBackground() method is allowing null, we should ensure
569 * that the clearRect() method doesn't fail in this case. With no
570 * background color, the clearRect() method should be a no-op but there
571 * is no easy way to test for that.
572 */
573 @Test
574 public void checkClearRectWithNullBackground() {
575 this.g2.setBackground(null);
576 this.g2.clearRect(1, 2, 3, 4);
577 //no exceptions and we're good
578 }
579
580 /**
581 * In the reference implementation, setting a null composite has been
582 * observed to throw an IllegalArgumentException.
583 */
584 @Test
585 public void checkSetCompositeNull() {
586 try {
587 this.g2.setComposite(null);
588 fail("Expected an IllegalArgumentException.");
589 } catch (IllegalArgumentException e) {
590 // this exception is expected in the test
591 }
592 }
593
594 @Test
595 public void checkSetStrokeNull() {
596 try {
597 this.g2.setStroke(null);
598 fail("Expected an IllegalArgumentException.");
599 } catch (IllegalArgumentException e) {
600 // this exception is expected in the test
601 }
602 }
603
604 /**
605 * Basic check of set then get.
606 */
607 @Test
608 public void checkSetRenderingHint() {
609 this.g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
610 RenderingHints.VALUE_STROKE_PURE);
611 assertEquals(RenderingHints.VALUE_STROKE_PURE,
612 this.g2.getRenderingHint(RenderingHints.KEY_STROKE_CONTROL));
613 }
614
615 /**
616 * The reference implementation has been observed to throw a
617 * NullPointerException when the key is null.
618 */
619 @Test
620 public void checkSetRenderingHintWithNullKey() {
621 try {
622 this.g2.setRenderingHint(null, "XYZ");
623 fail("NullPointerException is expected here.");
624 } catch (NullPointerException e) {
625 // this is expected
626 }
627 }
628
629 /**
630 * The reference implementation has been observed to accept a null key
631 * and return null in that case.
632 */
633 @Test
634 public void checkGetRenderingHintWithNullKey() {
635 assertNull(this.g2.getRenderingHint(null));
636 }
637
638 /**
639 * Check setting a hint with a value that doesn't match the key.
640 */
641 @Test
642 public void checkSetRenderingHintWithInconsistentValue() {
643 try {
644 this.g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
645 RenderingHints.VALUE_ANTIALIAS_DEFAULT);
646 fail("Expected an IllegalArgumentException.");
647 } catch (IllegalArgumentException e) {
648 // we expect this exception
649 }
650 }
651
652 /**
653 * A call to getRenderingHints() is returning a copy of the hints, so
654 * changing it will not affect the state of the Graphics2D instance.
655 */
656 @Test
657 public void checkGetRenderingHintsSafety() {
658 this.g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
659 RenderingHints.VALUE_ANTIALIAS_OFF);
660 RenderingHints hints = this.g2.getRenderingHints();
661 hints.put(RenderingHints.KEY_ANTIALIASING,
662 RenderingHints.VALUE_ANTIALIAS_ON);
663 assertEquals(RenderingHints.VALUE_ANTIALIAS_OFF,
664 this.g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING));
665 }
666
667 @Test
668 public void checkSetRenderingHintsNull() {
669 try {
670 this.g2.setRenderingHints(null);
671 fail("NullPointerException expected.");
672 } catch (NullPointerException e) {
673 // this is expected
674 }
675 }
676
677 @Test
678 public void checkHit() {
679 Shape shape = new Rectangle2D.Double(0.0, 0.0, 1.0, 1.0);
680 Rectangle r = new Rectangle(2, 2, 2, 2);
681 assertFalse(this.g2.hit(r, shape, false));
682 this.g2.scale(3.0, 3.0);
683 assertTrue(this.g2.hit(r, shape, false));
684 }
685
686 @Test
687 public void checkHitForOutline() {
688 Shape shape = new Rectangle2D.Double(0.0, 0.0, 3.0, 3.0);
689 Rectangle r = new Rectangle(1, 1, 1, 1);
690 assertFalse(this.g2.hit(r, shape, true));
691 this.g2.scale(0.5, 0.5);
692 // now the rectangle is entirely inside the shape, but does not touch
693 // the outline...
694 assertTrue(this.g2.hit(r, shape, true));
695 }
696
697 /**
698 * We have observed in the reference implementation that setting the font
699 * to null does not change the current font setting.
700 */
701 @Test
702 public void checkSetFontNull() {
703 Font f = new Font("Serif", Font.PLAIN, 8);
704 this.g2.setFont(f);
705 assertEquals(f, this.g2.getFont());
706 this.g2.setFont(null);
707 assertEquals(f, this.g2.getFont());
708 }
709
710 @Test
711 public void checkDefaultStroke() {
712 BasicStroke s = (BasicStroke) this.g2.getStroke();
713 assertEquals(BasicStroke.CAP_SQUARE, s.getEndCap());
714 assertEquals(1.0f, s.getLineWidth(), EPSILON);
715 assertEquals(BasicStroke.JOIN_MITER, s.getLineJoin());
716 }
717
718 /**
719 * Check that a null GlyphVector throws a {@code NullPointerException}.
720 */
721 @Test
722 public void drawGlyphVectorNull() {
723 try {
724 g2.drawGlyphVector(null, 10, 10);
725 fail("Expecting a NullPointerException.");
726 } catch (NullPointerException e) {
727 // expected
728 }
729 }
730
731 /**
732 * Check the shear() method.
733 */
734 @Test
735 public void shear() {
736 g2.setTransform(new AffineTransform());
737 g2.shear(2.0, 3.0);
738 assertEquals(new AffineTransform(1, 3, 2, 1, 0, 0), g2.getTransform());
739 }
740
741 /**
742 * Checks a translate() followed by a shear().
743 */
744 @Test
745 public void shearFollowingTranslate() {
746 g2.setTransform(new AffineTransform());
747 g2.translate(10.0, 20.0);
748 g2.shear(2.0, 3.0);
749 assertEquals(new AffineTransform(1, 3, 2, 1, 10, 20), g2.getTransform());
750 }
751
752 @Test
753 public void drawImageWithNullBackground() {
754 Image img = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
755 g2.drawImage(img, 10, 10, null, null);
756 assertTrue(true); // won't get here if there's an exception above
757 }
758
759 /**
760 * https://github.com/jfree/jfreesvg/issues/6
761 */
762 @Test
763 public void drawImageWithNullTransform() {
764 Image img = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
765 g2.drawImage(img, null, null);
766 assertTrue(true); // won't get here if there's an exception above
767 }
768
769 @Test
770 public void drawImageWithNullImage() {
771 // API docs say method does nothing if img is null
772 // still seems to return true
773 assertTrue(g2.drawImage(null, 10, 20, null));
774 assertTrue(g2.drawImage(null, 10, 20, 30, 40, null));
775 assertTrue(g2.drawImage(null, 10, 20, Color.YELLOW, null));
776 assertTrue(g2.drawImage(null, 1, 2, 3, 4, Color.RED, null));
777 assertTrue(g2.drawImage(null, 1, 2, 3, 4, 5, 6, 7, 8, null));
778 assertTrue(g2.drawImage(null, 1, 2, 3, 4, 5, 6, 7, 8, Color.RED, null));
779 }
780
781 @Test
782 public void drawImageWithNegativeDimensions() {
783 Image img = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
784 assertTrue(g2.drawImage(img, 1, 2, -10, 10, null));
785 assertTrue(g2.drawImage(img, 1, 2, 10, -10, null));
786 }
787
788 /**
789 * A test to check whether setting a transform on the Graphics2D affects
790 * the results of text measurements performed via getFontMetrics().
791 */
792 @Test
793 public void testGetFontMetrics() {
794 Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 10);
795 FontMetrics fm = this.g2.getFontMetrics(f);
796 int w = fm.stringWidth("ABC");
797 Rectangle2D bounds = fm.getStringBounds("ABC", this.g2);
798
799 // after scaling, the string width is not changed
800 this.g2.setTransform(AffineTransform.getScaleInstance(3.0, 2.0));
801 fm = this.g2.getFontMetrics(f);
802 assertEquals(w, fm.stringWidth("ABC"));
803 assertEquals(bounds.getWidth(), fm.getStringBounds("ABC", this.g2).getWidth(), EPSILON);
804 }
805
806 @Test
807 public void drawImageWithNullImageOp() {
808 BufferedImage img = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
809 g2.drawImage(img, null, 2, 3);
810 assertTrue(true); // won't get here if there's an exception above
811 }
812
813 /**
814 * API docs say the method does nothing when called with a null image.
815 */
816 @Test
817 public void drawRenderedImageWithNullImage() {
818 g2.drawRenderedImage(null, AffineTransform.getTranslateInstance(0, 0));
819 assertTrue(true); // won't get here if there's an exception above
820 }
821 }