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;
106 Text::_redraw () const
108 assert (!_text.empty());
110 Glib::RefPtr<Pango::Context> context = _canvas->get_pango_context();
111 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
113 #if 0 // def __APPLE__ // Looks like this is no longer needed 2017-03-11, pango 1.36.8, pangomm 2.34.0
114 if (_width_correction < 0.0) {
115 // Pango returns incorrect text width on some OS X
116 // So we have to make a correction
117 // To determine the correct indent take the largest symbol for which the width is correct
118 // and make the calculation
126 Glib::RefPtr<Pango::Layout> test_layout = foo.create_pango_layout ("H");
127 if (_font_description) {
128 test_layout->set_font_description (*_font_description);
130 test_layout->get_pixel_size (width, height);
132 _width_correction = width*1.5;
135 /* don't bother with a conditional here */
136 _width_correction = 0.0;
139 layout->set_text (_text);
141 if (_font_description) {
142 layout->set_font_description (*_font_description);
145 layout->set_alignment (_alignment);
150 layout->get_pixel_size (w, h);
152 _width = w + _width_correction;
156 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width * 2, _height * 2);
158 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
161 Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
164 /* Below, the rendering scaling is set to support retina display
166 img_context->scale (2, 2);
169 /* and draw, in the appropriate color of course */
172 set_source_rgba (img_context, _outline_color);
173 layout->update_from_cairo_context (img_context);
174 pango_cairo_layout_path (img_context->cobj(), layout->gobj());
175 img_context->stroke_preserve ();
176 set_source_rgba (img_context, _color);
177 img_context->fill ();
179 set_source_rgba (img_context, _color);
180 layout->show_in_cairo_context (img_context);
183 /* text has now been rendered in _image and is ready for blit in
187 _need_redraw = false;
191 Text::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
197 Rect self = item_to_window (Rect (0, 0, min (_clamped_width, (double)_image->get_width ()), _image->get_height ()));
198 Rect i = self.intersection (area);
208 Rect intersection (i);
210 context->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
212 /* Below, the rendering scaling is set to support retina display
214 Cairo::Matrix original_matrix = context->get_matrix();
215 context->scale (0.5, 0.5);
216 context->set_source (_image, self.x0 * 2, self.y0 * 2);
218 context->set_matrix (original_matrix);
220 context->set_source (_image, self.x0, self.y0);
226 Text::clamp_width (double w)
228 if (_clamped_width == w) {
233 _bounding_box_dirty = true;
238 Text::compute_bounding_box () const
240 if (!_canvas || _text.empty()) {
241 _bounding_box = Rect ();
242 _bounding_box_dirty = false;
246 if (_bounding_box_dirty) {
248 const float retina_factor = 0.5;
250 const float retina_factor = 1.0;
252 if (_need_redraw || !_image) {
255 _bounding_box = Rect (0, 0, min (_clamped_width, (double) _image->get_width() * retina_factor), _image->get_height() * retina_factor);
256 _bounding_box_dirty = false;
261 Text::set_alignment (Pango::Alignment alignment)
263 if (alignment == _alignment) {
269 _alignment = alignment;
271 _bounding_box_dirty = true;
276 Text::set_font_description (Pango::FontDescription font_description)
280 _font_description = new Pango::FontDescription (font_description);
282 _width_correction = -1.0;
284 _bounding_box_dirty = true;
289 Text::set_color (Color color)
291 if (color == _color) {
299 set_outline_color (contrasting_text_color (_color));
308 Text::dump (ostream& o) const
312 o << _canvas->indent() << '\t' << " text = " << _text << endl
313 << _canvas->indent() << " color = " << _color;
320 Text::text_width() const