1 /*
   2  * Copyright (c) 2011, 2013, 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 
  28 #import "java_awt_Font.h"
  29 #import "sun_awt_PlatformFont.h"
  30 #import "sun_awt_FontDescriptor.h"
  31 #import "sun_font_CFont.h"
  32 #import "sun_font_CFontManager.h"
  33 
  34 #import "AWTFont.h"
  35 #import "AWTStrike.h"
  36 #import "CoreTextSupport.h"
  37 
  38 @implementation AWTFont
  39 
  40 - (id) initWithFont:(NSFont *)font {
  41     self = [super init];
  42     if (self) {
  43         fFont = [font retain];
  44         fNativeCGFont = CTFontCopyGraphicsFont((CTFontRef)font, NULL);
  45     }
  46     return self;
  47 }
  48 
  49 - (void) dealloc {
  50     [fFont release];
  51     fFont = nil;
  52 
  53     if (fNativeCGFont) {
  54         CGFontRelease(fNativeCGFont);
  55     fNativeCGFont = NULL;
  56     }
  57 
  58     [super dealloc];
  59 }
  60 
  61 - (void) finalize {
  62     if (fNativeCGFont) {
  63         CGFontRelease(fNativeCGFont);
  64     fNativeCGFont = NULL;
  65     }
  66     [super finalize];
  67 }
  68 
  69 + (AWTFont *) awtFontForName:(NSString *)name
  70                        style:(int)style
  71 {
  72     // create font with family & size
  73     NSFont *nsFont = [NSFont fontWithName:name size:1.0];
  74 
  75     if (nsFont == nil) {
  76         // if can't get font of that name, substitute system default font
  77         nsFont = [NSFont fontWithName:@"Lucida Grande" size:1.0];
  78 #ifdef DEBUG
  79         NSLog(@"needed to substitute Lucida Grande for: %@", name);
  80 #endif
  81     }
  82 
  83     // create an italic style (if one is installed)
  84     if (style & java_awt_Font_ITALIC) {
  85         nsFont = [[NSFontManager sharedFontManager] convertFont:nsFont toHaveTrait:NSItalicFontMask];
  86     }
  87 
  88     // create a bold style (if one is installed)
  89     if (style & java_awt_Font_BOLD) {
  90         nsFont = [[NSFontManager sharedFontManager] convertFont:nsFont toHaveTrait:NSBoldFontMask];
  91     }
  92 
  93     return [[[AWTFont alloc] initWithFont:nsFont] autorelease];
  94 }
  95 
  96 + (NSFont *) nsFontForJavaFont:(jobject)javaFont env:(JNIEnv *)env {
  97     if (javaFont == NULL) {
  98 #ifdef DEBUG
  99         NSLog(@"nil font");
 100 #endif
 101         return nil;
 102     }
 103 
 104     static JNF_CLASS_CACHE(jc_Font, "java/awt/Font");
 105 
 106     // obtain the Font2D
 107     static JNF_MEMBER_CACHE(jm_Font_getFont2D, jc_Font, "getFont2D", "()Lsun/font/Font2D;");
 108     jobject font2d = JNFCallObjectMethod(env, javaFont, jm_Font_getFont2D);
 109     if (font2d == NULL) {
 110 #ifdef DEBUG
 111         NSLog(@"nil font2d");
 112 #endif
 113         return nil;
 114     }
 115 
 116     // if it's not a CFont, it's likely one of TTF or OTF fonts
 117     // from the Sun rendering loops
 118     static JNF_CLASS_CACHE(jc_CFont, "sun/font/CFont");
 119     if (!JNFIsInstanceOf(env, font2d, &jc_CFont)) {
 120 #ifdef DEBUG
 121         NSLog(@"font2d !instanceof CFont");
 122 #endif
 123         return nil;
 124     }
 125 
 126     static JNF_MEMBER_CACHE(jm_CFont_getFontStrike, jc_CFont, "getStrike", "(Ljava/awt/Font;)Lsun/font/FontStrike;");
 127     jobject fontStrike = JNFCallObjectMethod(env, font2d, jm_CFont_getFontStrike, javaFont);
 128 
 129     static JNF_CLASS_CACHE(jc_CStrike, "sun/font/CStrike");
 130     if (!JNFIsInstanceOf(env, fontStrike, &jc_CStrike)) {
 131 #ifdef DEBUG
 132         NSLog(@"fontStrike !instanceof CStrike");
 133 #endif
 134         return nil;
 135     }
 136 
 137     static JNF_MEMBER_CACHE(jm_CStrike_nativeStrikePtr, jc_CStrike, "getNativeStrikePtr", "()J");
 138     jlong awtStrikePtr = JNFCallLongMethod(env, fontStrike, jm_CStrike_nativeStrikePtr);
 139     if (awtStrikePtr == 0L) {
 140 #ifdef DEBUG
 141         NSLog(@"nil nativeFontPtr from CFont");
 142 #endif
 143         return nil;
 144     }
 145 
 146     AWTStrike *strike = (AWTStrike *)jlong_to_ptr(awtStrikePtr);
 147 
 148     return [NSFont fontWithName:[strike->fAWTFont->fFont fontName] matrix:(CGFloat *)(&(strike->fAltTx))];
 149 }
 150 
 151 @end
 152 
 153 
 154 #pragma mark --- Font Discovery and Loading ---
 155 
 156 static NSArray* sFilteredFonts = nil;
 157 static NSDictionary* sFontFamilyTable = nil;
 158 
 159 static NSString*
 160 GetFamilyNameForFontName(NSString* fontname)
 161 {
 162     return [sFontFamilyTable objectForKey:fontname];
 163 }
 164 
 165 static NSArray*
 166 GetFilteredFonts()
 167 {
 168     if (sFilteredFonts == nil) {
 169         NSFontManager *fontManager = [NSFontManager sharedFontManager];
 170         NSUInteger fontCount = [[fontManager availableFonts] count];
 171 
 172         NSMutableArray *allFonts = [[NSMutableArray alloc] initWithCapacity:fontCount];
 173         NSMutableDictionary* fontFamilyTable = [[NSMutableDictionary alloc] initWithCapacity:fontCount];
 174         NSArray *allFamilies = [fontManager availableFontFamilies];
 175 
 176         NSUInteger familyCount = [allFamilies count];
 177 
 178         NSUInteger familyIndex;
 179         for (familyIndex = 0; familyIndex < familyCount; familyIndex++) {
 180             NSString *family = [allFamilies objectAtIndex:familyIndex];
 181 
 182             if ((family == nil) || [family characterAtIndex:0] == '.') {
 183                 continue;
 184             }
 185 
 186             NSArray *fontFaces = [fontManager availableMembersOfFontFamily:family];
 187             NSUInteger faceCount = [fontFaces count];
 188 
 189             NSUInteger faceIndex;
 190             for (faceIndex = 0; faceIndex < faceCount; faceIndex++) {
 191                 NSString* face = [[fontFaces objectAtIndex:faceIndex] objectAtIndex:0];
 192                 if (face != nil) {
 193                     [allFonts addObject:face];
 194                     [fontFamilyTable setObject:family forKey:face];
 195                 }
 196             }
 197         }
 198 
 199         sFilteredFonts = allFonts;
 200         sFontFamilyTable = fontFamilyTable;
 201     }
 202 
 203     return sFilteredFonts;
 204 }
 205 
 206 #pragma mark --- sun.font.CFontManager JNI ---
 207 
 208 static OSStatus CreateFSRef(FSRef *myFSRefPtr, NSString *inPath)
 209 {
 210     return FSPathMakeRef((UInt8 *)[inPath fileSystemRepresentation],
 211                          myFSRefPtr, NULL);
 212 }
 213 
 214 // /*
 215 //  * Class:     sun_font_CFontManager
 216 //  * Method:    loadFileFont
 217 //  * Signature: (Ljava/lang/String;)Lsun/font/Font2D;
 218 //  */
 219 // JNIEXPORT /* sun.font.CFont */ jobject JNICALL
 220 // Java_sun_font_CFontManager_loadFileFont
 221 //     (JNIEnv *env, jclass obj, jstring fontpath)
 222 // {
 223 //     jobject result = NULL;
 224 //
 225 // JNF_COCOA_ENTER(env);
 226 //
 227 //     NSString *nsFilePath = JNFJavaToNSString(env, fontpath);
 228 //     jstring javaFontName = NULL;
 229 //
 230 //     //
 231 //     // Note: This API uses ATS and can therefore return Carbon error codes.
 232 //     // These codes can be found at:
 233 //     // http://developer.apple.com/techpubs/macosx/Carbon/Files/FileManager/File_Manager/ResultCodes/ResultCodes.html
 234 //     //
 235 //
 236 //     FSRef iFile;
 237 //     OSStatus status = CreateFSRef(&iFile, nsFilePath);
 238 //
 239 //     if (status == noErr) {
 240 //         ATSFontContainerRef oContainer;
 241 //         status = ATSFontActivateFromFileReference(&iFile, kATSFontContextLocal,
 242 //                                                   kATSFontFormatUnspecified,
 243 //                                                   NULL,
 244 //                                                   kATSOptionFlagsUseDataFork,
 245 //                                                   &oContainer);
 246 //         if (status == noErr) {
 247 //             ATSFontRef ioArray[1];
 248 //             ItemCount oCount;
 249 //             status = ATSFontFindFromContainer(oContainer,
 250 //                                               kATSOptionFlagsUseDataFork,
 251 //                                               1, ioArray, &oCount);
 252 //
 253 //             if (status == noErr) {
 254 //                 CFStringRef oName;
 255 //                 status = ATSFontGetPostScriptName(ioArray[0],
 256 //                                                   kATSOptionFlagsUseDataFork,
 257 //                                                   &oName);
 258 //                 if (status == noErr) {
 259 //                     javaFontName = JNFNSToJavaString(env, (NSString *)oName);
 260 //                     CFRelease(oName);
 261 //                 }
 262 //             }
 263 //         }
 264 //     }
 265 //
 266 //     if (javaFontName != NULL) {
 267 //         // create the CFont!
 268 //         static JNF_CLASS_CACHE(sjc_CFont, "sun/font/CFont");
 269 //         static JNF_CTOR_CACHE(sjf_CFont_ctor,
 270 //                               sjc_CFont, "(Ljava/lang/String;)V");
 271 //         result = JNFNewObject(env, sjf_CFont_ctor, javaFontName);
 272 //     }
 273 //
 274 // JNF_COCOA_EXIT(env);
 275 //
 276 //     return result;
 277 // }
 278 
 279 /*
 280  * Class:     sun_font_CFontManager
 281  * Method:    loadNativeFonts
 282  * Signature: ()V
 283  */
 284 JNIEXPORT void JNICALL
 285 Java_sun_font_CFontManager_loadNativeFonts
 286     (JNIEnv *env, jobject jthis)
 287 {
 288     static JNF_CLASS_CACHE(jc_CFontManager,
 289                            "sun/font/CFontManager");
 290     static JNF_MEMBER_CACHE(jm_registerFont, jc_CFontManager,
 291                             "registerFont",
 292                             "(Ljava/lang/String;Ljava/lang/String;)V");
 293 
 294     jint num = 0;
 295 
 296 JNF_COCOA_ENTER(env);
 297 
 298     NSArray *filteredFonts = GetFilteredFonts();
 299     num = (jint)[filteredFonts count];
 300 
 301     jint i;
 302     for (i = 0; i < num; i++) {
 303         NSString *fontname = [filteredFonts objectAtIndex:i];
 304         jobject jFontName = JNFNSToJavaString(env, fontname);
 305         jobject jFontFamilyName =
 306             JNFNSToJavaString(env, GetFamilyNameForFontName(fontname));
 307 
 308         JNFCallVoidMethod(env, jthis,
 309                           jm_registerFont, jFontName, jFontFamilyName);
 310     }
 311 
 312 JNF_COCOA_EXIT(env);
 313 }
 314 
 315 /*
 316  * Class:     Java_sun_font_CFontManager_loadNativeDirFonts
 317  * Method:    loadNativeDirFonts
 318  * Signature: (Ljava/lang/String;)V;
 319  */
 320 JNIEXPORT void JNICALL
 321 Java_sun_font_CFontManager_loadNativeDirFonts
 322 (JNIEnv *env, jclass clz, jstring filename)
 323 {
 324 JNF_COCOA_ENTER(env);
 325 
 326     NSString *nsFilePath = JNFJavaToNSString(env, filename);
 327 
 328     FSRef iFile;
 329     OSStatus status = CreateFSRef(&iFile, nsFilePath);
 330 
 331     if (status == noErr) {
 332         ATSFontContainerRef oContainer;
 333         status = ATSFontActivateFromFileReference(&iFile, kATSFontContextLocal,
 334                                                   kATSFontFormatUnspecified,
 335                                                   NULL, kNilOptions,
 336                                                   &oContainer);
 337     }
 338 
 339 JNF_COCOA_EXIT(env);
 340 }
 341 
 342 #pragma mark --- sun.font.CFont JNI ---
 343 
 344 /*
 345  * Class:     sun_font_CFont
 346  * Method:    initNativeFont
 347  * Signature: (Ljava/lang/String;I)J
 348  */
 349 JNIEXPORT jlong JNICALL
 350 Java_sun_font_CFont_createNativeFont
 351     (JNIEnv *env, jclass clazz,
 352      jstring nativeFontName, jint style)
 353 {
 354     AWTFont *awtFont = nil;
 355 
 356 JNF_COCOA_ENTER(env);
 357 
 358     awtFont =
 359         [AWTFont awtFontForName:JNFJavaToNSString(env, nativeFontName)
 360          style:style]; // autoreleased
 361 
 362     if (awtFont) {
 363         CFRetain(awtFont); // GC
 364     }
 365 
 366 JNF_COCOA_EXIT(env);
 367 
 368     return ptr_to_jlong(awtFont);
 369 }
 370 
 371 /*
 372  * Class:     sun_font_CFont
 373  * Method:    getWidthNative
 374  * Signature: (J)F
 375  */
 376 JNIEXPORT jfloat JNICALL
 377 Java_sun_font_CFont_getWidthNative
 378     (JNIEnv *env, jobject cfont, jlong awtFontPtr)
 379 {
 380     float widthVal;
 381 JNF_COCOA_ENTER(env);
 382 
 383     AWTFont *awtFont = (AWTFont *)jlong_to_ptr(awtFontPtr);
 384     NSFont* nsFont = awtFont->fFont;
 385     NSFontDescriptor *fontDescriptor = nsFont.fontDescriptor;
 386     NSDictionary *fontTraits = [fontDescriptor objectForKey : NSFontTraitsAttribute];
 387     NSNumber *width = [fontTraits objectForKey : NSFontWidthTrait];
 388     widthVal = (float)[width floatValue];
 389 
 390 JNF_COCOA_EXIT(env);
 391    return (jfloat)widthVal;
 392 }
 393 
 394 /*
 395  * Class:     sun_font_CFont
 396  * Method:    getWeightNative
 397  * Signature: (J)F
 398  */
 399 JNIEXPORT jfloat JNICALL
 400 Java_sun_font_CFont_getWeightNative
 401     (JNIEnv *env, jobject cfont, jlong awtFontPtr)
 402 {
 403     float weightVal;
 404 JNF_COCOA_ENTER(env);
 405 
 406     AWTFont *awtFont = (AWTFont *)jlong_to_ptr(awtFontPtr);
 407     NSFont* nsFont = awtFont->fFont;
 408     NSFontDescriptor *fontDescriptor = nsFont.fontDescriptor;
 409     NSDictionary *fontTraits = [fontDescriptor objectForKey : NSFontTraitsAttribute];
 410     NSNumber *weight = [fontTraits objectForKey : NSFontWeightTrait];
 411     weightVal = (float)[weight floatValue];
 412 
 413 JNF_COCOA_EXIT(env);
 414    return (jfloat)weightVal;
 415 }
 416 
 417 /*
 418  * Class:     sun_font_CFont
 419  * Method:    disposeNativeFont
 420  * Signature: (J)V
 421  */
 422 JNIEXPORT void JNICALL
 423 Java_sun_font_CFont_disposeNativeFont
 424     (JNIEnv *env, jclass clazz, jlong awtFontPtr)
 425 {
 426 JNF_COCOA_ENTER(env);
 427 
 428     if (awtFontPtr) {
 429         CFRelease((AWTFont *)jlong_to_ptr(awtFontPtr)); // GC
 430     }
 431 
 432 JNF_COCOA_EXIT(env);
 433 }
 434 
 435 
 436 #pragma mark --- Miscellaneous JNI ---
 437 
 438 #ifndef HEADLESS
 439 /*
 440  * Class:     sun_awt_PlatformFont
 441  * Method:    initIDs
 442  * Signature: ()V
 443  */
 444 JNIEXPORT void JNICALL
 445 Java_sun_awt_PlatformFont_initIDs
 446     (JNIEnv *env, jclass cls)
 447 {
 448 }
 449 
 450 /*
 451  * Class:     sun_awt_FontDescriptor
 452  * Method:    initIDs
 453  * Signature: ()V
 454  */
 455 JNIEXPORT void JNICALL
 456 Java_sun_awt_FontDescriptor_initIDs
 457     (JNIEnv *env, jclass cls)
 458 {
 459 }
 460 #endif