Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGLFontManager.cxx
Go to the documentation of this file.
1// @(#)root/gl:$Id$
2// Author: Alja Mrak-Tadel 2008
3
4/*************************************************************************
5 * Copyright (C) 1995-2007, 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 "RConfigure.h"
13#include "TGLFontManager.h"
14
15
16#include "TROOT.h"
17#include "TVirtualX.h"
18#include "TMath.h"
19#include "TSystem.h"
20#include "TEnv.h"
21#include "TObjString.h"
22#include "TGLUtil.h"
23#include "TGLIncludes.h"
24
25// Direct inclusion of FTGL headers is deprecated in ftgl-2.1.3 while
26// ftgl-2.1.2 shipped with ROOT requires manual inclusion.
27#ifndef BUILTIN_FTGL
28# include <FTGL/ftgl.h>
29#else
30# include "FTFont.h"
31# include "FTGLExtrdFont.h"
32# include "FTGLOutlineFont.h"
33# include "FTGLPolygonFont.h"
34# include "FTGLTextureFont.h"
35# include "FTGLPixmapFont.h"
36# include "FTGLBitmapFont.h"
37#endif
38
39
40/** \class TGLFont
41\ingroup opengl
42A wrapper class for FTFont.
43Holds pointer to FTFont object and its description: face size, font file
44and class ID. It wraps Render and BBox functions.
45*/
46
47
48////////////////////////////////////////////////////////////////////////////////
49/// Constructor.
50
52 fFont(0), fManager(0), fDepth(0),
53 fSize(0), fFile(0), fMode(kUndef),
54 fTrashCount(0)
55{
56}
57
58////////////////////////////////////////////////////////////////////////////////
59/// Constructor.
60
62 fFont(f), fManager(mng), fDepth(0),
63 fSize(size), fFile(font), fMode(mode),
64 fTrashCount(0)
65{
66}
67
68////////////////////////////////////////////////////////////////////////////////
69/// Assignment operator.
70
72 fFont(0), fManager(0), fDepth(0), fTrashCount(0)
73{
74 fFont = (FTFont*)o.GetFont();
75
76 fSize = o.fSize;
77 fFile = o.fFile;
78 fMode = o.fMode;
79
81}
82
83////////////////////////////////////////////////////////////////////////////////
84///Destructor
85
90
91////////////////////////////////////////////////////////////////////////////////
92/// Assignment operator.
93
95{
96 SetFont(o.fFont);
98
100
101 fSize = o.fSize;
102 fFile = o.fFile;
103 fMode = o.fMode;
104
106}
107
108
109/******************************************************************************/
110
111////////////////////////////////////////////////////////////////////////////////
112/// Get font's ascent.
113
115{
116 return fFont->Ascender();
117}
118
119////////////////////////////////////////////////////////////////////////////////
120/// Get font's descent. The returned value is positive.
121
123{
124 return -fFont->Descender();
125}
126
127////////////////////////////////////////////////////////////////////////////////
128/// Get font's line-height.
129
131{
132 return fFont->LineHeight();
133}
134
135////////////////////////////////////////////////////////////////////////////////
136/// Measure font's base-line parameters from the passed text.
137/// Note that the measured parameters are not the same as the ones
138/// returned by get-functions - those were set by the font designer.
139
141 const char* txt) const
142{
143 Float_t dum, lly, ury;
144 const_cast<FTFont*>(fFont)->BBox(txt, dum, lly, dum, dum, ury, dum);
145 ascent = ury;
146 descent = -lly;
147 line_height = ury - lly;
148}
149
150////////////////////////////////////////////////////////////////////////////////
151/// Get bounding box.
152
153void TGLFont::BBox(const char* txt,
155 Float_t& urx, Float_t& ury, Float_t& urz) const
156{
157 // FTGL is not const correct.
158 const_cast<FTFont*>(fFont)->BBox(txt, llx, lly, llz, urx, ury, urz);
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Get bounding box.
163
164void TGLFont::BBox(const wchar_t* txt,
166 Float_t& urx, Float_t& ury, Float_t& urz) const
167{
168 // FTGL is not const correct.
169 const_cast<FTFont*>(fFont)->BBox(txt, llx, lly, llz, urx, ury, urz);
170}
171
172////////////////////////////////////////////////////////////////////////////////
173///mgn is simply ignored, because ROOT's TVirtualX TGX11 are complete mess with
174///painting attributes.
175
176template<class Char>
178{
179 glPushMatrix();
180 //glLoadIdentity();
181
182 // FTGL is not const correct.
183 Float_t llx = 0.f, lly = 0.f, llz = 0.f, urx = 0.f, ury = 0.f, urz = 0.f;
184 BBox(txt, llx, lly, llz, urx, ury, urz);
185
186 /*
187 V\H | left | center | right
188 _______________________________
189 bottom | 7 | 8 | 9
190 _______________________________
191 center | 4 | 5 | 6
192 _______________________________
193 top | 1 | 2 | 3
194 */
195 const Double_t dx = urx - llx, dy = ury - lly;
196 Double_t xc = 0., yc = 0.;
197
198 // if align was not set before - use value from gVirtualX
199 const UInt_t align = fTextAlign ? fTextAlign : gVirtualX->GetTextAlign();
200
201 //Timur: Here's the nice X11 bullshido: you call gVirtualX->SetTextAlign(11),
202 //later gVirtualX->GetTextAling() will give you 7. Brilliant!
203 //But with Cocoa you'll have 11. As it should be, of course.
204 //Sergey: problem with gVirtualX solved, now all platforms works same way
205
206 const UInt_t hAlign = UInt_t(align / 10);
207 const UInt_t vAlign = UInt_t(align % 10);
208 switch (hAlign) {
209 case 1:
210 xc = 0.5 * dx;
211 break;
212 case 2:
213 break;
214 case 3:
215 xc = -0.5 * dy;
216 break;
217 }
218
219 switch (vAlign) {
220 case 1:
221 yc = 0.5 * dy;
222 break;
223 case 2:
224 break;
225 case 3:
226 yc = -0.5 * dy;
227 break;
228 }
229
230 glTranslated(x, y, 0.);
231 glRotated(angle, 0., 0., 1.);
232 glTranslated(xc, yc, 0.);
233 glTranslated(-0.5 * dx, -0.5 * dy, 0.);
234 //glScaled(mgn, mgn, 1.);
235
236 const_cast<FTFont*>(fFont)->Render(txt);
237
238 glPopMatrix();
239}
240
241////////////////////////////////////////////////////////////////////////////////
242
244{
246}
247
248////////////////////////////////////////////////////////////////////////////////
249
251{
253}
254
255////////////////////////////////////////////////////////////////////////////////
256/// Render text.
257
258void TGLFont::Render(const TString &txt) const
259{
260 Bool_t scaleDepth = (fMode == kExtrude && fDepth != 1.0f);
261
262 if (scaleDepth) {
263 glPushMatrix();
264 // !!! 0.2*fSize is hard-coded in TGLFontManager::GetFont(), too.
265 glTranslatef(0.0f, 0.0f, 0.5f*fDepth * 0.2f*fSize);
266 glScalef(1.0f, 1.0f, fDepth);
267 }
268
269 // FTGL is not const correct.
270 const_cast<FTFont*>(fFont)->Render(txt);
271
272 if (scaleDepth) {
273 glPopMatrix();
274 }
275}
276
277////////////////////////////////////////////////////////////////////////////////
278/// Render text with given alignmentrepl and at given position.
279
282{
283 glPushMatrix();
284
285 glTranslatef(x, y, z);
286
287 x=0, y=0;
288 Float_t llx, lly, llz, urx, ury, urz;
289 BBox(txt, llx, lly, llz, urx, ury, urz);
290
291 switch (alignH)
292 {
293 case TGLFont::kRight:
294 x = -urx;
295 break;
296
298 x = -urx*0.5;
299 break;
300 default:
301 break;
302 };
303
304 switch (alignV)
305 {
306 case TGLFont::kBottom:
307 y = -ury;
308 break;
310 y = -ury*0.5;
311 break;
312 default:
313 break;
314 };
315
317 {
318 glRasterPos2i(0, 0);
319 glBitmap(0, 0, 0, 0, x, y, 0);
320 }
321 else
322 {
323 glTranslatef(x, y, 0);
324 }
325 Render(txt);
326 glPopMatrix();
327}
328
329////////////////////////////////////////////////////////////////////////////////
330/// Set-up GL state before FTFont rendering.
331
333{
334 switch (fMode)
335 {
336 case kBitmap:
337 case kPixmap:
341 glAlphaFunc(GL_GEQUAL, 0.0625);
342 break;
343 case kTexture:
349 glAlphaFunc(GL_GEQUAL, 0.0625);
350 break;
351 case kOutline:
352 case kPolygon:
353 case kExtrude:
357 break;
358 default:
359 Warning("TGLFont::PreRender", "Font mode undefined.");
361 break;
362 }
363
366 else
368}
369
370////////////////////////////////////////////////////////////////////////////////
371/// Reset GL state after FTFont rendering.
372
374{
375 glPopAttrib();
376}
377
378/** \class TGLFontManager
379\ingroup opengl
380A FreeType GL font manager.
381
382Each GL rendering context has an instance of FTGLManager.
383This enables FTGL fonts to be shared same way as textures and display lists.
384*/
385
386
391
392////////////////////////////////////////////////////////////////////////////////
393/// Destructor.
394
396{
397 FontMap_i it = fFontMap.begin();
398 while (it != fFontMap.end()) {
399 delete it->first.GetFont();
400 it++;
401 }
402 fFontMap.clear();
403}
404
405////////////////////////////////////////////////////////////////////////////////
406/// Provide font with given size, file and FTGL class.
407
409{
411
413 if (mode == out.GetMode() && fileID == out.GetFile() && size == out.GetSize())
414 return;
415
417 if (it == fFontMap.end())
418 {
419 TString ttpath, file;
420 ttpath = gEnv->GetValue("Root.TTGLFontPath", TROOT::GetTTFFontDir());
421 {
422 //For extenede we have both ttf and otf.
424 ((TObjString*)fgFontFileArray[fileID])->String() + ".ttf" :
425 ((TObjString*)fgFontFileArray[fileID])->String());
426 file = fp;
427 delete [] fp;
428 }
429
430 FTFont* ftfont = 0;
431 switch (mode)
432 {
433 case TGLFont::kBitmap:
434 ftfont = new FTGLBitmapFont(file);
435 break;
436 case TGLFont::kPixmap:
437 ftfont = new FTGLPixmapFont(file);
438 break;
440 ftfont = new FTGLOutlineFont(file);
441 break;
443 ftfont = new FTGLPolygonFont(file);
444 break;
446 ftfont = new FTGLExtrdFont(file);
447 ftfont->Depth(0.2*size);
448 break;
450 ftfont = new FTGLTextureFont(file);
451 break;
452 default:
453 Error("TGLFontManager::RegisterFont", "invalid FTGL type");
454 return;
455 break;
456 }
457 ftfont->FaceSize(size);
458 const TGLFont &mf = fFontMap.insert(std::make_pair(TGLFont(size, fileID, mode, ftfont, 0), 1)).first->first;
459 out.CopyAttributes(mf);
460 }
461 else
462 {
463 if (it->first.GetTrashCount() > 0) {
464 fFontTrash.remove(&(it->first));
465 it->first.SetTrashCount(0);
466 }
467 ++(it->second);
468 out.CopyAttributes(it->first);
469 }
470 out.SetManager(this);
471}
472
473////////////////////////////////////////////////////////////////////////////////
474/// Get mapping from ttf id to font names. Table taken from TTF.cxx.
475
477{
479 TIter next(farr);
480 TObjString* os;
481 Int_t cnt = 0;
482 while ((os = (TObjString*) next()) != 0)
483 {
484 if (os->String() == name)
485 break;
486 cnt++;
487 }
488
489 if (cnt < farr->GetEntries())
490 RegisterFont(size, cnt, mode, out);
491 else
492 Error("TGLFontManager::RegisterFont", "unknown font name %s", name);
493}
494
495////////////////////////////////////////////////////////////////////////////////
496/// Release font with given attributes. Returns false if font has
497/// not been found in the managers font set.
498
500{
501 FontMap_i it = fFontMap.find(font);
502
503 if (it != fFontMap.end())
504 {
505 --(it->second);
506 if (it->second == 0)
507 {
508 assert(it->first.GetTrashCount() == 0);
509 it->first.IncTrashCount();
510 fFontTrash.push_back(&it->first);
511 }
512 }
513}
514
515////////////////////////////////////////////////////////////////////////////////
516/// Get id to file name map.
517
523
524////////////////////////////////////////////////////////////////////////////////
525/// Get valid font size vector.
526
532
533////////////////////////////////////////////////////////////////////////////////
534
536{
538
539 assert(fgExtendedFontStart > 0 && "GetExtendedFontStartIndex, invalid index");
540
541 return fgExtendedFontStart;
542}
543
544////////////////////////////////////////////////////////////////////////////////
545/// Get availabe font size.
546
548{
550
553
554 if (idx < 0) idx = 0;
555 return fgFontSizeArray[idx];
556}
557
558////////////////////////////////////////////////////////////////////////////////
559/// Get availabe font size.
560
562{
563 if (ds < min) ds = min;
564 if (ds > max) ds = max;
565 return GetFontSize(ds);
566}
567
568////////////////////////////////////////////////////////////////////////////////
569/// Get font name from TAttAxis font id.
570
572{
574
575 Int_t fontIndex = id / 10;
576
577 if (fontIndex > fgFontFileArray.GetEntries() || !fontIndex)
578 fontIndex = 5;//arialbd
579 else
580 fontIndex -= 1;
581
583 return os->String().Data();
584}
585
586////////////////////////////////////////////////////////////////////////////////
587/// Create a list of available font files and allowed font sizes.
588
590{
591 fgFontFileArray.Add(new TObjString("timesi")); // 10
592 fgFontFileArray.Add(new TObjString("timesbd")); // 20
593 fgFontFileArray.Add(new TObjString("timesbi")); // 30
594
595 fgFontFileArray.Add(new TObjString("arial")); // 40
596 fgFontFileArray.Add(new TObjString("ariali")); // 50
597 fgFontFileArray.Add(new TObjString("arialbd")); // 60
598 fgFontFileArray.Add(new TObjString("arialbi")); // 70
599
600 fgFontFileArray.Add(new TObjString("cour")); // 80
601 fgFontFileArray.Add(new TObjString("couri")); // 90
602 fgFontFileArray.Add(new TObjString("courbd")); // 100
603 fgFontFileArray.Add(new TObjString("courbi")); // 110
604
605 fgFontFileArray.Add(new TObjString("symbol")); // 120
606 fgFontFileArray.Add(new TObjString("times")); // 130
607 fgFontFileArray.Add(new TObjString("wingding")); // 140
608 fgFontFileArray.Add(new TObjString("symbol")); // 150
609
611 //"Extended" fonts for gl-pad.
612 //fgPadFontStart + ...
613 fgFontFileArray.Add(new TObjString("FreeSerifItalic.otf")); // 10 (160)
614 fgFontFileArray.Add(new TObjString("FreeSerifBold.otf")); // 20 (170)
615 fgFontFileArray.Add(new TObjString("FreeSerifBoldItalic.otf")); // 30
616
617 fgFontFileArray.Add(new TObjString("FreeSans.otf")); // 40
618 fgFontFileArray.Add(new TObjString("FreeSansOblique.otf")); // 50
619 fgFontFileArray.Add(new TObjString("FreeSansBold.otf")); // 60
620 fgFontFileArray.Add(new TObjString("FreeSansBoldOblique.otf")); // 70
621
622 fgFontFileArray.Add(new TObjString("FreeMono.otf")); // 80
623 fgFontFileArray.Add(new TObjString("FreeMonoOblique.otf")); // 90
624 fgFontFileArray.Add(new TObjString("FreeMonoBold.otf")); // 100
625 fgFontFileArray.Add(new TObjString("FreeMonoBoldOblique.otf")); // 110
626
627 fgFontFileArray.Add(new TObjString("symbol.ttf")); // 120
628 fgFontFileArray.Add(new TObjString("FreeSerif.otf")); // 130
629 fgFontFileArray.Add(new TObjString("wingding.ttf")); // 140
630 fgFontFileArray.Add(new TObjString("symbol.ttf")); // 150
631
632 fgFontFileArray.Add(new TObjString("STIXGeneral.otf")); // 200
633 fgFontFileArray.Add(new TObjString("STIXGeneralItalic.otf")); // 210
634 fgFontFileArray.Add(new TObjString("STIXGeneralBol.otf")); // 220
635 fgFontFileArray.Add(new TObjString("STIXGeneralBolIta.otf")); // 230
636
637 fgFontFileArray.Add(new TObjString("STIXSiz1Sym.otf")); // 240
638 fgFontFileArray.Add(new TObjString("STIXSiz1SymBol.otf")); // 250
639 fgFontFileArray.Add(new TObjString("STIXSiz2Sym.otf")); // 260
640 fgFontFileArray.Add(new TObjString("STIXSiz2SymBol.otf")); // 270
641
642 fgFontFileArray.Add(new TObjString("STIXSiz3Sym.otf")); // 280
643 fgFontFileArray.Add(new TObjString("STIXSiz3SymBol.otf")); // 290
644 fgFontFileArray.Add(new TObjString("STIXSiz4Sym.otf")); // 300
645 fgFontFileArray.Add(new TObjString("STIXSiz4SymBol.otf")); // 310
646
647 fgFontFileArray.Add(new TObjString("STIXSiz5Sym.otf")); // 320
648 fgFontFileArray.Add(new TObjString("DroidSansFallback.ttf")); // 330
649 fgFontFileArray.Add(new TObjString("DroidSansFallback.ttf")); // 340
650 fgFontFileArray.Add(new TObjString("DroidSansFallback.ttf")); // 350
651
652 for (Int_t i = 10; i <= 20; i+=2)
653 fgFontSizeArray.push_back(i);
654 for (Int_t i = 24; i <= 64; i+=4)
655 fgFontSizeArray.push_back(i);
656 for (Int_t i = 72; i <= 128; i+=8)
657 fgFontSizeArray.push_back(i);
658
660}
661
662////////////////////////////////////////////////////////////////////////////////
663/// Delete FTFFont objects registered for destruction.
664
666{
667 FontList_i it = fFontTrash.begin();
668 while (it != fFontTrash.end())
669 {
670 if ((*it)->IncTrashCount() > 10000)
671 {
672 FontMap_i mi = fFontMap.find(**it);
673 assert(mi != fFontMap.end());
674 fFontMap.erase(mi);
675 delete (*it)->GetFont();
676
677 FontList_i li = it++;
678 fFontTrash.erase(li);
679 }
680 else
681 {
682 ++it;
683 }
684 }
685}
dim_t fSize
#define f(i)
Definition RSha256.hxx:104
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:252
Option_t Option_t mgn
Option_t Option_t TPoint TPoint angle
Option_t Option_t TPoint TPoint const char mode
char name[80]
Definition TGX11.cxx:148
R__EXTERN TSystem * gSystem
Definition TSystem.h:582
#define gVirtualX
Definition TVirtualX.h:368
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:503
A FreeType GL font manager.
virtual ~TGLFontManager()
Destructor.
static TObjArray fgFontFileArray
void RegisterFont(Int_t size, Int_t file, TGLFont::EMode mode, TGLFont &out)
Provide font with given size, file and FTGL class.
static Int_t GetFontSize(Int_t ds)
Get availabe font size.
static void InitStatics()
Create a list of available font files and allowed font sizes.
static Int_t GetExtendedFontStartIndex()
static Bool_t fgStaticInitDone
static FontSizeVec_t fgFontSizeArray
static Int_t fgExtendedFontStart
static FontSizeVec_t * GetFontSizeArray()
Get valid font size vector.
std::list< constTGLFont * >::iterator FontList_i
void ClearFontTrash()
Delete FTFFont objects registered for destruction.
std::map< TGLFont, Int_t >::iterator FontMap_i
std::vector< Int_t > FontSizeVec_t
static const char * GetFontNameFromId(Int_t)
Get font name from TAttAxis font id.
static TObjArray * GetFontFileArray()
Get id to file name map.
void ReleaseFont(TGLFont &font)
Release font with given attributes.
FontList_t fFontTrash
A wrapper class for FTFont.
void MeasureBaseLineParams(Float_t &ascent, Float_t &descent, Float_t &line_height, const char *txt="Xj") const
Measure font's base-line parameters from the passed text.
Float_t GetDescent() const
Get font's descent. The returned value is positive.
virtual ~TGLFont()
Destructor.
Float_t GetAscent() const
Get font's ascent.
EMode GetMode() const
void SetManager(TGLFontManager *mng)
void SetFont(FTFont *f)
Float_t GetLineHeight() const
Get font's line-height.
EMode fMode
Int_t GetSize() const
const FTFont * GetFont() const
void RenderHelper(const Char *txt, Double_t x, Double_t y, Double_t angle, Double_t) const
mgn is simply ignored, because ROOT's TVirtualX TGX11 are complete mess with painting attributes.
FTFont * fFont
TGLFontManager * fManager
Int_t GetFile() const
UInt_t fTextAlign
void BBox(const char *txt, Float_t &llx, Float_t &lly, Float_t &llz, Float_t &urx, Float_t &ury, Float_t &urz) const
Get bounding box.
Int_t fTrashCount
Float_t fDepth
void Render(const char *txt, Double_t x, Double_t y, Double_t angle, Double_t mgn) const
virtual void PostRender() const
Reset GL state after FTFont rendering.
void SetDepth(Float_t d)
virtual void PreRender(Bool_t autoLight=kTRUE, Bool_t lightOn=kFALSE) const
Set-up GL state before FTFont rendering.
Int_t fSize
TGLFont()
Constructor.
void CopyAttributes(const TGLFont &o)
Assignment operator.
Int_t fFile
An array of TObjects.
Definition TObjArray.h:31
Collectable string class.
Definition TObjString.h:28
TString & String()
Definition TObjString.h:48
static const TString & GetTTFFontDir()
Get the fonts directory in the installation. Static utility function.
Definition TROOT.cxx:3527
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384
virtual char * Which(const char *search, const char *file, EAccessMode mode=kFileExists)
Find location of file in a search path.
Definition TSystem.cxx:1559
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Int_t CeilNint(Double_t x)
Returns the nearest integer of TMath::Ceil(x).
Definition TMath.h:685
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Binary search in an array of n values to locate value.
Definition TMathBase.h:329