changeset 6948:6d2f01663378

[Font, Linux] fix code format, remove tab from pango.c, rename vars, improve error checking
author Felipe Heidrich <felipe.heidrich@oracle.com>
date Wed, 30 Apr 2014 12:10:02 -0700
parents 084a6d5b5416
children a49dfbd3235c
files modules/graphics/src/main/java/com/sun/javafx/font/freetype/PangoGlyphLayout.java modules/graphics/src/main/native-font/pango.c
diffstat 2 files changed, 53 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/modules/graphics/src/main/java/com/sun/javafx/font/freetype/PangoGlyphLayout.java	Wed Apr 30 11:22:22 2014 -0700
+++ b/modules/graphics/src/main/java/com/sun/javafx/font/freetype/PangoGlyphLayout.java	Wed Apr 30 12:10:02 2014 -0700
@@ -63,6 +63,19 @@
         return slot;
     }
 
+    private boolean check(long checkValue, String message, long fontmap, long context, long desc, long attrList) {
+        if (checkValue != 0) return false;
+        if (message != null && PrismFontFactory.debugFonts) {
+            System.err.println(message);
+        }
+        /* pango_attr_list_unref() also frees the attributes it contains */
+        if (attrList != 0) OSPango.pango_attr_list_unref(attrList);
+        if (desc != 0) OSPango.pango_font_description_free(desc);
+        if (context != 0) OSPango.g_object_unref(context);
+        if (fontmap != 0) OSPango.g_object_unref(fontmap);
+        return true;
+    }
+
     private long str = 0L;
     public void layout(TextRun run, PGFont font, FontStrike strike, char[] text) {
 
@@ -73,18 +86,33 @@
             fr = ((CompositeFontResource)fr).getSlotResource(0);
         }
         long fontmap = OSPango.pango_ft2_font_map_new();
+        if (check(fontmap, "Failed allocating PangoFontMap.", 0, 0, 0, 0)) {
+            return;
+        }
         long context = OSPango.pango_font_map_create_context(fontmap);
+        if (check(context, "Failed allocating PangoContext.", fontmap, 0, 0, 0)) {
+            return;
+        }
         float size = font.getSize();
         int style = fr.isItalic() ? OSPango.PANGO_STYLE_ITALIC : OSPango.PANGO_STYLE_NORMAL;
         int weight = fr.isBold() ? OSPango.PANGO_WEIGHT_BOLD : OSPango.PANGO_WEIGHT_NORMAL;
         long desc = OSPango.pango_font_description_new();
+        if (check(desc, "Failed allocating FontDescription.", fontmap, context, 0, 0)) {
+            return;
+        }
         OSPango.pango_font_description_set_family(desc, fr.getFamilyName());
         OSPango.pango_font_description_set_absolute_size(desc, size * OSPango.PANGO_SCALE);
         OSPango.pango_font_description_set_stretch(desc, OSPango.PANGO_STRETCH_NORMAL);
         OSPango.pango_font_description_set_style(desc, style);
         OSPango.pango_font_description_set_weight(desc, weight);
         long attrList = OSPango.pango_attr_list_new();
+        if (check(attrList, "Failed allocating PangoAttributeList.", fontmap, context, desc, 0)) {
+            return;
+        }
         long attr = OSPango.pango_attr_font_desc_new(desc);
+        if (check(attr, "Failed allocating PangoAttribute.", fontmap, context, desc, attrList)) {
+            return;
+        }
         OSPango.pango_attr_list_insert(attrList, attr);
         if (!composite) {
             attr = OSPango.pango_attr_fallback_new(false);
@@ -93,10 +121,7 @@
 
         if (str == 0L) {
             str = OSPango.g_utf16_to_utf8(text);
-            if (str == 0L) {
-                if (PrismFontFactory.debugFonts) {
-                    System.err.println("Failed allocating UTF-8 buffer.");
-                }
+            if (check(str, "Failed allocating UTF-8 buffer.", fontmap, context, desc, attrList)) {
                 return;
             }
         }
@@ -162,11 +187,7 @@
             run.shape(glyphCount, glyphs, pos, indices);
         }
 
-        /* pango_attr_list_unref() also frees the attributes it contains */
-        OSPango.pango_attr_list_unref(attrList);
-        OSPango.pango_font_description_free(desc);
-        OSPango.g_object_unref(context);
-        OSPango.g_object_unref(fontmap);
+        check(0, null, fontmap, context, desc, attrList);
     }
 
     @Override
