|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 | +import java.awt.Color; |
| 25 | +import java.awt.Dimension; |
| 26 | +import java.awt.Font; |
| 27 | +import java.awt.Graphics; |
| 28 | +import java.awt.Graphics2D; |
| 29 | +import java.awt.geom.AffineTransform; |
| 30 | +import java.awt.image.BufferedImage; |
| 31 | +import java.io.File; |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Locale; |
| 36 | +import java.util.Objects; |
| 37 | +import java.util.stream.Stream; |
| 38 | + |
| 39 | +import javax.imageio.ImageIO; |
| 40 | + |
| 41 | +import static java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment; |
| 42 | +import static java.awt.RenderingHints.KEY_TEXT_ANTIALIASING; |
| 43 | +import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB; |
| 44 | +import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_OFF; |
| 45 | +import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON; |
| 46 | +import static java.awt.image.BufferedImage.TYPE_3BYTE_BGR; |
| 47 | + |
| 48 | +/* |
| 49 | + * @test |
| 50 | + * @bug 8312555 |
| 51 | + * @summary Verifies that hieroglyphs are stretched by AffineTransform.scale(2, 1) |
| 52 | + * @run main StretchedFontTest |
| 53 | + */ |
| 54 | +public final class StretchedFontTest { |
| 55 | + private static final String TEXT = "\u6F22"; |
| 56 | + private static final int FONT_SIZE = 20; |
| 57 | + |
| 58 | + private static final Color BACKGROUND = Color.WHITE; |
| 59 | + private static final Color[] FOREGROUNDS = { |
| 60 | + new Color(0xFF000000, true), |
| 61 | + new Color(0x7F000000, true) |
| 62 | + }; |
| 63 | + |
| 64 | + private static final AffineTransform STRETCH_TRANSFORM = |
| 65 | + AffineTransform.getScaleInstance(2.0, 1.0); |
| 66 | + |
| 67 | + public static void main(String[] args) { |
| 68 | + List<String> errors = |
| 69 | + Arrays.stream(getLocalGraphicsEnvironment() |
| 70 | + .getAvailableFontFamilyNames(Locale.ENGLISH)) |
| 71 | + .map(family -> new Font(family, Font.PLAIN, FONT_SIZE)) |
| 72 | + .filter(font -> font.canDisplay(TEXT.codePointAt(0))) |
| 73 | + .map(font -> font.deriveFont(STRETCH_TRANSFORM)) |
| 74 | + .flatMap(StretchedFontTest::testFont) |
| 75 | + .filter(Objects::nonNull) |
| 76 | + .toList(); |
| 77 | + |
| 78 | + if (!errors.isEmpty()) { |
| 79 | + errors.forEach(System.err::println); |
| 80 | + throw new Error(errors.size() + " failure(s) found;" |
| 81 | + + " the first one: " + errors.get(0)); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Tests the font with a set of text antialiasing hints. |
| 87 | + * |
| 88 | + * @param font the font to test |
| 89 | + * @return a stream of test results |
| 90 | + * @see #testFont(Font, Object) |
| 91 | + */ |
| 92 | + private static Stream<String> testFont(final Font font) { |
| 93 | + return Stream.of(VALUE_TEXT_ANTIALIAS_OFF, |
| 94 | + VALUE_TEXT_ANTIALIAS_ON, |
| 95 | + VALUE_TEXT_ANTIALIAS_LCD_HRGB) |
| 96 | + .flatMap(hint -> testFont(font, hint)); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Tests the font with the specified text antialiasing hint and a set of |
| 101 | + * foreground colors. |
| 102 | + * |
| 103 | + * @param font the font to test |
| 104 | + * @param hint the text antialiasing hint to test |
| 105 | + * @return a stream of test results |
| 106 | + * @see #testFont(Font, Object, Color) |
| 107 | + */ |
| 108 | + private static Stream<String> testFont(final Font font, final Object hint) { |
| 109 | + return Stream.of(FOREGROUNDS) |
| 110 | + .map(foreground -> testFont(font, hint, foreground)); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Tests the font with the specified text antialiasing hint and |
| 115 | + * foreground color. In case of failure, it saves the rendered |
| 116 | + * image to a file. |
| 117 | + * |
| 118 | + * @param font the font to test |
| 119 | + * @param hint the text antialiasing hint to test |
| 120 | + * @param foreground the foreground color to use |
| 121 | + * @return {@code null} if the text rendered correctly; otherwise, |
| 122 | + * a {@code String} with the font family name, the value of |
| 123 | + * the rendering hint and the color in hex |
| 124 | + */ |
| 125 | + private static String testFont(final Font font, |
| 126 | + final Object hint, |
| 127 | + final Color foreground) { |
| 128 | + final Dimension size = getTextSize(font); |
| 129 | + final BufferedImage image = |
| 130 | + new BufferedImage(size.width, size.height, TYPE_3BYTE_BGR); |
| 131 | + |
| 132 | + final Graphics2D g2d = image.createGraphics(); |
| 133 | + try { |
| 134 | + g2d.setColor(BACKGROUND); |
| 135 | + g2d.fillRect(0, 0, size.width, size.height); |
| 136 | + |
| 137 | + g2d.setRenderingHint(KEY_TEXT_ANTIALIASING, hint); |
| 138 | + g2d.setColor(foreground); |
| 139 | + g2d.setFont(font); |
| 140 | + g2d.drawString(TEXT, 0, g2d.getFontMetrics(font).getAscent()); |
| 141 | + } finally { |
| 142 | + g2d.dispose(); |
| 143 | + } |
| 144 | + |
| 145 | + if (verifyImage(image)) { |
| 146 | + return null; |
| 147 | + } |
| 148 | + String fontName = font.getFontName(Locale.ENGLISH); |
| 149 | + String hintValue = getHintString(hint); |
| 150 | + String hexColor = String.format("0x%08x", foreground.getRGB()); |
| 151 | + saveImage(image, fontName + "-" + hintValue + "-" + hexColor); |
| 152 | + return "Font: " + fontName + ", Hint: " + hintValue + ", Color: " + hexColor; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Verifies the rendered image of the hieroglyph. The hieroglyph |
| 157 | + * should be stretched across the entire width of the image. |
| 158 | + * If the right half of the image contains only pixels of the background |
| 159 | + * color, the hieroglyph isn't stretched correctly |
| 160 | + * — it's a failure. |
| 161 | + * |
| 162 | + * @param image the image to verify |
| 163 | + * @return {@code true} if the hieroglyph is stretched correctly; or |
| 164 | + * {@code false} if right half of the image contains only |
| 165 | + * background-colored pixels, which means the hieroglyph isn't |
| 166 | + * stretched. |
| 167 | + */ |
| 168 | + private static boolean verifyImage(final BufferedImage image) { |
| 169 | + final int width = image.getWidth(); |
| 170 | + final int height = image.getHeight(); |
| 171 | + for (int x = width / 2; x < width; x++) { |
| 172 | + for (int y = 0; y < height; y++) { |
| 173 | + if (image.getRGB(x, y) != BACKGROUND.getRGB()) { |
| 174 | + // Any other color but background means the glyph is stretched |
| 175 | + return true; |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + // The right side of the image is filled with the background color only, |
| 181 | + // the glyph isn't stretched. |
| 182 | + return false; |
| 183 | + } |
| 184 | + |
| 185 | + private static String getHintString(final Object hint) { |
| 186 | + if (hint == VALUE_TEXT_ANTIALIAS_OFF) { |
| 187 | + return "off"; |
| 188 | + } else if (hint == VALUE_TEXT_ANTIALIAS_ON) { |
| 189 | + return "on"; |
| 190 | + } else if (hint == VALUE_TEXT_ANTIALIAS_LCD_HRGB) { |
| 191 | + return "lcd"; |
| 192 | + } else { |
| 193 | + throw new IllegalArgumentException("Unexpected hint: " + hint); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private static final BufferedImage dummyImage = |
| 198 | + new BufferedImage(5, 5, TYPE_3BYTE_BGR); |
| 199 | + |
| 200 | + private static Dimension getTextSize(final Font font) { |
| 201 | + final Graphics g = dummyImage.getGraphics(); |
| 202 | + try { |
| 203 | + return g.getFontMetrics(font) |
| 204 | + .getStringBounds(TEXT, g) |
| 205 | + .getBounds() |
| 206 | + .getSize(); |
| 207 | + } finally { |
| 208 | + g.dispose(); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + private static void saveImage(final BufferedImage image, |
| 213 | + final String fileName) { |
| 214 | + try { |
| 215 | + ImageIO.write(image, |
| 216 | + "png", |
| 217 | + new File(fileName + ".png")); |
| 218 | + } catch (IOException ignored) { |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments