changeset 6930:686ac8b65d29

RT-34884: Using Helvetica font on Mac outputs "CoreText performance note"
author Felipe Heidrich <felipe.heidrich@oracle.com>
date Thu, 01 May 2014 09:06:27 -0700
parents 295b5b61e9c9
children 9e934742a081
files modules/graphics/src/main/native-font/dfontdecoder.c
diffstat 1 files changed, 29 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/modules/graphics/src/main/native-font/dfontdecoder.c	Thu May 01 10:37:16 2014 -0400
+++ b/modules/graphics/src/main/native-font/dfontdecoder.c	Thu May 01 09:06:27 2014 -0700
@@ -44,13 +44,38 @@
 JNIEXPORT jlong JNICALL Java_com_sun_javafx_font_DFontDecoder_createCTFont
 (JNIEnv *env, jclass clazz, jstring fontName)
 {
+    if (fontName == NULL) return 0l;
     CFIndex numChars = (*env)->GetStringLength(env, fontName);
     const jchar *fontBuffer = (*env)->GetStringChars(env, fontName, NULL);
-    CFStringRef fontNameRef = CFStringCreateWithCharacters(kCFAllocatorDefault, 
-                                                           fontBuffer, 
+    if (fontBuffer == NULL) return 0l;
+    CFStringRef fontNameRef = CFStringCreateWithCharacters(kCFAllocatorDefault,
+                                                           fontBuffer,
                                                            numChars);
-    
-    CTFontRef fontRef = CTFontCreateWithName(fontNameRef, 0, NULL);
+    (*env)->ReleaseStringChars(env, fontName, fontBuffer);
+
+    CTFontDescriptorRef descriptor = NULL;
+    CTFontCollectionRef collection = CTFontCollectionCreateFromAvailableFonts(NULL);
+    CFArrayRef fonts = CTFontCollectionCreateMatchingFontDescriptors(collection);
+    CFRelease(collection);
+    CFIndex count = CFArrayGetCount(fonts);
+    for (CFIndex i = 0; i < count && descriptor == NULL; i++) {
+        CTFontDescriptorRef fd = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fonts, i);
+        if (fd) {
+            CFStringRef fdNameRef = CTFontDescriptorCopyAttribute(fd, kCTFontDisplayNameAttribute);
+            if (fdNameRef) {
+                if (CFStringCompare(fdNameRef, fontNameRef, 0) == kCFCompareEqualTo) {
+                    descriptor = fd;
+                }
+                CFRelease(fdNameRef);
+            }
+        }
+    }
+
+    CTFontRef fontRef = NULL;
+    if (descriptor) {
+        fontRef = CTFontCreateWithFontDescriptor(descriptor, 0, NULL);
+    }
+    CFRelease(fonts);
     CFRelease(fontNameRef);
     return (jlong)fontRef;
 }