jdk

Форк
0
/
Win32FontManager.java 
359 строк · 13.3 Кб
1
/*
2
 * Copyright (c) 2008, 2021, 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.  Oracle designates this
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
10
 *
11
 * This code is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14
 * version 2 for more details (a copy is included in the LICENSE file that
15
 * accompanied this code).
16
 *
17
 * You should have received a copy of the GNU General Public License version
18
 * 2 along with this work; if not, write to the Free Software Foundation,
19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
 *
21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
 * or visit www.oracle.com if you need additional information or have any
23
 * questions.
24
 */
25

26

27
package sun.awt;
28

29
import java.awt.FontFormatException;
30
import java.awt.GraphicsEnvironment;
31
import java.io.File;
32
import java.security.AccessController;
33
import java.security.PrivilegedAction;
34
import java.util.ArrayList;
35
import java.util.HashMap;
36
import java.util.Locale;
37
import java.util.NoSuchElementException;
38
import java.util.StringTokenizer;
39

40
import sun.awt.windows.WFontConfiguration;
41
import sun.font.FontManager;
42
import sun.font.SunFontManager;
43
import sun.font.TrueTypeFont;
44

45
/**
46
 * The X11 implementation of {@link FontManager}.
47
 */
48
public final class Win32FontManager extends SunFontManager {
49

50
    @SuppressWarnings("removal")
51
    private static final TrueTypeFont eudcFont =
52
            AccessController.doPrivileged(new PrivilegedAction<TrueTypeFont>() {
53
                public TrueTypeFont run() {
54
                    String eudcFile = getEUDCFontFile();
55
                    if (eudcFile != null) {
56
                        try {
57
                            /* Must use Java rasteriser since GDI doesn't
58
                             * enumerate (allow direct use) of EUDC fonts.
59
                             */
60
                            return new TrueTypeFont(eudcFile, null, 0,
61
                                                        true, false);
62
                        } catch (FontFormatException e) {
63
                        }
64
                    }
65
                    return null;
66
                }
67
            });
68

69
    /* Used on Windows to obtain from the windows registry the name
70
     * of a file containing the system EUFC font. If running in one of
71
     * the locales for which this applies, and one is defined, the font
72
     * defined by this file is appended to all composite fonts as a
73
     * fallback component.
74
     */
75
    private static native String getEUDCFontFile();
76

77
    public TrueTypeFont getEUDCFont() {
78
        return eudcFont;
79
    }
80

81
    @SuppressWarnings("removal")
82
    public Win32FontManager() {
83
        super();
84
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
85
                public Object run() {
86

87
                    /* Register the JRE fonts so that the native platform can
88
                     * access them. This is used only on Windows so that when
89
                     * printing the printer driver can access the fonts.
90
                     */
91
                    registerJREFontsWithPlatform(jreFontDirName);
92
                    return null;
93
                }
94
            });
95
    }
96

97
    /**
98
     * Whether registerFontFile expects absolute or relative
99
     * font file names.
100
     */
101
    protected boolean useAbsoluteFontFileNames() {
102
        return false;
103
    }
104

105
    /* Unlike the shared code version, this expects a base file name -
106
     * not a full path name.
107
     * The font configuration file has base file names and the FontConfiguration
108
     * class reports these back to the GraphicsEnvironment, so these
109
     * are the componentFileNames of CompositeFonts.
110
     */
