cleanup: move MP_NOPTS_VALUE definition to mpcommon.h
[mplayer/glamo.git] / libvo / font_load_ft.c
blob41a0f886cba7fd8d16b792ce87d112a50c6de2a8
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 "mplayer.h"
50 #include "path.h"
51 #include "osd_font.h"
53 #if (FREETYPE_MAJOR > 2) || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 1)
54 #define HAVE_FREETYPE21
55 #endif
57 char *subtitle_font_encoding = NULL;
58 float text_font_scale_factor = 3.5;
59 float osd_font_scale_factor = 4.0;
60 float subtitle_font_radius = 2.0;
61 float subtitle_font_thickness = 2.0;
62 // 0 = no autoscale
63 // 1 = video height
64 // 2 = video width
65 // 3 = diagonal
66 int subtitle_autoscale = 3;
68 int vo_image_width = 0;
69 int vo_image_height = 0;
70 int force_load_font;
72 int using_freetype = 0;
73 #ifdef CONFIG_FONTCONFIG
74 int font_fontconfig = 1;
75 #else
76 int font_fontconfig = -1;
77 #endif
79 //// constants
80 static unsigned int const colors = 256;
81 static unsigned int const maxcolor = 255;
82 static unsigned const base = 256;
83 static unsigned const first_char = 33;
84 #define MAX_CHARSET_SIZE 60000
86 static FT_Library library;
88 #define OSD_CHARSET_SIZE 15
90 static const FT_ULong osd_charset[OSD_CHARSET_SIZE] =
92 0xe001, 0xe002, 0xe003, 0xe004, 0xe005, 0xe006, 0xe007, 0xe008,
93 0xe009, 0xe00a, 0xe00b, 0xe010, 0xe011, 0xe012, 0xe013
96 static const FT_ULong osd_charcodes[OSD_CHARSET_SIZE] =
98 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
99 0x09,0x0a,0x0b,0x10,0x11,0x12,0x13
102 #define f266ToInt(x) (((x)+32)>>6) // round fractional fixed point number to integer
103 // coordinates are in 26.6 pixels (i.e. 1/64th of pixels)
104 #define f266CeilToInt(x) (((x)+63)>>6) // ceiling
105 #define f266FloorToInt(x) ((x)>>6) // floor
106 #define f1616ToInt(x) (((x)+0x8000)>>16) // 16.16
107 #define floatTof266(x) ((int)((x)*(1<<6)+0.5))
109 #define ALIGN(x) (((x)+7)&~7) // 8 byte align
111 #define WARNING(msg, args...) mp_msg(MSGT_OSD, MSGL_WARN, msg "\n", ## args)
113 #define DEBUG 0
115 //static double ttime;
118 static void paste_bitmap(unsigned char *bbuffer, FT_Bitmap *bitmap, int x, int y, int width, int height, int bwidth) {
119 int drow = x+y*width;
120 int srow = 0;
121 int sp, dp, w, h;
122 if (bitmap->pixel_mode==ft_pixel_mode_mono)
123 for (h = bitmap->rows; h>0 && height > 0; --h, height--, drow+=width, srow+=bitmap->pitch)
124 for (w = bwidth, sp=dp=0; w>0; --w, ++dp, ++sp)
125 bbuffer[drow+dp] = (bitmap->buffer[srow+sp/8] & (0x80>>(sp%8))) ? 255:0;
126 else
127 for (h = bitmap->rows; h>0 && height > 0; --h, height--, drow+=width, srow+=bitmap->pitch)
128 for (w = bwidth, sp=dp=0; w>0; --w, ++dp, ++sp)
129 bbuffer[drow+dp] = bitmap->buffer[srow+sp];
133 static int check_font(font_desc_t *desc, float ppem, int padding, int pic_idx,
134 int charset_size, FT_ULong *charset, FT_ULong *charcodes,
135 int unicode) {
136 FT_Error error;
137 FT_Face face = desc->faces[pic_idx];
138 int const load_flags = FT_LOAD_DEFAULT;
139 int ymin = INT_MAX, ymax = INT_MIN;
140 int space_advance = 20;
141 int width, height;
142 unsigned char *bbuffer;
143 int i, uni_charmap = 1;
145 error = FT_Select_Charmap(face, ft_encoding_unicode);
146 // fprintf(stderr, "select unicode charmap: %d\n", error);
148 if (face->charmap==NULL || face->charmap->encoding!=ft_encoding_unicode) {
149 WARNING("Unicode charmap not available for this font. Very bad!");
150 uni_charmap = 0;
151 error = FT_Set_Charmap(face, face->charmaps[0]);
152 if (error) WARNING("No charmaps! Strange.");
155 /* set size */
156 if (FT_IS_SCALABLE(face)) {
157 error = FT_Set_Char_Size(face, 0, floatTof266(ppem), 0, 0);
158 if (error) WARNING("FT_Set_Char_Size failed.");
159 } else {
160 int j = 0;
161 int jppem = face->available_sizes[0].height;
162 /* find closest size */
163 for (i = 0; i<face->num_fixed_sizes; ++i) {
164 if (fabs(face->available_sizes[i].height - ppem) < abs(face->available_sizes[i].height - jppem)) {
165 j = i;
166 jppem = face->available_sizes[i].height;
169 WARNING("Selected font is not scalable. Using ppem=%i.", face->available_sizes[j].height);
170 error = FT_Set_Pixel_Sizes(face, face->available_sizes[j].width, face->available_sizes[j].height);
171 if (error) WARNING("FT_Set_Pixel_Sizes failed.");
174 if (FT_IS_FIXED_WIDTH(face))
175 WARNING("Selected font is fixed-width.");
177 /* compute space advance */
178 error = FT_Load_Char(face, ' ', load_flags);
179 if (error) WARNING("spacewidth set to default.");
180 else space_advance = f266ToInt(face->glyph->advance.x);
182 if (!desc->spacewidth) desc->spacewidth = 2*padding + space_advance;
183 if (!desc->charspace) desc->charspace = -2*padding;
184 if (!desc->height) desc->height = f266ToInt(face->size->metrics.height);
187 for (i= 0; i<charset_size; ++i) {
188 FT_ULong character, code;
189 FT_UInt glyph_index;
191 character = charset[i];
192 code = charcodes[i];
193 desc->font[unicode?character:code] = pic_idx;
194 // get glyph index
195 if (character==0)
196 glyph_index = 0;
197 else {
198 glyph_index = FT_Get_Char_Index(face, uni_charmap ? character:code);
199 if (glyph_index==0) {
200 WARNING("Glyph for char 0x%02lx|U+%04lX|%c not found.", code, character,
201 code<' '||code>255 ? '.':(char)code);
202 desc->font[unicode?character:code] = -1;
203 continue;
206 desc->glyph_index[unicode?character:code] = glyph_index;
208 // fprintf(stderr, "font height: %f\n", (double)(face->bbox.yMax-face->bbox.yMin)/(double)face->units_per_EM*ppem);
209 // fprintf(stderr, "font width: %f\n", (double)(face->bbox.xMax-face->bbox.xMin)/(double)face->units_per_EM*ppem);
211 ymax = (double)(face->bbox.yMax)/(double)face->units_per_EM*ppem+1;
212 ymin = (double)(face->bbox.yMin)/(double)face->units_per_EM*ppem-1;
214 width = ppem*(face->bbox.xMax-face->bbox.xMin)/face->units_per_EM+3+2*padding;
215 if (desc->max_width < width) desc->max_width = width;
216 width = ALIGN(width);
217 desc->pic_b[pic_idx]->charwidth = width;
219 if (width <= 0) {
220 mp_msg(MSGT_OSD, MSGL_ERR, "Wrong bounding box, width <= 0 !\n");
221 return -1;
224 if (ymax<=ymin) {
225 mp_msg(MSGT_OSD, MSGL_ERR, "Something went wrong. Use the source!\n");
226 return -1;
229 height = ymax - ymin + 2*padding;
230 if (height <= 0) {
231 mp_msg(MSGT_OSD, MSGL_ERR, "Wrong bounding box, height <= 0 !\n");
232 return -1;
235 if (desc->max_height < height) desc->max_height = height;
236 desc->pic_b[pic_idx]->charheight = height;
238 // fprintf(stderr, "font height2: %d\n", height);
239 desc->pic_b[pic_idx]->baseline = ymax + padding;
240 desc->pic_b[pic_idx]->padding = padding;
241 desc->pic_b[pic_idx]->current_alloc = 0;
242 desc->pic_b[pic_idx]->current_count = 0;
244 bbuffer = NULL;
246 desc->pic_b[pic_idx]->w = width;
247 desc->pic_b[pic_idx]->h = height;
248 desc->pic_b[pic_idx]->c = colors;
249 desc->pic_b[pic_idx]->bmp = bbuffer;
250 desc->pic_b[pic_idx]->pen = 0;
251 return 0;
254 // general outline
255 static void outline(
256 unsigned char *s,
257 unsigned char *t,
258 int width,
259 int height,
260 int stride,
261 unsigned char *m,
262 int r,
263 int mwidth,
264 int msize) {
266 int x, y;
268 for (y = 0; y<height; y++) {
269 for (x = 0; x<width; x++) {
270 const int src= s[x];
271 if(src==0) continue;
273 const int x1=(x<r) ? r-x : 0;
274 const int y1=(y<r) ? r-y : 0;
275 const int x2=(x+r>=width ) ? r+width -x : 2*r+1;
276 const int y2=(y+r>=height) ? r+height-y : 2*r+1;
277 register unsigned char *dstp= t + (y1+y-r)* stride + x-r;
278 //register int *mp = m + y1 *mwidth;
279 register unsigned char *mp= m + msize*src + y1*mwidth;
280 int my;
282 for(my= y1; my<y2; my++){
283 register int mx;
284 for(mx= x1; mx<x2; mx++){
285 if(dstp[mx] < mp[mx]) dstp[mx]= mp[mx];
287 dstp+=stride;
288 mp+=mwidth;
292 s+= stride;
297 // 1 pixel outline
298 static void outline1(
299 unsigned char *s,
300 unsigned char *t,
301 int width,
302 int height,
303 int stride) {
305 int x, y;
306 int skip = stride-width;
308 for (x = 0; x<width; ++x, ++s, ++t) *t = *s;
309 s += skip;
310 t += skip;
311 for (y = 1; y<height-1; ++y) {
312 *t++ = *s++;
313 for (x = 1; x<width-1; ++x, ++s, ++t) {
314 unsigned v = (
315 s[-1-stride]+
316 s[-1+stride]+
317 s[+1-stride]+
318 s[+1+stride]
319 )/2 + (
320 s[-1]+
321 s[+1]+
322 s[-stride]+
323 s[+stride]+
324 s[0]
326 *t = v>maxcolor ? maxcolor : v;
328 *t++ = *s++;
329 s += skip;
330 t += skip;
332 for (x = 0; x<width; ++x, ++s, ++t) *t = *s;
335 // "0 pixel outline"
336 static void outline0(
337 unsigned char *s,
338 unsigned char *t,
339 int width,
340 int height,
341 int stride) {
342 int y;
343 for (y = 0; y<height; ++y) {
344 memcpy(t, s, width);
345 s += stride;
346 t += stride;
350 // gaussian blur
351 void blur(
352 unsigned char *buffer,
353 unsigned short *tmp2,
354 int width,
355 int height,
356 int stride,
357 int *m2,
358 int r,
359 int mwidth) {
361 int x, y;
363 unsigned char *s = buffer;
364 unsigned short *t = tmp2+1;
365 for(y=0; y<height; y++){
366 memset(t-1, 0, (width+1)*sizeof(short));
368 for(x=0; x<r; x++){
369 const int src= s[x];
370 if(src){
371 register unsigned short *dstp= t + x-r;
372 int mx;
373 unsigned *m3= m2 + src*mwidth;
374 for(mx=r-x; mx<mwidth; mx++){
375 dstp[mx]+= m3[mx];
380 for(; x<width-r; x++){
381 const int src= s[x];
382 if(src){
383 register unsigned short *dstp= t + x-r;
384 int mx;
385 unsigned *m3= m2 + src*mwidth;
386 for(mx=0; mx<mwidth; mx++){
387 dstp[mx]+= m3[mx];
392 for(; x<width; x++){
393 const int src= s[x];
394 if(src){
395 register unsigned short *dstp= t + x-r;
396 int mx;
397 const int x2= r+width -x;
398 unsigned *m3= m2 + src*mwidth;
399 for(mx=0; mx<x2; mx++){
400 dstp[mx]+= m3[mx];
405 s+= stride;
406 t+= width + 1;
409 t = tmp2;
410 for(x=0; x<width; x++){
411 for(y=0; y<r; y++){
412 unsigned short *srcp= t + y*(width+1) + 1;
413 int src= *srcp;
414 if(src){
415 register unsigned short *dstp= srcp - 1 + width+1;
416 const int src2= (src + 128)>>8;
417 unsigned *m3= m2 + src2*mwidth;
419 int mx;
420 *srcp= 128;
421 for(mx=r-1; mx<mwidth; mx++){
422 *dstp += m3[mx];
423 dstp+= width+1;
427 for(; y<height-r; y++){
428 unsigned short *srcp= t + y*(width+1) + 1;
429 int src= *srcp;
430 if(src){
431 register unsigned short *dstp= srcp - 1 - r*(width+1);
432 const int src2= (src + 128)>>8;
433 unsigned *m3= m2 + src2*mwidth;
435 int mx;
436 *srcp= 128;
437 for(mx=0; mx<mwidth; mx++){
438 *dstp += m3[mx];
439 dstp+= width+1;
443 for(; y<height; y++){
444 unsigned short *srcp= t + y*(width+1) + 1;
445 int src= *srcp;
446 if(src){
447 const int y2=r+height-y;
448 register unsigned short *dstp= srcp - 1 - r*(width+1);
449 const int src2= (src + 128)>>8;
450 unsigned *m3= m2 + src2*mwidth;
452 int mx;
453 *srcp= 128;
454 for(mx=0; mx<y2; mx++){
455 *dstp += m3[mx];
456 dstp+= width+1;
460 t++;
463 t = tmp2;
464 s = buffer;
465 for(y=0; y<height; y++){
466 for(x=0; x<width; x++){
467 s[x]= t[x]>>8;
469 s+= stride;
470 t+= width + 1;
474 static void resample_alpha(unsigned char *abuf, unsigned char *bbuf, int width, int height, int stride, float factor)
476 int f=factor*256.0f;
477 int i,j;
478 for (i = 0; i < height; i++) {
479 unsigned char *a = abuf+i*stride;
480 unsigned char *b = bbuf+i*stride;
481 for(j=0;j<width;j++,a++,b++){
482 int x=*a; // alpha
483 int y=*b; // bitmap
484 x=255-((x*f)>>8); // scale
485 if (x+y>255) x=255-y; // to avoid overflows
486 if (x<1) x=1; else if (x>=252) x=0;
487 *a=x;
492 #define ALLOC_INCR 32
493 void render_one_glyph(font_desc_t *desc, int c)
495 FT_GlyphSlot slot;
496 FT_UInt glyph_index;
497 FT_BitmapGlyph glyph;
498 int width, height, stride, maxw, off;
499 unsigned char *abuffer, *bbuffer;
501 int const load_flags = FT_LOAD_DEFAULT;
502 int pen_xa;
503 int font = desc->font[c];
504 int error;
506 // fprintf(stderr, "render_one_glyph %d\n", c);
508 if (!desc->dynamic) return;
509 if (desc->width[c] != -1) return;
510 if (desc->font[c] == -1) return;
512 glyph_index = desc->glyph_index[c];
514 // load glyph
515 error = FT_Load_Glyph(desc->faces[font], glyph_index, load_flags);
516 if (error) {
517 WARNING("FT_Load_Glyph 0x%02x (char 0x%04x) failed.", glyph_index, c);
518 desc->font[c] = -1;
519 return;
521 slot = desc->faces[font]->glyph;
523 // render glyph
524 if (slot->format != ft_glyph_format_bitmap) {
525 error = FT_Render_Glyph(slot, ft_render_mode_normal);
526 if (error) {
527 WARNING("FT_Render_Glyph 0x%04x (char 0x%04x) failed.", glyph_index, c);
528 desc->font[c] = -1;
529 return;
533 // extract glyph image
534 error = FT_Get_Glyph(slot, (FT_Glyph*)&glyph);
535 if (error) {
536 WARNING("FT_Get_Glyph 0x%04x (char 0x%04x) failed.", glyph_index, c);
537 desc->font[c] = -1;
538 return;
541 // fprintf(stderr, "glyph generated\n");
543 maxw = desc->pic_b[font]->charwidth;
545 if (glyph->bitmap.width > maxw) {
546 fprintf(stderr, "glyph too wide!\n");
549 // allocate new memory, if needed
550 // fprintf(stderr, "\n%d %d %d\n", desc->pic_b[font]->charwidth, desc->pic_b[font]->charheight, desc->pic_b[font]->current_alloc);
551 if (desc->pic_b[font]->current_count >= desc->pic_b[font]->current_alloc) {
552 int newsize = desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight*(desc->pic_b[font]->current_alloc+ALLOC_INCR);
553 int increment = desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight*ALLOC_INCR;
554 desc->pic_b[font]->current_alloc += ALLOC_INCR;
556 // fprintf(stderr, "\nns = %d inc = %d\n", newsize, increment);
558 desc->pic_b[font]->bmp = realloc(desc->pic_b[font]->bmp, newsize);
559 desc->pic_a[font]->bmp = realloc(desc->pic_a[font]->bmp, newsize);
561 off = desc->pic_b[font]->current_count*desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight;
562 memset(desc->pic_b[font]->bmp+off, 0, increment);
563 memset(desc->pic_a[font]->bmp+off, 0, increment);
566 abuffer = desc->pic_a[font]->bmp;
567 bbuffer = desc->pic_b[font]->bmp;
569 off = desc->pic_b[font]->current_count*desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight;
571 paste_bitmap(bbuffer+off,
572 &glyph->bitmap,
573 desc->pic_b[font]->padding + glyph->left,
574 desc->pic_b[font]->baseline - glyph->top,
575 desc->pic_b[font]->charwidth, desc->pic_b[font]->charheight,
576 glyph->bitmap.width <= maxw ? glyph->bitmap.width : maxw);
578 // fprintf(stderr, "glyph pasted\n");
579 FT_Done_Glyph((FT_Glyph)glyph);
581 /* advance pen */
582 pen_xa = f266ToInt(slot->advance.x) + 2*desc->pic_b[font]->padding;
583 if (pen_xa > maxw) pen_xa = maxw;
585 desc->start[c] = off;
586 width = desc->width[c] = pen_xa;
587 height = desc->pic_b[font]->charheight;
588 stride = desc->pic_b[font]->w;
590 if (desc->tables.o_r == 0) {
591 outline0(bbuffer+off, abuffer+off, width, height, stride);
592 } else if (desc->tables.o_r == 1) {
593 outline1(bbuffer+off, abuffer+off, width, height, stride);
594 } else {
595 outline(bbuffer+off, abuffer+off, width, height, stride,
596 desc->tables.omt, desc->tables.o_r, desc->tables.o_w,
597 desc->tables.o_size);
599 // fprintf(stderr, "fg: outline t = %f\n", GetTimer()-t);
601 if (desc->tables.g_r) {
602 blur(abuffer+off, desc->tables.tmp, width, height, stride,
603 desc->tables.gt2, desc->tables.g_r,
604 desc->tables.g_w);
605 // fprintf(stderr, "fg: blur t = %f\n", GetTimer()-t);
608 resample_alpha(abuffer+off, bbuffer+off, width, height, stride, font_factor);
610 desc->pic_b[font]->current_count++;
614 static int prepare_font(font_desc_t *desc, FT_Face face, float ppem, int pic_idx,
615 int charset_size, FT_ULong *charset, FT_ULong *charcodes, int unicode,
616 double thickness, double radius)
618 int i, err;
619 int padding = ceil(radius) + ceil(thickness);
621 desc->faces[pic_idx] = face;
623 desc->pic_a[pic_idx] = malloc(sizeof(raw_file));
624 if (!desc->pic_a[pic_idx]) return -1;
625 desc->pic_b[pic_idx] = malloc(sizeof(raw_file));
626 if (!desc->pic_b[pic_idx]) return -1;
628 desc->pic_a[pic_idx]->bmp = NULL;
629 desc->pic_a[pic_idx]->pal = NULL;
630 desc->pic_b[pic_idx]->bmp = NULL;
631 desc->pic_b[pic_idx]->pal = NULL;
633 desc->pic_a[pic_idx]->pal = malloc(sizeof(unsigned char)*256*3);
634 if (!desc->pic_a[pic_idx]->pal) return -1;
635 for (i = 0; i<768; ++i) desc->pic_a[pic_idx]->pal[i] = i/3;
637 desc->pic_b[pic_idx]->pal = malloc(sizeof(unsigned char)*256*3);
638 if (!desc->pic_b[pic_idx]->pal) return -1;
639 for (i = 0; i<768; ++i) desc->pic_b[pic_idx]->pal[i] = i/3;
641 // ttime = GetTimer();
642 err = check_font(desc, ppem, padding, pic_idx, charset_size, charset, charcodes, unicode);
643 // ttime=GetTimer()-ttime;
644 // printf("render: %7f us\n",ttime);
645 if (err) return -1;
646 // fprintf(stderr, "fg: render t = %f\n", GetTimer()-t);
648 desc->pic_a[pic_idx]->w = desc->pic_b[pic_idx]->w;
649 desc->pic_a[pic_idx]->h = desc->pic_b[pic_idx]->h;
650 desc->pic_a[pic_idx]->c = colors;
652 desc->pic_a[pic_idx]->bmp = NULL;
654 // fprintf(stderr, "fg: w = %d, h = %d\n", desc->pic_a[pic_idx]->w, desc->pic_a[pic_idx]->h);
655 return 0;
659 static int generate_tables(font_desc_t *desc, double thickness, double radius)
661 int width = desc->max_height;
662 int height = desc->max_width;
664 double A = log(1.0/base)/(radius*radius*2);
665 int mx, my, i;
666 double volume_diff, volume_factor = 0;
667 unsigned char *omtp;
669 desc->tables.g_r = ceil(radius);
670 desc->tables.o_r = ceil(thickness);
671 desc->tables.g_w = 2*desc->tables.g_r+1;
672 desc->tables.o_w = 2*desc->tables.o_r+1;
673 desc->tables.o_size = desc->tables.o_w * desc->tables.o_w;
675 // fprintf(stderr, "o_r = %d\n", desc->tables.o_r);
677 if (desc->tables.g_r) {
678 desc->tables.g = malloc(desc->tables.g_w * sizeof(unsigned));
679 desc->tables.gt2 = malloc(256 * desc->tables.g_w * sizeof(unsigned));
680 if (desc->tables.g==NULL || desc->tables.gt2==NULL) {
681 return -1;
684 desc->tables.om = malloc(desc->tables.o_w*desc->tables.o_w * sizeof(unsigned));
685 desc->tables.omt = malloc(desc->tables.o_size*256);
687 omtp = desc->tables.omt;
688 desc->tables.tmp = malloc((width+1)*height*sizeof(short));
690 if (desc->tables.om==NULL || desc->tables.omt==NULL || desc->tables.tmp==NULL) {
691 return -1;
694 if (desc->tables.g_r) {
695 // gaussian curve with volume = 256
696 for (volume_diff=10000000; volume_diff>0.0000001; volume_diff*=0.5){
697 volume_factor+= volume_diff;
698 desc->tables.volume=0;
699 for (i = 0; i<desc->tables.g_w; ++i) {
700 desc->tables.g[i] = (unsigned)(exp(A * (i-desc->tables.g_r)*(i-desc->tables.g_r)) * volume_factor + .5);
701 desc->tables.volume+= desc->tables.g[i];
703 if(desc->tables.volume>256) volume_factor-= volume_diff;
705 desc->tables.volume=0;
706 for (i = 0; i<desc->tables.g_w; ++i) {
707 desc->tables.g[i] = (unsigned)(exp(A * (i-desc->tables.g_r)*(i-desc->tables.g_r)) * volume_factor + .5);
708 desc->tables.volume+= desc->tables.g[i];
711 // gauss table:
712 for(mx=0;mx<desc->tables.g_w;mx++){
713 for(i=0;i<256;i++){
714 desc->tables.gt2[mx+i*desc->tables.g_w] = i*desc->tables.g[mx];
719 /* outline matrix */
720 for (my = 0; my<desc->tables.o_w; ++my) {
721 for (mx = 0; mx<desc->tables.o_w; ++mx) {
722 // antialiased circle would be perfect here, but this one is good enough
723 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));
724 desc->tables.om[mx+my*desc->tables.o_w] = d>=1 ? base : d<=0 ? 0 : (d*base + .5);
728 // outline table:
729 for(i=0;i<256;i++){
730 for(mx=0;mx<desc->tables.o_size;mx++) *(omtp++) = (i*desc->tables.om[mx] + (base/2))/base;
733 return 0;
736 #ifdef CONFIG_ICONV
737 /* decode from 'encoding' to unicode */
738 static FT_ULong decode_char(iconv_t *cd, char c) {
739 FT_ULong o;
740 char *inbuf = &c;
741 char *outbuf = (char*)&o;
742 size_t inbytesleft = 1;
743 size_t outbytesleft = sizeof(FT_ULong);
745 iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
747 /* convert unicode BigEndian -> MachineEndian */
748 o = be2me_32(o);
750 // if (count==-1) o = 0; // not OK, at least my iconv() returns E2BIG for all
751 if (outbytesleft!=0) o = 0;
753 /* we don't want control characters */
754 if (o>=0x7f && o<0xa0) o = 0;
755 return o;
758 static int prepare_charset(char *charmap, char *encoding, FT_ULong *charset, FT_ULong *charcodes) {
759 FT_ULong i;
760 int count = 0;
761 int charset_size;
762 iconv_t cd;
764 // check if ucs-4 is available
765 cd = iconv_open(charmap, charmap);
766 if (cd==(iconv_t)-1) {
767 mp_msg(MSGT_OSD, MSGL_ERR, "iconv doesn't know %s encoding. Use the source!\n", charmap);
768 return -1;
771 iconv_close(cd);
773 cd = iconv_open(charmap, encoding);
774 if (cd==(iconv_t)-1) {
775 mp_msg(MSGT_OSD, MSGL_ERR, "Unsupported encoding `%s', use iconv --list to list character sets known on your system.\n", encoding);
776 return -1;
779 charset_size = 256 - first_char;
780 for (i = 0; i<charset_size; ++i) {
781 charcodes[count] = i+first_char;
782 charset[count] = decode_char(&cd, i+first_char);
783 if (charset[count]!=0) ++count;
785 charcodes[count] = charset[count] = 0; ++count;
786 charset_size = count;
788 iconv_close(cd);
789 if (charset_size==0) {
790 mp_msg(MSGT_OSD, MSGL_ERR, "No characters to render!\n");
791 return -1;
794 return charset_size;
797 static int prepare_charset_unicode(FT_Face face, FT_ULong *charset, FT_ULong *charcodes) {
798 #ifdef HAVE_FREETYPE21
799 FT_ULong charcode;
800 #else
801 int j;
802 #endif
803 FT_UInt gindex;
804 int i;
806 if (face->charmap==NULL || face->charmap->encoding!=ft_encoding_unicode) {
807 WARNING("Unicode charmap not available for this font. Very bad!");
808 return -1;
810 #ifdef HAVE_FREETYPE21
811 i = 0;
812 charcode = FT_Get_First_Char( face, &gindex );
813 while (gindex != 0) {
814 if (charcode < 65536 && charcode >= 33) { // sanity check
815 charset[i] = charcode;
816 charcodes[i] = 0;
817 i++;
819 charcode = FT_Get_Next_Char( face, charcode, &gindex );
821 #else
822 // for FT < 2.1 we have to use brute force enumeration
823 i = 0;
824 for (j = 33; j < 65536; j++) {
825 gindex = FT_Get_Char_Index(face, j);
826 if (gindex > 0) {
827 charset[i] = j;
828 charcodes[i] = 0;
829 i++;
832 #endif
833 mp_msg(MSGT_OSD, MSGL_V, "Unicode font: %d glyphs.\n", i);
835 return i;
837 #endif
839 static font_desc_t* init_font_desc(void)
841 font_desc_t *desc;
843 desc = calloc(1, sizeof(*desc));
844 if(!desc) return NULL;
846 desc->dynamic = 1;
848 /* setup sane defaults */
849 desc->freetype = 1;
851 memset(desc->start, 0xff, sizeof(desc->start));
852 memset(desc->width, 0xff, sizeof(desc->width));
853 memset(desc->font, 0xff, sizeof(desc->font));
855 return desc;
858 void free_font_desc(font_desc_t *desc)
860 int i;
862 if (!desc) return;
864 // if (!desc->dynamic) return; // some vo_aa crap, better leaking than crashing
866 free(desc->name);
867 free(desc->fpath);
869 for(i = 0; i < 16; i++) {
870 if (desc->pic_a[i]) {
871 free(desc->pic_a[i]->bmp);
872 free(desc->pic_a[i]->pal);
873 free(desc->pic_a[i]);
875 if (desc->pic_b[i]) {
876 free(desc->pic_b[i]->bmp);
877 free(desc->pic_b[i]->pal);
878 free(desc->pic_b[i]);
882 free(desc->tables.g);
883 free(desc->tables.gt2);
884 free(desc->tables.om);
885 free(desc->tables.omt);
886 free(desc->tables.tmp);
888 for(i = 0; i < desc->face_cnt; i++) {
889 FT_Done_Face(desc->faces[i]);
892 free(desc);
895 static int load_sub_face(const char *name, int face_index, FT_Face *face)
897 int err = -1;
899 if (name) err = FT_New_Face(library, name, face_index, face);
901 if (err) {
902 char *font_file = get_path("subfont.ttf");
903 err = FT_New_Face(library, font_file, 0, face);
904 free(font_file);
905 if (err) {
906 err = FT_New_Face(library, MPLAYER_DATADIR "/subfont.ttf", 0, face);
907 if (err) {
908 mp_tmsg(MSGT_OSD, MSGL_ERR, "New_Face failed. Maybe the font path is wrong.\nPlease supply the text font file (~/.mplayer/subfont.ttf).\n");
909 return -1;
913 return err;
916 static int load_osd_face(FT_Face *face)
918 if ( FT_New_Memory_Face(library, osd_font_pfb, sizeof(osd_font_pfb), 0, face) ) {
919 mp_tmsg(MSGT_OSD, MSGL_ERR, "New_Memory_Face failed..\n");
920 return -1;
922 return 0;
925 int kerning(font_desc_t *desc, int prevc, int c)
927 FT_Vector kern;
929 if (!desc->dynamic) return 0;
930 if (prevc < 0 || c < 0) return 0;
931 if (desc->font[prevc] != desc->font[c]) return 0;
932 if (desc->font[prevc] == -1 || desc->font[c] == -1) return 0;
933 FT_Get_Kerning(desc->faces[desc->font[c]],
934 desc->glyph_index[prevc], desc->glyph_index[c],
935 ft_kerning_default, &kern);
937 // fprintf(stderr, "kern: %c %c %d\n", prevc, c, f266ToInt(kern.x));
939 return f266ToInt(kern.x);
942 font_desc_t* read_font_desc_ft(const char *fname, int face_index, int movie_width, int movie_height, float font_scale_factor)
944 font_desc_t *desc = NULL;
946 FT_Face face;
948 FT_ULong *my_charset = malloc(MAX_CHARSET_SIZE * sizeof(FT_ULong)); /* characters we want to render; Unicode */
949 FT_ULong *my_charcodes = malloc(MAX_CHARSET_SIZE * sizeof(FT_ULong)); /* character codes in 'encoding' */
951 char *charmap = "ucs-4";
952 int err;
953 int charset_size;
954 int i, j;
955 int unicode;
957 float movie_size;
959 float subtitle_font_ppem;
960 float osd_font_ppem;
962 if (my_charset == NULL || my_charcodes == NULL) {
963 mp_msg(MSGT_OSD, MSGL_ERR, "subtitle font: malloc failed.\n");
964 goto err_out;
967 switch (subtitle_autoscale) {
968 case 1:
969 movie_size = movie_height;
970 break;
971 case 2:
972 movie_size = movie_width;
973 break;
974 case 3:
975 movie_size = sqrt(movie_height*movie_height+movie_width*movie_width);
976 break;
977 default:
978 movie_size = 100;
979 break;
982 subtitle_font_ppem = movie_size*font_scale_factor/100.0;
983 osd_font_ppem = movie_size*(font_scale_factor+1)/100.0;
985 if (subtitle_font_ppem < 5) subtitle_font_ppem = 5;
986 if (osd_font_ppem < 5) osd_font_ppem = 5;
988 if (subtitle_font_ppem > 128) subtitle_font_ppem = 128;
989 if (osd_font_ppem > 128) osd_font_ppem = 128;
991 if ((subtitle_font_encoding == NULL)
992 || (strcasecmp(subtitle_font_encoding, "unicode") == 0)) {
993 unicode = 1;
994 } else {
995 unicode = 0;
998 desc = init_font_desc();
999 if(!desc) goto err_out;
1001 // t=GetTimer();
1003 /* generate the subtitle font */
1004 err = load_sub_face(fname, face_index, &face);
1005 if (err) {
1006 mp_tmsg(MSGT_OSD, MSGL_WARN, "subtitle font: load_sub_face failed.\n");
1007 goto gen_osd;
1009 desc->face_cnt++;
1011 #ifdef CONFIG_ICONV
1012 if (unicode) {
1013 charset_size = prepare_charset_unicode(face, my_charset, my_charcodes);
1014 } else {
1015 if (subtitle_font_encoding) {
1016 charset_size = prepare_charset(charmap, subtitle_font_encoding, my_charset, my_charcodes);
1017 } else {
1018 charset_size = prepare_charset(charmap, "iso-8859-1", my_charset, my_charcodes);
1022 if (charset_size < 0) {
1023 mp_tmsg(MSGT_OSD, MSGL_ERR, "subtitle font: prepare_charset failed.\n");
1024 goto err_out;
1026 #else
1027 goto err_out;
1028 #endif
1030 // fprintf(stderr, "fg: prepare t = %f\n", GetTimer()-t);
1032 err = prepare_font(desc, face, subtitle_font_ppem, desc->face_cnt-1,
1033 charset_size, my_charset, my_charcodes, unicode,
1034 subtitle_font_thickness, subtitle_font_radius);
1036 if (err) {
1037 mp_tmsg(MSGT_OSD, MSGL_ERR, "Cannot prepare subtitle font.\n");
1038 goto err_out;
1041 gen_osd:
1043 /* generate the OSD font */
1044 err = load_osd_face(&face);
1045 if (err) {
1046 goto err_out;
1048 desc->face_cnt++;
1050 err = prepare_font(desc, face, osd_font_ppem, desc->face_cnt-1,
1051 OSD_CHARSET_SIZE, osd_charset, osd_charcodes, 0,
1052 subtitle_font_thickness, subtitle_font_radius);
1054 if (err) {
1055 mp_tmsg(MSGT_OSD, MSGL_ERR, "Cannot prepare OSD font.\n");
1056 goto err_out;
1059 err = generate_tables(desc, subtitle_font_thickness, subtitle_font_radius);
1061 if (err) {
1062 mp_tmsg(MSGT_OSD, MSGL_ERR, "Cannot generate tables.\n");
1063 goto err_out;
1066 // final cleanup
1067 desc->font[' ']=-1;
1068 desc->width[' ']=desc->spacewidth;
1070 j = '_';
1071 if (desc->font[j] < 0) j = '?';
1072 if (desc->font[j] < 0) j = ' ';
1073 render_one_glyph(desc, j);
1074 for(i = 0; i < 65536; i++) {
1075 if (desc->font[i] < 0 && i != ' ') {
1076 desc->start[i] = desc->start[j];
1077 desc->width[i] = desc->width[j];
1078 desc->font[i] = desc->font[j];
1081 free(my_charset);
1082 free(my_charcodes);
1083 return desc;
1085 err_out:
1086 if (desc)
1087 free_font_desc(desc);
1088 free(my_charset);
1089 free(my_charcodes);
1090 return NULL;
1093 int init_freetype(void)
1095 int err;
1097 /* initialize freetype */
1098 err = FT_Init_FreeType(&library);
1099 if (err) {
1100 mp_msg(MSGT_OSD, MSGL_ERR, "Init_FreeType failed.\n");
1101 return -1;
1103 mp_msg(MSGT_OSD, MSGL_V, "init_freetype\n");
1104 using_freetype = 1;
1105 return 0;
1108 int done_freetype(void)
1110 int err;
1112 if (!using_freetype)
1113 return 0;
1115 err = FT_Done_FreeType(library);
1116 if (err) {
1117 mp_tmsg(MSGT_OSD, MSGL_ERR, "FT_Done_FreeType failed.\n");
1118 return -1;
1121 return 0;
1124 void load_font_ft(int width, int height, font_desc_t** fontp, const char *font_name, float font_scale_factor)
1126 #ifdef CONFIG_FONTCONFIG
1127 FcPattern *fc_pattern;
1128 FcPattern *fc_pattern2;
1129 FcChar8 *s;
1130 int face_index;
1131 FcBool scalable;
1132 FcResult result;
1133 #endif
1134 font_desc_t *vo_font = *fontp;
1135 vo_image_width = width;
1136 vo_image_height = height;
1138 // protection against vo_aa font hacks
1139 if (vo_font && !vo_font->dynamic) return;
1141 if (vo_font) free_font_desc(vo_font);
1143 #ifdef CONFIG_FONTCONFIG
1144 if (font_fontconfig > 0)
1146 FcInit();
1147 fc_pattern = FcNameParse(font_name ? font_name : "sans-serif");
1148 FcConfigSubstitute(0, fc_pattern, FcMatchPattern);
1149 FcDefaultSubstitute(fc_pattern);
1150 fc_pattern2 = fc_pattern;
1151 fc_pattern = FcFontMatch(0, fc_pattern, &result);
1152 if (fc_pattern) {
1153 FcPatternDestroy(fc_pattern2);
1154 FcPatternGetBool(fc_pattern, FC_SCALABLE, 0, &scalable);
1155 if (scalable != FcTrue) {
1156 FcPatternDestroy(fc_pattern);
1157 fc_pattern = FcNameParse("sans-serif");
1158 FcConfigSubstitute(0, fc_pattern, FcMatchPattern);
1159 FcDefaultSubstitute(fc_pattern);
1160 fc_pattern2 = fc_pattern;
1161 fc_pattern = FcFontMatch(0, fc_pattern, 0);
1162 FcPatternDestroy(fc_pattern2);
1164 // s doesn't need to be freed according to fontconfig docs
1165 FcPatternGetString(fc_pattern, FC_FILE, 0, &s);
1166 FcPatternGetInteger(fc_pattern, FC_INDEX, 0, &face_index);
1167 *fontp=read_font_desc_ft(s, face_index, width, height, font_scale_factor);
1168 FcPatternDestroy(fc_pattern);
1169 return;
1171 mp_tmsg(MSGT_OSD, MSGL_ERR, "Fontconfig failed to select a font. "
1172 "Trying without fontconfig...\n");
1174 #endif
1175 *fontp=read_font_desc_ft(font_name, 0, width, height, font_scale_factor);