diff options
author | George Hazan <george.hazan@gmail.com> | 2024-03-30 19:43:42 +0300 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2024-03-30 19:43:42 +0300 |
commit | 032787f69076aafd43843c9ab64bdc373dc9aa6a (patch) | |
tree | e8d761cf9adbf492ab2d249d9fc427be7660ca66 /libs/litehtml/include | |
parent | 79353069a2cc268c37c3bf8c50e8d74039298231 (diff) |
update of litehtml, fixes the problem with empty lines
Diffstat (limited to 'libs/litehtml/include')
-rw-r--r-- | libs/litehtml/include/litehtml/background.h | 297 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/css_properties.h | 10 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/document.h | 17 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/document_container.h | 10 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/element.h | 11 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/encodings.h | 90 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/gradient.h | 13 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/html.h | 15 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/html_tag.h | 41 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/os_types.h | 5 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/render_item.h | 46 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/string_id.h | 1 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/style.h | 185 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/stylesheet.h | 1 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/types.h | 34 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/utf8_strings.h | 3 | ||||
-rw-r--r-- | libs/litehtml/include/litehtml/web_color.h | 2 |
17 files changed, 546 insertions, 235 deletions
diff --git a/libs/litehtml/include/litehtml/background.h b/libs/litehtml/include/litehtml/background.h index c56443d548..93e0502cad 100644 --- a/libs/litehtml/include/litehtml/background.h +++ b/libs/litehtml/include/litehtml/background.h @@ -9,10 +9,253 @@ namespace litehtml { + class background_gradient + { + public: + enum gradient_type + { + no_gradient, + linear_gradient, + repeating_linear_gradient, + radial_gradient, + repeating_radial_gradient, + conic_gradient, + repeating_conic_gradient, + }; + enum gradient_side + { + gradient_side_none = 0, + gradient_side_left = 0x01, + gradient_side_right = 0x02, + gradient_side_top = 0x04, + gradient_side_bottom = 0x08, + gradient_side_x_center = 0x10, + gradient_side_y_center = 0x20, + gradient_side_x_length = 0x40, + gradient_side_y_length = 0x80, + }; + enum radial_shape_t + { + radial_shape_none, + radial_shape_circle, + radial_shape_ellipse, + }; + enum radial_extent_t + { + radial_extent_none, + radial_extent_closest_corner, + radial_extent_closest_side, + radial_extent_farthest_corner, + radial_extent_farthest_side, + }; + enum conic_color_space_t + { + conic_color_space_none, + // rectangular-color-space + conic_color_space_srgb, + conic_color_space_srgb_linear, + conic_color_space_display_p3, + conic_color_space_a98_rgb, + conic_color_space_prophoto_rgb, + conic_color_space_rec2020, + conic_color_space_lab, + conic_color_space_oklab, + conic_color_space_xyz, + conic_color_space_xyz_d50, + conic_color_space_xyz_d65, + + // polar-color-space + conic_color_space_polar_start, + conic_color_space_hsl, + conic_color_space_hwb, + conic_color_space_lch, + conic_color_space_oklch, + }; + enum interpolation_method_t + { + interpolation_method_none, + interpolation_method_hue, + interpolation_method_shorter_hue, + interpolation_method_longer_hue, + interpolation_method_increasing_hue, + interpolation_method_decreasing_hue + }; + + class gradient_color + { + public: + bool is_color_hint; + web_color color; + css_length length; + def_value<float> angle; + + gradient_color() : + is_color_hint(false), angle(0) + {} + }; + gradient_type m_type; + uint32_t m_side; + float angle; + std::vector<gradient_color> m_colors; + css_length radial_position_x; + css_length radial_position_y; + radial_extent_t radial_extent; + radial_shape_t radial_shape; + css_length radial_length_x; + css_length radial_length_y; + float conic_from_angle; + conic_color_space_t conic_color_space; + interpolation_method_t conic_interpolation; + + + explicit background_gradient(gradient_type type = no_gradient) + { + m_type = type; + m_side = gradient_side_none; + angle = 180; + radial_extent = radial_extent_none; + radial_shape = radial_shape_ellipse; + radial_length_x.predef(0); + radial_length_y.predef(0); + radial_position_x.predef(0); + radial_position_y.predef(0); + conic_from_angle = 0; + conic_color_space = conic_color_space_none; + conic_interpolation = interpolation_method_none; + } + + bool is_empty() const + { + return m_type == no_gradient || m_colors.empty(); + } + + static background_gradient transparent; + }; + + class background_image + { + public: + enum bg_image_type_t + { + bg_image_type_none, + bg_image_type_url, + bg_image_type_gradient, + }; + bg_image_type_t type; + std::string url; + background_gradient gradient; + + background_image() : type(bg_image_type_none) {} + bool is_empty() const + { + switch (type) + { + case bg_image_type_none: + return false; + case bg_image_type_url: + return url.empty(); + case bg_image_type_gradient: + return gradient.is_empty(); + } + return true; + } + }; + + class background_layer + { + public: + // border_box defines draw boundary. Everything must be drawn inside this rectangle only. + position border_box; + // border_radius defines radius of the border_box. + border_radiuses border_radius; + // clip_box defines clipping rectangle. Works like border_box. Container must set additional clipping. + position clip_box; + // origin_box defines origin rectangle. + position origin_box; + background_attachment attachment; + background_repeat repeat; + // is_root is true for root element. Container can use this flag to apply background to the top window. + bool is_root; + + background_layer() : + attachment(background_attachment_scroll), + repeat(background_repeat_repeat), + is_root(false) + {} + + class image + { + public: + std::string url; + std::string base_url; + }; + + struct color_point + { + float offset; + web_color color; + color_point() { offset = 0.0; } + color_point(float _offset, web_color _color) : offset(_offset), color(_color) {} + }; + + class color + { + public: + web_color color; + }; + + class gradient_base + { + public: + std::vector<color_point> color_points; + + void color_points_transparent_fix(); + bool prepare_color_points(float len, background_gradient::gradient_type g_type, const std::vector<background_gradient::gradient_color>& colors); + bool prepare_angle_color_points(background_gradient::gradient_type g_type, const std::vector<background_gradient::gradient_color>& colors); + }; + + class linear_gradient : public gradient_base + { + public: + pointF start; + pointF end; + }; + + class radial_gradient : public gradient_base + { + public: + pointF position; + pointF radius; + }; + + class conic_gradient : public gradient_base + { + public: + pointF position; + float angle; + background_gradient::conic_color_space_t color_space; + background_gradient::interpolation_method_t interpolation; + conic_gradient() : angle(0), + color_space(background_gradient::conic_color_space_none), + interpolation(background_gradient::interpolation_method_none) + {} + }; + }; + class background { public: - string_vector m_image; + enum layer_type + { + type_none, + type_color, + type_image, + type_linear_gradient, + type_radial_gradient, + type_conic_gradient, + }; + + std::vector<background_image> m_image; string m_baseurl; web_color m_color; int_vector m_attachment; @@ -25,45 +268,33 @@ namespace litehtml bool is_empty() const { - if(m_color.alpha != 0) return false; - if(m_image.empty()) return true; + if(m_color.alpha != 0) + return false; + if(m_image.empty()) + return true; for(const auto& img : m_image) { - if(!img.empty()) return false; + if(!img.is_empty()) return false; } return true; } - }; - - class background_paint - { - public: - string image; - string baseurl; - background_attachment attachment; - background_repeat repeat; - web_color color; - position clip_box; - position origin_box; - position border_box; - border_radiuses border_radius; - size image_size; - int position_x; - int position_y; - bool is_root; - - public: - background_paint() - { - attachment = background_attachment_scroll; - repeat = background_repeat_repeat; - color = web_color::transparent; - position_x = 0; - position_y = 0; - is_root = false; + int get_layers_number() const + { + if(m_color != web_color::transparent) + { + return (int) m_image.size() + 1; + } + return (int) m_image.size(); } + bool get_layer(int idx, position pos, const element* el, const std::shared_ptr<render_item>& ri, background_layer& layer) const; + layer_type get_layer_type(int idx) const; + std::unique_ptr<background_layer::image> get_image_layer(int idx) const; + std::unique_ptr<background_layer::color> get_color_layer(int idx) const; + std::unique_ptr<background_layer::linear_gradient> get_linear_gradient_layer(int idx, const background_layer& layer) const; + std::unique_ptr<background_layer::radial_gradient> get_radial_gradient_layer(int idx, const background_layer& layer) const; + std::unique_ptr<background_layer::conic_gradient> get_conic_gradient_layer(int idx, const background_layer& layer) const; + void draw_layer(uint_ptr hdc, int idx, const background_layer& layer, document_container* container) const; }; - } #endif // LH_BACKGROUND_H diff --git a/libs/litehtml/include/litehtml/css_properties.h b/libs/litehtml/include/litehtml/css_properties.h index a915616861..d6acc4419a 100644 --- a/libs/litehtml/include/litehtml/css_properties.h +++ b/libs/litehtml/include/litehtml/css_properties.h @@ -10,7 +10,7 @@ namespace litehtml { - class element; + class html_tag; class document; class css_properties @@ -75,9 +75,9 @@ namespace litehtml int m_order; private: - void compute_font(const element* el, const std::shared_ptr<document>& doc); - void compute_background(const element* el, const std::shared_ptr<document>& doc); - void compute_flex(const element* el, const std::shared_ptr<document>& doc); + void compute_font(const html_tag* el, const std::shared_ptr<document>& doc); + void compute_background(const html_tag* el, const std::shared_ptr<document>& doc); + void compute_flex(const html_tag* el, const std::shared_ptr<document>& doc); public: css_properties() : @@ -126,7 +126,7 @@ namespace litehtml m_order(0) {} - void compute(const element* el, const std::shared_ptr<document>& doc); + void compute(const html_tag* el, const std::shared_ptr<document>& doc); std::vector<std::tuple<string, string>> dump_get_attrs(); element_position get_position() const; diff --git a/libs/litehtml/include/litehtml/document.h b/libs/litehtml/include/litehtml/document.h index e2623b2221..4871fc154f 100644 --- a/libs/litehtml/include/litehtml/document.h +++ b/libs/litehtml/include/litehtml/document.h @@ -4,6 +4,8 @@ #include "style.h" #include "types.h" #include "master_css.h" +#include "encodings.h" +typedef struct GumboInternalOutput GumboOutput; namespace litehtml { @@ -36,11 +38,11 @@ namespace litehtml { public: virtual ~dumper() {} - virtual void begin_node(const litehtml::string& descr) = 0; + virtual void begin_node(const string& descr) = 0; virtual void end_node() = 0; - virtual void begin_attrs_group(const litehtml::string& descr) = 0; + virtual void begin_attrs_group(const string& descr) = 0; virtual void end_attrs_group() = 0; - virtual void add_attr(const litehtml::string& name, const litehtml::string& value) = 0; + virtual void add_attr(const string& name, const string& value) = 0; }; class html_tag; @@ -70,6 +72,7 @@ namespace litehtml media_features m_media; string m_lang; string m_culture; + string m_text; public: document(document_container* objContainer); virtual ~document(); @@ -105,11 +108,17 @@ namespace litehtml void append_children_from_string(element& parent, const char* str); void dump(dumper& cout); - static litehtml::document::ptr createFromString(const char* str, litehtml::document_container* objPainter, const char* master_styles = litehtml::master_css, const char* user_styles = ""); + // see doc/document_createFromString.txt + static document::ptr createFromString( + const estring& str, + document_container* container, + const string& master_styles = litehtml::master_css, + const string& user_styles = ""); private: uint_ptr add_font(const char* name, int size, const char* weight, const char* style, const char* decoration, font_metrics* fm); + GumboOutput* parse_html(estring str); void create_node(void* gnode, elements_list& elements, bool parseTextNode); bool update_media_lists(const media_features& features); void fix_tables_layout(); diff --git a/libs/litehtml/include/litehtml/document_container.h b/libs/litehtml/include/litehtml/document_container.h index 391873c1ab..1fc4713f9b 100644 --- a/libs/litehtml/include/litehtml/document_container.h +++ b/libs/litehtml/include/litehtml/document_container.h @@ -37,11 +37,11 @@ namespace litehtml virtual void draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker& marker) = 0; virtual void load_image(const char* src, const char* baseurl, bool redraw_on_ready) = 0; virtual void get_image_size(const char* src, const char* baseurl, litehtml::size& sz) = 0; - // Note: regular <img> images are also drawn with draw_background - // bg is guaranteed to have at least one item. - // backgrounds in bg are in CSS order - the last one is the farthest from the user. - // only the last background has valid background-color. - virtual void draw_background(litehtml::uint_ptr hdc, const std::vector<litehtml::background_paint>& bg) = 0; + virtual void draw_image(litehtml::uint_ptr hdc, const background_layer& layer, const std::string& url, const std::string& base_url) = 0; + virtual void draw_solid_fill(litehtml::uint_ptr hdc, const background_layer& layer, const web_color& color) = 0; + virtual void draw_linear_gradient(litehtml::uint_ptr hdc, const background_layer& layer, const background_layer::linear_gradient& gradient) = 0; + virtual void draw_radial_gradient(litehtml::uint_ptr hdc, const background_layer& layer, const background_layer::radial_gradient& gradient) = 0; + virtual void draw_conic_gradient(litehtml::uint_ptr hdc, const background_layer& layer, const background_layer::conic_gradient& gradient) = 0; virtual void draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) = 0; virtual void set_caption(const char* caption) = 0; diff --git a/libs/litehtml/include/litehtml/element.h b/libs/litehtml/include/litehtml/element.h index 4d244736ba..b1aa8e30ae 100644 --- a/libs/litehtml/include/litehtml/element.h +++ b/libs/litehtml/include/litehtml/element.h @@ -102,17 +102,6 @@ namespace litehtml virtual void compute_styles(bool recursive = true); virtual void draw(uint_ptr hdc, int x, int y, const position *clip, const std::shared_ptr<render_item>& ri); virtual void draw_background(uint_ptr hdc, int x, int y, const position *clip, const std::shared_ptr<render_item> &ri); - virtual int get_enum_property (string_id name, bool inherited, int default_value, uint_ptr css_properties_member_offset) const; - virtual int get_int_property (string_id name, bool inherited, int default_value, uint_ptr css_properties_member_offset) const; - virtual css_length get_length_property(string_id name, bool inherited, css_length default_value, uint_ptr css_properties_member_offset) const; - virtual web_color get_color_property (string_id name, bool inherited, web_color default_value, uint_ptr css_properties_member_offset) const; - virtual string get_string_property(string_id name, bool inherited, const string& default_value, uint_ptr css_properties_member_offset) const; - virtual float get_number_property(string_id name, bool inherited, float default_value, uint_ptr css_properties_member_offset) const; - virtual string_vector get_string_vector_property(string_id name, bool inherited, const string_vector& default_value, uint_ptr css_properties_member_offset) const; - virtual int_vector get_int_vector_property (string_id name, bool inherited, const int_vector& default_value, uint_ptr css_properties_member_offset) const; - virtual length_vector get_length_vector_property(string_id name, bool inherited, const length_vector& default_value, uint_ptr css_properties_member_offset) const; - virtual size_vector get_size_vector_property (string_id name, bool inherited, const size_vector& default_value, uint_ptr css_properties_member_offset) const; - virtual string get_custom_property(string_id name, const string& default_value) const; virtual void get_text(string& text); virtual void parse_attributes(); diff --git a/libs/litehtml/include/litehtml/encodings.h b/libs/litehtml/include/litehtml/encodings.h new file mode 100644 index 0000000000..82b0323b44 --- /dev/null +++ b/libs/litehtml/include/litehtml/encodings.h @@ -0,0 +1,90 @@ +#ifndef LH_ENCODINGS_H +#define LH_ENCODINGS_H + +namespace litehtml +{ + +// https://encoding.spec.whatwg.org/#names-and-labels +enum class encoding +{ + null, // indicates error or absence of encoding + utf_8, + + // Legacy single-byte encodings; must be in sync with single_byte_indexes + ibm866, + iso_8859_2, + iso_8859_3, + iso_8859_4, + iso_8859_5, + iso_8859_6, + iso_8859_7, + iso_8859_8, + iso_8859_8_i, + iso_8859_10, + iso_8859_13, + iso_8859_14, + iso_8859_15, + iso_8859_16, + koi8_r, + koi8_u, + macintosh, + windows_874, + windows_1250, + windows_1251, + windows_1252, + windows_1253, + windows_1254, + windows_1255, + windows_1256, + windows_1257, + windows_1258, + x_mac_cyrillic, + + // Legacy multi-byte East Asian encodings + gbk, + gb18030, + big5, + euc_jp, + iso_2022_jp, + shift_jis, + euc_kr, + + // Legacy miscellaneous encodings + replacement, + utf_16be, + utf_16le, + x_user_defined +}; + +// https://html.spec.whatwg.org/multipage/parsing.html#concept-encoding-confidence +enum class confidence // encoding confidence +{ + tentative, + certain, + // irrelevant // not used here +}; + +// Used as argument for document::createFromString, parse_html and encoding_sniffing_algorithm. +struct estring : string // string with encoding +{ + litehtml::encoding encoding; + litehtml::confidence confidence; + + estring(const string& str, litehtml::encoding encoding = encoding::null, litehtml::confidence confidence = confidence::certain) + : string(str), encoding(encoding), confidence(confidence) {} + + estring(const char* str) : string(str), encoding(encoding::null), confidence(confidence::certain) {} +}; + + +encoding bom_sniff(const string& str); +void encoding_sniffing_algorithm(estring& str); + +encoding get_encoding(string label); +encoding extract_encoding_from_meta_element(string str); + +void decode(string input, encoding coding, string& output); + +} // namespace litehtml + +#endif // LH_ENCODINGS_H
\ No newline at end of file diff --git a/libs/litehtml/include/litehtml/gradient.h b/libs/litehtml/include/litehtml/gradient.h new file mode 100644 index 0000000000..782a764e28 --- /dev/null +++ b/libs/litehtml/include/litehtml/gradient.h @@ -0,0 +1,13 @@ +#ifndef LITEHTML_GRADIENT_H +#define LITEHTML_GRADIENT_H + +#include "background.h" + +namespace litehtml +{ + void parse_linear_gradient(const std::string& gradient_str, document_container *container, background_gradient& grad); + void parse_radial_gradient(const std::string& gradient_str, document_container *container, background_gradient& grad); + void parse_conic_gradient(const std::string& gradient_str, document_container *container, background_gradient& grad); +} + +#endif //LITEHTML_GRADIENT_H diff --git a/libs/litehtml/include/litehtml/html.h b/libs/litehtml/include/litehtml/html.h index 818d5ce565..0bde45d2bf 100644 --- a/libs/litehtml/include/litehtml/html.h +++ b/libs/litehtml/include/litehtml/html.h @@ -23,7 +23,7 @@ namespace litehtml { - void trim(string &s, const string& chars_to_trim = " \n\r\t"); + string& trim(string &s, const string& chars_to_trim = " \n\r\f\t"); void lcase(string &s); int value_index(const string& val, const string& strings, int defValue = -1, char delim = ';'); string index_value(int index, const string& strings, char delim = ';'); @@ -39,6 +39,12 @@ namespace litehtml bool is_number(const string& string, const bool allow_dot = 1); + // https://infra.spec.whatwg.org/#ascii-whitespace + inline bool is_whitespace(int c) + { + return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'; + } + inline int t_isdigit(int c) { return (c >= '0' && c <= '9'); @@ -53,7 +59,12 @@ namespace litehtml { return (c >= 'A' && c <= 'Z' ? c + 'a' - 'A' : c); } - + // https://infra.spec.whatwg.org/#ascii-lowercase + inline int lowcase(int c) + { + return t_tolower(c); + } + inline int round_f(float val) { int int_val = (int) val; diff --git a/libs/litehtml/include/litehtml/html_tag.h b/libs/litehtml/include/litehtml/html_tag.h index b085d7dfb2..4a7a9a78dc 100644 --- a/libs/litehtml/include/litehtml/html_tag.h +++ b/libs/litehtml/include/litehtml/html_tag.h @@ -10,6 +10,7 @@ #include "stylesheet.h" #include "line_box.h" #include "table.h" +#include <assert.h> namespace litehtml { @@ -69,19 +70,9 @@ namespace litehtml void draw_background(uint_ptr hdc, int x, int y, const position *clip, const std::shared_ptr<render_item> &ri) override; - template<class Type, property_type property_value_type, Type property_value::* property_value_member> - const Type& get_property_impl (string_id name, bool inherited, const Type& default_value, uint_ptr css_properties_member_offset) const; - int get_enum_property (string_id name, bool inherited, int default_value, uint_ptr css_properties_member_offset) const override; - int get_int_property (string_id name, bool inherited, int default_value, uint_ptr css_properties_member_offset) const override; - css_length get_length_property(string_id name, bool inherited, css_length default_value, uint_ptr css_properties_member_offset) const override; - web_color get_color_property (string_id name, bool inherited, web_color default_value, uint_ptr css_properties_member_offset) const override; - string get_string_property(string_id name, bool inherited, const string& default_value, uint_ptr css_properties_member_offset) const override; - float get_number_property(string_id name, bool inherited, float default_value, uint_ptr css_properties_member_offset) const override; - string_vector get_string_vector_property(string_id name, bool inherited, const string_vector& default_value, uint_ptr css_properties_member_offset) const override; - int_vector get_int_vector_property (string_id name, bool inherited, const int_vector& default_value, uint_ptr css_properties_member_offset) const override; - length_vector get_length_vector_property(string_id name, bool inherited, const length_vector& default_value, uint_ptr css_properties_member_offset) const override; - size_vector get_size_vector_property (string_id name, bool inherited, const size_vector& default_value, uint_ptr css_properties_member_offset) const override; - string get_custom_property(string_id name, const string& default_value) const override; + template<class Type> + const Type& get_property(string_id name, bool inherited, const Type& default_value, uint_ptr css_properties_member_offset) const; + string get_custom_property(string_id name, const string& default_value) const; elements_list& children(); @@ -114,8 +105,6 @@ namespace litehtml string dump_get_name() override; protected: - void init_background_paint(position pos, std::vector<background_paint>& bg_paint, const background* bg, const std::shared_ptr<render_item>& ri); - void init_one_background_paint(int i, position pos, background_paint& bg_paint, const background* bg, const std::shared_ptr<render_item>& ri); void draw_list_marker( uint_ptr hdc, const position &pos ); string get_list_marker_text(int index); element::ptr get_element_before(const style& style, bool create); @@ -134,6 +123,28 @@ namespace litehtml { return m_children; } + + template<class Type> + const Type& html_tag::get_property(string_id name, bool inherited, const Type& default_value, uint_ptr css_properties_member_offset) const + { + const property_value& value = m_style.get_property(name); + + if (value.is<Type>()) + { + return value.get<Type>(); + } + else if (inherited || value.is<inherit>()) + { + if (auto _parent = parent()) + { + return *(Type*)((byte*)&_parent->css() + css_properties_member_offset); + } + return default_value; + } + assert(value.is<invalid>()); + return default_value; + } + } #endif // LH_HTML_TAG_H diff --git a/libs/litehtml/include/litehtml/os_types.h b/libs/litehtml/include/litehtml/os_types.h index bbc2c3cf63..bf674b25b5 100644 --- a/libs/litehtml/include/litehtml/os_types.h +++ b/libs/litehtml/include/litehtml/os_types.h @@ -2,12 +2,15 @@ #define LH_OS_TYPES_H #include <string> +#include <memory> #include <cstdint> namespace litehtml { using std::string; - typedef std::uintptr_t uint_ptr; + using std::shared_ptr; + using std::make_shared; + using uint_ptr = std::uintptr_t; #if defined( WIN32 ) || defined( _WIN32 ) || defined( WINCE ) diff --git a/libs/litehtml/include/litehtml/render_item.h b/libs/litehtml/include/litehtml/render_item.h index 8ee3077676..d3535429cb 100644 --- a/libs/litehtml/include/litehtml/render_item.h +++ b/libs/litehtml/include/litehtml/render_item.h @@ -222,6 +222,52 @@ namespace litehtml return content_offset_top() + content_offset_bottom(); } + int render_offset_left() const + { + if(css().get_box_sizing() == box_sizing_content_box) + { + return m_margins.left + m_borders.left + m_padding.left; + } + return m_margins.left; + } + + int render_offset_right() const + { + if(css().get_box_sizing() == box_sizing_content_box) + { + return m_margins.right + m_borders.right + m_padding.right; + } + return m_margins.right; + } + + int render_offset_width() const + { + return render_offset_left() + render_offset_right(); + } + + int render_offset_top() const + { + if(css().get_box_sizing() == box_sizing_content_box) + { + return m_margins.top + m_borders.top + m_padding.top; + } + return m_margins.top; + } + + int render_offset_bottom() const + { + if(css().get_box_sizing() == box_sizing_content_box) + { + return m_margins.bottom + m_borders.bottom + m_padding.bottom; + } + return m_margins.bottom; + } + + int render_offset_height() const + { + return render_offset_top() + render_offset_bottom(); + } + int box_sizing_left() const { if(css().get_box_sizing() == box_sizing_border_box) diff --git a/libs/litehtml/include/litehtml/string_id.h b/libs/litehtml/include/litehtml/string_id.h index 56827a097d..46f8194727 100644 --- a/libs/litehtml/include/litehtml/string_id.h +++ b/libs/litehtml/include/litehtml/string_id.h @@ -172,6 +172,7 @@ STRING_ID( _background_position_, _background_position_x_, _background_position_y_, + _background_gradient_, _border_, _border_width_, diff --git a/libs/litehtml/include/litehtml/style.h b/libs/litehtml/include/litehtml/style.h index 9f25ed7f02..6014f02dd0 100644 --- a/libs/litehtml/include/litehtml/style.h +++ b/libs/litehtml/include/litehtml/style.h @@ -1,164 +1,40 @@ #ifndef LH_STYLE_H #define LH_STYLE_H +#include <variant> namespace litehtml { - enum property_type + struct invalid {}; // indicates "not found" condition in style::get_property + struct inherit {}; // "inherit" was specified as the value of this property + using property_value_base = std::variant< + invalid, + inherit, + int, + int_vector, + css_length, + length_vector, + float, + web_color, + std::vector<background_image>, + string, + string_vector, + size_vector + >; + + struct property_value : property_value_base { - prop_type_invalid, // indicates "not found" condition in style::get_property - prop_type_inherit, // "inherit" was specified as the value of this property + bool m_important = false; + bool m_has_var = false; // string; parsing is delayed because of var() - prop_type_enum_item, - prop_type_enum_item_vector, - prop_type_length, - prop_type_length_vector, - prop_type_number, - prop_type_color, - prop_type_string, - prop_type_string_vector, - prop_type_size_vector, + property_value() {} + template<class T> property_value(const T& val, bool important, bool has_var = false) + : property_value_base(val), m_important(important), m_has_var(has_var) {} - prop_type_var, // also string, but needs further parsing because of var() - }; - - class property_value - { - public: - property_type m_type; - bool m_important; - - union { - int m_enum_item; - int_vector m_enum_item_vector; - css_length m_length; - length_vector m_length_vector; - float m_number; - web_color m_color; - string m_string; - string_vector m_string_vector; - size_vector m_size_vector; - }; - - property_value() - : m_type(prop_type_invalid) - { - } - property_value(bool important, property_type type) - : m_type(type), m_important(important) - { - } - property_value(const string& str, bool important, property_type type = prop_type_string) - : m_type(type), m_important(important), m_string(str) - { - } - property_value(const string_vector& vec, bool important) - : m_type(prop_type_string_vector), m_important(important), m_string_vector(vec) - { - } - property_value(const css_length& length, bool important) - : m_type(prop_type_length), m_important(important), m_length(length) - { - } - property_value(const length_vector& vec, bool important) - : m_type(prop_type_length_vector), m_important(important), m_length_vector(vec) - { - } - property_value(float number, bool important) - : m_type(prop_type_number), m_important(important), m_number(number) - { - } - property_value(int enum_item, bool important) - : m_type(prop_type_enum_item), m_important(important), m_enum_item(enum_item) - { - } - property_value(const int_vector& vec, bool important) - : m_type(prop_type_enum_item_vector), m_important(important), m_enum_item_vector(vec) - { - } - property_value(web_color color, bool important) - : m_type(prop_type_color), m_important(important), m_color(color) - { - } - property_value(const size_vector& vec, bool important) - : m_type(prop_type_size_vector), m_important(important), m_size_vector(vec) - { - } - ~property_value() - { - switch (m_type) - { - case prop_type_string: - case prop_type_var: - m_string.~string(); - break; - case prop_type_string_vector: - m_string_vector.~string_vector(); - break; - case prop_type_length: - m_length.~css_length(); - break; - case prop_type_length_vector: - m_length_vector.~length_vector(); - break; - case prop_type_enum_item_vector: - m_enum_item_vector.~int_vector(); - break; - case prop_type_color: - m_color.~web_color(); - break; - case prop_type_size_vector: - m_size_vector.~size_vector(); - break; - default: - break; - } - } - property_value& operator=(const property_value& val) - { - this->~property_value(); - - switch (val.m_type) - { - case prop_type_invalid: - new(this) property_value(); - break; - case prop_type_inherit: - new(this) property_value(val.m_important, val.m_type); - break; - case prop_type_string: - case prop_type_var: - new(this) property_value(val.m_string, val.m_important, val.m_type); - break; - case prop_type_string_vector: - new(this) property_value(val.m_string_vector, val.m_important); - break; - case prop_type_enum_item: - new(this) property_value(val.m_enum_item, val.m_important); - break; - case prop_type_enum_item_vector: - new(this) property_value(val.m_enum_item_vector, val.m_important); - break; - case prop_type_length: - new(this) property_value(val.m_length, val.m_important); - break; - case prop_type_length_vector: - new(this) property_value(val.m_length_vector, val.m_important); - break; - case prop_type_number: - new(this) property_value(val.m_number, val.m_important); - break; - case prop_type_color: - new(this) property_value(val.m_color, val.m_important); - break; - case prop_type_size_vector: - new(this) property_value(val.m_size_vector, val.m_important); - break; - } - - return *this; - } + template<class T> bool is() const { return std::holds_alternative<T>(*this); } + template<class T> const T& get() const { return std::get<T>(*this); } }; + class html_tag; typedef std::map<string_id, property_value> props_map; class style @@ -185,14 +61,15 @@ namespace litehtml m_properties.clear(); } - void subst_vars(const element* el); + void subst_vars(const html_tag* el); private: + void inherit_property(string_id name, bool important); void parse_property(const string& txt, const string& baseurl, document_container* container); void parse(const string& txt, const string& baseurl, document_container* container); void parse_background(const string& val, const string& baseurl, bool important, document_container* container); bool parse_one_background(const string& val, document_container* container, background& bg); - void parse_background_image(const string& val, const string& baseurl, bool important); + void parse_background_image(const string& val, document_container* container, const string& baseurl, bool important); // parse comma-separated list of keywords void parse_keyword_comma_list(string_id name, const string& val, bool important); void parse_background_position(const string& val, bool important); @@ -205,7 +82,7 @@ namespace litehtml static css_length parse_border_width(const string& str); static void parse_two_lengths(const string& str, css_length len[2]); static int parse_four_lengths(const string& str, css_length len[4]); - static void subst_vars_(string& str, const element* el); + static void subst_vars_(string& str, const html_tag* el); void add_parsed_property(string_id name, const property_value& propval); void remove_property(string_id name, bool important); diff --git a/libs/litehtml/include/litehtml/stylesheet.h b/libs/litehtml/include/litehtml/stylesheet.h index 923853d768..e614a28310 100644 --- a/libs/litehtml/include/litehtml/stylesheet.h +++ b/libs/litehtml/include/litehtml/stylesheet.h @@ -28,6 +28,7 @@ namespace litehtml void parse_stylesheet(const char* str, const char* baseurl, const std::shared_ptr<document>& doc, const media_query_list::ptr& media); void sort_selectors(); static void parse_css_url(const string& str, string& url); + static void parse_gradient(const string &token, document_container *container, background_gradient& grad); private: void parse_atrule(const string& text, const char* baseurl, const std::shared_ptr<document>& doc, const media_query_list::ptr& media); diff --git a/libs/litehtml/include/litehtml/types.h b/libs/litehtml/include/litehtml/types.h index bfcaa6c3ac..415092b28a 100644 --- a/libs/litehtml/include/litehtml/types.h +++ b/libs/litehtml/include/litehtml/types.h @@ -41,6 +41,17 @@ namespace litehtml int height() const { return top + bottom; } }; + struct pointF + { + float x; + float y; + + pointF() : x(0), y(0) {} + pointF(float _x, float _y) : x(_x), y(_y) {} + + void set(float _x, float _y) { x = _x; y = _y; } + }; + struct size { int width; @@ -108,10 +119,10 @@ namespace litehtml height = sz.height; } - void move_to(int x, int y) + void move_to(int _x, int _y) { - this->x = x; - this->y = y; + x = _x; + y = _y; } bool does_intersect(const position* val) const @@ -139,9 +150,9 @@ namespace litehtml return false; } - bool is_point_inside(int x, int y) const + bool is_point_inside(int _x, int _y) const { - if(x >= left() && x <= right() && y >= top() && y <= bottom()) + if(_x >= left() && _x <= right() && _y >= top() && _y <= bottom()) { return true; } @@ -706,6 +717,11 @@ namespace litehtml m_is_default = true; m_val = def_val; } + def_value(const def_value<T>& val) + { + m_is_default = val.m_is_default; + m_val = val.m_val; + } void reset(T def_val) { m_is_default = true; @@ -721,6 +737,12 @@ namespace litehtml m_is_default = false; return m_val; } + def_value<T>& operator=(const def_value<T>& val) + { + m_is_default = val.m_is_default; + m_val = val.m_val; + return *this; + } operator T() const { return m_val; @@ -904,6 +926,8 @@ namespace litehtml render_fixed_only, }; + const char* const split_delims_spaces = " \t\r\n\f\v"; + // List of the Void Elements (can't have any contents) const char* const void_elements = "area;base;br;col;command;embed;hr;img;input;keygen;link;meta;param;source;track;wbr"; diff --git a/libs/litehtml/include/litehtml/utf8_strings.h b/libs/litehtml/include/litehtml/utf8_strings.h index 72969fd3d7..f438ad2b30 100644 --- a/libs/litehtml/include/litehtml/utf8_strings.h +++ b/libs/litehtml/include/litehtml/utf8_strings.h @@ -6,6 +6,9 @@ namespace litehtml { + // converts UTF-32 ch to UTF-8 and appends it to str + void append_char(string& str, int ch); + class utf8_to_wchar { const byte* m_utf8; diff --git a/libs/litehtml/include/litehtml/web_color.h b/libs/litehtml/include/litehtml/web_color.h index a6006d391e..145a4157c3 100644 --- a/libs/litehtml/include/litehtml/web_color.h +++ b/libs/litehtml/include/litehtml/web_color.h @@ -58,6 +58,8 @@ namespace litehtml return {(byte) v_red, (byte) v_green, (byte) v_blue, alpha}; } }; + + typedef std::vector<web_color> web_color_vector; } #endif // LH_WEB_COLOR_H |