Imported Upstream version 2.12.1
[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     { 350, FC_WEIGHT_DEMILIGHT },
33     { 300, FC_WEIGHT_LIGHT },
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 int lerp(int 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 int
53 FcWeightFromOpenType (int ot_weight)
54 {
55         int i;
56
57         /* Loosely based on WPF Font Selection Model's advice. */
58
59         if (ot_weight < 0)
60             return -1;
61         else if (1 <= ot_weight && ot_weight <= 9)
62         {
63             /* WPF Font Selection Model says do "ot_weight *= 100",
64              * but Greg Hitchcock revealed that GDI had a mapping
65              * reflected below: */
66             switch (ot_weight) {
67                 case 1: ot_weight =  80; break;
68                 case 2: ot_weight = 160; break;
69                 case 3: ot_weight = 240; break;
70                 case 4: ot_weight = 320; break;
71                 case 5: ot_weight = 400; break;
72                 case 6: ot_weight = 550; break;
73                 case 7: ot_weight = 700; break;
74                 case 8: ot_weight = 800; break;
75                 case 9: ot_weight = 900; break;
76             }
77         }
78         ot_weight = FC_MIN (ot_weight, map[(sizeof (map) / sizeof (map[0])) - 1].ot);
79
80         for (i = 1; ot_weight > map[i].ot; i++)
81           ;
82
83         if (ot_weight == map[i].ot)
84           return map[i].fc;
85
86         /* Interpolate between two items. */
87         return lerp (ot_weight, map[i-1].ot, map[i].ot, map[i-1].fc, map[i].fc);
88 }
89
90 int
91 FcWeightToOpenType (int fc_weight)
92 {
93         int i;
94         if (fc_weight < 0 || fc_weight > FC_WEIGHT_EXTRABLACK)
95             return -1;
96
97         for (i = 1; fc_weight > map[i].fc; i++)
98           ;
99
100         if (fc_weight == map[i].fc)
101           return map[i].ot;
102
103         /* Interpolate between two items. */
104         return lerp (fc_weight, map[i-1].fc, map[i].fc, map[i-1].ot, map[i].ot);
105 }
106
107 #define __fcweight__
108 #include "fcaliastail.h"
109 #undef __fcweight__