Imported Upstream version 2.11.93
[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         /* Follow WPF Font Selection Model's advice. */
58         if (1 <= ot_weight && ot_weight <= 9)
59             ot_weight *= 100;
60
61         /* WPF Font Selection Model rejects 1000, we allow it
62          * because Pango uses that number. */
63         if (ot_weight < 1 || ot_weight > 1000)
64             return -1;
65
66         for (i = 1; ot_weight > map[i].ot; i++)
67           ;
68
69         if (ot_weight == map[i].ot)
70           return map[i].fc;
71
72         /* Interpolate between two items. */
73         return lerp (ot_weight, map[i-1].ot, map[i].ot, map[i-1].fc, map[i].fc);
74 }
75
76 int
77 FcWeightToOpenType (int fc_weight)
78 {
79         int i;
80         if (fc_weight < 0 || fc_weight > FC_WEIGHT_EXTRABLACK)
81             return -1;
82
83         for (i = 1; fc_weight > map[i].fc; i++)
84           ;
85
86         if (fc_weight == map[i].fc)
87           return map[i].ot;
88
89         /* Interpolate between two items. */
90         return lerp (fc_weight, map[i-1].fc, map[i].fc, map[i-1].ot, map[i].ot);
91 }
92
93 #define __fcweight__
94 #include "fcaliastail.h"
95 #undef __fcweight__