diff options
author | Diego 'Flameeyes' Pettenò <flameeyes@gmail.com> | 2007-04-13 21:07:12 +0200 |
---|---|---|
committer | Diego 'Flameeyes' Pettenò <flameeyes@gmail.com> | 2007-04-13 21:07:12 +0200 |
commit | 3752d8308011eafe58e5534443d50a50f817f462 (patch) | |
tree | 662b3ba02752d65c1e0456c561659da62c6f1f14 /src | |
parent | 54008077bfd9040f498d7fc7eec0bb0e4fa11853 (diff) | |
download | xine-lib-3752d8308011eafe58e5534443d50a50f817f462.tar.gz xine-lib-3752d8308011eafe58e5534443d50a50f817f462.tar.bz2 |
Search for fonts to render with FreeType2 on XDG Data directories.
With this change, when rendering a font through FreeType2 (and not using
FontConfig) the directories looked up are the ones defined in XDG_DATA_DIRS,
right after XDG_DATA_HOME; this way the user can decide to have fonts data
in a different place than its home directory.
This also splits up the lookup for fontconfig and non-fontconfig cases in
two different functions, to avoid gotos and labels.
Diffstat (limited to 'src')
-rw-r--r-- | src/xine-engine/osd.c | 152 |
1 files changed, 95 insertions, 57 deletions
diff --git a/src/xine-engine/osd.c b/src/xine-engine/osd.c index b68b3b5c2..cc33de781 100644 --- a/src/xine-engine/osd.c +++ b/src/xine-engine/osd.c @@ -40,6 +40,8 @@ # include <iconv.h> #endif +#include <basedir.h> + #define LOG_MODULE "osd" #define LOG_VERBOSE /* @@ -826,6 +828,91 @@ static int osd_renderer_unload_font(osd_renderer_t *this, char *fontname ) { } #ifdef HAVE_FT2 + +# ifdef HAVE_FONTCONFIG +/** + * @brief Look up a font name using FontConfig library + * @param osd The OSD object to load the font for. + * @param fontname Name of the font to look up. + * @param size Size of the font to look for. + * + * @return If the lookup was done correctly, a non-zero value is returned. + */ +static int osd_lookup_fontconfig( osd_object_t *osd, const char *const fontname, const int size ) { + FcPattern *pat = NULL, *match = NULL; + FcFontSet *fs = FcFontSetCreate(); + FcResult result; + + pat = FcPatternBuild(NULL, FC_FAMILY, FcTypeString, fontname, FC_SIZE, FcTypeDouble, (double)size, NULL); + FcConfigSubstitute(NULL, pat, FcMatchPattern); + FcDefaultSubstitute(pat); + + match = FcFontMatch(NULL, pat, &result); + FcPatternDestroy(pat); + + if ( ! match ) { + FcFontSetDestroy(fs); + xprintf(osd->renderer->stream->xine, XINE_VERBOSITY_LOG, + _("osd: error matching font %s with FontConfig"), fontname); + return 0; + } + FcFontSetAdd(fs, match); + + if ( fs->nfont != 0 ) { + FcChar8 *filename = NULL; + FcPatternGetString(fs->fonts[0], FC_FILE, 0, &filename); + if ( ! FT_New_Face(osd->ft2->library, (const char*)filename, 0, &osd->ft2->face) ) { + FcFontSetDestroy(fs); + return 1; + } + + xprintf(osd->renderer->stream->xine, XINE_VERBOSITY_LOG, + _("osd: error loading font %s with FontConfig"), fontname); + return 0; + } else { + xprintf(osd->renderer->stream->xine, XINE_VERBOSITY_LOG, + _("osd: error looking up font %s with FontConfig"), fontname); + return 0; + } +} +# endif /* HAVE_FONTCONFIG */ + +/** + * @brief Look up a font file using XDG data directories. + * @param osd The OSD object to load the font for. + * @param fontname Name (absolute or relative) of the font to look up. + * + * @return If the lookup was done correctly, a non-zero value is returned. + * + * @see XDG Base Directory specification: + * http://standards.freedesktop.org/basedir-spec/latest/index.html + */ +static int osd_lookup_xdg( osd_object_t *osd, const char *const fontname ) { + const char *const *data_dirs = xdgDataDirectories(osd->renderer->stream->xine->basedir_handle); + + /* try load font from current directory or from an absolute path */ + if ( FT_New_Face(osd->ft2->library, fontname, 0, &osd->ft2->face) == FT_Err_Ok ) + return 1; + + if ( data_dirs ) + while( (*data_dirs) && *(*data_dirs) ) { + FT_Error fte = FT_Err_Ok; + char *fontpath = NULL; + asprintf(&fontpath, "%s/"PACKAGE"/%s", data_dirs, fontname); + + fte = FT_New_Face(osd->ft2->library, fontpath, 0, &osd->ft2->face); + + free(fontpath); + + if ( fte == FT_Err_Ok ) + return 1; + } + + xprintf(osd->renderer->stream->xine, XINE_VERBOSITY_LOG, + _("osd: error loading font %s with in XDG data directories.\n"), fontname); + return 0; +} + static int osd_set_font_freetype2( osd_object_t *osd, const char *fontname, int size ) { if (!osd->ft2) { osd->ft2 = xine_xmalloc(sizeof(osd_ft2context_t)); @@ -838,68 +925,19 @@ static int osd_set_font_freetype2( osd_object_t *osd, const char *fontname, int } } + do { /* while 0 */ #ifdef HAVE_FONTCONFIG - do { - FcPattern *pat = NULL, *match = NULL; - FcFontSet *fs = FcFontSetCreate(); - FcResult result; - - pat = FcPatternBuild(NULL, FC_FAMILY, FcTypeString, fontname, FC_SIZE, FcTypeDouble, (double)size, NULL); - FcConfigSubstitute(NULL, pat, FcMatchPattern); - FcDefaultSubstitute(pat); - - match = FcFontMatch(NULL, pat, &result); - FcPatternDestroy(pat); - - if ( ! match ) { - FcFontSetDestroy(fs); - xprintf(osd->renderer->stream->xine, XINE_VERBOSITY_LOG, - _("osd: error matching font %s with FontConfig"), fontname); + if ( osd_lookup_fontconfig(osd, fontname, size) ) break; - } - FcFontSetAdd(fs, match); - - if ( fs->nfont != 0 ) { - FcChar8 *filename = NULL; - FcPatternGetString(fs->fonts[0], FC_FILE, 0, &filename); - if ( ! FT_New_Face(osd->ft2->library, (const char*)filename, 0, &osd->ft2->face) ) { - FcFontSetDestroy(fs); - goto end; - } - - xprintf(osd->renderer->stream->xine, XINE_VERBOSITY_LOG, - _("osd: error loading font %s with FontConfig"), fontname); - } else { - xprintf(osd->renderer->stream->xine, XINE_VERBOSITY_LOG, - _("osd: error looking up font %s with FontConfig"), fontname); - } - } while(0); #endif - { - char pathname[1024]; - /* try load font from current directory */ - if ( !FT_New_Face(osd->ft2->library, fontname, 0, &osd->ft2->face) ) - goto end; - - /* try load font from home directory */ - snprintf(pathname, 1024, "%s/.xine/fonts/%s", xine_get_homedir(), fontname); - if ( !FT_New_Face(osd->ft2->library, pathname, 0, &osd->ft2->face) ) - goto end; - - /* try load font from xine font directory */ - snprintf(pathname, 1024, "%s/%s", XINE_FONTDIR, fontname); - if ( !FT_New_Face(osd->ft2->library, pathname, 0, &osd->ft2->face) ) - goto end; - - xprintf(osd->renderer->stream->xine, XINE_VERBOSITY_LOG, - _("osd: error loading font %s with ft2\n"), fontname); - } + if ( osd_lookup_xdg(osd, fontname) ) + break; - free(osd->ft2); - osd->ft2 = NULL; - return 0; + free(osd->ft2); + osd->ft2 = NULL; + return 0; + } while(0); - end: if (FT_Set_Pixel_Sizes(osd->ft2->face, 0, size)) { xprintf(osd->renderer->stream->xine, XINE_VERBOSITY_LOG, _("osd: error setting font size (no scalable font?)\n")); |