Skip to content

Commit 013b065

Browse files
committed
Added support for custom color names.
1 parent 6b96701 commit 013b065

File tree

7 files changed

+90
-18
lines changed

7 files changed

+90
-18
lines changed

containers/cairo/cairo_container.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _USE_MATH_DEFINES
33
#include <math.h>
44
#include "cairo_font.h"
5+
#include <strsafe.h>
56

67
cairo_container::cairo_container(void)
78
{
@@ -962,3 +963,65 @@ void cairo_container::transform_text( litehtml::tstring& text, litehtml::text_tr
962963
void cairo_container::link(const std::shared_ptr<litehtml::document>& doc, const litehtml::element::ptr& el)
963964
{
964965
}
966+
967+
litehtml::tstring cairo_container::resolve_color(const litehtml::tstring& color) const
968+
{
969+
struct custom_color
970+
{
971+
litehtml::tchar_t* name;
972+
int color_index;
973+
};
974+
975+
static custom_color colors[] = {
976+
{ _t("ActiveBorder"), COLOR_ACTIVEBORDER},
977+
{ _t("ActiveCaption"), COLOR_ACTIVECAPTION},
978+
{ _t("AppWorkspace"), COLOR_APPWORKSPACE },
979+
{ _t("Background"), COLOR_BACKGROUND },
980+
{ _t("ButtonFace"), COLOR_BTNFACE },
981+
{ _t("ButtonHighlight"), COLOR_BTNHIGHLIGHT },
982+
{ _t("ButtonShadow"), COLOR_BTNSHADOW },
983+
{ _t("ButtonText"), COLOR_BTNTEXT },
984+
{ _t("CaptionText"), COLOR_CAPTIONTEXT },
985+
{ _t("GrayText"), COLOR_GRAYTEXT },
986+
{ _t("Highlight"), COLOR_HIGHLIGHT },
987+
{ _t("HighlightText"), COLOR_HIGHLIGHTTEXT },
988+
{ _t("InactiveBorder"), COLOR_INACTIVEBORDER },
989+
{ _t("InactiveCaption"), COLOR_INACTIVECAPTION },
990+
{ _t("InactiveCaptionText"), COLOR_INACTIVECAPTIONTEXT },
991+
{ _t("InfoBackground"), COLOR_INFOBK },
992+
{ _t("InfoText"), COLOR_INFOTEXT },
993+
{ _t("Menu"), COLOR_MENU },
994+
{ _t("MenuText"), COLOR_MENUTEXT },
995+
{ _t("Scrollbar"), COLOR_SCROLLBAR },
996+
{ _t("ThreeDDarkShadow"), COLOR_3DDKSHADOW },
997+
{ _t("ThreeDFace"), COLOR_3DFACE },
998+
{ _t("ThreeDHighlight"), COLOR_3DHILIGHT },
999+
{ _t("ThreeDLightShadow"), COLOR_3DLIGHT },
1000+
{ _t("ThreeDShadow"), COLOR_3DSHADOW },
1001+
{ _t("Window"), COLOR_WINDOW },
1002+
{ _t("WindowFrame"), COLOR_WINDOWFRAME },
1003+
{ _t("WindowText"), COLOR_WINDOWTEXT }
1004+
};
1005+
1006+
if (color == L"Highlight")
1007+
{
1008+
int iii = 0;
1009+
iii++;
1010+
}
1011+
1012+
for (auto& clr : colors)
1013+
{
1014+
if (!t_strcasecmp(clr.name, color.c_str()))
1015+
{
1016+
litehtml::tchar_t str_clr[20];
1017+
DWORD rgb_color = GetSysColor(clr.color_index);
1018+
#ifdef LITEHTML_UTF8
1019+
StringCchPrintfA(str_clr, 20, "#%02X%02X%02X", GetRValue(rgb_color), GetGValue(rgb_color), GetBValue(rgb_color));
1020+
#else
1021+
StringCchPrintf(str_clr, 20, L"#%02X%02X%02X", GetRValue(rgb_color), GetGValue(rgb_color), GetBValue(rgb_color));
1022+
#endif // LITEHTML_UTF8
1023+
return std::move(litehtml::tstring(str_clr));
1024+
}
1025+
}
1026+
return std::move(litehtml::tstring());
1027+
}

containers/cairo/cairo_container.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class cairo_container : public litehtml::document_container
8383
virtual void get_media_features(litehtml::media_features& media) const override;
8484
virtual void get_language(litehtml::tstring& language, litehtml::tstring & culture) const override;
8585
virtual void link(const std::shared_ptr<litehtml::document>& doc, const litehtml::element::ptr& el) override;
86+
virtual litehtml::tstring resolve_color(const litehtml::tstring& color) const override;
8687

8788

8889
virtual void make_url( LPCWSTR url, LPCWSTR basepath, std::wstring& out ) = 0;

src/element.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ litehtml::web_color litehtml::element::get_color( const tchar_t* prop_name, bool
5353
{
5454
return def_color;
5555
}
56-
return web_color::from_string(clrstr);
56+
return web_color::from_string(clrstr, get_document()->container());
5757
}
5858

5959
litehtml::position litehtml::element::get_placement() const

src/html.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ namespace litehtml
6060

6161
virtual void get_media_features(litehtml::media_features& media) const = 0;
6262
virtual void get_language(litehtml::tstring& language, litehtml::tstring & culture) const = 0;
63+
virtual litehtml::tstring resolve_color(const litehtml::tstring& color) const = 0;
6364
};
6465

6566
void trim(tstring &s);

src/html_tag.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,16 +415,16 @@ void litehtml::html_tag::parse_styles(bool is_reparse)
415415
m_css_borders.top.width.fromString( get_style_property(_t("border-top-width"), false, _t("medium")), border_width_strings);
416416
m_css_borders.bottom.width.fromString( get_style_property(_t("border-bottom-width"), false, _t("medium")), border_width_strings);
417417

418-
m_css_borders.left.color = web_color::from_string(get_style_property(_t("border-left-color"), false, _t("")));
418+
m_css_borders.left.color = web_color::from_string(get_style_property(_t("border-left-color"), false, _t("")), doc->container());
419419
m_css_borders.left.style = (border_style) value_index(get_style_property(_t("border-left-style"), false, _t("none")), border_style_strings, border_style_none);
420420

421-
m_css_borders.right.color = web_color::from_string(get_style_property(_t("border-right-color"), false, _t("")));
421+
m_css_borders.right.color = web_color::from_string(get_style_property(_t("border-right-color"), false, _t("")), doc->container());
422422
m_css_borders.right.style = (border_style) value_index(get_style_property(_t("border-right-style"), false, _t("none")), border_style_strings, border_style_none);
423423

424-
m_css_borders.top.color = web_color::from_string(get_style_property(_t("border-top-color"), false, _t("")));
424+
m_css_borders.top.color = web_color::from_string(get_style_property(_t("border-top-color"), false, _t("")), doc->container());
425425
m_css_borders.top.style = (border_style) value_index(get_style_property(_t("border-top-style"), false, _t("none")), border_style_strings, border_style_none);
426426

427-
m_css_borders.bottom.color = web_color::from_string(get_style_property(_t("border-bottom-color"), false, _t("")));
427+
m_css_borders.bottom.color = web_color::from_string(get_style_property(_t("border-bottom-color"), false, _t("")), doc->container());
428428
m_css_borders.bottom.style = (border_style) value_index(get_style_property(_t("border-bottom-style"), false, _t("none")), border_style_strings, border_style_none);
429429

430430
m_css_borders.radius.top_left_x.fromString(get_style_property(_t("border-top-left-radius-x"), false, _t("0")));

src/web_color.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ litehtml::def_color litehtml::g_def_colors[] =
154154
};
155155