--- a/modules/graphics/src/main/native-font/pango.c	Wed Apr 30 11:22:22 2014 -0700
+++ b/modules/graphics/src/main/native-font/pango.c	Wed Apr 30 12:10:02 2014 -0700
@@ -129,13 +129,16 @@
 JNIEXPORT jobject JNICALL OS_NATIVE(pango_1shape)
     (JNIEnv *env, jclass that, jlong str, jlong pangoItem)
 {
+    if (!str) return NULL;
     if (!pangoItem) return NULL;
     PangoItem *item = (PangoItem *)pangoItem;
     PangoAnalysis analysis = item->analysis;
+    if (!pangoItem) return NULL;
     const gchar *text= (const gchar *)(str + item->offset);
     PangoGlyphString *glyphString = pango_glyph_string_new();
     if (!glyphString) return NULL;
 
+    jobject result = NULL;
     pango_shape(text, item->length, &analysis, glyphString);
     int count = glyphString->num_glyphs;
     if(count == 0) goto fail;
@@ -143,30 +146,29 @@
     jintArray glyphsArray = (*env)->NewIntArray(env, count);
     jintArray widthsArray = (*env)->NewIntArray(env, count);
     jintArray clusterArray = (*env)->NewIntArray(env, count);
-    jobject result = NULL;
     if (glyphsArray && widthsArray && clusterArray) {
-    	jint glyphs[count];
-    	jint widths[count];
+        jint glyphs[count];
+        jint widths[count];
         jint cluster[count];
         int i;
         for (i = 0; i < count; i++) {
-        	glyphs[i] = glyphString->glyphs[i].glyph;
-        	widths[i] = glyphString->glyphs[i].geometry.width;
+            glyphs[i] = glyphString->glyphs[i].glyph;
+            widths[i] = glyphString->glyphs[i].geometry.width;
             /* translate byte index to char index */
             cluster[i] = (jint)g_utf8_pointer_to_offset(text, text + glyphString->log_clusters[i]);
         }
         (*env)->SetIntArrayRegion(env, glyphsArray, 0, count, glyphs); 
-        if ((*env)->ExceptionOccurred(env)) {   
+        if ((*env)->ExceptionOccurred(env)) {
             fprintf(stderr, "OS_NATIVE error: JNI exception");
             goto fail;
         }
         (*env)->SetIntArrayRegion(env, widthsArray, 0, count, widths);
-        if ((*env)->ExceptionOccurred(env)) {   
+        if ((*env)->ExceptionOccurred(env)) {
             fprintf(stderr, "OS_NATIVE error: JNI exception");
             goto fail;
         }
         (*env)->SetIntArrayRegion(env, clusterArray, 0, count, cluster);
-        if ((*env)->ExceptionOccurred(env)) {   
+        if ((*env)->ExceptionOccurred(env)) {
             fprintf(stderr, "OS_NATIVE error: JNI exception");
             goto fail;
         }
@@ -359,25 +361,28 @@
 }
 
 JNIEXPORT jlong JNICALL OS_NATIVE(g_1utf8_1offset_1to_1pointer)
-    (JNIEnv *env, jclass that, jlong arg0, jlong arg1)
+    (JNIEnv *env, jclass that, jlong str, jlong offset)
 {
-    return (jlong)g_utf8_offset_to_pointer((const gchar *)arg0, (glong)arg1);
+    if (!str) return 0;
+    return (jlong)g_utf8_offset_to_pointer((const gchar *)str, (glong)offset);
 }
 
 JNIEXPORT jlong JNICALL OS_NATIVE(g_1utf8_1pointer_1to_1offset)
-    (JNIEnv *env, jclass that, jlong arg0, jlong arg1)
+    (JNIEnv *env, jclass that, jlong str, jlong pos)
 {
-    return (jlong)g_utf8_pointer_to_offset((const gchar *)arg0, (const gchar *)arg1);
+    if (!str) return 0;
+    return (jlong)g_utf8_pointer_to_offset((const gchar *)str, (const gchar *)pos);
 }
 
 JNIEXPORT jlong JNICALL OS_NATIVE(g_1utf16_1to_1utf8)
-    (JNIEnv *env, jclass that, jcharArray arg0)
+    (JNIEnv *env, jclass that, jcharArray str)
 {
-    jsize length = (*env)->GetArrayLength(env, arg0);
-    void *ch = (*env)->GetPrimitiveArrayCritical(env, arg0, 0);
-    jlong str = (jlong)g_utf16_to_utf8((const gunichar2 *)ch, length, NULL, NULL, NULL);
-    (*env)->ReleasePrimitiveArrayCritical(env, arg0, ch, 0);
-    return str;
+    if (!str) return 0;
+    jsize length = (*env)->GetArrayLength(env, str);
+    void *ch = (*env)->GetPrimitiveArrayCritical(env, str, 0);
+    jlong result = (jlong)g_utf16_to_utf8((const gunichar2 *)ch, length, NULL, NULL, NULL);
+    (*env)->ReleasePrimitiveArrayCritical(env, str, ch, 0);
+    return result;
 }
 
 JNIEXPORT void JNICALL OS_NATIVE(g_1free)