From 2cae02e2130395b29a07a3b261893ebd4dad7ce5 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 25 Jul 2014 16:24:26 -0400 Subject: [PATCH 10/14] Add FC_WEIGHT_DEMILIGHT Part of https://bugs.freedesktop.org/show_bug.cgi?id=81453 Also hooks up FC_WEIGHT_BOOK to fcfreetype.c. --- doc/fontconfig-user.sgml | 2 ++ fontconfig/fontconfig.h | 2 ++ src/fcfreetype.c | 8 +++++++- src/fcname.c | 2 ++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/fontconfig-user.sgml b/doc/fontconfig-user.sgml index 775d1f7..b54ba7a 100644 --- a/doc/fontconfig-user.sgml +++ b/doc/fontconfig-user.sgml @@ -473,6 +473,8 @@ symbolic names for common font values: extralight weight 40 ultralight weight 40 light weight 50 + demilight weight 65 + semilight weight 65 book weight 75 regular weight 80 normal weight 80 diff --git a/fontconfig/fontconfig.h b/fontconfig/fontconfig.h index 2258251..59c80e4 100644 --- a/fontconfig/fontconfig.h +++ b/fontconfig/fontconfig.h @@ -131,6 +131,8 @@ typedef int FcBool; #define FC_WEIGHT_EXTRALIGHT 40 #define FC_WEIGHT_ULTRALIGHT FC_WEIGHT_EXTRALIGHT #define FC_WEIGHT_LIGHT 50 +#define FC_WEIGHT_DEMILIGHT 65 +#define FC_WEIGHT_SEMILIGHT FC_WEIGHT_DEMILIGHT #define FC_WEIGHT_BOOK 75 #define FC_WEIGHT_REGULAR 80 #define FC_WEIGHT_NORMAL FC_WEIGHT_REGULAR diff --git a/src/fcfreetype.c b/src/fcfreetype.c index d271d69..b8406af 100644 --- a/src/fcfreetype.c +++ b/src/fcfreetype.c @@ -933,6 +933,8 @@ static const FcStringConst weightConsts[] = { { (FC8) "thin", FC_WEIGHT_THIN }, { (FC8) "extralight", FC_WEIGHT_EXTRALIGHT }, { (FC8) "ultralight", FC_WEIGHT_ULTRALIGHT }, + { (FC8) "demilight", FC_WEIGHT_DEMILIGHT }, + { (FC8) "semilight", FC_WEIGHT_SEMILIGHT }, { (FC8) "light", FC_WEIGHT_LIGHT }, { (FC8) "book", FC_WEIGHT_BOOK }, { (FC8) "regular", FC_WEIGHT_REGULAR }, @@ -1469,8 +1471,12 @@ FcFreeTypeQueryFace (const FT_Face face, weight = FC_WEIGHT_THIN; else if (os2->usWeightClass < 250) weight = FC_WEIGHT_EXTRALIGHT; - else if (os2->usWeightClass < 350) + else if (os2->usWeightClass < 325) weight = FC_WEIGHT_LIGHT; + else if (os2->usWeightClass < 365) + weight = FC_WEIGHT_SEMILIGHT; + else if (os2->usWeightClass < 390) + weight = FC_WEIGHT_BOOK; else if (os2->usWeightClass < 450) weight = FC_WEIGHT_REGULAR; else if (os2->usWeightClass < 550) diff --git a/src/fcname.c b/src/fcname.c index f302948..77c788f 100644 --- a/src/fcname.c +++ b/src/fcname.c @@ -132,6 +132,8 @@ static const FcConstant _FcBaseConstants[] = { { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, }, { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, }, { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, }, + { (FcChar8 *) "demilight", "weight", FC_WEIGHT_DEMILIGHT, }, + { (FcChar8 *) "semilight", "weight", FC_WEIGHT_DEMILIGHT, }, { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, }, { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, }, { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, }, -- 2.5.3.windows.1 From 84bdd3f752408b4ca0d30600b82dfd1737f67fc1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 25 Jul 2014 18:07:10 -0400 Subject: [PATCH 11/14] Change DemiLight from 65 to 55 Such that Regular is closer to Medium than to DemiLight --- doc/fontconfig-user.sgml | 4 ++-- fontconfig/fontconfig.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/fontconfig-user.sgml b/doc/fontconfig-user.sgml index b54ba7a..482b453 100644 --- a/doc/fontconfig-user.sgml +++ b/doc/fontconfig-user.sgml @@ -473,8 +473,8 @@ symbolic names for common font values: extralight weight 40 ultralight weight 40 light weight 50 - demilight weight 65 - semilight weight 65 + demilight weight 55 + semilight weight 55 book weight 75 regular weight 80 normal weight 80 diff --git a/fontconfig/fontconfig.h b/fontconfig/fontconfig.h index 59c80e4..a25bad9 100644 --- a/fontconfig/fontconfig.h +++ b/fontconfig/fontconfig.h @@ -131,7 +131,7 @@ typedef int FcBool; #define FC_WEIGHT_EXTRALIGHT 40 #define FC_WEIGHT_ULTRALIGHT FC_WEIGHT_EXTRALIGHT #define FC_WEIGHT_LIGHT 50 -#define FC_WEIGHT_DEMILIGHT 65 +#define FC_WEIGHT_DEMILIGHT 55 #define FC_WEIGHT_SEMILIGHT FC_WEIGHT_DEMILIGHT #define FC_WEIGHT_BOOK 75 #define FC_WEIGHT_REGULAR 80 -- 2.5.3.windows.1 From 06a81dd214aaa871871fe9833381df3f6571b872 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 25 Jul 2014 17:59:26 -0400 Subject: [PATCH 12/14] Linearly interpolate weight values Rest of Part of https://bugs.freedesktop.org/show_bug.cgi?id=81453 Adds new API: FcWeightFromOpenType() FcWeightToOpenType() --- doc/Makefile.am | 1 + doc/fcweight.fncs | 47 +++++++++++++++++++++++++++ fontconfig/fontconfig.h | 21 +++++++++++++ src/Makefile.am | 1 + src/fcfreetype.c | 27 +--------------- src/fcweight.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 155 insertions(+), 26 deletions(-) create mode 100644 doc/fcweight.fncs create mode 100644 src/fcweight.c diff --git a/doc/Makefile.am b/doc/Makefile.am index 78a7cdb..cfcc8de 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -84,6 +84,7 @@ DOC_FUNCS_FNCS = \ fcstring.fncs \ fcstrset.fncs \ fcvalue.fncs \ + fcweight.fncs \ $(NULL) SGML_FILES = \ fontconfig-user.sgml \ diff --git a/doc/fcweight.fncs b/doc/fcweight.fncs new file mode 100644 index 0000000..2872dd6 --- /dev/null +++ b/doc/fcweight.fncs @@ -0,0 +1,47 @@ +/* + * fontconfig/doc/fcweight.fncs + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the author(s) not be used in + * advertising or publicity pertaining to distribution of the software without + * specific, written prior permission. The authors make no + * representations about the suitability of this software for any purpose. It + * is provided "as is" without express or implied warranty. + * + * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ +@RET@ int +@FUNC@ FcWeightFromOpenType +@TYPE1@ int @ARG1@ ot_weight +@PURPOSE@ Convert from OpenType weight values to fontconfig ones +@DESC@ +FcWeightFromOpenType returns an integer value +to use with FC_WEIGHT, from an integer in the 1..1000 range, resembling +the numbers from OpenType specification's OS/2 usWeight numbers, which +are also similar to CSS font-weight numbers. If input is negative, +zero, or greater than 1000, returns -1. This function linearly interpolates +between various FC_WEIGHT_* constants. As such, the returned value does not +necessarily match any of the predefined constants. +@SINCE@ 2.11.91 +@@ + +@RET@ int +@FUNC@ FcWeightToOpenType +@TYPE1@ int @ARG1@ ot_weight +@PURPOSE@ Convert from fontconfig weight values to OpenType ones +@DESC@ +FcWeightToOpenType is the inverse of +FcWeightFromOpenType. If the input is less than +FC_WEIGHT_THIN or greater than FC_WEIGHT_EXTRABLACK, returns -1. Otherwise +returns a number in the range 1 to 1000. +@SINCE@ 2.11.91 +@@ diff --git a/fontconfig/fontconfig.h b/fontconfig/fontconfig.h index a25bad9..5b753a3 100644 --- a/fontconfig/fontconfig.h +++ b/fontconfig/fontconfig.h @@ -885,6 +885,27 @@ FcPatternBuild (FcPattern *p, ...) FC_ATTRIBUTE_SENTINEL(0); FcPublic FcChar8 * FcPatternFormat (FcPattern *pat, const FcChar8 *format); +/* fcrange.c */ +FcPublic FcRange * +FcRangeCreateDouble (double begin, double end); + +FcPublic FcRange * +FcRangeCreateInteger (FcChar32 begin, FcChar32 end); + +FcPublic void +FcRangeDestroy (FcRange *range); + +FcPublic FcRange * +FcRangeCopy (const FcRange *r); + +/* fcweight.c */ + +FcPublic int +FcWeightFromOpenType (int ot_weight); + +FcPublic int +FcWeightToOpenType (int fc_weight); + /* fcstr.c */ FcPublic FcChar8 * diff --git a/src/Makefile.am b/src/Makefile.am index 066cc03..d66ca5a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -154,6 +154,7 @@ libfontconfig_la_SOURCES = \ fcserialize.c \ fcstat.c \ fcstr.c \ + fcweight.c \ fcwindows.h \ fcxml.c \ ftglue.h \ diff --git a/src/fcfreetype.c b/src/fcfreetype.c index b8406af..00c1e2f 100644 --- a/src/fcfreetype.c +++ b/src/fcfreetype.c @@ -1465,32 +1465,7 @@ FcFreeTypeQueryFace (const FT_Face face, if (os2 && os2->version != 0xffff) { - if (os2->usWeightClass == 0) - ; - else if (os2->usWeightClass < 150) - weight = FC_WEIGHT_THIN; - else if (os2->usWeightClass < 250) - weight = FC_WEIGHT_EXTRALIGHT; - else if (os2->usWeightClass < 325) - weight = FC_WEIGHT_LIGHT; - else if (os2->usWeightClass < 365) - weight = FC_WEIGHT_SEMILIGHT; - else if (os2->usWeightClass < 390) - weight = FC_WEIGHT_BOOK; - else if (os2->usWeightClass < 450) - weight = FC_WEIGHT_REGULAR; - else if (os2->usWeightClass < 550) - weight = FC_WEIGHT_MEDIUM; - else if (os2->usWeightClass < 650) - weight = FC_WEIGHT_SEMIBOLD; - else if (os2->usWeightClass < 750) - weight = FC_WEIGHT_BOLD; - else if (os2->usWeightClass < 850) - weight = FC_WEIGHT_EXTRABOLD; - else if (os2->usWeightClass < 925) - weight = FC_WEIGHT_BLACK; - else if (os2->usWeightClass < 1000) - weight = FC_WEIGHT_EXTRABLACK; + weight = FcWeightFromOpenType (os2->usWeightClass); if ((FcDebug() & FC_DBG_SCANV) && weight != -1) printf ("\tos2 weight class %d maps to weight %d\n", os2->usWeightClass, weight); diff --git a/src/fcweight.c b/src/fcweight.c new file mode 100644 index 0000000..20c5da0 --- /dev/null +++ b/src/fcweight.c @@ -0,0 +1,84 @@ +/* + * fontconfig/src/fcweight.c + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the author(s) not be used in + * advertising or publicity pertaining to distribution of the software without + * specific, written prior permission. The authors make no + * representations about the suitability of this software for any purpose. It + * is provided "as is" without express or implied warranty. + * + * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ + +#include "fcint.h" + +static const struct { + int ot; + int fc; +} map[] = { + { 0, FC_WEIGHT_THIN }, + { 100, FC_WEIGHT_THIN }, + { 200, FC_WEIGHT_EXTRALIGHT }, + { 350, FC_WEIGHT_DEMILIGHT }, + { 300, FC_WEIGHT_LIGHT }, + { 380, FC_WEIGHT_BOOK }, + { 400, FC_WEIGHT_REGULAR }, + { 500, FC_WEIGHT_MEDIUM }, + { 600, FC_WEIGHT_DEMIBOLD }, + { 700, FC_WEIGHT_BOLD }, + { 800, FC_WEIGHT_EXTRABOLD }, + { 900, FC_WEIGHT_BLACK }, + {1000, FC_WEIGHT_EXTRABLACK }, +}; + +static int lerp(int x, int x1, int x2, int y1, int y2) +{ + int dx = x2 - x1; + int dy = y2 - y1; + assert (dx > 0 && dy > 0 && x1 <= x && x <= x2); + return y1 + (dy*(x-x1) + dx/2) / dx; +} + +FcPublic int +FcWeightFromOpenType (int ot_weight) +{ + int i; + if (ot_weight <= 0 || ot_weight > 1000) + return -1; + + for (i = 1; ot_weight > map[i].ot; i++) + ; + + if (ot_weight == map[i].ot) + return map[i].fc; + + /* Interpolate between two items. */ + return lerp (ot_weight, map[i-1].ot, map[i].ot, map[i-1].fc, map[i].fc); +} + +FcPublic int +FcWeightToOpenType (int fc_weight) +{ + int i; + if (fc_weight < 0 || fc_weight > FC_WEIGHT_EXTRABLACK) + return -1; + + for (i = 1; fc_weight > map[i].fc; i++) + ; + + if (fc_weight == map[i].fc) + return map[i].ot; + + /* Interpolate between two items. */ + return lerp (fc_weight, map[i-1].fc, map[i].fc, map[i-1].ot, map[i].ot); +} -- 2.5.3.windows.1 From c122587b08b9059c6c021614cf25992381bc80df Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 6 Aug 2014 12:23:24 -0400 Subject: [PATCH 13/14] Fix assertion failure https://bugs.freedesktop.org/show_bug.cgi?id=82220 https://bugs.freedesktop.org/show_bug.cgi?id=82228 --- src/fcweight.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fcweight.c b/src/fcweight.c index 20c5da0..f413d9d 100644 --- a/src/fcweight.c +++ b/src/fcweight.c @@ -45,7 +45,7 @@ static int lerp(int x, int x1, int x2, int y1, int y2) { int dx = x2 - x1; int dy = y2 - y1; - assert (dx > 0 && dy > 0 && x1 <= x && x <= x2); + assert (dx > 0 && dy >= 0 && x1 <= x && x <= x2); return y1 + (dy*(x-x1) + dx/2) / dx; } -- 2.5.3.windows.1 From fcb4fc1fea91d3b231f9f426b8a6e22ca7546e19 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 6 Aug 2014 12:29:35 -0400 Subject: [PATCH 14/14] If OS/2 table says weight is 1 to 9, multiply by 100 https://bugs.freedesktop.org/show_bug.cgi?id=82228 --- src/fcweight.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/fcweight.c b/src/fcweight.c index f413d9d..9530289 100644 --- a/src/fcweight.c +++ b/src/fcweight.c @@ -53,7 +53,14 @@ FcPublic int FcWeightFromOpenType (int ot_weight) { int i; - if (ot_weight <= 0 || ot_weight > 1000) + + /* Follow WPF Font Selection Model's advice. */ + if (1 <= ot_weight && ot_weight <= 9) + ot_weight *= 100; + + /* WPF Font Selection Model rejects 1000, we allow it + * because Pango uses that number. */ + if (ot_weight < 1 || ot_weight > 1000) return -1; for (i = 1; ot_weight > map[i].ot; i++) -- 2.5.3.windows.1