1 /*
2  * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
3  * Copyright(C) 2019 the Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write tothe Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  */
17 
18 #pragma once
19 
20 #include <vector>
21 #include <list>
22 #include <string>
23 
24 #include <cairo.h>
25 #include <gtk/gtk.h>
26 #include <fontconfig/fontconfig.h>
27 
28 #include "litehtml/litehtml.h"
29 
30 struct cairo_clip_box
31 {
32 	typedef std::vector<cairo_clip_box> vector;
33 	litehtml::position	box;
34 	litehtml::border_radiuses radius;
35 
cairo_clip_boxcairo_clip_box36 	cairo_clip_box(const litehtml::position& vBox, litehtml::border_radiuses vRad)
37 	{
38 		box = vBox;
39 		radius = vRad;
40 	}
41 
cairo_clip_boxcairo_clip_box42 	cairo_clip_box(const cairo_clip_box& val)
43 	{
44 		box = val.box;
45 		radius = val.radius;
46 	}
47 	cairo_clip_box& operator=(const cairo_clip_box& val)
48 	{
49 		box = val.box;
50 		radius = val.radius;
51 		return *this;
52 	}
53 };
54 
55 class container_linux :	public litehtml::document_container
56 {
57 	typedef std::pair<litehtml::tstring, GdkPixbuf*> image;
58 	typedef std::list<image> images_map;
59 
60 protected:
61 	cairo_surface_t*			m_temp_surface;
62 	cairo_t*					m_temp_cr;
63 	images_map					m_images;
64 	GRecMutex					m_images_lock;
65 	cairo_clip_box::vector				m_clips;
66 
67 public:
68 	container_linux(void);
69 	virtual ~container_linux(void);
70 
71 	virtual int						pt_to_px(int pt) override;
72 	virtual void 						load_image(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready) override;
73 	virtual void						get_image_size(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz) override;
74 	virtual void						draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) override;
75 	virtual void						draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) override;
76 	virtual void 						draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker& marker) override;
77 	virtual std::shared_ptr<litehtml::element>	create_element(const litehtml::tchar_t *tag_name,
78 																 const litehtml::string_map &attributes,
79 																 const std::shared_ptr<litehtml::document> &doc) override;
80 	virtual void						get_media_features(litehtml::media_features& media) const override;
81 	virtual void						get_language(litehtml::tstring& language, litehtml::tstring & culture) const override;
82 	virtual void 						link(const std::shared_ptr<litehtml::document> &ptr, const litehtml::element::ptr& el) override;
83 
84 
85 	virtual	void						transform_text(litehtml::tstring& text, litehtml::text_transform tt) override;
86 	virtual void						set_clip(const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y) override;
87 	virtual void						del_clip() override;
88 
89 	virtual void						make_url( const litehtml::tchar_t* url, const litehtml::tchar_t* basepath, litehtml::tstring& out );
90 
91 	void								clear_images();
92 
93 	/* Trim down images cache to less than desired_size [bytes],
94 	 * starting from oldest stored. */
95 	gint								clear_images(gint desired_size);
96 
97 	void								add_image_to_cache(const gchar *url, GdkPixbuf *image);
98 	virtual void				redraw(gboolean force_render) = 0;
99 	virtual GdkPixbuf *get_local_image(const litehtml::tstring url) const = 0;
100 
101 protected:
102 	virtual void						draw_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color, int line_width);
103 	virtual void						fill_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color);
104 	virtual void						rounded_rectangle( cairo_t* cr, const litehtml::position &pos, const litehtml::border_radiuses &radius );
105 	void								apply_clip(cairo_t* cr);
set_color(cairo_t * cr,litehtml::web_color color)106 	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); }
107 
108 private:
109 	void								add_path_arc(cairo_t* cr, double x, double y, double rx, double ry, double a1, double a2, bool neg);
110 	void								draw_pixbuf(cairo_t* cr, const GdkPixbuf *bmp, int x, int y, int cx, int cy);
111 	cairo_surface_t*					surface_from_pixbuf(const GdkPixbuf *bmp);
112 	void lock_images_cache(void);
113 	void unlock_images_cache(void);
114 };
115