]> git.sesse.net Git - vlc/blob - modules/misc/text_renderer.h
First pass for extracting common text renderer code.
[vlc] / modules / misc / text_renderer.h
1 /*****************************************************************************
2  * text_renderer.h: common text renderer code
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Bernie Purcell <bitmap@videolan.org>
8  *          Laurent Aimar < fenrir AT videolan DOT org >
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 typedef struct font_stack_t font_stack_t;
26 struct font_stack_t
27 {
28     char          *psz_name;
29     int            i_size;
30     uint32_t       i_color;            /* ARGB */
31     uint32_t       i_karaoke_bg_color; /* ARGB */
32
33     font_stack_t  *p_next;
34 };
35
36 static int PushFont( font_stack_t **p_font, const char *psz_name, int i_size,
37                      uint32_t i_color, uint32_t i_karaoke_bg_color )
38 {
39     font_stack_t *p_new;
40
41     if( !p_font )
42         return VLC_EGENERIC;
43
44     p_new = malloc( sizeof( font_stack_t ) );
45     if( ! p_new )
46         return VLC_ENOMEM;
47
48     p_new->p_next = NULL;
49
50     if( psz_name )
51         p_new->psz_name = strdup( psz_name );
52     else
53         p_new->psz_name = NULL;
54
55     p_new->i_size              = i_size;
56     p_new->i_color             = i_color;
57     p_new->i_karaoke_bg_color  = i_karaoke_bg_color;
58
59     if( !*p_font )
60     {
61         *p_font = p_new;
62     }
63     else
64     {
65         font_stack_t *p_last;
66
67         for( p_last = *p_font;
68              p_last->p_next;
69              p_last = p_last->p_next )
70         ;
71
72         p_last->p_next = p_new;
73     }
74     return VLC_SUCCESS;
75 }
76
77 static int PopFont( font_stack_t **p_font )
78 {
79     font_stack_t *p_last, *p_next_to_last;
80
81     if( !p_font || !*p_font )
82         return VLC_EGENERIC;
83
84     p_next_to_last = NULL;
85     for( p_last = *p_font;
86          p_last->p_next;
87          p_last = p_last->p_next )
88     {
89         p_next_to_last = p_last;
90     }
91
92     if( p_next_to_last )
93         p_next_to_last->p_next = NULL;
94     else
95         *p_font = NULL;
96
97     free( p_last->psz_name );
98     free( p_last );
99
100     return VLC_SUCCESS;
101 }
102
103 static int PeekFont( font_stack_t **p_font, char **psz_name, int *i_size,
104                      uint32_t *i_color, uint32_t *i_karaoke_bg_color )
105 {
106     font_stack_t *p_last;
107
108     if( !p_font || !*p_font )
109         return VLC_EGENERIC;
110
111     for( p_last=*p_font;
112          p_last->p_next;
113          p_last=p_last->p_next )
114     ;
115
116     *psz_name            = p_last->psz_name;
117     *i_size              = p_last->i_size;
118     *i_color             = p_last->i_color;
119     *i_karaoke_bg_color  = p_last->i_karaoke_bg_color;
120
121     return VLC_SUCCESS;
122 }
123
124 static const struct {
125     const char *psz_name;
126     uint32_t   i_value;
127 } p_html_colors[] = {
128     /* Official html colors */
129     { "Aqua",    0x00FFFF },
130     { "Black",   0x000000 },
131     { "Blue",    0x0000FF },
132     { "Fuchsia", 0xFF00FF },
133     { "Gray",    0x808080 },
134     { "Green",   0x008000 },
135     { "Lime",    0x00FF00 },
136     { "Maroon",  0x800000 },
137     { "Navy",    0x000080 },
138     { "Olive",   0x808000 },
139     { "Purple",  0x800080 },
140     { "Red",     0xFF0000 },
141     { "Silver",  0xC0C0C0 },
142     { "Teal",    0x008080 },
143     { "White",   0xFFFFFF },
144     { "Yellow",  0xFFFF00 },
145
146     /* Common ones */
147     { "AliceBlue", 0xF0F8FF },
148     { "AntiqueWhite", 0xFAEBD7 },
149     { "Aqua", 0x00FFFF },
150     { "Aquamarine", 0x7FFFD4 },
151     { "Azure", 0xF0FFFF },
152     { "Beige", 0xF5F5DC },
153     { "Bisque", 0xFFE4C4 },
154     { "Black", 0x000000 },
155     { "BlanchedAlmond", 0xFFEBCD },
156     { "Blue", 0x0000FF },
157     { "BlueViolet", 0x8A2BE2 },
158     { "Brown", 0xA52A2A },
159     { "BurlyWood", 0xDEB887 },
160     { "CadetBlue", 0x5F9EA0 },
161     { "Chartreuse", 0x7FFF00 },
162     { "Chocolate", 0xD2691E },
163     { "Coral", 0xFF7F50 },
164     { "CornflowerBlue", 0x6495ED },
165     { "Cornsilk", 0xFFF8DC },
166     { "Crimson", 0xDC143C },
167     { "Cyan", 0x00FFFF },
168     { "DarkBlue", 0x00008B },
169     { "DarkCyan", 0x008B8B },
170     { "DarkGoldenRod", 0xB8860B },
171     { "DarkGray", 0xA9A9A9 },
172     { "DarkGrey", 0xA9A9A9 },
173     { "DarkGreen", 0x006400 },
174     { "DarkKhaki", 0xBDB76B },
175     { "DarkMagenta", 0x8B008B },
176     { "DarkOliveGreen", 0x556B2F },
177     { "Darkorange", 0xFF8C00 },
178     { "DarkOrchid", 0x9932CC },
179     { "DarkRed", 0x8B0000 },
180     { "DarkSalmon", 0xE9967A },
181     { "DarkSeaGreen", 0x8FBC8F },
182     { "DarkSlateBlue", 0x483D8B },
183     { "DarkSlateGray", 0x2F4F4F },
184     { "DarkSlateGrey", 0x2F4F4F },
185     { "DarkTurquoise", 0x00CED1 },
186     { "DarkViolet", 0x9400D3 },
187     { "DeepPink", 0xFF1493 },
188     { "DeepSkyBlue", 0x00BFFF },
189     { "DimGray", 0x696969 },
190     { "DimGrey", 0x696969 },
191     { "DodgerBlue", 0x1E90FF },
192     { "FireBrick", 0xB22222 },
193     { "FloralWhite", 0xFFFAF0 },
194     { "ForestGreen", 0x228B22 },
195     { "Fuchsia", 0xFF00FF },
196     { "Gainsboro", 0xDCDCDC },
197     { "GhostWhite", 0xF8F8FF },
198     { "Gold", 0xFFD700 },
199     { "GoldenRod", 0xDAA520 },
200     { "Gray", 0x808080 },
201     { "Grey", 0x808080 },
202     { "Green", 0x008000 },
203     { "GreenYellow", 0xADFF2F },
204     { "HoneyDew", 0xF0FFF0 },
205     { "HotPink", 0xFF69B4 },
206     { "IndianRed", 0xCD5C5C },
207     { "Indigo", 0x4B0082 },
208     { "Ivory", 0xFFFFF0 },
209     { "Khaki", 0xF0E68C },
210     { "Lavender", 0xE6E6FA },
211     { "LavenderBlush", 0xFFF0F5 },
212     { "LawnGreen", 0x7CFC00 },
213     { "LemonChiffon", 0xFFFACD },
214     { "LightBlue", 0xADD8E6 },
215     { "LightCoral", 0xF08080 },
216     { "LightCyan", 0xE0FFFF },
217     { "LightGoldenRodYellow", 0xFAFAD2 },
218     { "LightGray", 0xD3D3D3 },
219     { "LightGrey", 0xD3D3D3 },
220     { "LightGreen", 0x90EE90 },
221     { "LightPink", 0xFFB6C1 },
222     { "LightSalmon", 0xFFA07A },
223     { "LightSeaGreen", 0x20B2AA },
224     { "LightSkyBlue", 0x87CEFA },
225     { "LightSlateGray", 0x778899 },
226     { "LightSlateGrey", 0x778899 },
227     { "LightSteelBlue", 0xB0C4DE },
228     { "LightYellow", 0xFFFFE0 },
229     { "Lime", 0x00FF00 },
230     { "LimeGreen", 0x32CD32 },
231     { "Linen", 0xFAF0E6 },
232     { "Magenta", 0xFF00FF },
233     { "Maroon", 0x800000 },
234     { "MediumAquaMarine", 0x66CDAA },
235     { "MediumBlue", 0x0000CD },
236     { "MediumOrchid", 0xBA55D3 },
237     { "MediumPurple", 0x9370D8 },
238     { "MediumSeaGreen", 0x3CB371 },
239     { "MediumSlateBlue", 0x7B68EE },
240     { "MediumSpringGreen", 0x00FA9A },
241     { "MediumTurquoise", 0x48D1CC },
242     { "MediumVioletRed", 0xC71585 },
243     { "MidnightBlue", 0x191970 },
244     { "MintCream", 0xF5FFFA },
245     { "MistyRose", 0xFFE4E1 },
246     { "Moccasin", 0xFFE4B5 },
247     { "NavajoWhite", 0xFFDEAD },
248     { "Navy", 0x000080 },
249     { "OldLace", 0xFDF5E6 },
250     { "Olive", 0x808000 },
251     { "OliveDrab", 0x6B8E23 },
252     { "Orange", 0xFFA500 },
253     { "OrangeRed", 0xFF4500 },
254     { "Orchid", 0xDA70D6 },
255     { "PaleGoldenRod", 0xEEE8AA },
256     { "PaleGreen", 0x98FB98 },
257     { "PaleTurquoise", 0xAFEEEE },
258     { "PaleVioletRed", 0xD87093 },
259     { "PapayaWhip", 0xFFEFD5 },
260     { "PeachPuff", 0xFFDAB9 },
261     { "Peru", 0xCD853F },
262     { "Pink", 0xFFC0CB },
263     { "Plum", 0xDDA0DD },
264     { "PowderBlue", 0xB0E0E6 },
265     { "Purple", 0x800080 },
266     { "Red", 0xFF0000 },
267     { "RosyBrown", 0xBC8F8F },
268     { "RoyalBlue", 0x4169E1 },
269     { "SaddleBrown", 0x8B4513 },
270     { "Salmon", 0xFA8072 },
271     { "SandyBrown", 0xF4A460 },
272     { "SeaGreen", 0x2E8B57 },
273     { "SeaShell", 0xFFF5EE },
274     { "Sienna", 0xA0522D },
275     { "Silver", 0xC0C0C0 },
276     { "SkyBlue", 0x87CEEB },
277     { "SlateBlue", 0x6A5ACD },
278     { "SlateGray", 0x708090 },
279     { "SlateGrey", 0x708090 },
280     { "Snow", 0xFFFAFA },
281     { "SpringGreen", 0x00FF7F },
282     { "SteelBlue", 0x4682B4 },
283     { "Tan", 0xD2B48C },
284     { "Teal", 0x008080 },
285     { "Thistle", 0xD8BFD8 },
286     { "Tomato", 0xFF6347 },
287     { "Turquoise", 0x40E0D0 },
288     { "Violet", 0xEE82EE },
289     { "Wheat", 0xF5DEB3 },
290     { "White", 0xFFFFFF },
291     { "WhiteSmoke", 0xF5F5F5 },
292     { "Yellow", 0xFFFF00 },
293     { "YellowGreen", 0x9ACD32 },
294
295     { NULL, 0 }
296 };
297
298 static int HandleFontAttributes( xml_reader_t *p_xml_reader,
299                                   font_stack_t **p_fonts, int i_scale )
300 {
301     int        rv;
302     char      *psz_fontname = NULL;
303     uint32_t   i_font_color = 0xffffff;
304     int        i_font_alpha = 0;
305     uint32_t   i_karaoke_bg_color = 0x00ffffff;
306     int        i_font_size  = 24;
307
308     /* Default all attributes to the top font in the stack -- in case not
309      * all attributes are specified in the sub-font
310      */
311     if( VLC_SUCCESS == PeekFont( p_fonts,
312                                  &psz_fontname,
313                                  &i_font_size,
314                                  &i_font_color,
315                                  &i_karaoke_bg_color ))
316     {
317         psz_fontname = strdup( psz_fontname );
318         i_font_size = i_font_size * 1000 / i_scale;
319     }
320     i_font_alpha = (i_font_color >> 24) & 0xff;
321     i_font_color &= 0x00ffffff;
322
323     while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
324     {
325         char *psz_name = xml_ReaderName( p_xml_reader );
326         char *psz_value = xml_ReaderValue( p_xml_reader );
327
328         if( psz_name && psz_value )
329         {
330             if( !strcasecmp( "face", psz_name ) )
331             {
332                 free( psz_fontname );
333                 psz_fontname = strdup( psz_value );
334             }
335             else if( !strcasecmp( "size", psz_name ) )
336             {
337                 if( ( *psz_value == '+' ) || ( *psz_value == '-' ) )
338                 {
339                     int i_value = atoi( psz_value );
340
341                     if( ( i_value >= -5 ) && ( i_value <= 5 ) )
342                         i_font_size += ( i_value * i_font_size ) / 10;
343                     else if( i_value < -5 )
344                         i_font_size = - i_value;
345                     else if( i_value > 5 )
346                         i_font_size = i_value;
347                 }
348                 else
349                     i_font_size = atoi( psz_value );
350             }
351             else if( !strcasecmp( "color", psz_name ) )
352             {
353                 if( psz_value[0] == '#' )
354                 {
355                     i_font_color = strtol( psz_value + 1, NULL, 16 );
356                     i_font_color &= 0x00ffffff;
357                 }
358                 else
359                 {
360                     for( int i = 0; p_html_colors[i].psz_name != NULL; i++ )
361                     {
362                         if( !strncasecmp( psz_value, p_html_colors[i].psz_name, strlen(p_html_colors[i].psz_name) ) )
363                         {
364                             i_font_color = p_html_colors[i].i_value;
365                             break;
366                         }
367                     }
368                 }
369             }
370             else if( !strcasecmp( "alpha", psz_name ) &&
371                      ( psz_value[0] == '#' ) )
372             {
373                 i_font_alpha = strtol( psz_value + 1, NULL, 16 );
374                 i_font_alpha &= 0xff;
375             }
376         }
377         free( psz_name );
378         free( psz_value );
379     }
380     rv = PushFont( p_fonts,
381                    psz_fontname,
382                    i_font_size * i_scale / 1000,
383                    (i_font_color & 0xffffff) | ((i_font_alpha & 0xff) << 24),
384                    i_karaoke_bg_color );
385
386     free( psz_fontname );
387
388     return rv;
389 }
390