~ubuntu-branches/ubuntu/wily/libiscwt-java/wily

« back to all changes in this revision

Viewing changes to .pc/java6andjava7-compat.patch/src/de/intarsys/cwt/font/FontEnvironment.java

  • Committer: Package Import Robot
  • Author(s): ShuxiongYe, Miguel Landaeta, James Page, ShuxiongYe
  • Date: 2013-07-05 18:59:40 UTC
  • Revision ID: package-import@ubuntu.com-20130705185940-xxf5x7yj3ed930aa
Tags: 5.3.20100629-3
[ Miguel Landaeta ]
* Team upload.
* Bump Standards-Version to 3.9.4. No changes were required.
* Fix vcs-field-not-canonical lintian warning.
* Fix extended-description-is-probably-too-short lintian warning.
* Fix needless-dependency-on-jre lintian warning.

[ James Page ]
* Fix FTBFS with openjdk-7 as default-jdk (LP: #888963) (Closes: 678032):
  - d/patches/java7-compat.patch: Update usage of FontManager for
    Java 7.

[ ShuxiongYe ]
* Compatible with both openjdk-6 and openjdk-7
  - debian/patch/java6andjava7-compat.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2008, intarsys consulting GmbH
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 * - Redistributions of source code must retain the above copyright notice,
 
8
 *   this list of conditions and the following disclaimer.
 
9
 *
 
10
 * - Redistributions in binary form must reproduce the above copyright notice,
 
11
 *   this list of conditions and the following disclaimer in the documentation
 
12
 *   and/or other materials provided with the distribution.
 
13
 *
 
14
 * - Neither the name of intarsys nor the names of its contributors may be used
 
15
 *   to endorse or promote products derived from this software without specific
 
16
 *   prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
21
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
22
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
23
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
24
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
25
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
26
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
27
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
28
 * POSSIBILITY OF SUCH DAMAGE.
 
29
 */
 
30
package de.intarsys.cwt.font;
 
31
 
 
32
import java.awt.Font;
 
33
import java.io.File;
 
34
import java.io.IOException;
 
35
import java.io.InputStream;
 
36
import java.net.URL;
 
37
import java.util.ArrayList;
 
38
import java.util.Enumeration;
 
39
import java.util.List;
 
40
import java.util.logging.Level;
 
41
import java.util.logging.Logger;
 
42
 
 
43
import sun.font.FontManager;
 
44
import de.intarsys.cwt.freetype.Face;
 
45
import de.intarsys.cwt.freetype.Freetype;
 
46
import de.intarsys.cwt.freetype.Library;
 
47
import de.intarsys.tools.installresource.InstallFileList;
 
48
import de.intarsys.tools.locator.FileLocator;
 
49
import de.intarsys.tools.stream.StreamTools;
 
50
 
 
51
/**
 
52
 * 
 
53
 */
 
54
public class FontEnvironment {
 
55
 
 
56
        private static final Logger Log = PACKAGE.Log;
 
57
 
 
58
        private static FontEnvironment Unique = new FontEnvironment();
 
59
 
 
60
        static public FontEnvironment get() {
 
61
                return Unique;
 
62
        }
 
63
 
 
64
        static public void set(FontEnvironment environment) {
 
65
                Unique = environment;
 
66
        }
 
67
 
 
68
        private List<ClassLoader> fontClassLoaders = new ArrayList<ClassLoader>();
 
69
 
 
70
        private List<File> fontDirectories = new ArrayList<File>();
 
71
 
 
72
        private List<File> fontFiles = new ArrayList<File>();
 
73
 
 
74
        private boolean registeredSystem = false;
 
75
 
 
76
        private boolean registeredUser = false;
 
77
 
 
78
        public FontEnvironment() {
 
79
                registerFontClassLoader(getClass().getClassLoader());
 
80
        }
 
81
 
 
82
        synchronized public ClassLoader[] getFontClassLoaders() {
 
83
                return fontClassLoaders
 
84
                                .toArray(new ClassLoader[fontClassLoaders.size()]);
 
85
        }
 
86
 
 
87
        synchronized public File[] getFontDirectories() {
 
88
                return fontDirectories.toArray(new File[fontDirectories.size()]);
 
89
        }
 
90
 
 
91
        synchronized public File[] getFontFiles() {
 
92
                return fontFiles.toArray(new File[fontFiles.size()]);
 
93
        }
 
94
 
 
95
        /**
 
96
         * This method determines the system's font directories.
 
97
         * 
 
98
         * @return an array containing the font directory paths found on the local
 
99
         *         system
 
100
         */
 
101
        synchronized public File[] getSystemFontDirectories() {
 
102
                String definition = null;
 
103
                // force FontManager initialization
 
104
                Font.decode("dummy").getFamily(); //$NON-NLS-1$
 
105
                definition = sun.font.SunFontManager.getInstance().getPlatformFontPath(true);
 
106
                if (definition == null) {
 
107
                        return new File[0];
 
108
                }
 
109
                String[] names = definition.split(System.getProperty("path.separator")); //$NON-NLS-1$
 
110
                File[] files = new File[names.length];
 
111
                for (int i = 0; i < names.length; i++) {
 
112
                        files[i] = new File(names[i]);
 
113
                }
 
114
                return files;
 
115
        }
 
116
 
 
117
        protected void loadFontClassLoader(Library library, ClassLoader loader) {
 
118
                try {
 
119
                        InstallFileList installer = new InstallFileList("fonts",
 
120
                                        "fonts.list", false);
 
121
                        installer.loadAll();
 
122
                        File[] roots = installer.getFiles();
 
123
                        for (int i = 0; i < roots.length; i++) {
 
124
                                File root = roots[i];
 
125
                                File[] fontFiles = root.listFiles();
 
126
                                for (int j = 0; j < fontFiles.length; j++) {
 
127
                                        File file = fontFiles[j];
 
128
                                        try {
 
129
                                                processFontFile(library, file);
 
130
                                        } catch (IOException e) {
 
131
                                                Log.log(Level.WARNING, "error loading font '"
 
132
                                                                + file.getName() + "'", e);
 
133
                                        } finally {
 
134
                                        }
 
135
                                }
 
136
                        }
 
137
                } catch (IOException e) {
 
138
                        Log.log(Level.WARNING,
 
139
                                        "error looking up 'font/fonts.list' resources", e);
 
140
                }
 
141
        }
 
142
 
 
143
        protected void loadFontDirectory(Library library, File directory) {
 
144
                if (!directory.exists()) {
 
145
                        return;
 
146
                }
 
147
                if (!directory.isDirectory()) {
 
148
                        return;
 
149
                }
 
150
                File[] files = directory.listFiles();
 
151
                if (files == null) {
 
152
                        return;
 
153
                }
 
154
                for (int i = 0; i < files.length; i++) {
 
155
                        File file = files[i];
 
156
                        loadFontFile(library, file);
 
157
                }
 
158
        }
 
159
 
 
160
        protected void loadFontFile(Library library, File file) {
 
161
                if (!file.isFile()) {
 
162
                        return;
 
163
                }
 
164
                String filepath = file.getAbsolutePath();
 
165
                try {
 
166
                        /*
 
167
                         * the absolute path/canonical path check is used to find out if the
 
168
                         * file path contains a symbolic link. if so we can ignore the file
 
169
                         * because it will also show up in another directory as a "real"
 
170
                         * file.
 
171
                         */
 
172
                        if (!filepath.equals(file.getCanonicalPath())) {
 
173
                                return;
 
174
                        }
 
175
                } catch (IOException ex) {
 
176
                        return;
 
177
                }
 
178
                loadFontUnchecked(library, filepath);
 
179
        }
 
180
 
 
181
        protected void loadFontMapClassLoader(Library library, ClassLoader loader) {
 
182
                try {
 
183
                        Enumeration<URL> urls = loader.getResources("fonts/fonts.maps");
 
184
                        while (urls.hasMoreElements()) {
 
185
                                URL url = urls.nextElement();
 
186
                                InputStream is = null;
 
187
                                try {
 
188
                                        is = url.openStream();
 
189
                                        parseMaps(library, loader, is);
 
190
                                } catch (IOException e) {
 
191
                                        Log.log(Level.WARNING,
 
192
                                                        "error loading 'fonts/fonts.maps' from '" + url
 
193
                                                                        + "'", e);
 
194
                                } finally {
 
195
                                        StreamTools.close(is);
 
196
                                }
 
197
                        }
 
198
                } catch (IOException e) {
 
199
                        Log.log(Level.WARNING,
 
200
                                        "error looking up 'font/fonts.maps' resources", e);
 
201
                }
 
202
        }
 
203
 
 
204
        protected IFont loadFontUnchecked(Library library, String filepath) {
 
205
                FileLocator locator = new FileLocator(filepath);
 
206
                Face face = library.newFace(filepath, 0);
 
207
                if (face != null) {
 
208
                        IFont font = null;
 
209
                        try {
 
210
                                font = GenericFont.createNew(locator, face);
 
211
                                FontRegistry.get().registerFont(font);
 
212
                        } finally {
 
213
                                face.doneFace();
 
214
                        }
 
215
                        return font;
 
216
                } else {
 
217
                        // Log.log(Level.WARNING, "can't register font for " + filepath);
 
218
                        return null;
 
219
                }
 
220
        }
 
221
 
 
222
        protected void loadSystemFonts(Library library) {
 
223
                File[] files;
 
224
                files = getSystemFontDirectories();
 
225
                for (int i = 0; i < files.length; i++) {
 
226
                        File directory = files[i];
 
227
                        loadFontDirectory(library, directory);
 
228
                }
 
229
        }
 
230
 
 
231
        protected void loadUserFonts(Library library) {
 
232
                File[] files;
 
233
                files = getFontDirectories();
 
234
                for (int i = 0; i < files.length; i++) {
 
235
                        File directory = files[i];
 
236
                        loadFontDirectory(library, directory);
 
237
                }
 
238
                files = getFontFiles();
 
239
                for (int i = 0; i < files.length; i++) {
 
240
                        File file = files[i];
 
241
                        loadFontFile(library, file);
 
242
                }
 
243
                ClassLoader[] loaders = getFontClassLoaders();
 
244
                for (int i = 0; i < loaders.length; i++) {
 
245
                        ClassLoader loader = loaders[i];
 
246
                        loadFontClassLoader(library, loader);
 
247
                        loadFontMapClassLoader(library, loader);
 
248
                }
 
249
        }
 
250
 
 
251
        protected void parseMaps(Library library, ClassLoader loader, InputStream is)
 
252
                        throws IOException {
 
253
                StringBuilder sb = new StringBuilder();
 
254
                int i = is.read();
 
255
                while (i != -1) {
 
256
                        if (i == '\n') {
 
257
                                String map = sb.toString().trim();
 
258
                                processFontMap(library, loader, map);
 
259
                                sb.setLength(0);
 
260
                                i = is.read();
 
261
                                continue;
 
262
                        }
 
263
                        sb.append((char) i);
 
264
                        i = is.read();
 
265
                }
 
266
                String map = sb.toString().trim();
 
267
                processFontMap(library, loader, map);
 
268
        }
 
269
 
 
270
        protected void processFontFile(Library library, File file)
 
271
                        throws IOException {
 
272
                IFont font = loadFontUnchecked(library, file.getAbsolutePath());
 
273
                if (font == null) {
 
274
                        return;
 
275
                }
 
276
                FontTools.mapFont(font.getFontName(), font);
 
277
        }
 
278
 
 
279
        protected void processFontMap(Library library, ClassLoader loader,
 
280
                        String map) throws IOException {
 
281
                if (map.length() == 0 || map.startsWith("#")) {
 
282
                        return;
 
283
                }
 
284
                String[] split = map.split("\\=");
 
285
                if (split.length < 2) {
 
286
                        return;
 
287
                }
 
288
                FontTools.mapAlias(split[0], split[1]);
 
289
        }
 
290
 
 
291
        public synchronized void registerFontClassLoader(ClassLoader loader) {
 
292
                fontClassLoaders.add(loader);
 
293
        }
 
294
 
 
295
        public synchronized void registerFontDirectory(File directory) {
 
296
                fontDirectories.add(directory);
 
297
        }
 
298
 
 
299
        synchronized public void registerFontFile(File file) {
 
300
                fontFiles.add(file);
 
301
        }
 
302
 
 
303
        synchronized public boolean registerSystemFonts() {
 
304
                if (registeredSystem) {
 
305
                        return false;
 
306
                }
 
307
                registeredSystem = true;
 
308
                Library library = Freetype.initFreeType();
 
309
                try {
 
310
                        loadSystemFonts(library);
 
311
                } finally {
 
312
                        library.doneFreeType();
 
313
                }
 
314
                return true;
 
315
        }
 
316
 
 
317
        synchronized public boolean registerUserFonts() {
 
318
                if (registeredUser) {
 
319
                        return false;
 
320
                }
 
321
                registeredUser = true;
 
322
                Library library = Freetype.initFreeType();
 
323
                try {
 
324
                        loadUserFonts(library);
 
325
                } finally {
 
326
                        library.doneFreeType();
 
327
                }
 
328
                return true;
 
329
        }
 
330
 
 
331
}