]> granicus.if.org Git - libass/commitdiff
fontselect: provide a way to use freetype to get font info
authorRodger Combs <rodger.combs@gmail.com>
Wed, 16 Jan 2019 00:17:57 +0000 (18:17 -0600)
committerOleg Oshmyan <chortos@inbox.lv>
Thu, 26 Sep 2019 13:51:09 +0000 (16:51 +0300)
libass/ass_fontselect.c
libass/ass_fontselect.h

index 85ff0dcbcff2a89fe42d42ac003f3f4f46a53239..651fc7a520afb3e435e09cdc3345265a077adc1c 100644 (file)
@@ -848,6 +848,47 @@ error:
     return false;
 }
 
+bool ass_get_font_info(ASS_Library *lib, FT_Library ftlib, const char *path,
+                       const char *postscript_name, int index,
+                       ASS_FontProviderMetaData *info)
+{
+    bool ret = false;
+    FT_Face face = NULL;
+    int error = FT_New_Face(ftlib, path, index, &face);
+    if (error) {
+        ass_msg(lib, MSGL_WARN, "Error opening font: '%s', %d", path, index);
+        return false;
+    }
+
+    if (postscript_name && index < 0 && face->num_faces > 0) {
+        // The font provider gave us a postscript name and is not sure
+        // about the face index.. so use the postscript name to find the
+        // correct face_index in the collection!
+        for (int i = 0; i < face->num_faces; i++) {
+            FT_Done_Face(face);
+            error = FT_New_Face(ftlib, path, i, &face);
+            if (error) {
+                ass_msg(lib, MSGL_WARN, "Error opening font: '%s', %d", path, i);
+                return false;
+            }
+
+            const char *face_psname = FT_Get_Postscript_Name(face);
+            if (face_psname != NULL &&
+                strcmp(face_psname, postscript_name) == 0)
+                break;
+        }
+    }
+
+    if (face) {
+        ret = get_font_info(ftlib, face, info);
+        if (ret)
+            info->postscript_name = strdup(info->postscript_name);
+        FT_Done_Face(face);
+    }
+
+    return ret;
+}
+
 /**
  * \brief Free the dynamically allocated fields of metadata
  * created by get_font_info.
index 9d56b5b29147d0558fd34cc0980e22e199574358..a239a94b7b25a096f17c87c87d3c094e6e8e5890 100644 (file)
@@ -266,6 +266,20 @@ ass_font_provider_add_font(ASS_FontProvider *provider,
                            ASS_FontProviderMetaData *meta, const char *path,
                            int index, void *data);
 
+/**
+ * \brief Read a font's parameters
+ * \param lib a FT_Library to use (need not be the global one)
+ * \param path the path to the font file to read
+ * \param postscript_name the PS name of the specific face to read (set either this or index)
+ * \param index the face index to read, or -1 if not applicable
+ * \param info the struct to store results into
+ * \return success
+ *
+ */
+bool ass_get_font_info(ASS_Library *lib, FT_Library ftlib, const char *path,
+                       const char *postscript_name, int index,
+                       ASS_FontProviderMetaData *info);
+
 /**
  * \brief Free font provider and associated fonts.
  * \param provider the font provider