Add FcWeightTo/FromOpenTypeDouble()

No idea why I didn't add these as double to begin with.
This commit is contained in:
Behdad Esfahbod 2018-01-03 15:59:24 +00:00
parent 97898b1158
commit 706535e107
4 changed files with 65 additions and 25 deletions

View File

@ -19,18 +19,41 @@
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE. * PERFORMANCE OF THIS SOFTWARE.
*/ */
@RET@ double
@FUNC@ FcWeightFromOpenTypeDouble
@TYPE1@ double @ARG1@ ot_weight
@PURPOSE@ Convert from OpenType weight values to fontconfig ones
@DESC@
<function>FcWeightFromOpenTypeDouble</function> returns an double value
to use with FC_WEIGHT, from an double 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 doubleerpolates
between various FC_WEIGHT_* constants. As such, the returned value does not
necessarily match any of the predefined constants.
@SINCE@ 2.12.92
@@
@RET@ double
@FUNC@ FcWeightToOpenTypeDouble
@TYPE1@ double @ARG1@ ot_weight
@PURPOSE@ Convert from fontconfig weight values to OpenType ones
@DESC@
<function>FcWeightToOpenTypeDouble</function> is the inverse of
<function>FcWeightFromOpenType</function>. 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.12.92
@@
@RET@ int @RET@ int
@FUNC@ FcWeightFromOpenType @FUNC@ FcWeightFromOpenType
@TYPE1@ int @ARG1@ ot_weight @TYPE1@ int @ARG1@ ot_weight
@PURPOSE@ Convert from OpenType weight values to fontconfig ones @PURPOSE@ Convert from OpenType weight values to fontconfig ones
@DESC@ @DESC@
<function>FcWeightFromOpenType</function> returns an integer value <function>FcWeightFromOpenType</function> is like
to use with FC_WEIGHT, from an integer in the 1..1000 range, resembling <function>FcWeightFromOpenTypeDouble</function> but with integer arguments.
the numbers from OpenType specification's OS/2 usWeight numbers, which Use the other function instead.
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 @SINCE@ 2.11.91
@@ @@
@ -39,9 +62,8 @@ necessarily match any of the predefined constants.
@TYPE1@ int @ARG1@ ot_weight @TYPE1@ int @ARG1@ ot_weight
@PURPOSE@ Convert from fontconfig weight values to OpenType ones @PURPOSE@ Convert from fontconfig weight values to OpenType ones
@DESC@ @DESC@
<function>FcWeightToOpenType</function> is the inverse of <function>FcWeightToOpenType</function> is like
<function>FcWeightFromOpenType</function>. If the input is less than <function>FcWeightToOpenTypeDouble</function> but with integer arguments.
FC_WEIGHT_THIN or greater than FC_WEIGHT_EXTRABLACK, returns -1. Otherwise Use the other function instead.
returns a number in the range 1 to 1000.
@SINCE@ 2.11.91 @SINCE@ 2.11.91
@@ @@

View File

@ -966,9 +966,15 @@ FcRangeGetDouble(const FcRange *range, double *begin, double *end);
FcPublic int FcPublic int
FcWeightFromOpenType (int ot_weight); FcWeightFromOpenType (int ot_weight);
FcPublic double
FcWeightFromOpenTypeDouble (double ot_weight);
FcPublic int FcPublic int
FcWeightToOpenType (int fc_weight); FcWeightToOpenType (int fc_weight);
FcPublic double
FcWeightToOpenTypeDouble (double fc_weight);
/* fcstr.c */ /* fcstr.c */
FcPublic FcChar8 * FcPublic FcChar8 *

View File

