author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 3928 | be186a33df9b |
child 8813 | d15a9204c2f0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.font; |
|
27 |
||
28 |
import java.lang.ref.Reference; |
|
29 |
import java.awt.FontFormatException; |
|
30 |
import java.awt.geom.GeneralPath; |
|
31 |
import java.awt.geom.Point2D; |
|
32 |
import java.awt.geom.Rectangle2D; |
|
33 |
import java.io.File; |
|
34 |
import java.nio.ByteBuffer; |
|
35 |
import java.nio.channels.FileChannel; |
|
36 |
import sun.java2d.Disposer; |
|
37 |
import sun.java2d.DisposerRecord; |
|
38 |
||
39 |
import java.lang.ref.WeakReference; |
|
40 |
import java.io.FileNotFoundException; |
|
41 |
import java.io.IOException; |
|
42 |
import java.io.RandomAccessFile; |
|
43 |
import java.io.UnsupportedEncodingException; |
|
44 |
import java.nio.ByteOrder; |
|
45 |
import java.nio.MappedByteBuffer; |
|
46 |
import java.nio.BufferUnderflowException; |
|
47 |
import java.nio.channels.ClosedChannelException; |
|
48 |
import java.util.HashSet; |
|
49 |
import java.util.HashMap; |
|
50 |
import java.awt.Font; |
|
51 |
||
52 |
public abstract class FileFont extends PhysicalFont { |
|
53 |
||
54 |
protected boolean useJavaRasterizer = true; |
|
55 |
||
56 |
/* I/O and file operations are always synchronized on the font |
|
57 |
* object. Two threads can be accessing the font and retrieving |
|
58 |
* information, and synchronized only to the extent that filesystem |
|
59 |
* operations require. |
|
60 |
* A limited number of files can be open at a time, to limit the |
|
61 |
* absorption of file descriptors. If a file needs to be opened |
|
62 |
* when there are none free, then the synchronization of all I/O |
|
63 |
* ensures that any in progress operation will complete before some |
|
64 |
* other thread closes the descriptor in order to allocate another one. |
|
65 |
*/ |
|
66 |
// NB consider using a RAF. FIS has finalize method so may take a |
|
67 |
// little longer to be GC'd. We don't use this stream at all anyway. |
|
68 |
// In fact why increase the size of a FileFont object if the stream |
|
69 |
// isn't needed .. |
|
70 |
//protected FileInputStream stream; |
|
71 |
//protected FileChannel channel; |
|
72 |
protected int fileSize; |
|
73 |
||
74 |
protected FontScaler scaler; |
|
75 |
||
76 |
/* The following variables are used, (and in the case of the arrays, |
|
77 |
* only initialised) for select fonts where a native scaler may be |
|
78 |
* used to get glyph images and metrics. |
|
79 |
* glyphToCharMap is filled in on the fly and used to do a reverse |
|
80 |
* lookup when a FileFont needs to get the charcode back from a glyph |
|
81 |
* code so it can re-map via a NativeGlyphMapper to get a native glyph. |
|
82 |
* This isn't a big hit in time, since a boolean test is sufficient |
|
83 |
* to choose the usual default path, nor in memory for fonts which take |
|
84 |
* the native path, since fonts have contiguous zero-based glyph indexes, |
|
85 |
* and these obviously do all exist in the font. |
|
86 |
*/ |
|
87 |
protected boolean checkedNatives; |
|
88 |
protected boolean useNatives; |
|
89 |
protected NativeFont[] nativeFonts; |
|
90 |
protected char[] glyphToCharMap; |
|
91 |
/* |
|
92 |
* @throws FontFormatException - if the font can't be opened |
|
93 |
*/ |
|
94 |
FileFont(String platname, Object nativeNames) |
|
95 |
throws FontFormatException { |
|
96 |
||
97 |
super(platname, nativeNames); |
|
98 |
} |
|
99 |
||
100 |
FontStrike createStrike(FontStrikeDesc desc) { |
|
101 |
if (!checkedNatives) { |
|
102 |
checkUseNatives(); |
|
103 |
} |
|
104 |
return new FileFontStrike(this, desc); |
|
105 |
} |
|
106 |
||
107 |
protected boolean checkUseNatives() { |
|
108 |
checkedNatives = true; |
|
109 |
return useNatives; |
|
110 |
} |
|
111 |
||
112 |
/* This method needs to be accessible to FontManager if there is |
|
113 |
* file pool management. It may be a no-op. |
|
114 |
*/ |
|
115 |
protected abstract void close(); |
|
116 |
||
117 |
||
118 |
/* |
|
119 |
* This is the public interface. The subclasses need to implement |
|
120 |
* this. The returned block may be longer than the requested length. |
|
121 |
*/ |
|
122 |
abstract ByteBuffer readBlock(int offset, int length); |
|
123 |
||
124 |
public boolean canDoStyle(int style) { |
|
125 |
return true; |
|
126 |
} |
|
127 |
||
2606
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
2
diff
changeset
|
128 |
void setFileToRemove(File file, CreatedFontTracker tracker) { |
2 | 129 |
Disposer.addObjectRecord(this, |
2606
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
2
diff
changeset
|
130 |
new CreatedFontFileDisposerRecord(file, tracker)); |
2 | 131 |
} |
132 |
||
133 |
/* This is called when a font scaler is determined to |
|
134 |
* be unusable (ie bad). |
|
135 |
* We want to replace current scaler with NullFontScaler, so |
|
136 |
* we never try to use same font scaler again. |
|
137 |
* Scaler native resources could have already been disposed |
|
138 |
* or they will be eventually by Java2D disposer. |
|
139 |
* However, it should be safe to call dispose() explicitly here. |
|
140 |
* |
|
141 |
* For safety we also invalidate all strike's scaler context. |
|
142 |
* So, in case they cache pointer to native scaler |
|
143 |
* it will not ever be used. |
|
144 |
* |
|
145 |
* It also appears desirable to remove all the entries from the |
|
146 |
* cache so no other code will pick them up. But we can't just |
|
147 |
* 'delete' them as code may be using them. And simply dropping |
|
148 |
* the reference to the cache will make the reference objects |
|
149 |
* unreachable and so they will not get disposed. |
|
150 |
* Since a strike may hold (via java arrays) native pointers to many |
|
151 |
* rasterised glyphs, this would be a memory leak. |
|
152 |
* The solution is : |
|
153 |
* - to move all the entries to another map where they |
|
154 |
* are no longer locatable |
|
155 |
* - update FontStrikeDisposer to be able to distinguish which |
|
156 |
* map they are held in via a boolean flag |
|
157 |
* Since this isn't expected to be anything other than an extremely |
|
158 |
* rare maybe it is not worth doing this last part. |
|
159 |
*/ |
|
160 |
synchronized void deregisterFontAndClearStrikeCache() { |
|
3928 | 161 |
SunFontManager fm = SunFontManager.getInstance(); |
162 |
fm.deRegisterBadFont(this); |
|
2 | 163 |
|
164 |
for (Reference strikeRef : strikeCache.values()) { |
|
165 |
if (strikeRef != null) { |
|
166 |
/* NB we know these are all FileFontStrike instances |
|
167 |
* because the cache is on this FileFont |
|
168 |
*/ |
|
169 |
FileFontStrike strike = (FileFontStrike)strikeRef.get(); |
|
170 |
if (strike != null && strike.pScalerContext != 0L) { |
|
171 |
scaler.invalidateScalerContext(strike.pScalerContext); |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
scaler.dispose(); |
|
3928 | 176 |
scaler = FontScaler.getNullScaler(); |
2 | 177 |
} |
178 |
||
179 |
StrikeMetrics getFontMetrics(long pScalerContext) { |
|
180 |
try { |
|
181 |
return getScaler().getFontMetrics(pScalerContext); |
|
182 |
} catch (FontScalerException fe) { |
|
3928 | 183 |
scaler = FontScaler.getNullScaler(); |
2 | 184 |
return getFontMetrics(pScalerContext); |
185 |
} |
|
186 |
} |
|
187 |
||
188 |
float getGlyphAdvance(long pScalerContext, int glyphCode) { |
|
189 |
try { |
|
190 |
return getScaler().getGlyphAdvance(pScalerContext, glyphCode); |
|
191 |
} catch (FontScalerException fe) { |
|
3928 | 192 |
scaler = FontScaler.getNullScaler(); |
2 | 193 |
return getGlyphAdvance(pScalerContext, glyphCode); |
194 |
} |
|
195 |
} |
|
196 |
||
197 |
void getGlyphMetrics(long pScalerContext, int glyphCode, Point2D.Float metrics) { |
|
198 |
try { |
|
199 |
getScaler().getGlyphMetrics(pScalerContext, glyphCode, metrics); |
|
200 |
} catch (FontScalerException fe) { |
|
3928 | 201 |
scaler = FontScaler.getNullScaler(); |
2 | 202 |
getGlyphMetrics(pScalerContext, glyphCode, metrics); |
203 |
} |
|
204 |
} |
|
205 |
||
206 |
long getGlyphImage(long pScalerContext, int glyphCode) { |
|
207 |
try { |
|
208 |
return getScaler().getGlyphImage(pScalerContext, glyphCode); |
|
209 |
} catch (FontScalerException fe) { |
|
3928 | 210 |
scaler = FontScaler.getNullScaler(); |
2 | 211 |
return getGlyphImage(pScalerContext, glyphCode); |
212 |
} |
|
213 |
} |
|
214 |
||
215 |
Rectangle2D.Float getGlyphOutlineBounds(long pScalerContext, int glyphCode) { |
|
216 |
try { |
|
217 |
return getScaler().getGlyphOutlineBounds(pScalerContext, glyphCode); |
|
218 |
} catch (FontScalerException fe) { |
|
3928 | 219 |
scaler = FontScaler.getNullScaler(); |
2 | 220 |
return getGlyphOutlineBounds(pScalerContext, glyphCode); |
221 |
} |
|
222 |
} |
|
223 |
||
224 |
GeneralPath getGlyphOutline(long pScalerContext, int glyphCode, float x, float y) { |
|
225 |
try { |
|
226 |
return getScaler().getGlyphOutline(pScalerContext, glyphCode, x, y); |
|
227 |
} catch (FontScalerException fe) { |
|
3928 | 228 |
scaler = FontScaler.getNullScaler(); |
2 | 229 |
return getGlyphOutline(pScalerContext, glyphCode, x, y); |
230 |
} |
|
231 |
} |
|
232 |
||
233 |
GeneralPath getGlyphVectorOutline(long pScalerContext, int[] glyphs, int numGlyphs, float x, float y) { |
|
234 |
try { |
|
235 |
return getScaler().getGlyphVectorOutline(pScalerContext, glyphs, numGlyphs, x, y); |
|
236 |
} catch (FontScalerException fe) { |
|
3928 | 237 |
scaler = FontScaler.getNullScaler(); |
2 | 238 |
return getGlyphVectorOutline(pScalerContext, glyphs, numGlyphs, x, y); |
239 |
} |
|
240 |
} |
|
241 |
||
242 |
/* T1 & TT implementation differ so this method is abstract. |
|
243 |
NB: null should not be returned here! */ |
|
244 |
protected abstract FontScaler getScaler(); |
|
245 |
||
246 |
protected long getUnitsPerEm() { |
|
247 |
return getScaler().getUnitsPerEm(); |
|
248 |
} |
|
249 |
||
2606
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
2
diff
changeset
|
250 |
private static class CreatedFontFileDisposerRecord |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
2
diff
changeset
|
251 |
implements DisposerRecord { |
2 | 252 |
|
253 |
File fontFile = null; |
|
2606
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
2
diff
changeset
|
254 |
CreatedFontTracker tracker; |
2 | 255 |
|
2606
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
2
diff
changeset
|
256 |
private CreatedFontFileDisposerRecord(File file, |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
2
diff
changeset
|
257 |
CreatedFontTracker tracker) { |
2 | 258 |
fontFile = file; |
2606
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
2
diff
changeset
|
259 |
this.tracker = tracker; |
2 | 260 |
} |
261 |
||
262 |
public void dispose() { |
|
263 |
java.security.AccessController.doPrivileged( |
|
264 |
new java.security.PrivilegedAction() { |
|
265 |
public Object run() { |
|
266 |
if (fontFile != null) { |
|
267 |
try { |
|
2606
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
2
diff
changeset
|
268 |
if (tracker != null) { |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
2
diff
changeset
|
269 |
tracker.subBytes((int)fontFile.length()); |
09ad5edb5330
6632886: Font.createFont can be persuaded to leak temporary files
prr
parents:
2
diff
changeset
|
270 |
} |
2 | 271 |
/* REMIND: is it possible that the file is |
272 |
* still open? It will be closed when the |
|
273 |
* font2D is disposed but could this code |
|
274 |
* execute first? If so the file would not |
|
275 |
* be deleted on MS-windows. |
|
276 |
*/ |
|
277 |
fontFile.delete(); |
|
278 |
/* remove from delete on exit hook list : */ |
|
3928 | 279 |
// FIXME: still need to be refactored |
280 |
SunFontManager.getInstance().tmpFontFiles.remove(fontFile); |
|
2 | 281 |
} catch (Exception e) { |
282 |
} |
|
283 |
} |
|
284 |
return null; |
|
285 |
} |
|
286 |
}); |
|
287 |
} |
|
288 |
} |
|
289 |
} |