Coverage Report

Created: 2025-07-07 10:01

/work/workdir/UnpackedTarball/fontconfig/src/fcweight.c
Line
Count
Source (jump to first uncovered line)
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
45
lerp (double x, int x1, int x2, int y1, int y2)
46
0
{
47
0
    int dx = x2 - x1;
48
0
    int dy = y2 - y1;
49
0
    assert (dx > 0 && dy >= 0 && x1 <= x && x <= x2);
50
0
    return y1 + (x - x1) * dy / dx;
51
0
}
52
53
double
54
FcWeightFromOpenTypeDouble (double ot_weight)
55
13
{
56
13
    int i;
57
58
13
    if (ot_weight < 0)
59
0
  return -1;
60
61
13
    ot_weight = FC_MIN (ot_weight, map[(sizeof (map) / sizeof (map[0])) - 1].ot);
62
63
96
    for (i = 1; ot_weight > map[i].ot; i++)
64
83
  ;
65
66
13
    if (ot_weight == map[i].ot)
67
13
  return map[i].fc;
68
69
    /* Interpolate between two items. */
70
0
    return lerp (ot_weight, map[i - 1].ot, map[i].ot, map[i - 1].fc, map[i].fc);
71
13
}
72
73
double
74
FcWeightToOpenTypeDouble (double fc_weight)
75
0
{
76
0
    int i;
77
0
    if (fc_weight < 0 || fc_weight > FC_WEIGHT_EXTRABLACK)
78
0
  return -1;
79
80
0
    for (i = 1; fc_weight > map[i].fc; i++)
81
0
  ;
82
83
0
    if (fc_weight == map[i].fc)
84
0
  return map[i].ot;
85
86
    /* Interpolate between two items. */
87
0
    return lerp (fc_weight, map[i - 1].fc, map[i].fc, map[i - 1].ot, map[i].ot);
88
0
}
89
90
int
91
FcWeightFromOpenType (int ot_weight)
92
0
{
93
0
    return FcWeightFromOpenTypeDouble (ot_weight) + .5;
94
0
}
95
96
int
97
FcWeightToOpenType (int fc_weight)
98
0
{
99
0
    return FcWeightToOpenTypeDouble (fc_weight) + .5;
100
0
}
101
102
#define __fcweight__
103
#include "fcaliastail.h"
104
#undef __fcweight__