src/java.desktop/macosx/native/libawt_lwawt/font/AWTFont.m

Print this page




  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 
  39 #define DEBUG
  40 
  41 @implementation AWTFont
  42 
  43 - (id) initWithFont:(NSFont *)font isFakeItalic:(BOOL)isFakeItalic {
  44     self = [super init];
  45     if (self) {
  46         fIsFakeItalic = isFakeItalic;
  47         fFont = [font retain];
  48         fNativeCGFont = CTFontCopyGraphicsFont((CTFontRef)font, NULL);
  49     }
  50     return self;
  51 }
  52 
  53 - (void) dealloc {
  54     [fFont release];
  55     fFont = nil;
  56 
  57     if (fNativeCGFont) {
  58         CGFontRelease(fNativeCGFont);
  59     fNativeCGFont = NULL;
  60     }
  61 
  62     [super dealloc];
  63 }
  64 
  65 - (void) finalize {
  66     if (fNativeCGFont) {
  67         CGFontRelease(fNativeCGFont);
  68     fNativeCGFont = NULL;
  69     }
  70     [super finalize];
  71 }
  72 
  73 + (AWTFont *) awtFontForName:(NSString *)name
  74                        style:(int)style
  75                 isFakeItalic:(BOOL)isFakeItalic
  76 {
  77     // create font with family & size
  78     NSFont *nsFont = [NSFont fontWithName:name size:1.0];
  79 
  80     if (nsFont == nil) {
  81         // if can't get font of that name, substitute system default font
  82         nsFont = [NSFont fontWithName:@"Lucida Grande" size:1.0];
  83 #ifdef DEBUG
  84         NSLog(@"needed to substitute Lucida Grande for: %@", name);
  85 #endif
  86     }
  87 
  88     // create an italic style (if one is installed)
  89     if (style & java_awt_Font_ITALIC) {
  90         nsFont = [[NSFontManager sharedFontManager] convertFont:nsFont toHaveTrait:NSItalicFontMask];
  91     }
  92 
  93     // create a bold style (if one is installed)
  94     if (style & java_awt_Font_BOLD) {
  95         nsFont = [[NSFontManager sharedFontManager] convertFont:nsFont toHaveTrait:NSBoldFontMask];
  96     }
  97 
  98     return [[[AWTFont alloc] initWithFont:nsFont isFakeItalic:isFakeItalic] autorelease];
  99 }
 100 
 101 + (NSFont *) nsFontForJavaFont:(jobject)javaFont env:(JNIEnv *)env {
 102     if (javaFont == NULL) {
 103 #ifdef DEBUG
 104         NSLog(@"nil font");
 105 #endif
 106         return nil;
 107     }
 108 
 109     static JNF_CLASS_CACHE(jc_Font, "java/awt/Font");
 110 
 111     // obtain the Font2D
 112     static JNF_MEMBER_CACHE(jm_Font_getFont2D, jc_Font, "getFont2D", "()Lsun/font/Font2D;");
 113     jobject font2d = JNFCallObjectMethod(env, javaFont, jm_Font_getFont2D);
 114     if (font2d == NULL) {
 115 #ifdef DEBUG
 116         NSLog(@"nil font2d");
 117 #endif
 118         return nil;


 337         ATSFontContainerRef oContainer;
 338         status = ATSFontActivateFromFileReference(&iFile, kATSFontContextLocal,
 339                                                   kATSFontFormatUnspecified,
 340                                                   NULL, kNilOptions,
 341                                                   &oContainer);
 342     }
 343 
 344 JNF_COCOA_EXIT(env);
 345 }
 346 
 347 #pragma mark --- sun.font.CFont JNI ---
 348 
 349 /*
 350  * Class:     sun_font_CFont
 351  * Method:    initNativeFont
 352  * Signature: (Ljava/lang/String;I)J
 353  */
 354 JNIEXPORT jlong JNICALL
 355 Java_sun_font_CFont_createNativeFont
 356     (JNIEnv *env, jclass clazz,
 357      jstring nativeFontName, jint style, jboolean isFakeItalic)
 358 {
 359     AWTFont *awtFont = nil;
 360 
 361 JNF_COCOA_ENTER(env);
 362 
 363     awtFont =
 364         [AWTFont awtFontForName:JNFJavaToNSString(env, nativeFontName)
 365          style:style
 366          isFakeItalic:isFakeItalic]; // autoreleased
 367 
 368     if (awtFont) {
 369         CFRetain(awtFont); // GC
 370     }
 371 
 372 JNF_COCOA_EXIT(env);
 373 
 374     return ptr_to_jlong(awtFont);
 375 }
 376 
 377 /*
 378  * Class:     sun_font_CFont














































 379  * Method:    disposeNativeFont
 380  * Signature: (J)V
 381  */
 382 JNIEXPORT void JNICALL
 383 Java_sun_font_CFont_disposeNativeFont
 384     (JNIEnv *env, jclass clazz, jlong awtFontPtr)
 385 {
 386 JNF_COCOA_ENTER(env);
 387 
 388     if (awtFontPtr) {
 389         CFRelease((AWTFont *)jlong_to_ptr(awtFontPtr)); // GC
 390     }
 391 
 392 JNF_COCOA_EXIT(env);
 393 }
 394 
 395 
 396 #pragma mark --- Miscellaneous JNI ---
 397 
 398 #ifndef HEADLESS




  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;


 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