1 /*****************************************************************************
2 * text_renderer.h: common text renderer code
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Bernie Purcell <bitmap@videolan.org>
8 * Laurent Aimar < fenrir AT videolan DOT org >
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include <vlc_memory.h>
27 typedef struct font_stack_t font_stack_t;
32 uint32_t i_color; /* ARGB */
33 uint32_t i_karaoke_bg_color; /* ARGB */
38 static void SetupLine( filter_t *p_filter, const char *psz_text_in,
39 UCHAR **psz_text_out, uint32_t *pi_runs,
40 uint32_t **ppi_run_lengths, TR_FONT_STYLE_PTR **ppp_styles,
41 TR_FONT_STYLE_PTR p_style );
43 static TR_FONT_STYLE_PTR GetStyleFromFontStack( filter_sys_t *p_sys,
44 font_stack_t **p_fonts, bool b_bold, bool b_italic,
45 bool b_uline, bool b_through );
47 static int PushFont( font_stack_t **p_font, const char *psz_name, int i_size,
48 uint32_t i_color, uint32_t i_karaoke_bg_color )
55 p_new = malloc( sizeof( font_stack_t ) );
62 p_new->psz_name = strdup( psz_name );
64 p_new->psz_name = NULL;
66 p_new->i_size = i_size;
67 p_new->i_color = i_color;
68 p_new->i_karaoke_bg_color = i_karaoke_bg_color;
78 for( p_last = *p_font;
80 p_last = p_last->p_next )
83 p_last->p_next = p_new;
88 static int PopFont( font_stack_t **p_font )
90 font_stack_t *p_last, *p_next_to_last;
92 if( !p_font || !*p_font )
95 p_next_to_last = NULL;
96 for( p_last = *p_font;
98 p_last = p_last->p_next )
100 p_next_to_last = p_last;
104 p_next_to_last->p_next = NULL;
108 free( p_last->psz_name );
114 static int PeekFont( font_stack_t **p_font, char **psz_name, int *i_size,
115 uint32_t *i_color, uint32_t *i_karaoke_bg_color )
117 font_stack_t *p_last;
119 if( !p_font || !*p_font )
124 p_last=p_last->p_next )
127 *psz_name = p_last->psz_name;
128 *i_size = p_last->i_size;
129 *i_color = p_last->i_color;
130 *i_karaoke_bg_color = p_last->i_karaoke_bg_color;
135 static const struct {
136 const char *psz_name;
138 } p_html_colors[] = {
139 /* Official html colors */
140 { "Aqua", 0x00FFFF },
141 { "Black", 0x000000 },
142 { "Blue", 0x0000FF },
143 { "Fuchsia", 0xFF00FF },
144 { "Gray", 0x808080 },
145 { "Green", 0x008000 },
146 { "Lime", 0x00FF00 },
147 { "Maroon", 0x800000 },
148 { "Navy", 0x000080 },
149 { "Olive", 0x808000 },
150 { "Purple", 0x800080 },
152 { "Silver", 0xC0C0C0 },
153 { "Teal", 0x008080 },
154 { "White", 0xFFFFFF },
155 { "Yellow", 0xFFFF00 },
158 { "AliceBlue", 0xF0F8FF },
159 { "AntiqueWhite", 0xFAEBD7 },
160 { "Aqua", 0x00FFFF },
161 { "Aquamarine", 0x7FFFD4 },
162 { "Azure", 0xF0FFFF },
163 { "Beige", 0xF5F5DC },
164 { "Bisque", 0xFFE4C4 },
165 { "Black", 0x000000 },
166 { "BlanchedAlmond", 0xFFEBCD },
167 { "Blue", 0x0000FF },
168 { "BlueViolet", 0x8A2BE2 },
169 { "Brown", 0xA52A2A },
170 { "BurlyWood", 0xDEB887 },
171 { "CadetBlue", 0x5F9EA0 },
172 { "Chartreuse", 0x7FFF00 },
173 { "Chocolate", 0xD2691E },
174 { "Coral", 0xFF7F50 },
175 { "CornflowerBlue", 0x6495ED },
176 { "Cornsilk", 0xFFF8DC },
177 { "Crimson", 0xDC143C },
178 { "Cyan", 0x00FFFF },
179 { "DarkBlue", 0x00008B },
180 { "DarkCyan", 0x008B8B },
181 { "DarkGoldenRod", 0xB8860B },
182 { "DarkGray", 0xA9A9A9 },
183 { "DarkGrey", 0xA9A9A9 },
184 { "DarkGreen", 0x006400 },
185 { "DarkKhaki", 0xBDB76B },
186 { "DarkMagenta", 0x8B008B },
187 { "DarkOliveGreen", 0x556B2F },
188 { "Darkorange", 0xFF8C00 },
189 { "DarkOrchid", 0x9932CC },
190 { "DarkRed", 0x8B0000 },
191 { "DarkSalmon", 0xE9967A },
192 { "DarkSeaGreen", 0x8FBC8F },
193 { "DarkSlateBlue", 0x483D8B },
194 { "DarkSlateGray", 0x2F4F4F },
195 { "DarkSlateGrey", 0x2F4F4F },
196 { "DarkTurquoise", 0x00CED1 },
197 { "DarkViolet", 0x9400D3 },
198 { "DeepPink", 0xFF1493 },
199 { "DeepSkyBlue", 0x00BFFF },
200 { "DimGray", 0x696969 },
201 { "DimGrey", 0x696969 },
202 { "DodgerBlue", 0x1E90FF },
203 { "FireBrick", 0xB22222 },
204 { "FloralWhite", 0xFFFAF0 },
205 { "ForestGreen", 0x228B22 },
206 { "Fuchsia", 0xFF00FF },
207 { "Gainsboro", 0xDCDCDC },
208 { "GhostWhite", 0xF8F8FF },
209 { "Gold", 0xFFD700 },
210 { "GoldenRod", 0xDAA520 },
211 { "Gray", 0x808080 },
212 { "Grey", 0x808080 },
213 { "Green", 0x008000 },
214 { "GreenYellow", 0xADFF2F },
215 { "HoneyDew", 0xF0FFF0 },
216 { "HotPink", 0xFF69B4 },
217 { "IndianRed", 0xCD5C5C },
218 { "Indigo", 0x4B0082 },
219 { "Ivory", 0xFFFFF0 },
220 { "Khaki", 0xF0E68C },
221 { "Lavender", 0xE6E6FA },
222 { "LavenderBlush", 0xFFF0F5 },
223 { "LawnGreen", 0x7CFC00 },
224 { "LemonChiffon", 0xFFFACD },
225 { "LightBlue", 0xADD8E6 },
226 { "LightCoral", 0xF08080 },
227 { "LightCyan", 0xE0FFFF },
228 { "LightGoldenRodYellow", 0xFAFAD2 },
229 { "LightGray", 0xD3D3D3 },
230 { "LightGrey", 0xD3D3D3 },
231 { "LightGreen", 0x90EE90 },
232 { "LightPink", 0xFFB6C1 },
233 { "LightSalmon", 0xFFA07A },
234 { "LightSeaGreen", 0x20B2AA },
235 { "LightSkyBlue", 0x87CEFA },
236 { "LightSlateGray", 0x778899 },
237 { "LightSlateGrey", 0x778899 },
238 { "LightSteelBlue", 0xB0C4DE },
239 { "LightYellow", 0xFFFFE0 },
240 { "Lime", 0x00FF00 },
241 { "LimeGreen", 0x32CD32 },
242 { "Linen", 0xFAF0E6 },
243 { "Magenta", 0xFF00FF },
244 { "Maroon", 0x800000 },
245 { "MediumAquaMarine", 0x66CDAA },
246 { "MediumBlue", 0x0000CD },
247 { "MediumOrchid", 0xBA55D3 },
248 { "MediumPurple", 0x9370D8 },
249 { "MediumSeaGreen", 0x3CB371 },
250 { "MediumSlateBlue", 0x7B68EE },
251 { "MediumSpringGreen", 0x00FA9A },
252 { "MediumTurquoise", 0x48D1CC },
253 { "MediumVioletRed", 0xC71585 },
254 { "MidnightBlue", 0x191970 },
255 { "MintCream", 0xF5FFFA },
256 { "MistyRose", 0xFFE4E1 },
257 { "Moccasin", 0xFFE4B5 },
258 { "NavajoWhite", 0xFFDEAD },
259 { "Navy", 0x000080 },
260 { "OldLace", 0xFDF5E6 },
261 { "Olive", 0x808000 },
262 { "OliveDrab", 0x6B8E23 },
263 { "Orange", 0xFFA500 },
264 { "OrangeRed", 0xFF4500 },
265 { "Orchid", 0xDA70D6 },
266 { "PaleGoldenRod", 0xEEE8AA },
267 { "PaleGreen", 0x98FB98 },
268 { "PaleTurquoise", 0xAFEEEE },
269 { "PaleVioletRed", 0xD87093 },
270 { "PapayaWhip", 0xFFEFD5 },
271 { "PeachPuff", 0xFFDAB9 },
272 { "Peru", 0xCD853F },
273 { "Pink", 0xFFC0CB },
274 { "Plum", 0xDDA0DD },
275 { "PowderBlue", 0xB0E0E6 },
276 { "Purple", 0x800080 },
278 { "RosyBrown", 0xBC8F8F },
279 { "RoyalBlue", 0x4169E1 },
280 { "SaddleBrown", 0x8B4513 },
281 { "Salmon", 0xFA8072 },
282 { "SandyBrown", 0xF4A460 },
283 { "SeaGreen", 0x2E8B57 },
284 { "SeaShell", 0xFFF5EE },
285 { "Sienna", 0xA0522D },
286 { "Silver", 0xC0C0C0 },
287 { "SkyBlue", 0x87CEEB },
288 { "SlateBlue", 0x6A5ACD },
289 { "SlateGray", 0x708090 },
290 { "SlateGrey", 0x708090 },
291 { "Snow", 0xFFFAFA },
292 { "SpringGreen", 0x00FF7F },
293 { "SteelBlue", 0x4682B4 },
295 { "Teal", 0x008080 },
296 { "Thistle", 0xD8BFD8 },
297 { "Tomato", 0xFF6347 },
298 { "Turquoise", 0x40E0D0 },
299 { "Violet", 0xEE82EE },
300 { "Wheat", 0xF5DEB3 },
301 { "White", 0xFFFFFF },
302 { "WhiteSmoke", 0xF5F5F5 },
303 { "Yellow", 0xFFFF00 },
304 { "YellowGreen", 0x9ACD32 },
309 static int HandleFontAttributes( xml_reader_t *p_xml_reader,
310 font_stack_t **p_fonts, int i_scale )
313 char *psz_fontname = NULL;
314 uint32_t i_font_color = 0xffffff;
315 int i_font_alpha = 0;
316 uint32_t i_karaoke_bg_color = 0x00ffffff;
317 int i_font_size = 24;
319 /* Default all attributes to the top font in the stack -- in case not
320 * all attributes are specified in the sub-font
322 if( VLC_SUCCESS == PeekFont( p_fonts,
326 &i_karaoke_bg_color ))
328 psz_fontname = strdup( psz_fontname );
329 i_font_size = i_font_size * 1000 / i_scale;
331 i_font_alpha = (i_font_color >> 24) & 0xff;
332 i_font_color &= 0x00ffffff;
334 const char *name, *value;
335 while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
337 if( !strcasecmp( "face", name ) )
339 free( psz_fontname );
340 psz_fontname = strdup( value );
342 else if( !strcasecmp( "size", name ) )
344 if( ( *value == '+' ) || ( *value == '-' ) )
346 int i_value = atoi( value );
348 if( ( i_value >= -5 ) && ( i_value <= 5 ) )
349 i_font_size += ( i_value * i_font_size ) / 10;
350 else if( i_value < -5 )
351 i_font_size = - i_value;
352 else if( i_value > 5 )
353 i_font_size = i_value;
356 i_font_size = atoi( value );
358 else if( !strcasecmp( "color", name ) )
360 if( value[0] == '#' )
362 i_font_color = strtol( value + 1, NULL, 16 );
363 i_font_color &= 0x00ffffff;
367 for( int i = 0; p_html_colors[i].psz_name != NULL; i++ )
369 if( !strncasecmp( value, p_html_colors[i].psz_name, strlen(p_html_colors[i].psz_name) ) )
371 i_font_color = p_html_colors[i].i_value;
377 else if( !strcasecmp( "alpha", name ) && ( value[0] == '#' ) )
379 i_font_alpha = strtol( value + 1, NULL, 16 );
380 i_font_alpha &= 0xff;
383 rv = PushFont( p_fonts,
385 i_font_size * i_scale / 1000,
386 (i_font_color & 0xffffff) | ((i_font_alpha & 0xff) << 24),
387 i_karaoke_bg_color );
389 free( psz_fontname );
394 static void SetKaraokeLen( uint32_t i_runs, uint32_t *pi_run_lengths,
395 uint32_t i_k_runs, uint32_t *pi_k_run_lengths )
397 /* Karaoke tags _PRECEDE_ the text they specify a duration
398 * for, therefore we are working out the length for the
399 * previous tag, and first time through we have nothing
401 if( pi_k_run_lengths )
406 /* Work out how many characters are presently in the string
408 for( i = 0; i < i_runs; i++ )
409 i_chars += pi_run_lengths[ i ];
411 /* Subtract away those we've already allocated to other
414 for( i = 0; i < i_k_runs; i++ )
415 i_chars -= pi_k_run_lengths[ i ];
417 pi_k_run_lengths[ i_k_runs - 1 ] = i_chars;
421 static void SetupKaraoke( xml_reader_t *p_xml_reader, uint32_t *pi_k_runs,
422 uint32_t **ppi_k_run_lengths,
423 uint32_t **ppi_k_durations )
425 const char *name, *value;
427 while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
429 if( !strcasecmp( "t", name ) )
431 if( ppi_k_durations && ppi_k_run_lengths )
435 if( *ppi_k_durations )
437 *ppi_k_durations = realloc_or_free( *ppi_k_durations,
438 *pi_k_runs * sizeof( uint32_t ) );
440 else if( *pi_k_runs == 1 )
442 *ppi_k_durations = (uint32_t *)
443 malloc( *pi_k_runs * sizeof( uint32_t ) );
446 if( *ppi_k_run_lengths )
448 *ppi_k_run_lengths = realloc_or_free( *ppi_k_run_lengths,
449 *pi_k_runs * sizeof( uint32_t ) );
451 else if( *pi_k_runs == 1 )
453 *ppi_k_run_lengths = (uint32_t *)
454 malloc( *pi_k_runs * sizeof( uint32_t ) );
456 if( *ppi_k_durations )
457 (*ppi_k_durations)[ *pi_k_runs - 1 ] = atoi( value );
459 if( *ppi_k_run_lengths )
460 (*ppi_k_run_lengths)[ *pi_k_runs - 1 ] = 0;
466 /* Turn any multiple-whitespaces into single spaces */
467 static void HandleWhiteSpace( char *psz_node )
469 char *s = strpbrk( psz_node, "\t\r\n " );
472 int i_whitespace = strspn( s, "\t\r\n " );
474 if( i_whitespace > 1 )
477 strlen( s ) - i_whitespace + 1 );
480 s = strpbrk( s, "\t\r\n " );
485 static int ProcessNodes( filter_t *p_filter,
486 xml_reader_t *p_xml_reader,
487 text_style_t *p_font_style,
492 uint32_t **ppi_run_lengths,
493 TR_FONT_STYLE_PTR **ppp_styles,
497 uint32_t **ppi_k_run_lengths,
498 uint32_t **ppi_k_durations )
500 int rv = VLC_SUCCESS;
501 filter_sys_t *p_sys = p_filter->p_sys;
502 UCHAR *psz_text_orig = psz_text;
503 font_stack_t *p_fonts = NULL;
507 bool b_italic = false;
509 bool b_uline = false;
510 bool b_through = false;
512 if( VLC_SUCCESS == var_Get( p_filter, "scale", &val ))
517 rv = PushFont( &p_fonts,
518 p_font_style->psz_fontname,
519 p_font_style->i_font_size * i_scale / 1000,
520 (p_font_style->i_font_color & 0xffffff) |
521 ((p_font_style->i_font_alpha & 0xff) << 24),
522 (p_font_style->i_karaoke_background_color & 0xffffff) |
523 ((p_font_style->i_karaoke_background_alpha & 0xff) << 24));
525 if( p_font_style->i_style_flags & STYLE_BOLD )
527 if( p_font_style->i_style_flags & STYLE_ITALIC )
529 if( p_font_style->i_style_flags & STYLE_UNDERLINE )
531 if( p_font_style->i_style_flags & STYLE_STRIKEOUT )
534 #ifdef HAVE_FONTCONFIG
537 rv = PushFont( &p_fonts,
540 (p_sys->i_font_color & 0xffffff) |
541 (((255-p_sys->i_font_opacity) & 0xff) << 24),
546 if( rv != VLC_SUCCESS )
552 while ( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
556 case XML_READER_ENDELEM:
557 if( !strcasecmp( "font", node ) )
559 else if( !strcasecmp( "b", node ) )
561 else if( !strcasecmp( "i", node ) )
563 else if( !strcasecmp( "u", node ) )
565 else if( !strcasecmp( "s", node ) )
569 case XML_READER_STARTELEM:
570 if( !strcasecmp( "font", node ) )
571 rv = HandleFontAttributes( p_xml_reader, &p_fonts, i_scale );
572 else if( !strcasecmp( "b", node ) )
574 else if( !strcasecmp( "i", node ) )
576 else if( !strcasecmp( "u", node ) )
578 else if( !strcasecmp( "s", node ) )
580 else if( !strcasecmp( "br", node ) )
582 SetupLine( p_filter, "\n", &psz_text,
583 pi_runs, ppi_run_lengths, ppp_styles,
584 GetStyleFromFontStack( p_sys,
591 else if( !strcasecmp( "k", node ) )
593 /* Only valid in karaoke */
597 SetKaraokeLen( *pi_runs, *ppi_run_lengths,
598 *pi_k_runs, *ppi_k_run_lengths );
599 SetupKaraoke( p_xml_reader, pi_k_runs,
600 ppi_k_run_lengths, ppi_k_durations );
605 case XML_READER_TEXT:
607 char *psz_node = strdup( node );
608 if( unlikely(!psz_node) )
611 HandleWhiteSpace( psz_node );
612 resolve_xml_special_chars( psz_node );
614 SetupLine( p_filter, psz_node, &psz_text,
615 pi_runs, ppi_run_lengths, ppp_styles,
616 GetStyleFromFontStack( p_sys,
626 if( rv != VLC_SUCCESS )
628 psz_text = psz_text_orig;
634 SetKaraokeLen( *pi_runs, *ppi_run_lengths,
635 *pi_k_runs, *ppi_k_run_lengths );
638 *pi_len = psz_text - psz_text_orig;
640 while( VLC_SUCCESS == PopFont( &p_fonts ) );