Imported Upstream version 2.13.0
[platform/upstream/fontconfig.git] / src / fcweight.c
1 /*
2  * fontconfig/src/fcweight.c
3  *
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.
13  *
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.
21  */
22
23 #include "fcint.h"
24
25 static const struct {
26   int ot;
27   int fc;
28 } map[] = {
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 },
42 };
43
44 static double lerp(double x, int x1, int x2, int y1, int y2)
45 {
46   int dx = x2 - x1;
47   int dy = y2 - y1;
48   assert (dx > 0 && dy >= 0 && x1 <= x && x <= x2);
49   return y1 + (dy*(x-x1) + dx/2) / dx;
50 }
51
52 double
53 FcWeightFromOpenTypeDouble (double ot_weight)
54 {
55         int i;
56
57         if (ot_weight < 0)
58             return -1;
59
60         ot_weight = FC_MIN (ot_weight, map[(sizeof (map) / sizeof (map[0])) - 1].ot);
61
62         for (i = 1; ot_weight > map[i].ot; i++)
63           ;
64
65         if (ot_weight == map[i].ot)
66           return map[i].fc;
67
68         /* Interpolate between two items. */
69         return lerp (ot_weight, map[i-1].ot, map[i].ot, map[i-1].fc, map[i].fc);
70 }
71
72 double
73 FcWeightToOpenTypeDouble (double fc_weight)
74 {
75         int i;
76         if (fc_weight < 0 || fc_weight > FC_WEIGHT_EXTRABLACK)
77             return -1;
78
79         for (i = 1; fc_weight > map[i].fc; i++)
80           ;
81
82         if (fc_weight == map[i].fc)
83           return map[i].ot;
84
85         /* Interpolate between two items. */
86         return lerp (fc_weight, map[i-1].fc, map[i].fc, map[i-1].ot, map[i].ot);
87 }
88
89 int
90 FcWeightFromOpenType (int ot_weight)
91 {
92         return FcWeightFromOpenTypeDouble (ot_weight) + .5;
93 }
94
95 int
96 FcWeightToOpenType (int fc_weight)
97 {
98         return FcWeightToOpenTypeDouble (fc_weight) + .5;
99 }
100
101 #define __fcweight__
102 #include "fcaliastail.h"
103 #undef __fcweight__