]> git.sesse.net Git - vlc/blob - modules/text_renderer/text_renderer.c
quartztext: fix relative font size handling
[vlc] / modules / text_renderer / text_renderer.c
1 /*****************************************************************************
2  * freetype.c : Put text on the video, using freetype2
3  *****************************************************************************
4  * Copyright (C) 2002 - 2012 VLC authors and VideoLAN
5  * $Id$
6  *
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>
12  *
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.
17  *
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.
22  *
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  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
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 */
38
39 #include "text_renderer.h"
40
41 text_style_t *CreateStyle( char *psz_fontname, int i_font_size,
42                                   uint32_t i_font_color, uint32_t i_karaoke_bg_color,
43                                   int i_style_flags )
44 {
45     text_style_t *p_style = text_style_New();
46     if( !p_style )
47         return NULL;
48
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;
56     return p_style;
57 }
58
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 )
61 {
62     if( !p_font )
63         return VLC_EGENERIC;
64
65     font_stack_t *p_new = malloc( sizeof(*p_new) );
66     if( !p_new )
67         return VLC_ENOMEM;
68
69     p_new->p_next = NULL;
70
71     if( psz_name )
72         p_new->psz_name = strdup( psz_name );
73     else
74         p_new->psz_name = NULL;
75
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;
79
80     if( !*p_font )
81     {
82         *p_font = p_new;
83     }
84     else
85     {
86         font_stack_t *p_last;
87
88         for( p_last = *p_font;
89              p_last->p_next;
90              p_last = p_last->p_next )
91         ;
92
93         p_last->p_next = p_new;
94     }
95     return VLC_SUCCESS;
96 }
97
98 int PopFont( font_stack_t **p_font )
99 {
100     font_stack_t *p_last, *p_next_to_last;
101
102     if( !p_font || !*p_font )
103         return VLC_EGENERIC;
104
105     p_next_to_last = NULL;
106     for( p_last = *p_font;
107          p_last->p_next;
108          p_last = p_last->p_next )
109     {
110         p_next_to_last = p_last;
111     }
112
113     if( p_next_to_last )
114         p_next_to_last->p_next = NULL;
115     else
116         *p_font = NULL;
117
118     free( p_last->psz_name );
119     free( p_last );
120
121     return VLC_SUCCESS;
122 }
123
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 )
126 {
127     font_stack_t *p_last;
128
129     if( !p_font || !*p_font )
130         return VLC_EGENERIC;
131
132     for( p_last=*p_font;
133          p_last->p_next;
134          p_last=p_last->p_next )
135     ;
136
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;
141
142     return VLC_SUCCESS;
143 }
144
145 static const struct {
146     const char *psz_name;
147     uint32_t   i_value;
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 },
161     { "Red",     0xFF0000 },
162     { "Silver",  0xC0C0C0 },
163     { "Teal",    0x008080 },
164     { "White",   0xFFFFFF },
165     { "Yellow",  0xFFFF00 },
166
167     /* Common ones */
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 },
287     { "Red", 0xFF0000 },
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 },
304     { "Tan", 0xD2B48C },
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 },
315
316     { NULL, 0 }
317 };
318
319 int HandleFontAttributes( xml_reader_t *p_xml_reader,
320                                  font_stack_t **p_fonts )
321 {
322     int        rv;
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;
328
329     /* Default all attributes to the top font in the stack -- in case not
330      * all attributes are specified in the sub-font
331      */
332     if( VLC_SUCCESS == PeekFont( p_fonts,
333                                  &psz_fontname,
334                                  &i_font_size,
335                                  &i_font_color,
336                                  &i_karaoke_bg_color ))
337     {
338         psz_fontname = strdup( psz_fontname );
339     }
340     i_font_alpha = (i_font_color >> 24) & 0xff;
341     i_font_color &= 0x00ffffff;
342
343     const char *name, *value;
344     while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
345     {
346         if( !strcasecmp( "face", name ) )
347         {
348             free( psz_fontname );
349             psz_fontname = strdup( value );
350         }
351         else if( !strcasecmp( "size", name ) )
352         {
353             if( ( *value == '+' ) || ( *value == '-' ) )
354             {
355                 int i_value = atoi( value );
356
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;
363             }
364             else
365                 i_font_size = atoi( value );
366         }
367         else if( !strcasecmp( "color", name ) )
368         {
369             if( value[0] == '#' )
370             {
371                 i_font_color = strtol( value + 1, NULL, 16 );
372                 i_font_color &= 0x00ffffff;
373             }
374             else
375             {
376                 char *end;
377                 uint32_t i_value = strtol( value, &end, 16 );
378                 if( *end == '\0' || *end == ' ' )
379                     i_font_color = i_value & 0x00ffffff;
380
381                 for( int i = 0; p_html_colors[i].psz_name != NULL; i++ )
382                 {
383                     if( !strncasecmp( value, p_html_colors[i].psz_name, strlen(p_html_colors[i].psz_name) ) )
384                     {
385                         i_font_color = p_html_colors[i].i_value;
386                         break;
387                     }
388                 }
389             }
390         }
391         else if( !strcasecmp( "alpha", name ) && ( value[0] == '#' ) )
392         {
393             i_font_alpha = strtol( value + 1, NULL, 16 );
394             i_font_alpha &= 0xff;
395         }
396     }
397     rv = PushFont( p_fonts,
398                    psz_fontname,
399                    i_font_size,
400                    (i_font_color & 0xffffff) | ((i_font_alpha & 0xff) << 24),
401                    i_karaoke_bg_color );
402
403     free( psz_fontname );
404
405     return rv;
406 }
407
408 int HandleTT(font_stack_t **p_fonts, const char *p_fontfamily )
409 {
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;
414
415     /* Default all attributes to the top font in the stack -- in case not
416      * all attributes are specified in the sub-font
417      */
418     PeekFont( p_fonts,
419              &psz_unused_fontname,
420              &i_font_size,
421              &i_font_color,
422              &i_karaoke_bg_color );
423
424     /* Keep all the parent's font attributes, but change to a monospace font */
425     return PushFont( p_fonts,
426                    p_fontfamily,
427                    i_font_size,
428                    i_font_color,
429                    i_karaoke_bg_color );
430 }
431
432 /* Turn any multiple-whitespaces into single spaces */
433 void HandleWhiteSpace( char *psz_node )
434 {
435     char *s = strpbrk( psz_node, "\t\r\n " );
436     while( s )
437     {
438         int i_whitespace = strspn( s, "\t\r\n " );
439
440         if( i_whitespace > 1 )
441             memmove( &s[1],
442                      &s[i_whitespace],
443                      strlen( s ) - i_whitespace + 1 );
444         *s++ = ' ';
445
446         s = strpbrk( s, "\t\r\n " );
447     }
448 }
449
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,
454
455                            const char *psz_text_in,
456                            text_style_t *p_style,
457                            uint32_t i_k_date )
458 {
459     size_t i_string_length;
460
461     size_t i_string_bytes;
462     uni_char_t *psz_tmp = ToCharset( FREETYPE_TO_UCS, psz_text_in, &i_string_bytes );
463     if( psz_tmp )
464     {
465         memcpy( psz_text_out, psz_tmp, i_string_bytes );
466         i_string_length = i_string_bytes / sizeof( *psz_tmp );
467         free( psz_tmp );
468     }
469     else
470     {
471         msg_Warn( p_filter, "failed to convert string to unicode (%m)" );
472         i_string_length = 0;
473     }
474
475     if( i_string_length > 0 )
476     {
477         for( unsigned i = 0; i < i_string_length; i++ )
478             pp_styles[i] = p_style;
479     }
480     else
481     {
482         text_style_Delete( p_style );
483     }
484     if( i_string_length > 0 && pi_k_dates )
485     {
486         for( unsigned i = 0; i < i_string_length; i++ )
487             pi_k_dates[i] = i_k_date;
488     }
489     return i_string_length;
490 }
491
492 bool FaceStyleEquals( const text_style_t *p_style1,
493                              const text_style_t *p_style2 )
494 {
495     if( !p_style1 || !p_style2 )
496         return false;
497     if( p_style1 == p_style2 )
498         return true;
499
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 );
503 }
504
505 text_style_t *GetStyleFromFontStack( filter_t *p_filter,
506                                      font_stack_t **p_fonts,
507                                      text_style_t *style,
508                                      int i_style_flags )
509 {
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;
514
515     int         i_font_size  = style->i_font_size;
516     uint32_t    i_karaoke_bg_color = i_font_color;
517
518     if( PeekFont( p_fonts, &psz_fontname, &i_font_size,
519                   &i_font_color, &i_karaoke_bg_color ) )
520         return NULL;
521
522     return CreateStyle( psz_fontname, i_font_size, i_font_color,
523                         i_karaoke_bg_color,
524                         i_style_flags );
525 }
526
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,
531                          int *pi_len,
532                          xml_reader_t *p_xml_reader,
533                          text_style_t *p_font_style,
534                          text_style_t *p_default_style )
535 {
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;
540
541     int i_style_flags = 0;
542
543     if( p_font_style )
544     {
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);
548
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));
557
558         i_style_flags = p_font_style->i_style_flags & (STYLE_BOLD |
559                                                        STYLE_ITALIC |
560                                                        STYLE_UNDERLINE |
561                                                        STYLE_STRIKEOUT);
562     }
563     else
564     {
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,
571                        i_font_size,
572                        (i_font_color & 0xffffff) |
573                           ((i_font_alpha & 0xff) << 24),
574                        0x00ffffff );
575     }
576     if( p_default_style->i_style_flags & STYLE_BOLD )
577         i_style_flags |= STYLE_BOLD;
578
579     if( rv != VLC_SUCCESS )
580         return rv;
581
582     const char *node;
583     int type;
584
585     while ( (type = xml_ReaderNextNode( p_xml_reader, &node )) > 0 )
586     {
587         switch ( type )
588         {
589             case XML_READER_ENDELEM:
590                 if( !strcasecmp( "font", node ) )
591                     PopFont( &p_fonts );
592                 else if( !strcasecmp( "tt", node ) )
593                     PopFont( &p_fonts );
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;
602                 break;
603
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 ) )
618                 {
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,
623                                                 "\n",
624                                                 GetStyleFromFontStack( p_filter,
625                                                                        &p_fonts,
626                                                                        p_default_style,
627                                                                        i_style_flags ),
628                                                 i_k_date );
629                 }
630                 else if( !strcasecmp( "k", node ) )
631                 {
632                     /* Karaoke tags */
633                     const char *name, *value;
634                     while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
635                     {
636                         if( !strcasecmp( "t", name ) && value )
637                             i_k_date += atoi( value );
638                     }
639                 }
640                 break;
641
642             case XML_READER_TEXT:
643             {
644                 char *psz_node = strdup( node );
645                 if( unlikely(!psz_node) )
646                     break;
647
648                 HandleWhiteSpace( psz_node );
649                 resolve_xml_special_chars( psz_node );
650
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,
655                                             psz_node,
656                                             GetStyleFromFontStack( p_filter,
657                                                                    &p_fonts,
658                                                                    p_default_style,
659                                                                    i_style_flags ),
660                                             i_k_date );
661                 free( psz_node );
662                 break;
663             }
664         }
665     }
666
667     *pi_len = i_text_length;
668
669     while( VLC_SUCCESS == PopFont( &p_fonts ) );
670
671     return VLC_SUCCESS;
672 }
673
674