Use Pango to render text in Litehtml plugin
[claws.git] / src / plugins / litehtml_viewer / container_linux.cpp
index c34df5fbb339d7208f0dd67ad646433d5777b3a7..15f8836ad3be5aa77387131077e5afa9116f8eb6 100644 (file)
@@ -46,169 +46,6 @@ container_linux::~container_linux(void)
        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);
-}
-
 int container_linux::pt_to_px( int pt )
 {
        GdkScreen* screen = gdk_screen_get_default();
@@ -217,11 +54,6 @@ int container_linux::pt_to_px( int pt )
        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())
@@ -278,14 +110,25 @@ void container_linux::load_image( const litehtml::tchar_t* src, const litehtml::
 {
        litehtml::tstring url;
        make_url(src, baseurl, url);
-       if(m_images.find(url.c_str()) == m_images.end())
+       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[url.c_str()] = img;
+                               m_images.push_back(std::make_pair(url, img));
                        }
                } catch(...)
                {
@@ -299,9 +142,19 @@ void container_linux::get_image_size( const litehtml::tchar_t* src, const liteht
 {
        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;
+               }
+       }
 
-       images_map::iterator img = m_images.find(url.c_str());
-       if(img != m_images.end())
+       if(img != NULL)
        {
                sz.width        = gdk_pixbuf_get_width(img->second);
                sz.height       = gdk_pixbuf_get_height(img->second);
@@ -334,8 +187,19 @@ void container_linux::draw_background( litehtml::uint_ptr hdc, const litehtml::b
        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)
+       bool found = false;
+       const image *img_i = NULL;
+
+       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)
        {
                GdkPixbuf *bgbmp = img_i->second;
 
@@ -791,20 +655,52 @@ void container_linux::fill_ellipse( cairo_t* cr, int x, int y, int width, int he
 
 void container_linux::clear_images()
 {
-/*     for(images_map::iterator i = m_images.begin(); i != m_images.end(); i++)
-       {
-               if(i->second)
-               {
-                       delete i->second;
+       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();
-*/
 }
 
-const litehtml::tchar_t* container_linux::get_default_font_name() const
+gint container_linux::clear_images(gint desired_size)
 {
-       return "Times New Roman";
+       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,
@@ -887,6 +783,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);
+       cairo_destroy(ctx);
 
        return ret;
 }