@ -1171,7 +1171,7 @@ FcFreeTypeQueryFaceInternal (const FT_Face face,
{ {
FcPattern *pat; FcPattern *pat;
int slant = -1; int slant = -1;
int weight = -1; double weight = -1;
int width = -1; int width = -1;
FcBool decorative = FcFalse; FcBool decorative = FcFalse;
FcBool variable = FcFalse; FcBool variable = FcFalse;
@ -1264,8 +1264,8 @@ FcFreeTypeQueryFaceInternal (const FT_Face face,
{ {
case FT_MAKE_TAG ('w','g','h','t'): case FT_MAKE_TAG ('w','g','h','t'):
elt = FC_WEIGHT; elt = FC_WEIGHT;
min_value = FcWeightFromOpenType (min_value); min_value = FcWeightFromOpenTypeDouble (min_value);
max_value = FcWeightFromOpenType (max_value); max_value = FcWeightFromOpenTypeDouble (max_value);
variable_weight = FcTrue; variable_weight = FcTrue;
weight = 0; /* To stop looking for weight. */ weight = 0; /* To stop looking for weight. */
break; break;
@ -1694,15 +1694,15 @@ FcFreeTypeQueryFaceInternal (const FT_Face face,
{ {
/* Work around bad values by cleaning them up before /* Work around bad values by cleaning them up before
* multiplying by weight_mult. */ * multiplying by weight_mult. */
weight = FcWeightToOpenType (FcWeightFromOpenType (weight)); weight = FcWeightToOpenTypeDouble (FcWeightFromOpenTypeDouble (weight));
} }
weight = FcWeightFromOpenType ((int) (weight * weight_mult + .5)); weight = FcWeightFromOpenTypeDouble ((int) (weight * weight_mult + .5));
if ((FcDebug() & FC_DBG_SCANV) && weight != -1) if ((FcDebug() & FC_DBG_SCANV) && weight != -1)
printf ("\tos2 weight class %d multiplier %g maps to weight %d\n", printf ("\tos2 weight class %d multiplier %g maps to weight %g\n",
os2->usWeightClass, weight_mult, weight); os2->usWeightClass, weight_mult, weight);
/* TODO: /* TODO:
* Add FcWidthFromOpenType and FcWidthToOpenType, * Add FcWidthFromOpenTypeDouble and FcWidthToOpenTypeDouble,
* and apply width_mult post-conversion? */ * and apply width_mult post-conversion? */
switch ((int) (os2->usWidthClass * width_mult + .5)) { switch ((int) (os2->usWidthClass * width_mult + .5)) {
case 1: width = FC_WIDTH_ULTRACONDENSED; break; case 1: width = FC_WIDTH_ULTRACONDENSED; break;
@ -1893,7 +1893,7 @@ FcFreeTypeQueryFaceInternal (const FT_Face face,
if (!FcPatternAddInteger (pat, FC_SLANT, slant)) if (!FcPatternAddInteger (pat, FC_SLANT, slant))
goto bail1; goto bail1;
if (!variable_weight && !FcPatternAddInteger (pat, FC_WEIGHT, weight)) if (!variable_weight && !FcPatternAddDouble (pat, FC_WEIGHT, weight))
goto bail1; goto bail1;
if (!variable_width && !FcPatternAddInteger (pat, FC_WIDTH, width)) if (!variable_width && !FcPatternAddInteger (pat, FC_WIDTH, width))

View File

@ -41,7 +41,7 @@ static const struct {
{1000, FC_WEIGHT_EXTRABLACK }, {1000, FC_WEIGHT_EXTRABLACK },
}; };
static int lerp(int x, int x1, int x2, int y1, int y2) static double lerp(double x, int x1, int x2, int y1, int y2)
{ {
int dx = x2 - x1; int dx = x2 - x1;
int dy = y2 - y1; int dy = y2 - y1;
@ -49,8 +49,8 @@ static int lerp(int x, int x1, int x2, int y1, int y2)
return y1 + (dy*(x-x1) + dx/2) / dx; return y1 + (dy*(x-x1) + dx/2) / dx;
} }
int double
FcWeightFromOpenType (int ot_weight) FcWeightFromOpenTypeDouble (double ot_weight)
{ {
int i; int i;
@ -63,7 +63,7 @@ FcWeightFromOpenType (int ot_weight)
/* WPF Font Selection Model says do "ot_weight *= 100", /* WPF Font Selection Model says do "ot_weight *= 100",
* but Greg Hitchcock revealed that GDI had a mapping * but Greg Hitchcock revealed that GDI had a mapping
* reflected below: */ * reflected below: */
switch (ot_weight) { switch ((int) ot_weight) {
case 1: ot_weight = 80; break; case 1: ot_weight = 80; break;
case 2: ot_weight = 160; break; case 2: ot_weight = 160; break;
case 3: ot_weight = 240; break; case 3: ot_weight = 240; break;
@ -87,8 +87,8 @@ FcWeightFromOpenType (int ot_weight)
return lerp (ot_weight, map[i-1].ot, map[i].ot, map[i-1].fc, map[i].fc); return lerp (ot_weight, map[i-1].ot, map[i].ot, map[i-1].fc, map[i].fc);
} }
int double
FcWeightToOpenType (int fc_weight) FcWeightToOpenTypeDouble (double fc_weight)
{ {
int i; int i;
if (fc_weight < 0 || fc_weight > FC_WEIGHT_EXTRABLACK) if (fc_weight < 0 || fc_weight > FC_WEIGHT_EXTRABLACK)
@ -104,6 +104,18 @@ FcWeightToOpenType (int fc_weight)
return lerp (fc_weight, map[i-1].fc, map[i].fc, map[i-1].ot, map[i].ot); return lerp (fc_weight, map[i-1].fc, map[i].fc, map[i-1].ot, map[i].ot);
} }
int
FcWeightFromOpenType (int ot_weight)
{
return FcWeightFromOpenTypeDouble (ot_weight) + .5;
}
int
FcWeightToOpenType (int fc_weight)
{
return FcWeightToOpenTypeDouble (fc_weight) + .5;
}
#define __fcweight__ #define __fcweight__
#include "fcaliastail.h" #include "fcaliastail.h"
#undef __fcweight__ #undef __fcweight__