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