Skip to content

Commit e077b97

Browse files
NiuGuohuitordex
authored andcommitted
fix: support text-emphasis all feature.
1 parent 770d55c commit e077b97

File tree

8 files changed

+125
-2
lines changed

8 files changed

+125
-2
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
Standard: c++17
55
TabWidth: 4
66
UseTab: Always
7+
IndentWidth: 4

include/litehtml/css_properties.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ namespace litehtml
6868
text_decoration_style m_text_decoration_style = text_decoration_style_solid;
6969
css_length m_text_decoration_thickness;
7070
web_color m_text_decoration_color;
71-
string m_text_emphasis;
71+
string m_text_emphasis_style;
72+
web_color m_text_emphasis_color;
73+
int m_text_emphasis_position;
7274
font_metrics m_font_metrics;
7375
text_transform m_text_transform;
7476
web_color m_color;
@@ -285,6 +287,10 @@ namespace litehtml
285287
text_decoration_style get_text_decoration_style() const;
286288
const css_length& get_text_decoration_thickness() const;
287289
const web_color& get_text_decoration_color() const;
290+
291+
string get_text_emphasis_style() const;
292+
web_color get_text_emphasis_color() const;
293+
int get_text_emphasis_position() const;
288294
};
289295

290296
inline element_position css_properties::get_position() const
@@ -725,6 +731,21 @@ namespace litehtml
725731
{
726732
return m_text_decoration_color;
727733
}
734+
735+
inline string css_properties::get_text_emphasis_style() const
736+
{
737+
return m_text_emphasis_style;
738+
}
739+
740+
inline web_color css_properties::get_text_emphasis_color() const
741+
{
742+
return m_text_emphasis_color;
743+
}
744+
745+
inline int css_properties::get_text_emphasis_position() const
746+
{
747+
return m_text_emphasis_position;
748+
}
728749
}
729750

730751
#endif //LITEHTML_CSS_PROPERTIES_H

include/litehtml/font_description.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ namespace litehtml
1818
css_length decoration_thickness; // Decoration line thickness in pixels. See predefined values in enumtext_decoration_thickness
1919
text_decoration_style decoration_style = text_decoration_style_solid; // Decoration line style. See enum text_decoration_style
2020
web_color decoration_color = web_color::current_color; // Decoration line color
21+
std::string emphasis_style; // Text emphasis style
22+
web_color emphasis_color = web_color::current_color; // Text emphasis color
23+
int emphasis_position = text_emphasis_position_over; // Text emphasis position
2124

2225
std::string hash() const
2326
{
@@ -30,6 +33,9 @@ namespace litehtml
3033
out += ":dt=" + decoration_thickness.to_string();
3134
out += ":ds=" + std::to_string(decoration_style);
3235
out += ":dc=" + decoration_color.to_string();
36+
out += ":ephs=" + emphasis_style;
37+
out += ":ephc=" + emphasis_color.to_string();
38+
out += ":ephp=" + std::to_string(emphasis_position);
3339

3440
return out;
3541
}

include/litehtml/string_id.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ STRING_ID(
257257
_text_decoration_color_,
258258
_text_decoration_thickness_,
259259
_text_emphasis_,
260+
_text_emphasis_style_,
261+
_text_emphasis_color_,
262+
_text_emphasis_position_,
260263

261264
_white_space_,
262265
_text_align_,

include/litehtml/style.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ namespace litehtml
8686
bool parse_text_decoration_color(const css_token& token, bool important, document_container* container);
8787
void parse_text_decoration_line(const css_token_vector& tokens, bool important);
8888

89+
void parse_text_emphasis(const css_token_vector& tokens, bool important, document_container* container);
90+
bool parse_text_emphasis_color(const css_token& token, bool important, document_container* container);
91+
void parse_text_emphasis_position(const css_token_vector& tokens, bool important);
92+
8993
void parse_flex_flow(const css_token_vector& tokens, bool important);
9094
void parse_flex(const css_token_vector& tokens, bool important);
9195
void parse_align_self(string_id name, const css_token_vector& tokens, bool important);

include/litehtml/types.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ namespace litehtml
8080
text_decoration_thickness_from_font,
8181
};
8282

83+
#define style_text_emphasis_position_strings "over;under;left;right"
84+
85+
enum text_emphasis_position
86+
{
87+
text_emphasis_position_over = 0x00,
88+
text_emphasis_position_under = 0x01,
89+
text_emphasis_position_left = 0x02,
90+
text_emphasis_position_right = 0x04,
91+
};
92+
8393

8494
using byte = unsigned char;
8595
using ucode_t = unsigned int;

