NOOP, remove trailing tabs/whitespace.
[ardour.git] / libs / canvas / text.cc
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
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.
9
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.
14
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.
18 */
19
20 #include <gdk/gdk.h>
21
22 #include <cairomm/cairomm.h>
23 #include <gtkmm/window.h>
24 #include <gtkmm/label.h>
25
26 #include "pbd/stacktrace.h"
27
28 #include "canvas/text.h"
29 #include "canvas/canvas.h"
30 #include "canvas/utils.h"
31 #include "canvas/colors.h"
32
33 using namespace std;
34 using namespace ArdourCanvas;
35
36
37 Text::Text (Canvas* c)
38         : Item (c)
39         , _color (0x000000ff)
40         , _font_description (0)
41         , _alignment (Pango::ALIGN_LEFT)
42         , _width (0)
43         , _height (0)
44         , _need_redraw (false)
45         , _width_correction (-1)
46         , _clamped_width (COORD_MAX)
47 {
48         _outline = false;
49 }
50
51 Text::Text (Item* parent)
52         : Item (parent)
53         , _color (0x000000ff)
54         , _font_description (0)
55         , _alignment (Pango::ALIGN_LEFT)
56         , _width (0)
57         , _height (0)
58         , _need_redraw (false)
59         , _width_correction (-1)
60         , _clamped_width (COORD_MAX)
61 {
62         _outline = false;
63 }
64
65 Text::~Text ()
66 {
67         delete _font_description;
68 }
69
70 void
71 Text::set (string const & text)
72 {
73         begin_change ();
74
75         _text = text;
76
77         _need_redraw = true;
78         _bounding_box_dirty = true;
79
80         end_change ();
81 }
82
83 void
84 Text::_redraw () const
85 {
86         assert (!_text.empty());
87         Glib::RefPtr<Pango::Context> context = Glib::wrap (gdk_pango_context_get());
88         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
89
90 #ifdef __APPLE__
91         if (_width_correction < 0.0) {
92                 // Pango returns incorrect text width on some OS X
93                 // So we have to make a correction
94                 // To determine the correct indent take the largest symbol for which the width is correct
95                 // and make the calculation
96                 Gtk::Window win;
97                 Gtk::Label foo;
98                 win.add (foo);
99                 win.ensure_style ();
100
101                 int width = 0;
102                 int height = 0;
103                 Glib::RefPtr<Pango::Layout> test_layout = foo.create_pango_layout ("H");
104                 if (_font_description) {
105                         test_layout->set_font_description (*_font_description);
106                 }
107                 test_layout->get_pixel_size (width, height);
108
109                 _width_correction = width*1.5;
110         }
111 #else
112         /* don't bother with a conditional here */
113         _width_correction = 0.0;
114 #endif
115
116         layout->set_text (_text);
117
118         if (_font_description) {
119                 layout->set_font_description (*_font_description);
120         }
121
122         layout->set_alignment (_alignment);
123
124         int w;
125         int h;
126
127         layout->get_pixel_size (w, h);
128
129         _width = w + _width_correction;
130         _height = h;
131
132 #ifdef __APPLE__
133         _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width * 2, _height * 2);
134 #else
135         _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
136 #endif
137
138         Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
139
140 #ifdef __APPLE__
141         /* Below, the rendering scaling is set to support retina display
142          */
143         img_context->scale (2, 2);
144 #endif
145
146         /* and draw, in the appropriate color of course */
147
148         if (_outline) {
149                 set_source_rgba (img_context, _outline_color);
150                 layout->update_from_cairo_context (img_context);
151                 pango_cairo_layout_path (img_context->cobj(), layout->gobj());
152                 img_context->stroke_preserve ();
153                 set_source_rgba (img_context, _color);
154                 img_context->fill ();
155         } else {
156                 set_source_rgba (img_context, _color);
157                 layout->show_in_cairo_context (img_context);
158         }
159
160         /* text has now been rendered in _image and is ready for blit in
161          * ::render
162          */
163
164         _need_redraw = false;
165 }
166
167 void
168 Text::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
169 {
170         if (_text.empty()) {
171                 return;
172         }
173
174         Rect self = item_to_window (Rect (0, 0, min (_clamped_width, (double)_image->get_width ()), _image->get_height ()));
175         boost::optional<Rect> i = self.intersection (area);
176
177         if (!i) {
178                 return;
179         }
180
181         if (_need_redraw) {
182                 _redraw ();
183         }
184
185         Rect intersection (i.get());
186
187         context->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
188 #ifdef __APPLE__
189         /* Below, the rendering scaling is set to support retina display
190          */
191         Cairo::Matrix original_matrix = context->get_matrix();
192         context->scale (0.5, 0.5);
193         context->set_source (_image, self.x0 * 2, self.y0 * 2);
194         context->fill ();
195         context->set_matrix (original_matrix);
196 #else
197         context->set_source (_image, self.x0, self.y0);
198         context->fill ();
199 #endif
200 }
201
202 void
203 Text::clamp_width (double w)
204 {
205         if (_clamped_width == w) {
206                 return;
207         }
208         begin_change ();
209         _clamped_width = w;
210         _bounding_box_dirty = true;
211         end_change ();
212 }
213
214 void
215 Text::compute_bounding_box () const
216 {
217         if (!_canvas || _text.empty()) {
218                 _bounding_box = boost::optional<Rect> ();
219                 _bounding_box_dirty = false;
220                 return;
221         }
222
223         if (_bounding_box_dirty) {
224 #ifdef __APPLE__
225                 const float retina_factor = 0.5;
226 #else
227                 const float retina_factor = 1.0;
228 #endif
229                 if (_need_redraw || !_image) {
230                         _redraw ();
231                 }
232                 _bounding_box = Rect (0, 0, min (_clamped_width, (double) _image->get_width() * retina_factor), _image->get_height() * retina_factor);
233                 _bounding_box_dirty = false;
234         }
235 }
236
237 void
238 Text::set_alignment (Pango::Alignment alignment)
239 {
240         begin_change ();
241
242         _alignment = alignment;
243         _need_redraw = true;
244         _bounding_box_dirty = true;
245         end_change ();
246 }
247
248 void
249 Text::set_font_description (Pango::FontDescription font_description)
250 {
251         begin_change ();
252
253         _font_description = new Pango::FontDescription (font_description);
254         _need_redraw = true;
255         _width_correction = -1.0;
256
257         _bounding_box_dirty = true;
258         end_change ();
259 }
260
261 void
262 Text::set_color (Color color)
263 {
264         begin_change ();
265
266         _color = color;
267         if (_outline) {
268                 set_outline_color (contrasting_text_color (_color));
269         }
270         _need_redraw = true;
271
272         end_change ();
273 }
274
275
276 void
277 Text::dump (ostream& o) const
278 {
279         Item::dump (o);
280
281         o << _canvas->indent() << '\t' << " text = " << _text << endl
282           << _canvas->indent() << " color = " << _color;
283
284         o << endl;
285 }
286
287
288 double
289 Text::text_width() const
290 {
291     if (_need_redraw) {
292         redraw ();
293     }
294
295     return _width;
296 }