111
    protected void registerFontFile(String fontFileName, String[] nativeNames,
112
                                    int fontRank, boolean defer) {
113

114
        // REMIND: case compare depends on platform
115
        if (registeredFontFiles.contains(fontFileName)) {
116
            return;
117
        }
118
        registeredFontFiles.add(fontFileName);
119

120
        int fontFormat;
121
        if (getTrueTypeFilter().accept(null, fontFileName)) {
122
            fontFormat = SunFontManager.FONTFORMAT_TRUETYPE;
123
        } else if (getType1Filter().accept(null, fontFileName)) {
124
            fontFormat = SunFontManager.FONTFORMAT_TYPE1;
125
        } else {
126
            /* on windows we don't use/register native fonts */
127
            return;
128
        }
129

130
        if (fontPath == null) {
131
            fontPath = getPlatformFontPath(noType1Font);
132
        }
133

134
        /* Look in the JRE font directory first.
135
         * This is playing it safe as we would want to find fonts in the
136
         * JRE font directory ahead of those in the system directory
137
         */
138
        String tmpFontPath = jreFontDirName+File.pathSeparator+fontPath;
139
        StringTokenizer parser = new StringTokenizer(tmpFontPath,
140
                                                     File.pathSeparator);
141

142
        boolean found = false;
143
        try {
144
            while (!found && parser.hasMoreTokens()) {
145
                String newPath = parser.nextToken();
146
                boolean isJREFont = newPath.equals(jreFontDirName);
147
                File theFile = new File(newPath, fontFileName);
148
                if (theFile.canRead()) {
149
                    found = true;
150
                    String path = theFile.getAbsolutePath();
151
                    if (defer) {
152
                        registerDeferredFont(fontFileName, path,
153
                                             nativeNames,
154
                                             fontFormat, isJREFont,
155
                                             fontRank);
156
                    } else {
157
                        registerFontFile(path, nativeNames,
158
                                         fontFormat, isJREFont,
159
                                         fontRank);
160
                    }
161
                    break;
162
                }
163
            }
164
        } catch (NoSuchElementException e) {
165
            System.err.println(e);
166
        }
167
        if (!found) {
168
            addToMissingFontFileList(fontFileName);
169
        }
170
    }
171

172
    @Override
173
    protected FontConfiguration createFontConfiguration() {
174

175
       FontConfiguration fc = new WFontConfiguration(this);
176
       fc.init();
177
       return fc;
178
    }
179

180
    @Override
181
    public FontConfiguration createFontConfiguration(boolean preferLocaleFonts,
182
            boolean preferPropFonts) {
183

184
        return new WFontConfiguration(this,
185
                                      preferLocaleFonts,preferPropFonts);
186
    }
187

188
    protected void
189
        populateFontFileNameMap(HashMap<String,String> fontToFileMap,
190
                                HashMap<String,String> fontToFamilyNameMap,
191
                                HashMap<String,ArrayList<String>>
192
                                familyToFontListMap,
193
                                Locale locale) {
194

195
        populateFontFileNameMap0(fontToFileMap, fontToFamilyNameMap,
196
                                 familyToFontListMap, locale);
197

198
    }
199

200
    private static native void
201
        populateFontFileNameMap0(HashMap<String,String> fontToFileMap,
202
                                 HashMap<String,String> fontToFamilyNameMap,
203
                                 HashMap<String,ArrayList<String>>
204
                                     familyToFontListMap,
205
                                 Locale locale);
206

207
    protected synchronized native String getFontPath(boolean noType1Fonts);
208

209
    @Override
210
    protected String[] getDefaultPlatformFont() {
211
        String[] info = new String[2];
212
        info[0] = "Arial";
213
        info[1] = "c:\\windows\\fonts";
214
        final String[] dirs = getPlatformFontDirs(true);
215
        if (dirs.length > 1) {
216
            @SuppressWarnings("removal")
217
            String dir = (String)
218
                AccessController.doPrivileged(new PrivilegedAction<Object>() {
219
                        public Object run() {
220
                            for (int i=0; i<dirs.length; i++) {
221
                                String path =
222
                                    dirs[i] + File.separator + "arial.ttf";
223
                                File file = new File(path);
224
                                if (file.exists()) {
225
                                    return dirs[i];
226
                                }
227
                            }
228
                            return null;
229
                        }
230
                    });
231
            if (dir != null) {
232
                info[1] = dir;
233
            }
234
        } else {
235
            info[1] = dirs[0];
236
        }
237
        info[1] = info[1] + File.separator + "arial.ttf";
238
        return info;
239
    }
240

241
    /* register only TrueType/OpenType fonts
242
     * Because these need to be registered just for use when printing,
243
     * we defer the actual registration and the static initialiser
244
     * for the printing class makes the call to registerJREFontsForPrinting()
245
     */
246
    static String fontsForPrinting = null;
247
    protected void registerJREFontsWithPlatform(String pathName) {
248
        fontsForPrinting = pathName;
249
    }
250

251
    @SuppressWarnings("removal")