src/css_properties.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ void litehtml::css_properties::compute_font(const html_tag* el, const document::
362362
m_font_family = el->get_property<string>( _font_family_, true, doc->container()->get_default_font_name(), offset(m_font_family));
363363
m_font_weight = el->get_property<css_length>(_font_weight_, true, css_length::predef_value(font_weight_normal), offset(m_font_weight));
364364
m_font_style = (font_style) el->get_property<int>( _font_style_, true, font_style_normal, offset(m_font_style));
365-
m_text_emphasis = el->get_property<string>(_text_emphasis_, true, "none", offset(m_text_emphasis));
366365
bool propagate_decoration = !is_one_of(m_display, display_inline_block, display_inline_table, display_inline_flex) &&
367366
m_float == float_none && !is_one_of(m_el_position, element_position_absolute, element_position_fixed);
368367

@@ -385,6 +384,23 @@ void litehtml::css_properties::compute_font(const html_tag* el, const document::
385384
m_text_decoration_color = web_color::current_color;
386385
}
387386

387+
// text-emphasis
388+
m_text_emphasis_style = el->get_property<string>(_text_emphasis_style_, true, "unset", offset(m_text_emphasis_style));
389+
m_text_emphasis_position = el->get_property<int>(_text_emphasis_position_, true, text_emphasis_position_over, offset(m_text_emphasis_position));
390+
m_text_emphasis_color = get_color_property(el, _text_emphasis_color_, true, web_color::current_color, offset(m_text_emphasis_color));
391+
392+
if(el->parent())
393+
{
394+
if(m_text_emphasis_style == "initial" || m_text_emphasis_style == "unset")
395+
{
396+
m_text_emphasis_style = el->parent()->css().get_text_emphasis_style();
397+
}
398+
if(m_text_emphasis_color == web_color::current_color)
399+
{
400+
m_text_emphasis_color = el->parent()->css().get_text_emphasis_color();
401+
}
402+
m_text_emphasis_position |= el->parent()->css().get_text_emphasis_position();
403+
}
388404

389405
if(m_font_weight.is_predefined())
390406
{
@@ -424,6 +440,9 @@ void litehtml::css_properties::compute_font(const html_tag* el, const document::
424440
descr.decoration_thickness = m_text_decoration_thickness;
425441
descr.decoration_style = m_text_decoration_style;
426442
descr.decoration_color = m_text_decoration_color;
443+
descr.emphasis_style = m_text_emphasis_style;
444+
descr.emphasis_color = m_text_emphasis_color;
445+
descr.emphasis_position = m_text_emphasis_position;
427446

428447
m_font = doc->get_font(descr, &m_font_metrics);
429448
}

src/style.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ std::map<string_id, string> style::m_valid_values =
6565
{ _caption_side_, caption_side_strings },
6666

6767
{ _text_decoration_style_, style_text_decoration_style_strings },
68+
{ _text_emphasis_position_, style_text_emphasis_position_strings },
6869
};
6970

7071
std::map<string_id, vector<string_id>> shorthands =
@@ -106,6 +107,7 @@ std::map<string_id, vector<string_id>> shorthands =
106107
{ _flex_flow_, {_flex_direction_, _flex_wrap_}},
107108

108109
{ _text_decoration_, {_text_decoration_color_, _text_decoration_line_, _text_decoration_style_, _text_decoration_thickness_}},
110+
{ _text_emphasis_, {_text_emphasis_style_, _text_emphasis_color_}},
109111
};
110112

111113
void style::add(const string& txt, const string& baseurl, document_container* container)
@@ -447,10 +449,22 @@ void style::add_property(string_id name, const css_token_vector& value, const st
447449
break;
448450

449451
case _text_emphasis_:
452+
parse_text_emphasis(value, important, container);
453+
break;
454+
455+
case _text_emphasis_style_:
450456
str = get_repr(value, 0, -1, true);
451457
add_parsed_property(name, property_value(str, important));
452458
break;
453459

460+
case _text_emphasis_color_:
461+
parse_text_emphasis_color(val, important, container);
462+
break;
463+
464+
case _text_emphasis_position_:
465+
parse_text_emphasis_position(value, important);
466+
break;
467+
454468
// ============================= FLEX =============================
455469

456470
case _flex_:
@@ -1322,6 +1336,51 @@ void style::parse_text_decoration_line(const css_token_vector& tokens, bool impo
13221336
add_parsed_property(_text_decoration_line_, property_value(val, important));
13231337
}
13241338

1339+
void style::parse_text_emphasis(const css_token_vector& tokens, bool important, document_container *container) {
1340+
string style;
1341+
for(const auto& token : std::vector(tokens.rbegin(), tokens.rend()))
1342+
{
1343+
if(parse_text_emphasis_color(token, important, container)) continue;
1344+
style.insert(0, token.str + " ");
1345+
}
1346+
if (!style.empty())
1347+
{
1348+
add_parsed_property(_text_emphasis_style_, property_value(style, important));
1349+
}
1350+
}
1351+
1352+
bool style::parse_text_emphasis_color(const css_token &token, bool important, document_container *container)
1353+
{
1354+
web_color _color;
1355+
if(parse_color(token, _color, container))
1356+
{
1357+
add_parsed_property(_text_emphasis_color_, property_value(_color, important));
1358+
return true;
1359+
}
1360+
if(token.type == IDENT && value_in_list(token.ident(), "auto;currentcolor"))
1361+
{
1362+
add_parsed_property(_text_emphasis_color_, property_value(web_color::current_color, important));
1363+
return true;
1364+
}
1365+
return false;
1366+
}
1367+
1368+
void style::parse_text_emphasis_position(const css_token_vector &tokens, bool important)
1369+
{
1370+
int val = 0;
1371+
for(const auto& token : tokens)
1372+
{
1373+
if(token.type == IDENT)
1374+
{
1375+
int idx = value_index(token.ident(), style_text_emphasis_position_strings);
1376+
if(idx >= 0)
1377+
{
1378+
val |= 1 << (idx - 1);
1379+
}
1380+
}
1381+
}
1382+
add_parsed_property(_text_emphasis_position_, property_value(val, important));
1383+
}
13251384

13261385
// https://developer.mozilla.org/en-US/docs/Web/CSS/flex
13271386
// https://drafts.csswg.org/css-flexbox/#flex-property

0 commit comments

Comments
 (0)