Skip to content

Commit e5ca809

Browse files
committed
Removed dependency of the Gdk::Pixbuf and images cache
1 parent e60910c commit e5ca809

File tree

2 files changed

+43
-62
lines changed

2 files changed

+43
-62
lines changed

containers/linux/container_linux.cpp

Lines changed: 40 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -247,43 +247,17 @@ void container_linux::draw_list_marker( litehtml::uint_ptr hdc, const litehtml::
247247
}
248248
}
249249

250-
void container_linux::load_image( const char* src, const char* baseurl, bool /*redraw_on_ready*/ )
251-
{
252-
litehtml::string url;
253-
make_url(src, baseurl, url);
254-
if(m_images.find(url) == m_images.end())
255-
{
256-
try
257-
{
258-
Glib::RefPtr<Gdk::Pixbuf> img = get_image(url.c_str(), true);
259-
if(img)
260-
{
261-
m_images[url.c_str()] = img;
262-
}
263-
} catch(...)
264-
{
265-
m_images[url.c_str()] = Glib::RefPtr<Gdk::Pixbuf>(nullptr);
266-
}
267-
}
268-
}
269-
270250
void container_linux::get_image_size( const char* src, const char* baseurl, litehtml::size& sz )
271251
{
272252
litehtml::string url;
273253
make_url(src, baseurl, url);
274254

275-
auto img = m_images.find(url);
276-
if(img != m_images.end())
277-
{
278-
if(img->second)
279-
{
280-
sz.width = img->second->get_width();
281-
sz.height = img->second->get_height();
282-
} else
283-
{
284-
sz.width = 0;
285-
sz.height = 0;
286-
}
255+
auto img = get_image(url);
256+
if(img)
257+
{
258+
sz.width = cairo_image_surface_get_width(img);
259+
sz.height = cairo_image_surface_get_height(img);
260+
cairo_surface_destroy(img);
287261
} else
288262
{
289263
sz.width = 0;
@@ -323,21 +297,17 @@ void container_linux::draw_background( litehtml::uint_ptr hdc, const std::vector
323297
std::string url;
324298
make_url(bg.image.c_str(), bg.baseurl.c_str(), url);
325299

326-
//lock_images_cache();
327-
auto img_i = m_images.find(url);
328-
if(img_i != m_images.end() && img_i->second)
300+
auto bgbmp = get_image(url);
301+
if(bgbmp)
329302
{
330-
Glib::RefPtr<Gdk::Pixbuf> bgbmp = img_i->second;
331-
332-
Glib::RefPtr<Gdk::Pixbuf> new_img;
333-
if(bg.image_size.width != bgbmp->get_width() || bg.image_size.height != bgbmp->get_height())
303+
if(bg.image_size.width != cairo_image_surface_get_width(bgbmp) || bg.image_size.height != cairo_image_surface_get_height(bgbmp))
334304
{
335-
new_img = bgbmp->scale_simple(bg.image_size.width, bg.image_size.height, Gdk::INTERP_BILINEAR);
305+
auto new_img = scale_surface(bgbmp, bg.image_size.width, bg.image_size.height);
306+
cairo_surface_destroy(bgbmp);
336307
bgbmp = new_img;
337308
}
338309

339-
cairo_surface_t* img = surface_from_pixbuf(bgbmp);
340-
cairo_pattern_t *pattern = cairo_pattern_create_for_surface(img);
310+
cairo_pattern_t *pattern = cairo_pattern_create_for_surface(bgbmp);
341311
cairo_matrix_t flib_m;
342312
cairo_matrix_init_identity(&flib_m);
343313
cairo_matrix_translate(&flib_m, -bg.position_x, -bg.position_y);
@@ -347,18 +317,18 @@ void container_linux::draw_background( litehtml::uint_ptr hdc, const std::vector
347317
switch(bg.repeat)
348318
{
349319
case litehtml::background_repeat_no_repeat:
350-
draw_pixbuf(cr, bgbmp, bg.position_x, bg.position_y, bgbmp->get_width(), bgbmp->get_height());
320+
draw_pixbuf(cr, bgbmp, bg.position_x, bg.position_y, cairo_image_surface_get_width(bgbmp), cairo_image_surface_get_height(bgbmp));
351321
break;
352322

353323
case litehtml::background_repeat_repeat_x:
354324
cairo_set_source(cr, pattern);
355-
cairo_rectangle(cr, bg.clip_box.left(), bg.position_y, bg.clip_box.width, bgbmp->get_height());
325+
cairo_rectangle(cr, bg.clip_box.left(), bg.position_y, bg.clip_box.width, cairo_image_surface_get_height(bgbmp));
356326
cairo_fill(cr);
357327
break;
358328

359329
case litehtml::background_repeat_repeat_y:
360330
cairo_set_source(cr, pattern);
361-
cairo_rectangle(cr, bg.position_x, bg.clip_box.top(), bgbmp->get_width(), bg.clip_box.height);
331+
cairo_rectangle(cr, bg.position_x, bg.clip_box.top(), cairo_image_surface_get_width(bgbmp), bg.clip_box.height);
362332
cairo_fill(cr);
363333
break;
364334

@@ -370,9 +340,8 @@ void container_linux::draw_background( litehtml::uint_ptr hdc, const std::vector
370340
}
371341

372342
cairo_pattern_destroy(pattern);
373-
cairo_surface_destroy(img);
343+
cairo_surface_destroy(bgbmp);
374344
}
375-
//unlock_images_cache();
376345
}
377346

378347
cairo_restore(cr);
@@ -681,25 +650,38 @@ void container_linux::rounded_rectangle( cairo_t* cr, const litehtml::position &
681650
}
682651
}
683652

684-
void container_linux::draw_pixbuf(cairo_t* cr, const Glib::RefPtr<Gdk::Pixbuf>& bmp, int x, int y, int cx, int cy)
653+
cairo_surface_t* container_linux::scale_surface(cairo_surface_t* surface, int width, int height)
654+
{
655+
int s_width = cairo_image_surface_get_width(surface);
656+
int s_height = cairo_image_surface_get_height(surface);
657+
cairo_surface_t *result = cairo_surface_create_similar(surface, cairo_surface_get_content(surface), width, height);
658+
cairo_t *cr = cairo_create(result);
659+
cairo_scale(cr, (double) width / (double) s_width, (double) height / (double) s_height);
660+
cairo_set_source_surface(cr, surface, 0, 0);
661+
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
662+
cairo_paint(cr);
663+
cairo_destroy(cr);
664+
return result;
665+
}
666+
667+
void container_linux::draw_pixbuf(cairo_t* cr, cairo_surface_t* bmp, int x, int y, int cx, int cy)
685668
{
686669
cairo_save(cr);
687670

688671
{
689-
Cairo::RefPtr<Cairo::Context> crobj(new Cairo::Context(cr, false));
690-
691-
cairo_matrix_t flib_m;
692-
cairo_matrix_init(&flib_m, 1, 0, 0, -1, 0, 0);
672+
cairo_matrix_t flip_m;
673+
cairo_matrix_init(&flip_m, 1, 0, 0, -1, 0, 0);
693674

694-
if(cx != bmp->get_width() || cy != bmp->get_height())
675+
if(cx != cairo_image_surface_get_width(bmp) || cy != cairo_image_surface_get_height(bmp))
695676
{
696-
Glib::RefPtr<Gdk::Pixbuf> new_img = bmp->scale_simple(cx, cy, Gdk::INTERP_BILINEAR);
697-
Gdk::Cairo::set_source_pixbuf(crobj, new_img, x, y);
698-
crobj->paint();
677+
auto bmp_scaled = scale_surface(bmp, cx, cy);
678+
cairo_set_source_surface(cr, bmp_scaled, x, y);
679+
cairo_paint(cr);
680+
cairo_surface_destroy(bmp_scaled);
699681
} else
700682
{
701-
Gdk::Cairo::set_source_pixbuf(crobj, bmp, x, y);
702-
crobj->paint();
683+
cairo_set_source_surface(cr, bmp, x, y);
684+
cairo_paint(cr);
703685
}
704686
}
705687

containers/linux/container_linux.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class container_linux : public litehtml::document_container
5252
protected:
5353
cairo_surface_t* m_temp_surface;
5454
cairo_t* m_temp_cr;
55-
images_map m_images;
5655
cairo_clip_box::vector m_clips;
5756
public:
5857
container_linux();
@@ -65,7 +64,6 @@ class container_linux : public litehtml::document_container
6564
int pt_to_px(int pt) const override;
6665
int get_default_font_size() const override;
6766
const char* get_default_font_name() const override;
68-
void load_image(const char* src, const char* baseurl, bool redraw_on_ready) override;
6967
void get_image_size(const char* src, const char* baseurl, litehtml::size& sz) override;
7068
void draw_background(litehtml::uint_ptr hdc, const std::vector<litehtml::background_paint>& bg) override;
7169
void draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root) override;
@@ -83,7 +81,7 @@ class container_linux : public litehtml::document_container
8381
void del_clip() override;
8482

8583
virtual void make_url( const char* url, const char* basepath, litehtml::string& out );
86-
virtual Glib::RefPtr<Gdk::Pixbuf> get_image(const char* url, bool redraw_on_ready) = 0;
84+
virtual cairo_surface_t* get_image(const std::string& url) = 0;
8785

8886
void clear_images();
8987

@@ -98,7 +96,8 @@ class container_linux : public litehtml::document_container
9896
static void add_path_arc(cairo_t* cr, double x, double y, double rx, double ry, double a1, double a2, bool neg);
9997
static void set_color(cairo_t* cr, const 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); }
10098
static cairo_surface_t* surface_from_pixbuf(const Glib::RefPtr<Gdk::Pixbuf>& bmp);
101-
static void draw_pixbuf(cairo_t* cr, const Glib::RefPtr<Gdk::Pixbuf>& bmp, int x, int y, int cx, int cy);
99+
static void draw_pixbuf(cairo_t* cr, cairo_surface_t* bmp, int x, int y, int cx, int cy);
100+
static cairo_surface_t* scale_surface(cairo_surface_t* surface, int width, int height);
102101
};
103102

104103
#endif

0 commit comments

Comments
 (0)