2 * fontconfig/src/fcweight.c
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the author(s) not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission. The authors make no
11 * representations about the suitability of this software for any purpose. It
12 * is provided "as is" without express or implied warranty.
14 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
29 { 0, FC_WEIGHT_THIN },
30 { 100, FC_WEIGHT_THIN },
31 { 200, FC_WEIGHT_EXTRALIGHT },
32 { 300, FC_WEIGHT_LIGHT },
33 { 350, FC_WEIGHT_DEMILIGHT },
34 { 380, FC_WEIGHT_BOOK },
35 { 400, FC_WEIGHT_REGULAR },
36 { 500, FC_WEIGHT_MEDIUM },
37 { 600, FC_WEIGHT_DEMIBOLD },
38 { 700, FC_WEIGHT_BOLD },
39 { 800, FC_WEIGHT_EXTRABOLD },
40 { 900, FC_WEIGHT_BLACK },
41 {1000, FC_WEIGHT_EXTRABLACK },
44 static double lerp(double x, int x1, int x2, int y1, int y2)
48 assert (dx > 0 && dy >= 0 && x1 <= x && x <= x2);
49 return y1 + (x-x1) * dy / dx;
53 FcWeightFromOpenTypeDouble (double ot_weight)
60 ot_weight = FC_MIN (ot_weight, map[(sizeof (map) / sizeof (map[0])) - 1].ot);
62 for (i = 1; ot_weight > map[i].ot; i++)
65 if (ot_weight == map[i].ot)
68 /* Interpolate between two items. */
69 return lerp (ot_weight, map[i-1].ot, map[i].ot, map[i-1].fc, map[i].fc);
73 FcWeightToOpenTypeDouble (double fc_weight)
76 if (fc_weight < 0 || fc_weight > FC_WEIGHT_EXTRABLACK)
79 for (i = 1; fc_weight > map[i].fc; i++)
82 if (fc_weight == map[i].fc)
85 /* Interpolate between two items. */
86 return lerp (fc_weight, map[i-1].fc, map[i].fc, map[i-1].ot, map[i].ot);
90 FcWeightFromOpenType (int ot_weight)
92 return FcWeightFromOpenTypeDouble (ot_weight) + .5;
96 FcWeightToOpenType (int fc_weight)
98 return FcWeightToOpenTypeDouble (fc_weight) + .5;
102 #include "fcaliastail.h"