-
Notifications
You must be signed in to change notification settings - Fork 19
/
fontconv.c
200 lines (159 loc) · 3.76 KB
/
fontconv.c
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <WINGs/WUtil.h>
#include "../src/wconfig.h"
#include "common.h"
#define DEFAULT_FONT "sans serif:pixelsize=12"
/* X Font Name Suffix field names */
enum {
FOUNDRY,
FAMILY_NAME,
WEIGHT_NAME,
SLANT,
SETWIDTH_NAME,
ADD_STYLE_NAME,
PIXEL_SIZE,
POINT_SIZE,
RESOLUTION_X,
RESOLUTION_Y,
SPACING,
AVERAGE_WIDTH,
CHARSET_REGISTRY,
CHARSET_ENCODING
};
static int countChar(const char *str, char c)
{
int count = 0;
if (!str)
return 0;
for (; *str != 0; str++) {
if (*str == c) {
count++;
}
}
return count;
}
typedef struct str {
const char *str;
int len;
} str;
#define XLFD_TOKENS 14
static str *getXLFDTokens(const char *xlfd)
{
static str tokens[XLFD_TOKENS];
int i, len, size;
const char *ptr;
/* XXX: why does this assume there can't ever be XFNextPrefix? */
if (!xlfd || *xlfd != '-' || countChar(xlfd, '-') != XLFD_TOKENS)
return NULL;
memset(tokens, 0, sizeof(str) * XLFD_TOKENS);
len = strlen(xlfd);
for (ptr = xlfd, i = 0; i < XLFD_TOKENS && len > 0; i++) {
/* skip one '-' */
ptr++;
len--;
if (len <= 0)
break;
size = strcspn(ptr, "-,");
tokens[i].str = ptr;
tokens[i].len = size;
ptr += size;
len -= size;
}
return tokens;
}
static int strToInt(str * token)
{
static char buf[32]; /* enough for an Incredibly Big Number */
if (token->len == 0 ||
token->str[0] == '*' ||
token->len >= sizeof(buf))
return -1;
memset(buf, 0, sizeof(buf));
strncpy(buf, token->str, token->len);
/* the code using this will gracefully handle overflows */
return (int)strtol(buf, NULL, 10);
}
static char *mapWeightToName(str * weight)
{
static const char *normalNames[] = { "medium", "normal", "regular" };
static char buf[32];
size_t i;
if (weight->len == 0)
return "";
for (i = 0; i < wlengthof(normalNames); i++) {
if (strlen(normalNames[i]) == weight->len && strncmp(normalNames[i], weight->str, weight->len)) {
return "";
}
}
snprintf(buf, sizeof(buf), ":%.*s", weight->len, weight->str);
return buf;
}
static char *mapSlantToName(str * slant)
{
if (slant->len == 0)
return "";
switch (slant->str[0]) {
case 'i':
return ":italic";
case 'o':
return ":oblique";
case 'r':
default:
return "";
}
}
static char *xlfdToFc(const char *xlfd, const char *useFamily, Bool keepXLFD)
{
str *tokens, *family, *weight, *slant;
char *name, buf[64];
int size, pixelsize;
tokens = getXLFDTokens(xlfd);
if (!tokens)
return wstrdup(DEFAULT_FONT);
family = &(tokens[FAMILY_NAME]);
weight = &(tokens[WEIGHT_NAME]);
slant = &(tokens[SLANT]);
pixelsize = strToInt(&tokens[PIXEL_SIZE]);
size = strToInt(&tokens[POINT_SIZE]);
if (useFamily) {
name = wstrdup(useFamily);
} else {
if (family->len == 0 || family->str[0] == '*')
return wstrdup(DEFAULT_FONT);
snprintf(buf, sizeof(buf), "%.*s", family->len, family->str);
name = wstrdup(buf);
}
if (size > 0 && pixelsize <= 0) {
snprintf(buf, sizeof(buf), "-%d", size / 10);
name = wstrappend(name, buf);
}
name = wstrappend(name, mapWeightToName(weight));
name = wstrappend(name, mapSlantToName(slant));
if (size <= 0 && pixelsize <= 0) {
name = wstrappend(name, ":pixelsize=12");
} else if (pixelsize > 0) {
/* if pixelsize is present size will be ignored so we skip it */
snprintf(buf, sizeof(buf), ":pixelsize=%d", pixelsize);
name = wstrappend(name, buf);
}
if (keepXLFD) {
name = wstrappend(name, ":xlfd=");
name = wstrappend(name, xlfd);
}
return name;
}
/* return converted font (if conversion is needed) else the original font */
char *convertFont(char *font, Bool keepXLFD)
{
if (font[0] == '-') {
if (MB_CUR_MAX < 2) {
return xlfdToFc(font, NULL, keepXLFD);
} else {
return xlfdToFc(font, "sans serif", keepXLFD);
}
} else {
return font;
}
}