author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58634 | da71aa13f86b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54239
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
2 |
* Copyright (c) 2007, 2019, 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 |
#include "jni.h" |
|
27 |
#include "jni_util.h" |
|
28 |
#include "jlong.h" |
|
29 |
#include "sunfontids.h" |
|
30 |
#include "sun_font_FreetypeFontScaler.h" |
|
31 |
||
55369
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
32 |
#include <stdlib.h> |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
33 |
#if !defined(_WIN32) && !defined(__APPLE_) |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
34 |
#include <dlfcn.h> |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
35 |
#endif |
2 | 36 |
#include <math.h> |
37 |
#include "ft2build.h" |
|
38 |
#include FT_FREETYPE_H |
|
39 |
#include FT_GLYPH_H |
|
40 |
#include FT_BBOX_H |
|
41 |
#include FT_SIZES_H |
|
42 |
#include FT_OUTLINE_H |
|
43 |
#include FT_SYNTHESIS_H |
|
52258
02e4b0ab0f97
8212071: Need to set the FreeType LCD Filter to reduce fringing.
prr
parents:
51932
diff
changeset
|
44 |
#include FT_LCD_FILTER_H |
55369
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
45 |
#include FT_MODULE_H |
2 | 46 |
|
47 |
#include "fontscaler.h" |
|
48 |
||
49 |
#define ftFixed1 (FT_Fixed) (1 << 16) |
|
50 |
#define FloatToFTFixed(f) (FT_Fixed)((f) * (float)(ftFixed1)) |
|
51 |
#define FTFixedToFloat(x) ((x) / (float)(ftFixed1)) |
|
52 |
#define FT26Dot6ToFloat(x) ((x) / ((float) (1<<6))) |
|
50346
9e530b150333
8203485: [freetype] text rotated on 180 degrees is too narrow
bae
parents:
47216
diff
changeset
|
53 |
#define FT26Dot6ToInt(x) (((int)(x)) >> 6) |
2 | 54 |
|
55 |
typedef struct { |
|
56 |
/* Important note: |
|
57 |
JNI forbids sharing same env between different threads. |
|
58 |
We are safe, because pointer is overwritten every time we get into |
|
59 |
JNI call (see setupFTContext). |
|
60 |
||
61 |
Pointer is used by font data reading callbacks |
|
62 |
such as ReadTTFontFileFunc. |
|
63 |
||
64 |
NB: We may consider switching to JNI_GetEnv. */ |
|
65 |
JNIEnv* env; |
|
66 |
FT_Library library; |
|
67 |
FT_Face face; |
|
33250
047311011afe
8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents:
30494
diff
changeset
|
68 |
FT_Stream faceStream; |
2 | 69 |
jobject font2D; |
70 |
jobject directBuffer; |
|
71 |
||
72 |
unsigned char* fontData; |
|
73 |
unsigned fontDataOffset; |
|
74 |
unsigned fontDataLength; |
|
75 |
unsigned fileSize; |
|
76 |
} FTScalerInfo; |
|
77 |
||
78 |
typedef struct FTScalerContext { |
|
79 |
FT_Matrix transform; /* glyph transform, including device transform */ |
|
80 |
jboolean useSbits; /* sbit usage enabled? */ |
|
81 |
jint aaType; /* antialiasing mode (off/on/grey/lcd) */ |
|
82 |
jint fmType; /* fractional metrics - on/off */ |
|
83 |
jboolean doBold; /* perform algorithmic bolding? */ |
|
84 |
jboolean doItalize; /* perform algorithmic italicizing? */ |
|
85 |
int renderFlags; /* configuration specific to particular engine */ |
|
86 |
int pathType; |
|
87 |
int ptsz; /* size in points */ |
|
88 |
} FTScalerContext; |
|
89 |
||
90 |
#ifdef DEBUG |
|
91 |
/* These are referenced in the freetype sources if DEBUG macro is defined. |
|
92 |
To simplify work with debuging version of freetype we define |
|
93 |
them here. */ |
|
94 |
int z_verbose; |
|
95 |
void z_error(char *s) {} |
|
96 |
#endif |
|
97 |
||
98 |
/**************** Error handling utilities *****************/ |
|
99 |
||
100 |
static jmethodID invalidateScalerMID; |
|
101 |
||
102 |
JNIEXPORT void JNICALL |
|
103 |
Java_sun_font_FreetypeFontScaler_initIDs( |
|
104 |
JNIEnv *env, jobject scaler, jclass FFSClass) { |
|
105 |
invalidateScalerMID = |
|
106 |
(*env)->GetMethodID(env, FFSClass, "invalidateScaler", "()V"); |
|
107 |
} |
|
108 |
||
109 |
static void freeNativeResources(JNIEnv *env, FTScalerInfo* scalerInfo) { |
|
4243 | 110 |
|
2 | 111 |
if (scalerInfo == NULL) |
112 |
return; |
|
113 |
||
33250
047311011afe
8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents:
30494
diff
changeset
|
114 |
// FT_Done_Face always closes the stream, but only frees the memory |
047311011afe
8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents:
30494
diff
changeset
|
115 |
// of the data structure if it was internally allocated by FT. |
047311011afe
8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents:
30494
diff
changeset
|
116 |
// We hold on to a pointer to the stream structure if we provide it |
047311011afe
8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents:
30494
diff
changeset
|
117 |
// ourselves, so that we can free it here. |
2 | 118 |
FT_Done_Face(scalerInfo->face); |
119 |
FT_Done_FreeType(scalerInfo->library); |
|
120 |
||
121 |
if (scalerInfo->directBuffer != NULL) { |
|
122 |
(*env)->DeleteGlobalRef(env, scalerInfo->directBuffer); |
|
123 |
} |
|
124 |
||
125 |
if (scalerInfo->fontData != NULL) { |
|
126 |
free(scalerInfo->fontData); |
|
127 |
} |
|
128 |
||
33250
047311011afe
8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents:
30494
diff
changeset
|
129 |
if (scalerInfo->faceStream != NULL) { |
047311011afe
8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents:
30494
diff
changeset
|
130 |
free(scalerInfo->faceStream); |
047311011afe
8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents:
30494
diff
changeset
|
131 |
} |
2 | 132 |
free(scalerInfo); |
133 |
} |
|
134 |
||
135 |
/* invalidates state of java scaler object */ |
|
136 |
static void invalidateJavaScaler(JNIEnv *env, |
|
137 |
jobject scaler, |
|
138 |
FTScalerInfo* scalerInfo) { |
|
139 |
freeNativeResources(env, scalerInfo); |
|
140 |
(*env)->CallVoidMethod(env, scaler, invalidateScalerMID); |
|
141 |
} |
|
142 |
||
143 |
/******************* I/O handlers ***************************/ |
|
144 |
||
145 |
#define FILEDATACACHESIZE 1024 |
|
146 |
||
147 |
static unsigned long ReadTTFontFileFunc(FT_Stream stream, |
|
148 |
unsigned long offset, |
|
149 |
unsigned char* destBuffer, |
|
150 |
unsigned long numBytes) |
|
151 |
{ |
|
152 |
FTScalerInfo *scalerInfo = (FTScalerInfo *) stream->pathname.pointer; |
|
153 |
JNIEnv* env = scalerInfo->env; |
|
154 |
jobject bBuffer; |
|
155 |
int bread = 0; |
|
156 |
||
58613 | 157 |
/* A call with numBytes == 0 is a seek. It should return 0 if the |
158 |
* seek position is within the file and non-zero otherwise. |
|
159 |
* For all other cases, ie numBytes !=0, return the number of bytes |
|
160 |
* actually read. This applies to truncated reads and also failed reads. |
|
161 |
*/ |
|
162 |
||
163 |
if (numBytes == 0) { |
|
58634
da71aa13f86b
8227662: freetype seeks to index at the end of the font data
prr
parents:
58629
diff
changeset
|
164 |
if (offset > scalerInfo->fileSize) { |
58613 | 165 |
return -1; |
166 |
} else { |
|
167 |
return 0; |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
if (offset + numBytes < offset) { |
|
172 |
return 0; // ft should not do this, but just in case. |
|
173 |
} |
|
174 |
||
175 |
if (offset >= scalerInfo->fileSize) { |
|
176 |
return 0; |
|
177 |
} |
|
178 |
||
179 |
if (offset + numBytes > scalerInfo->fileSize) { |
|
180 |
numBytes = scalerInfo->fileSize - offset; |
|
181 |
} |
|
2 | 182 |
|
183 |
/* Large reads will bypass the cache and data copying */ |
|
184 |
if (numBytes > FILEDATACACHESIZE) { |
|
185 |
bBuffer = (*env)->NewDirectByteBuffer(env, destBuffer, numBytes); |
|
186 |
if (bBuffer != NULL) { |
|
25528 | 187 |
bread = (*env)->CallIntMethod(env, |
188 |
scalerInfo->font2D, |
|
189 |
sunFontIDs.ttReadBlockMID, |
|
190 |
bBuffer, offset, numBytes); |
|
58613 | 191 |
if (bread < 0) { |
192 |
return 0; |
|
193 |
} else { |
|
194 |
return bread; |
|
195 |
} |
|
2 | 196 |
} else { |
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
27053
diff
changeset
|
197 |
/* We probably hit bug 4845371. For reasons that |
2 | 198 |
* are currently unclear, the call stacks after the initial |
199 |
* createScaler call that read large amounts of data seem to |
|
200 |
* be OK and can create the byte buffer above, but this code |
|
201 |
* is here just in case. |
|
202 |
* 4845371 is fixed now so I don't expect this code path to |
|
203 |
* ever get called but its harmless to leave it here on the |
|
204 |
* small chance its needed. |
|
205 |
*/ |
|
206 |
jbyteArray byteArray = (jbyteArray) |
|
207 |
(*env)->CallObjectMethod(env, scalerInfo->font2D, |
|
208 |
sunFontIDs.ttReadBytesMID, |
|
209 |
offset, numBytes); |
|
58613 | 210 |
/* If there's an OutofMemoryError then byteArray will be null */ |
211 |
if (byteArray == NULL) { |
|
212 |
return 0; |
|
213 |
} else { |
|
214 |
jsize len = (*env)->GetArrayLength(env, byteArray); |
|
215 |
if (len < numBytes) { |
|
216 |
numBytes = len; // don't get more bytes than there are .. |
|
217 |
} |
|
218 |
(*env)->GetByteArrayRegion(env, byteArray, |
|
219 |
0, numBytes, (jbyte*)destBuffer); |
|
220 |
return numBytes; |
|
221 |
} |
|
2 | 222 |
} |
223 |
} /* Do we have a cache hit? */ |
|
224 |
else if (scalerInfo->fontDataOffset <= offset && |
|
225 |
scalerInfo->fontDataOffset + scalerInfo->fontDataLength >= |
|
226 |
offset + numBytes) |
|
227 |
{ |
|
228 |
unsigned cacheOffset = offset - scalerInfo->fontDataOffset; |
|
229 |
||
230 |
memcpy(destBuffer, scalerInfo->fontData+(size_t)cacheOffset, numBytes); |
|
231 |
return numBytes; |
|
232 |
} else { |
|
233 |
/* Must fill the cache */ |
|
234 |
scalerInfo->fontDataOffset = offset; |
|
235 |
scalerInfo->fontDataLength = |
|
236 |
(offset + FILEDATACACHESIZE > scalerInfo->fileSize) ? |
|
237 |
scalerInfo->fileSize - offset : FILEDATACACHESIZE; |
|
238 |
bBuffer = scalerInfo->directBuffer; |
|
25528 | 239 |
bread = (*env)->CallIntMethod(env, scalerInfo->font2D, |
240 |
sunFontIDs.ttReadBlockMID, |
|
241 |
bBuffer, offset, |
|
242 |
scalerInfo->fontDataLength); |
|
58613 | 243 |
if (bread <= 0) { |
244 |
return 0; |
|
245 |
} else if (bread < numBytes) { |
|
246 |
numBytes = bread; |
|
247 |
} |
|
2 | 248 |
memcpy(destBuffer, scalerInfo->fontData, numBytes); |
249 |
return numBytes; |
|
250 |
} |
|
251 |
} |
|
252 |
||
55369
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
253 |
typedef FT_Error (*FT_Prop_Set_Func)(FT_Library library, |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
254 |
const FT_String* module_name, |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
255 |
const FT_String* property_name, |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
256 |
const void* value ); |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
257 |
|
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
258 |
/** |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
259 |
* Prefer the older v35 freetype byte code interpreter. |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
260 |
*/ |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
261 |
static void setInterpreterVersion(FT_Library library) { |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
262 |
|
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
263 |
char* props = getenv("FREETYPE_PROPERTIES"); |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
264 |
int version = 35; |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
265 |
const char* module = "truetype"; |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
266 |
const char* property = "interpreter-version"; |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
267 |
|
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
268 |
/* If some one is setting this, don't override it */ |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
269 |
if (props != NULL && strstr(property, props)) { |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
270 |
return; |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
271 |
} |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
272 |
/* |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
273 |
* FT_Property_Set was introduced in 2.4.11. |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
274 |
* Some older supported Linux OSes may not include it so look |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
275 |
* this up dynamically. |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
276 |
* And if its not available it doesn't matter, since the reason |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
277 |
* we need it dates from 2.7. |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
278 |
* On Windows & Mac the library is always bundled so it is safe |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
279 |
* to use directly in those cases. |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
280 |
*/ |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
281 |
#if defined(_WIN32) || defined(__APPLE__) |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
282 |
FT_Property_Set(library, module, property, (void*)(&version)); |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
283 |
#else |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
284 |
void *lib = dlopen("libfreetype.so", RTLD_LOCAL|RTLD_LAZY); |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
285 |
if (lib == NULL) { |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
286 |
lib = dlopen("libfreetype.so.6", RTLD_LOCAL|RTLD_LAZY); |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
287 |
if (lib == NULL) { |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
288 |
return; |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
289 |
} |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
290 |
} |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
291 |
FT_Prop_Set_Func func = (FT_Prop_Set_Func)dlsym(lib, "FT_Property_Set"); |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
292 |
if (func != NULL) { |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
293 |
func(library, module, property, (void*)(&version)); |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
294 |
} |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
295 |
dlclose(lib); |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
296 |
#endif |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
297 |
} |
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
298 |
|
2 | 299 |
/* |
300 |
* Class: sun_font_FreetypeFontScaler |
|
301 |
* Method: initNativeScaler |
|
302 |
* Signature: (Lsun/font/Font2D;IIZI)J |
|
303 |
*/ |
|
304 |
JNIEXPORT jlong JNICALL |
|
305 |
Java_sun_font_FreetypeFontScaler_initNativeScaler( |
|
306 |
JNIEnv *env, jobject scaler, jobject font2D, jint type, |
|
307 |
jint indexInCollection, jboolean supportsCJK, jint filesize) { |
|
308 |
FTScalerInfo* scalerInfo = NULL; |
|
309 |
FT_Open_Args ft_open_args; |
|
310 |
int error; |
|
311 |
jobject bBuffer; |
|
312 |
scalerInfo = (FTScalerInfo*) calloc(1, sizeof(FTScalerInfo)); |
|
313 |
||
314 |
if (scalerInfo == NULL) |
|
315 |
return 0; |
|
316 |
||
317 |
scalerInfo->env = env; |
|
318 |
scalerInfo->font2D = font2D; |
|
319 |
scalerInfo->fontDataOffset = 0; |
|
320 |
scalerInfo->fontDataLength = 0; |
|
321 |
scalerInfo->fileSize = filesize; |
|
322 |
||
323 |
/* |
|
324 |
We can consider sharing freetype library between different |
|
325 |
scalers. However, Freetype docs suggest to use different libraries |
|
326 |
for different threads. Also, our architecture implies that single |
|
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
27053
diff
changeset
|
327 |
FontScaler object is shared for different sizes/transforms/styles |
2 | 328 |
of the same font. |
329 |
||
330 |
On other hand these methods can not be concurrently executed |
|
331 |
becaused they are "synchronized" in java. |
|
332 |
*/ |
|
333 |
error = FT_Init_FreeType(&scalerInfo->library); |
|
334 |
if (error) { |
|
335 |
free(scalerInfo); |
|
336 |
return 0; |
|
337 |
} |
|
55369
18629738b64b
8217731: Font rendering and glyph spacing changed from jdk-8 to jdk-11
prr
parents:
54590
diff
changeset
|
338 |
setInterpreterVersion(scalerInfo->library); |
2 | 339 |
|
340 |
#define TYPE1_FROM_JAVA 2 |
|
341 |
||
342 |
error = 1; /* triggers memory freeing unless we clear it */ |
|
343 |
if (type == TYPE1_FROM_JAVA) { /* TYPE1 */ |
|
344 |
scalerInfo->fontData = (unsigned char*) malloc(filesize); |
|
345 |
scalerInfo->directBuffer = NULL; |
|
346 |
scalerInfo->fontDataLength = filesize; |
|
347 |
||
348 |
if (scalerInfo->fontData != NULL) { |
|
349 |
bBuffer = (*env)->NewDirectByteBuffer(env, |
|
350 |
scalerInfo->fontData, |
|
351 |
scalerInfo->fontDataLength); |
|
352 |
if (bBuffer != NULL) { |
|
34786 | 353 |
(*env)->CallVoidMethod(env, font2D, |
2 | 354 |
sunFontIDs.readFileMID, bBuffer); |
355 |
||
356 |
error = FT_New_Memory_Face(scalerInfo->library, |
|
357 |
scalerInfo->fontData, |
|
358 |
scalerInfo->fontDataLength, |
|
359 |
indexInCollection, |
|
360 |
&scalerInfo->face); |
|
361 |
} |
|
362 |
} |
|
363 |
} else { /* Truetype */ |
|
364 |
scalerInfo->fontData = (unsigned char*) malloc(FILEDATACACHESIZE); |
|
365 |
||
20424
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
366 |
if (scalerInfo->fontData != NULL) { |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
367 |
FT_Stream ftstream = (FT_Stream) calloc(1, sizeof(FT_StreamRec)); |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
368 |
if (ftstream != NULL) { |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
369 |
scalerInfo->directBuffer = (*env)->NewDirectByteBuffer(env, |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
370 |
scalerInfo->fontData, |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
371 |
FILEDATACACHESIZE); |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
372 |
if (scalerInfo->directBuffer != NULL) { |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
373 |
scalerInfo->directBuffer = (*env)->NewGlobalRef(env, |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
374 |
scalerInfo->directBuffer); |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
375 |
ftstream->base = NULL; |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
376 |
ftstream->size = filesize; |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
377 |
ftstream->pos = 0; |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
378 |
ftstream->read = (FT_Stream_IoFunc) ReadTTFontFileFunc; |
30494
a245f4430b4c
8078654: CloseTTFontFileFunc callback should be removed
adinn
parents:
28059
diff
changeset
|
379 |
ftstream->close = NULL; |
20424
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
380 |
ftstream->pathname.pointer = (void *) scalerInfo; |
2 | 381 |
|
20424
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
382 |
memset(&ft_open_args, 0, sizeof(FT_Open_Args)); |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
383 |
ft_open_args.flags = FT_OPEN_STREAM; |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
384 |
ft_open_args.stream = ftstream; |
2 | 385 |
|
20424
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
386 |
error = FT_Open_Face(scalerInfo->library, |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
387 |
&ft_open_args, |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
388 |
indexInCollection, |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
389 |
&scalerInfo->face); |
33250
047311011afe
8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents:
30494
diff
changeset
|
390 |
if (!error) { |
047311011afe
8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents:
30494
diff
changeset
|
391 |
scalerInfo->faceStream = ftstream; |
047311011afe
8132985: Crash in freetypescaler.c due to double free
psadhukhan
parents:
30494
diff
changeset
|
392 |
} |
20424
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
393 |
} |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
394 |
if (error || scalerInfo->directBuffer == NULL) { |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
395 |
free(ftstream); |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
396 |
} |
eace1945b974
8025664: [parfait] warnings from b62 for jdk.src.share.native.sun.font
jchen
parents:
20414
diff
changeset
|
397 |
} |
2 | 398 |
} |
399 |
} |
|
400 |
||
401 |
if (error) { |
|
402 |
FT_Done_FreeType(scalerInfo->library); |
|
403 |
if (scalerInfo->directBuffer != NULL) { |
|
404 |
(*env)->DeleteGlobalRef(env, scalerInfo->directBuffer); |
|
405 |
} |
|
406 |
if (scalerInfo->fontData != NULL) |
|
407 |
free(scalerInfo->fontData); |
|
408 |
free(scalerInfo); |
|
409 |
return 0; |
|
410 |
} |
|
411 |
||
412 |
return ptr_to_jlong(scalerInfo); |
|
413 |
} |
|
414 |
||
415 |
static double euclidianDistance(double a, double b) { |
|
416 |
if (a < 0) a=-a; |
|
417 |
if (b < 0) b=-b; |
|
418 |
||
419 |
if (a == 0) return b; |
|
420 |
if (b == 0) return a; |
|
421 |
||
422 |
return sqrt(a*a+b*b); |
|
423 |
} |
|
424 |
||
425 |
JNIEXPORT jlong JNICALL |
|
426 |
Java_sun_font_FreetypeFontScaler_createScalerContextNative( |
|
427 |
JNIEnv *env, jobject scaler, jlong pScaler, jdoubleArray matrix, |
|
9761
a9315b1a26e9
7049874: OpenJDK Build breakage fix: freetypescaler.c needs to match updated signature
prr
parents:
7668
diff
changeset
|
428 |
jint aa, jint fm, jfloat boldness, jfloat italic) { |
2 | 429 |
double dmat[4], ptsz; |
430 |
FTScalerContext *context = |
|
431 |
(FTScalerContext*) calloc(1, sizeof(FTScalerContext)); |
|
432 |
FTScalerInfo *scalerInfo = |
|
433 |
(FTScalerInfo*) jlong_to_ptr(pScaler); |
|
434 |
||
435 |
if (context == NULL) { |
|
436 |
invalidateJavaScaler(env, scaler, NULL); |
|
437 |
return (jlong) 0; |
|
438 |
} |
|
439 |
(*env)->GetDoubleArrayRegion(env, matrix, 0, 4, dmat); |
|
440 |
ptsz = euclidianDistance(dmat[2], dmat[3]); //i.e. y-size |
|
441 |
if (ptsz < 1.0) { |
|
442 |
//text can not be smaller than 1 point |
|
443 |
ptsz = 1.0; |
|
444 |
} |
|
554
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
445 |
context->ptsz = (int)(ptsz * 64); |
2 | 446 |
context->transform.xx = FloatToFTFixed((float)dmat[0]/ptsz); |
447 |
context->transform.yx = -FloatToFTFixed((float)dmat[1]/ptsz); |
|
448 |
context->transform.xy = -FloatToFTFixed((float)dmat[2]/ptsz); |
|
449 |
context->transform.yy = FloatToFTFixed((float)dmat[3]/ptsz); |
|
450 |
context->aaType = aa; |
|
451 |
context->fmType = fm; |
|
452 |
||
453 |
/* If using algorithmic styling, the base values are |
|
454 |
* boldness = 1.0, italic = 0.0. |
|
455 |
*/ |
|
456 |
context->doBold = (boldness != 1.0); |
|
457 |
context->doItalize = (italic != 0); |
|
458 |
||
50658
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
459 |
/* freetype is very keen to use embedded bitmaps, even if it knows |
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
460 |
* there is a rotation or you asked for antialiasing. |
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
461 |
* In the rendering path we will check useSBits and disable |
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
462 |
* bitmaps unless it is set. And here we set it only if none |
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
463 |
* of the conditions invalidate using it. |
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
464 |
* Note that we allow embedded bitmaps for the LCD case. |
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
465 |
*/ |
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
466 |
if ((aa != TEXT_AA_ON) && (fm != TEXT_FM_ON) && |
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
467 |
!context->doBold && !context->doItalize && |
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
468 |
(context->transform.yx == 0) && (context->transform.xy == 0)) |
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
469 |
{ |
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
470 |
context->useSbits = 1; |
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
471 |
} |
2 | 472 |
return ptr_to_jlong(context); |
473 |
} |
|
474 |
||
475 |
static int setupFTContext(JNIEnv *env, |
|
476 |
jobject font2D, |
|
477 |
FTScalerInfo *scalerInfo, |
|
478 |
FTScalerContext *context) { |
|
479 |
int errCode = 0; |
|
480 |
||
481 |
scalerInfo->env = env; |
|
482 |
scalerInfo->font2D = font2D; |
|
483 |
||
2369
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
484 |
if (context != NULL) { |
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
485 |
FT_Set_Transform(scalerInfo->face, &context->transform, NULL); |
2 | 486 |
|
2369
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
487 |
errCode = FT_Set_Char_Size(scalerInfo->face, 0, context->ptsz, 72, 72); |
2 | 488 |
|
2369
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
489 |
if (errCode == 0) { |
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
490 |
errCode = FT_Activate_Size(scalerInfo->face->size); |
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
491 |
} |
52258
02e4b0ab0f97
8212071: Need to set the FreeType LCD Filter to reduce fringing.
prr
parents:
51932
diff
changeset
|
492 |
|
02e4b0ab0f97
8212071: Need to set the FreeType LCD Filter to reduce fringing.
prr
parents:
51932
diff
changeset
|
493 |
FT_Library_SetLcdFilter(scalerInfo->library, FT_LCD_FILTER_DEFAULT); |
2 | 494 |
} |
495 |
||
496 |
return errCode; |
|
497 |
} |
|
498 |
||
54239
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
499 |
/* ftsynth.c uses (0x10000, 0x0366A, 0x0, 0x10000) matrix to get oblique |
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
500 |
outline. Therefore x coordinate will change by 0x0366A*y. |
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
501 |
Note that y coordinate does not change. These values are based on |
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
502 |
libfreetype version 2.9.1. */ |
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
503 |
#define OBLIQUE_MODIFIER(y) (context->doItalize ? ((y)*0x366A/0x10000) : 0) |
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
504 |
|
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
505 |
/* FT_GlyphSlot_Embolden (ftsynth.c) uses FT_MulFix(units_per_EM, y_scale) / 24 |
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
506 |
* strength value when glyph format is FT_GLYPH_FORMAT_OUTLINE. This value has |
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
507 |
* been taken from libfreetype version 2.6 and remain valid at least up to |
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
508 |
* 2.9.1. */ |
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
509 |
#define BOLD_MODIFIER(units_per_EM, y_scale) \ |
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
510 |
(context->doBold ? FT_MulFix(units_per_EM, y_scale) / 24 : 0) |
2 | 511 |
|
512 |
/* |
|
513 |
* Class: sun_font_FreetypeFontScaler |
|
514 |
* Method: getFontMetricsNative |
|
515 |
* Signature: (Lsun/font/Font2D;J)Lsun/font/StrikeMetrics; |
|
516 |
*/ |
|
517 |
JNIEXPORT jobject JNICALL |
|
518 |
Java_sun_font_FreetypeFontScaler_getFontMetricsNative( |
|
519 |
JNIEnv *env, jobject scaler, jobject font2D, |
|
520 |
jlong pScalerContext, jlong pScaler) { |
|
521 |
||
522 |
jobject metrics; |
|
523 |
jfloat ax, ay, dx, dy, bx, by, lx, ly, mx, my; |
|
524 |
jfloat f0 = 0.0; |
|
525 |
FTScalerContext *context = |
|
526 |
(FTScalerContext*) jlong_to_ptr(pScalerContext); |
|
527 |
FTScalerInfo *scalerInfo = |
|
528 |
(FTScalerInfo*) jlong_to_ptr(pScaler); |
|
529 |
||
530 |
int errCode; |
|
531 |
||
532 |
if (isNullScalerContext(context) || scalerInfo == NULL) { |
|
533 |
return (*env)->NewObject(env, |
|
534 |
sunFontIDs.strikeMetricsClass, |
|
535 |
sunFontIDs.strikeMetricsCtr, |
|
536 |
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0); |
|
537 |
} |
|
538 |
||
539 |
errCode = setupFTContext(env, font2D, scalerInfo, context); |
|
540 |
||
541 |
if (errCode) { |
|
542 |
metrics = (*env)->NewObject(env, |
|
543 |
sunFontIDs.strikeMetricsClass, |
|
544 |
sunFontIDs.strikeMetricsCtr, |
|
545 |
f0, f0, f0, f0, f0, f0, f0, f0, f0, f0); |
|
546 |
invalidateJavaScaler(env, scaler, scalerInfo); |
|
547 |
return metrics; |
|
548 |
} |
|
549 |
||
550 |
/* This is ugly and has to be reworked. |
|
551 |
Freetype provide means to add style to glyph but |
|
552 |
it seems there is no way to adjust metrics accordingly. |
|
553 |
||
554 |
So, we have to do adust them explicitly and stay consistent with what |
|
555 |
freetype does to outlines. */ |
|
556 |
||
557 |
||
558 |
/**** Note: only some metrics are affected by styling ***/ |
|
559 |
||
27053
b8def728da69
8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents:
26617
diff
changeset
|
560 |
/* See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=657854 */ |
b8def728da69
8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents:
26617
diff
changeset
|
561 |
#define FT_MulFixFloatShift6(a, b) (((float) (a)) * ((float) (b)) / 65536.0 / 64.0) |
b8def728da69
8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents:
26617
diff
changeset
|
562 |
|
52838
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
563 |
#define contextAwareMetricsX(x, y) \ |
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
564 |
(FTFixedToFloat(context->transform.xx) * (x) - \ |
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
565 |
FTFixedToFloat(context->transform.xy) * (y)) |
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
566 |
|
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
567 |
#define contextAwareMetricsY(x, y) \ |
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
568 |
(-FTFixedToFloat(context->transform.yx) * (x) + \ |
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
569 |
FTFixedToFloat(context->transform.yy) * (y)) |
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
570 |
|
27053
b8def728da69
8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents:
26617
diff
changeset
|
571 |
/* |
b8def728da69
8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents:
26617
diff
changeset
|
572 |
* See FreeType source code: src/base/ftobjs.c ft_recompute_scaled_metrics() |
b8def728da69
8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents:
26617
diff
changeset
|
573 |
* http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=1659 |
b8def728da69
8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents:
26617
diff
changeset
|
574 |
*/ |
2 | 575 |
/* ascent */ |
576 |
ax = 0; |
|
27053
b8def728da69
8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents:
26617
diff
changeset
|
577 |
ay = -(jfloat) (FT_MulFixFloatShift6( |
b8def728da69
8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents:
26617
diff
changeset
|
578 |
((jlong) scalerInfo->face->ascender), |
5754
ef19a58b3c42
6961732: FontMetrics.getLeading() may be negative in freetype-based OpenJDK builds.
neugens
parents:
5580
diff
changeset
|
579 |
(jlong) scalerInfo->face->size->metrics.y_scale)); |
2 | 580 |
/* descent */ |
581 |
dx = 0; |
|
27053
b8def728da69
8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents:
26617
diff
changeset
|
582 |
dy = -(jfloat) (FT_MulFixFloatShift6( |
b8def728da69
8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents:
26617
diff
changeset
|
583 |
((jlong) scalerInfo->face->descender), |
5754
ef19a58b3c42
6961732: FontMetrics.getLeading() may be negative in freetype-based OpenJDK builds.
neugens
parents:
5580
diff
changeset
|
584 |
(jlong) scalerInfo->face->size->metrics.y_scale)); |
2 | 585 |
/* baseline */ |
586 |
bx = by = 0; |
|
587 |
||
588 |
/* leading */ |
|
589 |
lx = 0; |
|
27053
b8def728da69
8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents:
26617
diff
changeset
|
590 |
ly = (jfloat) (FT_MulFixFloatShift6( |
b8def728da69
8017773: OpenJDK7 returns incorrect TrueType font metrics
ysuenaga
parents:
26617
diff
changeset
|
591 |
(jlong) scalerInfo->face->height, |
5754
ef19a58b3c42
6961732: FontMetrics.getLeading() may be negative in freetype-based OpenJDK builds.
neugens
parents:
5580
diff
changeset
|
592 |
(jlong) scalerInfo->face->size->metrics.y_scale)) |
ef19a58b3c42
6961732: FontMetrics.getLeading() may be negative in freetype-based OpenJDK builds.
neugens
parents:
5580
diff
changeset
|
593 |
+ ay - dy; |
2 | 594 |
/* max advance */ |
595 |
mx = (jfloat) FT26Dot6ToFloat( |
|
596 |
scalerInfo->face->size->metrics.max_advance + |
|
54239
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
597 |
OBLIQUE_MODIFIER(scalerInfo->face->size->metrics.height) + |
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
598 |
BOLD_MODIFIER(scalerInfo->face->units_per_EM, |
0804f29e8be7
8218854: FontMetrics.getMaxAdvance may be less than the maximum FontMetrics.charWidth
mbalao
parents:
53454
diff
changeset
|
599 |
scalerInfo->face->size->metrics.y_scale)); |
2 | 600 |
my = 0; |
601 |
||
602 |
metrics = (*env)->NewObject(env, |
|
52838
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
603 |
sunFontIDs.strikeMetricsClass, |
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
604 |
sunFontIDs.strikeMetricsCtr, |
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
605 |
contextAwareMetricsX(ax, ay), contextAwareMetricsY(ax, ay), |
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
606 |
contextAwareMetricsX(dx, dy), contextAwareMetricsY(dx, dy), |
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
607 |
bx, by, |
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
608 |
contextAwareMetricsX(lx, ly), contextAwareMetricsY(lx, ly), |
df92f1126c58
8139178: Wrong fontMetrics when printing in Landscape (OpenJDK)
bae
parents:
52258
diff
changeset
|
609 |
contextAwareMetricsX(mx, my), contextAwareMetricsY(mx, my)); |
2 | 610 |
|
611 |
return metrics; |
|
612 |
} |
|
613 |
||
614 |
/* |
|
615 |
* Class: sun_font_FreetypeFontScaler |
|
616 |
* Method: getGlyphAdvanceNative |
|
617 |
* Signature: (Lsun/font/Font2D;JI)F |
|
618 |
*/ |
|
619 |
JNIEXPORT jfloat JNICALL |
|
620 |
Java_sun_font_FreetypeFontScaler_getGlyphAdvanceNative( |
|
621 |
JNIEnv *env, jobject scaler, jobject font2D, |
|
622 |
jlong pScalerContext, jlong pScaler, jint glyphCode) { |
|
623 |
||
624 |
/* This method is rarely used because requests for metrics are usually |
|
625 |
coupled with request for bitmap and to large extend work can be reused |
|
626 |
(to find out metrics we need to hint glyph). |
|
627 |
So, we typically go through getGlyphImage code path. |
|
628 |
||
629 |
For initial freetype implementation we delegate |
|
630 |
all work to getGlyphImage but drop result image. |
|
631 |
This is waste of work related to scan conversion and conversion from |
|
632 |
freetype format to our format but for now this seems to be ok. |
|
633 |
||
634 |
NB: investigate performance benefits of refactoring code |
|
635 |
to avoid unnecesary work with bitmaps. */ |
|
636 |
||
637 |
GlyphInfo *info; |
|
58615 | 638 |
jfloat advance = 0.0f; |
2 | 639 |
jlong image; |
640 |
||
641 |
image = Java_sun_font_FreetypeFontScaler_getGlyphImageNative( |
|
642 |
env, scaler, font2D, pScalerContext, pScaler, glyphCode); |
|
643 |
info = (GlyphInfo*) jlong_to_ptr(image); |
|
644 |
||
58615 | 645 |
if (info != NULL) { |
646 |
advance = info->advanceX; |
|
647 |
free(info); |
|
648 |
} |
|
2 | 649 |
|
650 |
return advance; |
|
651 |
} |
|
652 |
||
653 |
/* |
|
654 |
* Class: sun_font_FreetypeFontScaler |
|
655 |
* Method: getGlyphMetricsNative |
|
656 |
* Signature: (Lsun/font/Font2D;JILjava/awt/geom/Point2D/Float;)V |
|
657 |
*/ |
|
658 |
JNIEXPORT void JNICALL |
|
659 |
Java_sun_font_FreetypeFontScaler_getGlyphMetricsNative( |
|
660 |
JNIEnv *env, jobject scaler, jobject font2D, jlong pScalerContext, |
|
661 |
jlong pScaler, jint glyphCode, jobject metrics) { |
|
662 |
||
663 |
/* As initial implementation we delegate all work to getGlyphImage |
|
664 |
but drop result image. This is clearly waste of resorces. |
|
665 |
||
666 |
TODO: investigate performance benefits of refactoring code |
|
667 |
by avoiding bitmap generation and conversion from FT |
|
668 |
bitmap format. */ |
|
669 |
GlyphInfo *info; |
|
670 |
||
671 |
jlong image = Java_sun_font_FreetypeFontScaler_getGlyphImageNative( |
|
672 |
env, scaler, font2D, |
|
673 |
pScalerContext, pScaler, glyphCode); |
|
674 |
info = (GlyphInfo*) jlong_to_ptr(image); |
|
675 |
||
676 |
(*env)->SetFloatField(env, metrics, sunFontIDs.xFID, info->advanceX); |
|
677 |
(*env)->SetFloatField(env, metrics, sunFontIDs.yFID, info->advanceY); |
|
678 |
||
679 |
free(info); |
|
680 |
} |
|
681 |
||
682 |
||
683 |
static GlyphInfo* getNullGlyphImage() { |
|
684 |
GlyphInfo *glyphInfo = (GlyphInfo*) calloc(1, sizeof(GlyphInfo)); |
|
685 |
return glyphInfo; |
|
686 |
} |
|
687 |
||
688 |
static void CopyBW2Grey8(const void* srcImage, int srcRowBytes, |
|
689 |
void* dstImage, int dstRowBytes, |
|
690 |
int width, int height) { |
|
691 |
const UInt8* srcRow = (UInt8*)srcImage; |
|
692 |
UInt8* dstRow = (UInt8*)dstImage; |
|
693 |
int wholeByteCount = width >> 3; |
|
694 |
int remainingBitsCount = width & 7; |
|
695 |
int i, j; |
|
696 |
||
697 |
while (height--) { |
|
698 |
const UInt8* src8 = srcRow; |
|
699 |
UInt8* dstByte = dstRow; |
|
700 |
unsigned srcValue; |
|
701 |
||
702 |
srcRow += srcRowBytes; |
|
703 |
dstRow += dstRowBytes; |
|
704 |
||
705 |
for (i = 0; i < wholeByteCount; i++) { |
|
706 |
srcValue = *src8++; |
|
707 |
for (j = 0; j < 8; j++) { |
|
708 |
*dstByte++ = (srcValue & 0x80) ? 0xFF : 0; |
|
709 |
srcValue <<= 1; |
|
710 |
} |
|
711 |
} |
|
712 |
if (remainingBitsCount) { |
|
713 |
srcValue = *src8; |
|
714 |
for (j = 0; j < remainingBitsCount; j++) { |
|
715 |
*dstByte++ = (srcValue & 0x80) ? 0xFF : 0; |
|
716 |
srcValue <<= 1; |
|
717 |
} |
|
718 |
} |
|
719 |
} |
|
720 |
} |
|
721 |
||
722 |
#define Grey4ToAlpha255(value) (((value) << 4) + ((value) >> 3)) |
|
723 |
||
724 |
static void CopyGrey4ToGrey8(const void* srcImage, int srcRowBytes, |
|
725 |
void* dstImage, int dstRowBytes, int width, int height) { |
|
726 |
const UInt8* srcRow = (UInt8*) srcImage; |
|
727 |
UInt8* dstRow = (UInt8*) dstImage; |
|
728 |
int i; |
|
729 |
||
730 |
while (height--) { |
|
731 |
const UInt8* src8 = srcRow; |
|
732 |
UInt8* dstByte = dstRow; |
|
733 |
unsigned srcValue; |
|
734 |
||
735 |
srcRow += srcRowBytes; |
|
736 |
dstRow += dstRowBytes; |
|
737 |
||
738 |
for (i = 0; i < width; i++) { |
|
739 |
srcValue = *src8++; |
|
740 |
*dstByte++ = Grey4ToAlpha255(srcValue & 0x0f); |
|
741 |
*dstByte++ = Grey4ToAlpha255(srcValue >> 4); |
|
742 |
} |
|
743 |
} |
|
744 |
} |
|
745 |
||
746 |
/* We need it because FT rows are often padded to 4 byte boundaries |
|
747 |
and our internal format is not padded */ |
|
748 |
static void CopyFTSubpixelToSubpixel(const void* srcImage, int srcRowBytes, |
|
749 |
void* dstImage, int dstRowBytes, |
|
750 |
int width, int height) { |
|
751 |
unsigned char *srcRow = (unsigned char *) srcImage; |
|
752 |
unsigned char *dstRow = (unsigned char *) dstImage; |
|
753 |
||
754 |
while (height--) { |
|
755 |
memcpy(dstRow, srcRow, width); |
|
756 |
srcRow += srcRowBytes; |
|
757 |
dstRow += dstRowBytes; |
|
758 |
} |
|
759 |
} |
|
760 |
||
761 |
/* We need it because FT rows are often padded to 4 byte boundaries |
|
762 |
and our internal format is not padded */ |
|
763 |
static void CopyFTSubpixelVToSubpixel(const void* srcImage, int srcRowBytes, |
|
764 |
void* dstImage, int dstRowBytes, |
|
765 |
int width, int height) { |
|
766 |
unsigned char *srcRow = (unsigned char *) srcImage, *srcByte; |
|
767 |
unsigned char *dstRow = (unsigned char *) dstImage, *dstByte; |
|
768 |
int i; |
|
769 |
||
770 |
while (height > 0) { |
|
771 |
srcByte = srcRow; |
|
772 |
dstByte = dstRow; |
|
773 |
for (i = 0; i < width; i++) { |
|
774 |
*dstByte++ = *srcByte; |
|
775 |
*dstByte++ = *(srcByte + srcRowBytes); |
|
776 |
*dstByte++ = *(srcByte + 2*srcRowBytes); |
|
777 |
srcByte++; |
|
778 |
} |
|
779 |
srcRow += 3*srcRowBytes; |
|
780 |
dstRow += dstRowBytes; |
|
781 |
height -= 3; |
|
782 |
} |
|
783 |
} |
|
784 |
||
785 |
||
58629 | 786 |
/* JDK does not use glyph images for fonts with a |
787 |
* pixel size > 100 (see THRESHOLD in OutlineTextRenderer.java) |
|
788 |
* so if the glyph bitmap image dimension is > 1024 pixels, |
|
789 |
* something is up. |
|
790 |
*/ |
|
791 |
#define MAX_GLYPH_DIM 1024 |
|
792 |
||
2 | 793 |
/* |
794 |
* Class: sun_font_FreetypeFontScaler |
|
795 |
* Method: getGlyphImageNative |
|
796 |
* Signature: (Lsun/font/Font2D;JI)J |
|
797 |
*/ |
|
798 |
JNIEXPORT jlong JNICALL |
|
799 |
Java_sun_font_FreetypeFontScaler_getGlyphImageNative( |
|
800 |
JNIEnv *env, jobject scaler, jobject font2D, |
|
801 |
jlong pScalerContext, jlong pScaler, jint glyphCode) { |
|
802 |
||
803 |
int error, imageSize; |
|
804 |
UInt16 width, height; |
|
805 |
GlyphInfo *glyphInfo; |
|
52966
7ed5edf6ba0c
8214002: Cannot use italic font style if the font has embedded bitmap
itakiguchi
parents:
52838
diff
changeset
|
806 |
int renderFlags = FT_LOAD_DEFAULT, target; |
2 | 807 |
FT_GlyphSlot ftglyph; |
808 |
||
809 |
FTScalerContext* context = |
|
810 |
(FTScalerContext*) jlong_to_ptr(pScalerContext); |
|
811 |
FTScalerInfo *scalerInfo = |
|
812 |
(FTScalerInfo*) jlong_to_ptr(pScaler); |
|
813 |
||
814 |
if (isNullScalerContext(context) || scalerInfo == NULL) { |
|
815 |
return ptr_to_jlong(getNullGlyphImage()); |
|
816 |
} |
|
817 |
||
818 |
error = setupFTContext(env, font2D, scalerInfo, context); |
|
819 |
if (error) { |
|
820 |
invalidateJavaScaler(env, scaler, scalerInfo); |
|
821 |
return ptr_to_jlong(getNullGlyphImage()); |
|
822 |
} |
|
823 |
||
50658
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
824 |
if (!context->useSbits) { |
db4f24a6cf34
8204929: Fonts with embedded bitmaps are not always rotated
prr
parents:
50346
diff
changeset
|
825 |
renderFlags |= FT_LOAD_NO_BITMAP; |
2 | 826 |
} |
827 |
||
828 |
/* NB: in case of non identity transform |
|
829 |
we might also prefer to disable transform before hinting, |
|
830 |
and apply it explicitly after hinting is performed. |
|
831 |
Or we can disable hinting. */ |
|
832 |
||
833 |
/* select appropriate hinting mode */ |
|
834 |
if (context->aaType == TEXT_AA_OFF) { |
|
835 |
target = FT_LOAD_TARGET_MONO; |
|
836 |
} else if (context->aaType == TEXT_AA_ON) { |
|
837 |
target = FT_LOAD_TARGET_NORMAL; |
|
838 |
} else if (context->aaType == TEXT_AA_LCD_HRGB || |
|
839 |
context->aaType == TEXT_AA_LCD_HBGR) { |
|
840 |
target = FT_LOAD_TARGET_LCD; |
|
841 |
} else { |
|
842 |
target = FT_LOAD_TARGET_LCD_V; |
|
843 |
} |
|
844 |
renderFlags |= target; |
|
845 |
||
846 |
error = FT_Load_Glyph(scalerInfo->face, glyphCode, renderFlags); |
|
847 |
if (error) { |
|
848 |
//do not destroy scaler yet. |
|
849 |
//this can be problem of particular context (e.g. with bad transform) |
|
850 |
return ptr_to_jlong(getNullGlyphImage()); |
|
851 |
} |
|
852 |
||
853 |
ftglyph = scalerInfo->face->glyph; |
|
854 |
||
855 |
/* apply styles */ |
|
856 |
if (context->doBold) { /* if bold style */ |
|
857 |
FT_GlyphSlot_Embolden(ftglyph); |
|
858 |
} |
|
859 |
if (context->doItalize) { /* if oblique */ |
|
860 |
FT_GlyphSlot_Oblique(ftglyph); |
|
861 |
} |
|
862 |
||
863 |
/* generate bitmap if it is not done yet |
|
864 |
e.g. if algorithmic styling is performed and style was added to outline */ |
|
865 |
if (ftglyph->format == FT_GLYPH_FORMAT_OUTLINE) { |
|
58629 | 866 |
FT_BBox bbox; |
867 |
FT_Outline_Get_CBox(&(ftglyph->outline), &bbox); |
|
868 |
int w = (int)((bbox.xMax>>6)-(bbox.xMin>>6)); |
|
869 |
int h = (int)((bbox.yMax>>6)-(bbox.yMin>>6)); |
|
870 |
if (w > MAX_GLYPH_DIM || h > MAX_GLYPH_DIM) { |
|
871 |
glyphInfo = getNullGlyphImage(); |
|
872 |
return ptr_to_jlong(glyphInfo); |
|
873 |
} |
|
53454 | 874 |
error = FT_Render_Glyph(ftglyph, FT_LOAD_TARGET_MODE(target)); |
875 |
if (error != 0) { |
|
876 |
return ptr_to_jlong(getNullGlyphImage()); |
|
877 |
} |
|
2 | 878 |
} |
879 |
||
880 |
width = (UInt16) ftglyph->bitmap.width; |
|
881 |
height = (UInt16) ftglyph->bitmap.rows; |
|
58629 | 882 |
if (width > MAX_GLYPH_DIM || height > MAX_GLYPH_DIM) { |
883 |
glyphInfo = getNullGlyphImage(); |
|
884 |
return ptr_to_jlong(glyphInfo); |
|
885 |
} |
|
886 |
||
2 | 887 |
|
888 |
imageSize = width*height; |
|
889 |
glyphInfo = (GlyphInfo*) malloc(sizeof(GlyphInfo) + imageSize); |
|
890 |
if (glyphInfo == NULL) { |
|
891 |
glyphInfo = getNullGlyphImage(); |
|
892 |
return ptr_to_jlong(glyphInfo); |
|
893 |
} |
|
894 |
glyphInfo->cellInfo = NULL; |
|
5579
1a5e995a710b
6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents:
4243
diff
changeset
|
895 |
glyphInfo->managed = UNMANAGED_GLYPH; |
2 | 896 |
glyphInfo->rowBytes = width; |
897 |
glyphInfo->width = width; |
|
898 |
glyphInfo->height = height; |
|
899 |
glyphInfo->topLeftX = (float) ftglyph->bitmap_left; |
|
900 |
glyphInfo->topLeftY = (float) -ftglyph->bitmap_top; |
|
901 |
||
881
6aefebba2393
6587560: OpenJDK problem handling bitmaps returned when LCD text is requested
igor
parents:
561
diff
changeset
|
902 |
if (ftglyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD) { |
2 | 903 |
glyphInfo->width = width/3; |
881
6aefebba2393
6587560: OpenJDK problem handling bitmaps returned when LCD text is requested
igor
parents:
561
diff
changeset
|
904 |
} else if (ftglyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V) { |
2 | 905 |
glyphInfo->height = glyphInfo->height/3; |
906 |
} |
|
907 |
||
908 |
if (context->fmType == TEXT_FM_ON) { |
|
554
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
909 |
double advh = FTFixedToFloat(ftglyph->linearHoriAdvance); |
2 | 910 |
glyphInfo->advanceX = |
554
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
911 |
(float) (advh * FTFixedToFloat(context->transform.xx)); |
2 | 912 |
glyphInfo->advanceY = |
554
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
913 |
(float) (advh * FTFixedToFloat(context->transform.xy)); |
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
914 |
} else { |
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
915 |
if (!ftglyph->advance.y) { |
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
916 |
glyphInfo->advanceX = |
50346
9e530b150333
8203485: [freetype] text rotated on 180 degrees is too narrow
bae
parents:
47216
diff
changeset
|
917 |
(float) FT26Dot6ToInt(ftglyph->advance.x); |
554
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
918 |
glyphInfo->advanceY = 0; |
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
919 |
} else if (!ftglyph->advance.x) { |
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
920 |
glyphInfo->advanceX = 0; |
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
921 |
glyphInfo->advanceY = |
50346
9e530b150333
8203485: [freetype] text rotated on 180 degrees is too narrow
bae
parents:
47216
diff
changeset
|
922 |
(float) FT26Dot6ToInt(-ftglyph->advance.y); |
554
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
923 |
} else { |
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
924 |
glyphInfo->advanceX = FT26Dot6ToFloat(ftglyph->advance.x); |
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
925 |
glyphInfo->advanceY = FT26Dot6ToFloat(-ftglyph->advance.y); |
88eb2812c2a4
6697721: OpenJDK: rotated text baseline different between TextLayout and drawString
prr
parents:
2
diff
changeset
|
926 |
} |
2 | 927 |
} |
928 |
||
929 |
if (imageSize == 0) { |
|
930 |
glyphInfo->image = NULL; |
|
931 |
} else { |
|
932 |
glyphInfo->image = (unsigned char*) glyphInfo + sizeof(GlyphInfo); |
|
933 |
//convert result to output format |
|
934 |
//output format is either 3 bytes per pixel (for subpixel modes) |
|
935 |
// or 1 byte per pixel for AA and B&W |
|
936 |
if (ftglyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO) { |
|
937 |
/* convert from 8 pixels per byte to 1 byte per pixel */ |
|
938 |
CopyBW2Grey8(ftglyph->bitmap.buffer, |
|
939 |
ftglyph->bitmap.pitch, |
|
940 |
(void *) glyphInfo->image, |
|
941 |
width, |
|
942 |
width, |
|
943 |
height); |
|
944 |
} else if (ftglyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY) { |
|
945 |
/* byte per pixel to byte per pixel => just copy */ |
|
946 |
memcpy(glyphInfo->image, ftglyph->bitmap.buffer, imageSize); |
|
947 |
} else if (ftglyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY4) { |
|
948 |
/* 4 bits per pixel to byte per pixel */ |
|
949 |
CopyGrey4ToGrey8(ftglyph->bitmap.buffer, |
|
950 |
ftglyph->bitmap.pitch, |
|
951 |
(void *) glyphInfo->image, |
|
952 |
width, |
|
953 |
width, |
|
954 |
height); |
|
955 |
} else if (ftglyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD) { |
|
956 |
/* 3 bytes per pixel to 3 bytes per pixel */ |
|
957 |
CopyFTSubpixelToSubpixel(ftglyph->bitmap.buffer, |
|
958 |
ftglyph->bitmap.pitch, |
|
959 |
(void *) glyphInfo->image, |
|
960 |
width, |
|
961 |
width, |
|
962 |
height); |
|
963 |
} else if (ftglyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V) { |
|
964 |
/* 3 bytes per pixel to 3 bytes per pixel */ |
|
965 |
CopyFTSubpixelVToSubpixel(ftglyph->bitmap.buffer, |
|
966 |
ftglyph->bitmap.pitch, |
|
967 |
(void *) glyphInfo->image, |
|
968 |
width*3, |
|
969 |
width, |
|
970 |
height); |
|
971 |
glyphInfo->rowBytes *=3; |
|
972 |
} else { |
|
973 |
free(glyphInfo); |
|
974 |
glyphInfo = getNullGlyphImage(); |
|
975 |
} |
|
976 |
} |
|
977 |
||
978 |
return ptr_to_jlong(glyphInfo); |
|
979 |
} |
|
980 |
||
981 |
/* |
|
982 |
* Class: sun_font_FreetypeFontScaler |
|
983 |
* Method: disposeNativeScaler |
|
984 |
* Signature: (J)V |
|
985 |
*/ |
|
986 |
JNIEXPORT void JNICALL |
|
987 |
Java_sun_font_FreetypeFontScaler_disposeNativeScaler( |
|
20414
c43f5228a1f9
8020190: Fatal: Bug in native code: jfieldID must match object
prr
parents:
18198
diff
changeset
|
988 |
JNIEnv *env, jobject scaler, jobject font2D, jlong pScaler) { |
2 | 989 |
FTScalerInfo* scalerInfo = (FTScalerInfo *) jlong_to_ptr(pScaler); |
990 |
||
2369
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
991 |
/* Freetype functions *may* cause callback to java |
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
992 |
that can use cached values. Make sure our cache is up to date. |
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
993 |
NB: scaler context is not important at this point, can use NULL. */ |
20414
c43f5228a1f9
8020190: Fatal: Bug in native code: jfieldID must match object
prr
parents:
18198
diff
changeset
|
994 |
int errCode = setupFTContext(env, font2D, scalerInfo, NULL); |
2369
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
995 |
if (errCode) { |
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
996 |
return; |
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
997 |
} |
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
998 |
|
2 | 999 |
freeNativeResources(env, scalerInfo); |
1000 |
} |
|
1001 |
||
1002 |
/* |
|
1003 |
* Class: sun_font_FreetypeFontScaler |
|
1004 |
* Method: getNumGlyphsNative |
|
1005 |
* Signature: ()I |
|
1006 |
*/ |
|
1007 |
JNIEXPORT jint JNICALL |
|
1008 |
Java_sun_font_FreetypeFontScaler_getNumGlyphsNative( |
|
1009 |
JNIEnv *env, jobject scaler, jlong pScaler) { |
|
1010 |
FTScalerInfo* scalerInfo = (FTScalerInfo *) jlong_to_ptr(pScaler); |
|
1011 |
||
1012 |
if (scalerInfo == NULL || scalerInfo->face == NULL) { /* bad/null scaler */ |
|
1013 |
/* null scaler can render 1 glyph - "missing glyph" with code 0 |
|
1014 |
(all glyph codes requested by user are mapped to code 0 at |
|
1015 |
validation step) */ |
|
1016 |
invalidateJavaScaler(env, scaler, scalerInfo); |
|
1017 |
return (jint) 1; |
|
1018 |
} |
|
1019 |
||
1020 |
return (jint) scalerInfo->face->num_glyphs; |
|
1021 |
} |
|
1022 |
||
1023 |
/* |
|
1024 |
* Class: sun_font_FreetypeFontScaler |
|
1025 |
* Method: getMissingGlyphCodeNative |
|
1026 |
* Signature: ()I |
|
1027 |
*/ |
|
1028 |
JNIEXPORT jint JNICALL |
|
1029 |
Java_sun_font_FreetypeFontScaler_getMissingGlyphCodeNative( |
|
1030 |
JNIEnv *env, jobject scaler, jlong pScaler) { |
|
1031 |
||
1032 |
/* Is it always 0 for freetype? */ |
|
1033 |
return 0; |
|
1034 |
} |
|
1035 |
||
1036 |
/* |
|
1037 |
* Class: sun_font_FreetypeFontScaler |
|
1038 |
* Method: getGlyphCodeNative |
|
1039 |
* Signature: (C)I |
|
1040 |
*/ |
|
1041 |
JNIEXPORT jint JNICALL |
|
1042 |
Java_sun_font_FreetypeFontScaler_getGlyphCodeNative( |
|
20414
c43f5228a1f9
8020190: Fatal: Bug in native code: jfieldID must match object
prr
parents:
18198
diff
changeset
|
1043 |
JNIEnv *env, jobject scaler, |
c43f5228a1f9
8020190: Fatal: Bug in native code: jfieldID must match object
prr
parents:
18198
diff
changeset
|
1044 |
jobject font2D, jlong pScaler, jchar charCode) { |
2 | 1045 |
|
1046 |
FTScalerInfo* scalerInfo = (FTScalerInfo *) jlong_to_ptr(pScaler); |
|
2369
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
1047 |
int errCode; |
2 | 1048 |
|
1049 |
if (scaler == NULL || scalerInfo->face == NULL) { /* bad/null scaler */ |
|
1050 |
invalidateJavaScaler(env, scaler, scalerInfo); |
|
1051 |
return 0; |
|
1052 |
} |
|
1053 |
||
2369
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
1054 |
/* Freetype functions *may* cause callback to java |
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
1055 |
that can use cached values. Make sure our cache is up to date. |
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
1056 |
Scaler context is not important here, can use NULL. */ |
20414
c43f5228a1f9
8020190: Fatal: Bug in native code: jfieldID must match object
prr
parents:
18198
diff
changeset
|
1057 |
errCode = setupFTContext(env, font2D, scalerInfo, NULL); |
2369
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
1058 |
if (errCode) { |
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
1059 |
return 0; |
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
1060 |
} |
b58cdc0321f5
6761791: Crash in the FontManager code due to use of JNIEnv saved by another thread
igor
parents:
1733
diff
changeset
|
1061 |
|
2 | 1062 |
return FT_Get_Char_Index(scalerInfo->face, charCode); |
1063 |
} |
|
1064 |
||
1065 |
||
1066 |
#define FloatToF26Dot6(x) ((unsigned int) ((x)*64)) |
|
1067 |
||
1068 |
static FT_Outline* getFTOutline(JNIEnv* env, jobject font2D, |
|
1069 |
FTScalerContext *context, FTScalerInfo* scalerInfo, |
|
1070 |
jint glyphCode, jfloat xpos, jfloat ypos) { |
|
1071 |
int renderFlags; |
|
1072 |
FT_Error error; |
|
1073 |
FT_GlyphSlot ftglyph; |
|
1074 |
||
1075 |
if (glyphCode >= INVISIBLE_GLYPHS || |
|
1076 |
isNullScalerContext(context) || scalerInfo == NULL) { |
|
1077 |
return NULL; |
|
1078 |
} |
|
1079 |
||
1080 |
error = setupFTContext(env, font2D, scalerInfo, context); |
|
1081 |
if (error) { |
|
1082 |
return NULL; |
|
1083 |
} |
|
1084 |
||
1085 |
renderFlags = FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP; |
|
1086 |
||
1087 |
error = FT_Load_Glyph(scalerInfo->face, glyphCode, renderFlags); |
|
1088 |
if (error) { |
|
1089 |
return NULL; |
|
1090 |
} |
|
1091 |
||
1092 |
ftglyph = scalerInfo->face->glyph; |
|
1093 |
||
1094 |
/* apply styles */ |
|
1095 |
if (context->doBold) { /* if bold style */ |
|
1096 |
FT_GlyphSlot_Embolden(ftglyph); |
|
1097 |
} |
|
1098 |
if (context->doItalize) { /* if oblique */ |
|
1099 |
FT_GlyphSlot_Oblique(ftglyph); |
|
1100 |
} |
|
1101 |
||
1102 |
FT_Outline_Translate(&ftglyph->outline, |
|
1103 |
FloatToF26Dot6(xpos), |
|
561
977a2ff62fc9
6703377: freetype: glyph vector outline is not translated correctly
igor
parents:
554
diff
changeset
|
1104 |
-FloatToF26Dot6(ypos)); |
2 | 1105 |
|
1106 |
return &ftglyph->outline; |
|
1107 |
} |
|
1108 |
||
1109 |
#define F26Dot6ToFloat(n) (((float)(n))/((float) 64)) |
|
1110 |
||
1111 |
/* Types of GeneralPath segments. |
|
1112 |
TODO: pull constants from other place? */ |
|
1113 |
||
1114 |
#define SEG_UNKNOWN -1 |
|
1115 |
#define SEG_MOVETO 0 |
|
1116 |
#define SEG_LINETO 1 |
|
1117 |
#define SEG_QUADTO 2 |
|
1118 |
#define SEG_CUBICTO 3 |
|
1119 |
#define SEG_CLOSE 4 |
|
1120 |
||
1121 |
#define WIND_NON_ZERO 0 |
|
1122 |
#define WIND_EVEN_ODD 1 |
|
1123 |
||
1124 |
/* Placeholder to accumulate GeneralPath data */ |
|
1125 |
typedef struct { |
|
1126 |
jint numTypes; |
|
1127 |
jint numCoords; |
|
1128 |
jint lenTypes; |
|
1129 |
jint lenCoords; |
|
1130 |
jint wr; |
|
1131 |
jbyte* pointTypes; |
|
1132 |
jfloat* pointCoords; |
|
1133 |
} GPData; |
|
1134 |
||
1135 |
/* returns 0 on failure */ |
|
1136 |
static int allocateSpaceForGP(GPData* gpdata, int npoints, int ncontours) { |
|
1137 |
int maxTypes, maxCoords; |
|
1138 |
||
1139 |
/* we may have up to N intermediate points per contour |
|
1140 |
(and for each point can actually cause new curve to be generated) |
|
1141 |
In addition we can also have 2 extra point per outline. |
|
1142 |
*/ |
|
1143 |
maxTypes = 2*npoints + 2*ncontours; |
|
1144 |
maxCoords = 4*(npoints + 2*ncontours); //we may need to insert |
|
1145 |
//up to n-1 intermediate points |
|
1146 |
||
1147 |
/* first usage - allocate space and intialize all fields */ |
|
1148 |
if (gpdata->pointTypes == NULL || gpdata->pointCoords == NULL) { |
|
1149 |
gpdata->lenTypes = maxTypes; |
|
1150 |
gpdata->lenCoords = maxCoords; |
|
1151 |
gpdata->pointTypes = (jbyte*) |
|
1152 |
malloc(gpdata->lenTypes*sizeof(jbyte)); |
|
1153 |
gpdata->pointCoords = (jfloat*) |
|
1154 |
malloc(gpdata->lenCoords*sizeof(jfloat)); |
|
1155 |
gpdata->numTypes = 0; |
|
1156 |
gpdata->numCoords = 0; |
|
1157 |
gpdata->wr = WIND_NON_ZERO; /* By default, outlines are filled |
|
1158 |
using the non-zero winding rule. */ |
|
1159 |
} else { |
|
1160 |
/* do we have enough space? */ |
|
1161 |
if (gpdata->lenTypes - gpdata->numTypes < maxTypes) { |
|
1162 |
gpdata->lenTypes += maxTypes; |
|
1163 |
gpdata->pointTypes = (jbyte*) |
|
1164 |
realloc(gpdata->pointTypes, gpdata->lenTypes*sizeof(jbyte)); |
|
1165 |
} |
|
1166 |
||
1167 |
if (gpdata->lenCoords - gpdata->numCoords < maxCoords) { |
|
1168 |
gpdata->lenCoords += maxCoords; |
|
1169 |
gpdata->pointCoords = (jfloat*) |
|
1170 |
realloc(gpdata->pointCoords, gpdata->lenCoords*sizeof(jfloat)); |
|
1171 |
} |
|
1172 |
} |
|
1173 |
||
1174 |
/* failure if any of mallocs failed */ |
|
1175 |
if (gpdata->pointTypes == NULL || gpdata->pointCoords == NULL) |
|
1176 |
return 0; |
|
1177 |
else |
|
1178 |
return 1; |
|
1179 |
} |
|
1180 |
||
26617
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1181 |
static void addSeg(GPData *gp, jbyte type) { |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1182 |
gp->pointTypes[gp->numTypes++] = type; |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1183 |
} |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1184 |
|
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1185 |
static void addCoords(GPData *gp, FT_Vector *p) { |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1186 |
gp->pointCoords[gp->numCoords++] = F26Dot6ToFloat(p->x); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1187 |
gp->pointCoords[gp->numCoords++] = -F26Dot6ToFloat(p->y); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1188 |
} |
2 | 1189 |
|
26617
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1190 |
static int moveTo(FT_Vector *to, GPData *gp) { |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1191 |
if (gp->numCoords) |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1192 |
addSeg(gp, SEG_CLOSE); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1193 |
addCoords(gp, to); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1194 |
addSeg(gp, SEG_MOVETO); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1195 |
return FT_Err_Ok; |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1196 |
} |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1197 |
|
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1198 |
static int lineTo(FT_Vector *to, GPData *gp) { |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1199 |
addCoords(gp, to); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1200 |
addSeg(gp, SEG_LINETO); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1201 |
return FT_Err_Ok; |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1202 |
} |
2 | 1203 |
|
26617
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1204 |
static int conicTo(FT_Vector *control, FT_Vector *to, GPData *gp) { |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1205 |
addCoords(gp, control); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1206 |
addCoords(gp, to); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1207 |
addSeg(gp, SEG_QUADTO); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1208 |
return FT_Err_Ok; |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1209 |
} |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1210 |
|
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1211 |
static int cubicTo(FT_Vector *control1, |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1212 |
FT_Vector *control2, |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1213 |
FT_Vector *to, |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1214 |
GPData *gp) { |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1215 |
addCoords(gp, control1); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1216 |
addCoords(gp, control2); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1217 |
addCoords(gp, to); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1218 |
addSeg(gp, SEG_CUBICTO); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1219 |
return FT_Err_Ok; |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1220 |
} |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1221 |
|
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1222 |
static void addToGP(GPData* gpdata, FT_Outline*outline) { |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1223 |
static const FT_Outline_Funcs outline_funcs = { |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1224 |
(FT_Outline_MoveToFunc) moveTo, |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1225 |
(FT_Outline_LineToFunc) lineTo, |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1226 |
(FT_Outline_ConicToFunc) conicTo, |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1227 |
(FT_Outline_CubicToFunc) cubicTo, |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1228 |
0, /* shift */ |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1229 |
0, /* delta */ |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1230 |
}; |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1231 |
|
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1232 |
FT_Outline_Decompose(outline, &outline_funcs, gpdata); |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1233 |
if (gpdata->numCoords) |
ca6dcc52ff1c
8057986: freetype code to get glyph outline does not handle initial control point properly
martin
parents:
25859
diff
changeset
|
1234 |
addSeg(gpdata, SEG_CLOSE); |
2 | 1235 |
|
1236 |
/* If set to 1, the outline will be filled using the even-odd fill rule */ |
|
1237 |
if (outline->flags & FT_OUTLINE_EVEN_ODD_FILL) { |
|
1238 |
gpdata->wr = WIND_EVEN_ODD; |
|
1239 |
} |
|
1240 |
} |
|
1241 |
||
1242 |
static void freeGP(GPData* gpdata) { |
|
1243 |
if (gpdata->pointCoords != NULL) { |
|
1244 |
free(gpdata->pointCoords); |
|
1245 |
gpdata->pointCoords = NULL; |
|
1246 |
gpdata->numCoords = 0; |
|
1247 |
gpdata->lenCoords = 0; |
|
1248 |
} |
|
1249 |
if (gpdata->pointTypes != NULL) { |
|
1250 |
free(gpdata->pointTypes); |
|
1251 |
gpdata->pointTypes = NULL; |
|
1252 |
gpdata->numTypes = 0; |
|
1253 |
gpdata->lenTypes = 0; |
|
1254 |
} |
|
1255 |
} |
|
1256 |
||
1257 |
static jobject getGlyphGeneralPath(JNIEnv* env, jobject font2D, |
|
1258 |
FTScalerContext *context, FTScalerInfo *scalerInfo, |
|
1259 |
jint glyphCode, jfloat xpos, jfloat ypos) { |
|
1260 |
||
1261 |
FT_Outline* outline; |
|
1262 |
jobject gp = NULL; |
|
1263 |
jbyteArray types; |
|
1264 |
jfloatArray coords; |
|
1265 |
GPData gpdata; |
|
1266 |
||
1267 |
outline = getFTOutline(env, font2D, context, scalerInfo, |
|
1268 |
glyphCode, xpos, ypos); |
|
1269 |
||
1270 |
if (outline == NULL || outline->n_points == 0) { |
|
1271 |
return gp; |
|
1272 |
} |
|
1273 |
||
1274 |
gpdata.pointTypes = NULL; |
|
1275 |
gpdata.pointCoords = NULL; |
|
1276 |
if (!allocateSpaceForGP(&gpdata, outline->n_points, outline->n_contours)) { |
|
1277 |
return gp; |
|
1278 |
} |
|
1279 |
||
1280 |
addToGP(&gpdata, outline); |
|
1281 |
||
1282 |
types = (*env)->NewByteArray(env, gpdata.numTypes); |
|
1283 |
coords = (*env)->NewFloatArray(env, gpdata.numCoords); |
|
1284 |
||
1285 |
if (types && coords) { |
|
1286 |
(*env)->SetByteArrayRegion(env, types, 0, |
|
1287 |
gpdata.numTypes, |
|
1288 |
gpdata.pointTypes); |
|
1289 |
(*env)->SetFloatArrayRegion(env, coords, 0, |
|
1290 |
gpdata.numCoords, |
|
1291 |
gpdata.pointCoords); |
|
1292 |
gp = (*env)->NewObject(env, |
|
1293 |
sunFontIDs.gpClass, |
|
1294 |
sunFontIDs.gpCtr, |
|
1295 |
gpdata.wr, |
|
1296 |
types, |
|
1297 |
gpdata.numTypes, |
|
1298 |
coords, |
|
1299 |
gpdata.numCoords); |
|
1300 |
} |
|
1301 |
||
1302 |
freeGP(&gpdata); |
|
1303 |
||
1304 |
return gp; |
|
1305 |
} |
|
1306 |
||
1307 |
/* |
|
1308 |
* Class: sun_font_FreetypeFontScaler |
|
1309 |
* Method: getGlyphOutlineNative |
|
1310 |
* Signature: (Lsun/font/Font2D;JIFF)Ljava/awt/geom/GeneralPath; |
|
1311 |
*/ |
|
1312 |
JNIEXPORT jobject JNICALL |
|
1313 |
Java_sun_font_FreetypeFontScaler_getGlyphOutlineNative( |
|
1314 |
JNIEnv *env, jobject scaler, jobject font2D, jlong pScalerContext, |
|
1315 |
jlong pScaler, jint glyphCode, jfloat xpos, jfloat ypos) { |
|
1316 |
||
1317 |
FTScalerContext *context = |
|
1318 |
(FTScalerContext*) jlong_to_ptr(pScalerContext); |
|
1319 |
FTScalerInfo* scalerInfo = (FTScalerInfo *) jlong_to_ptr(pScaler); |
|
1320 |
||
1321 |
jobject gp = getGlyphGeneralPath(env, |
|
1322 |
font2D, |
|
1323 |
context, |
|
1324 |
scalerInfo, |
|
1325 |
glyphCode, |
|
1326 |
xpos, |
|
1327 |
ypos); |
|
1328 |
if (gp == NULL) { /* can be legal */ |
|
1329 |
gp = (*env)->NewObject(env, |
|
1330 |
sunFontIDs.gpClass, |
|
1331 |
sunFontIDs.gpCtrEmpty); |
|
1332 |
} |
|
1333 |
return gp; |
|
1334 |
} |
|
1335 |
||
1336 |
/* |
|
1337 |
* Class: sun_font_FreetypeFontScaler |
|
1338 |
* Method: getGlyphOutlineBoundsNative |
|
1339 |
* Signature: (Lsun/font/Font2D;JI)Ljava/awt/geom/Rectangle2D/Float; |
|
1340 |
*/ |
|
1341 |
JNIEXPORT jobject JNICALL |
|
1342 |
Java_sun_font_FreetypeFontScaler_getGlyphOutlineBoundsNative( |
|
1343 |
JNIEnv *env, jobject scaler, jobject font2D, |
|
1344 |
jlong pScalerContext, jlong pScaler, jint glyphCode) { |
|
1345 |
||
1346 |
FT_Outline *outline; |
|
1347 |
FT_BBox bbox; |
|
1348 |
int error; |
|
1349 |
jobject bounds; |
|
1350 |
||
1351 |
FTScalerContext *context = |
|
1352 |
(FTScalerContext*) jlong_to_ptr(pScalerContext); |
|
1353 |
FTScalerInfo* scalerInfo = (FTScalerInfo *) jlong_to_ptr(pScaler); |
|
1354 |
||
1355 |
outline = getFTOutline(env, font2D, context, scalerInfo, glyphCode, 0, 0); |
|
1356 |
if (outline == NULL || outline->n_points == 0) { |
|
1357 |
/* it is legal case, e.g. invisible glyph */ |
|
1358 |
bounds = (*env)->NewObject(env, |
|
1359 |
sunFontIDs.rect2DFloatClass, |
|
1360 |
sunFontIDs.rect2DFloatCtr); |
|
1361 |
return bounds; |
|
1362 |
} |
|
1363 |
||
1364 |
error = FT_Outline_Get_BBox(outline, &bbox); |
|
1365 |
||
1366 |
//convert bbox |
|
1367 |
if (error || bbox.xMin >= bbox.xMax || bbox.yMin >= bbox.yMax) { |
|
1368 |
bounds = (*env)->NewObject(env, |
|
1369 |
sunFontIDs.rect2DFloatClass, |
|
1370 |
sunFontIDs.rect2DFloatCtr); |
|
1371 |
} else { |
|
1372 |
bounds = (*env)->NewObject(env, |
|
1373 |
sunFontIDs.rect2DFloatClass, |
|
1374 |
sunFontIDs.rect2DFloatCtr4, |
|
1375 |
F26Dot6ToFloat(bbox.xMin), |
|
1733
95c41a86eac9
6761856: OpenJDK: vertical text metrics may be significanly different from those returned by Sun JDK
igor
parents:
881
diff
changeset
|
1376 |
F26Dot6ToFloat(-bbox.yMax), |
2 | 1377 |
F26Dot6ToFloat(bbox.xMax-bbox.xMin), |
1378 |
F26Dot6ToFloat(bbox.yMax-bbox.yMin)); |
|
1379 |
} |
|
1380 |
||
1381 |
return bounds; |
|
1382 |
} |
|
1383 |
||
1384 |
/* |
|
1385 |
* Class: sun_font_FreetypeFontScaler |
|
1386 |
* Method: getGlyphVectorOutlineNative |
|
1387 |
* Signature: (Lsun/font/Font2D;J[IIFF)Ljava/awt/geom/GeneralPath; |
|
1388 |
*/ |
|
1389 |
JNIEXPORT jobject |
|
1390 |
JNICALL |
|
1391 |
Java_sun_font_FreetypeFontScaler_getGlyphVectorOutlineNative( |
|
1392 |
JNIEnv *env, jobject scaler, jobject font2D, |
|
1393 |
jlong pScalerContext, jlong pScaler, |
|
1394 |
jintArray glyphArray, jint numGlyphs, jfloat xpos, jfloat ypos) { |
|
1395 |
||
1396 |
FT_Outline* outline; |
|
1397 |
jobject gp = NULL; |
|
1398 |
jbyteArray types; |
|
1399 |
jfloatArray coords; |
|
1400 |
GPData gpdata; |
|
1401 |
int i; |
|
1402 |
jint *glyphs; |
|
1403 |
||
1404 |
FTScalerContext *context = |
|
1405 |
(FTScalerContext*) jlong_to_ptr(pScalerContext); |
|
1406 |
FTScalerInfo *scalerInfo = |
|
1407 |
(FTScalerInfo*) jlong_to_ptr(pScaler); |
|
1408 |
||
18198 | 1409 |
glyphs = NULL; |
1410 |
if (numGlyphs > 0 && 0xffffffffu / sizeof(jint) >= numGlyphs) { |
|
1411 |
glyphs = (jint*) malloc(numGlyphs*sizeof(jint)); |
|
1412 |
} |
|
2 | 1413 |
if (glyphs == NULL) { |
18198 | 1414 |
// We reach here if: |
1415 |
// 1. numGlyphs <= 0, |
|
1416 |
// 2. overflow check failed, or |
|
1417 |
// 3. malloc failed. |
|
2 | 1418 |
gp = (*env)->NewObject(env, sunFontIDs.gpClass, sunFontIDs.gpCtrEmpty); |
1419 |
return gp; |
|
1420 |
} |
|
1421 |
||
1422 |
(*env)->GetIntArrayRegion(env, glyphArray, 0, numGlyphs, glyphs); |
|
1423 |
||
18198 | 1424 |
gpdata.numCoords = 0; |
2 | 1425 |
for (i=0; i<numGlyphs;i++) { |
1426 |
if (glyphs[i] >= INVISIBLE_GLYPHS) { |
|
1427 |
continue; |
|
1428 |
} |
|
1429 |
outline = getFTOutline(env, |
|
1430 |
font2D, |
|
1431 |
context, |
|
1432 |
scalerInfo, |
|
1433 |
glyphs[i], |
|
1434 |
xpos, ypos); |
|
1435 |
||
1436 |
if (outline == NULL || outline->n_points == 0) { |
|
1437 |
continue; |
|
1438 |
} |
|
1439 |
||
1440 |
gpdata.pointTypes = NULL; |
|
1441 |
gpdata.pointCoords = NULL; |
|
1442 |
if (!allocateSpaceForGP(&gpdata, outline->n_points, |
|
1443 |
outline->n_contours)) { |
|
1444 |
break; |
|
1445 |
} |
|
1446 |
||
1447 |
addToGP(&gpdata, outline); |
|
1448 |
} |
|
1449 |
free(glyphs); |
|
1450 |
||
1451 |
if (gpdata.numCoords != 0) { |
|
1452 |
types = (*env)->NewByteArray(env, gpdata.numTypes); |
|
1453 |
coords = (*env)->NewFloatArray(env, gpdata.numCoords); |
|
1454 |
||
1455 |
if (types && coords) { |
|
1456 |
(*env)->SetByteArrayRegion(env, types, 0, |
|
1457 |
gpdata.numTypes, gpdata.pointTypes); |
|
1458 |
(*env)->SetFloatArrayRegion(env, coords, 0, |
|
1459 |
gpdata.numCoords, gpdata.pointCoords); |
|
1460 |
||
1461 |
gp=(*env)->NewObject(env, |
|
1462 |
sunFontIDs.gpClass, |
|
1463 |
sunFontIDs.gpCtr, |
|
1464 |
gpdata.wr, |
|
1465 |
types, |
|
1466 |
gpdata.numTypes, |
|
1467 |
coords, |
|
1468 |
gpdata.numCoords); |
|
1469 |
return gp; |
|
1470 |
} |
|
1471 |
} |
|
1472 |
return (*env)->NewObject(env, sunFontIDs.gpClass, sunFontIDs.gpCtrEmpty); |
|
1473 |
} |
|
1474 |
||
1475 |
JNIEXPORT jlong JNICALL |
|
1476 |
Java_sun_font_FreetypeFontScaler_getUnitsPerEMNative( |
|
1477 |
JNIEnv *env, jobject scaler, jlong pScaler) { |
|
1478 |
||
1479 |
FTScalerInfo *s = (FTScalerInfo* ) jlong_to_ptr(pScaler); |
|
1480 |
||
1481 |
/* Freetype doc says: |
|
1482 |
The number of font units per EM square for this face. |
|
1483 |
This is typically 2048 for TrueType fonts, and 1000 for Type 1 fonts. |
|
1484 |
Only relevant for scalable formats. |
|
1485 |
However, layout engine might be not tested with anything but 2048. |
|
1486 |
||
1487 |
NB: test it! */ |
|
1488 |
if (s != NULL) { |
|
1489 |
return s->face->units_per_EM; |
|
1490 |
} |
|
1491 |
return 2048; |
|
1492 |
} |
|
1493 |
||
1494 |
/* This native method is called by the OpenType layout engine. */ |
|
1495 |
JNIEXPORT jobject JNICALL |
|
1496 |
Java_sun_font_FreetypeFontScaler_getGlyphPointNative( |
|
1497 |
JNIEnv *env, jobject scaler, jobject font2D, jlong pScalerContext, |
|
1498 |
jlong pScaler, jint glyphCode, jint pointNumber) { |
|
1499 |
||
1500 |
FT_Outline* outline; |
|
1501 |
jobject point = NULL; |
|
1502 |
jfloat x=0, y=0; |
|
1503 |
FTScalerContext *context = |
|
1504 |
(FTScalerContext*) jlong_to_ptr(pScalerContext); |
|
1505 |
FTScalerInfo *scalerInfo = (FTScalerInfo*) jlong_to_ptr(pScaler); |
|
1506 |
||
1507 |
outline = getFTOutline(env, font2D, context, scalerInfo, glyphCode, 0, 0); |
|
1508 |
||
1509 |
if (outline != NULL && outline->n_points > pointNumber) { |
|
1510 |
x = F26Dot6ToFloat(outline->points[pointNumber].x); |
|
1511 |
y = -F26Dot6ToFloat(outline->points[pointNumber].y); |
|
1512 |
} |
|
1513 |
||
1514 |
return (*env)->NewObject(env, sunFontIDs.pt2DFloatClass, |
|
1515 |
sunFontIDs.pt2DFloatCtr, x, y); |
|
1516 |
} |