/src/vlc/modules/codec/webvtt/css_style.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * css_style.c : CSS styles conversions |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2017 VideoLabs, VLC authors and VideoLAN |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify it |
7 | | * under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation; either version 2.1 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with this program; if not, write to the Free Software Foundation, |
18 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
19 | | *****************************************************************************/ |
20 | | #ifdef HAVE_CONFIG_H |
21 | | # include "config.h" |
22 | | #endif |
23 | | |
24 | | #include <vlc_common.h> |
25 | | #include <vlc_text_style.h> |
26 | | |
27 | | #include "css_parser.h" |
28 | | #include "css_style.h" |
29 | | |
30 | | static void Color( vlc_css_term_t term, |
31 | | uint32_t *color, uint8_t *alpha, |
32 | | uint16_t *feat, int cflag, int aflag ) |
33 | 22.6k | { |
34 | 22.6k | if( unlikely( term.psz == NULL ) ) |
35 | 453 | return; |
36 | | |
37 | 22.1k | if( term.type == TYPE_FUNCTION ) |
38 | 4.31k | { |
39 | 4.31k | if( term.function ) /* func( expr ) */ |
40 | 3.77k | { |
41 | 3.77k | if( ( !strcmp( term.psz, "rgb" ) && term.function->i_count == 3 ) || |
42 | 2.26k | ( !strcmp( term.psz, "rgba" ) && term.function->i_count == 4 ) ) |
43 | 1.80k | { |
44 | 1.80k | *color = (((uint32_t)term.function->seq[0].term.val) << 16) | |
45 | 1.80k | (((uint32_t)term.function->seq[1].term.val) << 8) | |
46 | 1.80k | ((uint32_t)term.function->seq[2].term.val); |
47 | 1.80k | *feat |= cflag; |
48 | 1.80k | if( term.psz[3] != 0 ) /* rgba */ |
49 | 283 | { |
50 | 283 | *alpha = term.function->seq[3].term.val * STYLE_ALPHA_OPAQUE; |
51 | 283 | *feat |= aflag; |
52 | 283 | } |
53 | 1.80k | } |
54 | 3.77k | } |
55 | 4.31k | } |
56 | 17.8k | else if( term.type == TYPE_STRING || |
57 | 17.6k | term.type == TYPE_HEXCOLOR || |
58 | 15.0k | term.type == TYPE_IDENTIFIER ) |
59 | 17.6k | { |
60 | 17.6k | bool b_valid = false; |
61 | 17.6k | unsigned i_color = vlc_html_color( term.psz, &b_valid ); |
62 | 17.6k | if( b_valid ) |
63 | 12.2k | { |
64 | 12.2k | *alpha = (i_color & 0xFF000000) >> 24; |
65 | 12.2k | *color = i_color & 0x00FFFFFF; |
66 | 12.2k | *feat |= cflag|aflag; |
67 | 12.2k | } |
68 | 17.6k | } |
69 | 22.1k | } |
70 | | |
71 | | static void OutlineWidth( vlc_css_term_t term, text_style_t *p_style ) |
72 | 1.58k | { |
73 | 1.58k | if( term.type >= TYPE_PIXELS ) |
74 | 1.12k | { |
75 | 1.12k | p_style->i_outline_width = term.val; |
76 | 1.12k | p_style->i_style_flags |= STYLE_OUTLINE; |
77 | 1.12k | p_style->i_features |= STYLE_HAS_FLAGS; |
78 | 1.12k | } |
79 | 1.58k | } |
80 | | |
81 | | static void OutlineColor( vlc_css_term_t term, text_style_t *p_style ) |
82 | 970 | { |
83 | 970 | Color( term, &p_style->i_outline_color, &p_style->i_outline_alpha, |
84 | 970 | &p_style->i_features, STYLE_HAS_OUTLINE_COLOR, STYLE_HAS_OUTLINE_ALPHA ); |
85 | 970 | } |
86 | | |
87 | | static void ShadowDrop( vlc_css_term_t term, text_style_t *p_style ) |
88 | 2.11k | { |
89 | 2.11k | if( term.type >= TYPE_PIXELS ) |
90 | 1.91k | { |
91 | 1.91k | p_style->i_shadow_width = term.val; |
92 | 1.91k | p_style->i_style_flags |= STYLE_SHADOW; |
93 | 1.91k | p_style->i_features |= STYLE_HAS_FLAGS; |
94 | 1.91k | } |
95 | 2.11k | } |
96 | | |
97 | | static void ShadowColor( vlc_css_term_t term, text_style_t *p_style ) |
98 | 1.45k | { |
99 | 1.45k | Color( term, &p_style->i_shadow_color, &p_style->i_shadow_alpha, |
100 | 1.45k | &p_style->i_features, STYLE_HAS_SHADOW_COLOR, STYLE_HAS_SHADOW_ALPHA ); |
101 | 1.45k | } |
102 | | |
103 | | void webvtt_FillStyleFromCssDeclaration( const vlc_css_declaration_t *p_decl, text_style_t *p_style ) |
104 | 55.1k | { |
105 | 55.1k | if( !p_decl->psz_property || !p_style ) |
106 | 0 | return; |
107 | | |
108 | | /* Only support simple expressions for now */ |
109 | 55.1k | if( !p_decl->expr || p_decl->expr->i_count < 1 ) |
110 | 0 | return; |
111 | | |
112 | 55.1k | vlc_css_term_t term0 = p_decl->expr->seq[0].term; |
113 | | |
114 | 55.1k | if( !strcasecmp( p_decl->psz_property, "color" ) ) |
115 | 19.8k | { |
116 | 19.8k | Color( term0, &p_style->i_font_color, &p_style->i_font_alpha, |
117 | 19.8k | &p_style->i_features, STYLE_HAS_FONT_COLOR, STYLE_HAS_FONT_ALPHA ); |
118 | 19.8k | } |
119 | 35.2k | else if( !strcasecmp( p_decl->psz_property, "text-decoration" ) ) |
120 | 8.01k | { |
121 | 8.01k | if( term0.type == TYPE_STRING ) |
122 | 7.77k | { |
123 | 7.77k | if( !strcasecmp( term0.psz, "none" ) ) |
124 | 0 | { |
125 | 0 | p_style->i_style_flags &= ~(STYLE_STRIKEOUT|STYLE_UNDERLINE); |
126 | 0 | p_style->i_features |= STYLE_HAS_FLAGS; |
127 | 0 | } |
128 | 7.77k | else if( !strcasecmp( term0.psz, "line-through" ) ) |
129 | 0 | { |
130 | 0 | p_style->i_style_flags |= STYLE_STRIKEOUT; |
131 | 0 | p_style->i_features |= STYLE_HAS_FLAGS; |
132 | 0 | } |
133 | 7.77k | else if( !strcasecmp( term0.psz, "underline" ) ) |
134 | 221 | { |
135 | 221 | p_style->i_style_flags |= STYLE_UNDERLINE; |
136 | 221 | p_style->i_features |= STYLE_HAS_FLAGS; |
137 | 221 | } |
138 | 7.77k | } |
139 | 8.01k | } |
140 | 27.2k | else if( !strcasecmp( p_decl->psz_property, "text-shadow" ) ) |
141 | 2.11k | { |
142 | 2.11k | ShadowDrop( term0, p_style ); |
143 | 2.11k | if( p_decl->expr->i_count == 3 ) |
144 | 1.45k | ShadowColor( p_decl->expr->seq[2].term, p_style ); |
145 | 2.11k | } |
146 | 25.1k | else if( !strcasecmp( p_decl->psz_property, "background-color" ) ) |
147 | 349 | { |
148 | 349 | Color( term0, &p_style->i_background_color, &p_style->i_background_alpha, |
149 | 349 | &p_style->i_features, STYLE_HAS_BACKGROUND_COLOR, STYLE_HAS_BACKGROUND_ALPHA ); |
150 | 349 | p_style->i_style_flags |= STYLE_BACKGROUND; |
151 | 349 | p_style->i_features |= STYLE_HAS_FLAGS; |
152 | 349 | } |
153 | 24.8k | else if( !strcasecmp( p_decl->psz_property, "outline-color" ) ) |
154 | 377 | { |
155 | 377 | OutlineColor( term0, p_style ); |
156 | 377 | } |
157 | 24.4k | else if( !strcasecmp( p_decl->psz_property, "outline-width" ) ) |
158 | 481 | { |
159 | 481 | OutlineWidth( term0, p_style ); |
160 | 481 | } |
161 | 23.9k | else if( !strcasecmp( p_decl->psz_property, "outline" ) ) |
162 | 1.09k | { |
163 | 1.09k | OutlineWidth( term0, p_style ); |
164 | 1.09k | if( p_decl->expr->i_count == 3 ) |
165 | 593 | OutlineColor( p_decl->expr->seq[2].term, p_style ); |
166 | 1.09k | } |
167 | 22.8k | else if( !strcasecmp( p_decl->psz_property, "font-family" ) ) |
168 | 1.54k | { |
169 | 1.54k | vlc_css_term_t *term; |
170 | 1.54k | if( term0.type >= TYPE_STRING ) |
171 | 1.33k | { |
172 | 1.33k | size_t i_total = 1 + (p_decl->expr->i_count - 1) * 2; |
173 | 21.7k | for( size_t i=0; i<p_decl->expr->i_count; i++ ) |
174 | 20.4k | { |
175 | 20.4k | term = &p_decl->expr->seq[i].term; |
176 | 20.4k | if( term->type < TYPE_STRING ) |
177 | 15.9k | continue; |
178 | 4.50k | i_total += strlen( term->psz ); |
179 | 4.50k | if( term->type == TYPE_STRING ) |
180 | 2.05k | i_total += 2; |
181 | 4.50k | } |
182 | 1.33k | char *psz = malloc( i_total ); |
183 | 1.33k | if( psz ) |
184 | 1.33k | { |
185 | 1.33k | *psz = 0; |
186 | 21.7k | for( size_t i=0; i<p_decl->expr->i_count; i++ ) |
187 | 20.4k | { |
188 | 20.4k | term = &p_decl->expr->seq[i].term; |
189 | 20.4k | if( term->type < TYPE_STRING ) |
190 | 15.9k | continue; |
191 | 4.50k | if( i > 0 ) |
192 | 3.17k | strcat( psz, ", " ); |
193 | 4.50k | i_total += strlen( term->psz ); |
194 | 4.50k | if( term->type == TYPE_STRING ) |
195 | 2.05k | strcat( psz, "\"" ); |
196 | 4.50k | strcat( psz, term->psz ); |
197 | 4.50k | if( term->type == TYPE_STRING ) |
198 | 2.05k | strcat( psz, "\"" ); |
199 | 4.50k | } |
200 | 1.33k | free( p_style->psz_fontname ); |
201 | 1.33k | p_style->psz_fontname = psz; |
202 | 1.33k | } |
203 | | /*char *psz_font = NULL; |
204 | | const char *psz = strchr( term0.psz, ',' ); |
205 | | if( psz ) |
206 | | psz_font = strndup( term0.psz, psz - term0.psz + 1 ); |
207 | | else |
208 | | psz_font = strdup( term0.psz ); |
209 | | free( p_style->psz_fontname ); |
210 | | p_style->psz_fontname = vlc_css_unquoted( psz_font ); |
211 | | free( psz_font );*/ |
212 | 1.33k | } |
213 | 1.54k | } |
214 | 21.3k | else if( !strcasecmp( p_decl->psz_property, "font-style" ) ) |
215 | 1.22k | { |
216 | 1.22k | if( term0.type >= TYPE_STRING ) |
217 | 869 | { |
218 | 869 | if( !strcasecmp(term0.psz, "normal") ) |
219 | 210 | { |
220 | 210 | p_style->i_style_flags &= ~STYLE_ITALIC; |
221 | 210 | p_style->i_features |= STYLE_HAS_FLAGS; |
222 | 210 | } |
223 | 659 | else if( !strcasecmp(term0.psz, "italic") ) |
224 | 209 | { |
225 | 209 | p_style->i_style_flags |= STYLE_ITALIC; |
226 | 209 | p_style->i_features |= STYLE_HAS_FLAGS; |
227 | 209 | } |
228 | 869 | } |
229 | 1.22k | } |
230 | 20.0k | else if( !strcasecmp( p_decl->psz_property, "font-weight" ) ) |
231 | 1.68k | { |
232 | 1.68k | if( term0.type >= TYPE_STRING ) |
233 | 990 | { |
234 | 990 | if( !strcasecmp(term0.psz, "normal") ) |
235 | 217 | { |
236 | 217 | p_style->i_style_flags &= ~STYLE_BOLD; |
237 | 217 | p_style->i_features |= STYLE_HAS_FLAGS; |
238 | 217 | } |
239 | 990 | if( !strcasecmp(term0.psz, "bold") ) |
240 | 227 | { |
241 | 227 | p_style->i_style_flags |= STYLE_BOLD; |
242 | 227 | p_style->i_features |= STYLE_HAS_FLAGS; |
243 | 227 | } |
244 | 990 | } |
245 | 690 | else if( term0.type == TYPE_NONE ) |
246 | 481 | { |
247 | 481 | if( term0.val >= 700.0 ) |
248 | 234 | p_style->i_style_flags |= STYLE_BOLD; |
249 | 247 | else |
250 | 247 | p_style->i_style_flags &= ~STYLE_BOLD; |
251 | 481 | p_style->i_features |= STYLE_HAS_FLAGS; |
252 | 481 | } |
253 | 1.68k | } |
254 | 18.4k | else if( !strcasecmp( p_decl->psz_property, "font-size" ) ) |
255 | 2.21k | { |
256 | 2.21k | if( term0.type == TYPE_PIXELS ) |
257 | 796 | p_style->i_font_size = term0.val; |
258 | 1.41k | else if( term0.type == TYPE_EMS ) |
259 | 0 | p_style->f_font_relsize = term0.val * 5.33 / 1.06; |
260 | 1.41k | else if( term0.type == TYPE_PERCENT ) |
261 | 284 | p_style->f_font_relsize = term0.val * 5.33 / 100; |
262 | 2.21k | } |
263 | 16.2k | else if( !strcasecmp( p_decl->psz_property, "font" ) ) |
264 | 408 | { |
265 | | /* what to do ? */ |
266 | 408 | } |
267 | 15.7k | else if( !strcasecmp( p_decl->psz_property, "white-space" ) ) |
268 | 1.60k | { |
269 | 1.60k | if( term0.type >= TYPE_STRING ) |
270 | 1.33k | { |
271 | 1.33k | if( !strcasecmp(term0.psz, "normal" ) ) |
272 | 297 | p_style->e_wrapinfo = STYLE_WRAP_DEFAULT; |
273 | 1.33k | if( !strcasecmp(term0.psz, "nowrap" ) ) |
274 | 240 | p_style->e_wrapinfo = STYLE_WRAP_NONE; |
275 | 1.33k | } |
276 | 1.60k | } |
277 | 55.1k | } |