Fix CID 1492251: resource leaks.
[claws.git] / src / plugins / litehtml_viewer / container_linux.cpp
index c34df5fbb339d7208f0dd67ad646433d5777b3a7..4a455696fbceddceccbfe3ac74148be38d6069ea 100644 (file)
@@ -1,9 +1,7 @@
 /*
  * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
 /*
  * 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
  * 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);
 {
        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)
 }
 
 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);
        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 )
 }
 
 int container_linux::pt_to_px( int pt )
@@ -217,11 +54,6 @@ int container_linux::pt_to_px( int pt )
        return (int) ((double) pt * dpi / 72.0);
 }
 
        return (int) ((double) pt * dpi / 72.0);
 }
 
-int container_linux::get_default_font_size() const
-{
-       return 16;
-}
-
 void container_linux::draw_list_marker( litehtml::uint_ptr hdc, const litehtml::list_marker& marker )
 {
        if(!marker.image.empty())
 void container_linux::draw_list_marker( litehtml::uint_ptr hdc, const litehtml::list_marker& marker )
 {
        if(!marker.image.empty())
@@ -274,44 +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);
-       if(m_images.find(url.c_str()) == m_images.end())
-       {
-               try
-               {
-                       GdkPixbuf *img = get_image(url.c_str(), true);
-                       if(img)
-                       {
-                               m_images[url.c_str()] = 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);
-
-       images_map::iterator img = m_images.find(url.c_str());
-       if(img != m_images.end())
-       {
-               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;
 void container_linux::draw_background( litehtml::uint_ptr hdc, const litehtml::background_paint& bg )
 {
        cairo_t* cr = (cairo_t*) hdc;
@@ -333,11 +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);
 
        litehtml::tstring url;
        make_url(bg.image.c_str(), bg.baseurl.c_str(), url);
 
-       //lock_images_cache();
-       images_map::iterator img_i = m_images.find(url.c_str());
-       if(img_i != m_images.end() && img_i->second)
+       lock_images_cache();
+
+       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))
 
                GdkPixbuf *new_img;
                if(bg.image_size.width != gdk_pixbuf_get_width(bgbmp) || bg.image_size.height != gdk_pixbuf_get_height(bgbmp))
@@ -383,7 +178,8 @@ void container_linux::draw_background( litehtml::uint_ptr hdc, const litehtml::b
                cairo_surface_destroy(img);
 
        }
                cairo_surface_destroy(img);
 
        }
-//     unlock_images_cache();
+
+       unlock_images_cache();
        cairo_restore(cr);
 }
 
        cairo_restore(cr);
 }
 
@@ -789,24 +585,6 @@ void container_linux::fill_ellipse( cairo_t* cr, int x, int y, int width, int he
        cairo_restore(cr);
 }
 
        cairo_restore(cr);
 }
 
-void container_linux::clear_images()
-{
-/*     for(images_map::iterator i = m_images.begin(); i != m_images.end(); i++)
-       {
-               if(i->second)
-               {
-                       delete i->second;
-               }
-       }
-       m_images.clear();
-*/
-}
-
-const litehtml::tchar_t* container_linux::get_default_font_name() const
-{
-       return "Times New Roman";
-}
-
 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)
 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)
@@ -887,6 +665,7 @@ cairo_surface_t* container_linux::surface_from_pixbuf(const GdkPixbuf *bmp)
 //     Gdk::Cairo::set_source_pixbuf(ctx, bmp, 0.0, 0.0);
        cairo_t *ctx = cairo_create(ret);
        cairo_paint(ctx);
 //     Gdk::Cairo::set_source_pixbuf(ctx, bmp, 0.0, 0.0);
        cairo_t *ctx = cairo_create(ret);
        cairo_paint(ctx);
+       cairo_destroy(ctx);
 
        return ret;
 }
 
        return ret;
 }