5379
|
1 /*
|
|
2 * Renders antialiased fonts for mplayer using freetype library.
|
|
3 * Should work with TrueType, Type1 and any other font supported by libfreetype.
|
|
4 * Can generate font.desc for any encoding.
|
|
5 *
|
|
6 *
|
|
7 * Artur Zaprzala <zybi@fanthom.irc.pl>
|
|
8 *
|
|
9 */
|
|
10
|
|
11 #include <stdio.h>
|
|
12 #include <stdlib.h>
|
|
13 #include <iconv.h>
|
|
14 #include <math.h>
|
|
15 #include <string.h>
|
|
16 #include <libgen.h>
|
|
17
|
|
18 #ifndef OLD_FREETYPE2
|
|
19 #include <ft2build.h>
|
|
20 #include FT_FREETYPE_H
|
|
21 #include FT_GLYPH_H
|
|
22 #else /* freetype 2.0.1 */
|
|
23 #include <freetype/freetype.h>
|
|
24 #include <freetype/ftglyph.h>
|
|
25 #endif
|
|
26
|
|
27 #include "../config.h"
|
|
28 #include "../mp_msg.h"
|
|
29
|
|
30 #include "../bswap.h"
|
|
31
|
|
32
|
|
33 #ifndef DEBUG
|
|
34 #define DEBUG 0
|
|
35 #endif
|
|
36
|
|
37 #include "font_load.h"
|
|
38
|
|
39 //// default values
|
|
40 static char *encoding = "iso-8859-1"; /* target encoding */
|
|
41 static char *charmap = "ucs-4"; /* font charmap encoding, I hope ucs-4 is always big endian */
|
|
42 /* gcc 2.1.3 doesn't support ucs-4le, but supports ucs-4 (==ucs-4be) */
|
|
43 static float ppem = 28; /* font size in pixels */
|
|
44
|
|
45 static double radius = 2; /* blur radius */
|
|
46 static double thickness = 2.5; /* outline thickness */
|
|
47
|
|
48 //char* font_desc = "font.desc";
|
|
49 //char* font_desc = "/dev/stdout";
|
|
50
|
|
51 //char *outdir = ".";
|
|
52
|
|
53 static font_desc_t *desc=NULL;
|
|
54
|
|
55 //// constants
|
|
56 static int const colors = 256;
|
|
57 static int const maxcolor = 255;
|
|
58 static unsigned const base = 256;
|
|
59 static unsigned const first_char = 33;
|
|
60 #define max_charset_size 60000
|
|
61 //int const max_charset_size = 256;
|
|
62 static unsigned charset_size = 0;
|
|
63
|
|
64 ////
|
|
65 //static char *command;
|
|
66 //static char *encoding_name;
|
|
67 static char *font_path=NULL;
|
|
68 //char *font_metrics;
|
|
69 //static int append_mode = 0;
|
|
70 static int unicode_desc = 0;
|
|
71 static int font_id=0;
|
|
72
|
|
73 static unsigned char *bbuffer, *abuffer;
|
|
74 static int width, height;
|
|
75 static int padding;
|
|
76 static FT_ULong charset[max_charset_size]; /* characters we want to render; Unicode */
|
|
77 static FT_ULong charcodes[max_charset_size]; /* character codes in 'encoding' */
|
|
78 static iconv_t cd; // iconv conversion descriptor
|
|
79
|
|
80
|
|
81 #define eprintf(...) mp_msg(MSGT_CPLAYER,MSGL_INFO, __VA_ARGS__)
|
|
82 #define ERROR_(msg, ...) (mp_msg(MSGT_CPLAYER,MSGL_ERR,"[font_load] error: " msg "\n", __VA_ARGS__), exit(1))
|
|
83 #define WARNING_(msg, ...) mp_msg(MSGT_CPLAYER,MSGL_WARN,"[font_load] warning: " msg "\n", __VA_ARGS__)
|
|
84 #define ERROR(...) ERROR_(__VA_ARGS__, NULL)
|
|
85 #define WARNING(...) WARNING_(__VA_ARGS__, NULL)
|
|
86
|
|
87
|
|
88 #define f266ToInt(x) (((x)+32)>>6) // round fractional fixed point number to integer
|
|
89 // coordinates are in 26.6 pixels (i.e. 1/64th of pixels)
|
|
90 #define f266CeilToInt(x) (((x)+63)>>6) // ceiling
|
|
91 #define f266FloorToInt(x) ((x)>>6) // floor
|
|
92 #define f1616ToInt(x) (((x)+0x8000)>>16) // 16.16
|
|
93 #define floatTof266(x) ((int)((x)*(1<<6)+0.5))
|
|
94
|
|
95 #define ALIGN(x) (((x)+7)&~7) // 8 byte align
|
|
96
|
|
97
|
|
98
|
|
99 static void paste_bitmap(FT_Bitmap *bitmap, int x, int y) {
|
|
100 int drow = x+y*width;
|
|
101 int srow = 0;
|
|
102 int sp, dp, w, h;
|
|
103 if (bitmap->pixel_mode==ft_pixel_mode_mono)
|
|
104 for (h = bitmap->rows; h>0; --h, drow+=width, srow+=bitmap->pitch)
|
|
105 for (w = bitmap->width, sp=dp=0; w>0; --w, ++dp, ++sp)
|
|
106 bbuffer[drow+dp] = (bitmap->buffer[srow+sp/8] & (0x80>>(sp%8))) ? 255:0;
|
|
107 else
|
|
108 for (h = bitmap->rows; h>0; --h, drow+=width, srow+=bitmap->pitch)
|
|
109 for (w = bitmap->width, sp=dp=0; w>0; --w, ++dp, ++sp)
|
|
110 bbuffer[drow+dp] = bitmap->buffer[srow+sp];
|
|
111 }
|
|
112
|
|
113
|
|
114 static void render() {
|
|
115 FT_Library library;
|
|
116 FT_Face face;
|
|
117 FT_Error error;
|
|
118 FT_Glyph *glyphs;
|
|
119 FT_BitmapGlyph glyph;
|
|
120 //FILE *f;
|
|
121 int const load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
|
|
122 int pen_x = 0, pen_xa;
|
|
123 int ymin = INT_MAX, ymax = INT_MIN;
|
|
124 int i, uni_charmap = 1;
|
|
125 int baseline, space_advance = 20;
|
|
126 int glyphs_count = 0;
|
|
127
|
|
128
|
|
129 /* initialize freetype */
|
|
130 error = FT_Init_FreeType(&library);
|
|
131 if (error) ERROR("Init_FreeType failed.");
|
|
132 error = FT_New_Face(library, font_path, 0, &face);
|
|
133 if (error) ERROR("New_Face failed. Maybe the font path `%s' is wrong.", font_path);
|
|
134
|
|
135 /*
|
|
136 if (font_metrics) {
|
|
137 error = FT_Attach_File(face, font_metrics);
|
|
138 if (error) WARNING("FT_Attach_File failed.");
|
|
139 }
|
|
140 */
|
|
141
|
|
142
|
|
143 #if 0
|
|
144 /************************************************************/
|
|
145 eprintf("Font encodings:\n");
|
|
146 for (i = 0; i<face->num_charmaps; ++i)
|
|
147 eprintf("'%.4s'\n", (char*)&face->charmaps[i]->encoding);
|
|
148
|
|
149 //error = FT_Select_Charmap(face, ft_encoding_unicode);
|
|
150 //error = FT_Select_Charmap(face, ft_encoding_adobe_standard);
|
|
151 //error = FT_Select_Charmap(face, ft_encoding_adobe_custom);
|
|
152 //error = FT_Set_Charmap(face, face->charmaps[1]);
|
|
153 //if (error) WARNING("FT_Select_Charmap failed.");
|
|
154 #endif
|
|
155
|
|
156
|
|
157 #if 0
|
|
158 /************************************************************/
|
|
159 if (FT_HAS_GLYPH_NAMES(face)) {
|
|
160 int const max_gname = 128;
|
|
161 char gname[max_gname];
|
|
162 for (i = 0; i<face->num_glyphs; ++i) {
|
|
163 FT_Get_Glyph_Name(face, i, gname, max_gname);
|
|
164 eprintf("%02x `%s'\n", i, gname);
|
|
165 }
|
|
166
|
|
167 }
|
|
168 #endif
|
|
169
|
|
170
|
|
171 if (face->charmap==NULL || face->charmap->encoding!=ft_encoding_unicode) {
|
|
172 WARNING("Unicode charmap not available for this font. Very bad!");
|
|
173 uni_charmap = 0;
|
|
174 error = FT_Set_Charmap(face, face->charmaps[0]);
|
|
175 if (error) WARNING("No charmaps! Strange.");
|
|
176 }
|
|
177
|
|
178
|
|
179
|
|
180 /* set size */
|
|
181 if (FT_IS_SCALABLE(face)) {
|
|
182 error = FT_Set_Char_Size(face, floatTof266(ppem), 0, 0, 0);
|
|
183 if (error) WARNING("FT_Set_Char_Size failed.");
|
|
184 } else {
|
|
185 int j = 0;
|
|
186 int jppem = face->available_sizes[0].height;
|
|
187 /* find closest size */
|
|
188 for (i = 0; i<face->num_fixed_sizes; ++i) {
|
|
189 if (fabs(face->available_sizes[i].height - ppem) < abs(face->available_sizes[i].height - jppem)) {
|
|
190 j = i;
|
|
191 jppem = face->available_sizes[i].height;
|
|
192 }
|
|
193 }
|
|
194 WARNING("Selected font is not scalable. Using ppem=%i.", face->available_sizes[j].height);
|
|
195 error = FT_Set_Pixel_Sizes(face, face->available_sizes[j].width, face->available_sizes[j].height);
|
|
196 if (error) WARNING("FT_Set_Pixel_Sizes failed.");
|
|
197 }
|
|
198
|
|
199
|
|
200 if (FT_IS_FIXED_WIDTH(face))
|
|
201 WARNING("Selected font is fixed-width.");
|
|
202
|
|
203
|
|
204 /* compute space advance */
|
|
205 error = FT_Load_Char(face, ' ', load_flags);
|
|
206 if (error) WARNING("spacewidth set to default.");
|
|
207 else space_advance = f266ToInt(face->glyph->advance.x);
|
|
208
|
|
209
|
|
210 /* create font.desc */
|
|
211 if(!font_id){ // first font
|
|
212 desc->spacewidth=2*padding + space_advance;
|
|
213 desc->charspace=-2*padding;
|
|
214 desc->height=f266ToInt(face->size->metrics.height);
|
|
215 // fprintf(f, "ascender %i\n", f266CeilToInt(face->size->metrics.ascender));
|
|
216 // fprintf(f, "descender %i\n", f266FloorToInt(face->size->metrics.descender));
|
|
217 }
|
|
218
|
|
219 // render glyphs, compute bitmap size and [characters] section
|
|
220 glyphs = (FT_Glyph*)malloc(charset_size*sizeof(FT_Glyph*));
|
|
221 for (i= 0; i<charset_size; ++i) {
|
|
222 FT_GlyphSlot slot;
|
|
223 FT_ULong character, code;
|
|
224 FT_UInt glyph_index;
|
|
225 FT_BBox bbox;
|
|
226
|
|
227 character = charset[i];
|
|
228 code = charcodes[i];
|
|
229
|
|
230 // get glyph index
|
|
231 if (character==0)
|
|
232 glyph_index = 0;
|
|
233 else {
|
|
234 glyph_index = FT_Get_Char_Index(face, uni_charmap ? character:code);
|
|
235 if (glyph_index==0) {
|
|
236 WARNING("Glyph for char 0x%02x|U+%04X|%c not found.", code, character,
|
|
237 code<' '||code>255 ? '.':code);
|
|
238 continue;
|
|
239 }
|
|
240 }
|
|
241
|
|
242 // load glyph
|
|
243 error = FT_Load_Glyph(face, glyph_index, load_flags);
|
|
244 if (error) {
|
|
245 WARNING("FT_Load_Glyph 0x%02x (char 0x%02x|U+%04X) failed.", glyph_index, code, character);
|
|
246 continue;
|
|
247 }
|
|
248 slot = face->glyph;
|
|
249
|
|
250 // render glyph
|
|
251 if (slot->format != ft_glyph_format_bitmap) {
|
|
252 error = FT_Render_Glyph(slot, ft_render_mode_normal);
|
|
253 if (error) {
|
|
254 WARNING("FT_Render_Glyph 0x%04x (char 0x%02x|U+%04X) failed.", glyph_index, code, character);
|
|
255 continue;
|
|
256 }
|
|
257 }
|
|
258
|
|
259 // extract glyph image
|
|
260 error = FT_Get_Glyph(slot, (FT_Glyph*)&glyph);
|
|
261 if (error) {
|
|
262 WARNING("FT_Get_Glyph 0x%04x (char 0x%02x|U+%04X) failed.", glyph_index, code, character);
|
|
263 continue;
|
|
264 }
|
|
265 glyphs[glyphs_count++] = (FT_Glyph)glyph;
|
|
266
|
|
267 #ifdef NEW_DESC
|
|
268 // max height
|
|
269 if (glyph->bitmap.rows > height) height = glyph->bitmap.rows;
|
|
270
|
|
271 // advance pen
|
|
272 pen_xa = pen_x + glyph->bitmap.width + 2*padding;
|
|
273
|
|
274 // font.desc
|
|
275 fprintf(f, "0x%04x %i %i %i %i %i %i;\tU+%04X|%c\n", unicode_desc ? character:code,
|
|
276 pen_x, // bitmap start
|
|
277 glyph->bitmap.width + 2*padding, // bitmap width
|
|
278 glyph->bitmap.rows + 2*padding, // bitmap height
|
|
279 glyph->left - padding, // left bearing
|
|
280 glyph->top + padding, // top bearing
|
|
281 f266ToInt(slot->advance.x), // advance
|
|
282 character, code<' '||code>255 ? '.':code);
|
|
283 #else
|
|
284 // max height
|
|
285 if (glyph->top > ymax) {
|
|
286 ymax = glyph->top;
|
|
287 //eprintf("%3i: ymax %i (%c)\n", code, ymax, code);
|
|
288 }
|
|
289 if (glyph->top - glyph->bitmap.rows < ymin) {
|
|
290 ymin = glyph->top - glyph->bitmap.rows;
|
|
291 //eprintf("%3i: ymin %i (%c)\n", code, ymin, code);
|
|
292 }
|
|
293
|
|
294 /* advance pen */
|
|
295 pen_xa = pen_x + f266ToInt(slot->advance.x) + 2*padding;
|
|
296
|
|
297 /* font.desc */
|
|
298 // fprintf(f, "0x%04x %i %i;\tU+%04X|%c\n", unicode_desc ? character:code,
|
|
299 // pen_x, // bitmap start
|
|
300 // pen_xa-1, // bitmap end
|
|
301 // character, code<' '||code>255 ? '.':code);
|
|
302 desc->start[unicode_desc ? character:code]=pen_x;
|
|
303 desc->width[unicode_desc ? character:code]=pen_xa-1-pen_x;
|
|
304 desc->font[unicode_desc ? character:code]=font_id;
|
|
305
|
|
306 #endif
|
|
307 pen_x = ALIGN(pen_xa);
|
|
308 }
|
|
309
|
|
310
|
|
311 width = pen_x;
|
|
312 pen_x = 0;
|
|
313 #ifdef NEW_DESC
|
|
314 if (height<=0) ERROR("Something went wrong. Use the source!");
|
|
315 height += 2*padding;
|
|
316 #else
|
|
317 if (ymax<=ymin) ERROR("Something went wrong. Use the source!");
|
|
318 height = ymax - ymin + 2*padding;
|
|
319 baseline = ymax + padding;
|
|
320 #endif
|
|
321
|
|
322 // end of font.desc
|
|
323 if (DEBUG) eprintf("bitmap size: %ix%i\n", width, height);
|
|
324 // fprintf(f, "# bitmap size: %ix%i\n", width, height);
|
|
325 // fclose(f);
|
|
326
|
|
327 bbuffer = (unsigned char*)malloc(width*height);
|
|
328 if (bbuffer==NULL) ERROR("malloc failed.");
|
|
329 memset(bbuffer, 0, width*height);
|
|
330
|
|
331
|
|
332 /* paste glyphs */
|
|
333 for (i= 0; i<glyphs_count; ++i) {
|
|
334 glyph = (FT_BitmapGlyph)glyphs[i];
|
|
335 #ifdef NEW_DESC
|
|
336 paste_bitmap(&glyph->bitmap,
|
|
337 pen_x + padding,
|
|
338 padding);
|
|
339
|
|
340 /* advance pen */
|
|
341 pen_x += glyph->bitmap.width + 2*padding;
|
|
342 #else
|
|
343 paste_bitmap(&glyph->bitmap,
|
|
344 pen_x + padding + glyph->left,
|
|
345 baseline - glyph->top);
|
|
346
|
|
347 /* advance pen */
|
|
348 pen_x += f1616ToInt(glyph->root.advance.x) + 2*padding;
|
|
349 #endif
|
|
350 pen_x = ALIGN(pen_x);
|
|
351
|
|
352 FT_Done_Glyph((FT_Glyph)glyph);
|
|
353 }
|
|
354 free(glyphs);
|
|
355
|
|
356
|
|
357 error = FT_Done_FreeType(library);
|
|
358 if (error) ERROR("FT_Done_FreeType failed.");
|
|
359 }
|
|
360
|
|
361
|
|
362 /* decode from 'encoding' to unicode */
|
|
363 static FT_ULong decode_char(char c) {
|
|
364 FT_ULong o;
|
|
365 char *inbuf = &c;
|
|
366 char *outbuf = (char*)&o;
|
|
367 int inbytesleft = 1;
|
|
368 int outbytesleft = sizeof(FT_ULong);
|
|
369
|
|
370 size_t count = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
|
|
371
|
|
372 /* convert unicode BigEndian -> MachineEndian */
|
|
373 o = be2me_32(o);
|
|
374
|
|
375 // if (count==-1) o = 0; // not OK, at least my iconv() returns E2BIG for all
|
|
376 if (outbytesleft!=0) o = 0;
|
|
377
|
|
378 /* we don't want control characters */
|
|
379 if (o>=0x7f && o<0xa0) o = 0;
|
|
380 return o;
|
|
381 }
|
|
382
|
|
383
|
|
384 static void prepare_charset() {
|
|
385 FILE *f;
|
|
386 FT_ULong i;
|
|
387
|
|
388 f = fopen(encoding, "r"); // try to read custom encoding
|
|
389 if (f==NULL) {
|
|
390 int count = 0;
|
|
391 // check if ucs-4 is available
|
|
392 cd = iconv_open(charmap, charmap);
|
|
393 if (cd==(iconv_t)-1) ERROR("iconv doesn't know %s encoding. Use the source!", charmap);
|
|
394 iconv_close(cd);
|
|
395
|
|
396 cd = iconv_open(charmap, encoding);
|
|
397 if (cd==(iconv_t)-1) ERROR("Unsupported encoding `%s', use iconv --list to list character sets known on your system.", encoding);
|
|
398
|
|
399 charset_size = 256 - first_char;
|
|
400 for (i = 0; i<charset_size; ++i) {
|
|
401 charcodes[count] = i+first_char;
|
|
402 charset[count] = decode_char(i+first_char);
|
|
403 //eprintf("%04X U%04X\n", charcodes[count], charset[count]);
|
|
404 if (charset[count]!=0) ++count;
|
|
405 }
|
|
406 charcodes[count] = charset[count] = 0; ++count;
|
|
407 charset_size = count;
|
|
408
|
|
409 iconv_close(cd);
|
|
410 } else {
|
|
411 unsigned int character, code;
|
|
412 int count;
|
|
413
|
|
414 eprintf("Reading custom encoding from file '%s'.\n", encoding);
|
|
415
|
|
416 while ((count = fscanf(f, "%x%*[ \t]%x", &character, &code)) != EOF) {
|
|
417 if (charset_size==max_charset_size) {
|
|
418 WARNING("There is no place for more than %i characters. Use the source!", max_charset_size);
|
|
419 break;
|
|
420 }
|
|
421 if (count==0) ERROR("Unable to parse custom encoding file.");
|
|
422 if (character<32) continue; // skip control characters
|
|
423 charset[charset_size] = character;
|
|
424 charcodes[charset_size] = count==2 ? code : character;
|
|
425 ++charset_size;
|
|
426 }
|
|
427 fclose(f);
|
|
428 // encoding = basename(encoding);
|
|
429 }
|
|
430 if (charset_size==0) ERROR("No characters to render!");
|
|
431 }
|
|
432
|
|
433
|
|
434 // general outline
|
|
435 static void outline(
|
|
436 unsigned char *s,
|
|
437 unsigned char *t,
|
|
438 int width,
|
|
439 int height,
|
|
440 int *m,
|
|
441 int r,
|
|
442 int mwidth) {
|
|
443
|
|
444 int x, y;
|
|
445 for (y = 0; y<height; ++y) {
|
|
446 for (x = 0; x<width; ++x, ++s, ++t) {
|
|
447 unsigned max = 0;
|
|
448 unsigned *mrow = m + r;
|
|
449 unsigned char *srow = s -r*width;
|
|
450 int x1=(x<r)?-x:-r;
|
|
451 int x2=(x+r>=width)?(width-x-1):r;
|
|
452 int my;
|
|
453
|
|
454 for (my = -r; my<=r; ++my, srow+= width, mrow+= mwidth) {
|
|
455 int mx;
|
|
456 if (y+my < 0) continue;
|
|
457 if (y+my >= height) break;
|
|
458
|
|
459 for (mx = x1; mx<=x2; ++mx) {
|
|
460 unsigned v = srow[mx] * mrow[mx];
|
|
461 if (v>max) max = v;
|
|
462 }
|
|
463 }
|
|
464 *t = (max + base/2) / base;
|
|
465 }
|
|
466 }
|
|
467 }
|
|
468
|
|
469
|
|
470 // 1 pixel outline
|
|
471 static void outline1(
|
|
472 unsigned char *s,
|
|
473 unsigned char *t,
|
|
474 int width,
|
|
475 int height) {
|
|
476
|
|
477 int x, y, mx, my;
|
|
478
|
|
479 for (x = 0; x<width; ++x, ++s, ++t) *t = *s;
|
|
480 for (y = 1; y<height-1; ++y) {
|
|
481 *t++ = *s++;
|
|
482 for (x = 1; x<width-1; ++x, ++s, ++t) {
|
|
483 unsigned v = (
|
|
484 s[-1-width]+
|
|
485 s[-1+width]+
|
|
486 s[+1-width]+
|
|
487 s[+1+width]
|
|
488 )/2 + (
|
|
489 s[-1]+
|
|
490 s[+1]+
|
|
491 s[-width]+
|
|
492 s[+width]+
|
|
493 s[0]
|
|
494 );
|
|
495 *t = v>maxcolor ? maxcolor : v;
|
|
496 }
|
|
497 *t++ = *s++;
|
|
498 }
|
|
499 for (x = 0; x<width; ++x, ++s, ++t) *t = *s;
|
|
500 }
|
|
501
|
|
502
|
|
503 // gaussian blur
|
|
504 static void blur(
|
|
505 unsigned char *buffer,
|
|
506 unsigned char *tmp,
|
|
507 int width,
|
|
508 int height,
|
|
509 int *m,
|
|
510 int r,
|
|
511 int mwidth,
|
|
512 unsigned volume) {
|
|
513
|
|
514 int x, y;
|
|
515
|
|
516 unsigned char *s = buffer - r;
|
|
517 unsigned char *t = tmp;
|
|
518 for (y = 0; y<height; ++y) {
|
|
519 for (x = 0; x<width; ++x, ++s, ++t) {
|
|
520 unsigned sum = 0;
|
|
521 int x1 = (x<r) ? r-x:0;
|
|
522 int x2 = (x+r>=width) ? (r+width-x):mwidth;
|
|
523 int mx;
|
|
524 for (mx = x1; mx<x2; ++mx)
|
|
525 sum+= s[mx] * m[mx];
|
|
526 *t = (sum + volume/2) / volume;
|
|
527 //*t = sum;
|
|
528 }
|
|
529 }
|
|
530 tmp -= r*width;
|
|
531 for (x = 0; x<width; ++x, ++tmp, ++buffer) {
|
|
532 s = tmp;
|
|
533 t = buffer;
|
|
534 for (y = 0; y<height; ++y, s+= width, t+= width) {
|
|
535 unsigned sum = 0;
|
|
536 int y1 = (y<r) ? r-y:0;
|
|
537 int y2 = (y+r>=height) ? (r+height-y):mwidth;
|
|
538 unsigned char *smy = s + y1*width;
|
|
539 int my;
|
|
540 for (my = y1; my<y2; ++my, smy+= width)
|
|
541 sum+= *smy * m[my];
|
|
542 *t = (sum + volume/2) / volume;
|
|
543 }
|
|
544 }
|
|
545 }
|
|
546
|
|
547
|
|
548 // Gaussian matrix
|
|
549 // Maybe for future use.
|
|
550 static unsigned gmatrix(unsigned *m, int r, int w, double const A) {
|
|
551 unsigned volume = 0; // volume under Gaussian area is exactly -pi*base/A
|
|
552 int mx, my;
|
|
553
|
|
554 for (my = 0; my<w; ++my) {
|
|
555 for (mx = 0; mx<w; ++mx) {
|
|
556 m[mx+my*w] = (unsigned)(exp(A * ((mx-r)*(mx-r)+(my-r)*(my-r))) * base + .5);
|
|
557 volume+= m[mx+my*w];
|
|
558 if (DEBUG) eprintf("%3i ", m[mx+my*w]);
|
|
559 }
|
|
560 if (DEBUG) eprintf("\n");
|
|
561 }
|
|
562 if (DEBUG) {
|
|
563 eprintf("A= %f\n", A);
|
|
564 eprintf("volume: %i; exact: %.0f; volume/exact: %.6f\n\n", volume, -M_PI*base/A, volume/(-M_PI*base/A));
|
|
565 }
|
|
566 return volume;
|
|
567 }
|
|
568
|
|
569
|
|
570 static void alpha() {
|
|
571 int const g_r = ceil(radius);
|
|
572 int const o_r = ceil(thickness);
|
|
573 int const g_w = 2*g_r+1; // matrix size
|
|
574 int const o_w = 2*o_r+1; // matrix size
|
|
575 double const A = log(1.0/base)/(radius*radius*2);
|
|
576
|
|
577 int mx, my, i;
|
|
578 unsigned volume = 0; // volume under Gaussian area is exactly -pi*base/A
|
|
579
|
|
580 unsigned *g = (unsigned*)malloc(g_w * sizeof(unsigned));
|
|
581 unsigned *om = (unsigned*)malloc(o_w*o_w * sizeof(unsigned));
|
|
582 if (g==NULL || om==NULL) ERROR("malloc failed.");
|
|
583
|
|
584 // gaussian curve
|
|
585 for (i = 0; i<g_w; ++i) {
|
|
586 g[i] = (unsigned)(exp(A * (i-g_r)*(i-g_r)) * base + .5);
|
|
587 volume+= g[i];
|
|
588 if (DEBUG) eprintf("%3i ", g[i]);
|
|
589 }
|
|
590 //volume *= volume;
|
|
591 if (DEBUG) eprintf("\n");
|
|
592
|
|
593 /* outline matrix */
|
|
594 for (my = 0; my<o_w; ++my) {
|
|
595 for (mx = 0; mx<o_w; ++mx) {
|
|
596 // antialiased circle would be perfect here, but this one is good enough
|
|
597 double d = thickness + 1 - sqrt((mx-o_r)*(mx-o_r)+(my-o_r)*(my-o_r));
|
|
598 om[mx+my*o_w] = d>=1 ? base : d<=0 ? 0 : (d*base + .5);
|
|
599 if (DEBUG) eprintf("%3i ", om[mx+my*o_w]);
|
|
600 }
|
|
601 if (DEBUG) eprintf("\n");
|
|
602 }
|
|
603 if (DEBUG) eprintf("\n");
|
|
604
|
|
605
|
|
606 if(thickness==1.0)
|
|
607 outline1(bbuffer, abuffer, width, height); // FAST solid 1 pixel outline
|
|
608 else
|
|
609 outline(bbuffer, abuffer, width, height, om, o_r, o_w); // solid outline
|
|
610
|
|
611 //outline(bbuffer, abuffer, width, height, gm, g_r, g_w); // Gaussian outline
|
|
612
|
|
613 blur(abuffer, bbuffer, width, height, g, g_r, g_w, volume);
|
|
614
|
|
615 free(g);
|
|
616 free(om);
|
|
617 }
|
|
618
|
|
619 font_desc_t* read_font_desc(char* fname,float factor,int verbose){
|
|
620 int i=font_id;
|
|
621 int j;
|
|
622
|
|
623 if(!font_id){
|
|
624 desc=malloc(sizeof(font_desc_t));if(!desc) return NULL;
|
|
625 memset(desc,0,sizeof(font_desc_t));
|
|
626 memset(desc->font,255,sizeof(short)*65536);
|
|
627 }
|
|
628
|
|
629 padding = ceil(radius) + ceil(thickness);
|
|
630
|
|
631 font_path=fname;
|
|
632
|
|
633 prepare_charset();
|
|
634
|
|
635 render();
|
|
636 desc->pic_b[font_id]=malloc(sizeof(raw_file));
|
|
637 desc->pic_b[font_id]->bmp=malloc(width*height);
|
|
638 memcpy(desc->pic_b[font_id]->bmp,bbuffer,width*height);
|
|
639 desc->pic_b[font_id]->pal=NULL;
|
|
640 desc->pic_b[font_id]->w=width;
|
|
641 desc->pic_b[font_id]->h=height;
|
|
642 desc->pic_b[font_id]->c=256;
|
|
643 // write_bitmap(bbuffer, 'b');
|
|
644
|
|
645 abuffer = (unsigned char*)malloc(width*height);
|
|
646 if (abuffer==NULL) ERROR("malloc failed.");
|
|
647 alpha();
|
|
648 // write_bitmap(abuffer, 'a');
|
|
649 desc->pic_a[font_id]=malloc(sizeof(raw_file));
|
|
650 desc->pic_a[font_id]->bmp=abuffer;
|
|
651 // desc->pic_a[font_id]->bmp=malloc(width*height);
|
|
652 // memcpy(desc->pic_a[font_id]->bmp,abuffer,width*height);
|
|
653 desc->pic_a[font_id]->pal=NULL;
|
|
654 desc->pic_a[font_id]->w=width;
|
|
655 desc->pic_a[font_id]->h=height;
|
|
656 desc->pic_a[font_id]->c=256;
|
|
657
|
|
658 free(bbuffer);
|
|
659 // free(abuffer);
|
|
660
|
|
661 //if(factor!=1.0f)
|
|
662 {
|
|
663 // re-sample alpha
|
|
664 int f=factor*256.0f;
|
|
665 int size=desc->pic_a[i]->w*desc->pic_a[i]->h;
|
|
666 int j;
|
|
667 if(verbose) printf("font: resampling alpha by factor %5.3f (%d) ",factor,f);fflush(stdout);
|
|
668 for(j=0;j<size;j++){
|
|
669 int x=desc->pic_a[i]->bmp[j]; // alpha
|
|
670 int y=desc->pic_b[i]->bmp[j]; // bitmap
|
|
671
|
|
672 #ifdef FAST_OSD
|
|
673 x=(x<(255-f))?0:1;
|
|
674 #else
|
|
675
|
|
676 x=255-((x*f)>>8); // scale
|
|
677 //if(x<0) x=0; else if(x>255) x=255;
|
|
678 //x^=255; // invert
|
|
679
|
|
680 if(x+y>255) x=255-y; // to avoid overflows
|
|
681
|
|
682 //x=0;
|
|
683 //x=((x*f*(255-y))>>16);
|
|
684 //x=((x*f*(255-y))>>16)+y;
|
|
685 //x=(x*f)>>8;if(x<y) x=y;
|
|
686
|
|
687 if(x<1) x=1; else
|
|
688 if(x>=252) x=0;
|
|
689 #endif
|
|
690
|
|
691 desc->pic_a[i]->bmp[j]=x;
|
|
692 // desc->pic_b[i]->bmp[j]=0; // hack
|
|
693 }
|
|
694 if(verbose) printf("DONE!\n");
|
|
695 }
|
|
696 if(!desc->height) desc->height=desc->pic_a[i]->h;
|
|
697
|
|
698 j='_';if(desc->font[j]<0) j='?';
|
|
699 for(i=0;i<512;i++)
|
|
700 if(desc->font[i]<0){
|
|
701 desc->start[i]=desc->start[j];
|
|
702 desc->width[i]=desc->width[j];
|
|
703 desc->font[i]=desc->font[j];
|
|
704 }
|
|
705 desc->font[' ']=-1;
|
|
706 desc->width[' ']=desc->spacewidth;
|
|
707
|
|
708 printf("Font %s loaded successfully!\n",fname);
|
|
709
|
|
710 ++font_id;
|
|
711
|
|
712 // fflush(stderr);
|
|
713 return desc;
|
|
714 }
|