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