Skip to content

Commit d19ab52

Browse files
author
Matthew Allen
committed
Add linear gradient css support.
1 parent ba9df3d commit d19ab52

File tree

11 files changed

+94
-7
lines changed

11 files changed

+94
-7
lines changed

include/litehtml/background.h

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,38 @@
99

1010
namespace litehtml
1111
{
12+
class gradient
13+
{
14+
public:
15+
enum gradient_type
16+
{
17+
no_gradient,
18+
linear_gradient,
19+
radial_gradient,
20+
};
21+
gradient_type m_type = no_gradient;
22+
web_color_vector m_colors;
23+
24+
gradient(gradient_type type = no_gradient)
25+
{
26+
m_type = type;
27+
}
28+
29+
bool is_empty() const
30+
{
31+
return m_type == no_gradient || m_colors.size() == 0;
32+
}
33+
34+
static gradient transparent;
35+
};
36+
1237
class background
1338
{
1439
public:
1540
string_vector m_image;
1641
string m_baseurl;
1742
web_color m_color;
43+
gradient m_gradient;
1844
int_vector m_attachment;
1945
length_vector m_position_x;
2046
length_vector m_position_y;
@@ -25,8 +51,10 @@ namespace litehtml
2551

2652
bool is_empty() const
2753
{
28-
if(m_color.alpha != 0) return false;
29-
if(m_image.empty()) return true;
54+
if(m_color.alpha != 0 || m_gradient.m_type != gradient::no_gradient)
55+
return false;
56+
if(m_image.empty())
57+
return true;
3058
for(const auto& img : m_image)
3159
{
3260
if(!img.empty()) return false;
@@ -43,6 +71,7 @@ namespace litehtml
4371
background_attachment attachment;
4472
background_repeat repeat;
4573
web_color color;
74+
gradient gradient;
4675
position clip_box;
4776
position origin_box;
4877
position border_box;

include/litehtml/element.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ namespace litehtml
106106
virtual int get_int_property (string_id name, bool inherited, int default_value, uint_ptr css_properties_member_offset) const;
107107
virtual css_length get_length_property(string_id name, bool inherited, css_length default_value, uint_ptr css_properties_member_offset) const;
108108
virtual web_color get_color_property (string_id name, bool inherited, web_color default_value, uint_ptr css_properties_member_offset) const;
109+
virtual gradient get_gradient_property (string_id name, bool inherited, gradient default_value, uint_ptr css_properties_member_offset) const;
109110
virtual string get_string_property(string_id name, bool inherited, const string& default_value, uint_ptr css_properties_member_offset) const;
110111
virtual float get_number_property(string_id name, bool inherited, float default_value, uint_ptr css_properties_member_offset) const;
111112
virtual string_vector get_string_vector_property(string_id name, bool inherited, const string_vector& default_value, uint_ptr css_properties_member_offset) const;

include/litehtml/html_tag.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ namespace litehtml
7575
int get_int_property (string_id name, bool inherited, int default_value, uint_ptr css_properties_member_offset) const override;
7676
css_length get_length_property(string_id name, bool inherited, css_length default_value, uint_ptr css_properties_member_offset) const override;
7777
web_color get_color_property (string_id name, bool inherited, web_color default_value, uint_ptr css_properties_member_offset) const override;
78+
gradient get_gradient_property(string_id name, bool inherited, litehtml::gradient default_value, uint_ptr css_properties_member_offset) const override;
7879
string get_string_property(string_id name, bool inherited, const string& default_value, uint_ptr css_properties_member_offset) const override;
7980
float get_number_property(string_id name, bool inherited, float default_value, uint_ptr css_properties_member_offset) const override;
8081
string_vector get_string_vector_property(string_id name, bool inherited, const string_vector& default_value, uint_ptr css_properties_member_offset) const override;

include/litehtml/string_id.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ STRING_ID(
172172
_background_position_,
173173
_background_position_x_,
174174
_background_position_y_,
175+
_background_gradient_,
175176

176177
_border_,
177178
_border_width_,

include/litehtml/style.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace litehtml
1414
prop_type_length_vector,
1515
prop_type_number,
1616
prop_type_color,
17+
prop_type_gradient,
1718
prop_type_string,
1819
prop_type_string_vector,
1920
prop_type_size_vector,
@@ -34,6 +35,7 @@ namespace litehtml
3435
length_vector m_length_vector;
3536
float m_number;
3637
web_color m_color;
38+
gradient m_gradient;
3739
string m_string;
3840
string_vector m_string_vector;
3941
size_vector m_size_vector;
@@ -79,6 +81,10 @@ namespace litehtml
7981
: m_type(prop_type_color), m_important(important), m_color(color)
8082
{
8183
}
84+
property_value(gradient grad, bool important)
85+
: m_type(prop_type_gradient), m_important(important), m_gradient(grad)
86+
{
87+
}
8288
property_value(const size_vector& vec, bool important)
8389
: m_type(prop_type_size_vector), m_important(important), m_size_vector(vec)
8490
{
@@ -106,6 +112,9 @@ namespace litehtml
106112
case prop_type_color:
107113
m_color.~web_color();
108114
break;
115+
case prop_type_gradient:
116+
m_gradient.~gradient();
117+
break;
109118
case prop_type_size_vector:
110119
m_size_vector.~size_vector();
111120
break;
@@ -150,6 +159,9 @@ namespace litehtml
150159
case prop_type_color:
151160
new(this) property_value(val.m_color, val.m_important);
152161
break;
162+
case prop_type_gradient:
163+
new(this) property_value(val.m_gradient, val.m_important);
164+
break;
153165
case prop_type_size_vector:
154166
new(this) property_value(val.m_size_vector, val.m_important);
155167
break;

include/litehtml/web_color.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ namespace litehtml
5858
return {(byte) v_red, (byte) v_green, (byte) v_blue, alpha};
5959
}
6060
};
61+
62+
typedef std::vector<web_color> web_color_vector;
6163
}
6264

6365
#endif // LH_WEB_COLOR_H

src/css_properties.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ void litehtml::css_properties::compute_background(const element* el, const docum
360360
int font_size = get_font_size();
361361

362362
m_bg.m_color = el->get_color_property(_background_color_, false, web_color::transparent, offset(m_bg.m_color));
363+
m_bg.m_gradient = el->get_gradient_property(_background_gradient_, false, gradient::transparent, offset(m_bg.m_gradient));
364+
if (!m_bg.m_gradient.is_empty())
365+
{
366+
int asd=0;
367+
}
363368

364369
const css_size auto_auto(css_length::predef_value(background_size_auto), css_length::predef_value(background_size_auto));
365370
m_bg.m_position_x = el->get_length_vector_property(_background_position_x_, false, { css_length(0, css_units_percentage) }, offset(m_bg.m_position_x));

src/element.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ int element::get_enum_property (string_id /*name*/, bool /*inherited*/, int
461461
int element::get_int_property (string_id /*name*/, bool /*inherited*/, int /*defval*/, uint_ptr /*css_properties_member_offset*/) const LITEHTML_RETURN_FUNC(0)
462462
css_length element::get_length_property (string_id /*name*/, bool /*inherited*/, css_length /*defval*/, uint_ptr /*css_properties_member_offset*/) const LITEHTML_RETURN_FUNC(0)
463463
web_color element::get_color_property (string_id /*name*/, bool /*inherited*/, web_color /*defval*/, uint_ptr /*css_properties_member_offset*/) const LITEHTML_RETURN_FUNC(web_color())
464+
gradient element::get_gradient_property (string_id /*name*/, bool /*inherited*/, gradient /*defval*/, uint_ptr /*css_properties_member_offset*/) const LITEHTML_RETURN_FUNC(gradient())
464465
string element::get_string_property (string_id /*name*/, bool /*inherited*/, const string& /*defval*/, uint_ptr /*css_properties_member_offset*/) const LITEHTML_RETURN_FUNC("")
465466
float element::get_number_property (string_id /*name*/, bool /*inherited*/, float /*defval*/, uint_ptr /*css_properties_member_offset*/) const LITEHTML_RETURN_FUNC(0)
466467
string_vector element::get_string_vector_property (string_id /*name*/, bool /*inherited*/, const string_vector& /*default_value*/, uint_ptr /*css_properties_member_offset*/) const LITEHTML_RETURN_FUNC({})

src/html_tag.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,11 @@ litehtml::web_color litehtml::html_tag::get_color_property(string_id name, bool
382382
return get_property_impl<web_color, prop_type_color, &property_value::m_color>(name, inherited, default_value, css_properties_member_offset);
383383
}
384384

385+
litehtml::gradient litehtml::html_tag::get_gradient_property(string_id name, bool inherited, litehtml::gradient default_value, uint_ptr css_properties_member_offset) const
386+
{
387+
return get_property_impl<gradient, prop_type_gradient, &property_value::m_gradient>(name, inherited, default_value, css_properties_member_offset);
388+
}
389+
385390
litehtml::string litehtml::html_tag::get_string_property(string_id name, bool inherited, const string& default_value, uint_ptr css_properties_member_offset) const
386391
{
387392
return get_property_impl<string, prop_type_string, &property_value::m_string>(name, inherited, default_value, css_properties_member_offset);
@@ -1113,6 +1118,7 @@ void litehtml::html_tag::init_background_paint(position pos, std::vector<backgro
11131118
}
11141119

11151120
bg_paint.back().color = bg->m_color;
1121+
bg_paint.back().gradient = bg->m_gradient;
11161122
}
11171123

11181124
void litehtml::html_tag::init_one_background_paint(int i, position pos, background_paint& bg_paint, const background* bg, const std::shared_ptr<render_item>& ri)

src/style.cpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ void style::parse_background(const string& val, const string& baseurl, bool impo
623623
int_vector repeats, origins, clips, attachments;
624624
length_vector x_positions, y_positions;
625625
size_vector sizes;
626+
gradient grad;
626627

627628
for (const auto& token : tokens)
628629
{
@@ -631,6 +632,7 @@ void style::parse_background(const string& val, const string& baseurl, bool impo
631632
return;
632633

633634
color = bg.m_color;
635+
grad = bg.m_gradient;
634636
images.push_back(bg.m_image[0]);
635637
repeats.push_back(bg.m_repeat[0]);
636638
origins.push_back(bg.m_origin[0]);
@@ -642,6 +644,7 @@ void style::parse_background(const string& val, const string& baseurl, bool impo
642644
}
643645

644646
add_parsed_property(_background_color_, property_value(color, important));
647+
add_parsed_property(_background_gradient_, property_value(grad, important));
645648
add_parsed_property(_background_image_, property_value(images, important));
646649
add_parsed_property(_background_image_baseurl_, property_value(baseurl, important));
647650
add_parsed_property(_background_repeat_, property_value(repeats, important));
@@ -691,17 +694,20 @@ bool style::parse_one_background(const string& val, document_container* containe
691694
css::parse_css_url(token, url);
692695
bg.m_image = { url };
693696
image_found = true;
694-
} else if( (idx = value_index(token, background_repeat_strings)) >= 0 )
697+
}
698+
else if( (idx = value_index(token, background_repeat_strings)) >= 0 )
695699
{
696700
if (repeat_found) return false;
697701
bg.m_repeat = { idx };
698702
repeat_found = true;
699-
} else if( (idx = value_index(token, background_attachment_strings)) >= 0 )
703+
}
704+
else if( (idx = value_index(token, background_attachment_strings)) >= 0 )
700705
{
701706
if (attachment_found) return false;
702707
bg.m_attachment = { idx };
703708
attachment_found = true;
704-
} else if( (idx = value_index(token, background_box_strings)) >= 0 )
709+
}
710+
else if( (idx = value_index(token, background_box_strings)) >= 0 )
705711
{
706712
if(!origin_found)
707713
{
@@ -713,20 +719,41 @@ bool style::parse_one_background(const string& val, document_container* containe
713719
bg.m_clip = { idx };
714720
clip_found = true;
715721
}
716-
} else if( value_in_list(token, background_position_strings) ||
722+
}
723+
else if( value_in_list(token, background_position_strings) ||
717724
token.find('/') != string::npos ||
718725
t_isdigit(token[0]) ||
719726
token[0] == '+' ||
720727
token[0] == '-' ||
721728
token[0] == '.' )
722729
{
723730
position += " " + token;
724-
} else if (web_color::is_color(token, container))
731+
}
732+
else if (web_color::is_color(token, container))
725733
{
726734
if (color_found) return false;
727735
bg.m_color = web_color::from_string(token, container);
728736
color_found = true;
729737
}
738+
else if (token.substr(0, 15) == "linear-gradient")
739+
{
740+
string_vector gradient;
741+
split_string(token, gradient, "()", "", "");
742+
if (gradient.size() == 2)
743+
{
744+
auto arg = gradient[1];
745+
746+
bg.m_gradient.m_type = gradient::linear_gradient;
747+
748+
string_vector colors;
749+
split_string(gradient[1], colors, ", ");
750+
for (auto c: colors)
751+
{
752+
if (web_color::is_color(c, container))
753+
bg.m_gradient.m_colors.push_back(web_color::from_string(c, container));
754+
}
755+
}
756+
}
730757
else
731758
{
732759
return false;

0 commit comments

Comments
 (0)