1 #include <cairomm/cairomm.h>
2 #include <gtkmm/label.h>
6 #include "canvas/text.h"
7 #include "canvas/canvas.h"
8 #include "canvas/utils.h"
11 using namespace ArdourCanvas;
13 Text::Text (Group* parent)
16 , _font_description (0)
17 , _alignment (Pango::ALIGN_LEFT)
20 , _need_redraw (false)
27 delete _font_description;
31 Text::set (string const & text)
38 _bounding_box_dirty = true;
44 Text::redraw (Cairo::RefPtr<Cairo::Context> context) const
46 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
48 layout->set_text (_text);
50 if (_font_description) {
51 layout->set_font_description (*_font_description);
54 layout->set_alignment (_alignment);
56 Pango::Rectangle ink_rect = layout->get_ink_extents();
58 _origin.x = ink_rect.get_x() / Pango::SCALE;
59 _origin.y = ink_rect.get_y() / Pango::SCALE;
60 _width = (ink_rect.get_width() + Pango::SCALE / 2) / Pango::SCALE;
61 _height = (ink_rect.get_height() + Pango::SCALE / 2) / Pango::SCALE;
63 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
65 Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
67 /* and draw, in the appropriate color of course */
69 set_source_rgba (img_context, _color);
70 layout->show_in_cairo_context (img_context);
72 /* text has now been rendered in _image and is ready for blit in
80 Text::compute_bounding_box () const
82 _bounding_box = Rect (_origin.x, _origin.y, _origin.x + _width, _origin.y + _height);
83 _bounding_box_dirty = false;
87 Text::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
97 context->set_source (_image, 0, 0);
98 context->rectangle (0, 0, _width, _height);
103 Text::get_state () const
105 XMLNode* node = new XMLNode ("Text");
107 if (!name.empty ()) {
108 node->add_property ("name", name);
115 Text::set_state (XMLNode const * /*node*/)
121 Text::set_alignment (Pango::Alignment alignment)
125 _alignment = alignment;
127 _bounding_box_dirty = true;
132 Text::set_font_description (Pango::FontDescription font_description)
136 _font_description = new Pango::FontDescription (font_description);
139 _bounding_box_dirty = true;
144 Text::set_color (Color color)