|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 6562639 |
| 27 | + * @summary Verify correct getPixelBounds() behavior regardless of text color. |
| 28 | + */ |
| 29 | + |
| 30 | +import java.awt.Color; |
| 31 | +import java.awt.Font; |
| 32 | +import java.awt.Rectangle; |
| 33 | +import java.awt.font.FontRenderContext; |
| 34 | +import java.awt.font.TextAttribute; |
| 35 | +import java.awt.font.TextLayout; |
| 36 | +import java.awt.geom.Rectangle2D; |
| 37 | +import java.util.HashMap; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +public class TestGetPixelBoundsWithColors { |
| 41 | + |
| 42 | + private static final Color TRANSPARENT_BLACK = new Color(0, 0, 0, 0); |
| 43 | + private static final Color TRANSPARENT_WHITE = new Color(255, 255, 255, 0); |
| 44 | + |
| 45 | + public static void main(String[] args) throws Exception { |
| 46 | + |
| 47 | + Color[] colors = new Color[] { |
| 48 | + Color.WHITE, Color.BLACK, Color.YELLOW, Color.RED, Color.GREEN, |
| 49 | + Color.GRAY, Color.LIGHT_GRAY, Color.DARK_GRAY, Color.PINK, |
| 50 | + Color.CYAN, Color.MAGENTA, Color.BLUE, null |
| 51 | + }; |
| 52 | + |
| 53 | + for (Color color : colors) { |
| 54 | + test(color); |
| 55 | + } |
| 56 | + |
| 57 | + testTransparent(TRANSPARENT_BLACK); |
| 58 | + testTransparent(TRANSPARENT_WHITE); |
| 59 | + } |
| 60 | + |
| 61 | + private static void test(Color c) { |
| 62 | + Map< TextAttribute, Object > underline = new HashMap<>(); |
| 63 | + underline.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); |
| 64 | + Font font1 = new Font(Font.DIALOG, Font.PLAIN, 60).deriveFont(underline); |
| 65 | + Map< TextAttribute, Object > foreground = new HashMap<>(); |
| 66 | + foreground.put(TextAttribute.FOREGROUND, c); |
| 67 | + Font font2 = font1.deriveFont(foreground); |
| 68 | + FontRenderContext frc = new FontRenderContext(null, true, true); |
| 69 | + TextLayout layout1 = new TextLayout("TEST", font1, frc); |
| 70 | + TextLayout layout2 = new TextLayout("TEST", font2, frc); |
| 71 | + Rectangle r1 = layout1.getPixelBounds(frc, 0, 0); |
| 72 | + Rectangle r2 = layout2.getPixelBounds(frc, 0, 0); |
| 73 | + if (!r1.equals(r2)) { |
| 74 | + throw new RuntimeException("For color " + c + ", " + r1 + " != " + r2); |
| 75 | + } |
| 76 | + Rectangle2D bounds = layout1.getBounds(); |
| 77 | + if (Math.abs(bounds.getX() - r1.x) > 3 || |
| 78 | + Math.abs(bounds.getY() - r1.y) > 3 || |
| 79 | + Math.abs(bounds.getWidth() - r1.width) > 6 || |
| 80 | + Math.abs(bounds.getHeight() - r1.height) > 6) { |
| 81 | + throw new RuntimeException("For color " + c + ", pixel bounds " + |
| 82 | + r1 + " not similar to " + bounds); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private static void testTransparent(Color c) { |
| 87 | + Font font1 = new Font(Font.DIALOG, Font.PLAIN, 60); |
| 88 | + Map< TextAttribute, Object > attributes = new HashMap<>(); |
| 89 | + attributes.put(TextAttribute.FOREGROUND, c); |
| 90 | + Font font2 = font1.deriveFont(attributes); |
| 91 | + FontRenderContext frc = new FontRenderContext(null, true, true); |
| 92 | + TextLayout layout = new TextLayout("TEST", font2, frc); |
| 93 | + Rectangle r = layout.getPixelBounds(frc, 0, 0); |
| 94 | + if (!r.isEmpty()) { |
| 95 | + throw new RuntimeException("Expected empty pixel bounds for " + c + " but got " + r); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments