Queue a draw event in open_html
[claws.git] / src / plugins / litehtml_viewer / container_linux.cpp
index a2b190859eed45253b19eefb9d11e28d98443f48..4a455696fbceddceccbfe3ac74148be38d6069ea 100644 (file)
@@ -1,9 +1,7 @@
 /*
  * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
- * Copyright(C) 1999-2015 the Claws Mail Team
- * == Fancy Plugin ==
- * This file Copyright (C) 2009-2015 Salvatore De Paolis
- * <iwkse@claws-mail.org> and the Claws Mail Team
+ * Copyright(C) 2019 the Claws Mail Team
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 3 of the License, or
@@ -37,6 +35,7 @@ container_linux::container_linux(void)
 {
        m_temp_surface  = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 2, 2);
        m_temp_cr               = cairo_create(m_temp_surface);
+       g_rec_mutex_init(&m_images_lock);
 }
 
 container_linux::~container_linux(void)
@@ -44,169 +43,7 @@ container_linux::~container_linux(void)
        clear_images();
        cairo_surface_destroy(m_temp_surface);
        cairo_destroy(m_temp_cr);
-}
-
-litehtml::uint_ptr container_linux::create_font( const litehtml::tchar_t* faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics* fm )
-{
-       litehtml::string_vector fonts;
-       litehtml::split_string(faceName, fonts, ",");
-       if (! fonts.empty()) {
-           litehtml::trim(fonts[0]);
-       }
-
-       cairo_font_face_t* fnt = 0;
-
-       FcPattern *pattern = FcPatternCreate();
-       bool found = false;
-       for(litehtml::string_vector::iterator i = fonts.begin(); i != fonts.end(); i++)
-       {
-               if(FcPatternAddString(pattern, FC_FAMILY, (unsigned char *) i->c_str()))
-               {
-                       found = true;
-                       break;
-               }
-       }
-       if(found)
-       {
-               if(italic == litehtml::fontStyleItalic )
-               {
-                       FcPatternAddInteger (pattern, FC_SLANT, FC_SLANT_ITALIC);
-               } else
-               {
-                       FcPatternAddInteger (pattern, FC_SLANT, FC_SLANT_ROMAN);
-               }
-
-               int fc_weight = FC_WEIGHT_NORMAL;
-               if(weight >= 0 && weight < 150)                 fc_weight = FC_WEIGHT_THIN;
-               else if(weight >= 150 && weight < 250)  fc_weight = FC_WEIGHT_EXTRALIGHT;
-               else if(weight >= 250 && weight < 350)  fc_weight = FC_WEIGHT_LIGHT;
-               else if(weight >= 350 && weight < 450)  fc_weight = FC_WEIGHT_NORMAL;
-               else if(weight >= 450 && weight < 550)  fc_weight = FC_WEIGHT_MEDIUM;
-               else if(weight >= 550 && weight < 650)  fc_weight = FC_WEIGHT_SEMIBOLD;
-               else if(weight >= 650 && weight < 750)  fc_weight = FC_WEIGHT_BOLD;
-               else if(weight >= 750 && weight < 850)  fc_weight = FC_WEIGHT_EXTRABOLD;
-               else if(weight >= 950)                                  fc_weight = FC_WEIGHT_BLACK;
-
-               FcPatternAddInteger (pattern, FC_WEIGHT, fc_weight);
-
-               fnt = cairo_ft_font_face_create_for_pattern(pattern);
-       }
-
-       FcPatternDestroy(pattern);
-
-       cairo_font* ret = 0;
-
-       if(fm && fnt)
-       {
-               cairo_save(m_temp_cr);
-
-               cairo_set_font_face(m_temp_cr, fnt);
-               cairo_set_font_size(m_temp_cr, size);
-               cairo_font_extents_t ext;
-               cairo_font_extents(m_temp_cr, &ext);
-
-               cairo_text_extents_t tex;
-               cairo_text_extents(m_temp_cr, "x", &tex);
-
-               fm->ascent              = (int) ext.ascent;
-               fm->descent             = (int) ext.descent;
-               fm->height              = (int) (ext.ascent + ext.descent);
-               fm->x_height    = (int) tex.height;
-
-               cairo_restore(m_temp_cr);
-
-               ret = new cairo_font;
-               ret->font               = fnt;
-               ret->size               = size;
-               ret->strikeout  = (decoration & litehtml::font_decoration_linethrough) ? true : false;
-               ret->underline  = (decoration & litehtml::font_decoration_underline) ? true : false;
-
-       }
-
-       return (litehtml::uint_ptr) ret;
-}
-
-void container_linux::delete_font( litehtml::uint_ptr hFont )
-{
-       cairo_font* fnt = (cairo_font*) hFont;
-       if(fnt)
-       {
-               cairo_font_face_destroy(fnt->font);
-               delete fnt;
-       }
-}
-
-int container_linux::text_width( const litehtml::tchar_t* text, litehtml::uint_ptr hFont )
-{
-       cairo_font* fnt = (cairo_font*) hFont;
-
-       cairo_save(m_temp_cr);
-
-       if (fnt) {
-           cairo_set_font_size(m_temp_cr, fnt->size);
-           cairo_set_font_face(m_temp_cr, fnt->font);
-       }
-       cairo_text_extents_t ext;
-       cairo_text_extents(m_temp_cr, text, &ext);
-
-       cairo_restore(m_temp_cr);
-
-       return (int) ext.x_advance;
-}
-
-void container_linux::draw_text( litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos )
-{
-       cairo_font* fnt = (cairo_font*) hFont;
-       cairo_t* cr             = (cairo_t*) hdc;
-       cairo_save(cr);
-
-       apply_clip(cr);
-
-       if (fnt) {
-           cairo_set_font_face(cr, fnt->font);
-           cairo_set_font_size(cr, fnt->size);
-       }
-       cairo_font_extents_t ext;
-       cairo_font_extents(cr, &ext);
-
-       int x = pos.left();
-       int y = pos.bottom()    - ext.descent;
-
-       set_color(cr, color);
-
-       cairo_move_to(cr, x, y);
-       cairo_show_text(cr, text);
-
-       int tw = 0;
-
-       if (fnt) {
-           if(fnt->underline || fnt->strikeout)
-           {
-               tw = text_width(text, hFont);
-           }
-
-           if(fnt->underline)
-           {
-               cairo_set_line_width(cr, 1);
-               cairo_move_to(cr, x, y + 1.5);
-               cairo_line_to(cr, x + tw, y + 1.5);
-               cairo_stroke(cr);
-           }
-           if(fnt->strikeout)
-           {
-               cairo_text_extents_t tex;
-               cairo_text_extents(cr, "x", &tex);
-
-               int ln_y = y - tex.height / 2.0;
-
-               cairo_set_line_width(cr, 1);
-               cairo_move_to(cr, x, (double) ln_y - 0.5);
-               cairo_line_to(cr, x + tw, (double) ln_y - 0.5);
-               cairo_stroke(cr);
-           }
-       }   
-
-       cairo_restore(cr);
+       g_rec_mutex_clear(&m_images_lock);
 }
 
 int container_linux::pt_to_px( int pt )
@@ -269,65 +106,6 @@ void container_linux::draw_list_marker( litehtml::uint_ptr hdc, const litehtml::
        }
 }
 
-void container_linux::load_image( const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready )
-{
-       litehtml::tstring url;
-       make_url(src, baseurl, url);
-       bool found = false;
-
-       for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
-               const image *i = &(*ii);
-
-               if (!strcmp(i->first.c_str(), url.c_str())) {
-                       found = true;
-                       break;
-               }
-       }
-
-       if(!found)
-       {
-               try
-               {
-                       GdkPixbuf *img = get_image(url.c_str(), true);
-                       if(img)
-                       {
-                               m_images.push_back(std::make_pair(url, img));
-                       }
-               } catch(...)
-               {
-                       int iii=0;
-                       iii++;
-               }
-       }
-}
-
-void container_linux::get_image_size( const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz )
-{
-       litehtml::tstring url;
-       make_url(src, baseurl, url);
-       bool found = false;
-       const image *img = NULL;
-
-       for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
-               const image *i = &(*ii);
-               if (i->first == url) {
-                       img = i;
-                       found = true;
-                       break;
-               }
-       }
-
-       if(img != NULL)
-       {
-               sz.width        = gdk_pixbuf_get_width(img->second);
-               sz.height       = gdk_pixbuf_get_height(img->second);
-       } else
-       {
-               sz.width        = 0;
-               sz.height       = 0;
-       }
-}
-
 void container_linux::draw_background( litehtml::uint_ptr hdc, const litehtml::background_paint& bg )
 {
        cairo_t* cr = (cairo_t*) hdc;
@@ -349,22 +127,12 @@ void container_linux::draw_background( litehtml::uint_ptr hdc, const litehtml::b
        litehtml::tstring url;
        make_url(bg.image.c_str(), bg.baseurl.c_str(), url);
 
-       //lock_images_cache();
-       bool found = false;
-       const image *img_i = NULL;
+       lock_images_cache();
 
-       for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
-               const image *i = &(*ii);
-               if (i->first == url) {
-                       img_i = i;
-                       found = true;
-                       break;
-               }
-       }
-
-       if(img_i != NULL && img_i->second)
+       auto i = m_images.find(url);
+       if(i != m_images.end() && i->second.first)
        {
-               GdkPixbuf *bgbmp = img_i->second;
+               GdkPixbuf *bgbmp = i->second.first;
 
                GdkPixbuf *new_img;
                if(bg.image_size.width != gdk_pixbuf_get_width(bgbmp) || bg.image_size.height != gdk_pixbuf_get_height(bgbmp))
@@ -410,7 +178,8 @@ void container_linux::draw_background( litehtml::uint_ptr hdc, const litehtml::b
                cairo_surface_destroy(img);
 
        }
-//     unlock_images_cache();
+
+       unlock_images_cache();
        cairo_restore(cr);
 }
 
@@ -816,56 +585,6 @@ void container_linux::fill_ellipse( cairo_t* cr, int x, int y, int width, int he
        cairo_restore(cr);
 }
 
-void container_linux::clear_images()
-{
-       for(auto i = m_images.begin(); i != m_images.end(); ++i) {
-               image *img = &(*i);
-
-               if (img->second) {
-                       g_object_unref(img->second);
-               }
-       }
-
-       m_images.clear();
-}
-
-gint container_linux::clear_images(gint desired_size)
-{
-       gint size = 0;
-       gint num = 0;
-
-       /* First, tally up size of all the stored GdkPixbufs and
-        * deallocate those which make the total size be above
-        * the desired_size limit. We will remove their list
-        * elements later. */
-       for (auto i = m_images.rbegin(); i != m_images.rend(); ++i) {
-               image *img = &(*i);
-               gint cursize;
-
-               if (img->second == NULL)
-                       continue;
-
-               cursize = gdk_pixbuf_get_byte_length(img->second);
-
-               if (size + cursize > desired_size) {
-                       g_object_unref(img->second);
-                       img->second = NULL;
-                       num++;
-               } else {
-                       size += cursize;
-               }
-       }
-
-       /* Remove elements whose GdkPixbuf pointers point to NULL. */
-       m_images.remove_if([&](image _img) -> bool {
-                       if (_img.second == NULL)
-                               return true;
-                       return false;
-                       });
-
-       return num;
-}
-
 std::shared_ptr<litehtml::element>     container_linux::create_element(const litehtml::tchar_t *tag_name,
                                                                                                                                          const litehtml::string_map &attributes,
                                                                                                                                          const std::shared_ptr<litehtml::document> &doc)