1 /*
2  * Copyright (c) 2019, 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 8219901
27  * @modules java.desktop/sun.font
28  * @requires (os.family == "linux")
29  * @summary Verifies if the first fonts of CompositeFont and fc-match are same.
30  */
31 
32 import java.awt.Font;
33 import java.util.Locale;
34 import java.io.BufferedReader;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.InputStreamReader;
38 import sun.font.FontUtilities;
39 import sun.font.Font2D;
40 import sun.font.CompositeFont;
41 import sun.font.PhysicalFont;
42 
43 public class FCCompositeTest {
44 
45     final static String[] names =
46         new String[]{"Monospaced","SansSerif","Serif"};
47     final static String[] fcnames =
48         new String[]{"monospace","sans","serif"};
49 
main(String args[])50     public static void main(String args[]) {
51         for(int i = 0; i < names.length; i++) {
52             test(i);
53         }
54     }
55 
test(int index)56     private static void test(int index) {
57         boolean matched = false;
58         String fullName = "";
59         String fcFullName = "";
60         try {
61             Font2D f2d = FontUtilities.getFont2D(
62                              new Font(names[index], Font.PLAIN, 12));
63             if (!(f2d instanceof CompositeFont)) {
64                 System.out.println("WARNING: Not CompositeFont");
65                 return;
66             }
67             PhysicalFont pf = ((CompositeFont)f2d).getSlotFont(0);
68             fullName = pf.getFontName(Locale.ENGLISH);
69             System.out.println("PF="+fullName);
70 
71             String[] command = {"fc-match",
72                                 fcnames[index],
73                                 "fullname"};
74             Runtime runtime = Runtime.getRuntime();
75             Process p = runtime.exec(command, null, null);
76             p.waitFor();
77             InputStream is = p.getInputStream();
78             InputStream es = p.getErrorStream();
79             BufferedReader br =
80                 new BufferedReader(new InputStreamReader(is));
81             BufferedReader errorBr =
82                 new BufferedReader(new InputStreamReader(es));
83             String line;
84             while ((line = errorBr.readLine()) != null) {
85                 if (line.contains("warning") && line.contains("language")) {
86                     System.out.println("Skip test by fc-match warning");
87                     return;
88                 }
89             }
90             while (!matched) {
91                 String fcname = br.readLine();
92                 if (fcname == null) break;
93                 fcFullName = fcname;
94                 if (fcname.equals("")) {
95                     System.out.println("Skip if no fullname");
96                     return;
97                 }
98                 fcname = fcname.replaceAll("\\\\", "");
99                 String[] list = fcname.split("=|,", 0);
100                 for (int i = 1; i < list.length; i++) {
101                     // skip header
102                     if (fullName.equals(list[i])) {
103                         matched = true;
104                         break;
105                     }
106                 }
107             }
108             br.close();
109         } catch (Exception e) {
110             e.printStackTrace();
111             throw new RuntimeException("Method invocation exception");
112         }
113         if (!matched) {
114             throw new RuntimeException("FullName mismatch: "+fullName+"|"+
115                                            fcFullName);
116         }
117     }
118 }
119