1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2010 VLC authors and VideoLAN
7 * Author: basOS G <noxelia 4t gmail , com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_text_style.h>
34 text_style_t
*text_style_New( void )
36 return text_style_Create( STYLE_FULLY_SET
);
39 text_style_t
*text_style_Create( int i_defaults
)
41 text_style_t
*p_style
= calloc( 1, sizeof(*p_style
) );
45 if( i_defaults
== STYLE_NO_DEFAULTS
)
48 /* initialize to default text style (FIXME: by flag) */
49 p_style
->psz_fontname
= NULL
;
50 p_style
->psz_monofontname
= NULL
;
51 p_style
->i_features
= STYLE_FULLY_SET
;
52 p_style
->i_style_flags
= STYLE_OUTLINE
;
53 p_style
->f_font_relsize
= STYLE_DEFAULT_REL_FONT_SIZE
;
54 p_style
->i_font_size
= STYLE_DEFAULT_FONT_SIZE
;
55 p_style
->i_font_color
= 0xffffff;
56 p_style
->i_font_alpha
= STYLE_ALPHA_OPAQUE
;
57 p_style
->i_outline_color
= 0x000000;
58 p_style
->i_outline_alpha
= STYLE_ALPHA_OPAQUE
;
59 p_style
->i_shadow_color
= 0x808080;
60 p_style
->i_shadow_alpha
= STYLE_ALPHA_OPAQUE
;
61 p_style
->i_background_color
= 0x000000;
62 p_style
->i_background_alpha
= STYLE_ALPHA_OPAQUE
;
63 p_style
->i_karaoke_background_color
= 0xffffff;
64 p_style
->i_karaoke_background_alpha
= STYLE_ALPHA_OPAQUE
;
65 p_style
->i_outline_width
= 1;
66 p_style
->i_shadow_width
= 0;
67 p_style
->i_spacing
= -1;
68 p_style
->e_wrapinfo
= STYLE_WRAP_DEFAULT
;
73 text_style_t
*text_style_Copy( text_style_t
*p_dst
, const text_style_t
*p_src
)
81 if( p_src
->psz_fontname
)
82 p_dst
->psz_fontname
= strdup( p_src
->psz_fontname
);
84 if( p_src
->psz_monofontname
)
85 p_dst
->psz_monofontname
= strdup( p_src
->psz_monofontname
);
90 #define MERGE(var, fflag) \
91 if( (p_src->i_features & fflag) && (b_override || !(p_dst->i_features & fflag)) )\
92 p_dst->var = p_src->var
94 #define MERGE_SIZE(var) \
95 if( p_src->var > 0 && (b_override || p_dst->var <= 0) )\
96 p_dst->var = p_src->var
98 void text_style_Merge( text_style_t
*p_dst
, const text_style_t
*p_src
, bool b_override
)
100 if( p_src
->psz_fontname
&& (!p_dst
->psz_fontname
|| b_override
) )
102 free( p_dst
->psz_fontname
);
103 p_dst
->psz_fontname
= strdup( p_src
->psz_fontname
);
106 if( p_src
->psz_monofontname
&& (!p_dst
->psz_monofontname
|| b_override
) )
108 free( p_dst
->psz_monofontname
);
109 p_dst
->psz_monofontname
= strdup( p_src
->psz_monofontname
);
112 if( p_src
->i_features
!= STYLE_NO_DEFAULTS
)
114 MERGE(i_font_color
, STYLE_HAS_FONT_COLOR
);
115 MERGE(i_font_alpha
, STYLE_HAS_FONT_ALPHA
);
116 MERGE(i_outline_color
, STYLE_HAS_OUTLINE_COLOR
);
117 MERGE(i_outline_alpha
, STYLE_HAS_OUTLINE_ALPHA
);
118 MERGE(i_shadow_color
, STYLE_HAS_SHADOW_COLOR
);
119 MERGE(i_shadow_alpha
, STYLE_HAS_SHADOW_ALPHA
);
120 MERGE(i_background_color
, STYLE_HAS_BACKGROUND_COLOR
);
121 MERGE(i_background_alpha
, STYLE_HAS_BACKGROUND_ALPHA
);
122 MERGE(i_karaoke_background_color
, STYLE_HAS_K_BACKGROUND_COLOR
);
123 MERGE(i_karaoke_background_alpha
, STYLE_HAS_K_BACKGROUND_ALPHA
);
124 MERGE(e_wrapinfo
, STYLE_HAS_WRAP_INFO
);
125 p_dst
->i_features
|= p_src
->i_features
;
126 p_dst
->i_style_flags
|= p_src
->i_style_flags
;
129 MERGE_SIZE(f_font_relsize
);
130 MERGE_SIZE(i_font_size
);
131 MERGE_SIZE(i_outline_width
);
132 MERGE_SIZE(i_shadow_width
);
133 MERGE_SIZE(i_spacing
);
139 text_style_t
*text_style_Duplicate( const text_style_t
*p_src
)
144 text_style_t
*p_dst
= calloc( 1, sizeof(*p_dst
) );
146 text_style_Copy( p_dst
, p_src
);
150 void text_style_Delete( text_style_t
*p_style
)
153 free( p_style
->psz_fontname
);
155 free( p_style
->psz_monofontname
);
159 text_segment_t
*text_segment_New( const char *psz_text
)
161 text_segment_t
* segment
= calloc( 1, sizeof(*segment
) );
166 segment
->psz_text
= strdup( psz_text
);
171 text_segment_t
*text_segment_NewInheritStyle( const text_style_t
* p_style
)
174 return NULL
; //FIXME: Allow this, even if it is an alias to text_segment_New( NULL ) ?
175 text_segment_t
* p_segment
= text_segment_New( NULL
);
176 if ( unlikely( !p_segment
) )
178 p_segment
->style
= text_style_Duplicate( p_style
);
179 if ( unlikely( !p_segment
->style
) )
181 text_segment_Delete( p_segment
);
187 void text_segment_Delete( text_segment_t
* segment
)
189 if ( segment
!= NULL
)
191 free( segment
->psz_text
);
192 text_style_Delete( segment
->style
);
197 void text_segment_ChainDelete( text_segment_t
*segment
)
199 while( segment
!= NULL
)
201 text_segment_t
*p_next
= segment
->p_next
;
203 text_segment_Delete( segment
);
209 text_segment_t
*text_segment_Copy( text_segment_t
*p_src
)
211 text_segment_t
*p_dst
= NULL
, *p_dst0
= NULL
;
214 text_segment_t
*p_new
= text_segment_New( p_src
->psz_text
);
216 if( unlikely( !p_new
) )
219 p_new
->style
= text_style_Duplicate( p_src
->style
);
223 p_dst
= p_dst0
= p_new
;
227 p_dst
->p_next
= p_new
;
228 p_dst
= p_dst
->p_next
;
231 p_src
= p_src
->p_next
;
237 unsigned int vlc_html_color( const char *psz_value
, bool* ok
)
239 unsigned int color
= 0;
243 const char *psz_hex
= (*psz_value
== '#') ? psz_value
+ 1 : psz_value
;
245 if( psz_hex
!= psz_value
||
246 (*psz_hex
>= '0' && *psz_hex
<= '9') ||
247 (*psz_hex
>= 'A' && *psz_hex
<= 'F') )
249 uint32_t i_value
= strtol( psz_hex
, &psz_end
, 16 );
250 if( *psz_end
== 0 || isspace( *psz_end
) )
252 switch( psz_end
- psz_hex
)
255 color
= (i_value
<< 24) | (i_value
>> 8);
259 color
= i_value
| 0xFF000000;
268 if( !b_ret
&& psz_hex
== psz_value
&&
269 !strncmp( "rgb", psz_value
, 3 ) )
271 unsigned r
,g
,b
,a
= 0xFF;
272 if( psz_value
[3] == 'a' )
273 b_ret
= (sscanf( psz_value
, "rgba(%3u,%3u,%3u,%3u)", &r
, &g
, &b
, &a
) == 4);
275 b_ret
= (sscanf( psz_value
, "rgb(%3u,%3u,%3u)", &r
, &g
, &b
) == 3);
276 color
= (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
279 if( !b_ret
&& psz_hex
== psz_value
)
281 for( int i
= 0; p_html_colors
[i
].psz_name
!= NULL
; i
++ )
283 if( !strcasecmp( psz_value
, p_html_colors
[i
].psz_name
) )
285 // Assume opaque color since the table doesn't specify an alpha
286 color
= p_html_colors
[i
].i_value
| 0xFF000000;