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 #import <JavaNativeFoundation/JavaNativeFoundation.h> 27 #import "java_awt_geom_PathIterator.h" 28 #import "sun_awt_SunHints.h" 29 #import "sun_font_CStrike.h" 30 #import "CGGlyphImages.h" 31 #import "CGGlyphOutlines.h" 32 #import "AWTStrike.h" 33 #import "CoreTextSupport.h" 34 //#import "jni_util.h" 35 36 @implementation AWTStrike 37 38 static CGAffineTransform sInverseTX = { 1, 0, 0, -1, 0, 0 }; 39 40 - (id) initWithFont:(AWTFont *)awtFont 41 tx:(CGAffineTransform)tx 42 invDevTx:(CGAffineTransform)invDevTx 43 style:(JRSFontRenderingStyle)style 44 aaStyle:(jint)aaStyle { 45 46 self = [super init]; 47 if (self) { 48 fAWTFont = [awtFont retain]; 49 fStyle = style; 50 fAAStyle = aaStyle; 51 52 fTx = tx; // composited glyph and device transform 53 54 fAltTx = tx; 55 fAltTx.b *= -1; 56 fAltTx.d *= -1; 57 58 invDevTx.b *= -1; 59 invDevTx.c *= -1; 60 fFontTx = CGAffineTransformConcat(CGAffineTransformConcat(tx, invDevTx), sInverseTX); 61 62 // the "font size" is the square root of the determinant of the matrix 63 fSize = sqrt(abs(fFontTx.a * fFontTx.d - fFontTx.b * fFontTx.c)); 64 } 65 return self; 66 } 67 68 - (void) dealloc { 69 [fAWTFont release]; 70 fAWTFont = nil; 71 72 [super dealloc]; 73 } 74 75 + (AWTStrike *) awtStrikeForFont:(AWTFont *)awtFont 76 tx:(CGAffineTransform)tx 77 invDevTx:(CGAffineTransform)invDevTx 78 style:(JRSFontRenderingStyle)style 79 aaStyle:(jint)aaStyle { 80 81 return [[[AWTStrike alloc] initWithFont:awtFont 82 tx:tx invDevTx:invDevTx 83 style:style 84 aaStyle:aaStyle] autorelease]; 85 } 86 87 @end 88 89 90 #define AWT_FONT_CLEANUP_SETUP \ 91 BOOL _fontThrowJavaException = NO; 92 93 #define AWT_FONT_CLEANUP_CHECK(a) \ 94 if ((a) == NULL) { \ 95 _fontThrowJavaException = YES; \ 96 goto cleanup; \ 97 } \ 98 if ((*env)->ExceptionCheck(env) == JNI_TRUE) { \ 99 goto cleanup; \ 100 } 101 102 #define AWT_FONT_CLEANUP_FINISH \ 103 if (_fontThrowJavaException == YES) { \ 104 char s[512]; \ 105 sprintf(s, "%s-%s:%d", __FILE__, __FUNCTION__, __LINE__); \ 106 [JNFException raise:env as:kRuntimeException reason:s]; \ 107 } 108 109 110 /* 111 * Creates an affine transform from the corresponding doubles sent 112 * from CStrike.getGlyphTx(). 113 */ 114 static inline CGAffineTransform 115 GetTxFromDoubles(JNIEnv *env, jdoubleArray txArray) 116 { 117 if (txArray == NULL) { 118 return CGAffineTransformIdentity; 119 } 120 121 jdouble *txPtr = (*env)->GetPrimitiveArrayCritical(env, txArray, NULL); 122 123 CGAffineTransform tx = 124 CGAffineTransformMake(txPtr[0], txPtr[1], txPtr[2], 125 txPtr[3], txPtr[4], txPtr[5]); 126 tx = CGAffineTransformConcat(sInverseTX, tx); 127 128 (*env)->ReleasePrimitiveArrayCritical(env, txArray, txPtr, JNI_ABORT); 129 130 return tx; 131 } 132 133 /* 134 * Class: sun_font_CStrike 135 * Method: getNativeGlyphAdvance 136 * Signature: (JI)F 137 */ 138 JNIEXPORT jfloat JNICALL 139 Java_sun_font_CStrike_getNativeGlyphAdvance 140 (JNIEnv *env, jclass clazz, jlong awtStrikePtr, jint glyphCode) 141 { 142 CGSize advance; 143 JNF_COCOA_ENTER(env); 144 AWTFont *awtFont = ((AWTStrike *)jlong_to_ptr(awtStrikePtr))->fAWTFont; 145 146 // negative glyph codes are really unicodes, which were placed there by the mapper 147 // to indicate we should use CoreText to substitute the character 148 CGGlyph glyph; 149 const CTFontRef fallback = CTS_CopyCTFallbackFontAndGlyphForJavaGlyphCode(awtFont, glyphCode, &glyph); 150 CTFontGetAdvancesForGlyphs(fallback, kCTFontDefaultOrientation, &glyph, &advance, 1); 151 CFRelease(fallback); 152 153 JNF_COCOA_EXIT(env); 154 return advance.width; 155 } 156 157 /* 158 * Class: sun_font_CStrike 159 * Method: getNativeGlyphImageBounds 160 * Signature: (JJILjava/awt/geom/Rectangle2D/Float;DD)V 161 */ 162 JNIEXPORT void JNICALL 163 Java_sun_font_CStrike_getNativeGlyphImageBounds 164 (JNIEnv *env, jclass clazz, 165 jlong awtStrikePtr, jint glyphCode, 166 jobject result /*Rectangle*/, jdouble x, jdouble y) 167 { 168 JNF_COCOA_ENTER(env); 169 170 AWTStrike *awtStrike = (AWTStrike *)jlong_to_ptr(awtStrikePtr); 171 AWTFont *awtFont = awtStrike->fAWTFont; 172 173 CGAffineTransform tx = awtStrike->fAltTx; 174 tx.tx += x; 175 tx.ty += y; 176 177 // negative glyph codes are really unicodes, which were placed there by the mapper 178 // to indicate we should use CoreText to substitute the character 179 CGGlyph glyph; 180 const CTFontRef fallback = CTS_CopyCTFallbackFontAndGlyphForJavaGlyphCode(awtFont, glyphCode, &glyph); 181 182 CGRect bbox; 183 JRSFontGetBoundingBoxesForGlyphsAndStyle(fallback, &tx, awtStrike->fStyle, &glyph, 1, &bbox); 184 CFRelease(fallback); 185 186 // the origin of this bounding box is relative to the bottom-left corner baseline 187 CGFloat decender = -bbox.origin.y; 188 bbox.origin.y = -bbox.size.height + decender; 189 190 // Rectangle2D.Float.setRect(float x, float y, float width, float height); 191 static JNF_CLASS_CACHE(sjc_Rectangle2D_Float, "java/awt/geom/Rectangle2D$Float"); // cache class id for Rectangle 192 static JNF_MEMBER_CACHE(sjr_Rectangle2DFloat_setRect, sjc_Rectangle2D_Float, "setRect", "(FFFF)V"); 193 JNFCallVoidMethod(env, result, sjr_Rectangle2DFloat_setRect, (jfloat)bbox.origin.x, (jfloat)bbox.origin.y, (jfloat)bbox.size.width, (jfloat)bbox.size.height); 194 195 JNF_COCOA_EXIT(env); 196 } 197 198 /* 199 * Class: sun_font_CStrike 200 * Method: getNativeGlyphOutline 201 * Signature: (JJIDD)Ljava/awt/geom/GeneralPath; 202 */ 203 JNIEXPORT jobject JNICALL 204 Java_sun_font_CStrike_getNativeGlyphOutline 205 (JNIEnv *env, jclass clazz, 206 jlong awtStrikePtr, jint glyphCode, jdouble xPos, jdouble yPos) 207 { 208 jobject generalPath = NULL; 209 210 JNF_COCOA_ENTER(env); 211 212 AWTPathRef path = NULL; 213 jfloatArray pointCoords = NULL; 214 jbyteArray pointTypes = NULL; 215 216 AWT_FONT_CLEANUP_SETUP; 217 218 AWTStrike *awtStrike = (AWTStrike *)jlong_to_ptr(awtStrikePtr); 219 AWTFont *awtfont = awtStrike->fAWTFont; 220 221 AWT_FONT_CLEANUP_CHECK(awtfont); 222 223 // inverting the shear order and sign to compensate for the flipped coordinate system 224 CGAffineTransform tx = awtStrike->fTx; 225 tx.tx += xPos; 226 tx.ty += yPos; 227 228 // get the right font and glyph for this "Java GlyphCode" 229 230 CGGlyph glyph; 231 const CTFontRef font = CTS_CopyCTFallbackFontAndGlyphForJavaGlyphCode(awtfont, glyphCode, &glyph); 232 233 // get the advance of this glyph 234 CGSize advance; 235 CTFontGetAdvancesForGlyphs(font, kCTFontDefaultOrientation, &glyph, &advance, 1); 236 237 // Create AWTPath 238 path = AWTPathCreate(CGSizeMake(xPos, yPos)); 239 AWT_FONT_CLEANUP_CHECK(path); 240 241 // Get the paths 242 tx = awtStrike->fTx; 243 tx = CGAffineTransformConcat(tx, sInverseTX); 244 AWTGetGlyphOutline(&glyph, (NSFont *)font, &advance, &tx, 0, 1, &path); 245 CFRelease(font); 246 247 pointCoords = (*env)->NewFloatArray(env, path->fNumberOfDataElements); 248 AWT_FONT_CLEANUP_CHECK(pointCoords); 249 250 (*env)->SetFloatArrayRegion(env, pointCoords, 0, path->fNumberOfDataElements, (jfloat*)path->fSegmentData); 251 252 // Copy the pointTypes to the general path 253 pointTypes = (*env)->NewByteArray(env, path->fNumberOfSegments); 254 AWT_FONT_CLEANUP_CHECK(pointTypes); 255 256 (*env)->SetByteArrayRegion(env, pointTypes, 0, path->fNumberOfSegments, (jbyte*)path->fSegmentType); 257 258 static JNF_CLASS_CACHE(jc_GeneralPath, "java/awt/geom/GeneralPath"); 259 static JNF_CTOR_CACHE(jc_GeneralPath_ctor, jc_GeneralPath, "(I[BI[FI)V"); 260 generalPath = JNFNewObject(env, jc_GeneralPath_ctor, java_awt_geom_PathIterator_WIND_NON_ZERO, pointTypes, path->fNumberOfSegments, pointCoords, path->fNumberOfDataElements); // AWT_THREADING Safe (known object) 261 262 // Cleanup 263 cleanup: 264 if (path != NULL) { 265 AWTPathFree(path); 266 path = NULL; 267 } 268 269 if (pointCoords != NULL) { 270 (*env)->DeleteLocalRef(env, pointCoords); 271 pointCoords = NULL; 272 } 273 274 if (pointTypes != NULL) { 275 (*env)->DeleteLocalRef(env, pointTypes); 276 pointTypes = NULL; 277 } 278 279 AWT_FONT_CLEANUP_FINISH; 280 JNF_COCOA_EXIT(env); 281 return generalPath; 282 } 283 284 /* 285 * Class: sun_font_CStrike 286 * Method: getGlyphImagePtrsNative 287 * Signature: (JJ[J[II)V 288 */ 289 JNIEXPORT void JNICALL 290 Java_sun_font_CStrike_getGlyphImagePtrsNative 291 (JNIEnv *env, jclass clazz, 292 jlong awtStrikePtr, jlongArray glyphInfoLongArray, 293 jintArray glyphCodes, jint len) 294 { 295 JNF_COCOA_ENTER(env); 296 297 AWTStrike *awtStrike = (AWTStrike *)jlong_to_ptr(awtStrikePtr); 298 299 jlong *glyphInfos = 300 (*env)->GetPrimitiveArrayCritical(env, glyphInfoLongArray, NULL); 301 jint *rawGlyphCodes = 302 (*env)->GetPrimitiveArrayCritical(env, glyphCodes, NULL); 303 304 CGGlyphImages_GetGlyphImagePtrs(glyphInfos, awtStrike, 305 rawGlyphCodes, len); 306 307 (*env)->ReleasePrimitiveArrayCritical(env, glyphCodes, 308 rawGlyphCodes, JNI_ABORT); 309 // Do not use JNI_COMMIT, as that will not free the buffer copy 310 // when +ProtectJavaHeap is on. 311 (*env)->ReleasePrimitiveArrayCritical(env, glyphInfoLongArray, 312 glyphInfos, 0); 313 314 JNF_COCOA_EXIT(env); 315 } 316 317 /* 318 * Class: sun_font_CStrike 319 * Method: createNativeStrikePtr 320 * Signature: (J[D[DII)J 321 */ 322 JNIEXPORT jlong JNICALL Java_sun_font_CStrike_createNativeStrikePtr 323 (JNIEnv *env, jclass clazz, jlong nativeFontPtr, jdoubleArray glyphTxArray, jdoubleArray invDevTxArray, jint aaStyle, jint fmHint) 324 { 325 AWTStrike *awtStrike = nil; 326 JNF_COCOA_ENTER(env); 327 328 AWTFont *awtFont = (AWTFont *)jlong_to_ptr(nativeFontPtr); 329 JRSFontRenderingStyle style = JRSFontGetRenderingStyleForHints(fmHint, aaStyle); 330 331 CGAffineTransform glyphTx = GetTxFromDoubles(env, glyphTxArray); 332 CGAffineTransform invDevTx = GetTxFromDoubles(env, invDevTxArray); 333 334 awtStrike = [AWTStrike awtStrikeForFont:awtFont tx:glyphTx invDevTx:invDevTx style:style aaStyle:aaStyle]; // autoreleased 335 336 if (awtStrike) 337 { 338 CFRetain(awtStrike); // GC 339 } 340 341 JNF_COCOA_EXIT(env); 342 return ptr_to_jlong(awtStrike); 343 } 344 345 /* 346 * Class: sun_font_CStrike 347 * Method: disposeNativeStrikePtr 348 * Signature: (J)V 349 */ 350 JNIEXPORT void JNICALL 351 Java_sun_font_CStrike_disposeNativeStrikePtr 352 (JNIEnv *env, jclass clazz, jlong awtStrike) 353 { 354 JNF_COCOA_ENTER(env); 355 356 if (awtStrike) { 357 CFRelease((AWTStrike *)jlong_to_ptr(awtStrike)); // GC 358 } 359 360 JNF_COCOA_EXIT(env); 361 } 362 363 /* 364 * Class: sun_font_CStrike 365 * Method: getFontMetrics 366 * Signature: (J)Lsun/font/StrikeMetrics; 367 */ 368 JNIEXPORT jobject JNICALL 369 Java_sun_font_CStrike_getFontMetrics 370 (JNIEnv *env, jclass clazz, jlong awtStrikePtr) 371 { 372 jobject metrics = NULL; 373 374 JNF_COCOA_ENTER(env); 375 AWT_FONT_CLEANUP_SETUP; 376 377 AWTFont *awtfont = ((AWTStrike *)jlong_to_ptr(awtStrikePtr))->fAWTFont; 378 AWT_FONT_CLEANUP_CHECK(awtfont); 379 380 CGFontRef cgFont = awtfont->fNativeCGFont; 381 382 jfloat ay=0.0, dy=0.0, mx=0.0, ly=0.0; 383 int unitsPerEm = CGFontGetUnitsPerEm(cgFont); 384 CGFloat scaleX = (1.0 / unitsPerEm); 385 CGFloat scaleY = (1.0 / unitsPerEm); 386 387 // Ascent 388 ay = -(CGFloat)CGFontGetAscent(cgFont) * scaleY; 389 390 // Descent 391 dy = -(CGFloat)CGFontGetDescent(cgFont) * scaleY; 392 393 // Leading 394 ly = (CGFloat)CGFontGetLeading(cgFont) * scaleY; 395 396 // Max Advance for Font Direction (Strictly horizontal) 397 mx = [awtfont->fFont maximumAdvancement].width; 398 399 /* 400 * ascent: no need to set ascentX - it will be zero. 401 * descent: no need to set descentX - it will be zero. 402 * baseline: old releases "made up" a number and also seemed to 403 * make it up for "X" and set "Y" to 0. 404 * leadingX: no need to set leadingX - it will be zero. 405 * leadingY: made-up number, but being compatible with what 1.4.x did. 406 * advance: no need to set yMaxLinearAdvanceWidth - it will be zero. 407 */ 408 409 JNF_CLASS_CACHE(sjc_StrikeMetrics, "sun/font/StrikeMetrics"); 410 JNF_CTOR_CACHE(strikeMetricsCtr, sjc_StrikeMetrics, "(FFFFFFFFFF)V"); 411 metrics = JNFNewObject(env, strikeMetricsCtr, 412 0.0, ay, 0.0, dy, 1.0, 413 0.0, 0.0, ly, mx, 0.0); 414 415 cleanup: 416 AWT_FONT_CLEANUP_FINISH; 417 JNF_COCOA_EXIT(env); 418 419 return metrics; 420 }