156156

157-
litehtml::web_color litehtml::web_color::from_string( const tchar_t* str )
157+
litehtml::web_color litehtml::web_color::from_string(const tchar_t* str, litehtml::document_container* callback)
158158
{
159-
if(!str)
159+
if(!str || !str[0])
160160
{
161161
return web_color(0, 0, 0);
162162
}
@@ -216,34 +216,39 @@ litehtml::web_color litehtml::web_color::from_string( const tchar_t* str )
216216
return clr;
217217
} else
218218
{
219-
const tchar_t* rgb = resolve_name(str);
220-
if(rgb)
219+
tstring rgb = resolve_name(str, callback);
220+
if(!rgb.empty())
221221
{
222-
return from_string(rgb);
222+
return from_string(rgb.c_str(), callback);
223223
}
224224
}
225225
return web_color(0, 0, 0);
226226
}
227227

228-
const litehtml::tchar_t* litehtml::web_color::resolve_name( const tchar_t* name )
228+
litehtml::tstring litehtml::web_color::resolve_name(const tchar_t* name, litehtml::document_container* callback)
229229
{
230230
for(int i=0; g_def_colors[i].name; i++)
231231
{
232232
if(!t_strcasecmp(name, g_def_colors[i].name))
233233
{
234-
return g_def_colors[i].rgb;
234+
return std::move(litehtml::tstring(g_def_colors[i].rgb));
235235
}
236236
}
237-
return 0;
237+
if (callback)
238+
{
239+
litehtml::tstring clr = callback->resolve_color(name);
240+
return std::move(clr);
241+
}
242+
return std::move(litehtml::tstring());
238243
}
239244

240-
bool litehtml::web_color::is_color( const tchar_t* str )
245+
bool litehtml::web_color::is_color(const tchar_t* str)
241246
{
242247
if(!t_strncasecmp(str, _t("rgb"), 3) || str[0] == _t('#'))
243248
{
244249
return true;
245250
}
246-
if(resolve_name(str))
251+
if (!t_isdigit(str[0]) && str[0] != _t('.'))
247252
{
248253
return true;
249254
}

src/web_color.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace litehtml
1010

1111
extern def_color g_def_colors[];
1212

13+
class document_container;
14+
1315
struct web_color
1416
{
1517
byte blue;
@@ -49,8 +51,8 @@ namespace litehtml
4951
alpha = val.alpha;
5052
return *this;
5153
}
52-
static web_color from_string(const tchar_t* str);
53-
static const tchar_t* resolve_name(const tchar_t* name);
54-
static bool is_color(const tchar_t* str);
54+
static web_color from_string(const tchar_t* str, litehtml::document_container* callback);
55+
static litehtml::tstring resolve_name(const tchar_t* name, litehtml::document_container* callback);
56+
static bool is_color(const tchar_t* str);
5557
};
5658
}

0 commit comments

Comments
 (0)