author | lana |
Fri, 14 Sep 2012 13:52:30 -0700 | |
changeset 13768 | 4bf20f0aaac4 |
parent 13766 | 0e523a5ed1af |
parent 13678 | 5c8001201f98 |
child 13983 | 0c34ee8d36fc |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
2 |
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
// Native side of the Quartz text pipe, paints on Quartz Surface Datas. |
|
27 |
// Interesting Docs : /Developer/Documentation/Cocoa/TasksAndConcepts/ProgrammingTopics/FontHandling/FontHandling.html |
|
28 |
||
29 |
#import "sun_awt_SunHints.h" |
|
30 |
#import "sun_lwawt_macosx_CTextPipe.h" |
|
31 |
#import "sun_java2d_OSXSurfaceData.h" |
|
32 |
||
33 |
#import <JavaNativeFoundation/JavaNativeFoundation.h> |
|
34 |
||
35 |
#import "CoreTextSupport.h" |
|
36 |
#import "QuartzSurfaceData.h" |
|
37 |
#include "AWTStrike.h" |
|
38 |
||
13678
5c8001201f98
7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents:
12047
diff
changeset
|
39 |
/* Use THIS_FILE when it is available. */ |
5c8001201f98
7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents:
12047
diff
changeset
|
40 |
#ifndef THIS_FILE |
5c8001201f98
7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents:
12047
diff
changeset
|
41 |
#define THIS_FILE __FILE__ |
5c8001201f98
7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents:
12047
diff
changeset
|
42 |
#endif |
12047 | 43 |
|
44 |
static const CGAffineTransform sInverseTX = { 1, 0, 0, -1, 0, 0 }; |
|
45 |
||
46 |
||
47 |
#pragma mark --- CoreText Support --- |
|
48 |
||
49 |
||
50 |
// Translates a Unicode into a CGGlyph/CTFontRef pair |
|
51 |
// Returns the substituted font, and places the appropriate glyph into "glyphRef" |
|
52 |
CTFontRef JavaCT_CopyCTFallbackFontAndGlyphForUnicode |
|
53 |
(const AWTFont *font, const UTF16Char *charRef, CGGlyph *glyphRef, int count) { |
|
54 |
CTFontRef fallback = JRSFontCreateFallbackFontForCharacters((CTFontRef)font->fFont, charRef, count); |
|
55 |
if (fallback == NULL) |
|
56 |
{ |
|
57 |
// use the original font if we somehow got duped into trying to fallback something we can't |
|
58 |
fallback = (CTFontRef)font->fFont; |
|
59 |
CFRetain(fallback); |
|
60 |
} |
|
61 |
||
62 |
CTFontGetGlyphsForCharacters(fallback, charRef, glyphRef, count); |
|
63 |
return fallback; |
|
64 |
} |
|
65 |
||
66 |
// Translates a Java glyph code int (might be a negative unicode value) into a CGGlyph/CTFontRef pair |
|
67 |
// Returns the substituted font, and places the appropriate glyph into "glyph" |
|
68 |
CTFontRef JavaCT_CopyCTFallbackFontAndGlyphForJavaGlyphCode |
|
69 |
(const AWTFont *font, const jint glyphCode, CGGlyph *glyphRef) |
|
70 |
{ |
|
71 |
// negative glyph codes are really unicodes, which were placed there by the mapper |
|
72 |
// to indicate we should use CoreText to substitute the character |
|
73 |
if (glyphCode >= 0) |
|
74 |
{ |
|
75 |
*glyphRef = glyphCode; |
|
76 |
CFRetain(font->fFont); |
|
77 |
return (CTFontRef)font->fFont; |
|
78 |
} |
|
79 |
||
80 |
UTF16Char character = -glyphCode; |
|
81 |
return JavaCT_CopyCTFallbackFontAndGlyphForUnicode(font, &character, glyphRef, 1); |
|
82 |
} |
|
83 |
||
84 |
// Breakup a 32 bit unicode value into the component surrogate pairs |
|
85 |
void JavaCT_BreakupUnicodeIntoSurrogatePairs(int uniChar, UTF16Char charRef[]) { |
|
86 |
int value = uniChar - 0x10000; |
|
87 |
UTF16Char low_surrogate = (value & 0x3FF) | LO_SURROGATE_START; |
|
88 |
UTF16Char high_surrogate = (((int)(value & 0xFFC00)) >> 10) | HI_SURROGATE_START; |
|
89 |
charRef[0] = high_surrogate; |
|
90 |
charRef[1] = low_surrogate; |
|
91 |
} |
|
92 |
||
93 |
||
94 |
||
95 |
/* |
|
96 |
* Callback for CoreText which uses the CoreTextProviderStruct to feed CT UniChars |
|
97 |
* We only use it for one-off lines, and don't attempt to fragment our strings |
|
98 |
*/ |
|
99 |
const UniChar *Java_CTProvider |
|
100 |
(CFIndex stringIndex, CFIndex *charCount, CFDictionaryRef *attributes, void *refCon) |
|
101 |
{ |
|
102 |
// if we have a zero length string we can just return NULL for the string |
|
103 |
// or if the index anything other than 0 we are not using core text |
|
104 |
// correctly since we only have one run. |
|
105 |
if (stringIndex != 0) |
|
106 |
{ |
|
107 |
return NULL; |
|
108 |
} |
|
109 |
||
110 |
CTS_ProviderStruct *ctps = (CTS_ProviderStruct *)refCon; |
|
111 |
*charCount = ctps->length; |
|
112 |
*attributes = ctps->attributes; |
|
113 |
return ctps->unicodes; |
|
114 |
} |
|
115 |
||
116 |
||
117 |
/* |
|
118 |
* Gets a Dictionary filled with common details we want to use for CoreText when we are interacting |
|
119 |
* with it from Java. |
|
120 |
*/ |
|
121 |
static NSDictionary* ctsDictionaryFor(const NSFont *font, BOOL useFractionalMetrics) |
|
122 |
{ |
|
123 |
NSNumber *gZeroNumber = [NSNumber numberWithInt:0]; |
|
124 |
NSNumber *gOneNumber = [NSNumber numberWithInt:1]; |
|
125 |
||
126 |
return [NSDictionary dictionaryWithObjectsAndKeys: |
|
127 |
font, NSFontAttributeName, |
|
128 |
gOneNumber, (id)kCTForegroundColorFromContextAttributeName, |
|
129 |
useFractionalMetrics ? gZeroNumber : gOneNumber, @"CTIntegerMetrics", // force integer hack in CoreText to help with Java's integer assumptions |
|
130 |
gZeroNumber, NSLigatureAttributeName, |
|
131 |
gZeroNumber, NSKernAttributeName, |
|
132 |
nil]; |
|
133 |
} |
|
134 |
||
135 |
// Itterates though each glyph, and if a transform is present for that glyph, apply it to the CGContext, and strike the glyph. |
|
136 |
// If there is no per-glyph transform, just strike the glyph. Advances must also be transformed on-the-spot as well. |
|
137 |
void JavaCT_DrawGlyphVector |
|
138 |
(const QuartzSDOps *qsdo, const AWTStrike *strike, const BOOL useSubstituion, const int uniChars[], const CGGlyph glyphs[], CGSize advances[], const jint g_gvTXIndicesAsInts[], const jdouble g_gvTransformsAsDoubles[], const CFIndex length) |
|
139 |
{ |
|
140 |
CGPoint pt = { 0, 0 }; |
|
141 |
||
142 |
// get our baseline transform and font |
|
143 |
CGContextRef cgRef = qsdo->cgRef; |
|
144 |
CGAffineTransform ctmText = CGContextGetTextMatrix(cgRef); |
|
145 |
//CGFontRef cgFont = CGContextGetFont(cgRef); |
|
146 |
||
147 |
CGContextSaveGState(cgRef); |
|
148 |
CGAffineTransform invTx = CGAffineTransformInvert(strike->fTx); |
|
149 |
||
150 |
NSUInteger i; |
|
151 |
for (i = 0; i < length; i++) |
|
152 |
{ |
|
153 |
CGGlyph glyph = glyphs[i]; |
|
154 |
int uniChar = uniChars[i]; |
|
155 |
// if we found a unichar instead of a glyph code, get the fallback font, |
|
156 |
// find the glyph code for the fallback font, and set the font on the current context |
|
157 |
if (uniChar != 0) |
|
158 |
{ |
|
159 |
CTFontRef fallback; |
|
160 |
if (uniChar > 0xFFFF) { |
|
161 |
UTF16Char charRef[2]; |
|
162 |
JavaCT_BreakupUnicodeIntoSurrogatePairs(uniChar, charRef); |
|
163 |
CGGlyph glyphTmp[2]; |
|
164 |
fallback = JavaCT_CopyCTFallbackFontAndGlyphForUnicode(strike->fAWTFont, (const UTF16Char *)&charRef, (CGGlyph *)&glyphTmp, 2); |
|
165 |
glyph = glyphTmp[0]; |
|
166 |
} else { |
|
167 |
const UTF16Char u = uniChar; |
|
168 |
fallback = JavaCT_CopyCTFallbackFontAndGlyphForUnicode(strike->fAWTFont, &u, (CGGlyph *)&glyph, 1); |
|
169 |
} |
|
170 |
if (fallback) { |
|
171 |
const CGFontRef cgFallback = CTFontCopyGraphicsFont(fallback, NULL); |
|
172 |
CFRelease(fallback); |
|
173 |
||
174 |
if (cgFallback) { |
|
175 |
CGContextSetFont(cgRef, cgFallback); |
|
176 |
CFRelease(cgFallback); |
|
177 |
} |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
// if we have per-glyph transformations |
|
182 |
int tin = (g_gvTXIndicesAsInts == NULL) ? -1 : (g_gvTXIndicesAsInts[i] - 1) * 6; |
|
183 |
if (tin < 0) |
|
184 |
{ |
|
185 |
CGContextShowGlyphsAtPoint(cgRef, pt.x, pt.y, &glyph, 1); |
|
186 |
} |
|
187 |
else |
|
188 |
{ |
|
189 |
CGAffineTransform tx = CGAffineTransformMake( |
|
190 |
(CGFloat)g_gvTransformsAsDoubles[tin + 0], (CGFloat)g_gvTransformsAsDoubles[tin + 2], |
|
191 |
(CGFloat)g_gvTransformsAsDoubles[tin + 1], (CGFloat)g_gvTransformsAsDoubles[tin + 3], |
|
192 |
0, 0); |
|
193 |
||
194 |
CGPoint txOffset = { (CGFloat)g_gvTransformsAsDoubles[tin + 4], (CGFloat)g_gvTransformsAsDoubles[tin + 5] }; |
|
195 |
||
196 |
txOffset = CGPointApplyAffineTransform(txOffset, invTx); |
|
197 |
||
198 |
// apply the transform, strike the glyph, can change the transform back |
|
199 |
CGContextSetTextMatrix(cgRef, CGAffineTransformConcat(ctmText, tx)); |
|
200 |
CGContextShowGlyphsAtPoint(cgRef, txOffset.x + pt.x, txOffset.y + pt.y, &glyph, 1); |
|
201 |
CGContextSetTextMatrix(cgRef, ctmText); |
|
202 |
||
203 |
// transform the measured advance for this strike |
|
204 |
advances[i] = CGSizeApplyAffineTransform(advances[i], tx); |
|
205 |
advances[i].width += txOffset.x; |
|
206 |
advances[i].height += txOffset.y; |
|
207 |
} |
|
208 |
||
209 |
// move our next x,y |
|
210 |
pt.x += advances[i].width; |
|
211 |
pt.y += advances[i].height; |
|
212 |
||
213 |
// reset the font on the context after striking a unicode with CoreText |
|
214 |
if (uniChar != 0) |
|
215 |
{ |
|
216 |
// CGContextSetFont(cgRef, cgFont); |
|
217 |
CGContextSaveGState(cgRef); |
|
218 |
} |
|
219 |
} |
|
220 |
} |
|
221 |
||
222 |
// Using the Quartz Surface Data context, draw a hot-substituted character run |
|
223 |
void JavaCT_DrawTextUsingQSD(JNIEnv *env, const QuartzSDOps *qsdo, const AWTStrike *strike, const jchar *chars, const jsize length) |
|
224 |
{ |
|
225 |
CGContextRef cgRef = qsdo->cgRef; |
|
226 |
||
227 |
AWTFont *awtFont = strike->fAWTFont; |
|
228 |
CGFloat ptSize = strike->fSize; |
|
229 |
CGAffineTransform tx = strike->fFontTx; |
|
230 |
||
231 |
NSFont *nsFont = [NSFont fontWithName:[awtFont->fFont fontName] size:ptSize]; |
|
232 |
||
233 |
if (ptSize != 0) { |
|
234 |
CGFloat invScale = 1 / ptSize; |
|
235 |
tx = CGAffineTransformConcat(tx, CGAffineTransformMakeScale(invScale, invScale)); |
|
236 |
CGContextConcatCTM(cgRef, tx); |
|
237 |
} |
|
238 |
||
239 |
CGContextSetTextMatrix(cgRef, CGAffineTransformIdentity); // resets the damage from CoreText |
|
240 |
||
241 |
NSString *string = [NSString stringWithCharacters:chars length:length]; |
|
13766
0e523a5ed1af
7183516: [macosx]Can't print-out the defined fonts for PrintFont_2D and AntialiasTableTest.
jgodinez
parents:
12047
diff
changeset
|
242 |
/* |
0e523a5ed1af
7183516: [macosx]Can't print-out the defined fonts for PrintFont_2D and AntialiasTableTest.
jgodinez
parents:
12047
diff
changeset
|
243 |
The calls below were used previously but for unknown reason did not |
0e523a5ed1af
7183516: [macosx]Can't print-out the defined fonts for PrintFont_2D and AntialiasTableTest.
jgodinez
parents:
12047
diff
changeset
|
244 |
render using the right font (see bug 7183516) when attribString is not |
0e523a5ed1af
7183516: [macosx]Can't print-out the defined fonts for PrintFont_2D and AntialiasTableTest.
jgodinez
parents:
12047
diff
changeset
|
245 |
initialized with font dictionary attributes. It seems that "options" |
0e523a5ed1af
7183516: [macosx]Can't print-out the defined fonts for PrintFont_2D and AntialiasTableTest.
jgodinez
parents:
12047
diff
changeset
|
246 |
in CTTypesetterCreateWithAttributedStringAndOptions which contains the |
0e523a5ed1af
7183516: [macosx]Can't print-out the defined fonts for PrintFont_2D and AntialiasTableTest.
jgodinez
parents:
12047
diff
changeset
|
247 |
font dictionary is ignored. |
0e523a5ed1af
7183516: [macosx]Can't print-out the defined fonts for PrintFont_2D and AntialiasTableTest.
jgodinez
parents:
12047
diff
changeset
|
248 |
|
12047 | 249 |
NSAttributedString *attribString = [[NSAttributedString alloc] initWithString:string]; |
250 |
||
251 |
CTTypesetterRef typeSetterRef = CTTypesetterCreateWithAttributedStringAndOptions((CFAttributedStringRef) attribString, (CFDictionaryRef) ctsDictionaryFor(nsFont, JRSFontStyleUsesFractionalMetrics(strike->fStyle))); |
|
13766
0e523a5ed1af
7183516: [macosx]Can't print-out the defined fonts for PrintFont_2D and AntialiasTableTest.
jgodinez
parents:
12047
diff
changeset
|
252 |
*/ |
0e523a5ed1af
7183516: [macosx]Can't print-out the defined fonts for PrintFont_2D and AntialiasTableTest.
jgodinez
parents:
12047
diff
changeset
|
253 |
NSAttributedString *attribString = [[NSAttributedString alloc] |
0e523a5ed1af
7183516: [macosx]Can't print-out the defined fonts for PrintFont_2D and AntialiasTableTest.
jgodinez
parents:
12047
diff
changeset
|
254 |
initWithString:string |
0e523a5ed1af
7183516: [macosx]Can't print-out the defined fonts for PrintFont_2D and AntialiasTableTest.
jgodinez
parents:
12047
diff
changeset
|
255 |
attributes:ctsDictionaryFor(nsFont, JRSFontStyleUsesFractionalMetrics(strike->fStyle))]; |
0e523a5ed1af
7183516: [macosx]Can't print-out the defined fonts for PrintFont_2D and AntialiasTableTest.
jgodinez
parents:
12047
diff
changeset
|
256 |
|
0e523a5ed1af
7183516: [macosx]Can't print-out the defined fonts for PrintFont_2D and AntialiasTableTest.
jgodinez
parents:
12047
diff
changeset
|
257 |
CTTypesetterRef typeSetterRef = CTTypesetterCreateWithAttributedString((CFAttributedStringRef) attribString); |
12047 | 258 |
|
259 |
CFRange range = {0, length}; |
|
260 |
CTLineRef lineRef = CTTypesetterCreateLine(typeSetterRef, range); |
|
261 |
||
262 |
CTLineDraw(lineRef, cgRef); |
|
263 |
||
264 |
[attribString release]; |
|
265 |
CFRelease(lineRef); |
|
266 |
CFRelease(typeSetterRef); |
|
267 |
} |
|
268 |
||
269 |
||
270 |
/*---------------------- |
|
271 |
DrawTextContext is the funnel for all of our CoreText drawing. |
|
272 |
All three JNI apis call through this method. |
|
273 |
----------------------*/ |
|
274 |
static void DrawTextContext |
|
275 |
(JNIEnv *env, QuartzSDOps *qsdo, const AWTStrike *strike, const jchar *chars, const jsize length, const jdouble x, const jdouble y) |
|
276 |
{ |
|
277 |
if (length == 0) |
|
278 |
{ |
|
279 |
return; |
|
280 |
} |
|
281 |
||
282 |
qsdo->BeginSurface(env, qsdo, SD_Text); |
|
283 |
if (qsdo->cgRef == NULL) |
|
284 |
{ |
|
285 |
qsdo->FinishSurface(env, qsdo); |
|
286 |
return; |
|
287 |
} |
|
288 |
||
289 |
CGContextRef cgRef = qsdo->cgRef; |
|
290 |
||
291 |
||
292 |
CGContextSaveGState(cgRef); |
|
293 |
JRSFontSetRenderingStyleOnContext(cgRef, strike->fStyle); |
|
294 |
||
295 |
// we want to translate before we transform (scale or rotate) <rdar://4042541> (vm) |
|
296 |
CGContextTranslateCTM(cgRef, x, y); |
|
297 |
||
298 |
AWTFont *awtfont = strike->fAWTFont; //(AWTFont *)(qsdo->fontInfo.awtfont); |
|
299 |
NSCharacterSet *charSet = [awtfont->fFont coveredCharacterSet]; |
|
300 |
||
301 |
JavaCT_DrawTextUsingQSD(env, qsdo, strike, chars, length); // Draw with CoreText |
|
302 |
||
303 |
CGContextRestoreGState(cgRef); |
|
304 |
||
305 |
qsdo->FinishSurface(env, qsdo); |
|
306 |
} |
|
307 |
||
308 |
#pragma mark --- Glyph Vector Pipeline --- |
|
309 |
||
310 |
/*----------------------------------- |
|
311 |
Glyph Vector Pipeline |
|
312 |
||
313 |
doDrawGlyphs() has been separated into several pipelined functions to increase performance, |
|
314 |
and improve accountability for JNI resources, malloc'd memory, and error handling. |
|
315 |
||
316 |
Each stage of the pipeline is responsible for doing only one major thing, like allocating buffers, |
|
317 |
aquiring transform arrays from JNI, filling buffers, or striking glyphs. All resources or memory |
|
318 |
aquired at a given stage, must be released in that stage. Any error that occurs (like a failed malloc) |
|
319 |
is to be handled in the stage it occurs in, and is to return immediatly after freeing it's resources. |
|
320 |
||
321 |
-----------------------------------*/ |
|
322 |
||
323 |
static JNF_CLASS_CACHE(jc_StandardGlyphVector, "sun/font/StandardGlyphVector"); |
|
324 |
||
325 |
// Checks the GlyphVector Java object for any transforms that were applied to individual characters. If none are present, |
|
326 |
// strike the glyphs immediately in Core Graphics. Otherwise, obtain the arrays, and defer to above. |
|
327 |
static inline void doDrawGlyphsPipe_checkForPerGlyphTransforms |
|
328 |
(JNIEnv *env, QuartzSDOps *qsdo, const AWTStrike *strike, jobject gVector, BOOL useSubstituion, int *uniChars, CGGlyph *glyphs, CGSize *advances, size_t length) |
|
329 |
{ |
|
330 |
// if we have no character substitution, and no per-glyph transformations - strike now! |
|
331 |
static JNF_MEMBER_CACHE(jm_StandardGlyphVector_gti, jc_StandardGlyphVector, "gti", "Lsun/font/StandardGlyphVector$GlyphTransformInfo;"); |
|
332 |
jobject gti = JNFGetObjectField(env, gVector, jm_StandardGlyphVector_gti); |
|
333 |
if (gti == 0) |
|
334 |
{ |
|
335 |
if (useSubstituion) |
|
336 |
{ |
|
337 |
// quasi-simple case, substitution, but no per-glyph transforms |
|
338 |
JavaCT_DrawGlyphVector(qsdo, strike, TRUE, uniChars, glyphs, advances, NULL, NULL, length); |
|
339 |
} |
|
340 |
else |
|
341 |
{ |
|
342 |
// fast path, straight to CG without per-glyph transforms |
|
343 |
CGContextShowGlyphsWithAdvances(qsdo->cgRef, glyphs, advances, length); |
|
344 |
} |
|
345 |
return; |
|
346 |
} |
|
347 |
||
348 |
static JNF_CLASS_CACHE(jc_StandardGlyphVector_GlyphTransformInfo, "sun/font/StandardGlyphVector$GlyphTransformInfo"); |
|
349 |
static JNF_MEMBER_CACHE(jm_StandardGlyphVector_GlyphTransformInfo_transforms, jc_StandardGlyphVector_GlyphTransformInfo, "transforms", "[D"); |
|
350 |
jdoubleArray g_gtiTransformsArray = JNFGetObjectField(env, gti, jm_StandardGlyphVector_GlyphTransformInfo_transforms); //(*env)->GetObjectField(env, gti, g_gtiTransforms); |
|
351 |
jdouble *g_gvTransformsAsDoubles = (*env)->GetPrimitiveArrayCritical(env, g_gtiTransformsArray, NULL); |
|
352 |
||
353 |
static JNF_MEMBER_CACHE(jm_StandardGlyphVector_GlyphTransformInfo_indices, jc_StandardGlyphVector_GlyphTransformInfo, "indices", "[I"); |
|
354 |
jintArray g_gtiTXIndicesArray = JNFGetObjectField(env, gti, jm_StandardGlyphVector_GlyphTransformInfo_indices); |
|
355 |
jint *g_gvTXIndicesAsInts = (*env)->GetPrimitiveArrayCritical(env, g_gtiTXIndicesArray, NULL); |
|
356 |
||
357 |
// slowest case, we have per-glyph transforms, and possibly glyph substitution as well |
|
358 |
JavaCT_DrawGlyphVector(qsdo, strike, useSubstituion, uniChars, glyphs, advances, g_gvTXIndicesAsInts, g_gvTransformsAsDoubles, length); |
|
359 |
||
360 |
(*env)->ReleasePrimitiveArrayCritical(env, g_gtiTransformsArray, g_gvTransformsAsDoubles, JNI_ABORT); |
|
361 |
(*env)->DeleteLocalRef(env, g_gtiTransformsArray); |
|
362 |
||
363 |
(*env)->ReleasePrimitiveArrayCritical(env, g_gtiTXIndicesArray, g_gvTXIndicesAsInts, JNI_ABORT); |
|
364 |
(*env)->DeleteLocalRef(env, g_gtiTXIndicesArray); |
|
365 |
} |
|
366 |
||
367 |
// Retrieves advances for translated unicodes |
|
368 |
// Uses "glyphs" as a temporary buffer for the glyph-to-unicode translation |
|
369 |
void JavaCT_GetAdvancesForUnichars |
|
370 |
(const NSFont *font, const int uniChars[], CGGlyph glyphs[], const size_t length, CGSize advances[]) |
|
371 |
{ |
|
372 |
// cycle over each spot, and if we discovered a unicode to substitute, we have to calculate the advance for it |
|
373 |
size_t i; |
|
374 |
for (i = 0; i < length; i++) |
|
375 |
{ |
|
376 |
UniChar uniChar = uniChars[i]; |
|
377 |
if (uniChar == 0) continue; |
|
378 |
||
379 |
CGGlyph glyph = 0; |
|
380 |
const CTFontRef fallback = JRSFontCreateFallbackFontForCharacters((CTFontRef)font, &uniChar, 1); |
|
381 |
if (fallback) { |
|
382 |
CTFontGetGlyphsForCharacters(fallback, &uniChar, &glyph, 1); |
|
383 |
CTFontGetAdvancesForGlyphs(fallback, kCTFontDefaultOrientation, &glyph, &(advances[i]), 1); |
|
384 |
CFRelease(fallback); |
|
385 |
} |
|
386 |
||
387 |
glyphs[i] = glyph; |
|
388 |
} |
|
389 |
} |
|
390 |
||
391 |
// Fills the glyph buffer with glyphs from the GlyphVector object. Also checks to see if the glyph's positions have been |
|
392 |
// already caculated from GlyphVector, or we simply ask Core Graphics to make some advances for us. Pre-calculated positions |
|
393 |
// are translated into advances, since CG only understands advances. |
|
394 |
static inline void doDrawGlyphsPipe_fillGlyphAndAdvanceBuffers |
|
395 |
(JNIEnv *env, QuartzSDOps *qsdo, const AWTStrike *strike, jobject gVector, CGGlyph *glyphs, int *uniChars, CGSize *advances, size_t length, jintArray glyphsArray) |
|
396 |
{ |
|
397 |
// fill the glyph buffer |
|
398 |
jint *glyphsAsInts = (*env)->GetPrimitiveArrayCritical(env, glyphsArray, NULL); |
|
399 |
||
400 |
// if a glyph code from Java is negative, that means it is really a unicode value |
|
401 |
// which we can use in CoreText to strike the character in another font |
|
402 |
size_t i; |
|
403 |
BOOL complex = NO; |
|
404 |
for (i = 0; i < length; i++) |
|
405 |
{ |
|
406 |
jint code = glyphsAsInts[i]; |
|
407 |
if (code < 0) |
|
408 |
{ |
|
409 |
complex = YES; |
|
410 |
uniChars[i] = -code; |
|
411 |
glyphs[i] = 0; |
|
412 |
} |
|
413 |
else |
|
414 |
{ |
|
415 |
uniChars[i] = 0; |
|
416 |
glyphs[i] = code; |
|
417 |
} |
|
418 |
} |
|
419 |
||
420 |
(*env)->ReleasePrimitiveArrayCritical(env, glyphsArray, glyphsAsInts, JNI_ABORT); |
|
421 |
||
422 |
// fill the advance buffer |
|
423 |
static JNF_MEMBER_CACHE(jm_StandardGlyphVector_positions, jc_StandardGlyphVector, "positions", "[F"); |
|
424 |
jfloatArray posArray = JNFGetObjectField(env, gVector, jm_StandardGlyphVector_positions); |
|
425 |
if (posArray != NULL) |
|
426 |
{ |
|
427 |
// in this case, the positions have already been pre-calculated for us on the Java side |
|
428 |
||
429 |
jfloat *positions = (*env)->GetPrimitiveArrayCritical(env, posArray, NULL); |
|
430 |
CGPoint prev; |
|
431 |
prev.x = positions[0]; |
|
432 |
prev.y = positions[1]; |
|
433 |
||
434 |
// <rdar://problem/4294061> take the first point, and move the context to that location |
|
435 |
CGContextTranslateCTM(qsdo->cgRef, prev.x, prev.y); |
|
436 |
||
437 |
CGAffineTransform invTx = CGAffineTransformInvert(strike->fFontTx); |
|
438 |
||
439 |
// for each position, figure out the advance (since CG won't take positions directly) |
|
440 |
size_t i; |
|
441 |
for (i = 0; i < length - 1; i++) |
|
442 |
{ |
|
443 |
size_t i2 = (i+1) * 2; |
|
444 |
CGPoint pt; |
|
445 |
pt.x = positions[i2]; |
|
446 |
pt.y = positions[i2+1]; |
|
447 |
pt = CGPointApplyAffineTransform(pt, invTx); |
|
448 |
advances[i].width = pt.x - prev.x; |
|
449 |
advances[i].height = -(pt.y - prev.y); // negative to translate to device space |
|
450 |
prev.x = pt.x; |
|
451 |
prev.y = pt.y; |
|
452 |
} |
|
453 |
||
454 |
(*env)->ReleasePrimitiveArrayCritical(env, posArray, positions, JNI_ABORT); |
|
455 |
(*env)->DeleteLocalRef(env, posArray); |
|
456 |
} |
|
457 |
else |
|
458 |
{ |
|
459 |
// in this case, we have to go and calculate the positions ourselves |
|
460 |
// there were no pre-calculated positions from the glyph buffer on the Java side |
|
461 |
AWTFont *awtFont = strike->fAWTFont; |
|
462 |
CTFontGetAdvancesForGlyphs((CTFontRef)awtFont->fFont, kCTFontDefaultOrientation, glyphs, advances, length); |
|
463 |
||
464 |
if (complex) |
|
465 |
{ |
|
466 |
JavaCT_GetAdvancesForUnichars(awtFont->fFont, uniChars, glyphs, length, advances); |
|
467 |
} |
|
468 |
} |
|
469 |
||
470 |
// continue on to the next stage of the pipe |
|
471 |
doDrawGlyphsPipe_checkForPerGlyphTransforms(env, qsdo, strike, gVector, complex, uniChars, glyphs, advances, length); |
|
472 |
} |
|
473 |
||
474 |
// Obtains the glyph array to determine the number of glyphs we are dealing with. If we are dealing a large number of glyphs, |
|
475 |
// we malloc a buffer to hold the glyphs and their advances, otherwise we use stack allocated buffers. |
|
476 |
static inline void doDrawGlyphsPipe_getGlyphVectorLengthAndAlloc |
|
477 |
(JNIEnv *env, QuartzSDOps *qsdo, const AWTStrike *strike, jobject gVector) |
|
478 |
{ |
|
479 |
static JNF_MEMBER_CACHE(jm_StandardGlyphVector_glyphs, jc_StandardGlyphVector, "glyphs", "[I"); |
|
480 |
jintArray glyphsArray = JNFGetObjectField(env, gVector, jm_StandardGlyphVector_glyphs); |
|
481 |
jsize length = (*env)->GetArrayLength(env, glyphsArray); |
|
482 |
||
483 |
if (length == 0) |
|
484 |
{ |
|
485 |
// nothing to draw |
|
486 |
(*env)->DeleteLocalRef(env, glyphsArray); |
|
487 |
return; |
|
488 |
} |
|
489 |
||
490 |
if (length < MAX_STACK_ALLOC_GLYPH_BUFFER_SIZE) |
|
491 |
{ |
|
492 |
// if we are small enough, fit everything onto the stack |
|
493 |
CGGlyph glyphs[length]; |
|
494 |
int uniChars[length]; |
|
495 |
CGSize advances[length]; |
|
496 |
doDrawGlyphsPipe_fillGlyphAndAdvanceBuffers(env, qsdo, strike, gVector, glyphs, uniChars, advances, length, glyphsArray); |
|
497 |
} |
|
498 |
else |
|
499 |
{ |
|
500 |
// otherwise, we should malloc and free buffers for this large run |
|
501 |
CGGlyph *glyphs = (CGGlyph *)malloc(sizeof(CGGlyph) * length); |
|
502 |
int *uniChars = (int *)malloc(sizeof(int) * length); |
|
503 |
CGSize *advances = (CGSize *)malloc(sizeof(CGSize) * length); |
|
504 |
||
505 |
if (glyphs == NULL || advances == NULL) |
|
506 |
{ |
|
507 |
(*env)->DeleteLocalRef(env, glyphsArray); |
|
13678
5c8001201f98
7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents:
12047
diff
changeset
|
508 |
[NSException raise:NSMallocException format:@"%s-%s:%d", THIS_FILE, __FUNCTION__, __LINE__]; |
12047 | 509 |
return; |
510 |
} |
|
511 |
||
512 |
doDrawGlyphsPipe_fillGlyphAndAdvanceBuffers(env, qsdo, strike, gVector, glyphs, uniChars, advances, length, glyphsArray); |
|
513 |
||
514 |
free(glyphs); |
|
515 |
free(uniChars); |
|
516 |
free(advances); |
|
517 |
} |
|
518 |
||
519 |
(*env)->DeleteLocalRef(env, glyphsArray); |
|
520 |
} |
|
521 |
||
522 |
// Setup and save the state of the CGContext, and apply any java.awt.Font transforms to the context. |
|
523 |
static inline void doDrawGlyphsPipe_applyFontTransforms |
|
524 |
(JNIEnv *env, QuartzSDOps *qsdo, const AWTStrike *strike, jobject gVector, const jfloat x, const jfloat y) |
|
525 |
{ |
|
526 |
CGContextRef cgRef = qsdo->cgRef; |
|
527 |
CGContextSetFontSize(cgRef, 1.0); |
|
528 |
CGContextSetFont(cgRef, strike->fAWTFont->fNativeCGFont); |
|
529 |
CGContextSetTextMatrix(cgRef, CGAffineTransformIdentity); |
|
530 |
||
531 |
CGAffineTransform tx = strike->fFontTx; |
|
532 |
tx.tx += x; |
|
533 |
tx.ty += y; |
|
534 |
CGContextConcatCTM(cgRef, tx); |
|
535 |
||
536 |
doDrawGlyphsPipe_getGlyphVectorLengthAndAlloc(env, qsdo, strike, gVector); |
|
537 |
} |
|
538 |
||
539 |
||
540 |
#pragma mark --- CTextPipe JNI --- |
|
541 |
||
542 |
||
543 |
/* |
|
544 |
* Class: sun_lwawt_macosx_CTextPipe |
|
545 |
* Method: doDrawString |
|
546 |
* Signature: (Lsun/java2d/SurfaceData;JLjava/lang/String;DD)V |
|
547 |
*/ |
|
548 |
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CTextPipe_doDrawString |
|
549 |
(JNIEnv *env, jobject jthis, jobject jsurfacedata, jlong awtStrikePtr, jstring str, jdouble x, jdouble y) |
|
550 |
{ |
|
551 |
QuartzSDOps *qsdo = (QuartzSDOps *)SurfaceData_GetOps(env, jsurfacedata); |
|
552 |
AWTStrike *awtStrike = (AWTStrike *)jlong_to_ptr(awtStrikePtr); |
|
553 |
||
554 |
JNF_COCOA_ENTER(env); |
|
555 |
||
556 |
jsize len = (*env)->GetStringLength(env, str); |
|
557 |
||
558 |
if (len < MAX_STACK_ALLOC_GLYPH_BUFFER_SIZE) // optimized for stack allocation <rdar://problem/4285041> |
|
559 |
{ |
|
560 |
jchar unichars[len]; |
|
561 |
(*env)->GetStringRegion(env, str, 0, len, unichars); |
|
562 |
JNF_CHECK_AND_RETHROW_EXCEPTION(env); |
|
563 |
||
564 |
// Draw the text context |
|
565 |
DrawTextContext(env, qsdo, awtStrike, unichars, len, x, y); |
|
566 |
} |
|
567 |
else |
|
568 |
{ |
|
569 |
// Get string to draw and the length |
|
570 |
const jchar *unichars = JNFGetStringUTF16UniChars(env, str); |
|
571 |
||
572 |
// Draw the text context |
|
573 |
DrawTextContext(env, qsdo, awtStrike, unichars, len, x, y); |
|
574 |
||
575 |
JNFReleaseStringUTF16UniChars(env, str, unichars); |
|
576 |
} |
|
577 |
||
578 |
JNF_COCOA_RENDERER_EXIT(env); |
|
579 |
} |
|
580 |
||
581 |
||
582 |
/* |
|
583 |
* Class: sun_lwawt_macosx_CTextPipe |
|
584 |
* Method: doUnicodes |
|
585 |
* Signature: (Lsun/java2d/SurfaceData;J[CIIFF)V |
|
586 |
*/ |
|
587 |
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CTextPipe_doUnicodes |
|
588 |
(JNIEnv *env, jobject jthis, jobject jsurfacedata, jlong awtStrikePtr, jcharArray unicodes, jint offset, jint length, jfloat x, jfloat y) |
|
589 |
{ |
|
590 |
QuartzSDOps *qsdo = (QuartzSDOps *)SurfaceData_GetOps(env, jsurfacedata); |
|
591 |
AWTStrike *awtStrike = (AWTStrike *)jlong_to_ptr(awtStrikePtr); |
|
592 |
||
593 |
JNF_COCOA_ENTER(env); |
|
594 |
||
595 |
// Setup the text context |
|
596 |
if (length < MAX_STACK_ALLOC_GLYPH_BUFFER_SIZE) // optimized for stack allocation |
|
597 |
{ |
|
598 |
jchar copyUnichars[length]; |
|
599 |
(*env)->GetCharArrayRegion(env, unicodes, offset, length, copyUnichars); |
|
600 |
JNF_CHECK_AND_RETHROW_EXCEPTION(env); |
|
601 |
DrawTextContext(env, qsdo, awtStrike, copyUnichars, length, x, y); |
|
602 |
} |
|
603 |
else |
|
604 |
{ |
|
605 |
jchar *copyUnichars = malloc(length * sizeof(jchar)); |
|
606 |
if (!copyUnichars) { |
|
607 |
[JNFException raise:env as:kOutOfMemoryError reason:"Failed to malloc memory to create the glyphs for string drawing"]; |
|
608 |
} |
|
609 |
||
610 |
@try { |
|
611 |
(*env)->GetCharArrayRegion(env, unicodes, offset, length, copyUnichars); |
|
612 |
JNF_CHECK_AND_RETHROW_EXCEPTION(env); |
|
613 |
DrawTextContext(env, qsdo, awtStrike, copyUnichars, length, x, y); |
|
614 |
} @finally { |
|
615 |
free(copyUnichars); |
|
616 |
} |
|
617 |
} |
|
618 |
||
619 |
JNF_COCOA_RENDERER_EXIT(env); |
|
620 |
} |
|
621 |
||
622 |
/* |
|
623 |
* Class: sun_lwawt_macosx_CTextPipe |
|
624 |
* Method: doOneUnicode |
|
625 |
* Signature: (Lsun/java2d/SurfaceData;JCFF)V |
|
626 |
*/ |
|
627 |
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CTextPipe_doOneUnicode |
|
628 |
(JNIEnv *env, jobject jthis, jobject jsurfacedata, jlong awtStrikePtr, jchar aUnicode, jfloat x, jfloat y) |
|
629 |
{ |
|
630 |
QuartzSDOps *qsdo = (QuartzSDOps *)SurfaceData_GetOps(env, jsurfacedata); |
|
631 |
AWTStrike *awtStrike = (AWTStrike *)jlong_to_ptr(awtStrikePtr); |
|
632 |
||
633 |
JNF_COCOA_ENTER(env); |
|
634 |
||
635 |
DrawTextContext(env, qsdo, awtStrike, &aUnicode, 1, x, y); |
|
636 |
||
637 |
JNF_COCOA_RENDERER_EXIT(env); |
|
638 |
} |
|
639 |
||
640 |
/* |
|
641 |
* Class: sun_lwawt_macosx_CTextPipe |
|
642 |
* Method: doDrawGlyphs |
|
643 |
* Signature: (Lsun/java2d/SurfaceData;JLjava/awt/font/GlyphVector;FF)V |
|
644 |
*/ |
|
645 |
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CTextPipe_doDrawGlyphs |
|
646 |
(JNIEnv *env, jobject jthis, jobject jsurfacedata, jlong awtStrikePtr, jobject gVector, jfloat x, jfloat y) |
|
647 |
{ |
|
648 |
QuartzSDOps *qsdo = (QuartzSDOps *)SurfaceData_GetOps(env, jsurfacedata); |
|
649 |
AWTStrike *awtStrike = (AWTStrike *)jlong_to_ptr(awtStrikePtr); |
|
650 |
||
651 |
JNF_COCOA_ENTER(env); |
|
652 |
||
653 |
qsdo->BeginSurface(env, qsdo, SD_Text); |
|
654 |
if (qsdo->cgRef == NULL) |
|
655 |
{ |
|
656 |
qsdo->FinishSurface(env, qsdo); |
|
657 |
return; |
|
658 |
} |
|
659 |
||
660 |
CGContextSaveGState(qsdo->cgRef); |
|
661 |
JRSFontSetRenderingStyleOnContext(qsdo->cgRef, JRSFontGetRenderingStyleForHints(sun_awt_SunHints_INTVAL_FRACTIONALMETRICS_ON, sun_awt_SunHints_INTVAL_TEXT_ANTIALIAS_ON)); |
|
662 |
||
663 |
doDrawGlyphsPipe_applyFontTransforms(env, qsdo, awtStrike, gVector, x, y); |
|
664 |
||
665 |
CGContextRestoreGState(qsdo->cgRef); |
|
666 |
||
667 |
qsdo->FinishSurface(env, qsdo); |
|
668 |
||
669 |
JNF_COCOA_RENDERER_EXIT(env); |
|
670 |
} |