2 Copyright (C) 2011-2013 Paul Davis
3 Author: Carl Hetherington <cth@carlh.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <cairomm/cairomm.h>
23 #include <gtkmm/label.h>
25 #include "pbd/stacktrace.h"
27 #include "canvas/text.h"
28 #include "canvas/canvas.h"
29 #include "canvas/utils.h"
32 using namespace ArdourCanvas;
34 Text::Text (Group* parent)
37 , _font_description (0)
38 , _alignment (Pango::ALIGN_LEFT)
41 , _need_redraw (false)
42 , _clamped_width (COORD_MAX)
49 delete _font_description;
53 Text::set (string const & text)
60 _bounding_box_dirty = true;
66 Text::redraw (Cairo::RefPtr<Cairo::Context> context) const
68 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
70 layout->set_text (_text);
72 if (_font_description) {
73 layout->set_font_description (*_font_description);
76 layout->set_alignment (_alignment);
78 Pango::Rectangle ink_rect = layout->get_ink_extents();
80 _origin.x = ink_rect.get_x() / Pango::SCALE;
81 _origin.y = ink_rect.get_y() / Pango::SCALE;
83 _width = _origin.x + (ink_rect.get_width() / Pango::SCALE);
84 _height = _origin.y + (ink_rect.get_height() / Pango::SCALE);
86 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
88 Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
90 /* and draw, in the appropriate color of course */
92 set_source_rgba (img_context, _color);
93 layout->show_in_cairo_context (img_context);
95 /* text has now been rendered in _image and is ready for blit in
103 Text::clamp_width (double w)
109 Text::compute_bounding_box () const
111 if (!_canvas || _text.empty()) {
112 _bounding_box = boost::optional<Rect> ();
113 _bounding_box_dirty = false;
117 PangoContext* _pc = gdk_pango_context_get ();
118 Glib::RefPtr<Pango::Context> context = Glib::wrap (_pc); // context now owns _pc and will free it
119 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
121 layout->set_text (_text);
122 layout->set_font_description (*_font_description);
123 layout->set_alignment (_alignment);
124 Pango::Rectangle const r = layout->get_ink_extents ();
126 _bounding_box = Rect (
127 r.get_x() / Pango::SCALE,
128 r.get_y() / Pango::SCALE,
129 (r.get_x() + r.get_width()) / Pango::SCALE,
130 (r.get_y() + r.get_height()) / Pango::SCALE
133 _bounding_box_dirty = false;
137 Text::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
147 context->set_source (_image, 0, 0);
148 context->rectangle (0, 0, min (_clamped_width, _width), _height);
153 Text::set_alignment (Pango::Alignment alignment)
157 _alignment = alignment;
159 _bounding_box_dirty = true;
164 Text::set_font_description (Pango::FontDescription font_description)
168 _font_description = new Pango::FontDescription (font_description);
171 _bounding_box_dirty = true;
176 Text::set_color (Color color)
188 Text::dump (ostream& o) const
192 o << _canvas->indent() << '\t' << " text = " << _text << endl
193 << _canvas->indent() << " color = " << _color;