aboutsummaryrefslogtreecommitdiffstats
path: root/libfonts_encode_font_description.c
blob: 5c25315f46110ec7e1c84c0dd6bdba59611a5c0e (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
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


#define LIST_FIELDS(X, _)\
	X(foundry) _\
	X(family_name) _\
	X(weight_name) _\
	X(slant) _\
	X(setwidth_name) _\
	X(add_style_name) _\
	X(pixel_size) _\
	X(point_size) _\
	X(resolution_x) _\
	X(resolution_y) _\
	X(spacing) _\
	X(average_width) _\
	X(charset_registry) _\
	X(charset_encoding)


static char *
encode(char *out, const char *s)
{
	for (; *s; s++)
		*out++ = (*s == '-' ? '~' : *s);
	return out;
}


static char *
encode_range(char *out, const char *s)
{
	if (*s == '-') {
		*out++ = '~';
		s++;
	}
	for (; *s; s++)
		*out++ = (*s == '-' ? isdigit(s[-1]) ? '_' : '~' : *s);
	return out;
}


int
libfonts_encode_font_description(const struct libfonts_font_description *desc, char out[static 256])
{
	size_t n = 1;
	char *p;

	if (desc->private_font_name) {
		if (strlen(desc->private_font_name) > 255)
			goto einval;
		stpcpy(out, desc->private_font_name);
		return 0;
	}

	if (desc->xlfd_version)
		n += strlen(desc->xlfd_version) + 1;

#define X(F)\
	if (!desc->F)\
		goto einval;\
	n += strlen(desc->F) + 1
	LIST_FIELDS(X, ;);
#undef X

	if (desc->charset_subset)
		n += strlen(desc->charset_subset) + 2;

	if (desc->unrecognised_fields)
		n += strlen(desc->unrecognised_fields) + 1;

	if (n > 255)
		goto einval;

	p = out;
	if (desc->xlfd_version) {
		*p++ = '+';
		p = encode(p, desc->xlfd_version);
	}

#define X(F)\
	*p++ = '-';\
	p = encode(p, desc->F)
	LIST_FIELDS(X, ;);
#undef X

	if (desc->charset_subset) {
		*p++ = '[';
		p = encode_range(p, desc->charset_subset);
		*p++ = ']';
	}

	if (desc->unrecognised_fields) {
		*p++ = '-';
		p = stpcpy(p, desc->unrecognised_fields);
	}

	*p = '\0';
	return 0;

einval:
	errno = EINVAL;
	return -1;
}


#else


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


#endif