NOOP, reindent code.
[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 (Cairo::RefPtr<Cairo::Context> context) const
85 {
86         if (_text.empty()) {
87                 return;
88         }
89
90         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
91
92         __redraw (layout);
93 }
94
95 void
96 Text::_redraw (Glib::RefPtr<Pango::Context> context) const
97 {
98         if (_text.empty()) {
99                 return;
100         }
101
102         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
103         __redraw (layout);
104 }
105
106 void
107 Text::__redraw (Glib::RefPtr<Pango::Layout> layout) const
108 {
109 #ifdef __APPLE__
110         if (_width_correction < 0.0) {
111                 // Pango returns incorrect text width on some OS X
112                 // So we have to make a correction
113                 // To determine the correct indent take the largest symbol for which the width is correct
114                 // and make the calculation
115                 Gtk::Window win;
116                 Gtk::Label foo;
117                 win.add (foo);
118
119                 int width = 0;
120                 int height = 0;
121                 Glib::RefPtr<Pango::Layout> test_layout = foo.create_pango_layout ("H");
122                 if (_font_description) {
123                         test_layout->set_font_description (*_font_description);
124                 }
125                 test_layout->get_pixel_size (width, height);
126
127                 _width_correction = width*1.5;
128         }
129 #else
130         /* don't bother with a conditional here */
131         _width_correction = 0.0;
132 #endif
133
134         layout->set_text (_text);
135
136         if (_font_description) {
137                 layout->set_font_description (*_font_description);
138         }
139
140         layout->set_alignment (_alignment);
141     
142         int w;
143         int h;
144
145         layout->get_pixel_size (w, h);
146
147         _width = w + _width_correction;
148         _height = h;
149
150         _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
151
152         Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
153
154         /* and draw, in the appropriate color of course */
155
156         if (_outline) {
157                 set_source_rgba (img_context, _outline_color);
158                 layout->update_from_cairo_context (img_context);
159                 pango_cairo_layout_path (img_context->cobj(), layout->gobj());
160                 img_context->stroke_preserve ();
161                 set_source_rgba (img_context, _color);
162                 img_context->fill ();
163         } else {
164                 set_source_rgba (img_context, _color);
165                 layout->show_in_cairo_context (img_context);
166         }
167
168         /* text has now been rendered in _image and is ready for blit in
169          * ::render 
170          */
171
172         _need_redraw = false;
173 }
174
175 void
176 Text::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
177 {
178         if (_text.empty()) {
179                 return;
180         }
181
182         Rect self = item_to_window (Rect (0, 0, min (_clamped_width, (double)_image->get_width ()), _image->get_height ()));
183         boost::optional<Rect> i = self.intersection (area);
184         
185         if (!i) {
186                 return;
187         }
188
189         if (_need_redraw) {
190                 _redraw (context);
191         }
192         
193         Rect intersection (i.get());
194
195         context->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
196         context->set_source (_image, self.x0, self.y0);
197         context->fill ();
198 }
199
200 void
201 Text::clamp_width (double w)
202 {
203         begin_change ();
204         _clamped_width = w;
205         _bounding_box_dirty = true;
206         end_change ();
207 }
208
209 void
210 Text::compute_bounding_box () const
211 {
212         if (!_canvas || _text.empty()) {
213                 _bounding_box = boost::optional<Rect> ();
214                 _bounding_box_dirty = false;
215                 return;
216         }
217
218         if (_bounding_box_dirty) {
219                 if (_need_redraw || !_image) {
220                         Glib::RefPtr<Pango::Context> context = Glib::wrap (gdk_pango_context_get()); // context now owns C object and will free it
221                         _redraw (context);
222                 }
223                 _bounding_box = Rect (0, 0, min (_clamped_width, (double) _image->get_width()), _image->get_height());
224                 _bounding_box_dirty = false;
225         }
226 }
227
228 void
229 Text::set_alignment (Pango::Alignment alignment)
230 {
231         begin_change ();
232         
233         _alignment = alignment;
234         _need_redraw = true;
235         _bounding_box_dirty = true;
236         end_change ();
237 }
238
239 void
240 Text::set_font_description (Pango::FontDescription font_description)
241 {
242         begin_change ();
243         
244         _font_description = new Pango::FontDescription (font_description);
245         _need_redraw = true;
246         _width_correction = -1.0;
247
248         _bounding_box_dirty = true;
249         end_change ();
250 }
251
252 void
253 Text::set_color (Color color)
254 {
255         begin_change ();
256
257         _color = color;
258         if (_outline) {
259                 set_outline_color (contrasting_text_color (_color));
260         }
261         _need_redraw = true;
262
263         end_change ();
264 }
265
266                 
267 void
268 Text::dump (ostream& o) const
269 {
270         Item::dump (o);
271
272         o << _canvas->indent() << '\t' << " text = " << _text << endl
273           << _canvas->indent() << " color = " << _color;
274
275         o << endl;
276 }
277
278
279 double
280 Text::text_width() const
281 {
282     if (_need_redraw) {
283         redraw ();
284     }
285     
286     return _width;
287 }