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_none
);
98 bool litehtml::media_query::check( const media_features
& features
) const
101 if(m_media_type
== media_type_all
|| m_media_type
== features
.type
)
104 for(auto expression
: m_expressions
)
106 if(!expression
.check(features
))
122 //////////////////////////////////////////////////////////////////////////
124 litehtml::media_query_list::ptr
litehtml::media_query_list::create_from_string(const string
& str
, const std::shared_ptr
<document
>& doc
)
126 media_query_list::ptr list
= std::make_shared
<media_query_list
>();
128 string_vector tokens
;
129 split_string(str
, tokens
, ",");
131 for(auto & token
: tokens
)
136 litehtml::media_query::ptr query
= media_query::create_from_string(token
, doc
);
139 list
->m_queries
.push_back(query
);
142 if(list
->m_queries
.empty())
150 bool litehtml::media_query_list::apply_media_features( const media_features
& features
)
154 for(auto & query
: m_queries
)
156 if(query
->check(features
))
163 bool ret
= (apply
!= m_is_used
);
168 bool litehtml::media_query_expression::check( const media_features
& features
) const
172 case media_feature_width
:
175 return (features
.width
!= 0);
176 } else if(features
.width
== val
)
181 case media_feature_min_width
:
182 if(features
.width
>= val
)
187 case media_feature_max_width
:
188 if(features
.width
<= val
)
193 case media_feature_height
:
196 return (features
.height
!= 0);
197 } else if(features
.height
== val
)
202 case media_feature_min_height
:
203 if(features
.height
>= val
)
208 case media_feature_max_height
:
209 if(features
.height
<= val
)
215 case media_feature_device_width
:
218 return (features
.device_width
!= 0);
219 } else if(features
.device_width
== val
)
224 case media_feature_min_device_width
:
225 if(features
.device_width
>= val
)
230 case media_feature_max_device_width
:
231 if(features
.device_width
<= val
)
236 case media_feature_device_height
:
239 return (features
.device_height
!= 0);
240 } else if(features
.device_height
== val
)
245 case media_feature_min_device_height
:
246 if(features
.device_height
>= val
)
251 case media_feature_max_device_height
:
252 if(features
.device_height
<= val
)
258 case media_feature_orientation
:
259 if(features
.height
>= features
.width
)
261 if(val
== media_orientation_portrait
)
267 if(val
== media_orientation_landscape
)
273 case media_feature_aspect_ratio
:
274 if(features
.height
&& val2
)
276 int ratio_this
= round_d( (double) val
/ (double) val2
* 100 );
277 int ratio_feat
= round_d( (double) features
.width
/ (double) features
.height
* 100.0 );
278 if(ratio_this
== ratio_feat
)
284 case media_feature_min_aspect_ratio
:
285 if(features
.height
&& val2
)
287 int ratio_this
= round_d( (double) val
/ (double) val2
* 100 );
288 int ratio_feat
= round_d( (double) features
.width
/ (double) features
.height
* 100.0 );
289 if(ratio_feat
>= ratio_this
)
295 case media_feature_max_aspect_ratio
:
296 if(features
.height
&& val2
)
298 int ratio_this
= round_d( (double) val
/ (double) val2
* 100 );
299 int ratio_feat
= round_d( (double) features
.width
/ (double) features
.height
* 100.0 );
300 if(ratio_feat
<= ratio_this
)
307 case media_feature_device_aspect_ratio
:
308 if(features
.device_height
&& val2
)
310 int ratio_this
= round_d( (double) val
/ (double) val2
* 100 );
311 int ratio_feat
= round_d( (double) features
.device_width
/ (double) features
.device_height
* 100.0 );
312 if(ratio_feat
== ratio_this
)
318 case media_feature_min_device_aspect_ratio
:
319 if(features
.device_height
&& val2
)
321 int ratio_this
= round_d( (double) val
/ (double) val2
* 100 );
322 int ratio_feat
= round_d( (double) features
.device_width
/ (double) features
.device_height
* 100.0 );
323 if(ratio_feat
>= ratio_this
)
329 case media_feature_max_device_aspect_ratio
:
330 if(features
.device_height
&& val2
)
332 int ratio_this
= round_d( (double) val
/ (double) val2
* 100 );
333 int ratio_feat
= round_d( (double) features
.device_width
/ (double) features
.device_height
* 100.0 );
334 if(ratio_feat
<= ratio_this
)
341 case media_feature_color
:
344 return (features
.color
!= 0);
345 } else if(features
.color
== val
)
350 case media_feature_min_color
:
351 if(features
.color
>= val
)
356 case media_feature_max_color
:
357 if(features
.color
<= val
)
363 case media_feature_color_index
:
366 return (features
.color_index
!= 0);
367 } else if(features
.color_index
== val
)
372 case media_feature_min_color_index
:
373 if(features
.color_index
>= val
)
378 case media_feature_max_color_index
:
379 if(features
.color_index
<= val
)
385 case media_feature_monochrome
:
388 return (features
.monochrome
!= 0);
389 } else if(features
.monochrome
== val
)
394 case media_feature_min_monochrome
:
395 if(features
.monochrome
>= val
)
400 case media_feature_max_monochrome
:
401 if(features
.monochrome
<= val
)
407 case media_feature_resolution
:
408 if(features
.resolution
== val
)
413 case media_feature_min_resolution
:
414 if(features
.resolution
>= val
)
419 case media_feature_max_resolution
:
420 if(features
.resolution
<= val
)