From d9c1df24c148fb6f6a19d6563c3f6abe8653264b Mon Sep 17 00:00:00 2001 From: Rodger Combs Date: Fri, 12 Oct 2018 00:37:47 -0500 Subject: fontconfig: improve weight mapping Use FcWeightToOpenType when available; otherwise, use an if/elseif ladder implementing the inverse of fontconfig's behavior. --- libass/ass_fontconfig.c | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) (limited to 'libass') diff --git a/libass/ass_fontconfig.c b/libass/ass_fontconfig.c index e87dba9..2610d3d 100644 --- a/libass/ass_fontconfig.c +++ b/libass/ass_fontconfig.c @@ -117,12 +117,43 @@ static void scan_fonts(FcConfig *config, ASS_FontProvider *provider) // fontconfig uses its own weight scale, apparently derived // from typographical weight. we're using truetype weights, so // convert appropriately - if (weight <= FC_WEIGHT_LIGHT) - meta.weight = FONT_WEIGHT_LIGHT; - else if (weight <= FC_WEIGHT_MEDIUM) - meta.weight = FONT_WEIGHT_MEDIUM; +#if FC_VERSION >= 21191 + meta.weight = FcWeightToOpenType(weight); +#else + // On older fontconfig, FcWeightToOpenType is unavailable, and its inverse was + // implemented more simply, using an if/else ladder instead of linear interpolation. + // We implement an inverse of that ladder here. + // We don't expect actual FC caches from these versions to have intermediate + // values, so the average checks are only for completeness. +#define AVG(x, y) ((x + y) / 2) +#ifndef FC_WEIGHT_SEMILIGHT +#define FC_WEIGHT_SEMILIGHT 55 +#endif + if (weight < AVG(FC_WEIGHT_THIN, FC_WEIGHT_EXTRALIGHT)) + meta.weight = 100; + else if (weight < AVG(FC_WEIGHT_EXTRALIGHT, FC_WEIGHT_LIGHT)) + meta.weight = 200; + else if (weight < AVG(FC_WEIGHT_LIGHT, FC_WEIGHT_SEMILIGHT)) + meta.weight = 300; + else if (weight < AVG(FC_WEIGHT_SEMILIGHT, FC_WEIGHT_BOOK)) + meta.weight = 350; + else if (weight < AVG(FC_WEIGHT_BOOK, FC_WEIGHT_REGULAR)) + meta.weight = 380; + else if (weight < AVG(FC_WEIGHT_REGULAR, FC_WEIGHT_MEDIUM)) + meta.weight = 400; + else if (weight < AVG(FC_WEIGHT_MEDIUM, FC_WEIGHT_SEMIBOLD)) + meta.weight = 500; + else if (weight < AVG(FC_WEIGHT_SEMIBOLD, FC_WEIGHT_BOLD)) + meta.weight = 600; + else if (weight < AVG(FC_WEIGHT_BOLD, FC_WEIGHT_EXTRABOLD)) + meta.weight = 700; + else if (weight < AVG(FC_WEIGHT_EXTRABOLD, FC_WEIGHT_BLACK)) + meta.weight = 800; + else if (weight < AVG(FC_WEIGHT_BLACK, FC_WEIGHT_EXTRABLACK)) + meta.weight = 900; else - meta.weight = FONT_WEIGHT_BOLD; + meta.weight = 1000; +#endif // path result = FcPatternGetString(pat, FC_FILE, 0, (FcChar8 **)&path); -- cgit v1.2.3