summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOleg Oshmyan <chortos@inbox.lv>2021-06-17 16:33:57 +0300
committerOleg Oshmyan <chortos@inbox.lv>2021-07-10 02:36:31 +0300
commita7f67df5f96f03fab6661d90d716c16e0fab4a21 (patch)
tree3329ce6a73bff4a352f04a621621e2fa71cf05ff
parent677e8e3ca75e25163bfd5c217d9d4c1de09bd242 (diff)
downloadlibass-a7f67df5f96f03fab6661d90d716c16e0fab4a21.tar.bz2
libass-a7f67df5f96f03fab6661d90d716c16e0fab4a21.tar.xz
font, fontselect: factor out common code for creating FT_Face
-rw-r--r--libass/ass_font.c121
-rw-r--r--libass/ass_font.h5
-rw-r--r--libass/ass_fontselect.c37
-rw-r--r--libass/ass_fontselect.h3
4 files changed, 84 insertions, 82 deletions
diff --git a/libass/ass_font.c b/libass/ass_font.c
index 7c1e76d..55514b5 100644
--- a/libass/ass_font.c
+++ b/libass/ass_font.c
@@ -130,6 +130,38 @@ static void set_font_metrics(FT_Face face)
}
}
+FT_Face ass_face_open(ASS_Library *lib, FT_Library ftlib, const char *path,
+ const char *postscript_name, int index)
+{
+ FT_Face face;
+ int error = FT_New_Face(ftlib, path, index, &face);
+ if (error) {
+ ass_msg(lib, MSGL_WARN, "Error opening font: '%s', %d", path, index);
+ return NULL;
+ }
+
+ 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 NULL;
+ }
+
+ const char *face_psname = FT_Get_Postscript_Name(face);
+ if (face_psname != NULL &&
+ strcmp(face_psname, postscript_name) == 0)
+ break;
+ }
+ }
+
+ return face;
+}
+
static unsigned long
read_stream_font(FT_Stream stream, unsigned long offset, unsigned char *buffer,
unsigned long count)
@@ -147,6 +179,38 @@ close_stream_font(FT_Stream stream)
free(stream);
}
+FT_Face ass_face_stream(ASS_Library *lib, FT_Library ftlib, const char *name,
+ const ASS_FontStream *stream, int index)
+{
+ ASS_FontStream *fs = calloc(1, sizeof(ASS_FontStream));
+ if (!fs)
+ return NULL;
+ *fs = *stream;
+
+ FT_Stream ftstream = calloc(1, sizeof(FT_StreamRec));
+ if (!ftstream)
+ return NULL;
+ ftstream->size = stream->func(stream->priv, NULL, 0, 0);
+ ftstream->read = read_stream_font;
+ ftstream->close = close_stream_font;
+ ftstream->descriptor.pointer = (void *)fs;
+
+ FT_Open_Args args = {
+ .flags = FT_OPEN_STREAM,
+ .stream = ftstream,
+ };
+
+ FT_Face face;
+ int error = FT_Open_Face(ftlib, &args, index, &face);
+ if (error) {
+ ass_msg(lib, MSGL_WARN,
+ "Error opening memory font: '%s'", name);
+ return NULL;
+ }
+
+ return face;
+}
+
/**
* \brief Select a face with the given charcode and add it to ASS_Font
* \return index of the new face in font->faces, -1 if failed
@@ -155,7 +219,7 @@ static int add_face(ASS_FontSelector *fontsel, ASS_Font *font, uint32_t ch)
{
char *path;
char *postscript_name = NULL;
- int i, index, uid, error;
+ int i, index, uid;
ASS_FontStream stream = { NULL, NULL };
FT_Face face;
@@ -177,57 +241,16 @@ static int add_face(ASS_FontSelector *fontsel, ASS_Font *font, uint32_t ch)
}
if (stream.func) {
- FT_Open_Args args;
- FT_Stream ftstream = calloc(1, sizeof(FT_StreamRec));
- ASS_FontStream *fs = calloc(1, sizeof(ASS_FontStream));
-
- *fs = stream;
- ftstream->size = stream.func(stream.priv, NULL, 0, 0);
- ftstream->read = read_stream_font;
- ftstream->close = close_stream_font;
- ftstream->descriptor.pointer = (void *)fs;
-
- memset(&args, 0, sizeof(FT_Open_Args));
- args.flags = FT_OPEN_STREAM;
- args.stream = ftstream;
-
- error = FT_Open_Face(font->ftlibrary, &args, index, &face);
-
- if (error) {
- ass_msg(font->library, MSGL_WARN,
- "Error opening memory font: '%s'", path);
- return -1;
- }
-
+ face = ass_face_stream(font->library, font->ftlibrary, path,
+ &stream, index);
} else {
- error = FT_New_Face(font->ftlibrary, path, index, &face);
- if (error) {
- ass_msg(font->library, MSGL_WARN,
- "Error opening font: '%s', %d", path, index);
- return -1;
- }
-
- if (postscript_name && index < 0 && face->num_faces > 0) {
- // The font provider gave us a post_script 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(font->ftlibrary, path, i, &face);
- if (error) {
- ass_msg(font->library, MSGL_WARN,
- "Error opening font: '%s', %d", path, i);
- return -1;
- }
-
- const char *face_psname = FT_Get_Postscript_Name(face);
- if (face_psname != NULL &&
- strcmp(face_psname, postscript_name) == 0)
- break;
- }
- }
+ face = ass_face_open(font->library, font->ftlibrary, path,
+ postscript_name, index);
}
+ if (!face)
+ return -1;
+
charmap_magic(font->library, face);
set_font_metrics(face);
diff --git a/libass/ass_font.h b/libass/ass_font.h
index 71bbf3f..98ac1c8 100644
--- a/libass/ass_font.h
+++ b/libass/ass_font.h
@@ -63,4 +63,9 @@ FT_Glyph ass_font_get_glyph(ASS_Font *font, int face_index, int index,
ASS_Hinting hinting, int deco);
void ass_font_clear(ASS_Font *font);
+FT_Face ass_face_open(ASS_Library *lib, FT_Library ftlib, const char *path,
+ const char *postscript_name, int index);
+FT_Face ass_face_stream(ASS_Library *lib, FT_Library ftlib, const char *name,
+ const ASS_FontStream *stream, int index);
+
#endif /* LIBASS_FONT_H */
diff --git a/libass/ass_fontselect.c b/libass/ass_fontselect.c
index ef79066..40daf85 100644
--- a/libass/ass_fontselect.c
+++ b/libass/ass_fontselect.c
@@ -884,39 +884,14 @@ bool ass_get_font_info(ASS_Library *lib, FT_Library ftlib, const char *path,
bool require_family_name,
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);
+ FT_Face face = ass_face_open(lib, ftlib, path, postscript_name, index);
+ if (!face)
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, require_family_name, info);
- if (ret)
- info->postscript_name = strdup(info->postscript_name);
- FT_Done_Face(face);
- }
+ bool ret = get_font_info(ftlib, face, require_family_name, info);
+ if (ret)
+ info->postscript_name = strdup(info->postscript_name);
+ FT_Done_Face(face);
return ret;
}
diff --git a/libass/ass_fontselect.h b/libass/ass_fontselect.h
index ce71b90..907a1b5 100644
--- a/libass/ass_fontselect.h
+++ b/libass/ass_fontselect.h
@@ -27,6 +27,7 @@
typedef struct ass_shaper_font_data ASS_ShaperFontData;
typedef struct font_selector ASS_FontSelector;
typedef struct font_info ASS_FontInfo;
+typedef struct ass_font_stream ASS_FontStream;
#include "ass_types.h"
#include "ass.h"
@@ -209,8 +210,6 @@ struct ass_font_provider_meta_data {
// See FONT_WIDTH_*
};
-typedef struct ass_font_stream ASS_FontStream;
-
struct ass_font_stream {
// GetDataFunc
size_t (*func)(void *font_priv, unsigned char *data,