2 * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
3 * Copyright(C) 2019 the Claws Mail Team
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.
20 #include "claws-features.h"
23 #include "container_linux.h"
27 #define _USE_MATH_DEFINES
31 # define M_PI 3.14159265358979323846
34 container_linux::container_linux(void)
36 m_temp_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 2, 2);
37 m_temp_cr = cairo_create(m_temp_surface);
40 container_linux::~container_linux(void)
43 cairo_surface_destroy(m_temp_surface);
44 cairo_destroy(m_temp_cr);
47 int container_linux::pt_to_px( int pt )
49 GdkScreen* screen = gdk_screen_get_default();
50 double dpi = gdk_screen_get_resolution(screen);
52 return (int) ((double) pt * dpi / 72.0);
55 void container_linux::draw_list_marker( litehtml::uint_ptr hdc, const litehtml::list_marker& marker )
57 if(!marker.image.empty())
59 /*litehtml::tstring url;
60 make_url(marker.image.c_str(), marker.baseurl, url);
63 images_map::iterator img_i = m_images.find(url.c_str());
64 if(img_i != m_images.end())
68 draw_txdib((cairo_t*) hdc, img_i->second, marker.pos.x, marker.pos.y, marker.pos.width, marker.pos.height);
71 unlock_images_cache();*/
74 switch(marker.marker_type)
76 case litehtml::list_style_type_circle:
78 draw_ellipse((cairo_t*) hdc, marker.pos.x, marker.pos.y, marker.pos.width, marker.pos.height, marker.color, 0.5);
81 case litehtml::list_style_type_disc:
83 fill_ellipse((cairo_t*) hdc, marker.pos.x, marker.pos.y, marker.pos.width, marker.pos.height, marker.color);
86 case litehtml::list_style_type_square:
89 cairo_t* cr = (cairo_t*) hdc;
93 cairo_rectangle(cr, marker.pos.x, marker.pos.y, marker.pos.width, marker.pos.height);
95 set_color(cr, marker.color);
107 void container_linux::load_image( const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready )
109 litehtml::tstring url;
110 make_url(src, baseurl, url);
113 for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
114 const image *i = &(*ii);
116 if (!strcmp(i->first.c_str(), url.c_str())) {
126 GdkPixbuf *img = get_image(url.c_str(), true);
129 m_images.push_back(std::make_pair(url, img));
139 void container_linux::get_image_size( const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz )
141 litehtml::tstring url;
142 make_url(src, baseurl, url);
144 const image *img = NULL;
146 for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
147 const image *i = &(*ii);
148 if (i->first == url) {
157 sz.width = gdk_pixbuf_get_width(img->second);
158 sz.height = gdk_pixbuf_get_height(img->second);
166 void container_linux::draw_background( litehtml::uint_ptr hdc, const litehtml::background_paint& bg )
168 cairo_t* cr = (cairo_t*) hdc;
172 rounded_rectangle(cr, bg.border_box, bg.border_radius);
175 cairo_rectangle(cr, bg.clip_box.x, bg.clip_box.y, bg.clip_box.width, bg.clip_box.height);
180 set_color(cr, bg.color);
184 litehtml::tstring url;
185 make_url(bg.image.c_str(), bg.baseurl.c_str(), url);
187 //lock_images_cache();
189 const image *img_i = NULL;
191 for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
192 const image *i = &(*ii);
193 if (i->first == url) {
200 if(img_i != NULL && img_i->second)
202 GdkPixbuf *bgbmp = img_i->second;
205 if(bg.image_size.width != gdk_pixbuf_get_width(bgbmp) || bg.image_size.height != gdk_pixbuf_get_height(bgbmp))
207 new_img = gdk_pixbuf_scale_simple(bgbmp, bg.image_size.width, bg.image_size.height, GDK_INTERP_BILINEAR);
211 cairo_surface_t* img = surface_from_pixbuf(bgbmp);
212 cairo_pattern_t *pattern = cairo_pattern_create_for_surface(img);
213 cairo_matrix_t flib_m;
214 cairo_matrix_init_identity(&flib_m);
215 cairo_matrix_translate(&flib_m, -bg.position_x, -bg.position_y);
216 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
217 cairo_pattern_set_matrix (pattern, &flib_m);
221 case litehtml::background_repeat_no_repeat:
222 draw_pixbuf(cr, bgbmp, bg.position_x, bg.position_y, gdk_pixbuf_get_width(bgbmp), gdk_pixbuf_get_height(bgbmp));
225 case litehtml::background_repeat_repeat_x:
226 cairo_set_source(cr, pattern);
227 cairo_rectangle(cr, bg.clip_box.left(), bg.position_y, bg.clip_box.width, gdk_pixbuf_get_height(bgbmp));
231 case litehtml::background_repeat_repeat_y:
232 cairo_set_source(cr, pattern);
233 cairo_rectangle(cr, bg.position_x, bg.clip_box.top(), gdk_pixbuf_get_width(bgbmp), bg.clip_box.height);
237 case litehtml::background_repeat_repeat:
238 cairo_set_source(cr, pattern);
239 cairo_rectangle(cr, bg.clip_box.left(), bg.clip_box.top(), bg.clip_box.width, bg.clip_box.height);
244 cairo_pattern_destroy(pattern);
245 cairo_surface_destroy(img);
248 // unlock_images_cache();
252 void container_linux::make_url(const litehtml::tchar_t* url, const litehtml::tchar_t* basepath, litehtml::tstring& out)
257 void container_linux::add_path_arc(cairo_t* cr, double x, double y, double rx, double ry, double a1, double a2, bool neg)
264 cairo_translate(cr, x, y);
265 cairo_scale(cr, 1, ry / rx);
266 cairo_translate(cr, -x, -y);
270 cairo_arc_negative(cr, x, y, rx, a1, a2);
273 cairo_arc(cr, x, y, rx, a1, a2);
279 cairo_move_to(cr, x, y);
283 void container_linux::draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root)
285 cairo_t* cr = (cairo_t*) hdc;
296 if(borders.top.width != 0 && borders.top.style > litehtml::border_style_hidden)
298 bdr_top = (int) borders.top.width;
300 if(borders.bottom.width != 0 && borders.bottom.style > litehtml::border_style_hidden)
302 bdr_bottom = (int) borders.bottom.width;
304 if(borders.left.width != 0 && borders.left.style > litehtml::border_style_hidden)
306 bdr_left = (int) borders.left.width;
308 if(borders.right.width != 0 && borders.right.style > litehtml::border_style_hidden)
310 bdr_right = (int) borders.right.width;
316 set_color(cr, borders.right.color);
318 double r_top = borders.radius.top_right_x;
319 double r_bottom = borders.radius.bottom_right_x;
323 double end_angle = 2 * M_PI;
324 double start_angle = end_angle - M_PI / 2.0 / ((double) bdr_top / (double) bdr_right + 1);
327 draw_pos.right() - r_top,
328 draw_pos.top() + r_top,
330 r_top - bdr_right + (bdr_right - bdr_top),
335 draw_pos.right() - r_top,
336 draw_pos.top() + r_top,
343 cairo_move_to(cr, draw_pos.right() - bdr_right, draw_pos.top() + bdr_top);
344 cairo_line_to(cr, draw_pos.right(), draw_pos.top());
349 cairo_line_to(cr, draw_pos.right(), draw_pos.bottom() - r_bottom);
351 double start_angle = 0;
352 double end_angle = start_angle + M_PI / 2.0 / ((double) bdr_bottom / (double) bdr_right + 1);
355 draw_pos.right() - r_bottom,
356 draw_pos.bottom() - r_bottom,
363 draw_pos.right() - r_bottom,
364 draw_pos.bottom() - r_bottom,
365 r_bottom - bdr_right,
366 r_bottom - bdr_right + (bdr_right - bdr_bottom),
371 cairo_line_to(cr, draw_pos.right(), draw_pos.bottom());
372 cairo_line_to(cr, draw_pos.right() - bdr_right, draw_pos.bottom() - bdr_bottom);
378 // draw bottom border
381 set_color(cr, borders.bottom.color);
383 double r_left = borders.radius.bottom_left_x;
384 double r_right = borders.radius.bottom_right_x;
388 double start_angle = M_PI / 2.0;
389 double end_angle = start_angle + M_PI / 2.0 / ((double) bdr_left / (double) bdr_bottom + 1);
392 draw_pos.left() + r_left,
393 draw_pos.bottom() - r_left,
394 r_left - bdr_bottom + (bdr_bottom - bdr_left),
400 draw_pos.left() + r_left,
401 draw_pos.bottom() - r_left,
408 cairo_move_to(cr, draw_pos.left(), draw_pos.bottom());
409 cairo_line_to(cr, draw_pos.left() + bdr_left, draw_pos.bottom() - bdr_bottom);
414 cairo_line_to(cr, draw_pos.right() - r_right, draw_pos.bottom());
416 double end_angle = M_PI / 2.0;
417 double start_angle = end_angle - M_PI / 2.0 / ((double) bdr_right / (double) bdr_bottom + 1);
420 draw_pos.right() - r_right,
421 draw_pos.bottom() - r_right,
428 draw_pos.right() - r_right,
429 draw_pos.bottom() - r_right,
430 r_right - bdr_bottom + (bdr_bottom - bdr_right),
431 r_right - bdr_bottom,
436 cairo_line_to(cr, draw_pos.right() - bdr_right, draw_pos.bottom() - bdr_bottom);
437 cairo_line_to(cr, draw_pos.right(), draw_pos.bottom());
446 set_color(cr, borders.top.color);
448 double r_left = borders.radius.top_left_x;
449 double r_right = borders.radius.top_right_x;
453 double end_angle = M_PI * 3.0 / 2.0;
454 double start_angle = end_angle - M_PI / 2.0 / ((double) bdr_left / (double) bdr_top + 1);
457 draw_pos.left() + r_left,
458 draw_pos.top() + r_left,
465 draw_pos.left() + r_left,
466 draw_pos.top() + r_left,
467 r_left - bdr_top + (bdr_top - bdr_left),
473 cairo_move_to(cr, draw_pos.left(), draw_pos.top());
474 cairo_line_to(cr, draw_pos.left() + bdr_left, draw_pos.top() + bdr_top);
479 cairo_line_to(cr, draw_pos.right() - r_right, draw_pos.top() + bdr_top);
481 double start_angle = M_PI * 3.0 / 2.0;
482 double end_angle = start_angle + M_PI / 2.0 / ((double) bdr_right / (double) bdr_top + 1);
485 draw_pos.right() - r_right,
486 draw_pos.top() + r_right,
487 r_right - bdr_top + (bdr_top - bdr_right),
493 draw_pos.right() - r_right,
494 draw_pos.top() + r_right,
501 cairo_line_to(cr, draw_pos.right() - bdr_right, draw_pos.top() + bdr_top);
502 cairo_line_to(cr, draw_pos.right(), draw_pos.top());
511 set_color(cr, borders.left.color);
513 double r_top = borders.radius.top_left_x;
514 double r_bottom = borders.radius.bottom_left_x;
518 double start_angle = M_PI;
519 double end_angle = start_angle + M_PI / 2.0 / ((double) bdr_top / (double) bdr_left + 1);
522 draw_pos.left() + r_top,
523 draw_pos.top() + r_top,
525 r_top - bdr_left + (bdr_left - bdr_top),
530 draw_pos.left() + r_top,
531 draw_pos.top() + r_top,
538 cairo_move_to(cr, draw_pos.left() + bdr_left, draw_pos.top() + bdr_top);
539 cairo_line_to(cr, draw_pos.left(), draw_pos.top());
544 cairo_line_to(cr, draw_pos.left(), draw_pos.bottom() - r_bottom);
546 double end_angle = M_PI;
547 double start_angle = end_angle - M_PI / 2.0 / ((double) bdr_bottom / (double) bdr_left + 1);
550 draw_pos.left() + r_bottom,
551 draw_pos.bottom() - r_bottom,
558 draw_pos.left() + r_bottom,
559 draw_pos.bottom() - r_bottom,
561 r_bottom - bdr_left + (bdr_left - bdr_bottom),
566 cairo_line_to(cr, draw_pos.left(), draw_pos.bottom());
567 cairo_line_to(cr, draw_pos.left() + bdr_left, draw_pos.bottom() - bdr_bottom);
575 void container_linux::transform_text(litehtml::tstring& text, litehtml::text_transform tt)
580 void container_linux::set_clip( const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y )
582 litehtml::position clip_pos = pos;
583 litehtml::position client_pos;
584 get_client_rect(client_pos);
587 clip_pos.x = client_pos.x;
588 clip_pos.width = client_pos.width;
592 clip_pos.y = client_pos.y;
593 clip_pos.height = client_pos.height;
595 m_clips.emplace_back(clip_pos, bdr_radius);
598 void container_linux::del_clip()
606 void container_linux::apply_clip( cairo_t* cr )
608 for(const auto& clip_box : m_clips)
610 rounded_rectangle(cr, clip_box.box, clip_box.radius);
615 void container_linux::draw_ellipse( cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color, int line_width )
624 cairo_translate (cr, x + width / 2.0, y + height / 2.0);
625 cairo_scale (cr, width / 2.0, height / 2.0);
626 cairo_arc (cr, 0, 0, 1, 0, 2 * M_PI);
628 set_color(cr, color);
629 cairo_set_line_width(cr, line_width);
635 void container_linux::fill_ellipse( cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color )
644 cairo_translate (cr, x + width / 2.0, y + height / 2.0);
645 cairo_scale (cr, width / 2.0, height / 2.0);
646 cairo_arc (cr, 0, 0, 1, 0, 2 * M_PI);
648 set_color(cr, color);
654 void container_linux::clear_images()
656 for(auto i = m_images.begin(); i != m_images.end(); ++i) {
660 g_object_unref(img->second);
667 gint container_linux::clear_images(gint desired_size)
672 /* First, tally up size of all the stored GdkPixbufs and
673 * deallocate those which make the total size be above
674 * the desired_size limit. We will remove their list
676 for (auto i = m_images.rbegin(); i != m_images.rend(); ++i) {
680 if (img->second == NULL)
683 cursize = gdk_pixbuf_get_byte_length(img->second);
685 if (size + cursize > desired_size) {
686 g_object_unref(img->second);
694 /* Remove elements whose GdkPixbuf pointers point to NULL. */
695 m_images.remove_if([&](image _img) -> bool {
696 if (_img.second == NULL)
704 std::shared_ptr<litehtml::element> container_linux::create_element(const litehtml::tchar_t *tag_name,
705 const litehtml::string_map &attributes,
706 const std::shared_ptr<litehtml::document> &doc)
711 void container_linux::rounded_rectangle( cairo_t* cr, const litehtml::position &pos, const litehtml::border_radiuses &radius )
714 if(radius.top_left_x)
716 cairo_arc(cr, pos.left() + radius.top_left_x, pos.top() + radius.top_left_x, radius.top_left_x, M_PI, M_PI * 3.0 / 2.0);
719 cairo_move_to(cr, pos.left(), pos.top());
722 cairo_line_to(cr, pos.right() - radius.top_right_x, pos.top());
724 if(radius.top_right_x)
726 cairo_arc(cr, pos.right() - radius.top_right_x, pos.top() + radius.top_right_x, radius.top_right_x, M_PI * 3.0 / 2.0, 2.0 * M_PI);
729 cairo_line_to(cr, pos.right(), pos.bottom() - radius.bottom_right_x);
731 if(radius.bottom_right_x)
733 cairo_arc(cr, pos.right() - radius.bottom_right_x, pos.bottom() - radius.bottom_right_x, radius.bottom_right_x, 0, M_PI / 2.0);
736 cairo_line_to(cr, pos.left() - radius.bottom_left_x, pos.bottom());
738 if(radius.bottom_left_x)
740 cairo_arc(cr, pos.left() + radius.bottom_left_x, pos.bottom() - radius.bottom_left_x, radius.bottom_left_x, M_PI / 2.0, M_PI);
744 void container_linux::draw_pixbuf(cairo_t* cr, const GdkPixbuf *bmp, int x, int y, int cx, int cy)
749 cairo_matrix_t flib_m;
750 cairo_matrix_init(&flib_m, 1, 0, 0, -1, 0, 0);
752 if(cx != gdk_pixbuf_get_width(bmp) || cy != gdk_pixbuf_get_height(bmp))
754 GdkPixbuf *new_img = gdk_pixbuf_scale_simple(bmp, cx, cy, GDK_INTERP_BILINEAR);
755 gdk_cairo_set_source_pixbuf(cr, new_img, x, y);
759 gdk_cairo_set_source_pixbuf(cr, bmp, x, y);
767 cairo_surface_t* container_linux::surface_from_pixbuf(const GdkPixbuf *bmp)
769 cairo_surface_t* ret = NULL;
771 if(gdk_pixbuf_get_has_alpha(bmp))
773 ret = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, gdk_pixbuf_get_width(bmp), gdk_pixbuf_get_height(bmp));
776 ret = cairo_image_surface_create(CAIRO_FORMAT_RGB24, gdk_pixbuf_get_width(bmp), gdk_pixbuf_get_height(bmp));
779 // Cairo::RefPtr<Cairo::Surface> surface(new Cairo::Surface(ret, false));
780 // Cairo::RefPtr<Cairo::Context> ctx = Cairo::Context::create(surface);
781 // Gdk::Cairo::set_source_pixbuf(ctx, bmp, 0.0, 0.0);
782 cairo_t *ctx = cairo_create(ret);
789 void container_linux::get_media_features(litehtml::media_features& media) const
791 litehtml::position client;
792 get_client_rect(client);
793 media.type = litehtml::media_type_screen;
794 media.width = client.width;
795 media.height = client.height;
796 media.device_width = gdk_screen_width();
797 media.device_height = gdk_screen_height();
799 media.monochrome = 0;
800 media.color_index = 256;
801 media.resolution = 96;
804 void container_linux::get_language(litehtml::tstring& language, litehtml::tstring& culture) const
810 void container_linux::link(const std::shared_ptr<litehtml::document> &ptr, const litehtml::element::ptr& el)