3 #include <cairomm/cairomm.h>
4 #include <gtkmm/label.h>
7 #include "pbd/stacktrace.h"
9 #include "canvas/text.h"
10 #include "canvas/canvas.h"
11 #include "canvas/utils.h"
14 using namespace ArdourCanvas;
16 Text::Text (Group* parent)
19 , _font_description (0)
20 , _alignment (Pango::ALIGN_LEFT)
23 , _need_redraw (false)
24 , _clamped_width (COORD_MAX)
31 delete _font_description;
35 Text::set (string const & text)
42 _bounding_box_dirty = true;
48 Text::redraw (Cairo::RefPtr<Cairo::Context> context) const
50 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
52 layout->set_text (_text);
54 if (_font_description) {
55 layout->set_font_description (*_font_description);
58 layout->set_alignment (_alignment);
60 Pango::Rectangle ink_rect = layout->get_ink_extents();
62 _origin.x = ink_rect.get_x() / Pango::SCALE;
63 _origin.y = ink_rect.get_y() / Pango::SCALE;
65 _width = _origin.x + (ink_rect.get_width() / Pango::SCALE);
66 _height = _origin.y + (ink_rect.get_height() / Pango::SCALE);
68 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
70 Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
72 /* and draw, in the appropriate color of course */
74 set_source_rgba (img_context, _color);
75 layout->show_in_cairo_context (img_context);
77 /* text has now been rendered in _image and is ready for blit in
85 Text::clamp_width (double w)
91 Text::compute_bounding_box () const
93 if (!_canvas || _text.empty()) {
94 _bounding_box = boost::optional<Rect> ();
95 _bounding_box_dirty = false;
99 PangoContext* _pc = gdk_pango_context_get ();
100 Glib::RefPtr<Pango::Context> context = Glib::wrap (_pc); // context now owns _pc and will free it
101 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
103 layout->set_text (_text);
104 layout->set_font_description (*_font_description);
105 layout->set_alignment (_alignment);
106 Pango::Rectangle const r = layout->get_ink_extents ();
108 _bounding_box = Rect (
109 r.get_x() / Pango::SCALE,
110 r.get_y() / Pango::SCALE,
111 (r.get_x() + r.get_width()) / Pango::SCALE,
112 (r.get_y() + r.get_height()) / Pango::SCALE
115 _bounding_box_dirty = false;
119 Text::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
129 context->set_source (_image, 0, 0);
130 context->rectangle (0, 0, min (_clamped_width, _width), _height);
135 Text::set_alignment (Pango::Alignment alignment)
139 _alignment = alignment;
141 _bounding_box_dirty = true;
146 Text::set_font_description (Pango::FontDescription font_description)
150 _font_description = new Pango::FontDescription (font_description);
153 _bounding_box_dirty = true;
158 Text::set_color (Color color)
170 Text::dump (ostream& o) const
174 o << _canvas->indent() << '\t' << " text = " << _text << endl
175 << _canvas->indent() << " color = " << _color;