-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft.c
554 lines (464 loc) · 13.5 KB
/
ft.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
* 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 <ft2build.h>
#include FT_FREETYPE_H
#include FT_OPENTYPE_VALIDATE_H
#include FT_TRUETYPE_TABLES_H
#include FT_GLYPH_H
#include "ft.h"
#include "hbz.h"
#include "fc.h"
#include "error.h"
char *freetype_version(char *string, int maxlen)
{
int major, minor, patch;
FT_Library library;
if (FT_Init_FreeType(&library))
{
font_specimen_error("freetype: can not initialize library");
return NULL;
}
FT_Library_Version(library, &major, &minor, &patch);
FT_Done_FreeType(library);
snprintf(string, maxlen, "%d.%d.%d", major, minor, patch);
return string;
}
int ft_initialize_bitmap(bitmap_t *bitmap, int height, int width,
int ord, int lcdfilter)
{
int row;
FT_Error err;
if (lay_horizontal(ord))
width *= 3;
if (lay_vertical(ord))
height *= 3;
bitmap->data = (unsigned char **)malloc(height*sizeof(unsigned char *));
if (! bitmap->data)
{
font_specimen_error("freetype: no free memory");
return -1;
}
for (row = 0; row < height; row++)
{
bitmap->data[row] = (unsigned char *)malloc(width*sizeof(unsigned char));
if (! bitmap->data[row])
{
font_specimen_error("freetype: out of memory");
return -1;
}
memset(bitmap->data[row], 255, width*sizeof(unsigned char)); /* white */
}
bitmap->height = height;
bitmap->width = width;
err = FT_Init_FreeType(&bitmap->library);
if (err)
{
font_specimen_error("freetype: can not initialize library");
return -1;
}
bitmap->face = NULL;
bitmap->load_flags = FT_LOAD_DEFAULT;
bitmap->render_mode = FT_RENDER_MODE_NORMAL;
/* these are common for whole specimen image */
if (lay_color(ord))
{
err = FT_Library_SetLcdFilter(bitmap->library, lcdfilter);
if (err)
{
font_specimen_error("freetype: can not set lcd filter");
bitmap->ord = FC_RGBA_NONE;
return 0;
}
if (lay_horizontal(ord))
{
bitmap->load_flags |= FT_LOAD_TARGET_LCD;
bitmap->render_mode = FT_RENDER_MODE_LCD;
}
if (lay_vertical(ord))
{
bitmap->load_flags |= FT_LOAD_TARGET_LCD_V;
bitmap->render_mode = FT_RENDER_MODE_LCD_V;
}
}
bitmap->ord = ord;
return 0;
}
int ft_bitmap_set_font(bitmap_t *bitmap,
FcPattern *pattern,
int pxsize,
int grayscale,
int text_direction,
const char *script,
const char *lang)
{
FcBool scalable;
FcBool hinting;
FcBool autohint;
FcBool antialias;
int hintstyle;
FcBool embeddedbitmaps;
const char *family;
const char *style;
const char *file;
const char *fontformat;
double size, real_size;
FT_Error err;
FcPattern *font, *p;
if (bitmap->face)
FT_Done_Face(bitmap->face);
if (fontconfig_pattern_get_string(pattern, FC_FILE, &file) < 0)
return -1;
if (! pxsize)
{
if (fontconfig_pattern_get_double(pattern, FC_PIXEL_SIZE, &size) < 0)
return -1;
pxsize = size;
}
if (fontconfig_pattern_get_string(pattern, FC_FONTFORMAT, &fontformat) < 0)
return -1;
if (strcmp(fontformat, "PCF") == 0)
{
/* let fontconfig return nearest size for given size and
bitmap font */
if (fontconfig_pattern_get_string(pattern, FC_FAMILY, &family) < 0)
return -1;
if (fontconfig_pattern_get_string(pattern, FC_STYLE, &style) < 0)
return -1;
if (!(p = fontconfig_pattern_new()))
return -1;
if (fontconfig_pattern_set_string(p, FC_FAMILY, family) < 0)
return -1;
if (fontconfig_pattern_set_string(p, FC_STYLE, style) < 0)
return -1;
if (fontconfig_pattern_set_double(p, FC_SIZE, (double)pxsize) < 0)
return -1;
if (! (font = fontconfig_get_font(p)))
return -1;
fontconfig_pattern_destroy(p);
if (fontconfig_pattern_get_double(font, FC_PIXEL_SIZE, &real_size) < 0)
return -1;
if (fontconfig_pattern_set_double(pattern, FC_PIXEL_SIZE, real_size) < 0)
return -1;
pxsize = (int)real_size;
if (fontconfig_pattern_get_string(font, FC_FILE, &file) < 0)
return -1;
if (fontconfig_pattern_set_string(pattern, FC_FILE, file) < 0)
return -1;
}
bitmap->grayscale = grayscale;
err = FT_New_Face(bitmap->library, (char *)file, 0, &bitmap->face);
if (err)
{
font_specimen_error("freetype: can not create face object");
return -1;
}
err = FT_Set_Pixel_Sizes(bitmap->face, 0, pxsize);
if (err)
{
font_specimen_error("freetype: can not set face size");
return -1;
}
if (fontconfig_pattern_get_bool(pattern, FC_SCALABLE, &scalable) < 0)
return -1;
if (scalable)
{
if (fontconfig_pattern_get_bool(pattern, FC_ANTIALIAS, &antialias) < 0)
antialias = FcTrue;
if (fontconfig_pattern_get_bool(pattern, FC_HINTING, &hinting) < 0)
hinting = FcTrue;
if (fontconfig_pattern_get_bool(pattern, FC_AUTOHINT, &autohint) < 0)
autohint = FcTrue;
if (fontconfig_pattern_get_integer(pattern, FC_HINT_STYLE, &hintstyle) < 0)
hintstyle = FC_HINT_MEDIUM;
if (fontconfig_pattern_get_bool(pattern, FC_EMBEDDED_BITMAP, &embeddedbitmaps) < 0)
embeddedbitmaps = FcTrue;
if (!lay_color(bitmap->ord) && !antialias)
{
bitmap->render_mode = FT_RENDER_MODE_MONO;
bitmap->load_flags |= FT_LOAD_TARGET_MONO;
}
if (!hinting)
bitmap->load_flags |= FT_LOAD_NO_HINTING;
if (autohint)
bitmap->load_flags |= FT_LOAD_FORCE_AUTOHINT;
switch(hintstyle)
{
case FC_HINT_NONE: bitmap->load_flags |= FT_LOAD_NO_HINTING; break;
case FC_HINT_SLIGHT: bitmap->load_flags |= FT_LOAD_TARGET_LIGHT; break;
case FC_HINT_MEDIUM: bitmap->load_flags |= FT_LOAD_TARGET_NORMAL; break;
case FC_HINT_FULL: bitmap->load_flags |= FT_LOAD_TARGET_NORMAL; break;
}
if (!embeddedbitmaps)
bitmap->load_flags |= FT_LOAD_NO_BITMAP;
}
bitmap->text_direction = text_direction;
bitmap->script = script;
bitmap->lang = lang;
if (text_direction >= 2)
bitmap->load_flags |= FT_LOAD_VERTICAL_LAYOUT;
return 0;
}
int ft_reduce_height(bitmap_t *bitmap, int new_height)
{
int row;
if (new_height < 0 || new_height > bitmap->height)
{
font_specimen_error("freetype: can not reduce height (wrong new_height)");
return -1;
}
for (row = bitmap->height - 1; row > new_height - 1; row--)
free(bitmap->data[row]);
bitmap->height = new_height;
return 0;
}
void ft_free_bitmap(bitmap_t *bitmap)
{
int row;
for (row = 0; row < bitmap->height; row++)
free(bitmap->data[row]);
free(bitmap->data);
bitmap->data = NULL;
bitmap->height = 0;
bitmap->width = 0;
FT_Done_Face(bitmap->face);
FT_Done_FreeType(bitmap->library);
bitmap->load_flags = 0;
}
void draw_bitmap(FT_Bitmap *glyph,
FT_Int x,
FT_Int y,
bitmap_t bitmap,
int monochrome)
{
FT_Int i, j, p, q;
FT_Int x_max, y_max;
if (lay_horizontal(bitmap.ord))
x *= 3;
if (lay_vertical(bitmap.ord))
y *= 3;
x_max = x + glyph->width;
y_max = y + glyph->rows;
for (i = x, p = 0; i < x_max; i++, p++)
{
for (j = y, q = 0; j < y_max; j++, q++)
{
if (i < 0 || j < 0 ||
i >= bitmap.width || j >= bitmap.height)
continue;
if (monochrome)
{
if (glyph->buffer[glyph->pitch * q + (p >> 3)] & (128 >> (p & 7)))
{
bitmap.data[j][i] = ~(~bitmap.data[j][i] | (255*bitmap.grayscale/100));
}
}
else
{
bitmap.data[j][i] &= ~((char)((int)glyph->buffer[q * glyph->pitch + p]*bitmap.grayscale/100));
}
}
}
}
static int ft_draw_text_(uint32_t text[], int x, int y,
bitmap_t *bitmap, FT_Bool dry);
int ft_draw_text(uint32_t text[], int x, int y, bitmap_t *bitmap)
{
return ft_draw_text_(text, x, y, bitmap, 0);
}
int ft_text_length(uint32_t text[], FcPattern *pattern, int pxsize,
int dir, const char *script, const char *lang)
{
bitmap_t bitmap;
int len;
if (ft_initialize_bitmap(&bitmap, 0, 0, FC_RGBA_UNKNOWN, FC_LCD_NONE) < 0)
return -1;
if (ft_bitmap_set_font(&bitmap, pattern, pxsize, 1.0,
dir, script, lang) < 0)
return -1;
len = ft_draw_text_(text, 0, 0, &bitmap, 1);
ft_free_bitmap(&bitmap);
return len;
}
int text_length(uint32_t text[])
{
int len = 0;
while (text[len])
len++;
return len;
}
/* dry true: do not draw the bitmap, only return string length */
static int ft_draw_text_(uint32_t text[], int x, int y,
bitmap_t *bitmap, FT_Bool dry)
{
FT_Error err;
FT_Vector pen;
FT_UInt *glyph_codepoints;
FT_Vector *glyph_offsets;
FT_Vector *glyph_advances;
FT_Vector *glyph_positions;
FT_Glyph *glyphs;
FT_BitmapGlyph bit;
int monochrome;
int nglyphs, g;
int sum_advances_x, sum_advances_y;
int text_width;
text_width = 0;
nglyphs = hbz_glyphs(text,
text_length(text),
bitmap->script,
bitmap->lang,
bitmap->text_direction,
bitmap->face,
&glyph_codepoints,
&glyph_offsets,
&glyph_advances,
&sum_advances_x,
&sum_advances_y);
if (nglyphs == -1)
return -1;
glyph_positions = malloc(nglyphs*sizeof(FT_Vector));
glyphs = malloc(nglyphs*sizeof(FT_Glyph));
if (glyph_positions == NULL || glyphs == NULL)
{
font_specimen_error("freetype: out of memory");
return -1;
}
pen.x = x << 6;
if (bitmap->text_direction == 1)
pen.x -= sum_advances_x;
pen.y = y << 6;
if (bitmap->text_direction == 3)
pen.y -= (-sum_advances_y);
if (bitmap->text_direction == 2)
pen.y -= glyph_advances[0].y - glyph_offsets[0].y;
for (g = 0; g < nglyphs; g++)
{
glyph_positions[g].x = (pen.x + glyph_offsets[g].x) >> 6;
glyph_positions[g].y = (pen.y - glyph_offsets[g].y) >> 6;
pen.x += glyph_advances[g].x;
pen.y -= glyph_advances[g].y;
err = FT_Load_Glyph(bitmap->face,
glyph_codepoints[g], bitmap->load_flags);
if (err)
{
font_specimen_error("freetype: can not load glyph");
return -1;
}
err = FT_Get_Glyph(bitmap->face->glyph, &glyphs[g]);
if (err)
{
font_specimen_error("freetype: can not get glyph");
return -1;
}
}
/* one of operands is zero */
text_width = (sum_advances_x + (-sum_advances_y)) >> 6;
free(glyph_codepoints);
free(glyph_offsets);
free(glyph_advances);
if (! dry)
{
for (g = 0; g < nglyphs; g++)
{
monochrome = 0;
if (bitmap->render_mode == FT_RENDER_MODE_MONO ||
glyphs[g]->format == FT_GLYPH_FORMAT_BITMAP)
{
monochrome = 1;
}
err = FT_Glyph_To_Bitmap(&glyphs[g], bitmap->render_mode,
NULL, 1);
if (err)
{
font_specimen_error("freetype: can not render glyph");
return -1;
}
bit = (FT_BitmapGlyph)glyphs[g];
draw_bitmap(&bit->bitmap,
glyph_positions[g].x + bit->left,
glyph_positions[g].y - bit->top,
*bitmap, monochrome);
}
}
free(glyph_positions);
free(glyphs);
if (text_width < 0)
text_width = 0;
return text_width;
}
void ft_fill_region(bitmap_t *bitmap, int left, int top,
int right, int bottom, unsigned char gray)
{
int i, j;
if (left >= bitmap->width ||
top >= bitmap->height ||
right < 0 ||
bottom < 0)
return;
if (left < 0)
left = 0;
if (top < 0)
top = 0;
if (right >= bitmap->width)
right = bitmap->width - 1;
if (bottom >= bitmap->height)
bottom = bitmap->height - 1;
for (i = left; i <= right; i++)
for (j = top; j <= bottom; j++)
bitmap->data[j][i] = gray;
}
int ft_rot270(bitmap_t *bitmap)
{
int tmp;
int row, col;
unsigned char **data;
data = bitmap->data;
tmp = bitmap->height;
bitmap->height = bitmap->width;
bitmap->width = tmp;
bitmap->data
= (unsigned char **)malloc(bitmap->height*sizeof(unsigned char *));
if (! bitmap->data)
{
font_specimen_error("freetype: no free memory");
return -1;
}
for (row = 0; row < bitmap->height; row++)
{
bitmap->data[row]
= (unsigned char *)malloc(bitmap->width*sizeof(unsigned char));
if (! bitmap->data[row])
{
font_specimen_error("freetype: no free memory");
return -1;
}
}
for (row = 0; row < bitmap->height; row++)
for (col = 0; col < bitmap->width; col++)
bitmap->data[row][bitmap->width-col-1] = data[col][row];
/* bitmap->width is now former bitmap->height */
for (row = 0; row < bitmap->width; row++)
free(data[row]);
free(data);
return 0;
}