File: bug8132119.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (362 lines) | stat: -rw-r--r-- 13,128 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.font.FontRenderContext;
import java.awt.font.NumericShaper;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicGraphicsUtils;
import javax.swing.plaf.metal.MetalLookAndFeel;

/**
 * @test
 * @bug 8132119 8168992 8169897 8207941
 * @author Alexandr Scherbatiy
 * @summary Provide public API for text related methods in SwingBasicGraphicsUtils2
 */
public class bug8132119 {

    private static final int WIDTH = 50;
    private static final int HEIGHT = 50;
    private static final Color DRAW_COLOR = Color.RED;
    private static final Color BACKGROUND_COLOR = Color.GREEN;
    private static final NumericShaper NUMERIC_SHAPER = NumericShaper.getShaper(
            NumericShaper.ARABIC);

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeAndWait(bug8132119::testStringMethods);
    }

    private static void testStringMethods() {
        setMetalLAF();
        testStringWidth();
        testStringClip();
        testDrawEmptyString();
        testDrawString(false);
        testDrawString(true);
        checkNullArguments();
    }

    private static void testStringWidth() {

        String str = "12345678910\u036F";
        JComponent comp = createComponent(str);
        Font font = comp.getFont();
        FontMetrics fontMetrics = comp.getFontMetrics(font);
        float stringWidth = BasicGraphicsUtils.getStringWidth(comp, fontMetrics, str);

        if (stringWidth == fontMetrics.stringWidth(str)) {
            throw new RuntimeException("Numeric shaper is not used!");
        }

        if (stringWidth != getLayoutWidth(str, font, NUMERIC_SHAPER)) {
            throw new RuntimeException("Wrong text width!");
        }
    }

    private static void testStringClip() {

        String str = "1234567890";
        JComponent comp = createComponent(str);
        FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont());

        int width = (int) BasicGraphicsUtils.getStringWidth(comp, fontMetrics, str);

        String clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, width);
        checkClippedString(str, clip, str);

        clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, width + 1);
        checkClippedString(str, clip, str);

        clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, -1);
        checkClippedString(str, clip, "...");

        clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, 0);
        checkClippedString(str, clip, "...");

        clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics,
                str, width - width / str.length());
        int endIndex = str.length() - 3;
        checkClippedString(str, clip, str.substring(0, endIndex) + "...");
    }

    private static void checkClippedString(String str, String res, String golden) {
        if (!golden.equals(res)) {
            throw new RuntimeException(String.format("The string '%s' is not "
                    + "properly clipped. The result is '%s' instead of '%s'",
                    str, res, golden));
        }
    }

    private static void testDrawEmptyString() {
        JLabel label = new JLabel();
        BufferedImage buffImage = createBufferedImage(50, 50);
        Graphics2D g2 = buffImage.createGraphics();
        g2.setColor(DRAW_COLOR);
        BasicGraphicsUtils.drawString(null, g2, null, 0, 0);
        BasicGraphicsUtils.drawString(label, g2, null, 0, 0);
        BasicGraphicsUtils.drawString(null, g2, "", 0, 0);
        BasicGraphicsUtils.drawString(label, g2, "", 0, 0);
        BasicGraphicsUtils.drawStringUnderlineCharAt(null, g2, null, 3, 0, 0);
        BasicGraphicsUtils.drawStringUnderlineCharAt(label, g2, null, 3, 0, 0);
        BasicGraphicsUtils.drawStringUnderlineCharAt(null, g2, "", 3, 0, 0);
        BasicGraphicsUtils.drawStringUnderlineCharAt(label, g2, "", 3, 0, 0);
        g2.dispose();
        checkImageIsEmpty(buffImage);
    }

    private static void testDrawString(boolean underlined) {
        String str = "AOB";
        JComponent comp = createComponent(str);

        BufferedImage buffImage = createBufferedImage(WIDTH, HEIGHT);
        Graphics2D g2 = buffImage.createGraphics();

        g2.setColor(DRAW_COLOR);
        g2.setFont(comp.getFont());

        FontMetrics fontMetrices = comp.getFontMetrics(comp.getFont());
        float width = BasicGraphicsUtils.getStringWidth(comp, fontMetrices, str);
        float x = (WIDTH - width) / 2;
        int y = 3 * HEIGHT / 4;

        if (underlined) {
            BasicGraphicsUtils.drawStringUnderlineCharAt(comp, g2, str, 1, x, y);
        } else {
            BasicGraphicsUtils.drawString(comp, g2, str, x, y);
        }
        g2.dispose();

        float xx = BasicGraphicsUtils.getStringWidth(comp, fontMetrices, "A") +
                BasicGraphicsUtils.getStringWidth(comp, fontMetrices, "O")/2;

        checkImageContainsSymbol(buffImage, (int) xx, underlined ? 3 : 2);
    }

    private static void checkNullArguments() {

        Graphics2D g = null;
        try {
            String text = "Test";
            JComponent component = new JLabel(text);
            BufferedImage img = createBufferedImage(100, 100);
            g = img.createGraphics();
            checkNullArguments(component, g, text);
        } finally {
            g.dispose();
        }
    }

    private static void checkNullArguments(JComponent comp, Graphics2D g,
            String text) {

        checkNullArgumentsDrawString(comp, g, text);
        checkNullArgumentsDrawStringUnderlineCharAt(comp, g, text);
        checkNullArgumentsGetClippedString(comp, text);
        checkNullArgumentsGetStringWidth(comp, text);
    }

    private static void checkNullArgumentsDrawString(JComponent comp, Graphics2D g,
            String text) {

        float x = 50;
        float y = 50;
        BasicGraphicsUtils.drawString(null, g, text, x, y);
        BasicGraphicsUtils.drawString(comp, g, null, x, y);

        try {
            BasicGraphicsUtils.drawString(comp, null, text, x, y);
        } catch (NullPointerException e) {
            return;
        }

        throw new RuntimeException("NPE is not thrown");
    }

    private static void checkNullArgumentsDrawStringUnderlineCharAt(
            JComponent comp, Graphics2D g, String text) {

        int x = 50;
        int y = 50;
        BasicGraphicsUtils.drawStringUnderlineCharAt(null, g, text, 1, x, y);
        BasicGraphicsUtils.drawStringUnderlineCharAt(comp, g, null, 1, x, y);

        try {
            BasicGraphicsUtils.drawStringUnderlineCharAt(comp, null, text, 1, x, y);
        } catch (NullPointerException e) {
            return;
        }

        throw new RuntimeException("NPE is not thrown");
    }

    private static void checkNullArgumentsGetClippedString(
            JComponent comp, String text) {

        FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont());

        BasicGraphicsUtils.getClippedString(null, fontMetrics, text, 1);
        String result = BasicGraphicsUtils.getClippedString(comp, fontMetrics, null, 1);
        if (!"".equals(result)) {
            throw new RuntimeException("Empty string is not returned!");
        }

        try {
            BasicGraphicsUtils.getClippedString(comp, null, text, 1);
        } catch (NullPointerException e) {
            return;
        }

        throw new RuntimeException("NPE is not thrown");
    }

    private static void checkNullArgumentsGetStringWidth(JComponent comp,
            String text) {

        FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont());
        BasicGraphicsUtils.getStringWidth(null, fontMetrics, text);
        float result = BasicGraphicsUtils.getStringWidth(comp, fontMetrics, null);

        if (result != 0) {
            throw new RuntimeException("The string length is not 0");
        }

        try {
            BasicGraphicsUtils.getStringWidth(comp, null, text);
        } catch (NullPointerException e) {
            return;
        }

        throw new RuntimeException("NPE is not thrown");
    }

    private static void setMetalLAF() {
        try {
            UIManager.setLookAndFeel(new MetalLookAndFeel());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static JComponent createComponent(String str) {
        JComponent comp = new JLabel(str);
        comp.setSize(WIDTH, HEIGHT);
        comp.putClientProperty(TextAttribute.NUMERIC_SHAPING, NUMERIC_SHAPER);
        comp.setFont(getFont());
        return comp;
    }

    private static String getFontName(String fn, String[] fontNames) {
        String fontName = null;
        for (String name : fontNames) {
            if (fn.equals(name)) {
                fontName = name;
                break;
            }
        }
        return fontName;
    }

    private static Font getFont() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fontNames = ge.getAvailableFontFamilyNames();

        // We do not have Arial on all systems so provide some reasonable fallbacks.
        // In case the fallbacks are not available as well, choose as last fallback
        // the first font - however this might be a problematic choice.
        String fontName = getFontName("Arial", fontNames);
        if (fontName == null) {
            fontName = getFontName("Bitstream Charter", fontNames);
            if (fontName == null) {
                fontName = getFontName("Dialog", fontNames);
                if (fontName == null) {
                    fontName = fontNames[0];
                    System.out.println("warning - preferred fonts not on the system, fall back to first font " + fontName);
                }
            }
        }
        return new Font(fontName, Font.PLAIN, 30);
    }

    private static float getLayoutWidth(String text, Font font, NumericShaper shaper) {
        HashMap map = new HashMap();
        map.put(TextAttribute.FONT, font);
        map.put(TextAttribute.NUMERIC_SHAPING, shaper);
        FontRenderContext frc = new FontRenderContext(null, false, false);
        TextLayout layout = new TextLayout(text, map, frc);
        return layout.getAdvance();
    }

    private static void checkImageIsEmpty(BufferedImage buffImage) {
        int background = BACKGROUND_COLOR.getRGB();

        for (int i = 0; i < buffImage.getWidth(); i++) {
            for (int j = 0; j < buffImage.getHeight(); j++) {
                if (background != buffImage.getRGB(i, j)) {
                    throw new RuntimeException("Image is not empty!");
                }
            }
        }
    }

    private static void checkImageContainsSymbol(BufferedImage buffImage,
            int x, int intersections) {

        int background = BACKGROUND_COLOR.getRGB();
        boolean isBackground = true;
        int backgroundChangesCount = 0;

        for (int y = 0; y < buffImage.getHeight(); y++) {
            if (!(isBackground ^ (background != buffImage.getRGB(x, y)))) {
                isBackground = !isBackground;
                backgroundChangesCount++;
            }
        }

        if (backgroundChangesCount != intersections * 2) {
            throw new RuntimeException("String is not properly drawn!");
        }
    }

    private static BufferedImage createBufferedImage(int width, int height) {
        BufferedImage bufffImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);

        Graphics2D g = bufffImage.createGraphics();
        g.setColor(BACKGROUND_COLOR);
        g.fillRect(0, 0, width, height);
        g.dispose();
        return bufffImage;
    }
}