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 "gtkmm2ext/colors.h"
33 using namespace ArdourCanvas;
36 Text::Text (Canvas* c)
39 , _font_description (0)
40 , _alignment (Pango::ALIGN_LEFT)
43 , _need_redraw (false)
44 , _width_correction (-1)
45 , _clamped_width (COORD_MAX)
50 Text::Text (Item* parent)
53 , _font_description (0)
54 , _alignment (Pango::ALIGN_LEFT)
57 , _need_redraw (false)
58 , _width_correction (-1)
59 , _clamped_width (COORD_MAX)
66 delete _font_description;
70 Text::set (string const & text)
81 _bounding_box_dirty = true;
105 Text::_redraw () const
107 assert (!_text.empty());
109 Glib::RefPtr<Pango::Context> context = _canvas->get_pango_context();
110 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
112 #if 0 // def __APPLE__ // Looks like this is no longer needed 2017-03-11, pango 1.36.8, pangomm 2.34.0
113 if (_width_correction < 0.0) {
114 // Pango returns incorrect text width on some OS X
115 // So we have to make a correction
116 // To determine the correct indent take the largest symbol for which the width is correct
117 // and make the calculation
125 Glib::RefPtr<Pango::Layout> test_layout = foo.create_pango_layout ("H");
126 if (_font_description) {
127 test_layout->set_font_description (*_font_description);
129 test_layout->get_pixel_size (width, height);
131 _width_correction = width*1.5;
134 /* don't bother with a conditional here */
135 _width_correction = 0.0;
138 layout->set_text (_text);
140 if (_font_description) {
141 layout->set_font_description (*_font_description);
144 layout->set_alignment (_alignment);
149 layout->get_pixel_size (w, h);
151 _width = w + _width_correction;
155 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width * 2, _height * 2);
157 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
160 Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
163 /* Below, the rendering scaling is set to support retina display
165 img_context->scale (2, 2);
168 /* and draw, in the appropriate color of course */
171 Gtkmm2ext::set_source_rgba (img_context, _outline_color);
172 layout->update_from_cairo_context (img_context);
173 pango_cairo_layout_path (img_context->cobj(), layout->gobj());
174 img_context->stroke_preserve ();
175 Gtkmm2ext::set_source_rgba (img_context, _color);
176 img_context->fill ();
178 Gtkmm2ext::set_source_rgba (img_context, _color);
179 layout->show_in_cairo_context (img_context);
182 /* text has now been rendered in _image and is ready for blit in
186 _need_redraw = false;
190 Text::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
196 Rect self = item_to_window (Rect (0, 0, min (_clamped_width, (double)_image->get_width ()), _image->get_height ()));
197 Rect i = self.intersection (area);
207 Rect intersection (i);
209 context->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
211 /* Below, the rendering scaling is set to support retina display
213 Cairo::Matrix original_matrix = context->get_matrix();
214 context->scale (0.5, 0.5);
215 context->set_source (_image, self.x0 * 2, self.y0 * 2);
217 context->set_matrix (original_matrix);
219 context->set_source (_image, self.x0, self.y0);
225 Text::clamp_width (double w)
227 if (_clamped_width == w) {
232 _bounding_box_dirty = true;
237 Text::compute_bounding_box () const
239 if (!_canvas || _text.empty()) {
240 _bounding_box = Rect ();
241 _bounding_box_dirty = false;
245 if (_bounding_box_dirty) {
247 const float retina_factor = 0.5;
249 const float retina_factor = 1.0;
251 if (_need_redraw || !_image) {
254 _bounding_box = Rect (0, 0, min (_clamped_width, (double) _image->get_width() * retina_factor), _image->get_height() * retina_factor);
255 _bounding_box_dirty = false;
260 Text::set_alignment (Pango::Alignment alignment)
262 if (alignment == _alignment) {
268 _alignment = alignment;
270 _bounding_box_dirty = true;
275 Text::set_font_description (Pango::FontDescription font_description)
279 _font_description = new Pango::FontDescription (font_description);
281 _width_correction = -1.0;
283 _bounding_box_dirty = true;
288 Text::set_color (Gtkmm2ext::Color color)
290 if (color == _color) {
298 set_outline_color (Gtkmm2ext::contrasting_text_color (_color));
307 Text::dump (ostream& o) const
311 o << _canvas->indent() << '\t' << " text = " << _text << endl
312 << _canvas->indent() << " color = " << _color;
319 Text::text_width() const