Mercurial > mplayer.hg
comparison libvo/font_load_ft.c @ 7122:0dc9cb756b68
freetype 2.0/2.1+ support - disabled by default until bugs fixed
patch by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
author | arpi |
---|---|
date | Wed, 28 Aug 2002 20:52:02 +0000 |
parents | |
children | dd1e21e775b6 |
comparison
equal
deleted
inserted
replaced
7121:6abc330b5b32 | 7122:0dc9cb756b68 |
---|---|
1 /* | |
2 * Renders antialiased fonts for mplayer using freetype library. | |
3 * Should work with TrueType, Type1 and any other font supported by libfreetype. | |
4 * | |
5 * Artur Zaprzala <zybi@fanthom.irc.pl> | |
6 * | |
7 * ported inside mplayer by Jindrich Makovicka | |
8 * <makovick@kmlinux.fjfi.cvut.cz> | |
9 * | |
10 */ | |
11 | |
12 #include "config.h" | |
13 | |
14 #ifdef HAVE_FREETYPE | |
15 | |
16 #include <stdio.h> | |
17 #include <stdlib.h> | |
18 #include <iconv.h> | |
19 #include <math.h> | |
20 #include <string.h> | |
21 #include <libgen.h> | |
22 | |
23 #include <freetype/freetype.h> | |
24 #include <freetype/ftglyph.h> | |
25 | |
26 #include "../bswap.h" | |
27 #include "font_load.h" | |
28 #include "mp_msg.h" | |
29 #include "../mplayer.h" | |
30 #include "osd_font.h" | |
31 | |
32 #if (FREETYPE_MAJOR > 2) || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 1) | |
33 #define HAVE_FREETYPE21 | |
34 #endif | |
35 | |
36 char *get_path(char *filename); | |
37 | |
38 char *subtitle_font_encoding = NULL; | |
39 float text_font_scale_factor = 5.0; | |
40 float osd_font_scale_factor = 6.0; | |
41 float subtitle_font_radius = 2.0; | |
42 float subtitle_font_thickness = 2.0; | |
43 // 0 = no autoscale | |
44 // 1 = video height | |
45 // 2 = video width | |
46 // 3 = diagonal | |
47 int subtitle_autoscale = 3; | |
48 | |
49 int vo_image_width = 0; | |
50 int vo_image_height = 0; | |
51 int force_load_font; | |
52 | |
53 //// constants | |
54 static int const colors = 256; | |
55 static int const maxcolor = 255; | |
56 static unsigned const base = 256; | |
57 static unsigned const first_char = 33; | |
58 #define MAX_CHARSET_SIZE 60000 | |
59 | |
60 static FT_Library library; | |
61 | |
62 #define OSD_CHARSET_SIZE 15 | |
63 | |
64 static FT_ULong osd_charset[OSD_CHARSET_SIZE] = | |
65 { | |
66 0xe001, 0xe002, 0xe003, 0xe004, 0xe005, 0xe006, 0xe007, 0xe008, | |
67 0xe009, 0xe00a, 0xe00b, 0xe010, 0xe011, 0xe012, 0xe013 | |
68 }; | |
69 | |
70 static FT_ULong osd_charcodes[OSD_CHARSET_SIZE] = | |
71 { | |
72 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08, | |
73 0x09,0x0a,0x0b,0x10,0x11,0x12,0x13 | |
74 }; | |
75 | |
76 #define f266ToInt(x) (((x)+32)>>6) // round fractional fixed point number to integer | |
77 // coordinates are in 26.6 pixels (i.e. 1/64th of pixels) | |
78 #define f266CeilToInt(x) (((x)+63)>>6) // ceiling | |
79 #define f266FloorToInt(x) ((x)>>6) // floor | |
80 #define f1616ToInt(x) (((x)+0x8000)>>16) // 16.16 | |
81 #define floatTof266(x) ((int)((x)*(1<<6)+0.5)) | |
82 | |
83 #define ALIGN(x) (((x)+7)&~7) // 8 byte align | |
84 | |
85 #define WARNING(msg, args...) mp_msg(MSGT_OSD, MSGL_WARN, msg "\n", ## args) | |
86 | |
87 #define DEBUG 0 | |
88 | |
89 //static double ttime; | |
90 | |
91 | |
92 static void paste_bitmap(unsigned char *bbuffer, FT_Bitmap *bitmap, int x, int y, int width, int height, int bwidth) { | |
93 int drow = x+y*width; | |
94 int srow = 0; | |
95 int sp, dp, w, h; | |
96 if (bitmap->pixel_mode==ft_pixel_mode_mono) | |
97 for (h = bitmap->rows; h>0 && height > 0; --h, height--, drow+=width, srow+=bitmap->pitch) | |
98 for (w = bwidth, sp=dp=0; w>0; --w, ++dp, ++sp) | |
99 bbuffer[drow+dp] = (bitmap->buffer[srow+sp/8] & (0x80>>(sp%8))) ? 255:0; | |
100 else | |
101 for (h = bitmap->rows; h>0 && height > 0; --h, height--, drow+=width, srow+=bitmap->pitch) | |
102 for (w = bwidth, sp=dp=0; w>0; --w, ++dp, ++sp) | |
103 bbuffer[drow+dp] = bitmap->buffer[srow+sp]; | |
104 } | |
105 | |
106 | |
107 static int check_font(font_desc_t *desc, float ppem, int padding, int pic_idx, | |
108 int charset_size, FT_ULong *charset, FT_ULong *charcodes, | |
109 int unicode) { | |
110 FT_Error error; | |
111 FT_Face face = desc->faces[pic_idx]; | |
112 int const load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING; | |
113 int ymin = INT_MAX, ymax = INT_MIN; | |
114 int baseline, space_advance = 20; | |
115 int width, height; | |
116 unsigned char *bbuffer; | |
117 int i, uni_charmap = 1; | |
118 | |
119 | |
120 if (face->charmap==NULL || face->charmap->encoding!=ft_encoding_unicode) { | |
121 WARNING("Unicode charmap not available for this font. Very bad!"); | |
122 uni_charmap = 0; | |
123 error = FT_Set_Charmap(face, face->charmaps[0]); | |
124 if (error) WARNING("No charmaps! Strange."); | |
125 } | |
126 | |
127 /* set size */ | |
128 if (FT_IS_SCALABLE(face)) { | |
129 error = FT_Set_Char_Size(face, 0, floatTof266(ppem), 0, 0); | |
130 if (error) WARNING("FT_Set_Char_Size failed."); | |
131 } else { | |
132 int j = 0; | |
133 int jppem = face->available_sizes[0].height; | |
134 /* find closest size */ | |
135 for (i = 0; i<face->num_fixed_sizes; ++i) { | |
136 if (fabs(face->available_sizes[i].height - ppem) < abs(face->available_sizes[i].height - jppem)) { | |
137 j = i; | |
138 jppem = face->available_sizes[i].height; | |
139 } | |
140 } | |
141 WARNING("Selected font is not scalable. Using ppem=%i.", face->available_sizes[j].height); | |
142 error = FT_Set_Pixel_Sizes(face, face->available_sizes[j].width, face->available_sizes[j].height); | |
143 if (error) WARNING("FT_Set_Pixel_Sizes failed."); | |
144 } | |
145 | |
146 if (FT_IS_FIXED_WIDTH(face)) | |
147 WARNING("Selected font is fixed-width."); | |
148 | |
149 /* compute space advance */ | |
150 error = FT_Load_Char(face, ' ', load_flags); | |
151 if (error) WARNING("spacewidth set to default."); | |
152 else space_advance = f266ToInt(face->glyph->advance.x); | |
153 | |
154 if (!desc->spacewidth) desc->spacewidth = 2*padding + space_advance; | |
155 if (!desc->charspace) desc->charspace = -2*padding; | |
156 if (!desc->height) desc->height = f266ToInt(face->size->metrics.height); | |
157 | |
158 | |
159 for (i= 0; i<charset_size; ++i) { | |
160 FT_ULong character, code; | |
161 FT_UInt glyph_index; | |
162 | |
163 character = charset[i]; | |
164 code = charcodes[i]; | |
165 desc->font[unicode?character:code] = pic_idx; | |
166 // get glyph index | |
167 if (character==0) | |
168 glyph_index = 0; | |
169 else { | |
170 glyph_index = FT_Get_Char_Index(face, uni_charmap ? character:code); | |
171 if (glyph_index==0) { | |
172 WARNING("Glyph for char 0x%02x|U+%04X|%c not found.", code, character, | |
173 code<' '||code>255 ? '.':code); | |
174 desc->font[unicode?character:code] = -1; | |
175 continue; | |
176 } | |
177 } | |
178 desc->glyph_index[unicode?character:code] = glyph_index; | |
179 } | |
180 // fprintf(stderr, "font height: %lf\n", (double)(face->bbox.yMax-face->bbox.yMin)/(double)face->units_per_EM*ppem); | |
181 // fprintf(stderr, "font width: %lf\n", (double)(face->bbox.xMax-face->bbox.xMin)/(double)face->units_per_EM*ppem); | |
182 | |
183 ymax = (double)(face->bbox.yMax)/(double)face->units_per_EM*ppem+1; | |
184 ymin = (double)(face->bbox.yMin)/(double)face->units_per_EM*ppem-1; | |
185 | |
186 width = ppem*(face->bbox.xMax-face->bbox.xMin)/face->units_per_EM+3+2*padding; | |
187 if (desc->max_width < width) desc->max_width = width; | |
188 width = ALIGN(width); | |
189 desc->pic_b[pic_idx]->charwidth = width; | |
190 | |
191 if (ymax<=ymin) { | |
192 mp_msg(MSGT_OSD, MSGL_ERR, "Something went wrong. Use the source!\n"); | |
193 return -1; | |
194 } | |
195 | |
196 height = ymax - ymin + 2*padding; | |
197 if (desc->max_height < height) desc->max_height = height; | |
198 desc->pic_b[pic_idx]->charheight = height; | |
199 | |
200 // fprintf(stderr, "font height2: %d\n", height); | |
201 desc->pic_b[pic_idx]->baseline = ymax + padding; | |
202 desc->pic_b[pic_idx]->padding = padding; | |
203 desc->pic_b[pic_idx]->current_alloc = 0; | |
204 desc->pic_b[pic_idx]->current_count = 0; | |
205 | |
206 bbuffer = NULL; | |
207 | |
208 desc->pic_b[pic_idx]->w = width; | |
209 desc->pic_b[pic_idx]->h = height; | |
210 desc->pic_b[pic_idx]->c = colors; | |
211 desc->pic_b[pic_idx]->bmp = bbuffer; | |
212 desc->pic_b[pic_idx]->pen = 0; | |
213 return 0; | |
214 } | |
215 | |
216 // general outline | |
217 void outline( | |
218 unsigned char *s, | |
219 unsigned char *t, | |
220 int width, | |
221 int height, | |
222 int stride, | |
223 unsigned char *m, | |
224 int r, | |
225 int mwidth, | |
226 int msize) { | |
227 | |
228 int x, y; | |
229 | |
230 for (y = 0; y<height; y++) { | |
231 for (x = 0; x<width; x++) { | |
232 const int src= s[x]; | |
233 if(src==0) continue; | |
234 { | |
235 const int x1=(x<r) ? r-x : 0; | |
236 const int y1=(y<r) ? r-y : 0; | |
237 const int x2=(x+r>=width ) ? r+width -x : 2*r+1; | |
238 const int y2=(y+r>=height) ? r+height-y : 2*r+1; | |
239 register unsigned char *dstp= t + (y1+y-r)* stride + x-r; | |
240 //register int *mp = m + y1 *mwidth; | |
241 register unsigned char *mp= m + msize*src + y1*mwidth; | |
242 int my; | |
243 | |
244 for(my= y1; my<y2; my++){ | |
245 register int mx; | |
246 for(mx= x1; mx<x2; mx++){ | |
247 if(dstp[mx] < mp[mx]) dstp[mx]= mp[mx]; | |
248 } | |
249 dstp+=stride; | |
250 mp+=mwidth; | |
251 } | |
252 } | |
253 } | |
254 s+= stride; | |
255 } | |
256 } | |
257 | |
258 | |
259 // 1 pixel outline | |
260 void outline1( | |
261 unsigned char *s, | |
262 unsigned char *t, | |
263 int width, | |
264 int height, | |
265 int stride) { | |
266 | |
267 int x, y, mx, my; | |
268 int skip = stride-width; | |
269 | |
270 for (x = 0; x<width; ++x, ++s, ++t) *t = *s; | |
271 s += skip; | |
272 t += skip; | |
273 for (y = 1; y<height-1; ++y) { | |
274 *t++ = *s++; | |
275 for (x = 1; x<width-1; ++x, ++s, ++t) { | |
276 unsigned v = ( | |
277 s[-1-stride]+ | |
278 s[-1+stride]+ | |
279 s[+1-stride]+ | |
280 s[+1+stride] | |
281 )/2 + ( | |
282 s[-1]+ | |
283 s[+1]+ | |
284 s[-stride]+ | |
285 s[+stride]+ | |
286 s[0] | |
287 ); | |
288 *t = v>maxcolor ? maxcolor : v; | |
289 } | |
290 *t++ = *s++; | |
291 s += skip; | |
292 t += skip; | |
293 } | |
294 for (x = 0; x<width; ++x, ++s, ++t) *t = *s; | |
295 } | |
296 | |
297 | |
298 // gaussian blur | |
299 void blur( | |
300 unsigned char *buffer, | |
301 unsigned short *tmp2, | |
302 int width, | |
303 int height, | |
304 int stride, | |
305 int *m, | |
306 int *m2, | |
307 int r, | |
308 int mwidth, | |
309 unsigned volume) { | |
310 | |
311 int x, y; | |
312 | |
313 unsigned char *s = buffer; | |
314 unsigned short *t = tmp2+1; | |
315 for(y=0; y<height; y++){ | |
316 memset(t-1, 0, (width+1)*sizeof(short)); | |
317 | |
318 for(x=0; x<r; x++){ | |
319 const int src= s[x]; | |
320 if(src){ | |
321 register unsigned short *dstp= t + x-r; | |
322 int mx; | |
323 unsigned *m3= m2 + src*mwidth; | |
324 for(mx=r-x; mx<mwidth; mx++){ | |
325 dstp[mx]+= m3[mx]; | |
326 } | |
327 } | |
328 } | |
329 | |
330 for(; x<width-r; x++){ | |
331 const int src= s[x]; | |
332 if(src){ | |
333 register unsigned short *dstp= t + x-r; | |
334 int mx; | |
335 unsigned *m3= m2 + src*mwidth; | |
336 for(mx=0; mx<mwidth; mx++){ | |
337 dstp[mx]+= m3[mx]; | |
338 } | |
339 } | |
340 } | |
341 | |
342 for(; x<width; x++){ | |
343 const int src= s[x]; | |
344 if(src){ | |
345 register unsigned short *dstp= t + x-r; | |
346 int mx; | |
347 const int x2= r+width -x; | |
348 const int off= src*mwidth; | |
349 unsigned *m3= m2 + src*mwidth; | |
350 for(mx=0; mx<x2; mx++){ | |
351 dstp[mx]+= m3[mx]; | |
352 } | |
353 } | |
354 } | |
355 | |
356 s+= stride; | |
357 t+= width + 1; | |
358 } | |
359 | |
360 t = tmp2; | |
361 for(x=0; x<width; x++){ | |
362 for(y=0; y<r; y++){ | |
363 unsigned short *srcp= t + y*(width+1) + 1; | |
364 int src= *srcp; | |
365 if(src){ | |
366 register unsigned short *dstp= srcp - 1 + width+1; | |
367 const int src2= (src + 128)>>8; | |
368 unsigned *m3= m2 + src2*mwidth; | |
369 | |
370 int mx; | |
371 *srcp= 128; | |
372 for(mx=r-1; mx<mwidth; mx++){ | |
373 *dstp += m3[mx]; | |
374 dstp+= width+1; | |
375 } | |
376 } | |
377 } | |
378 for(; y<height-r; y++){ | |
379 unsigned short *srcp= t + y*(width+1) + 1; | |
380 int src= *srcp; | |
381 if(src){ | |
382 register unsigned short *dstp= srcp - 1 - r*(width+1); | |
383 const int src2= (src + 128)>>8; | |
384 unsigned *m3= m2 + src2*mwidth; | |
385 | |
386 int mx; | |
387 *srcp= 128; | |
388 for(mx=0; mx<mwidth; mx++){ | |
389 *dstp += m3[mx]; | |
390 dstp+= width+1; | |
391 } | |
392 } | |
393 } | |
394 for(; y<height; y++){ | |
395 unsigned short *srcp= t + y*(width+1) + 1; | |
396 int src= *srcp; | |
397 if(src){ | |
398 const int y2=r+height-y; | |
399 register unsigned short *dstp= srcp - 1 - r*(width+1); | |
400 const int src2= (src + 128)>>8; | |
401 unsigned *m3= m2 + src2*mwidth; | |
402 | |
403 int mx; | |
404 *srcp= 128; | |
405 for(mx=0; mx<y2; mx++){ | |
406 *dstp += m3[mx]; | |
407 dstp+= width+1; | |
408 } | |
409 } | |
410 } | |
411 t++; | |
412 } | |
413 | |
414 t = tmp2; | |
415 s = buffer; | |
416 for(y=0; y<height; y++){ | |
417 for(x=0; x<width; x++){ | |
418 s[x]= t[x]>>8; | |
419 } | |
420 s+= stride; | |
421 t+= width + 1; | |
422 } | |
423 } | |
424 | |
425 // Gaussian matrix | |
426 static unsigned gmatrix(unsigned char *m, int r, int w, double const A) { | |
427 unsigned volume = 0; // volume under Gaussian area is exactly -pi*base/A | |
428 int mx, my; | |
429 | |
430 for (my = 0; my<w; ++my) { | |
431 for (mx = 0; mx<w; ++mx) { | |
432 m[mx+my*w] = (exp(A * ((mx-r)*(mx-r)+(my-r)*(my-r))) * base + .5); | |
433 volume+= m[mx+my*w]; | |
434 } | |
435 } | |
436 mp_msg(MSGT_OSD, MSGL_DBG2, "A= %f\n", A); | |
437 mp_msg(MSGT_OSD, MSGL_DBG2, "volume: %i; exact: %.0f; volume/exact: %.6f\n\n", volume, -M_PI*base/A, volume/(-M_PI*base/A)); | |
438 return volume; | |
439 } | |
440 | |
441 static void resample_alpha(unsigned char *abuf, unsigned char *bbuf, int width, int height, int stride, float factor) | |
442 { | |
443 int f=factor*256.0f; | |
444 int i,j; | |
445 for (i = 0; i < height; i++) { | |
446 unsigned char *a = abuf+i*stride; | |
447 unsigned char *b = bbuf+i*stride; | |
448 for(j=0;j<width;j++,a++,b++){ | |
449 int x=*a; // alpha | |
450 int y=*b; // bitmap | |
451 x=255-((x*f)>>8); // scale | |
452 if (x+y>255) x=255-y; // to avoid overflows | |
453 if (x<1) x=1; else if (x>=252) x=0; | |
454 *a=x; | |
455 } | |
456 } | |
457 } | |
458 | |
459 #define ALLOC_INCR 32 | |
460 void render_one_glyph(font_desc_t *desc, int c) | |
461 { | |
462 FT_GlyphSlot slot; | |
463 FT_ULong character, code; | |
464 FT_UInt glyph_index; | |
465 FT_BBox bbox; | |
466 FT_BitmapGlyph glyph; | |
467 int width, height, stride, maxw, off; | |
468 unsigned char *abuffer, *bbuffer; | |
469 | |
470 int const load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING; | |
471 int pen_xa; | |
472 int font = desc->font[c]; | |
473 int error; | |
474 | |
475 // fprintf(stderr, "render_one_glyph %d\n", c); | |
476 | |
477 if (desc->width[c] != -1) return; | |
478 if (desc->font[c] == -1) return; | |
479 | |
480 glyph_index = desc->glyph_index[c]; | |
481 | |
482 // load glyph | |
483 error = FT_Load_Glyph(desc->faces[font], glyph_index, load_flags); | |
484 if (error) { | |
485 WARNING("FT_Load_Glyph 0x%02x (char 0x%02x|U+%04X) failed.", glyph_index, code, character); | |
486 desc->font[c] = -1; | |
487 return; | |
488 } | |
489 slot = desc->faces[font]->glyph; | |
490 | |
491 // render glyph | |
492 if (slot->format != ft_glyph_format_bitmap) { | |
493 error = FT_Render_Glyph(slot, ft_render_mode_normal); | |
494 if (error) { | |
495 WARNING("FT_Render_Glyph 0x%04x (char 0x%02x|U+%04X) failed.", glyph_index, code, character); | |
496 desc->font[c] = -1; | |
497 return; | |
498 } | |
499 } | |
500 | |
501 // extract glyph image | |
502 error = FT_Get_Glyph(slot, (FT_Glyph*)&glyph); | |
503 if (error) { | |
504 WARNING("FT_Get_Glyph 0x%04x (char 0x%02x|U+%04X) failed.", glyph_index, code, character); | |
505 desc->font[c] = -1; | |
506 return; | |
507 } | |
508 | |
509 // fprintf(stderr, "glyph generated\n"); | |
510 | |
511 maxw = desc->pic_b[font]->charwidth; | |
512 | |
513 if (glyph->bitmap.width > maxw) { | |
514 fprintf(stderr, "glyph too wide!\n"); | |
515 } | |
516 | |
517 // allocate new memory, if needed | |
518 if (desc->pic_b[font]->current_count >= desc->pic_b[font]->current_alloc) { | |
519 int newsize = desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight*(desc->pic_b[font]->current_alloc+ALLOC_INCR); | |
520 int increment = desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight*ALLOC_INCR; | |
521 | |
522 printf("\nincr=%d w=%d h=%d \n",increment,desc->pic_b[font]->charwidth,desc->pic_b[font]->charheight); | |
523 | |
524 desc->pic_b[font]->current_alloc += ALLOC_INCR; | |
525 | |
526 desc->pic_b[font]->bmp = realloc(desc->pic_b[font]->bmp, newsize); | |
527 desc->pic_a[font]->bmp = realloc(desc->pic_a[font]->bmp, newsize); | |
528 | |
529 off = desc->pic_b[font]->current_count*desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight; | |
530 memset(desc->pic_b[font]->bmp+off, 0, increment); | |
531 memset(desc->pic_a[font]->bmp+off, 0, increment); | |
532 } | |
533 | |
534 abuffer = desc->pic_a[font]->bmp; | |
535 bbuffer = desc->pic_b[font]->bmp; | |
536 | |
537 off = desc->pic_b[font]->current_count*desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight; | |
538 | |
539 paste_bitmap(bbuffer+off, | |
540 &glyph->bitmap, | |
541 desc->pic_b[font]->padding + glyph->left, | |
542 desc->pic_b[font]->baseline - glyph->top, | |
543 desc->pic_b[font]->charwidth, desc->pic_b[font]->charheight, | |
544 glyph->bitmap.width <= maxw ? glyph->bitmap.width : maxw); | |
545 | |
546 // fprintf(stderr, "glyph pasted\n"); | |
547 FT_Done_Glyph((FT_Glyph)glyph); | |
548 | |
549 /* advance pen */ | |
550 pen_xa = f266ToInt(slot->advance.x) + 2*desc->pic_b[font]->padding; | |
551 if (pen_xa > maxw) pen_xa = maxw; | |
552 | |
553 desc->start[c] = off; | |
554 width = desc->width[c] = pen_xa; | |
555 height = desc->pic_b[font]->charheight; | |
556 stride = desc->pic_b[font]->w; | |
557 | |
558 if (desc->tables.o_r <= 1) { | |
559 outline1(bbuffer+off, abuffer+off, width, height, stride); | |
560 } else { | |
561 outline(bbuffer+off, abuffer+off, width, height, stride, | |
562 desc->tables.omt, desc->tables.o_r, desc->tables.o_w, | |
563 desc->tables.o_size); | |
564 } | |
565 // fprintf(stderr, "fg: outline t = %lf\n", GetTimer()-t); | |
566 | |
567 if (desc->tables.g_r) { | |
568 blur(abuffer+off, desc->tables.tmp, width, height, stride, | |
569 desc->tables.gt, desc->tables.gt2, desc->tables.g_r, | |
570 desc->tables.g_w, desc->tables.volume); | |
571 // fprintf(stderr, "fg: blur t = %lf\n", GetTimer()-t); | |
572 } | |
573 | |
574 resample_alpha(abuffer+off, bbuffer+off, width, height, stride, font_factor); | |
575 | |
576 desc->pic_b[font]->current_count++; | |
577 } | |
578 | |
579 | |
580 static int prepare_font(font_desc_t *desc, FT_Face face, float ppem, int pic_idx, | |
581 int charset_size, FT_ULong *charset, FT_ULong *charcodes, int unicode, | |
582 double thickness, double radius) | |
583 { | |
584 int i, err; | |
585 int padding = ceil(radius) + ceil(thickness); | |
586 | |
587 desc->faces[pic_idx] = face; | |
588 | |
589 desc->pic_a[pic_idx] = (raw_file*)malloc(sizeof(raw_file)); | |
590 if (!desc->pic_a[pic_idx]) return -1; | |
591 desc->pic_b[pic_idx] = (raw_file*)malloc(sizeof(raw_file)); | |
592 if (!desc->pic_b[pic_idx]) return -1; | |
593 | |
594 desc->pic_a[pic_idx]->bmp = NULL; | |
595 desc->pic_a[pic_idx]->pal = NULL; | |
596 desc->pic_b[pic_idx]->bmp = NULL; | |
597 desc->pic_b[pic_idx]->pal = NULL; | |
598 | |
599 desc->pic_a[pic_idx]->pal = (unsigned char*)malloc(sizeof(unsigned char)*256*3); | |
600 if (!desc->pic_a[pic_idx]->pal) return -1; | |
601 for (i = 0; i<768; ++i) desc->pic_a[pic_idx]->pal[i] = i/3; | |
602 | |
603 desc->pic_b[pic_idx]->pal = (unsigned char*)malloc(sizeof(unsigned char)*256*3); | |
604 if (!desc->pic_b[pic_idx]->pal) return -1; | |
605 for (i = 0; i<768; ++i) desc->pic_b[pic_idx]->pal[i] = i/3; | |
606 | |
607 // ttime = GetTimer(); | |
608 err = check_font(desc, ppem, padding, pic_idx, charset_size, charset, charcodes, unicode); | |
609 // ttime=GetTimer()-ttime; | |
610 // printf("render: %7lf us\n",ttime); | |
611 if (err) return -1; | |
612 // fprintf(stderr, "fg: render t = %lf\n", GetTimer()-t); | |
613 | |
614 desc->pic_a[pic_idx]->w = desc->pic_b[pic_idx]->w; | |
615 desc->pic_a[pic_idx]->h = desc->pic_b[pic_idx]->h; | |
616 desc->pic_a[pic_idx]->c = colors; | |
617 | |
618 desc->pic_a[pic_idx]->bmp = NULL; | |
619 | |
620 // fprintf(stderr, "fg: w = %d, h = %d\n", desc->pic_a[pic_idx]->w, desc->pic_a[pic_idx]->h); | |
621 return 0; | |
622 | |
623 } | |
624 | |
625 int generate_tables(font_desc_t *desc, double thickness, double radius) | |
626 { | |
627 int err; | |
628 | |
629 int width = desc->max_height; | |
630 int height = desc->max_width; | |
631 | |
632 double A = log(1.0/base)/(radius*radius*2); | |
633 int mx, my, i; | |
634 unsigned volume2 = 0; // volume under Gaussian area is exactly -pi*base/A | |
635 double volume_diff, volume_factor = 0; | |
636 unsigned char *omtp; | |
637 | |
638 desc->tables.g_r = ceil(radius); | |
639 desc->tables.o_r = ceil(thickness); | |
640 desc->tables.g_w = 2*desc->tables.g_r+1; | |
641 desc->tables.o_w = 2*desc->tables.o_r+1; | |
642 desc->tables.o_size = desc->tables.o_w * desc->tables.o_w; | |
643 | |
644 desc->tables.g = (unsigned*)malloc(desc->tables.g_w * sizeof(unsigned)); | |
645 desc->tables.gt = (unsigned*)malloc(256 * desc->tables.g_w * sizeof(unsigned)); | |
646 desc->tables.gt2 = (unsigned*)malloc(256 * desc->tables.g_w * sizeof(unsigned)); | |
647 desc->tables.om = (unsigned*)malloc(desc->tables.o_w*desc->tables.o_w * sizeof(unsigned)); | |
648 desc->tables.omt = malloc(desc->tables.o_size*256); | |
649 | |
650 omtp = desc->tables.omt; | |
651 desc->tables.tmp = malloc((width+1)*height*sizeof(short)); | |
652 | |
653 if (desc->tables.g==NULL || desc->tables.gt==NULL || desc->tables.gt2==NULL | |
654 || desc->tables.om==NULL || desc->tables.omt==NULL) { | |
655 return -1; | |
656 }; | |
657 | |
658 // gaussian curve with volume = 256 | |
659 for (volume_diff=10000000; volume_diff>0.0000001; volume_diff*=0.5){ | |
660 volume_factor+= volume_diff; | |
661 desc->tables.volume=0; | |
662 for (i = 0; i<desc->tables.g_w; ++i) { | |
663 desc->tables.g[i] = (unsigned)(exp(A * (i-desc->tables.g_r)*(i-desc->tables.g_r)) * volume_factor + .5); | |
664 desc->tables.volume+= desc->tables.g[i]; | |
665 } | |
666 if(desc->tables.volume>256) volume_factor-= volume_diff; | |
667 } | |
668 desc->tables.volume=0; | |
669 for (i = 0; i<desc->tables.g_w; ++i) { | |
670 desc->tables.g[i] = (unsigned)(exp(A * (i-desc->tables.g_r)*(i-desc->tables.g_r)) * volume_factor + .5); | |
671 desc->tables.volume+= desc->tables.g[i]; | |
672 } | |
673 | |
674 // gauss table: | |
675 for(mx=0;mx<desc->tables.g_w;mx++){ | |
676 for(i=0;i<256;i++){ | |
677 desc->tables.gt[256*mx+i] = (i*desc->tables.g[mx]*65536+(desc->tables.volume/2))/desc->tables.volume; | |
678 desc->tables.gt2[mx+i*desc->tables.g_w] = i*desc->tables.g[mx]; | |
679 } | |
680 } | |
681 | |
682 /* outline matrix */ | |
683 for (my = 0; my<desc->tables.o_w; ++my) { | |
684 for (mx = 0; mx<desc->tables.o_w; ++mx) { | |
685 // antialiased circle would be perfect here, but this one is good enough | |
686 double d = thickness + 1 - sqrt((mx-desc->tables.o_r)*(mx-desc->tables.o_r)+(my-desc->tables.o_r)*(my-desc->tables.o_r)); | |
687 desc->tables.om[mx+my*desc->tables.o_w] = d>=1 ? base : d<=0 ? 0 : (d*base + .5); | |
688 } | |
689 } | |
690 | |
691 // outline table: | |
692 for(i=0;i<256;i++){ | |
693 for(mx=0;mx<desc->tables.o_size;mx++) *(omtp++) = (i*desc->tables.om[mx] + (base/2))/base; | |
694 } | |
695 | |
696 return 0; | |
697 } | |
698 | |
699 | |
700 /* decode from 'encoding' to unicode */ | |
701 static FT_ULong decode_char(iconv_t *cd, char c) { | |
702 FT_ULong o; | |
703 char *inbuf = &c; | |
704 char *outbuf = (char*)&o; | |
705 int inbytesleft = 1; | |
706 int outbytesleft = sizeof(FT_ULong); | |
707 | |
708 size_t count = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); | |
709 | |
710 /* convert unicode BigEndian -> MachineEndian */ | |
711 o = be2me_32(o); | |
712 | |
713 // if (count==-1) o = 0; // not OK, at least my iconv() returns E2BIG for all | |
714 if (outbytesleft!=0) o = 0; | |
715 | |
716 /* we don't want control characters */ | |
717 if (o>=0x7f && o<0xa0) o = 0; | |
718 return o; | |
719 } | |
720 | |
721 static int prepare_charset(char *charmap, char *encoding, FT_ULong *charset, FT_ULong *charcodes) { | |
722 FT_ULong i; | |
723 int count = 0; | |
724 int charset_size; | |
725 iconv_t cd; | |
726 | |
727 // check if ucs-4 is available | |
728 cd = iconv_open(charmap, charmap); | |
729 if (cd==(iconv_t)-1) { | |
730 mp_msg(MSGT_OSD, MSGL_ERR, "iconv doesn't know %s encoding. Use the source!\n", charmap); | |
731 return -1; | |
732 } | |
733 | |
734 iconv_close(cd); | |
735 | |
736 cd = iconv_open(charmap, encoding); | |
737 if (cd==(iconv_t)-1) { | |
738 mp_msg(MSGT_OSD, MSGL_ERR, "Unsupported encoding `%s', use iconv --list to list character sets known on your system.\n", encoding); | |
739 return -1; | |
740 } | |
741 | |
742 charset_size = 256 - first_char; | |
743 for (i = 0; i<charset_size; ++i) { | |
744 charcodes[count] = i+first_char; | |
745 charset[count] = decode_char(&cd, i+first_char); | |
746 if (charset[count]!=0) ++count; | |
747 } | |
748 charcodes[count] = charset[count] = 0; ++count; | |
749 charset_size = count; | |
750 | |
751 iconv_close(cd); | |
752 if (charset_size==0) { | |
753 mp_msg(MSGT_OSD, MSGL_ERR, "No characters to render!\n"); | |
754 return -1; | |
755 } | |
756 | |
757 return charset_size; | |
758 } | |
759 | |
760 #ifdef HAVE_FREETYPE21 | |
761 static int prepare_charset_unicode(FT_Face face, FT_ULong *charset, FT_ULong *charcodes) { | |
762 FT_ULong charcode; | |
763 FT_UInt gindex; | |
764 int i; | |
765 | |
766 if (face->charmap==NULL || face->charmap->encoding!=ft_encoding_unicode) { | |
767 WARNING("Unicode charmap not available for this font. Very bad!"); | |
768 return -1; | |
769 } | |
770 | |
771 i = 0; | |
772 charcode = FT_Get_First_Char( face, &gindex ); | |
773 while ( gindex != 0 ) { | |
774 if (charcode < 65536 && charcode >= 33) { // sanity check | |
775 charset[i] = charcode; | |
776 charcodes[i] = 0; | |
777 i++; | |
778 } | |
779 charcode = FT_Get_Next_Char( face, charcode, &gindex ); | |
780 } | |
781 | |
782 mp_msg(MSGT_OSD, MSGL_V, "Unicode font: %d glyphs.\n", i); | |
783 | |
784 return i; | |
785 } | |
786 #endif | |
787 | |
788 static font_desc_t* init_font_desc() | |
789 { | |
790 font_desc_t *desc; | |
791 int i; | |
792 | |
793 desc = malloc(sizeof(font_desc_t)); | |
794 if(!desc) return NULL; | |
795 memset(desc,0,sizeof(font_desc_t)); | |
796 | |
797 /* setup sane defaults */ | |
798 desc->name = NULL; | |
799 desc->fpath = NULL; | |
800 | |
801 desc->face_cnt = 0; | |
802 desc->charspace = 0; | |
803 desc->spacewidth = 0; | |
804 desc->height = 0; | |
805 desc->max_width = 0; | |
806 desc->max_height = 0; | |
807 | |
808 desc->tables.g = NULL; | |
809 desc->tables.gt = NULL; | |
810 desc->tables.gt2 = NULL; | |
811 desc->tables.om = NULL; | |
812 desc->tables.omt = NULL; | |
813 desc->tables.tmp = NULL; | |
814 | |
815 for(i = 0; i < 65536; i++) | |
816 desc->start[i] = desc->width[i] = desc->font[i] = -1; | |
817 for(i = 0; i < 16; i++) | |
818 desc->pic_a[i] = desc->pic_b[i] = NULL; | |
819 | |
820 return desc; | |
821 } | |
822 | |
823 void free_font_desc(font_desc_t *desc) | |
824 { | |
825 int i; | |
826 | |
827 if (!desc) return; | |
828 | |
829 if (desc->name) free(desc->name); | |
830 if (desc->fpath) free(desc->fpath); | |
831 | |
832 for(i = 0; i < 16; i++) { | |
833 if (desc->pic_a[i]) { | |
834 if (desc->pic_a[i]->bmp) free(desc->pic_a[i]->bmp); | |
835 if (desc->pic_a[i]->pal) free(desc->pic_a[i]->pal); | |
836 } | |
837 if (desc->pic_b[i]) { | |
838 if (desc->pic_b[i]->bmp) free(desc->pic_b[i]->bmp); | |
839 if (desc->pic_b[i]->pal) free(desc->pic_b[i]->pal); | |
840 } | |
841 } | |
842 | |
843 if (desc->tables.g) free(desc->tables.g); | |
844 if (desc->tables.gt) free(desc->tables.gt); | |
845 if (desc->tables.gt2) free(desc->tables.gt2); | |
846 if (desc->tables.om) free(desc->tables.om); | |
847 if (desc->tables.omt) free(desc->tables.omt); | |
848 if (desc->tables.tmp) free(desc->tables.tmp); | |
849 | |
850 for(i = 0; i < desc->face_cnt; i++) { | |
851 FT_Done_Face(desc->faces[i]); | |
852 } | |
853 | |
854 free(desc); | |
855 } | |
856 | |
857 static int load_sub_face(char *name, FT_Face *face) | |
858 { | |
859 int err; | |
860 | |
861 if (name) { | |
862 err = FT_New_Face(library, name, 0, face); | |
863 } else { | |
864 err = 1; | |
865 } | |
866 | |
867 if (err) { | |
868 err = FT_New_Face(library, get_path("subfont.ttf"), 0, face); | |
869 if (err) { | |
870 err = FT_New_Face(library, DATADIR"/subfont.ttf", 0, face); | |
871 if (err) { | |
872 mp_msg(MSGT_OSD, MSGL_ERR, "New_Face failed. Maybe the font path is wrong.\n"); | |
873 mp_msg(MSGT_OSD, MSGL_ERR, "Please supply the text font file (~/.mplayer/subfont.ttf).\n"); | |
874 return -1; | |
875 } | |
876 } | |
877 } | |
878 return err; | |
879 } | |
880 | |
881 static int load_osd_face(FT_Face *face) | |
882 { | |
883 int err; | |
884 | |
885 err = FT_New_Memory_Face(library, osd_font_pfb, sizeof(osd_font_pfb), 0, face); | |
886 if (err) { | |
887 mp_msg(MSGT_OSD, MSGL_ERR, "New_Memory_Face failed..\n"); | |
888 return -1; | |
889 } | |
890 return 0; | |
891 } | |
892 | |
893 int kerning(font_desc_t *desc, int prevc, int c) | |
894 { | |
895 FT_Vector kern; | |
896 | |
897 if (prevc < 0 || c < 0) return 0; | |
898 if (desc->font[prevc] != desc->font[c]) return 0; | |
899 if (desc->font[prevc] == -1 || desc->font[c] == -1) return 0; | |
900 FT_Get_Kerning(desc->faces[desc->font[c]], | |
901 desc->glyph_index[prevc], desc->glyph_index[c], | |
902 ft_kerning_default, &kern); | |
903 | |
904 // fprintf(stderr, "kern: %c %c %d\n", prevc, c, f266ToInt(kern.x)); | |
905 | |
906 return f266ToInt(kern.x); | |
907 } | |
908 | |
909 font_desc_t* read_font_desc(char *fname, float factor, int movie_width, int movie_height) | |
910 { | |
911 font_desc_t *desc; | |
912 | |
913 FT_Face face; | |
914 | |
915 FT_ULong my_charset[MAX_CHARSET_SIZE]; /* characters we want to render; Unicode */ | |
916 FT_ULong my_charcodes[MAX_CHARSET_SIZE]; /* character codes in 'encoding' */ | |
917 | |
918 char *charmap = "ucs-4"; | |
919 int err; | |
920 int charset_size; | |
921 int i, j; | |
922 int unicode; | |
923 | |
924 float movie_size; | |
925 | |
926 float subtitle_font_ppem; | |
927 float osd_font_ppem; | |
928 | |
929 switch (subtitle_autoscale) { | |
930 case 0: | |
931 movie_size = 100; | |
932 break; | |
933 case 1: | |
934 movie_size = movie_height; | |
935 break; | |
936 case 2: | |
937 movie_size = movie_width; | |
938 break; | |
939 case 3: | |
940 movie_size = sqrt(movie_height*movie_height+movie_width*movie_width); | |
941 break; | |
942 } | |
943 | |
944 subtitle_font_ppem = movie_size*text_font_scale_factor/100.0; | |
945 osd_font_ppem = movie_size*osd_font_scale_factor/100.0; | |
946 | |
947 if (subtitle_font_ppem < 5) subtitle_font_ppem = 5; | |
948 if (osd_font_ppem < 5) osd_font_ppem = 5; | |
949 | |
950 #ifdef HAVE_FREETYPE21 | |
951 if ((subtitle_font_encoding == NULL) | |
952 || (strcasecmp(subtitle_font_encoding, "unicode") == 0)) { | |
953 unicode = 1; | |
954 } else { | |
955 unicode = 0; | |
956 } | |
957 #else | |
958 unicode = 0; | |
959 #endif | |
960 | |
961 desc = init_font_desc(); | |
962 if(!desc) return NULL; | |
963 | |
964 // t=GetTimer(); | |
965 | |
966 /* generate the subtitle font */ | |
967 err = load_sub_face(fname, &face); | |
968 if (err) { | |
969 mp_msg(MSGT_OSD, MSGL_ERR, "subtitle font: load_sub_face failed.\n"); | |
970 goto gen_osd; | |
971 } | |
972 | |
973 #ifdef HAVE_FREETYPE21 | |
974 if (unicode) { | |
975 charset_size = prepare_charset_unicode(face, my_charset, my_charcodes); | |
976 } else { | |
977 if (subtitle_font_encoding) { | |
978 charset_size = prepare_charset(charmap, subtitle_font_encoding, my_charset, my_charcodes); | |
979 } else { | |
980 charset_size = prepare_charset(charmap, "iso-8859-1", my_charset, my_charcodes); | |
981 } | |
982 } | |
983 #else | |
984 if (subtitle_font_encoding) { | |
985 charset_size = prepare_charset(charmap, subtitle_font_encoding, my_charset, my_charcodes); | |
986 } else { | |
987 charset_size = prepare_charset(charmap, "iso-8859-1", my_charset, my_charcodes); | |
988 } | |
989 #endif | |
990 | |
991 if (charset_size < 0) { | |
992 mp_msg(MSGT_OSD, MSGL_ERR, "subtitle font: prepare_charset failed.\n"); | |
993 free_font_desc(desc); | |
994 return NULL; | |
995 } | |
996 | |
997 // fprintf(stderr, "fg: prepare t = %lf\n", GetTimer()-t); | |
998 | |
999 err = prepare_font(desc, face, subtitle_font_ppem, desc->face_cnt, | |
1000 charset_size, my_charset, my_charcodes, unicode, | |
1001 subtitle_font_thickness, subtitle_font_radius); | |
1002 | |
1003 if (err) { | |
1004 mp_msg(MSGT_OSD, MSGL_ERR, "Cannot prepare subtitle font.\n"); | |
1005 free_font_desc(desc); | |
1006 return NULL; | |
1007 } | |
1008 desc->face_cnt++; | |
1009 | |
1010 gen_osd: | |
1011 | |
1012 /* generate the OSD font */ | |
1013 err = load_osd_face(&face); | |
1014 if (err) { | |
1015 free_font_desc(desc); | |
1016 return NULL; | |
1017 } | |
1018 err = prepare_font(desc, face, osd_font_ppem, desc->face_cnt, | |
1019 OSD_CHARSET_SIZE, osd_charset, osd_charcodes, 0, | |
1020 subtitle_font_thickness, subtitle_font_radius); | |
1021 | |
1022 if (err) { | |
1023 mp_msg(MSGT_OSD, MSGL_ERR, "Cannot prepare OSD font.\n"); | |
1024 free_font_desc(desc); | |
1025 return NULL; | |
1026 } | |
1027 desc->face_cnt++; | |
1028 | |
1029 err = generate_tables(desc, subtitle_font_thickness, subtitle_font_radius); | |
1030 | |
1031 if (err) { | |
1032 mp_msg(MSGT_OSD, MSGL_ERR, "Cannot generate tables.\n"); | |
1033 free_font_desc(desc); | |
1034 return NULL; | |
1035 } | |
1036 | |
1037 // final cleanup | |
1038 desc->font[' ']=-1; | |
1039 desc->width[' ']=desc->spacewidth; | |
1040 | |
1041 j = '_'; | |
1042 if (desc->font[j] < 0) j = '?'; | |
1043 if (desc->font[j] < 0) j = ' '; | |
1044 render_one_glyph(desc, j); | |
1045 for(i = 0; i < 65536; i++) { | |
1046 if (desc->font[i] < 0 && i != ' ') { | |
1047 desc->start[i] = desc->start[j]; | |
1048 desc->width[i] = desc->width[j]; | |
1049 desc->font[i] = desc->font[j]; | |
1050 } | |
1051 } | |
1052 return desc; | |
1053 } | |
1054 | |
1055 int init_freetype() | |
1056 { | |
1057 int err; | |
1058 | |
1059 /* initialize freetype */ | |
1060 err = FT_Init_FreeType(&library); | |
1061 if (err) { | |
1062 mp_msg(MSGT_OSD, MSGL_ERR, "Init_FreeType failed.\n"); | |
1063 return -1; | |
1064 } | |
1065 fprintf(stderr, "init_freetype\n"); | |
1066 return 0; | |
1067 } | |
1068 | |
1069 int done_freetype() | |
1070 { | |
1071 int err; | |
1072 | |
1073 err = FT_Done_FreeType(library); | |
1074 if (err) { | |
1075 mp_msg(MSGT_OSD, MSGL_ERR, "FT_Done_FreeType failed.\n"); | |
1076 return -1; | |
1077 } | |
1078 | |
1079 return 0; | |
1080 } | |
1081 | |
1082 void load_font(int width, int height) | |
1083 { | |
1084 vo_image_width = width; | |
1085 vo_image_height = height; | |
1086 | |
1087 if (vo_font) free_font_desc(vo_font); | |
1088 | |
1089 #ifdef USE_OSD | |
1090 vo_font=read_font_desc(font_name, font_factor, width, height); | |
1091 #endif | |
1092 } | |
1093 | |
1094 #endif /* HAVE_FREETYPE */ |