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;
61 _width = _origin.x + ((ink_rect.get_width() + Pango::SCALE / 2) / Pango::SCALE);
62 _height = _origin.y + ((ink_rect.get_height() + Pango::SCALE / 2) / Pango::SCALE);
64 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
66 Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
68 /* and draw, in the appropriate color of course */
70 set_source_rgba (img_context, _color);
72 std::cerr << "render " << _text << " as "
73 << ((_color >> 24) & 0xff) / 255.0
74 << ((_color >> 16) & 0xff) / 255.0
75 << ((_color >> 8) & 0xff) / 255.0
76 << ((_color >> 0) & 0xff) / 255.
79 layout->show_in_cairo_context (img_context);
81 /* text has now been rendered in _image and is ready for blit in
89 Text::compute_bounding_box () const
91 if (!_canvas || !_canvas->context () || _text.empty()) {
92 _bounding_box = boost::optional<Rect> ();
93 _bounding_box_dirty = false;
97 redraw (_canvas->context());
99 _bounding_box = Rect (_origin.x, _origin.y, _width - _origin.x, _height - _origin.y);
100 _bounding_box_dirty = false;
102 cerr << "bbox for " << _text << " = " << _bounding_box << endl;
106 Text::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
116 cerr << " with " << _origin << " and " << _width << " x " << _height << " render " << _text << endl;
118 context->set_source (_image, 0, 0);
119 context->rectangle (0, 0, _width, _height);
124 Text::get_state () const
126 XMLNode* node = new XMLNode ("Text");
128 if (!name.empty ()) {
129 node->add_property ("name", name);
136 Text::set_state (XMLNode const * /*node*/)
142 Text::set_alignment (Pango::Alignment alignment)
146 _alignment = alignment;
148 _bounding_box_dirty = true;
153 Text::set_font_description (Pango::FontDescription font_description)
157 _font_description = new Pango::FontDescription (font_description);
160 _bounding_box_dirty = true;
165 Text::set_color (Color color)
177 Text::dump (ostream& o) const
181 o << _canvas->indent() << '\t' << " text = " << _text << endl
182 << _canvas->indent() << " color = " << _color;