aboutsummaryrefslogtreecommitdiffstats
path: root/libfonts_do_decoded_font_descriptions_match.c
blob: 59c38d20d5798fd94ac73e313537c27fe1d6911c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


#define LIST_ALL_FIELDS(X, _)\
	X(xlfd_version, equal) _\
	X(foundry, equal) _\
	X(family_name, equal) _\
	X(weight_name, equal) _\
	X(slant, equal) _\
	X(setwidth_name, equal) _\
	X(add_style_name, equal) _\
	X(pixel_size, zero_or_equal) _\
	X(point_size, zero_or_equal) _\
	X(resolution_x, zero_or_equal) _\
	X(resolution_y, zero_or_equal) _\
	X(spacing, equal) _\
	X(average_width, equal) _\
	X(charset_registry, equal) _\
	X(charset_encoding, equal) _\
	X(charset_subset, super)


static const char *
read_uint32(uint32_t *restrict nump, const char *s)
{
	uint32_t r;
	for (r = 0; isdigit(*s); s++)
		r = r * 10 + (uint32_t)(*s & 15);
	*nump = r;
	return s;
}

static int
super(const char *desc, const char *spec)
{
	uint32_t dl = 0, dh = 0, sl = 0, sh = 0;
	int have_d = 0, have_s = 0;

	if (!spec || !desc)
		return 1;

	while (*desc && *spec) {
		if (!have_d) {
			have_d = 1;
			if (!isdigit(*desc))
				return 0;
			desc = read_uint32(&dl, desc);
			if (desc[0] == '-' && isdigit(desc[1]))
				desc = read_uint32(&dh, &desc[1]);
			else
				dh = dl;
			if (*desc) {
				if (*desc != ' ')
					return 0;
				desc++;
			}
		}

		if (!have_s) {
			have_s = 1;
			if (!isdigit(*spec))
				return 0;
			spec = read_uint32(&sl, spec);
			if (spec[0] == '-' && isdigit(spec[1]))
				spec = read_uint32(&sh, &spec[1]);
			else
				sh = sl;
			if (*spec) {
				if (*spec != ' ')
					return 0;
				spec++;
			}
		}

		if (sl < dl)
			return 0;
		else if (sl > dh)
			have_d = 0;
		else if (sh > dh)
			return 0;
		else
			have_s = 0;
	}

	return !*spec;
}

static int
equal(const char *desc, const char *spec)
{
	size_t i;

	if (!spec)
		return !desc;
	if (spec[0] == '*' && !spec[1])
		return 1;
	if (!desc)
		return 0;

	for (; desc[i] && spec[i]; i++)
		if (tolower(spec[i]) != tolower(desc[i]) && spec[i] != '?')
			return 0;

	return desc[i] == spec[i];
}

static int
zero_or_equal(const char *desc, const char *spec)
{
	if (desc && desc[0] == '0' && !desc[1])
		return 1;
	else
		return equal(desc, spec);
}

static int
many_equal(const char *desc, const char *spec)
{
	if (!spec)
		return !desc;
	if (!desc)
		return 0;

	while (*spec && *desc) {
		if (spec[0] == '*' && (!spec[1] || spec[1] == '-')) {
			spec++;
			while (*desc && *desc != '-')
				desc++;
		} else {
			while (*spec && *desc && *spec != '-' && *desc != '-') {
				if (tolower(*spec) != tolower(*desc) && *spec != '?')
					return 0;
			}
		}
		if (*spec != *desc || (*spec && *spec != '-'))
			return 0;
	}

	return *spec == *desc;
}

int
libfonts_do_decoded_font_descriptions_match(const struct libfonts_font_description *desc,
                                            const struct libfonts_font_description *spec)
{
	if (desc->private_font_name || spec->private_font_name)
		return many_equal(desc->private_font_name, spec->private_font_name);

#define X(FIELD, FUNC)\
	if (!FUNC(desc->FIELD, spec->FIELD))\
		return 0
	LIST_ALL_FIELDS(X, ;);
#undef X

	return many_equal(desc->unrecognised_fields, spec->unrecognised_fields);
}


#else


int
main(void)
{
	return 0; /* XXX add test */
}


#endif