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/window.h>
24 #include <gtkmm/label.h>
26 #include "pbd/stacktrace.h"
28 #include "canvas/text.h"
29 #include "canvas/canvas.h"
30 #include "canvas/utils.h"
31 #include "canvas/colors.h"
34 using namespace ArdourCanvas;
37 Text::Text (Canvas* c)
40 , _font_description (0)
41 , _alignment (Pango::ALIGN_LEFT)
44 , _need_redraw (false)
45 , _width_correction (-1)
46 , _clamped_width (COORD_MAX)
51 Text::Text (Item* parent)
54 , _font_description (0)
55 , _alignment (Pango::ALIGN_LEFT)
58 , _need_redraw (false)
59 , _width_correction (-1)
60 , _clamped_width (COORD_MAX)
67 delete _font_description;
71 Text::set (string const & text)
82 _bounding_box_dirty = true;
88 Text::_redraw () const
90 assert (!_text.empty());
91 Glib::RefPtr<Pango::Context> context = Glib::wrap (gdk_pango_context_get());
92 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
95 if (_width_correction < 0.0) {
96 // Pango returns incorrect text width on some OS X
97 // So we have to make a correction
98 // To determine the correct indent take the largest symbol for which the width is correct
99 // and make the calculation
107 Glib::RefPtr<Pango::Layout> test_layout = foo.create_pango_layout ("H");
108 if (_font_description) {
109 test_layout->set_font_description (*_font_description);
111 test_layout->get_pixel_size (width, height);
113 _width_correction = width*1.5;
116 /* don't bother with a conditional here */
117 _width_correction = 0.0;
120 layout->set_text (_text);
122 if (_font_description) {
123 layout->set_font_description (*_font_description);
126 layout->set_alignment (_alignment);
131 layout->get_pixel_size (w, h);
133 _width = w + _width_correction;
137 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width * 2, _height * 2);
139 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
142 Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
145 /* Below, the rendering scaling is set to support retina display
147 img_context->scale (2, 2);
150 /* and draw, in the appropriate color of course */
153 set_source_rgba (img_context, _outline_color);
154 layout->update_from_cairo_context (img_context);
155 pango_cairo_layout_path (img_context->cobj(), layout->gobj());
156 img_context->stroke_preserve ();
157 set_source_rgba (img_context, _color);
158 img_context->fill ();
160 set_source_rgba (img_context, _color);
161 layout->show_in_cairo_context (img_context);
164 /* text has now been rendered in _image and is ready for blit in
168 _need_redraw = false;
172 Text::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
178 Rect self = item_to_window (Rect (0, 0, min (_clamped_width, (double)_image->get_width ()), _image->get_height ()));
179 boost::optional<Rect> i = self.intersection (area);
189 Rect intersection (i.get());
191 context->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
193 /* Below, the rendering scaling is set to support retina display
195 Cairo::Matrix original_matrix = context->get_matrix();
196 context->scale (0.5, 0.5);
197 context->set_source (_image, self.x0 * 2, self.y0 * 2);
199 context->set_matrix (original_matrix);
201 context->set_source (_image, self.x0, self.y0);
207 Text::clamp_width (double w)
209 if (_clamped_width == w) {
214 _bounding_box_dirty = true;
219 Text::compute_bounding_box () const
221 if (!_canvas || _text.empty()) {
222 _bounding_box = boost::optional<Rect> ();
223 _bounding_box_dirty = false;
227 if (_bounding_box_dirty) {
229 const float retina_factor = 0.5;
231 const float retina_factor = 1.0;
233 if (_need_redraw || !_image) {
236 _bounding_box = Rect (0, 0, min (_clamped_width, (double) _image->get_width() * retina_factor), _image->get_height() * retina_factor);
237 _bounding_box_dirty = false;
242 Text::set_alignment (Pango::Alignment alignment)
244 if (alignment == _alignment) {
250 _alignment = alignment;
252 _bounding_box_dirty = true;
257 Text::set_font_description (Pango::FontDescription font_description)
261 _font_description = new Pango::FontDescription (font_description);
263 _width_correction = -1.0;
265 _bounding_box_dirty = true;
270 Text::set_color (Color color)
272 if (color == _color) {
280 set_outline_color (contrasting_text_color (_color));
289 Text::dump (ostream& o) const
293 o << _canvas->indent() << '\t' << " text = " << _text << endl
294 << _canvas->indent() << " color = " << _color;
301 Text::text_width() const