Globally ignore all example binaries.
[mplayer/glamo.git] / libvo / font_load_ft.c
blob20d673a0fcbb944ac147821fc59af3fb1e323783
1 /*
2 * Renders antialiased fonts for mplayer using freetype library.
3 * Should work with TrueType, Type1 and any other font supported by libfreetype.
5 * Artur Zaprzala <zybi@fanthom.irc.pl>
7 * ported inside MPlayer by Jindrich Makovicka <makovick@gmail.com>
9 * This file is part of MPlayer.
11 * MPlayer is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * MPlayer is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <math.h>
31 #include <string.h>
33 #ifdef CONFIG_ICONV
34 #include <iconv.h>
35 #endif
37 #include <ft2build.h>
38 #include FT_FREETYPE_H
39 #include FT_GLYPH_H
41 #ifdef CONFIG_FONTCONFIG
42 #include <fontconfig/fontconfig.h>
43 #endif
45 #include "libavutil/common.h"
46 #include "mpbswap.h"
47 #include "font_load.h"
48 #include "mp_msg.h"
49 #include "help_mp.h"
50 #include "mplayer.h"
51 #include "get_path.h"
52 #include "osd_font.h"
54 #if (FREETYPE_MAJOR > 2) || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 1)
55 #define HAVE_FREETYPE21
56 #endif
58 char *subtitle_font_encoding = NULL;
59 float text_font_scale_factor = 3.5;
60 float osd_font_scale_factor = 4.0;
61 float subtitle_font_radius = 2.0;
62 float subtitle_font_thickness = 2.0;
63 // 0 = no autoscale
64 // 1 = video height
65 // 2 = video width
66 // 3 = diagonal
67 int subtitle_autoscale = 3;
69 int vo_image_width = 0;
70 int vo_image_height = 0;
71 int force_load_font;
73 int using_freetype = 0;
74 int font_fontconfig = 0;
76 //// constants
77 static unsigned int const colors = 256;
78 static unsigned int const maxcolor = 255;
79 static unsigned const base = 256;
80 static unsigned const first_char = 33;
81 #define MAX_CHARSET_SIZE 60000
83 static FT_Library library;
85 #define OSD_CHARSET_SIZE 15
87 static FT_ULong osd_charset[OSD_CHARSET_SIZE] =
89 0xe001, 0xe002, 0xe003, 0xe004, 0xe005, 0xe006, 0xe007, 0xe008,
90 0xe009, 0xe00a, 0xe00b, 0xe010, 0xe011, 0xe012, 0xe013
93 static FT_ULong osd_charcodes[OSD_CHARSET_SIZE] =
95 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
96 0x09,0x0a,0x0b,0x10,0x11,0x12,0x13
99 #define f266ToInt(x) (((x)+32)>>6) // round fractional fixed point number to integer
100 // coordinates are in 26.6 pixels (i.e. 1/64th of pixels)
101 #define f266CeilToInt(x) (((x)+63)>>6) // ceiling
102 #define f266FloorToInt(x) ((x)>>6) // floor
103 #define f1616ToInt(x) (((x)+0x8000)>>16) // 16.16
104 #define floatTof266(x) ((int)((x)*(1<<6)+0.5))
106 #define ALIGN(x) (((x)+7)&~7) // 8 byte align
108 #define WARNING(msg, args...) mp_msg(MSGT_OSD, MSGL_WARN, msg "\n", ## args)
110 #define DEBUG 0
112 //static double ttime;
115 static void paste_bitmap(unsigned char *bbuffer, FT_Bitmap *bitmap, int x, int y, int width, int height, int bwidth) {
116 int drow = x+y*width;
117 int srow = 0;
118 int sp, dp, w, h;
119 if (bitmap->pixel_mode==ft_pixel_mode_mono)
120 for (h = bitmap->rows; h>0 && height > 0; --h, height--, drow+=width, srow+=bitmap->pitch)
121 for (w = bwidth, sp=dp=0; w>0; --w, ++dp, ++sp)
122 bbuffer[drow+dp] = (bitmap->buffer[srow+sp/8] & (0x80>>(sp%8))) ? 255:0;
123 else
124 for (h = bitmap->rows; h>0 && height > 0; --h, height--, drow+=width, srow+=bitmap->pitch)
125 for (w = bwidth, sp=dp=0; w>0; --w, ++dp, ++sp)
126 bbuffer[drow+dp] = bitmap->buffer[srow+sp];
130 static int check_font(font_desc_t *desc, float ppem, int padding, int pic_idx,
131 int charset_size, FT_ULong *charset, FT_ULong *charcodes,
132 int unicode) {
133 FT_Error error;
134 FT_Face face = desc->faces[pic_idx];
135 int const load_flags = FT_LOAD_DEFAULT;
136 int ymin = INT_MAX, ymax = INT_MIN;
137 int space_advance = 20;
138 int width, height;
139 unsigned char *bbuffer;
140 int i, uni_charmap = 1;
142 error = FT_Select_Charmap(face, ft_encoding_unicode);
143 // fprintf(stderr, "select unicode charmap: %d\n", error);
145 if (face->charmap==NULL || face->charmap->encoding!=ft_encoding_unicode) {
146 WARNING("Unicode charmap not available for this font. Very bad!");
147 uni_charmap = 0;
148 error = FT_Set_Charmap(face, face->charmaps[0]);
149 if (error) WARNING("No charmaps! Strange.");
152 /* set size */
153 if (FT_IS_SCALABLE(face)) {
154 error = FT_Set_Char_Size(face, 0, floatTof266(ppem), 0, 0);
155 if (error) WARNING("FT_Set_Char_Size failed.");
156 } else {
157 int j = 0;
158 int jppem = face->available_sizes[0].height;
159 /* find closest size */
160 for (i = 0; i<face->num_fixed_sizes; ++i) {
161 if (fabs(face->available_sizes[i].height - ppem) < abs(face->available_sizes[i].height - jppem)) {
162 j = i;
163 jppem = face->available_sizes[i].height;
166 WARNING("Selected font is not scalable. Using ppem=%i.", face->available_sizes[j].height);
167 error = FT_Set_Pixel_Sizes(face, face->available_sizes[j].width, face->available_sizes[j].height);
168 if (error) WARNING("FT_Set_Pixel_Sizes failed.");
171 if (FT_IS_FIXED_WIDTH(face))
172 WARNING("Selected font is fixed-width.");
174 /* compute space advance */
175 error = FT_Load_Char(face, ' ', load_flags);
176 if (error) WARNING("spacewidth set to default.");
177 else space_advance = f266ToInt(face->glyph->advance.x);
179 if (!desc->spacewidth) desc->spacewidth = 2*padding + space_advance;
180 if (!desc->charspace) desc->charspace = -2*padding;
181 if (!desc->height) desc->height = f266ToInt(face->size->metrics.height);
184 for (i= 0; i<charset_size; ++i) {
185 FT_ULong character, code;
186 FT_UInt glyph_index;
188 character = charset[i];
189 code = charcodes[i];
190 desc->font[unicode?character:code] = pic_idx;
191 // get glyph index
192 if (character==0)
193 glyph_index = 0;
194 else {
195 glyph_index = FT_Get_Char_Index(face, uni_charmap ? character:code);
196 if (glyph_index==0) {
197 WARNING("Glyph for char 0x%02lx|U+%04lX|%c not found.", code, character,
198 code<' '||code>255 ? '.':(char)code);
199 desc->font[unicode?character:code] = -1;
200 continue;
203 desc->glyph_index[unicode?character:code] = glyph_index;
205 // fprintf(stderr, "font height: %lf\n", (double)(face->bbox.yMax-face->bbox.yMin)/(double)face->units_per_EM*ppem);
206 // fprintf(stderr, "font width: %lf\n", (double)(face->bbox.xMax-face->bbox.xMin)/(double)face->units_per_EM*ppem);
208 ymax = (double)(face->bbox.yMax)/(double)face->units_per_EM*ppem+1;
209 ymin = (double)(face->bbox.yMin)/(double)face->units_per_EM*ppem-1;
211 width = ppem*(face->bbox.xMax-face->bbox.xMin)/face->units_per_EM+3+2*padding;
212 if (desc->max_width < width) desc->max_width = width;
213 width = ALIGN(width);
214 desc->pic_b[pic_idx]->charwidth = width;
216 if (width <= 0) {
217 mp_msg(MSGT_OSD, MSGL_ERR, "Wrong bounding box, width <= 0 !\n");
218 return -1;
221 if (ymax<=ymin) {
222 mp_msg(MSGT_OSD, MSGL_ERR, "Something went wrong. Use the source!\n");
223 return -1;
226 height = ymax - ymin + 2*padding;
227 if (height <= 0) {
228 mp_msg(MSGT_OSD, MSGL_ERR, "Wrong bounding box, height <= 0 !\n");
229 return -1;
232 if (desc->max_height < height) desc->max_height = height;
233 desc->pic_b[pic_idx]->charheight = height;
235 // fprintf(stderr, "font height2: %d\n", height);
236 desc->pic_b[pic_idx]->baseline = ymax + padding;
237 desc->pic_b[pic_idx]->padding = padding;
238 desc->pic_b[pic_idx]->current_alloc = 0;
239 desc->pic_b[pic_idx]->current_count = 0;
241 bbuffer = NULL;
243 desc->pic_b[pic_idx]->w = width;
244 desc->pic_b[pic_idx]->h = height;
245 desc->pic_b[pic_idx]->c = colors;
246 desc->pic_b[pic_idx]->bmp = bbuffer;
247 desc->pic_b[pic_idx]->pen = 0;
248 return 0;
251 // general outline
252 static void outline(
253 unsigned char *s,
254 unsigned char *t,
255 int width,
256 int height,
257 int stride,
258 unsigned char *m,
259 int r,
260 int mwidth,
261 int msize) {
263 int x, y;
265 for (y = 0; y<height; y++) {
266 for (x = 0; x<width; x++) {
267 const int src= s[x];
268 if(src==0) continue;
270 const int x1=(x<r) ? r-x : 0;
271 const int y1=(y<r) ? r-y : 0;
272 const int x2=(x+r>=width ) ? r+width -x : 2*r+1;
273 const int y2=(y+r>=height) ? r+height-y : 2*r+1;
274 register unsigned char *dstp= t + (y1+y-r)* stride + x-r;
275 //register int *mp = m + y1 *mwidth;
276 register unsigned char *mp= m + msize*src + y1*mwidth;
277 int my;
279 for(my= y1; my<y2; my++){
280 register int mx;
281 for(mx= x1; mx<x2; mx++){
282 if(dstp[mx] < mp[mx]) dstp[mx]= mp[mx];
284 dstp+=stride;
285 mp+=mwidth;
289 s+= stride;
294 // 1 pixel outline
295 static void outline1(
296 unsigned char *s,
297 unsigned char *t,
298 int width,
299 int height,
300 int stride) {
302 int x, y;
303 int skip = stride-width;
305 for (x = 0; x<width; ++x, ++s, ++t) *t = *s;
306 s += skip;
307 t += skip;
308 for (y = 1; y<height-1; ++y) {
309 *t++ = *s++;
310 for (x = 1; x<width-1; ++x, ++s, ++t) {
311 unsigned v = (
312 s[-1-stride]+
313 s[-1+stride]+
314 s[+1-stride]+
315 s[+1+stride]
316 )/2 + (
317 s[-1]+
318 s[+1]+
319 s[-stride]+
320 s[+stride]+
321 s[0]
323 *t = v>maxcolor ? maxcolor : v;
325 *t++ = *s++;
326 s += skip;
327 t += skip;
329 for (x = 0; x<width; ++x, ++s, ++t) *t = *s;
332 // "0 pixel outline"
333 static void outline0(
334 unsigned char *s,
335 unsigned char *t,
336 int width,
337 int height,
338 int stride) {
339 int y;
340 for (y = 0; y<height; ++y) {
341 memcpy(t, s, width);
342 s += stride;
343 t += stride;
347 // gaussian blur
348 void blur(
349 unsigned char *buffer,
350 unsigned short *tmp2,
351 int width,
352 int height,
353 int stride,
354 int *m2,
355 int r,
356 int mwidth) {
358 int x, y;
360 unsigned char *s = buffer;
361 unsigned short *t = tmp2+1;
362 for(y=0; y<height; y++){
363 memset(t-1, 0, (width+1)*sizeof(short));
365 for(x=0; x<r; x++){
366 const int src= s[x];
367 if(src){
368 register unsigned short *dstp= t + x-r;
369 int mx;
370 unsigned *m3= m2 + src*mwidth;
371 for(mx=r-x; mx<mwidth; mx++){
372 dstp[mx]+= m3[mx];
377 for(; x<width-r; x++){
378 const int src= s[x];
379 if(src){
380 register unsigned short *dstp= t + x-r;
381 int mx;
382 unsigned *m3= m2 + src*mwidth;
383 for(mx=0; mx<mwidth; mx++){
384 dstp[mx]+= m3[mx];
389 for(; x<width; x++){
390 const int src= s[x];
391 if(src){
392 register unsigned short *dstp= t + x-r;
393 int mx;
394 const int x2= r+width -x;
395 unsigned *m3= m2 + src*mwidth;
396 for(mx=0; mx<x2; mx++){
397 dstp[mx]+= m3[mx];
402 s+= stride;
403 t+= width + 1;
406 t = tmp2;
407 for(x=0; x<width; x++){
408 for(y=0; y<r; y++){
409 unsigned short *srcp= t + y*(width+1) + 1;
410 int src= *srcp;
411 if(src){
412 register unsigned short *dstp= srcp - 1 + width+1;
413 const int src2= (src + 128)>>8;
414 unsigned *m3= m2 + src2*mwidth;
416 int mx;
417 *srcp= 128;
418 for(mx=r-1; mx<mwidth; mx++){
419 *dstp += m3[mx];
420 dstp+= width+1;
424 for(; y<height-r; y++){
425 unsigned short *srcp= t + y*(width+1) + 1;
426 int src= *srcp;
427 if(src){
428 register unsigned short *dstp= srcp - 1 - r*(width+1);
429 const int src2= (src + 128)>>8;
430 unsigned *m3= m2 + src2*mwidth;
432 int mx;
433 *srcp= 128;
434 for(mx=0; mx<mwidth; mx++){
435 *dstp += m3[mx];
436 dstp+= width+1;
440 for(; y<height; y++){
441 unsigned short *srcp= t + y*(width+1) + 1;
442 int src= *srcp;
443 if(src){
444 const int y2=r+height-y;
445 register unsigned short *dstp= srcp - 1 - r*(width+1);
446 const int src2= (src + 128)>>8;
447 unsigned *m3= m2 + src2*mwidth;
449 int mx;
450 *srcp= 128;
451 for(mx=0; mx<y2; mx++){
452 *dstp += m3[mx];
453 dstp+= width+1;
457 t++;
460 t = tmp2;
461 s = buffer;
462 for(y=0; y<height; y++){
463 for(x=0; x<width; x++){
464 s[x]= t[x]>>8;
466 s+= stride;
467 t+= width + 1;
471 static void resample_alpha(unsigned char *abuf, unsigned char *bbuf, int width, int height, int stride, float factor)
473 int f=factor*256.0f;
474 int i,j;
475 for (i = 0; i < height; i++) {
476 unsigned char *a = abuf+i*stride;
477 unsigned char *b = bbuf+i*stride;
478 for(j=0;j<width;j++,a++,b++){
479 int x=*a; // alpha
480 int y=*b; // bitmap
481 x=255-((x*f)>>8); // scale
482 if (x+y>255) x=255-y; // to avoid overflows
483 if (x<1) x=1; else if (x>=252) x=0;
484 *a=x;
489 #define ALLOC_INCR 32
490 void render_one_glyph(font_desc_t *desc, int c)
492 FT_GlyphSlot slot;
493 FT_UInt glyph_index;
494 FT_BitmapGlyph glyph;
495 int width, height, stride, maxw, off;
496 unsigned char *abuffer, *bbuffer;
498 int const load_flags = FT_LOAD_DEFAULT;
499 int pen_xa;
500 int font = desc->font[c];
501 int error;
503 // fprintf(stderr, "render_one_glyph %d\n", c);
505 if (!desc->dynamic) return;
506 if (desc->width[c] != -1) return;
507 if (desc->font[c] == -1) return;
509 glyph_index = desc->glyph_index[c];
511 // load glyph
512 error = FT_Load_Glyph(desc->faces[font], glyph_index, load_flags);
513 if (error) {
514 WARNING("FT_Load_Glyph 0x%02x (char 0x%04x) failed.", glyph_index, c);
515 desc->font[c] = -1;
516 return;
518 slot = desc->faces[font]->glyph;
520 // render glyph
521 if (slot->format != ft_glyph_format_bitmap) {
522 error = FT_Render_Glyph(slot, ft_render_mode_normal);
523 if (error) {
524 WARNING("FT_Render_Glyph 0x%04x (char 0x%04x) failed.", glyph_index, c);
525 desc->font[c] = -1;
526 return;
530 // extract glyph image
531 error = FT_Get_Glyph(slot, (FT_Glyph*)&glyph);
532 if (error) {
533 WARNING("FT_Get_Glyph 0x%04x (char 0x%04x) failed.", glyph_index, c);
534 desc->font[c] = -1;
535 return;
538 // fprintf(stderr, "glyph generated\n");
540 maxw = desc->pic_b[font]->charwidth;
542 if (glyph->bitmap.width > maxw) {
543 fprintf(stderr, "glyph too wide!\n");
546 // allocate new memory, if needed
547 // fprintf(stderr, "\n%d %d %d\n", desc->pic_b[font]->charwidth, desc->pic_b[font]->charheight, desc->pic_b[font]->current_alloc);
548 if (desc->pic_b[font]->current_count >= desc->pic_b[font]->current_alloc) {
549 int newsize = desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight*(desc->pic_b[font]->current_alloc+ALLOC_INCR);
550 int increment = desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight*ALLOC_INCR;
551 desc->pic_b[font]->current_alloc += ALLOC_INCR;
553 // fprintf(stderr, "\nns = %d inc = %d\n", newsize, increment);
555 desc->pic_b[font]->bmp = realloc(desc->pic_b[font]->bmp, newsize);
556 desc->pic_a[font]->bmp = realloc(desc->pic_a[font]->bmp, newsize);
558 off = desc->pic_b[font]->current_count*desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight;
559 memset(desc->pic_b[font]->bmp+off, 0, increment);
560 memset(desc->pic_a[font]->bmp+off, 0, increment);
563 abuffer = desc->pic_a[font]->bmp;
564 bbuffer = desc->pic_b[font]->bmp;
566 off = desc->pic_b[font]->current_count*desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight;
568 paste_bitmap(bbuffer+off,
569 &glyph->bitmap,
570 desc->pic_b[font]->padding + glyph->left,
571 desc->pic_b[font]->baseline - glyph->top,
572 desc->pic_b[font]->charwidth, desc->pic_b[font]->charheight,
573 glyph->bitmap.width <= maxw ? glyph->bitmap.width : maxw);
575 // fprintf(stderr, "glyph pasted\n");
576 FT_Done_Glyph((FT_Glyph)glyph);
578 /* advance pen */
579 pen_xa = f266ToInt(slot->advance.x) + 2*desc->pic_b[font]->padding;
580 if (pen_xa > maxw) pen_xa = maxw;
582 desc->start[c] = off;
583 width = desc->width[c] = pen_xa;
584 height = desc->pic_b[font]->charheight;
585 stride = desc->pic_b[font]->w;
587 if (desc->tables.o_r == 0) {
588 outline0(bbuffer+off, abuffer+off, width, height, stride);
589 } else if (desc->tables.o_r == 1) {
590 outline1(bbuffer+off, abuffer+off, width, height, stride);
591 } else {
592 outline(bbuffer+off, abuffer+off, width, height, stride,
593 desc->tables.omt, desc->tables.o_r, desc->tables.o_w,
594 desc->tables.o_size);
596 // fprintf(stderr, "fg: outline t = %lf\n", GetTimer()-t);
598 if (desc->tables.g_r) {
599 blur(abuffer+off, desc->tables.tmp, width, height, stride,
600 desc->tables.gt2, desc->tables.g_r,
601 desc->tables.g_w);
602 // fprintf(stderr, "fg: blur t = %lf\n", GetTimer()-t);
605 resample_alpha(abuffer+off, bbuffer+off, width, height, stride, font_factor);
607 desc->pic_b[font]->current_count++;
611 static int prepare_font(font_desc_t *desc, FT_Face face, float ppem, int pic_idx,
612 int charset_size, FT_ULong *charset, FT_ULong *charcodes, int unicode,
613 double thickness, double radius)
615 int i, err;
616 int padding = ceil(radius) + ceil(thickness);
618 desc->faces[pic_idx] = face;
620 desc->pic_a[pic_idx] = malloc(sizeof(raw_file));
621 if (!desc->pic_a[pic_idx]) return -1;
622 desc->pic_b[pic_idx] = malloc(sizeof(raw_file));
623 if (!desc->pic_b[pic_idx]) return -1;
625 desc->pic_a[pic_idx]->bmp = NULL;
626 desc->pic_a[pic_idx]->pal = NULL;
627 desc->pic_b[pic_idx]->bmp = NULL;
628 desc->pic_b[pic_idx]->pal = NULL;
630 desc->pic_a[pic_idx]->pal = malloc(sizeof(unsigned char)*256*3);
631 if (!desc->pic_a[pic_idx]->pal) return -1;
632 for (i = 0; i<768; ++i) desc->pic_a[pic_idx]->pal[i] = i/3;
634 desc->pic_b[pic_idx]->pal = malloc(sizeof(unsigned char)*256*3);
635 if (!desc->pic_b[pic_idx]->pal) return -1;
636 for (i = 0; i<768; ++i) desc->pic_b[pic_idx]->pal[i] = i/3;
638 // ttime = GetTimer();
639 err = check_font(desc, ppem, padding, pic_idx, charset_size, charset, charcodes, unicode);
640 // ttime=GetTimer()-ttime;
641 // printf("render: %7lf us\n",ttime);
642 if (err) return -1;
643 // fprintf(stderr, "fg: render t = %lf\n", GetTimer()-t);
645 desc->pic_a[pic_idx]->w = desc->pic_b[pic_idx]->w;
646 desc->pic_a[pic_idx]->h = desc->pic_b[pic_idx]->h;
647 desc->pic_a[pic_idx]->c = colors;
649 desc->pic_a[pic_idx]->bmp = NULL;
651 // fprintf(stderr, "fg: w = %d, h = %d\n", desc->pic_a[pic_idx]->w, desc->pic_a[pic_idx]->h);
652 return 0;
656 static int generate_tables(font_desc_t *desc, double thickness, double radius)
658 int width = desc->max_height;
659 int height = desc->max_width;
661 double A = log(1.0/base)/(radius*radius*2);
662 int mx, my, i;
663 double volume_diff, volume_factor = 0;
664 unsigned char *omtp;
666 desc->tables.g_r = ceil(radius);
667 desc->tables.o_r = ceil(thickness);
668 desc->tables.g_w = 2*desc->tables.g_r+1;
669 desc->tables.o_w = 2*desc->tables.o_r+1;
670 desc->tables.o_size = desc->tables.o_w * desc->tables.o_w;
672 // fprintf(stderr, "o_r = %d\n", desc->tables.o_r);
674 if (desc->tables.g_r) {
675 desc->tables.g = malloc(desc->tables.g_w * sizeof(unsigned));
676 desc->tables.gt2 = malloc(256 * desc->tables.g_w * sizeof(unsigned));
677 if (desc->tables.g==NULL || desc->tables.gt2==NULL) {
678 return -1;
681 desc->tables.om = malloc(desc->tables.o_w*desc->tables.o_w * sizeof(unsigned));
682 desc->tables.omt = malloc(desc->tables.o_size*256);
684 omtp = desc->tables.omt;
685 desc->tables.tmp = malloc((width+1)*height*sizeof(short));
687 if (desc->tables.om==NULL || desc->tables.omt==NULL || desc->tables.tmp==NULL) {
688 return -1;
691 if (desc->tables.g_r) {
692 // gaussian curve with volume = 256
693 for (volume_diff=10000000; volume_diff>0.0000001; volume_diff*=0.5){
694 volume_factor+= volume_diff;
695 desc->tables.volume=0;
696 for (i = 0; i<desc->tables.g_w; ++i) {
697 desc->tables.g[i] = (unsigned)(exp(A * (i-desc->tables.g_r)*(i-desc->tables.g_r)) * volume_factor + .5);
698 desc->tables.volume+= desc->tables.g[i];
700 if(desc->tables.volume>256) volume_factor-= volume_diff;
702 desc->tables.volume=0;
703 for (i = 0; i<desc->tables.g_w; ++i) {
704 desc->tables.g[i] = (unsigned)(exp(A * (i-desc->tables.g_r)*(i-desc->tables.g_r)) * volume_factor + .5);
705 desc->tables.volume+= desc->tables.g[i];
708 // gauss table:
709 for(mx=0;mx<desc->tables.g_w;mx++){
710 for(i=0;i<256;i++){
711 desc->tables.gt2[mx+i*desc->tables.g_w] = i*desc->tables.g[mx];
716 /* outline matrix */
717 for (my = 0; my<desc->tables.o_w; ++my) {
718 for (mx = 0; mx<desc->tables.o_w; ++mx) {
719 // antialiased circle would be perfect here, but this one is good enough
720 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));
721 desc->tables.om[mx+my*desc->tables.o_w] = d>=1 ? base : d<=0 ? 0 : (d*base + .5);
725 // outline table:
726 for(i=0;i<256;i++){
727 for(mx=0;mx<desc->tables.o_size;mx++) *(omtp++) = (i*desc->tables.om[mx] + (base/2))/base;
730 return 0;
733 #ifdef CONFIG_ICONV
734 /* decode from 'encoding' to unicode */
735 static FT_ULong decode_char(iconv_t *cd, char c) {
736 FT_ULong o;
737 char *inbuf = &c;
738 char *outbuf = (char*)&o;
739 size_t inbytesleft = 1;
740 size_t outbytesleft = sizeof(FT_ULong);
742 iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
744 /* convert unicode BigEndian -> MachineEndian */
745 o = be2me_32(o);
747 // if (count==-1) o = 0; // not OK, at least my iconv() returns E2BIG for all
748 if (outbytesleft!=0) o = 0;
750 /* we don't want control characters */
751 if (o>=0x7f && o<0xa0) o = 0;
752 return o;
755 static int prepare_charset(char *charmap, char *encoding, FT_ULong *charset, FT_ULong *charcodes) {
756 FT_ULong i;
757 int count = 0;
758 int charset_size;
759 iconv_t cd;
761 // check if ucs-4 is available
762 cd = iconv_open(charmap, charmap);
763 if (cd==(iconv_t)-1) {
764 mp_msg(MSGT_OSD, MSGL_ERR, "iconv doesn't know %s encoding. Use the source!\n", charmap);
765 return -1;
768 iconv_close(cd);
770 cd = iconv_open(charmap, encoding);
771 if (cd==(iconv_t)-1) {
772 mp_msg(MSGT_OSD, MSGL_ERR, "Unsupported encoding `%s', use iconv --list to list character sets known on your system.\n", encoding);
773 return -1;
776 charset_size = 256 - first_char;
777 for (i = 0; i<charset_size; ++i) {
778 charcodes[count] = i+first_char;
779 charset[count] = decode_char(&cd, i+first_char);
780 if (charset[count]!=0) ++count;
782 charcodes[count] = charset[count] = 0; ++count;
783 charset_size = count;
785 iconv_close(cd);
786 if (charset_size==0) {
787 mp_msg(MSGT_OSD, MSGL_ERR, "No characters to render!\n");
788 return -1;
791 return charset_size;
794 static int prepare_charset_unicode(FT_Face face, FT_ULong *charset, FT_ULong *charcodes) {
795 #ifdef HAVE_FREETYPE21
796 FT_ULong charcode;
797 #else
798 int j;
799 #endif
800 FT_UInt gindex;
801 int i;
803 if (face->charmap==NULL || face->charmap->encoding!=ft_encoding_unicode) {
804 WARNING("Unicode charmap not available for this font. Very bad!");
805 return -1;
807 #ifdef HAVE_FREETYPE21
808 i = 0;
809 charcode = FT_Get_First_Char( face, &gindex );
810 while (gindex != 0) {
811 if (charcode < 65536 && charcode >= 33) { // sanity check
812 charset[i] = charcode;
813 charcodes[i] = 0;
814 i++;
816 charcode = FT_Get_Next_Char( face, charcode, &gindex );
818 #else
819 // for FT < 2.1 we have to use brute force enumeration
820 i = 0;
821 for (j = 33; j < 65536; j++) {
822 gindex = FT_Get_Char_Index(face, j);
823 if (gindex > 0) {
824 charset[i] = j;
825 charcodes[i] = 0;
826 i++;
829 #endif
830 mp_msg(MSGT_OSD, MSGL_V, "Unicode font: %d glyphs.\n", i);
832 return i;
834 #endif
836 static font_desc_t* init_font_desc(void)
838 font_desc_t *desc;
839 int i;
841 desc = malloc(sizeof(font_desc_t));
842 if(!desc) return NULL;
843 memset(desc,0,sizeof(font_desc_t));
845 desc->dynamic = 1;
847 /* setup sane defaults */
848 desc->name = NULL;
849 desc->fpath = NULL;
851 desc->face_cnt = 0;
852 desc->charspace = 0;
853 desc->spacewidth = 0;
854 desc->height = 0;
855 desc->max_width = 0;
856 desc->max_height = 0;
857 desc->freetype = 1;
859 desc->tables.g = NULL;
860 desc->tables.gt2 = NULL;
861 desc->tables.om = NULL;
862 desc->tables.omt = NULL;
863 desc->tables.tmp = NULL;
865 for(i = 0; i < 65536; i++)
866 desc->start[i] = desc->width[i] = desc->font[i] = -1;
867 for(i = 0; i < 16; i++)
868 desc->pic_a[i] = desc->pic_b[i] = NULL;
870 return desc;
873 void free_font_desc(font_desc_t *desc)
875 int i;
877 if (!desc) return;
879 // if (!desc->dynamic) return; // some vo_aa crap, better leaking than crashing
881 if (desc->name) free(desc->name);
882 if (desc->fpath) free(desc->fpath);
884 for(i = 0; i < 16; i++) {
885 if (desc->pic_a[i]) {
886 if (desc->pic_a[i]->bmp) free(desc->pic_a[i]->bmp);
887 if (desc->pic_a[i]->pal) free(desc->pic_a[i]->pal);
888 free (desc->pic_a[i]);
890 if (desc->pic_b[i]) {
891 if (desc->pic_b[i]->bmp) free(desc->pic_b[i]->bmp);
892 if (desc->pic_b[i]->pal) free(desc->pic_b[i]->pal);
893 free (desc->pic_b[i]);
897 if (desc->tables.g) free(desc->tables.g);
898 if (desc->tables.gt2) free(desc->tables.gt2);
899 if (desc->tables.om) free(desc->tables.om);
900 if (desc->tables.omt) free(desc->tables.omt);
901 if (desc->tables.tmp) free(desc->tables.tmp);
903 for(i = 0; i < desc->face_cnt; i++) {
904 FT_Done_Face(desc->faces[i]);
907 free(desc);
910 static int load_sub_face(const char *name, int face_index, FT_Face *face)
912 int err = -1;
914 if (name) err = FT_New_Face(library, name, face_index, face);
916 if (err) {
917 char *font_file = get_path("subfont.ttf");
918 err = FT_New_Face(library, font_file, 0, face);
919 free(font_file);
920 if (err) {
921 err = FT_New_Face(library, MPLAYER_DATADIR "/subfont.ttf", 0, face);
922 if (err) {
923 mp_msg(MSGT_OSD, MSGL_ERR, MSGTR_LIBVO_FONT_LOAD_FT_NewFaceFailed);
924 return -1;
928 return err;
931 static int load_osd_face(FT_Face *face)
933 if ( FT_New_Memory_Face(library, osd_font_pfb, sizeof(osd_font_pfb), 0, face) ) {
934 mp_msg(MSGT_OSD, MSGL_ERR, MSGTR_LIBVO_FONT_LOAD_FT_NewMemoryFaceFailed);
935 return -1;
937 return 0;
940 int kerning(font_desc_t *desc, int prevc, int c)
942 FT_Vector kern;
944 if (!desc->dynamic) return 0;
945 if (prevc < 0 || c < 0) return 0;
946 if (desc->font[prevc] != desc->font[c]) return 0;
947 if (desc->font[prevc] == -1 || desc->font[c] == -1) return 0;
948 FT_Get_Kerning(desc->faces[desc->font[c]],
949 desc->glyph_index[prevc], desc->glyph_index[c],
950 ft_kerning_default, &kern);
952 // fprintf(stderr, "kern: %c %c %d\n", prevc, c, f266ToInt(kern.x));
954 return f266ToInt(kern.x);
957 font_desc_t* read_font_desc_ft(const char *fname, int face_index, int movie_width, int movie_height, float font_scale_factor)
959 font_desc_t *desc = NULL;
961 FT_Face face;
963 FT_ULong *my_charset = malloc(MAX_CHARSET_SIZE * sizeof(FT_ULong)); /* characters we want to render; Unicode */
964 FT_ULong *my_charcodes = malloc(MAX_CHARSET_SIZE * sizeof(FT_ULong)); /* character codes in 'encoding' */
966 char *charmap = "ucs-4";
967 int err;
968 int charset_size;
969 int i, j;
970 int unicode;
972 float movie_size;
974 float subtitle_font_ppem;
975 float osd_font_ppem;
977 if (my_charset == NULL || my_charcodes == NULL) {
978 mp_msg(MSGT_OSD, MSGL_ERR, "subtitle font: malloc failed.\n");
979 goto err_out;
982 switch (subtitle_autoscale) {
983 case 1:
984 movie_size = movie_height;
985 break;
986 case 2:
987 movie_size = movie_width;
988 break;
989 case 3:
990 movie_size = sqrt(movie_height*movie_height+movie_width*movie_width);
991 break;
992 default:
993 movie_size = 100;
994 break;
997 subtitle_font_ppem = movie_size*font_scale_factor/100.0;
998 osd_font_ppem = movie_size*(font_scale_factor+1)/100.0;
1000 if (subtitle_font_ppem < 5) subtitle_font_ppem = 5;
1001 if (osd_font_ppem < 5) osd_font_ppem = 5;
1003 if (subtitle_font_ppem > 128) subtitle_font_ppem = 128;
1004 if (osd_font_ppem > 128) osd_font_ppem = 128;
1006 if ((subtitle_font_encoding == NULL)
1007 || (strcasecmp(subtitle_font_encoding, "unicode") == 0)) {
1008 unicode = 1;
1009 } else {
1010 unicode = 0;
1013 desc = init_font_desc();
1014 if(!desc) goto err_out;
1016 // t=GetTimer();
1018 /* generate the subtitle font */
1019 err = load_sub_face(fname, face_index, &face);
1020 if (err) {
1021 mp_msg(MSGT_OSD, MSGL_WARN, MSGTR_LIBVO_FONT_LOAD_FT_SubFaceFailed);
1022 goto gen_osd;
1024 desc->face_cnt++;
1026 #ifdef CONFIG_ICONV
1027 if (unicode) {
1028 charset_size = prepare_charset_unicode(face, my_charset, my_charcodes);
1029 } else {
1030 if (subtitle_font_encoding) {
1031 charset_size = prepare_charset(charmap, subtitle_font_encoding, my_charset, my_charcodes);
1032 } else {
1033 charset_size = prepare_charset(charmap, "iso-8859-1", my_charset, my_charcodes);
1037 if (charset_size < 0) {
1038 mp_msg(MSGT_OSD, MSGL_ERR, MSGTR_LIBVO_FONT_LOAD_FT_SubFontCharsetFailed);
1039 goto err_out;
1041 #else
1042 goto err_out;
1043 #endif
1045 // fprintf(stderr, "fg: prepare t = %lf\n", GetTimer()-t);
1047 err = prepare_font(desc, face, subtitle_font_ppem, desc->face_cnt-1,
1048 charset_size, my_charset, my_charcodes, unicode,
1049 subtitle_font_thickness, subtitle_font_radius);
1051 if (err) {
1052 mp_msg(MSGT_OSD, MSGL_ERR, MSGTR_LIBVO_FONT_LOAD_FT_CannotPrepareSubtitleFont);
1053 goto err_out;
1056 gen_osd:
1058 /* generate the OSD font */
1059 err = load_osd_face(&face);
1060 if (err) {
1061 goto err_out;
1063 desc->face_cnt++;
1065 err = prepare_font(desc, face, osd_font_ppem, desc->face_cnt-1,
1066 OSD_CHARSET_SIZE, osd_charset, osd_charcodes, 0,
1067 subtitle_font_thickness, subtitle_font_radius);
1069 if (err) {
1070 mp_msg(MSGT_OSD, MSGL_ERR, MSGTR_LIBVO_FONT_LOAD_FT_CannotPrepareOSDFont);
1071 goto err_out;
1074 err = generate_tables(desc, subtitle_font_thickness, subtitle_font_radius);
1076 if (err) {
1077 mp_msg(MSGT_OSD, MSGL_ERR, MSGTR_LIBVO_FONT_LOAD_FT_CannotGenerateTables);
1078 goto err_out;
1081 // final cleanup
1082 desc->font[' ']=-1;
1083 desc->width[' ']=desc->spacewidth;
1085 j = '_';
1086 if (desc->font[j] < 0) j = '?';
1087 if (desc->font[j] < 0) j = ' ';
1088 render_one_glyph(desc, j);
1089 for(i = 0; i < 65536; i++) {
1090 if (desc->font[i] < 0 && i != ' ') {
1091 desc->start[i] = desc->start[j];
1092 desc->width[i] = desc->width[j];
1093 desc->font[i] = desc->font[j];
1096 free(my_charset);
1097 free(my_charcodes);
1098 return desc;
1100 err_out:
1101 if (desc)
1102 free_font_desc(desc);
1103 free(my_charset);
1104 free(my_charcodes);
1105 return NULL;
1108 int init_freetype(void)
1110 int err;
1112 /* initialize freetype */
1113 err = FT_Init_FreeType(&library);
1114 if (err) {
1115 mp_msg(MSGT_OSD, MSGL_ERR, "Init_FreeType failed.\n");
1116 return -1;
1118 mp_msg(MSGT_OSD, MSGL_V, "init_freetype\n");
1119 using_freetype = 1;
1120 return 0;
1123 int done_freetype(void)
1125 int err;
1127 if (!using_freetype)
1128 return 0;
1130 err = FT_Done_FreeType(library);
1131 if (err) {
1132 mp_msg(MSGT_OSD, MSGL_ERR, MSGTR_LIBVO_FONT_LOAD_FT_DoneFreeTypeFailed);
1133 return -1;
1136 return 0;
1139 void load_font_ft(int width, int height, font_desc_t** fontp, const char *font_name, float font_scale_factor)
1141 #ifdef CONFIG_FONTCONFIG
1142 FcPattern *fc_pattern;
1143 FcPattern *fc_pattern2;
1144 FcChar8 *s;
1145 int face_index;
1146 FcBool scalable;
1147 #endif
1148 font_desc_t *vo_font = *fontp;
1149 vo_image_width = width;
1150 vo_image_height = height;
1152 // protection against vo_aa font hacks
1153 if (vo_font && !vo_font->dynamic) return;
1155 if (vo_font) free_font_desc(vo_font);
1157 #ifdef CONFIG_FONTCONFIG
1158 if (font_fontconfig > 0)
1160 if (!font_name)
1161 font_name = strdup("sans-serif");
1162 FcInit();
1163 fc_pattern = FcNameParse(font_name);
1164 FcConfigSubstitute(0, fc_pattern, FcMatchPattern);
1165 FcDefaultSubstitute(fc_pattern);
1166 fc_pattern2 = fc_pattern;
1167 fc_pattern = FcFontMatch(0, fc_pattern, 0);
1168 FcPatternDestroy(fc_pattern2);
1169 FcPatternGetBool(fc_pattern, FC_SCALABLE, 0, &scalable);
1170 if (scalable != FcTrue) {
1171 FcPatternDestroy(fc_pattern);
1172 fc_pattern = FcNameParse("sans-serif");
1173 FcConfigSubstitute(0, fc_pattern, FcMatchPattern);
1174 FcDefaultSubstitute(fc_pattern);
1175 fc_pattern2 = fc_pattern;
1176 fc_pattern = FcFontMatch(0, fc_pattern, 0);
1177 FcPatternDestroy(fc_pattern2);
1179 // s doesn't need to be freed according to fontconfig docs
1180 FcPatternGetString(fc_pattern, FC_FILE, 0, &s);
1181 FcPatternGetInteger(fc_pattern, FC_INDEX, 0, &face_index);
1182 *fontp=read_font_desc_ft(s, face_index, width, height, font_scale_factor);
1183 FcPatternDestroy(fc_pattern);
1185 else
1186 #endif
1187 *fontp=read_font_desc_ft(font_name, 0, width, height, font_scale_factor);