pango.h
1 /*
2  @copyright Russell Standish 2000-2013
3  @author Russell Standish
4  This file is part of EcoLab
5 
6  Open source licensed under the MIT license. See LICENSE for details.
7 */
8 
9 /*
10  Utility class for wrapping Pango over cairo functionality
11 
12  Pango text is in the Pango basic markup
13 */
14 #ifndef ECOLAB_PANGO_H
15 #define ECOLAB_PANGO_H
16 
17 #ifdef PANGO
18 #include <pango/pangocairo.h>
19 #endif
20 
21 namespace ecolab
22 {
23 #ifdef PANGO
24  class Pango
25  {
26  cairo_t* cairo;
27  PangoLayout* layout;
28  PangoFontDescription* fd;
29  PangoRectangle bbox;
30  void operator=(const Pango&);
31  Pango(const Pango&);
32  public:
33  double angle; // angle the text
34  static const char *defaultFamily;
35  Pango(cairo_t* cairo):
36  cairo(cairo), layout(pango_cairo_create_layout(cairo)), angle(0)
37  {
38  fd=pango_font_description_copy(pango_layout_get_font_description(layout));
39  if (!fd) // if NULL, we must get it from the context
40  {
41  fd=pango_font_description_copy
42  (pango_context_get_font_description(pango_layout_get_context(layout)));
43  }
44  if (defaultFamily)
45  pango_font_description_set_family(fd,defaultFamily);
46  pango_layout_set_font_description(layout, fd); //asume ownership not passe
47  }
48  ~Pango() {pango_font_description_free(fd); g_object_unref(layout); }
49  void setMarkup(const std::string& markup) {
50  pango_layout_set_markup(layout, markup.c_str(), -1);
51  pango_layout_get_extents(layout,0,&bbox);
52  }
53  void setFontSize(double sz) {
54  if (gint(sz*PANGO_SCALE)<=0) return;
55  pango_font_description_set_size(fd, gint(sz*PANGO_SCALE));
56  pango_layout_set_font_description(layout, fd); //asume ownership not passed?
57  }
58  void setFontFamily(const char* family) {
59  pango_font_description_set_family(fd,family);
60  pango_layout_set_font_description(layout, fd); //asume ownership not passe
61  }
62  double getFontSize() const {
63  return pango_font_description_get_size(fd)/double(PANGO_SCALE);
64  }
65  void show() {
66 // cairo_save(cairo);
67 // cairo_identity_matrix(cairo);
68 // cairo_rotate(cairo, angle);
69 // cairo_rectangle(cairo,left(),top(),width(),height());
70 // cairo_stroke(cairo);
71 // cairo_restore(cairo);
72 
73  cairo_save(cairo);
74  cairo_identity_matrix(cairo);
75  cairo_rotate(cairo, angle);
76  cairo_rel_move_to(cairo,left(),top());
77  pango_cairo_update_layout(cairo, layout);
78  pango_cairo_show_layout(cairo, layout);
79  cairo_restore(cairo);
80  }
82  double width() const {return double(bbox.width)/PANGO_SCALE;}
84  double height() const {return double(bbox.height)/PANGO_SCALE;}
86  double left() const {return double(bbox.x)/PANGO_SCALE;}
88  double top() const {return double(bbox.y)/PANGO_SCALE;}
89  };
90 #else // fall back to basic Cairo text handling
91  class Pango
92  {
93  cairo_t* cairo;
94  std::string markup;
95  cairo_text_extents_t bbox;
96  double fontSize; // default according to cairo documentation
97  // make this non-copiable so that it behaves the same as the real Pango one
98  void operator=(const Pango&);
99  Pango(const Pango&);
100  public:
101  double angle; // angle the text
102  Pango(cairo_t* cairo):
103  cairo(cairo), fontSize(10), angle(0) {}
104  void setMarkup(const std::string& markup) {
105  this->markup=markup;
106  cairo_text_extents(cairo,markup.c_str(),&bbox);
107 
108  }
109  void setFontSize(unsigned sz) {
110  fontSize=sz;
111  cairo_set_font_size(cairo, sz);
112  }
113  double getFontSize() const {
114  return fontSize;
115  }
116  void show() {
117  cairo_save(cairo);
118  cairo_identity_matrix(cairo);
119  cairo_rotate(cairo, angle);
120  cairo_rel_move_to(cairo,left(),-top());
121  cairo_show_text(cairo, markup.c_str());
122  cairo_restore(cairo);
123  }
125  double width() const {return bbox.width;}
127  double height() const {return bbox.height;}
129  double left() const {return bbox.x_bearing;}
131  double top() const {return bbox.y_bearing;}
132  };
133 #endif
134 }
135 
136 #endif
double height() const
height of rendered text
Definition: pango.h:127
double left() const
x-coordinate of left hand side of the rendered text
Definition: pango.h:129
Definition: pango.h:91
double top() const
y-coordinate of the top of the rendered text
Definition: pango.h:131
_OPENMP
Definition: accessor.h:16
double width() const
width of rendered text
Definition: pango.h:125