Logo ROOT   6.10/00
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TGLText.cxx
Go to the documentation of this file.
1 // @(#)root/gl:$Id$
2 // Author: Olivier Couet 12/04/2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "Riostream.h"
13 #include "TROOT.h"
14 #include "TError.h"
15 
16 #include "TGLText.h"
17 #include "TGLUtil.h"
18 #include "TColor.h"
19 #include "TSystem.h"
20 #include "TEnv.h"
21 #include "TGLIncludes.h"
22 
23 // Direct inclusion of FTGL headers is deprecated in ftgl-2.1.3 while
24 // ftgl-2.1.2 shipped with ROOT requires manual inclusion.
25 #ifndef BUILTIN_FTGL
26 # include <FTGL/ftgl.h>
27 #else
28 # include "FTFont.h"
29 # include "FTGLExtrdFont.h"
30 # include "FTGLOutlineFont.h"
31 # include "FTGLPolygonFont.h"
32 # include "FTGLTextureFont.h"
33 # include "FTGLPixmapFont.h"
34 # include "FTGLBitmapFont.h"
35 #endif
36 
37 #include <fontconfig/fontconfig.h>
38 
39 #define FTGL_BITMAP 0
40 #define FTGL_PIXMAP 1
41 #define FTGL_OUTLINE 2
42 #define FTGL_POLYGON 3
43 #define FTGL_EXTRUDE 4
44 #define FTGL_TEXTURE 5
45 
46 /** \class TGLText
47 \ingroup opengl
48 GL Text.
49 
50 To draw a 3D text in a GL window. This class uses uses FTGL to render text.
51 FTGL is a package making the interface between the Free Type fonts and GL.
52 */
53 
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 
59 {
60  fX = 0;
61  fY = 0;
62  fZ = 0;
63  fAngle1 = 90;
64  fAngle2 = 0;
65  fAngle3 = 0;
66  fGLTextFont = 0;
67  SetGLTextFont(13); // Default font.
68 }
69 
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// TGLext normal constructor.
73 
74 TGLText::TGLText(Double_t x, Double_t y, Double_t z, const char * /*text*/)
75 {
76  fX = x;
77  fY = y;
78  fZ = z;
79  fAngle1 = 90;
80  fAngle2 = 0;
81  fAngle3 = 0;
82  fGLTextFont = 0;
83  SetGLTextFont(13); // Default font.
84 }
85 
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 
90 {
91  if (fGLTextFont) delete fGLTextFont;
92 }
93 
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 /// Draw text
97 
99 {
100  if (!fGLTextFont) return;
101 
102  glPushMatrix();
103  glTranslatef(x, y, z);
104 
106  Double_t s = GetTextSize();
107  glScalef(s,s,s);
108 
109  // Text alignment
110  Float_t llx, lly, llz, urx, ury, urz;
111  fGLTextFont->BBox(text, llx, lly, llz, urx, ury, urz);
112  Short_t halign = fTextAlign/10;
113  Short_t valign = fTextAlign - 10*halign;
114  Float_t dx = 0, dy = 0;
115  switch (halign) {
116  case 1 : dx = 0 ; break;
117  case 2 : dx = -urx/2; break;
118  case 3 : dx = -urx ; break;
119  }
120  switch (valign) {
121  case 1 : dy = 0 ; break;
122  case 2 : dy = -ury/2; break;
123  case 3 : dy = -ury ; break;
124  }
125  glTranslatef(dx, dy, 0);
126 
127  //In XY plane
128  glRotatef(fAngle1,1.,0.,0.);
129 
130  //In XZ plane
131  glRotatef(fAngle2,0.,1.,0.);
132 
133  //In YZ plane
134  glRotatef(fAngle3,0.,0.,1.);
135 
136  // Render text
137  fGLTextFont->Render(text);
138 
139  glPopMatrix();
140 }
141 
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 
145 void TGLText::PaintBBox(const char *text)
146 {
147  Float_t llx, lly, llz, urx, ury, urz;
148  fGLTextFont->BBox(text, llx, lly, llz, urx, ury, urz);
149  glBegin(GL_LINES);
150  glVertex3f( 0, 0, 0); glVertex3f( urx, 0, 0);
151  glVertex3f( 0, 0, 0); glVertex3f( 0, ury, 0);
152  glVertex3f( 0, ury, 0); glVertex3f( urx, ury, 0);
153  glVertex3f( urx, ury, 0); glVertex3f( urx, 0, 0);
154  glEnd();
155 }
156 
157 ////////////////////////////////////////////////////////////////////////////////
158 /// Calculate bounding-box for given string.
159 
160 void TGLText::BBox(const char* string, float& llx, float& lly, float& llz,
161  float& urx, float& ury, float& urz)
162 {
163  fGLTextFont->BBox(string, llx, lly, llz, urx, ury, urz);
164 }
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// Set the text rotation angles.
168 
170 {
171  fAngle1 = a1;
172  fAngle2 = a2;
173  fAngle3 = a3;
174 }
175 
176 
177 ////////////////////////////////////////////////////////////////////////////////
178 
180 {
181  int fontid = fontnumber / 10;
182 
183  FcPattern *pat, *match;
184  FcResult result;
185  char *ttfont;
186  int ttindex;
187 
188  pat = FcPatternCreate ();
189 
190  switch (fontid) {
191  case 1:
192  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freeserif");
193  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
194  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ITALIC);
195  break;
196  case 2:
197  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freeserif");
198  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
199  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
200  break;
201  case 3:
202  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freeserif");
203  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
204  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ITALIC);
205  break;
206  case 4:
207  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freesans");
208  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
209  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
210  break;
211  case 5:
212  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freesans");
213  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
214  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ITALIC);
215  break;
216  case 0:
217  case 6:
218  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freesans");
219  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
220  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
221  break;
222  case 7:
223  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freesans");
224  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
225  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ITALIC);
226  break;
227  case 8:
228  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freemono");
229  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
230  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
231  break;
232  case 9:
233  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freemono");
234  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
235  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ITALIC);
236  break;
237  case 10:
238  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freemono");
239  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
240  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
241  break;
242  case 11:
243  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freemono");
244  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
245  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ITALIC);
246  break;
247  case 12:
248  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"symbol");
249  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
250  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
251  break;
252  case 13:
253  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freeserif");
254  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
255  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
256  break;
257  case 14:
258  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"dingbats");
259  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
260  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
261  break;
262  }
263 
264  FcConfigSubstitute (NULL, pat, FcMatchPattern);
265  FcDefaultSubstitute (pat);
266  match = FcFontMatch (NULL, pat, &result);
267  FcPatternGetString (match, FC_FILE, 0, (FcChar8**)&ttfont);
268  FcPatternGetInteger (match, FC_INDEX, 0, &ttindex);
269 
270  if (fGLTextFont) delete fGLTextFont;
271 
272 // fGLTextFont = new FTGLOutlineFont(ttfont);
273 
274  fGLTextFont = new FTGLPolygonFont(ttfont);
275 
276  FcPatternDestroy (match);
277  FcPatternDestroy (pat);
278 
279  if (!fGLTextFont->FaceSize(1))
280  Error("SetGLTextFont","Cannot set FTGL::FaceSize");
281 }
void PaintGLText(Double_t x, Double_t y, Double_t z, const char *text)
Draw text.
Definition: TGLText.cxx:98
Double_t fX
Definition: TGLText.h:24
static void Color(const TGLColor &color)
Set color from TGLColor.
Definition: TGLUtil.cxx:1658
float Float_t
Definition: RtypesCore.h:53
GL Text.
Definition: TGLText.h:19
void PaintBBox(const char *text)
Definition: TGLText.cxx:145
virtual Float_t GetTextSize() const
Return the text size.
Definition: TAttText.h:36
Double_t fAngle1
Definition: TGLText.h:27
#define NULL
Definition: RtypesCore.h:88
short Font_t
Definition: RtypesCore.h:75
void SetGLTextAngles(Double_t a1, Double_t a2, Double_t a3)
Set the text rotation angles.
Definition: TGLText.cxx:169
Double_t x[n]
Definition: legend1.C:17
virtual Color_t GetTextColor() const
Return the text color.
Definition: TAttText.h:34
void Error(const char *location, const char *msgfmt,...)
void BBox(const char *string, float &llx, float &lly, float &llz, float &urx, float &ury, float &urz)
Calculate bounding-box for given string.
Definition: TGLText.cxx:160
FTFont * fGLTextFont
Definition: TGLText.h:30
Double_t fY
Definition: TGLText.h:25
virtual ~TGLText()
Definition: TGLText.cxx:89
short Short_t
Definition: RtypesCore.h:35
#define ClassImp(name)
Definition: Rtypes.h:336
double Double_t
Definition: RtypesCore.h:55
TText * text
Double_t fAngle2
Definition: TGLText.h:28
Double_t y[n]
Definition: legend1.C:17
TGLText()
Definition: TGLText.cxx:58
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
void SetGLTextFont(Font_t fontnumber)
Definition: TGLText.cxx:179
double result[121]
Double_t fZ
Definition: TGLText.h:26
Short_t fTextAlign
Text alignment.
Definition: TAttText.h:23
Double_t fAngle3
Definition: TGLText.h:29