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"
30 #include "canvas/colors.h"
33 using namespace ArdourCanvas;
35 Text::Text (Canvas* c)
38 , _font_description (0)
39 , _alignment (Pango::ALIGN_LEFT)
42 , _need_redraw (false)
43 , _clamped_width (COORD_MAX)
48 Text::Text (Item* parent)
51 , _font_description (0)
52 , _alignment (Pango::ALIGN_LEFT)
55 , _need_redraw (false)
56 , _clamped_width (COORD_MAX)
63 delete _font_description;
67 Text::set (string const & text)
74 _bounding_box_dirty = true;
80 Text::_redraw (Cairo::RefPtr<Cairo::Context> context) const
86 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
92 Text::_redraw (Glib::RefPtr<Pango::Context> context) const
98 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
103 Text::__redraw (Glib::RefPtr<Pango::Layout> layout) const
105 layout->set_text (_text);
107 if (_font_description) {
108 layout->set_font_description (*_font_description);
111 layout->set_alignment (_alignment);
116 layout->get_pixel_size (w, h);
121 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
123 Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
125 /* and draw, in the appropriate color of course */
128 set_source_rgba (img_context, _outline_color);
129 layout->update_from_cairo_context (img_context);
130 pango_cairo_layout_path (img_context->cobj(), layout->gobj());
131 img_context->stroke_preserve ();
132 set_source_rgba (img_context, _color);
133 img_context->fill ();
135 set_source_rgba (img_context, _color);
136 layout->show_in_cairo_context (img_context);
139 /* text has now been rendered in _image and is ready for blit in
143 _need_redraw = false;
147 Text::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
153 Rect self = item_to_window (Rect (0, 0, min (_clamped_width, (double)_image->get_width ()), _image->get_height ()));
154 boost::optional<Rect> i = self.intersection (area);
164 Rect intersection (i.get());
166 context->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
167 context->set_source (_image, self.x0, self.y0);
172 Text::clamp_width (double w)
176 _bounding_box_dirty = true;
181 Text::compute_bounding_box () const
183 if (!_canvas || _text.empty()) {
184 _bounding_box = boost::optional<Rect> ();
185 _bounding_box_dirty = false;
189 if (_bounding_box_dirty) {
190 if (_need_redraw || !_image) {
191 Glib::RefPtr<Pango::Context> context = Glib::wrap (gdk_pango_context_get()); // context now owns C object and will free it
194 _bounding_box = Rect (0, 0, min (_clamped_width, (double) _image->get_width()), _image->get_height());
195 _bounding_box_dirty = false;
200 Text::set_alignment (Pango::Alignment alignment)
204 _alignment = alignment;
206 _bounding_box_dirty = true;
211 Text::set_font_description (Pango::FontDescription font_description)
215 _font_description = new Pango::FontDescription (font_description);
218 _bounding_box_dirty = true;
223 Text::set_color (Color color)
229 set_outline_color (contrasting_text_color (_color));
238 Text::dump (ostream& o) const
242 o << _canvas->indent() << '\t' << " text = " << _text << endl
243 << _canvas->indent() << " color = " << _color;