[libbluray-devel] Get font family and style from font file. Fixes loading fonts from streams.

hpi1 git at videolan.org
Sun Nov 23 13:19:56 CET 2014


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Fri Nov 21 13:21:16 2014 +0200| [a6ac65861a3817f8f53d358c2836df1dae5f4f37] | committer: hpi1

Get font family and style from font file. Fixes loading fonts from streams.

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=a6ac65861a3817f8f53d358c2836df1dae5f4f37
---

 src/libbluray/bdj/java/java/awt/BDFontMetrics.java |    5 +++
 src/libbluray/bdj/java/java/awt/Font.java          |   45 +++++++++++++++++++-
 src/libbluray/bdj/native/java_awt_BDFontMetrics.c  |   43 +++++++++++++++++++
 3 files changed, 92 insertions(+), 1 deletion(-)

diff --git a/src/libbluray/bdj/java/java/awt/BDFontMetrics.java b/src/libbluray/bdj/java/java/awt/BDFontMetrics.java
index 5c2492d..2a2f9d5 100644
--- a/src/libbluray/bdj/java/java/awt/BDFontMetrics.java
+++ b/src/libbluray/bdj/java/java/awt/BDFontMetrics.java
@@ -37,6 +37,11 @@ public class BDFontMetrics extends FontMetrics {
 
     private static native long initN();
     private static native void destroyN(long ftLib);
+    private static native String[] getFontFamilyAndStyleN(long ftLib, String fontName);
+
+    protected synchronized static String[] getFontFamilyAndStyle(String fontFile) {
+        return getFontFamilyAndStyleN(ftLib, fontFile);
+    }
 
     private native static String resolveFontN(String fontFamily, int fontStyle);
     private native static void   unloadFontConfigN();
diff --git a/src/libbluray/bdj/java/java/awt/Font.java b/src/libbluray/bdj/java/java/awt/Font.java
index e97b9b1..4bf3042 100644
--- a/src/libbluray/bdj/java/java/awt/Font.java
+++ b/src/libbluray/bdj/java/java/awt/Font.java
@@ -178,7 +178,20 @@ public class Font implements java.io.Serializable {
         if (type != TRUETYPE_FONT) {
             throw new FontFormatException("unsupported font format");
         }
-        return new Font(null, -1, 1, fontFile, null);
+
+        if (fontFile == null) {
+            throw new NullPointerException("fontFile is null");
+        }
+
+        String data[] = BDFontMetrics.getFontFamilyAndStyle(fontFile.getPath());
+        if (data == null || data.length < 2) {
+            throw new FontFormatException("error loading font " + fontFile.getPath());
+        }
+
+        String family = data[0];
+        int    style  = parseStyle(data[1]);
+
+        return new Font(family, style, 1, fontFile, family);
     }
 
     /* used by org.dvb.ui.FontFacrtory */
@@ -215,6 +228,36 @@ public class Font implements java.io.Serializable {
         setFamily();
     }
 
+    private static void parseStyle(String styleName) {
+        int style = PLAIN;
+
+        if (styleName != null && styleName.length() > 0) {
+            String[] styles = org.videolan.StrUtil.split(styleName, ' ');
+            if (styles.length == 1) {
+                styles = org.videolan.StrUtil.split(styles[0], ',');
+            }
+
+            for (int i = 0; i < styles.length; i++) {
+                styleName = styles[i].toLowerCase();
+                if (styleName.equals("bolditalic")) {
+                    style |= BOLD | ITALIC;
+                } else if (styleName.equals("italic")) {
+                    style |= ITALIC;
+                } else if (styleName.equals("bold")) {
+                    style |= BOLD;
+                } else if (styleName.equals("plain")) {
+                } else if (styleName.equals("serif")) {
+                } else if (styleName.equals("regular")) {
+                } else if (styleName.equals("roman")) {
+                } else {
+                    org.videolan.Logger.getLogger("Font").info("unregonized style: " + styleName);
+                }
+            }
+        }
+
+        return style;
+    }
+
     private void setFamily() {
         String[] names = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
         if (names.length == 0) {
diff --git a/src/libbluray/bdj/native/java_awt_BDFontMetrics.c b/src/libbluray/bdj/native/java_awt_BDFontMetrics.c
index 269f749..cfc256c 100644
--- a/src/libbluray/bdj/native/java_awt_BDFontMetrics.c
+++ b/src/libbluray/bdj/native/java_awt_BDFontMetrics.c
@@ -23,6 +23,8 @@
 
 #include <jni.h>
 
+#include "util.h"
+
 #include "util/logging.h"
 
 #ifdef HAVE_FT2
@@ -184,6 +186,41 @@ Java_java_awt_BDFontMetrics_destroyN(JNIEnv * env, jclass cls, jlong ftLib)
 #endif
 }
 
+JNIEXPORT jobjectArray JNICALL
+Java_java_awt_BDFontMetrics_getFontFamilyAndStyleN(JNIEnv * env, jclass cls, jlong ftLib, jstring fontName)
+{
+    jobjectArray array = bdj_make_array(env, "java/lang/String", 2);
+
+#ifdef HAVE_FT2
+    const char *name;
+    FT_Face ftFace;
+    FT_Error result;
+    FT_Library lib = (FT_Library)(intptr_t)ftLib;
+    jstring jfamily, jstyle;
+
+    if (!lib) {
+        return NULL;
+    }
+
+    name = (*env)->GetStringUTFChars(env, fontName, NULL);
+    result = FT_New_Face(lib, name, 0, &ftFace);
+    (*env)->ReleaseStringUTFChars(env, fontName, name);
+    if (result) {
+        return NULL;
+    }
+
+    jfamily = (*env)->NewStringUTF(env, ftFace->family_name);
+    jstyle  = (*env)->NewStringUTF(env, ftFace->style_name);
+
+    FT_Done_Face(ftFace);
+
+    (*env)->SetObjectArrayElement(env, array, 0, jfamily);
+    (*env)->SetObjectArrayElement(env, array, 1, jstyle);
+#endif
+
+    return array;
+}
+
 JNIEXPORT jlong JNICALL
 Java_java_awt_BDFontMetrics_loadFontN(JNIEnv * env, jobject obj, jlong ftLib, jstring fontName, jint size)
 {
@@ -204,6 +241,7 @@ Java_java_awt_BDFontMetrics_loadFontN(JNIEnv * env, jobject obj, jlong ftLib, js
     (*env)->ReleaseStringUTFChars(env, fontName, name);
     if (result)
         return 0;
+
     FT_Set_Char_Size(ftFace, 0, size << 6, 0, 0);
 
     cls = (*env)->GetObjectClass(env, obj);
@@ -353,6 +391,11 @@ Java_java_awt_BDFontMetrics_methods[] =
         VC(Java_java_awt_BDFontMetrics_unloadFontConfigN),
     },
     {
+        CC("getFontFamilyAndStyleN"),
+        CC("(JLjava/lang/String;)[Ljava/lang/String;"),
+        VC(Java_java_awt_BDFontMetrics_getFontFamilyAndStyleN),
+    },
+    {
         CC("loadFontN"),
         CC("(JLjava/lang/String;I)J"),
         VC(Java_java_awt_BDFontMetrics_loadFontN),



More information about the libbluray-devel mailing list