From 09edb29ea61785570d612b9fb246ddea93173bac Mon Sep 17 00:00:00 2001 From: Grigori Goronzy Date: Sat, 3 Sep 2011 15:20:00 +0200 Subject: Add support for font width property Add a width field to metadata. This is used for sorting fonts as well. Fixes wrong matches with different width variants in the same font family. --- libass/ass_fontconfig.c | 11 +++++------ libass/ass_fontselect.c | 11 ++++++++++- libass/ass_types.h | 4 ++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/libass/ass_fontconfig.c b/libass/ass_fontconfig.c index a3a371a..4fbf824 100644 --- a/libass/ass_fontconfig.c +++ b/libass/ass_fontconfig.c @@ -82,6 +82,7 @@ static void scan_fonts(FcConfig *config, ASS_FontProvider *provider) // simple types result = FcPatternGetInteger(pat, FC_SLANT, 0, &meta.slant); + result |= FcPatternGetInteger(pat, FC_WIDTH, 0, &meta.width); result |= FcPatternGetInteger(pat, FC_WEIGHT, 0, &weight); result |= FcPatternGetInteger(pat, FC_INDEX, 0, &index); if (result != FcResultMatch) @@ -98,12 +99,10 @@ static void scan_fonts(FcConfig *config, ASS_FontProvider *provider) meta.weight = FONT_WEIGHT_BOLD; // family name - // HACK: get the last family name. that fixes fonts - // like Arial Narrow in some versions - int n_family = 0; - while (FcPatternGetString(pat, FC_FAMILY, n_family, - (FcChar8 **)&meta.family) == FcResultMatch) - n_family++; + result = FcPatternGetString(pat, FC_FAMILY, 0, + (FcChar8 **)&meta.family); + if (result != FcResultMatch) + continue; // path result = FcPatternGetString(pat, FC_FILE, 0, (FcChar8 **)&path); diff --git a/libass/ass_fontselect.c b/libass/ass_fontselect.c index ff2d707..046e8e5 100644 --- a/libass/ass_fontselect.c +++ b/libass/ass_fontselect.c @@ -63,6 +63,7 @@ struct font_info { int slant; int weight; // TrueType scale, 100-900 + int width; // how to access this face char *path; // absolute path @@ -225,18 +226,21 @@ ass_font_provider_add_font(ASS_FontProvider *provider, unsigned int index, void *data) { int i; - int weight, slant; + int weight, slant, width; ASS_FontSelector *selector = provider->parent; ASS_FontInfo *info; weight = meta->weight; slant = meta->slant; + width = meta->width; // check slant/weight for validity, use defaults if they're invalid if (weight < 100 || weight > 900) weight = 400; if (slant < 0 || slant > 110) slant = 0; + if (width < 50 || width > 200) + width = 100; // check size if (selector->n_font >= selector->alloc_font) { @@ -254,6 +258,7 @@ ass_font_provider_add_font(ASS_FontProvider *provider, info->slant = slant; info->weight = weight; + info->width = width; info->family = strdup(meta->family); info->n_fullname = meta->n_fullname; info->fullnames = calloc(meta->n_fullname, sizeof(char *)); @@ -384,6 +389,9 @@ static unsigned font_info_similarity(ASS_FontInfo *a, ASS_FontInfo *req) // compare weight similarity += ABS(a->weight - req->weight); + // compare width + similarity += ABS(a->width - req->width); + return similarity; } @@ -448,6 +456,7 @@ static char *select_font(ASS_FontSelector *priv, ASS_Library *library, memset(&req, 0, sizeof(ASS_FontInfo)); req.slant = italic; req.weight = bold; + req.width = 100; req.n_fullname = 1; req.fullnames = &req_fullname; req.fullnames[0] = trim_space(strdup(family)); diff --git a/libass/ass_types.h b/libass/ass_types.h index f52e537..8411348 100644 --- a/libass/ass_types.h +++ b/libass/ass_types.h @@ -35,6 +35,9 @@ #define FONT_SLANT_NONE 0 #define FONT_SLANT_ITALIC 100 #define FONT_SLANT_OBLIQUE 110 +#define FONT_WIDTH_CONDENSED 75 +#define FONT_WIDTH_NORMAL 100 +#define FONT_WIDTH_EXPANDED 125 /* Opaque objects internally used by libass. Contents are private. */ @@ -68,6 +71,7 @@ typedef struct font_provider_meta_data { int n_fullname; // number of localized full names int slant; // uses the above scale (NONE/ITALIC/OBLIQUE) int weight; // TrueType scale, 100-900 + int width; // in percent, normally 100 } ASS_FontProviderMetaData; -- cgit v1.2.3