2 #include "media_query.h"
6 litehtml::media_query::media_query()
8 m_media_type
= media_type_all
;
12 litehtml::media_query::media_query( const media_query
& val
)
15 m_expressions
= val
.m_expressions
;
16 m_media_type
= val
.m_media_type
;
19 litehtml::media_query::ptr
litehtml::media_query::create_from_string(const string
& str
, const std::shared_ptr
<document
>& doc
)
21 media_query::ptr query
= std::make_shared
<media_query
>();
24 split_string(str
, tokens
, " \t\r\n", "", "(");
26 for(auto & token
: tokens
)
31 } else if(token
.at(0) == '(')
34 if(!token
.empty() && token
.at(token
.length() - 1) == ')')
36 token
.erase(token
.length() - 1, 1);
38 media_query_expression expr
;
39 string_vector expr_tokens
;
40 split_string(token
, expr_tokens
, ":");
41 if(!expr_tokens
.empty())
44 expr
.feature
= (media_feature
) value_index(expr_tokens
[0], media_feature_strings
, media_feature_none
);
45 if(expr
.feature
!= media_feature_none
)
47 if(expr_tokens
.size() == 1)
49 expr
.check_as_bool
= true;
53 expr
.check_as_bool
= false;
54 if(expr
.feature
== media_feature_orientation
)
56 expr
.val
= value_index(expr_tokens
[1], media_orientation_strings
, media_orientation_landscape
);
59 string::size_type slash_pos
= expr_tokens
[1].find('/');
60 if( slash_pos
!= string::npos
)
62 string val1
= expr_tokens
[1].substr(0, slash_pos
);
63 string val2
= expr_tokens
[1].substr(slash_pos
+ 1);
66 expr
.val
= atoi(val1
.c_str());
67 expr
.val2
= atoi(val2
.c_str());
71 length
.fromString(expr_tokens
[1]);
72 if(length
.units() == css_units_dpcm
|| length
.units() == css_units_dpi
)
74 expr
.val
= (int) (length
.val() * 2.54);
79 doc
->cvt_units(length
, doc
->container()->get_default_font_size());
81 expr
.val
= (int) length
.val();
86 query
->m_expressions
.push_back(expr
);
91 query
->m_media_type
= (media_type
) value_index(token
, media_type_strings
, media_type_all
);
99 bool litehtml::media_query::check( const media_features
& features
) const
102 if(m_media_type
== media_type_all
|| m_media_type
== features
.type
)
105 for(auto expression
: m_expressions
)
107 if(!expression
.check(features
))
123 //////////////////////////////////////////////////////////////////////////
125 litehtml::media_query_list::ptr
litehtml::media_query_list::create_from_string(const string
& str
, const std::shared_ptr
<document
>& doc
)
127 media_query_list::ptr list
= std::make_shared
<media_query_list
>();
129 string_vector tokens
;
130 split_string(str
, tokens
, ",");
132 for(auto & token
: tokens
)
137 litehtml::media_query::ptr query
= media_query::create_from_string(token
, doc
);
140 list
->m_queries
.push_back(query
);
143 if(list
->m_queries
.empty())
151 bool litehtml::media_query_list::apply_media_features( const media_features
& features
)
155 for(auto & query
: m_queries
)
157 if(query
->check(features
))
164 bool ret
= (apply
!= m_is_used
);
169 bool litehtml::media_query_expression::check( const media_features
& features
) const
173 case media_feature_width
:
176 return (features
.width
!= 0);
177 } else if(features
.width
== val
)
182 case media_feature_min_width
:
183 if(features
.width
>= val
)
188 case media_feature_max_width
:
189 if(features
.width
<= val
)
194 case media_feature_height
:
197 return (features
.height
!= 0);
198 } else if(features
.height
== val
)
203 case media_feature_min_height
:
204 if(features
.height
>= val
)
209 case media_feature_max_height
:
210 if(features
.height
<= val
)
216 case media_feature_device_width
:
219 return (features
.device_width
!= 0);
220 } else if(features
.device_width
== val
)
225 case media_feature_min_device_width
:
226 if(features
.device_width
>= val
)
231 case media_feature_max_device_width
:
232 if(features
.device_width
<= val
)
237 case media_feature_device_height
:
240 return (features
.device_height
!= 0);
241 } else if(features
.device_height
== val
)
246 case media_feature_min_device_height
:
247 if(features
.device_height
>= val
)
252 case media_feature_max_device_height
:
253 if(features
.device_height
<= val
)
259 case media_feature_orientation
:
260 if(features
.height
>= features
.width
)
262 if(val
== media_orientation_portrait
)
268 if(val
== media_orientation_landscape
)
274 case media_feature_aspect_ratio
:
275 if(features
.height
&& val2
)
277 int ratio_this
= round_d( (double) val
/ (double) val2
* 100 );
278 int ratio_feat
= round_d( (double) features
.width
/ (double) features
.height
* 100.0 );
279 if(ratio_this
== ratio_feat
)
285 case media_feature_min_aspect_ratio
:
286 if(features
.height
&& val2
)
288 int ratio_this
= round_d( (double) val
/ (double) val2
* 100 );
289 int ratio_feat
= round_d( (double) features
.width
/ (double) features
.height
* 100.0 );
290 if(ratio_feat
>= ratio_this
)
296 case media_feature_max_aspect_ratio
:
297 if(features
.height
&& val2
)
299 int ratio_this
= round_d( (double) val
/ (double) val2
* 100 );
300 int ratio_feat
= round_d( (double) features
.width
/ (double) features
.height
* 100.0 );
301 if(ratio_feat
<= ratio_this
)
308 case media_feature_device_aspect_ratio
:
309 if(features
.device_height
&& val2
)
311 int ratio_this
= round_d( (double) val
/ (double) val2
* 100 );
312 int ratio_feat
= round_d( (double) features
.device_width
/ (double) features
.device_height
* 100.0 );
313 if(ratio_feat
== ratio_this
)
319 case media_feature_min_device_aspect_ratio
:
320 if(features
.device_height
&& val2
)
322 int ratio_this
= round_d( (double) val
/ (double) val2
* 100 );
323 int ratio_feat
= round_d( (double) features
.device_width
/ (double) features
.device_height
* 100.0 );
324 if(ratio_feat
>= ratio_this
)
330 case media_feature_max_device_aspect_ratio
:
331 if(features
.device_height
&& val2
)
333 int ratio_this
= round_d( (double) val
/ (double) val2
* 100 );
334 int ratio_feat
= round_d( (double) features
.device_width
/ (double) features
.device_height
* 100.0 );
335 if(ratio_feat
<= ratio_this
)
342 case media_feature_color
:
345 return (features
.color
!= 0);
346 } else if(features
.color
== val
)
351 case media_feature_min_color
:
352 if(features
.color
>= val
)
357 case media_feature_max_color
:
358 if(features
.color
<= val
)
364 case media_feature_color_index
:
367 return (features
.color_index
!= 0);
368 } else if(features
.color_index
== val
)
373 case media_feature_min_color_index
:
374 if(features
.color_index
>= val
)
379 case media_feature_max_color_index
:
380 if(features
.color_index
<= val
)
386 case media_feature_monochrome
:
389 return (features
.monochrome
!= 0);
390 } else if(features
.monochrome
== val
)
395 case media_feature_min_monochrome
:
396 if(features
.monochrome
>= val
)
401 case media_feature_max_monochrome
:
402 if(features
.monochrome
<= val
)
408 case media_feature_resolution
:
409 if(features
.resolution
== val
)
414 case media_feature_min_resolution
:
415 if(features
.resolution
>= val
)
420 case media_feature_max_resolution
:
421 if(features
.resolution
<= val
)