252
    public static void registerJREFontsForPrinting() {
253
        final String pathName;
254
        synchronized (Win32GraphicsEnvironment.class) {
255
            GraphicsEnvironment.getLocalGraphicsEnvironment();
256
            if (fontsForPrinting == null) {
257
                return;
258
            }
259
            pathName = fontsForPrinting;
260
            fontsForPrinting = null;
261
        }
262
        java.security.AccessController.doPrivileged(
263
            new java.security.PrivilegedAction<Object>() {
264
                public Object run() {
265
                    File f1 = new File(pathName);
266
                    String[] ls = f1.list(SunFontManager.getInstance().
267
                            getTrueTypeFilter());
268
                    if (ls == null) {
269
                        return null;
270
                    }
271
                    for (int i=0; i <ls.length; i++ ) {
272
                        File fontFile = new File(f1, ls[i]);
273
                        registerFontWithPlatform(fontFile.getAbsolutePath());
274
                    }
275
                    return null;
276
                }
277
         });
278
    }
279

280
    private static native void registerFontWithPlatform(String fontName);
281

282
    private static native void deRegisterFontWithPlatform(String fontName);
283

284
    /**
285
     * populate the map with the most common windows fonts.
286
     */
287
    @Override
288
    public HashMap<String, FamilyDescription> populateHardcodedFileNameMap() {
289
        HashMap<String, FamilyDescription> platformFontMap
290
            = new HashMap<String, FamilyDescription>();
291
        FamilyDescription fd;
292

293
        /* Segoe UI is the default UI font for Vista and later, and
294
         * is used by the Win L&F which is used by FX too.
295
         * Tahoma is used for the Win L&F on XP.
296
         * Verdana is used in some FX UI controls.
297
         */
298
        fd = new FamilyDescription();
299
        fd.familyName = "Segoe UI";
300
        fd.plainFullName = "Segoe UI";
301
        fd.plainFileName = "segoeui.ttf";
302
        fd.boldFullName = "Segoe UI Bold";
303
        fd.boldFileName = "segoeuib.ttf";
304
        fd.italicFullName = "Segoe UI Italic";
305
        fd.italicFileName = "segoeuii.ttf";
306
        fd.boldItalicFullName = "Segoe UI Bold Italic";
307
        fd.boldItalicFileName = "segoeuiz.ttf";
308
        platformFontMap.put("segoe", fd);
309

310
        fd = new FamilyDescription();
311
        fd.familyName = "Tahoma";
312
        fd.plainFullName = "Tahoma";
313
        fd.plainFileName = "tahoma.ttf";
314
        fd.boldFullName = "Tahoma Bold";
315
        fd.boldFileName = "tahomabd.ttf";
316
        platformFontMap.put("tahoma", fd);
317

318
        fd = new FamilyDescription();
319
        fd.familyName = "Verdana";
320
        fd.plainFullName = "Verdana";
321
        fd.plainFileName = "verdana.TTF";
322
        fd.boldFullName = "Verdana Bold";
323
        fd.boldFileName = "verdanab.TTF";
324
        fd.italicFullName = "Verdana Italic";
325
        fd.italicFileName = "verdanai.TTF";
326
        fd.boldItalicFullName = "Verdana Bold Italic";
327
        fd.boldItalicFileName = "verdanaz.TTF";
328
        platformFontMap.put("verdana", fd);
329

330
        /* The following are important because they are the core
331
         * members of the default "Dialog" font.
332
         */
333
        fd = new FamilyDescription();
334
        fd.familyName = "Arial";
335
        fd.plainFullName = "Arial";
336
        fd.plainFileName = "ARIAL.TTF";
337
        fd.boldFullName = "Arial Bold";
338
        fd.boldFileName = "ARIALBD.TTF";
339
        fd.italicFullName = "Arial Italic";
340
        fd.italicFileName = "ARIALI.TTF";
341
        fd.boldItalicFullName = "Arial Bold Italic";
342
        fd.boldItalicFileName = "ARIALBI.TTF";
343
        platformFontMap.put("arial", fd);
344

345
        fd = new FamilyDescription();
346
        fd.familyName = "Symbol";
347
        fd.plainFullName = "Symbol";
348
        fd.plainFileName = "Symbol.TTF";
349
        platformFontMap.put("symbol", fd);
350

351
        fd = new FamilyDescription();
352
        fd.familyName = "WingDings";
353
        fd.plainFullName = "WingDings";
354
        fd.plainFileName = "WINGDING.TTF";
355
        platformFontMap.put("wingdings", fd);
356

357
        return platformFontMap;
358
    }
359
}
360

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.