lazy redraw of Canvas::Text image

This commit is contained in:
Paul Davis 2013-04-06 19:04:34 -04:00
parent b9750bac72
commit f2f92aaf8c
2 changed files with 33 additions and 18 deletions

View File

@ -28,15 +28,16 @@ public:
private: private:
std::string _text; std::string _text;
Cairo::RefPtr<Cairo::ImageSurface> _image;
uint32_t _color; uint32_t _color;
Pango::FontDescription* _font_description; Pango::FontDescription* _font_description;
Pango::Alignment _alignment; Pango::Alignment _alignment;
Duple _origin; mutable Cairo::RefPtr<Cairo::ImageSurface> _image;
int _width; mutable Duple _origin;
int _height; mutable int _width;
mutable int _height;
mutable bool _need_redraw;
void redraw (); void redraw (Cairo::RefPtr<Cairo::Context>) const;
}; };
} }

View File

@ -12,12 +12,12 @@ using namespace ArdourCanvas;
Text::Text (Group* parent) Text::Text (Group* parent)
: Item (parent) : Item (parent)
, _image (0)
, _color (0x000000ff) , _color (0x000000ff)
, _font_description (0) , _font_description (0)
, _alignment (Pango::ALIGN_LEFT) , _alignment (Pango::ALIGN_LEFT)
, _width (0) , _width (0)
, _height (0) , _height (0)
, _need_redraw (false)
{ {
} }
@ -34,18 +34,15 @@ Text::set (string const & text)
_text = text; _text = text;
redraw (); _need_redraw = true;
_bounding_box_dirty = true; _bounding_box_dirty = true;
end_change (); end_change ();
} }
void void
Text::redraw () Text::redraw (Cairo::RefPtr<Cairo::Context> context) const
{ {
_image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (_image);
Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context); Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
layout->set_text (_text); layout->set_text (_text);
@ -63,11 +60,20 @@ Text::redraw ()
_width = (ink_rect.get_width() + Pango::SCALE / 2) / Pango::SCALE; _width = (ink_rect.get_width() + Pango::SCALE / 2) / Pango::SCALE;
_height = (ink_rect.get_height() + Pango::SCALE / 2) / Pango::SCALE; _height = (ink_rect.get_height() + Pango::SCALE / 2) / Pango::SCALE;
set_source_rgba (context, _color); _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
layout->show_in_cairo_context (context); Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
/* text has now been rendered */ /* and draw, in the appropriate color of course */
set_source_rgba (img_context, _color);
layout->show_in_cairo_context (img_context);
/* text has now been rendered in _image and is ready for blit in
* ::render
*/
_need_redraw = false;
} }
void void
@ -80,6 +86,14 @@ Text::compute_bounding_box () const
void void
Text::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const Text::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
{ {
if (_text.empty()) {
return;
}
if (_need_redraw) {
redraw (context);
}
context->set_source (_image, 0, 0); context->set_source (_image, 0, 0);
context->rectangle (0, 0, _width, _height); context->rectangle (0, 0, _width, _height);
context->fill (); context->fill ();
@ -109,7 +123,7 @@ Text::set_alignment (Pango::Alignment alignment)
begin_change (); begin_change ();
_alignment = alignment; _alignment = alignment;
redraw (); _need_redraw = true;
_bounding_box_dirty = true; _bounding_box_dirty = true;
end_change (); end_change ();
} }
@ -120,7 +134,7 @@ Text::set_font_description (Pango::FontDescription font_description)
begin_change (); begin_change ();
_font_description = new Pango::FontDescription (font_description); _font_description = new Pango::FontDescription (font_description);
redraw (); _need_redraw = true;
_bounding_box_dirty = true; _bounding_box_dirty = true;
end_change (); end_change ();
@ -132,7 +146,7 @@ Text::set_color (Color color)
begin_change (); begin_change ();
_color = color; _color = color;
redraw (); _need_redraw = true;
end_change (); end_change ();
} }