ROOT  6.06/09
Reference Guide
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 #define FTGL_BITMAP 0
38 #define FTGL_PIXMAP 1
39 #define FTGL_OUTLINE 2
40 #define FTGL_POLYGON 3
41 #define FTGL_EXTRUDE 4
42 #define FTGL_TEXTURE 5
43 
44 /** \class TGLText
45 \ingroup opengl
46 GL Text.
47 
48 To draw a 3D text in a GL window. This class uses uses FTGL to render text.
49 FTGL is a package making the interface between the Free Type fonts and GL.
50 */
51 
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 
57 {
58  fX = 0;
59  fY = 0;
60  fZ = 0;
61  fAngle1 = 90;
62  fAngle2 = 0;
63  fAngle3 = 0;
64  fGLTextFont = 0;
65  SetGLTextFont(13); // Default font.
66 }
67 
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// TGLext normal constructor.
71 
72 TGLText::TGLText(Double_t x, Double_t y, Double_t z, const char * /*text*/)
73 {
74  fX = x;
75  fY = y;
76  fZ = z;
77  fAngle1 = 90;
78  fAngle2 = 0;
79  fAngle3 = 0;
80  fGLTextFont = 0;
81  SetGLTextFont(13); // Default font.
82 }
83 
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 
88 {
89  if (fGLTextFont) delete fGLTextFont;
90 }
91 
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 /// Draw text
95 
97 {
98  if (!fGLTextFont) return;
99 
100  glPushMatrix();
101  glTranslatef(x, y, z);
102 
104  Double_t s = GetTextSize();
105  glScalef(s,s,s);
106 
107  // Text alignment
108  Float_t llx, lly, llz, urx, ury, urz;
109  fGLTextFont->BBox(text, llx, lly, llz, urx, ury, urz);
110  Short_t halign = fTextAlign/10;
111  Short_t valign = fTextAlign - 10*halign;
112  Float_t dx = 0, dy = 0;
113  switch (halign) {
114  case 1 : dx = 0 ; break;
115  case 2 : dx = -urx/2; break;
116  case 3 : dx = -urx ; break;
117  }
118  switch (valign) {
119  case 1 : dy = 0 ; break;
120  case 2 : dy = -ury/2; break;
121  case 3 : dy = -ury ; break;
122  }
123  glTranslatef(dx, dy, 0);
124 
125  //In XY plane
126  glRotatef(fAngle1,1.,0.,0.);
127 
128  //In XZ plane
129  glRotatef(fAngle2,0.,1.,0.);
130 
131  //In YZ plane
132  glRotatef(fAngle3,0.,0.,1.);
133 
134  // Render text
135  fGLTextFont->Render(text);
136 
137  glPopMatrix();
138 }
139 
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 
143 void TGLText::PaintBBox(const char *text)
144 {
145  Float_t llx, lly, llz, urx, ury, urz;
146  fGLTextFont->BBox(text, llx, lly, llz, urx, ury, urz);
147  glBegin(GL_LINES);
148  glVertex3f( 0, 0, 0); glVertex3f( urx, 0, 0);
149  glVertex3f( 0, 0, 0); glVertex3f( 0, ury, 0);
150  glVertex3f( 0, ury, 0); glVertex3f( urx, ury, 0);
151  glVertex3f( urx, ury, 0); glVertex3f( urx, 0, 0);
152  glEnd();
153 }
154 
155 ////////////////////////////////////////////////////////////////////////////////
156 /// Calculate bounding-box for given string.
157 
158 void TGLText::BBox(const char* string, float& llx, float& lly, float& llz,
159  float& urx, float& ury, float& urz)
160 {
161  fGLTextFont->BBox(string, llx, lly, llz, urx, ury, urz);
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// Set the text rotation angles.
166 
168 {
169  fAngle1 = a1;
170  fAngle2 = a2;
171  fAngle3 = a3;
172 }
173 
174 
175 ////////////////////////////////////////////////////////////////////////////////
176 
178 {
179  int fontid = fontnumber / 10;
180 
181  const char *fontname=0;
182  if (fontid == 0) fontname = "arialbd.ttf";
183  if (fontid == 1) fontname = "timesi.ttf";
184  if (fontid == 2) fontname = "timesbd.ttf";
185  if (fontid == 3) fontname = "timesbi.ttf";
186  if (fontid == 4) fontname = "arial.ttf";
187  if (fontid == 5) fontname = "ariali.ttf";
188  if (fontid == 6) fontname = "arialbd.ttf";
189  if (fontid == 7) fontname = "arialbi.ttf";
190  if (fontid == 8) fontname = "cour.ttf";
191  if (fontid == 9) fontname = "couri.ttf";
192  if (fontid == 10) fontname = "courbd.ttf";
193  if (fontid == 11) fontname = "courbi.ttf";
194  if (fontid == 12) fontname = "symbol.ttf";
195  if (fontid == 13) fontname = "times.ttf";
196  if (fontid == 14) fontname = "wingding.ttf";
197 
198  // try to load font (font must be in Root.TTFontPath resource)
199  const char *ttpath = gEnv->GetValue("Root.TTFontPath",
200 # ifdef TTFFONTDIR
201  TTFFONTDIR
202 # else
203  "$(ROOTSYS)/fonts"
204 # endif
205  );
206 
207  char *ttfont = gSystem->Which(ttpath, fontname, kReadPermission);
208 
209  if (fGLTextFont) delete fGLTextFont;
210 
211 // fGLTextFont = new FTGLOutlineFont(ttfont);
212 
213  fGLTextFont = new FTGLPolygonFont(ttfont);
214 
215  if (!fGLTextFont->FaceSize(1))
216  Error("SetGLTextFont","Cannot set FTGL::FaceSize");
217  delete [] ttfont;
218 }
void PaintGLText(Double_t x, Double_t y, Double_t z, const char *text)
Draw text.
Definition: TGLText.cxx:96
Double_t fX
Definition: TGLText.h:26
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:21
void PaintBBox(const char *text)
Definition: TGLText.cxx:143
virtual Float_t GetTextSize() const
Definition: TAttText.h:50
Double_t fAngle1
Definition: TGLText.h:29
virtual char * Which(const char *search, const char *file, EAccessMode mode=kFileExists)
Find location of file in a search path.
Definition: TSystem.cxx:1511
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:167
Double_t x[n]
Definition: legend1.C:17
virtual Color_t GetTextColor() const
Definition: TAttText.h:48
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:158
FTFont * fGLTextFont
Definition: TGLText.h:32
Double_t fY
Definition: TGLText.h:27
virtual ~TGLText()
Definition: TGLText.cxx:87
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
virtual Int_t GetValue(const char *name, Int_t dflt)
Returns the integer value for a resource.
Definition: TEnv.cxx:494
short Short_t
Definition: RtypesCore.h:35
double Double_t
Definition: RtypesCore.h:55
TText * text
R__EXTERN TEnv * gEnv
Definition: TEnv.h:174
Double_t fAngle2
Definition: TGLText.h:30
Double_t y[n]
Definition: legend1.C:17
void SetGLTextFont(Font_t fontnumber)
Definition: TGLText.cxx:177
ClassImp(TGLText) TGLText
Definition: TGLText.cxx:52
Double_t fZ
Definition: TGLText.h:28
Short_t fTextAlign
Definition: TAttText.h:37
Double_t fAngle3
Definition: TGLText.h:31