aboutsummaryrefslogtreecommitdiff
path: root/test/jbu/quality/text/DroidFontTest.java
blob: c080b0cfee47a56cc25aad5b442d04afd943c301 (plain)
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
package quality.text;

import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;

import static org.junit.Assert.*;

public class DroidFontTest {
    /* Tests for the following font names and styles:

            "droid sans" : PLAIN, BOLD
            "droid sans bold" : alias for "droid sans"

            "droid sans mono" : PLAIN
            "droid sans mono slashed" : PLAIN
            "droid sans mono dotted" : PLAIN

            "droid serif" : PLAIN, BOLD, ITALIC, BOLD | ITALIC

            "droid serif bold" : alias for "droid serif" BOLD, BOLD | ITALIC
    */

    private static final String abc =
            "the quick brown fox jumps over the lazy dog";

    private static final String digits = "0123456789";

    @SuppressWarnings("SameParameterValue")
    private void doTestFont(String aliasName, String name, int style, int size)
            throws Exception {

        String[] testDataVariant = {
                "osx_hardware_rendering", "osx_software_rendering", "osx_sierra_rendering",
                "linux_rendering"};

        String testDataStr = System.getProperty("testdata");
        assertNotNull("testdata property is not set", testDataStr);

        File testData = new File(testDataStr, "quality" + File.separator +
                "text");
        assertTrue("Test data dir does not exist", testData.exists());

        String testStr = abc.toUpperCase() + abc + digits;

        BufferedImage image = new BufferedImage((size + 3)*testStr.length(),
                size*3, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();

        Font f = new Font(aliasName, style, size);

        g2d.setFont(f);
        g2d.setColor(Color.WHITE);
        Rectangle2D bnd = f.getStringBounds(testStr,
                g2d.getFontRenderContext());

        g2d.drawString(testStr, 0, size + 3);


        BufferedImage resultImage = image.getSubimage((int) bnd.getX(),
                (int) (size + 3 + bnd.getY()),
                (int) bnd.getWidth(), (int) bnd.getHeight());

        String gfName = name.toLowerCase().replace(" ", "") +
                Integer.toString(style) + "_" + Integer.toString(size) + ".png";

        if (System.getProperty("gentestdata") == null) {
            boolean failed = true;
            StringBuilder failureReason = new StringBuilder();
            for (String variant : testDataVariant) {
                File goldenFile = new File(testData, variant + File.separator +
                        gfName);

                BufferedImage goldenImage = ImageIO.read(goldenFile);
                failed = true;
                if (resultImage.getWidth() != goldenImage.getWidth() ||
                    resultImage.getHeight() != resultImage.getHeight())
                {
                    failureReason.append(variant).append(" : Golden image and result have different sizes\n");
                    continue;
                }

                Raster gRaster = goldenImage.getData();
                Raster rRaster = resultImage.getData();
                int[] gArr = new int[3];
                int[] rArr = new int[3];
                failed = false;
                scan:
                for (int i = 0; i < gRaster.getWidth(); i++) {
                    for (int j = 0; j < gRaster.getHeight(); j++) {
                        gRaster.getPixel(i, j, gArr);
                        rRaster.getPixel(i, j, rArr);
                        assertTrue(gArr.length == rArr.length);
                        for (int k = 0; k < gArr.length; k++) {
                            if (gArr[k] != rArr[k]) {
                                failureReason.append(variant).append(" : Different pixels found ").append("at (").append(i).append(",").append(j).append(")");
                                failed = true;
                                break scan;
                            }
                        }
                    }
                }

                if (!failed) break;
            }

            if (failed) throw new RuntimeException(failureReason.toString());
        }
        else {
            ImageIO.write(resultImage, "png", new File(testData, gfName));
        }
    }

    private void doTestFont(String name, int style)
            throws Exception {
        doTestFont(name, name, style, 20);
    }

        @Test
    public void testDroidSans() throws Exception {
        doTestFont("Droid Sans", Font.PLAIN);
    }

    @Test
    public void testDroidSansBold() throws Exception {
        doTestFont("Droid Sans", Font.BOLD);
        doTestFont("Droid Sans Bold", "Droid Sans", Font.BOLD, 20);
    }

    @Test
    public void testDroidSansMono() throws Exception {
        doTestFont("Droid Sans Mono", Font.PLAIN);
    }

    @Test
    public void testDroidSansMonoSlashed() throws Exception {
        doTestFont("Droid Sans Mono Slashed", Font.PLAIN);
    }

    @Test
    public void testDroidSansMonoDotted() throws Exception {
        doTestFont("Droid Sans Mono Dotted", Font.PLAIN);
    }

    @Test
    public void testDroidSerif() throws Exception {
        doTestFont("Droid Serif", Font.PLAIN);
    }

    @Test
    public void testDroidSerifBold() throws Exception {
        doTestFont("Droid Serif", Font.BOLD);
        doTestFont("Droid Serif Bold", "Droid Serif",
                Font.BOLD, 20);
        doTestFont("Droid Serif Bold", "Droid Serif",
                Font.BOLD | Font.ITALIC, 20);
    }

    @Test
    public void testDroidSerifItalic() throws Exception {
        doTestFont("Droid Serif", Font.ITALIC);
        doTestFont("Droid Serif Italic", "Droid Serif",
                Font.ITALIC, 20);
    }
}