43a917c6e7cb70bdc6a7a19102af4be66e276a02
[claws.git] / src / plugins / litehtml_viewer / container_linux.h
1 #pragma once
2
3 #include <vector>
4 #include <list>
5 #include <string>
6
7 #include <cairo.h>
8 #include <gtk/gtk.h>
9 #include <fontconfig/fontconfig.h>
10
11 #include "litehtml/litehtml.h"
12
13 struct cairo_clip_box
14 {
15         typedef std::vector<cairo_clip_box> vector;
16         litehtml::position      box;
17         litehtml::border_radiuses radius;
18
19         cairo_clip_box(const litehtml::position& vBox, litehtml::border_radiuses vRad)
20         {
21                 box = vBox;
22                 radius = vRad;
23         }
24
25         cairo_clip_box(const cairo_clip_box& val)
26         {
27                 box = val.box;
28                 radius = val.radius;
29         }
30         cairo_clip_box& operator=(const cairo_clip_box& val)
31         {
32                 box = val.box;
33                 radius = val.radius;
34                 return *this;
35         }
36 };
37
38 struct cairo_font
39 {
40         cairo_font_face_t*      font;
41         int                                     size;
42         bool                            underline;
43         bool                            strikeout;
44 };
45
46 class container_linux : public litehtml::document_container
47 {
48         typedef std::pair<litehtml::tstring, GdkPixbuf*> image;
49         typedef std::list<image> images_map;
50
51 protected:
52         cairo_surface_t*                        m_temp_surface;
53         cairo_t*                                        m_temp_cr;
54         images_map                                      m_images;
55     cairo_clip_box::vector              m_clips;
56 public:
57         container_linux(void);
58         virtual ~container_linux(void);
59
60         virtual litehtml::uint_ptr                      create_font(const litehtml::tchar_t* faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics* fm) override;
61         virtual void                                            delete_font(litehtml::uint_ptr hFont) override;
62         virtual int                                             text_width(const litehtml::tchar_t* text, litehtml::uint_ptr hFont) override;
63         virtual void                                            draw_text(litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos) override;
64         virtual int                                             pt_to_px(int pt) override;
65         virtual void                                            load_image(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready) override;
66         virtual void                                            get_image_size(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz) override;
67         virtual void                                            draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) override;
68         virtual void                                            draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) override;
69         virtual void                                            draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker& marker) override;
70         virtual std::shared_ptr<litehtml::element>      create_element(const litehtml::tchar_t *tag_name,
71                                                                                                                                  const litehtml::string_map &attributes,
72                                                                                                                                  const std::shared_ptr<litehtml::document> &doc) override;
73         virtual void                                            get_media_features(litehtml::media_features& media) const override;
74         virtual void                                            get_language(litehtml::tstring& language, litehtml::tstring & culture) const override;
75         virtual void                                            link(const std::shared_ptr<litehtml::document> &ptr, const litehtml::element::ptr& el) override;
76
77
78         virtual void                                            transform_text(litehtml::tstring& text, litehtml::text_transform tt) override;
79         virtual void                                            set_clip(const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y) override;
80         virtual void                                            del_clip() override;
81
82         virtual void                                            make_url( const litehtml::tchar_t* url, const litehtml::tchar_t* basepath, litehtml::tstring& out );
83         virtual GdkPixbuf       *get_image(const litehtml::tchar_t* url, bool redraw_on_ready) = 0;
84
85         void                                                            clear_images();
86
87         /* Trim down images cache to less than desired_size [bytes],
88          * starting from oldest stored. */
89         gint                                                            clear_images(gint desired_size);
90
91 protected:
92         virtual void                                            draw_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color, int line_width);
93         virtual void                                            fill_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color);
94         virtual void                                            rounded_rectangle( cairo_t* cr, const litehtml::position &pos, const litehtml::border_radiuses &radius );
95
96 private:
97         void                                                            apply_clip(cairo_t* cr);
98         void                                                            add_path_arc(cairo_t* cr, double x, double y, double rx, double ry, double a1, double a2, bool neg);
99         void                                                            set_color(cairo_t* cr, litehtml::web_color color)       { cairo_set_source_rgba(cr, color.red / 255.0, color.green / 255.0, color.blue / 255.0, color.alpha / 255.0); }
100         void                                                            draw_pixbuf(cairo_t* cr, const GdkPixbuf *bmp, int x, int y, int cx, int cy);
101         cairo_surface_t*                                        surface_from_pixbuf(const GdkPixbuf *bmp);
102 };