-
Notifications
You must be signed in to change notification settings - Fork 0
/
specimen.c
459 lines (401 loc) · 10.8 KB
/
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* 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 <fontconfig/fontconfig.h>
#include "specimen.h"
#include "unicode.h"
#include "fc.h"
#include "ft.h"
#include "img_png.h"
#include "error.h"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define MAX_PX_SIZE 100
#define MAX_SENTENCE_LEN 50
typedef struct
{
int x;
int y;
int pxsize;
uint32_t *sentence;
FcPattern *pattern;
const char *script;
const char *lang;
text_dir_t dir;
int grayscale;
} specimen_string_t;
void specimen_set_debug(int on)
{
set_debug(on);
}
static int strings_waterfall(uint32_t string[],
FcPattern *pattern,
const char *script,
const char *lang,
text_dir_t dir,
specimen_string_t **strings,
int *width,
int *height)
{
const int size_from = 7, size_step = 1;
int size_to = 35;
const int strings_dist = 7;
int s, nsizes;
int sumsizes;
int i;
int x, y, len;
size_to = size_from + ((size_to - size_from)/size_step)*size_step;
nsizes = (size_to - size_from)/size_step + 1;
sumsizes = 0;
for (s = size_from; s <= size_to; s++)
{
sumsizes += s;
}
if (nsizes <= 0 || size_to > MAX_PX_SIZE)
{
font_specimen_error("specimen: wrong size intervals");
return -1;
}
*strings = (specimen_string_t*)malloc(nsizes*sizeof(specimen_string_t));
if (!*strings)
{
font_specimen_error("specimen: not enough memory");
return -1;
}
len = ft_text_length(string, pattern, size_to, dir, script, lang);
if (dir < 2)
{
if (! *width)
*width = len;
if (! *height)
*height = sumsizes + (nsizes)*strings_dist;
}
else
{
if (! *width)
*width = sumsizes + (nsizes)*strings_dist;
if (! *height)
*height = len;
}
switch (dir)
{
case TDIR_L2R: /* left to right */
x = 0;
y = size_from + strings_dist;
break;
case TDIR_R2L: /* right to left */
x = *width;
y = size_from + strings_dist;
break;
case TDIR_T2B: /* top to bottom */
x = size_from;
y = 0;
break;
case TDIR_B2T: /* bottom to top */
x = size_from;
y = *height - strings_dist;
break;
default:
return -2;
}
for (s = size_from, i = 0; i < nsizes; s += size_step, i++)
{
(*strings)[i].sentence = string;
(*strings)[i].x = x;
(*strings)[i].y = y;
(*strings)[i].pxsize = s;
(*strings)[i].pattern = pattern;
(*strings)[i].script = script;
(*strings)[i].lang = lang;
(*strings)[i].dir = dir;
(*strings)[i].grayscale = 100;
if (dir < 2)
y += s + strings_dist;
else
x += s + strings_dist;
}
return nsizes;
}
static int strings_compact(uint32_t string[],
FcPattern *pattern,
const char *script,
const char *lang,
text_dir_t dir,
specimen_string_t **strings,
int *width,
int *height)
{
const int sizes[] = {7, 8, 9, 10, 12, 15, 20, 35};
const int shadow_size = 70;
const int strings_dist = 10;
int nsizes = sizeof(sizes)/sizeof(sizes[0]);
int sumsizes;
int i;
int x, y, len;
sumsizes = 0;
for (i = 0; i < nsizes; i++)
sumsizes += sizes[i];
*strings = (specimen_string_t*)malloc((nsizes + 1)*sizeof(specimen_string_t));
if (!*strings)
{
font_specimen_error("specimen: no memory");
return -1;
}
len = ft_text_length(string, pattern, sizes[nsizes-1], dir, script, lang);
if (len < 0)
return -1;
if (dir < 2)
{
if (!*width)
*width = len;
if (!*height)
*height = sumsizes + (nsizes + 1)*strings_dist;
}
else
{
if (!*width)
*width = sumsizes + (nsizes + 1)*strings_dist;
if (!*height)
*height = len;
}
switch (dir)
{
case TDIR_L2R: /* left to right */
x = 0;
y = sizes[0] + strings_dist;
break;
case TDIR_R2L: /* right to left */
x = *width;
y = sizes[0] + strings_dist;
break;
case TDIR_T2B: /* top to bottom */
x = sizes[0];
y = 0;
break;
case TDIR_B2T: /* bottom to top */
x = sizes[0];
y = *height - strings_dist;
break;
default:
return -2;
}
i = 0;
while (1)
{
(*strings)[i].sentence = string;
(*strings)[i].x = x;
(*strings)[i].y = y;
(*strings)[i].pxsize = sizes[i];
(*strings)[i].pattern = pattern;
(*strings)[i].script = script;
(*strings)[i].lang = lang;
(*strings)[i].dir = dir;
(*strings)[i].grayscale = 100;
if (++i == nsizes)
break;
if (dir < 2)
y += sizes[i] + strings_dist;
else
x += sizes[i] + strings_dist;
}
/* gray big on the background */
(*strings)[i].sentence = string;
switch(dir)
{
case TDIR_L2R:
(*strings)[i].x = shadow_size/2;
(*strings)[i].y = shadow_size;
break;
case TDIR_R2L:
(*strings)[i].x = *width - shadow_size/2;
(*strings)[i].y = shadow_size;
break;
case TDIR_T2B:
(*strings)[i].x = shadow_size/2;
(*strings)[i].y = shadow_size/2;
break;
case TDIR_B2T:
(*strings)[i].x = shadow_size/2;
(*strings)[i].y = shadow_size/2;
break;
}
(*strings)[i].pxsize = shadow_size;
(*strings)[i].pattern = pattern;
(*strings)[i].script = script;
(*strings)[i].lang = lang;
(*strings)[i].dir = dir;
(*strings)[i].grayscale = 25;
return nsizes + 1;
}
int specimen_font_scripts(const char *font,
script_sort_t sort,
const char *scripts[],
double coverages[],
int maxscripts)
{
uinterval_stat_t *stats;
int i, nintervals;
FcPattern *pat, *fnt;
pat = fontconfig_get_pattern(font);
if (! pat)
return -1;
fnt = fontconfig_get_font(pat);
if (! fnt)
return -1;
nintervals = unicode_interval_statistics(fnt, &stats,
UI_SCRIPT, sort);
if (nintervals < 0)
return -1;
for (i = 0; i < nintervals && i < maxscripts; i++)
{
scripts[i] = stats[i].ui_name;
coverages[i] = stats[i].coverage;
}
free(stats);
fontconfig_pattern_destroy(pat);
fontconfig_pattern_destroy(fnt);
return i;
}
int specimen_write(specimen_type_t type,
const char *font,
const char *script,
FILE *png,
int width,
int height)
{
FcPattern *pat, *fnt;
text_dir_t dir;
img_transform_t transform;
const char *lang;
int random;
uint32_t sentence[MAX_SENTENCE_LEN];
int nstrings;
int tmp;
bitmap_t bitmap;
int ord, lcdfilter;
int t;
specimen_string_t *strings;
int o, value;
const char *rendering_options_bool[] = { FC_ANTIALIAS,
FC_HINTING,
FC_AUTOHINT,
NULL };
const char *rendering_options_int[] = { FC_HINT_STYLE,
FC_LCD_FILTER,
FC_RGBA,
FC_EMBEDDED_BITMAP,
NULL };
pat = fontconfig_get_pattern(font);
if (! pat)
return -1;
fnt = fontconfig_get_font(pat);
if (! fnt)
return -1;
/* set requested rendering options which are lost
via current system's fontconfig */
o = 0;
while (rendering_options_bool[o])
{
if (fontconfig_pattern_get_bool(pat, rendering_options_bool[o], &value) == 0)
fontconfig_pattern_set_bool(fnt, rendering_options_bool[o], value);
o++;
}
o = 0;
while (rendering_options_int[o])
{
if (fontconfig_pattern_get_integer(pat, rendering_options_int[o], &value) == 0)
fontconfig_pattern_set_integer(fnt, rendering_options_int[o], value);
o++;
}
fontconfig_pattern_destroy(pat);
if (unicode_specimen_sentence(fnt, NULL, script, MAX_SENTENCE_LEN,
&dir, &transform, &lang,
&random, sentence) < 0)
return -1;
if (random == 2)
{
font_specimen_error("specimen: no symbols for this font and script");
return -1;
}
switch (type)
{
case SPECIMEN_WATERFALL:
nstrings = strings_waterfall(sentence, fnt, script,
lang, dir, &strings,
&width, &height);
if (nstrings < 0)
return -1;
break;
case SPECIMEN_COMPACT:
nstrings = strings_compact(sentence, fnt, script,
lang, dir, &strings,
&width, &height);
if (nstrings < 0)
return -1;
break;
default:
return -1;
}
switch (transform)
{
case TRNS_ROT270:
tmp = width;
width = height;
height = tmp;
break;
case TRNS_NONE:
break;
default:
return -1;
}
if (fontconfig_pattern_get_integer(fnt, FC_RGBA, &ord) < 0)
ord = FC_RGBA_UNKNOWN;
if (fontconfig_pattern_get_integer(fnt, FC_LCD_FILTER, &lcdfilter) < 0)
lcdfilter = FC_LCD_NONE;
lcdfilter = (lcdfilter != 3 ? lcdfilter : 16);
if (ft_initialize_bitmap(&bitmap, height, width, ord, lcdfilter) < 0)
return -1;
for (t = 0; t < nstrings; t++)
{
if (ft_bitmap_set_font(&bitmap, strings[t].pattern, strings[t].pxsize,
strings[t].grayscale, strings[t].dir, strings[t].script,
strings[t].lang) < 0)
return -1;
if (ft_draw_text(strings[t].sentence, strings[t].x,
strings[t].y, &bitmap) < 0)
return -1;
}
switch (transform)
{
case TRNS_ROT270:
ft_rot270(&bitmap);
break;
case TRNS_NONE:
break;
default:
return -1;
}
if (img_png_write(png, bitmap) < 0)
return -1;
ft_free_bitmap(&bitmap);
free(strings);
fontconfig_pattern_destroy(fnt);
return 0;
}