-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont-specimen.c
241 lines (215 loc) · 5.83 KB
/
font-specimen.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* font-specimen
*
* Display Font Specimen
*
* Copyright (C) 2014 Petr Gajdos (pgajdos at suse)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "specimen.h"
/* removes spaces and slashes from string */
void remove_spaces_and_slashes(char *string)
{
char *p1 = string, *p2 = string;
while (*p2 != '\0')
{
if (!isspace(*p2) && *p2 != '/')
{
*p1 = *p2;
p1++;
}
else
{
/* a capital letter before space */
/* as side efect of this, both 'ETL fixed' */
/* and 'ETL Fixed' maps to the 'ETLFixed' */
*(p2 + 1) = toupper(*(p2 + 1));
}
p2++;
}
*p1 = '\0';
return;
}
void usage(const char *err)
{
if (err)
fprintf(stderr, "ERROR: %s\n\n", err);
fprintf(stderr, "Usage: font-specimen [-d] -p pattern [-option1 value1 [...]]\n");
fprintf(stderr, " font-specimen [-d] -l\n");
fprintf(stderr, "\n");
fprintf(stderr, " Generates specimen for given font and\n");
fprintf(stderr, " writes it to PNG file.\n");
fprintf(stderr, "\n");
fprintf(stderr, " -p string: fontconfig-like pattern including rendering options\n");
fprintf(stderr, " [mandatory; otherwise usage is displayed]\n");
fprintf(stderr, " -s string: unicode script name to take the sentence from\n");
fprintf(stderr, " [default value: the most coveraged script]\n");
fprintf(stderr, " -o string: name of the file\n");
fprintf(stderr, " [default value: ${pattern}-${script}.png]\n");
fprintf(stderr, " -t string: type of specimen [waterfall, compact]\n");
fprintf(stderr, " [default value: compact]\n");
fprintf(stderr, " -w int: width of the PNG, 0 for auto\n");
fprintf(stderr, " [default value: 0]\n");
fprintf(stderr, " -h int: height of th PNG, 0 for auto\n");
fprintf(stderr, " [default value: 0]\n");
fprintf(stderr, "\n");
fprintf(stderr, " -l lists significant scripts and its coverage for\n");
fprintf(stderr, " given font (do not write any png)\n");
fprintf(stderr, " -d print errors\n");
}
int main(int argc, char *argv[])
{
const int maxscripts = 20;
int opt;
const char *pattern;
const char *script;
specimen_type_t type;
int width, height;
int script_list;
char pngname[FILENAME_MAX];
const char *scripts[maxscripts];
double coverages[maxscripts];
int s, nscripts;
FILE *png;
pattern = NULL;
script_list = 0;
script = NULL;
pngname[0] = '\0';
width = height = 0;
type = SPECIMEN_COMPACT;
while ((opt = getopt(argc, argv, "p:s:o:lt:w:h:d")) != -1)
{
switch (opt)
{
case 'p':
pattern = optarg;
break;
case 's':
script = optarg;
break;
case 'o':
snprintf(pngname, FILENAME_MAX, "%s", optarg);
break;
case 't':
if (strcmp(optarg, "compact") == 0)
{
type = SPECIMEN_COMPACT;
}
else if (strcmp(optarg, "waterfall") == 0)
{
type = SPECIMEN_WATERFALL;
}
else
{
usage("Wrong type of specimen.");
return 1;
}
break;
case 'w':
width = atoi(optarg);
if (width < 0)
{
usage("Wrong width.");
return 1;
}
break;
case 'h':
height = atoi(optarg);
if (height < 0)
{
usage("Wrong height.");
return 1;
}
break;
case 'd':
specimen_set_debug(1);
break;
case 'l':
script_list = 1;
break;
}
}
if (!pattern)
{
usage(NULL);
return 1;
}
nscripts = specimen_font_scripts(pattern, SCRIPT_SORT_PERCENT,
scripts, coverages, maxscripts);
if (nscripts < 0)
{
fprintf(stderr, "Can not get list of scripts from the font.\n");
return 1;
}
if (script_list)
{
if (nscripts == 0)
{
fprintf(stderr, "No valid scripts detected in the font.\n");
return 1;
}
for (s = 0; s < nscripts; s++)
{
fprintf(stdout, "%s (%.1f)\n",
scripts[s], coverages[s]);
}
return 0;
}
if (!script)
{
if (nscripts > 0)
script = scripts[0];
else
{
fprintf(stderr, "No script found in given font.\n");
return 1;
}
}
else
{
for (s = 0; s < nscripts; s++)
if (strcmp(script, scripts[s]) == 0)
break;
if (s == nscripts)
{
fprintf(stderr, "Given script not found in the font.\n");
return 1;
}
}
if (pngname[0] == '\0')
{
snprintf(pngname, FILENAME_MAX, "%s-%s.png", pattern, script);
remove_spaces_and_slashes(pngname);
}
fprintf(stderr, "Writing %s.\n", pngname);
png = fopen(pngname, "wb");
if (!png)
{
fprintf(stderr, "Can not open %s.\n", pngname);
return 1;
}
if (specimen_write(type, pattern, script, png, width, height) < 0)
{
fprintf(stderr, "Can not write specimen.\n");
return 1;
}
return 0;
}