1 /*****************************************************************************
2 * freetype.c : Put text on the video, using freetype2
3 *****************************************************************************
4 * Copyright (C) 2002 - 2012 VLC authors and VideoLAN
7 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Bernie Purcell <bitmap@videolan.org>
10 * Jean-Baptiste Kempf <jb@videolan.org>
11 * Felix Paul Kühne <fkuehne@videolan.org>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2.1 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software Foundation, Inc.,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_variables.h>
34 #include <vlc_filter.h> /* filter_sys_t */
35 #include <vlc_xml.h> /* xml_reader */
36 #include <vlc_charset.h> /* ToCharset */
37 #include <vlc_strings.h> /* resolve_xml_special_chars */
39 #include "text_renderer.h"
41 text_style_t *CreateStyle( char *psz_fontname, int i_font_size,
42 uint32_t i_font_color, uint32_t i_karaoke_bg_color,
45 text_style_t *p_style = text_style_New();
49 p_style->psz_fontname = psz_fontname ? strdup( psz_fontname ) : NULL;
50 p_style->i_font_size = i_font_size;
51 p_style->i_font_color = (i_font_color & 0x00ffffff) >> 0;
52 p_style->i_font_alpha = (i_font_color & 0xff000000) >> 24;
53 p_style->i_karaoke_background_color = (i_karaoke_bg_color & 0x00ffffff) >> 0;
54 p_style->i_karaoke_background_alpha = (i_karaoke_bg_color & 0xff000000) >> 24;
55 p_style->i_style_flags |= i_style_flags;
59 int PushFont( font_stack_t **p_font, const char *psz_name, int i_size,
60 uint32_t i_color, uint32_t i_karaoke_bg_color )
65 font_stack_t *p_new = malloc( sizeof(*p_new) );
72 p_new->psz_name = strdup( psz_name );
74 p_new->psz_name = NULL;
76 p_new->i_size = i_size;
77 p_new->i_color = i_color;
78 p_new->i_karaoke_bg_color = i_karaoke_bg_color;
88 for( p_last = *p_font;
90 p_last = p_last->p_next )
93 p_last->p_next = p_new;
98 int PopFont( font_stack_t **p_font )
100 font_stack_t *p_last, *p_next_to_last;
102 if( !p_font || !*p_font )
105 p_next_to_last = NULL;
106 for( p_last = *p_font;
108 p_last = p_last->p_next )
110 p_next_to_last = p_last;
114 p_next_to_last->p_next = NULL;
118 free( p_last->psz_name );
124 int PeekFont( font_stack_t **p_font, char **psz_name, int *i_size,
125 uint32_t *i_color, uint32_t *i_karaoke_bg_color )
127 font_stack_t *p_last;
129 if( !p_font || !*p_font )
134 p_last=p_last->p_next )
137 *psz_name = p_last->psz_name;
138 *i_size = p_last->i_size;
139 *i_color = p_last->i_color;
140 *i_karaoke_bg_color = p_last->i_karaoke_bg_color;
145 static const struct {
146 const char *psz_name;
148 } p_html_colors[] = {
149 /* Official html colors */
150 { "Aqua", 0x00FFFF },
151 { "Black", 0x000000 },
152 { "Blue", 0x0000FF },
153 { "Fuchsia", 0xFF00FF },
154 { "Gray", 0x808080 },
155 { "Green", 0x008000 },
156 { "Lime", 0x00FF00 },
157 { "Maroon", 0x800000 },
158 { "Navy", 0x000080 },
159 { "Olive", 0x808000 },
160 { "Purple", 0x800080 },
162 { "Silver", 0xC0C0C0 },
163 { "Teal", 0x008080 },
164 { "White", 0xFFFFFF },
165 { "Yellow", 0xFFFF00 },
168 { "AliceBlue", 0xF0F8FF },
169 { "AntiqueWhite", 0xFAEBD7 },
170 { "Aqua", 0x00FFFF },
171 { "Aquamarine", 0x7FFFD4 },
172 { "Azure", 0xF0FFFF },
173 { "Beige", 0xF5F5DC },
174 { "Bisque", 0xFFE4C4 },
175 { "Black", 0x000000 },
176 { "BlanchedAlmond", 0xFFEBCD },
177 { "Blue", 0x0000FF },
178 { "BlueViolet", 0x8A2BE2 },
179 { "Brown", 0xA52A2A },
180 { "BurlyWood", 0xDEB887 },
181 { "CadetBlue", 0x5F9EA0 },
182 { "Chartreuse", 0x7FFF00 },
183 { "Chocolate", 0xD2691E },
184 { "Coral", 0xFF7F50 },
185 { "CornflowerBlue", 0x6495ED },
186 { "Cornsilk", 0xFFF8DC },
187 { "Crimson", 0xDC143C },
188 { "Cyan", 0x00FFFF },
189 { "DarkBlue", 0x00008B },
190 { "DarkCyan", 0x008B8B },
191 { "DarkGoldenRod", 0xB8860B },
192 { "DarkGray", 0xA9A9A9 },
193 { "DarkGrey", 0xA9A9A9 },
194 { "DarkGreen", 0x006400 },
195 { "DarkKhaki", 0xBDB76B },
196 { "DarkMagenta", 0x8B008B },
197 { "DarkOliveGreen", 0x556B2F },
198 { "Darkorange", 0xFF8C00 },
199 { "DarkOrchid", 0x9932CC },
200 { "DarkRed", 0x8B0000 },
201 { "DarkSalmon", 0xE9967A },
202 { "DarkSeaGreen", 0x8FBC8F },
203 { "DarkSlateBlue", 0x483D8B },
204 { "DarkSlateGray", 0x2F4F4F },
205 { "DarkSlateGrey", 0x2F4F4F },
206 { "DarkTurquoise", 0x00CED1 },
207 { "DarkViolet", 0x9400D3 },
208 { "DeepPink", 0xFF1493 },
209 { "DeepSkyBlue", 0x00BFFF },
210 { "DimGray", 0x696969 },
211 { "DimGrey", 0x696969 },
212 { "DodgerBlue", 0x1E90FF },
213 { "FireBrick", 0xB22222 },
214 { "FloralWhite", 0xFFFAF0 },
215 { "ForestGreen", 0x228B22 },
216 { "Fuchsia", 0xFF00FF },
217 { "Gainsboro", 0xDCDCDC },
218 { "GhostWhite", 0xF8F8FF },
219 { "Gold", 0xFFD700 },
220 { "GoldenRod", 0xDAA520 },
221 { "Gray", 0x808080 },
222 { "Grey", 0x808080 },
223 { "Green", 0x008000 },
224 { "GreenYellow", 0xADFF2F },
225 { "HoneyDew", 0xF0FFF0 },
226 { "HotPink", 0xFF69B4 },
227 { "IndianRed", 0xCD5C5C },
228 { "Indigo", 0x4B0082 },
229 { "Ivory", 0xFFFFF0 },
230 { "Khaki", 0xF0E68C },
231 { "Lavender", 0xE6E6FA },
232 { "LavenderBlush", 0xFFF0F5 },
233 { "LawnGreen", 0x7CFC00 },
234 { "LemonChiffon", 0xFFFACD },
235 { "LightBlue", 0xADD8E6 },
236 { "LightCoral", 0xF08080 },
237 { "LightCyan", 0xE0FFFF },
238 { "LightGoldenRodYellow", 0xFAFAD2 },
239 { "LightGray", 0xD3D3D3 },
240 { "LightGrey", 0xD3D3D3 },
241 { "LightGreen", 0x90EE90 },
242 { "LightPink", 0xFFB6C1 },
243 { "LightSalmon", 0xFFA07A },
244 { "LightSeaGreen", 0x20B2AA },
245 { "LightSkyBlue", 0x87CEFA },
246 { "LightSlateGray", 0x778899 },
247 { "LightSlateGrey", 0x778899 },
248 { "LightSteelBlue", 0xB0C4DE },
249 { "LightYellow", 0xFFFFE0 },
250 { "Lime", 0x00FF00 },
251 { "LimeGreen", 0x32CD32 },
252 { "Linen", 0xFAF0E6 },
253 { "Magenta", 0xFF00FF },
254 { "Maroon", 0x800000 },
255 { "MediumAquaMarine", 0x66CDAA },
256 { "MediumBlue", 0x0000CD },
257 { "MediumOrchid", 0xBA55D3 },
258 { "MediumPurple", 0x9370D8 },
259 { "MediumSeaGreen", 0x3CB371 },
260 { "MediumSlateBlue", 0x7B68EE },
261 { "MediumSpringGreen", 0x00FA9A },
262 { "MediumTurquoise", 0x48D1CC },
263 { "MediumVioletRed", 0xC71585 },
264 { "MidnightBlue", 0x191970 },
265 { "MintCream", 0xF5FFFA },
266 { "MistyRose", 0xFFE4E1 },
267 { "Moccasin", 0xFFE4B5 },
268 { "NavajoWhite", 0xFFDEAD },
269 { "Navy", 0x000080 },
270 { "OldLace", 0xFDF5E6 },
271 { "Olive", 0x808000 },
272 { "OliveDrab", 0x6B8E23 },
273 { "Orange", 0xFFA500 },
274 { "OrangeRed", 0xFF4500 },
275 { "Orchid", 0xDA70D6 },
276 { "PaleGoldenRod", 0xEEE8AA },
277 { "PaleGreen", 0x98FB98 },
278 { "PaleTurquoise", 0xAFEEEE },
279 { "PaleVioletRed", 0xD87093 },
280 { "PapayaWhip", 0xFFEFD5 },
281 { "PeachPuff", 0xFFDAB9 },
282 { "Peru", 0xCD853F },
283 { "Pink", 0xFFC0CB },
284 { "Plum", 0xDDA0DD },
285 { "PowderBlue", 0xB0E0E6 },
286 { "Purple", 0x800080 },
288 { "RosyBrown", 0xBC8F8F },
289 { "RoyalBlue", 0x4169E1 },
290 { "SaddleBrown", 0x8B4513 },
291 { "Salmon", 0xFA8072 },
292 { "SandyBrown", 0xF4A460 },
293 { "SeaGreen", 0x2E8B57 },
294 { "SeaShell", 0xFFF5EE },
295 { "Sienna", 0xA0522D },
296 { "Silver", 0xC0C0C0 },
297 { "SkyBlue", 0x87CEEB },
298 { "SlateBlue", 0x6A5ACD },
299 { "SlateGray", 0x708090 },
300 { "SlateGrey", 0x708090 },
301 { "Snow", 0xFFFAFA },
302 { "SpringGreen", 0x00FF7F },
303 { "SteelBlue", 0x4682B4 },
305 { "Teal", 0x008080 },
306 { "Thistle", 0xD8BFD8 },
307 { "Tomato", 0xFF6347 },
308 { "Turquoise", 0x40E0D0 },
309 { "Violet", 0xEE82EE },
310 { "Wheat", 0xF5DEB3 },
311 { "White", 0xFFFFFF },
312 { "WhiteSmoke", 0xF5F5F5 },
313 { "Yellow", 0xFFFF00 },
314 { "YellowGreen", 0x9ACD32 },
319 int HandleFontAttributes( xml_reader_t *p_xml_reader,
320 font_stack_t **p_fonts )
323 char *psz_fontname = NULL;
324 uint32_t i_font_color = 0xffffff;
325 int i_font_alpha = 255;
326 uint32_t i_karaoke_bg_color = 0x00ffffff;
327 int i_font_size = 24;
329 /* Default all attributes to the top font in the stack -- in case not
330 * all attributes are specified in the sub-font
332 if( VLC_SUCCESS == PeekFont( p_fonts,
336 &i_karaoke_bg_color ))
338 psz_fontname = strdup( psz_fontname );
340 i_font_alpha = (i_font_color >> 24) & 0xff;
341 i_font_color &= 0x00ffffff;
343 const char *name, *value;
344 while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
346 if( !strcasecmp( "face", name ) )
348 free( psz_fontname );
349 psz_fontname = strdup( value );
351 else if( !strcasecmp( "size", name ) )
353 if( ( *value == '+' ) || ( *value == '-' ) )
355 int i_value = atoi( value );
357 if( ( i_value >= -5 ) && ( i_value <= 5 ) )
358 i_font_size += ( i_value * i_font_size ) / 10;
359 else if( i_value < -5 )
360 i_font_size = - i_value;
361 else if( i_value > 5 )
362 i_font_size = i_value;
365 i_font_size = atoi( value );
367 else if( !strcasecmp( "color", name ) )
369 if( value[0] == '#' )
371 i_font_color = strtol( value + 1, NULL, 16 );
372 i_font_color &= 0x00ffffff;
377 uint32_t i_value = strtol( value, &end, 16 );
378 if( *end == '\0' || *end == ' ' )
379 i_font_color = i_value & 0x00ffffff;
381 for( int i = 0; p_html_colors[i].psz_name != NULL; i++ )
383 if( !strncasecmp( value, p_html_colors[i].psz_name, strlen(p_html_colors[i].psz_name) ) )
385 i_font_color = p_html_colors[i].i_value;
391 else if( !strcasecmp( "alpha", name ) && ( value[0] == '#' ) )
393 i_font_alpha = strtol( value + 1, NULL, 16 );
394 i_font_alpha &= 0xff;
397 rv = PushFont( p_fonts,
400 (i_font_color & 0xffffff) | ((i_font_alpha & 0xff) << 24),
401 i_karaoke_bg_color );
403 free( psz_fontname );
408 int HandleTT(font_stack_t **p_fonts, const char *p_fontfamily )
410 char *psz_unused_fontname = NULL;
411 uint32_t i_font_color = 0xffffff;
412 uint32_t i_karaoke_bg_color = 0x00ffffff;
413 int i_font_size = 24;
415 /* Default all attributes to the top font in the stack -- in case not
416 * all attributes are specified in the sub-font
419 &psz_unused_fontname,
422 &i_karaoke_bg_color );
424 /* Keep all the parent's font attributes, but change to a monospace font */
425 return PushFont( p_fonts,
429 i_karaoke_bg_color );
432 /* Turn any multiple-whitespaces into single spaces */
433 void HandleWhiteSpace( char *psz_node )
435 char *s = strpbrk( psz_node, "\t\r\n " );
438 int i_whitespace = strspn( s, "\t\r\n " );
440 if( i_whitespace > 1 )
443 strlen( s ) - i_whitespace + 1 );
446 s = strpbrk( s, "\t\r\n " );
450 unsigned SetupText( filter_t *p_filter,
451 uni_char_t *psz_text_out,
452 text_style_t **pp_styles,
453 uint32_t *pi_k_dates,
455 const char *psz_text_in,
456 text_style_t *p_style,
459 size_t i_string_length;
461 size_t i_string_bytes;
462 uni_char_t *psz_tmp = ToCharset( FREETYPE_TO_UCS, psz_text_in, &i_string_bytes );
465 memcpy( psz_text_out, psz_tmp, i_string_bytes );
466 i_string_length = i_string_bytes / sizeof( *psz_tmp );
471 msg_Warn( p_filter, "failed to convert string to unicode (%m)" );
475 if( i_string_length > 0 )
477 for( unsigned i = 0; i < i_string_length; i++ )
478 pp_styles[i] = p_style;
482 text_style_Delete( p_style );
484 if( i_string_length > 0 && pi_k_dates )
486 for( unsigned i = 0; i < i_string_length; i++ )
487 pi_k_dates[i] = i_k_date;
489 return i_string_length;
492 bool FaceStyleEquals( const text_style_t *p_style1,
493 const text_style_t *p_style2 )
495 if( !p_style1 || !p_style2 )
497 if( p_style1 == p_style2 )
500 const int i_style_mask = STYLE_BOLD | STYLE_ITALIC;
501 return (p_style1->i_style_flags & i_style_mask) == (p_style2->i_style_flags & i_style_mask) &&
502 !strcmp( p_style1->psz_fontname, p_style2->psz_fontname );
505 text_style_t *GetStyleFromFontStack( filter_t *p_filter,
506 font_stack_t **p_fonts,
510 char *psz_fontname = NULL;
511 uint32_t i_font_color = var_InheritInteger( p_filter, "freetype-color" );
512 i_font_color = VLC_CLIP( i_font_color, 0, 0xFFFFFF );
513 i_font_color = i_font_color & 0x00ffffff;
515 int i_font_size = style->i_font_size;
516 uint32_t i_karaoke_bg_color = i_font_color;
518 if( PeekFont( p_fonts, &psz_fontname, &i_font_size,
519 &i_font_color, &i_karaoke_bg_color ) )
522 return CreateStyle( psz_fontname, i_font_size, i_font_color,
527 int ProcessNodes( filter_t *p_filter,
528 uni_char_t *psz_text,
529 text_style_t **pp_styles,
530 uint32_t *pi_k_dates,
532 xml_reader_t *p_xml_reader,
533 text_style_t *p_font_style,
534 text_style_t *p_default_style )
536 int rv = VLC_SUCCESS;
537 int i_text_length = 0;
538 font_stack_t *p_fonts = NULL;
539 uint32_t i_k_date = 0;
541 int i_style_flags = 0;
545 /* If the font is not specified in the style, assume the system font */
546 if(!p_font_style->psz_fontname)
547 p_font_style->psz_fontname = strdup(p_default_style->psz_fontname);
549 rv = PushFont( &p_fonts,
550 p_font_style->psz_fontname,
551 p_font_style->i_font_size > 0 ? p_font_style->i_font_size
552 : p_default_style->i_font_size,
553 (p_font_style->i_font_color & 0xffffff) |
554 ((p_font_style->i_font_alpha & 0xff) << 24),
555 (p_font_style->i_karaoke_background_color & 0xffffff) |
556 ((p_font_style->i_karaoke_background_alpha & 0xff) << 24));
558 i_style_flags = p_font_style->i_style_flags & (STYLE_BOLD |
565 uint32_t i_font_size = p_default_style->i_font_size;
566 uint32_t i_font_color = var_InheritInteger( p_filter, "freetype-color" );
567 i_font_color = VLC_CLIP( i_font_color, 0, 0xFFFFFF );
568 int i_font_alpha = p_default_style->i_font_alpha;
569 rv = PushFont( &p_fonts,
570 p_default_style->psz_fontname,
572 (i_font_color & 0xffffff) |
573 ((i_font_alpha & 0xff) << 24),
576 if( p_default_style->i_style_flags & STYLE_BOLD )
577 i_style_flags |= STYLE_BOLD;
579 if( rv != VLC_SUCCESS )
585 while ( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
589 case XML_READER_ENDELEM:
590 if( !strcasecmp( "font", node ) )
592 else if( !strcasecmp( "tt", node ) )
594 else if( !strcasecmp( "b", node ) )
595 i_style_flags &= ~STYLE_BOLD;
596 else if( !strcasecmp( "i", node ) )
597 i_style_flags &= ~STYLE_ITALIC;
598 else if( !strcasecmp( "u", node ) )
599 i_style_flags &= ~STYLE_UNDERLINE;
600 else if( !strcasecmp( "s", node ) )
601 i_style_flags &= ~STYLE_STRIKEOUT;
604 case XML_READER_STARTELEM:
605 if( !strcasecmp( "font", node ) )
606 HandleFontAttributes( p_xml_reader, &p_fonts );
607 else if( !strcasecmp( "tt", node ) )
608 HandleTT( &p_fonts, p_default_style->psz_monofontname );
609 else if( !strcasecmp( "b", node ) )
610 i_style_flags |= STYLE_BOLD;
611 else if( !strcasecmp( "i", node ) )
612 i_style_flags |= STYLE_ITALIC;
613 else if( !strcasecmp( "u", node ) )
614 i_style_flags |= STYLE_UNDERLINE;
615 else if( !strcasecmp( "s", node ) )
616 i_style_flags |= STYLE_STRIKEOUT;
617 else if( !strcasecmp( "br", node ) )
619 i_text_length += SetupText( p_filter,
620 &psz_text[i_text_length],
621 &pp_styles[i_text_length],
622 pi_k_dates ? &pi_k_dates[i_text_length] : NULL,
624 GetStyleFromFontStack( p_filter,
630 else if( !strcasecmp( "k", node ) )
633 const char *name, *value;
634 while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
636 if( !strcasecmp( "t", name ) && value )
637 i_k_date += atoi( value );
642 case XML_READER_TEXT:
644 char *psz_node = strdup( node );
645 if( unlikely(!psz_node) )
648 HandleWhiteSpace( psz_node );
649 resolve_xml_special_chars( psz_node );
651 i_text_length += SetupText( p_filter,
652 &psz_text[i_text_length],
653 &pp_styles[i_text_length],
654 pi_k_dates ? &pi_k_dates[i_text_length] : NULL,
656 GetStyleFromFontStack( p_filter,
667 *pi_len = i_text_length;
669 while( VLC_SUCCESS == PopFont( &p_fonts ) );