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 const UInt_t align = gVirtualX->GetTextAlign();
198
199 //Here's the nice X11 bullshido: you call gVirtualX->SetTextAlign(11),
200 //later gVirtualX->GetTextAling() will give you 7. Brilliant!
201 //But with Cocoa you'll have 11. As it should be, of course.
202
203 if (gVirtualX->InheritsFrom("TGCocoa")) {
204 const UInt_t hAlign = UInt_t(align / 10);
205 switch (hAlign) {
206 case 1:
207 xc = 0.5 * dx;
208 break;
209 case 2:
210 break;
211 case 3:
212 xc = -0.5 * dy;
213 break;
214 }
215
216 const UInt_t vAlign = UInt_t(align % 10);
217 switch (vAlign) {
218 case 1:
219 yc = 0.5 * dy;
220 break;
221 case 2:
222 break;
223 case 3:
224 yc = -0.5 * dy;
225 break;
226 }
227 } else {
228 switch (align) {
229 case 7:
230 xc += 0.5 * dx;
231 yc += 0.5 * dy;
232 break;
233 case 8:
234 yc += 0.5 * dy;
235 break;
236 case 9:
237 xc -= 0.5 * dx;
238 yc += 0.5 * dy;
239 break;
240 case 4:
241 xc += 0.5 * dx;
242 break;
243 case 5:
244 break;
245 case 6:
246 xc = -0.5 * dx;
247 break;
248 case 1:
249 xc += 0.5 * dx;
250 yc -= 0.5 * dy;
251 break;
252 case 2:
253 yc -= 0.5 * dy;
254 break;
255 case 3:
256 xc -= 0.5 * dx;
257 yc -= 0.5 * dy;
258 break;
259 }
260 }
261
262 glTranslated(x, y, 0.);
263 glRotated(angle, 0., 0., 1.);
264 glTranslated(xc, yc, 0.);
265 glTranslated(-0.5 * dx, -0.5 * dy, 0.);
266 //glScaled(mgn, mgn, 1.);
267
268 const_cast<FTFont*>(fFont)->Render(txt);
269
270 glPopMatrix();
271}
272
273////////////////////////////////////////////////////////////////////////////////
274
276{
278}
279
280////////////////////////////////////////////////////////////////////////////////
281
283{
285}
286
287////////////////////////////////////////////////////////////////////////////////
288/// Render text.
289
290void TGLFont::Render(const TString &txt) const
291{
292 Bool_t scaleDepth = (fMode == kExtrude && fDepth != 1.0f);
293
294 if (scaleDepth) {
295 glPushMatrix();
296 // !!! 0.2*fSize is hard-coded in TGLFontManager::GetFont(), too.
297 glTranslatef(0.0f, 0.0f, 0.5f*fDepth * 0.2f*fSize);
298 glScalef(1.0f, 1.0f, fDepth);
299 }
300
301 // FTGL is not const correct.
302 const_cast<FTFont*>(fFont)->Render(txt);
303
304 if (scaleDepth) {
305 glPopMatrix();
306 }
307}
308
309////////////////////////////////////////////////////////////////////////////////
310/// Render text with given alignmentrepl and at given position.
311
314{
315 glPushMatrix();
316
317 glTranslatef(x, y, z);
318
319 x=0, y=0;
320 Float_t llx, lly, llz, urx, ury, urz;
321 BBox(txt, llx, lly, llz, urx, ury, urz);
322
323 switch (alignH)
324 {
325 case TGLFont::kRight:
326 x = -urx;
327 break;
328
330 x = -urx*0.5;
331 break;
332 default:
333 break;
334 };
335
336 switch (alignV)
337 {
338 case TGLFont::kBottom:
339 y = -ury;
340 break;
342 y = -ury*0.5;
343 break;
344 default:
345 break;
346 };
347
349 {
350 glRasterPos2i(0, 0);
351 glBitmap(0, 0, 0, 0, x, y, 0);
352 }
353 else
354 {
355 glTranslatef(x, y, 0);
356 }
357 Render(txt);
358 glPopMatrix();
359}
360
361////////////////////////////////////////////////////////////////////////////////
362/// Set-up GL state before FTFont rendering.
363
365{
366 switch (fMode)
367 {
368 case kBitmap:
369 case kPixmap:
373 glAlphaFunc(GL_GEQUAL, 0.0625);
374 break;
375 case kTexture:
381 glAlphaFunc(GL_GEQUAL, 0.0625);
382 break;
383 case kOutline:
384 case kPolygon:
385 case kExtrude:
389 break;
390 default:
391 Warning("TGLFont::PreRender", "Font mode undefined.");
393 break;
394 }
395
398 else
400}
401
402////////////////////////////////////////////////////////////////////////////////
403/// Reset GL state after FTFont rendering.
404
406{
407 glPopAttrib();
408}
409
410/** \class TGLFontManager
411\ingroup opengl
412A FreeType GL font manager.
413
414Each GL rendering context has an instance of FTGLManager.
415This enables FTGL fonts to be shared same way as textures and display lists.
416*/
417
418
423
424////////////////////////////////////////////////////////////////////////////////
425/// Destructor.
426
428{
429 FontMap_i it = fFontMap.begin();
430 while (it != fFontMap.end()) {
431 delete it->first.GetFont();
432 it++;
433 }
434 fFontMap.clear();
435}
436
437////////////////////////////////////////////////////////////////////////////////
438/// Provide font with given size, file and FTGL class.
439
441{
443
445 if (mode == out.GetMode() && fileID == out.GetFile() && size == out.GetSize())
446 return;
447
449 if (it == fFontMap.end())
450 {
451 TString ttpath, file;
452 ttpath = gEnv->GetValue("Root.TTGLFontPath", TROOT::GetTTFFontDir());
453 {
454 //For extenede we have both ttf and otf.
456 ((TObjString*)fgFontFileArray[fileID])->String() + ".ttf" :
457 ((TObjString*)fgFontFileArray[fileID])->String());
458 file = fp;
459 delete [] fp;
460 }
461
462 FTFont* ftfont = 0;
463 switch (mode)
464 {
465 case TGLFont::kBitmap:
466 ftfont = new FTGLBitmapFont(file);
467 break;
468 case TGLFont::kPixmap:
469 ftfont = new FTGLPixmapFont(file);
470 break;
472 ftfont = new FTGLOutlineFont(file);
473 break;
475 ftfont = new FTGLPolygonFont(file);
476 break;
478 ftfont = new FTGLExtrdFont(file);
479 ftfont->Depth(0.2*size);
480 break;
482 ftfont = new FTGLTextureFont(file);
483 break;
484 default:
485 Error("TGLFontManager::RegisterFont", "invalid FTGL type");
486 return;
487 break;
488 }
489 ftfont->FaceSize(size);
490 const TGLFont &mf = fFontMap.insert(std::make_pair(TGLFont(size, fileID, mode, ftfont, 0), 1)).first->first;
491 out.CopyAttributes(mf);
492 }
493 else
494 {
495 if (it->first.GetTrashCount() > 0) {
496 fFontTrash.remove(&(it->first));
497 it->first.SetTrashCount(0);
498 }
499 ++(it->second);
500 out.CopyAttributes(it->first);
501 }
502 out.SetManager(this);
503}
504
505////////////////////////////////////////////////////////////////////////////////
506/// Get mapping from ttf id to font names. Table taken from TTF.cxx.
507
509{
511 TIter next(farr);
512 TObjString* os;
513 Int_t cnt = 0;
514 while ((os = (TObjString*) next()) != 0)
515 {
516 if (os->String() == name)
517 break;
518 cnt++;
519 }
520
521 if (cnt < farr->GetEntries())
522 RegisterFont(size, cnt, mode, out);
523 else
524 Error("TGLFontManager::RegisterFont", "unknown font name %s", name);
525}
526
527////////////////////////////////////////////////////////////////////////////////
528/// Release font with given attributes. Returns false if font has
529/// not been found in the managers font set.
530
532{
533 FontMap_i it = fFontMap.find(font);
534
535 if (it != fFontMap.end())
536 {
537 --(it->second);
538 if (it->second == 0)
539 {
540 assert(it->first.GetTrashCount() == 0);
541 it->first.IncTrashCount();
542 fFontTrash.push_back(&it->first);
543 }
544 }
545}
546
547////////////////////////////////////////////////////////////////////////////////
548/// Get id to file name map.
549
555
556////////////////////////////////////////////////////////////////////////////////
557/// Get valid font size vector.
558
564
565////////////////////////////////////////////////////////////////////////////////
566
568{
570
571 assert(fgExtendedFontStart > 0 && "GetExtendedFontStartIndex, invalid index");
572
573 return fgExtendedFontStart;
574}
575
576////////////////////////////////////////////////////////////////////////////////
577/// Get availabe font size.
578
580{
582
585
586 if (idx < 0) idx = 0;
587 return fgFontSizeArray[idx];
588}
589
590////////////////////////////////////////////////////////////////////////////////
591/// Get availabe font size.
592
594{
595 if (ds < min) ds = min;
596 if (ds > max) ds = max;
597 return GetFontSize(ds);
598}
599
600////////////////////////////////////////////////////////////////////////////////
601/// Get font name from TAttAxis font id.
602
604{
606
607 Int_t fontIndex = id / 10;
608
609 if (fontIndex > fgFontFileArray.GetEntries() || !fontIndex)
610 fontIndex = 5;//arialbd
611 else
612 fontIndex -= 1;
613
615 return os->String().Data();
616}
617
618////////////////////////////////////////////////////////////////////////////////
619/// Create a list of available font files and allowed font sizes.
620
622{
623 fgFontFileArray.Add(new TObjString("timesi")); // 10
624 fgFontFileArray.Add(new TObjString("timesbd")); // 20
625 fgFontFileArray.Add(new TObjString("timesbi")); // 30
626
627 fgFontFileArray.Add(new TObjString("arial")); // 40
628 fgFontFileArray.Add(new TObjString("ariali")); // 50
629 fgFontFileArray.Add(new TObjString("arialbd")); // 60
630 fgFontFileArray.Add(new TObjString("arialbi")); // 70
631
632 fgFontFileArray.Add(new TObjString("cour")); // 80
633 fgFontFileArray.Add(new TObjString("couri")); // 90
634 fgFontFileArray.Add(new TObjString("courbd")); // 100
635 fgFontFileArray.Add(new TObjString("courbi")); // 110
636
637 fgFontFileArray.Add(new TObjString("symbol")); // 120
638 fgFontFileArray.Add(new TObjString("times")); // 130
639 fgFontFileArray.Add(new TObjString("wingding")); // 140
640 fgFontFileArray.Add(new TObjString("symbol")); // 150
641
643 //"Extended" fonts for gl-pad.
644 //fgPadFontStart + ...
645 fgFontFileArray.Add(new TObjString("FreeSerifItalic.otf")); // 10 (160)
646 fgFontFileArray.Add(new TObjString("FreeSerifBold.otf")); // 20 (170)
647 fgFontFileArray.Add(new TObjString("FreeSerifBoldItalic.otf")); // 30
648
649 fgFontFileArray.Add(new TObjString("FreeSans.otf")); // 40
650 fgFontFileArray.Add(new TObjString("FreeSansOblique.otf")); // 50
651 fgFontFileArray.Add(new TObjString("FreeSansBold.otf")); // 60
652 fgFontFileArray.Add(new TObjString("FreeSansBoldOblique.otf")); // 70
653
654 fgFontFileArray.Add(new TObjString("FreeMono.otf")); // 80
655 fgFontFileArray.Add(new TObjString("FreeMonoOblique.otf")); // 90
656 fgFontFileArray.Add(new TObjString("FreeMonoBold.otf")); // 100
657 fgFontFileArray.Add(new TObjString("FreeMonoBoldOblique.otf")); // 110
658
659 fgFontFileArray.Add(new TObjString("symbol.ttf")); // 120
660 fgFontFileArray.Add(new TObjString("FreeSerif.otf")); // 130
661 fgFontFileArray.Add(new TObjString("wingding.ttf")); // 140
662 fgFontFileArray.Add(new TObjString("symbol.ttf")); // 150
663
664 fgFontFileArray.Add(new TObjString("STIXGeneral.otf")); // 200
665 fgFontFileArray.Add(new TObjString("STIXGeneralItalic.otf")); // 210
666 fgFontFileArray.Add(new TObjString("STIXGeneralBol.otf")); // 220
667 fgFontFileArray.Add(new TObjString("STIXGeneralBolIta.otf")); // 230
668
669 fgFontFileArray.Add(new TObjString("STIXSiz1Sym.otf")); // 240
670 fgFontFileArray.Add(new TObjString("STIXSiz1SymBol.otf")); // 250
671 fgFontFileArray.Add(new TObjString("STIXSiz2Sym.otf")); // 260
672 fgFontFileArray.Add(new TObjString("STIXSiz2SymBol.otf")); // 270
673
674 fgFontFileArray.Add(new TObjString("STIXSiz3Sym.otf")); // 280
675 fgFontFileArray.Add(new TObjString("STIXSiz3SymBol.otf")); // 290
676 fgFontFileArray.Add(new TObjString("STIXSiz4Sym.otf")); // 300
677 fgFontFileArray.Add(new TObjString("STIXSiz4SymBol.otf")); // 310
678
679 fgFontFileArray.Add(new TObjString("STIXSiz5Sym.otf")); // 320
680 fgFontFileArray.Add(new TObjString("DroidSansFallback.ttf")); // 330
681 fgFontFileArray.Add(new TObjString("DroidSansFallback.ttf")); // 340
682 fgFontFileArray.Add(new TObjString("DroidSansFallback.ttf")); // 350
683
684 for (Int_t i = 10; i <= 20; i+=2)
685 fgFontSizeArray.push_back(i);
686 for (Int_t i = 24; i <= 64; i+=4)
687 fgFontSizeArray.push_back(i);
688 for (Int_t i = 72; i <= 128; i+=8)
689 fgFontSizeArray.push_back(i);
690
692}
693
694////////////////////////////////////////////////////////////////////////////////
695/// Delete FTFFont objects registered for destruction.
696
698{
699 FontList_i it = fFontTrash.begin();
700 while (it != fFontTrash.end())
701 {
702 if ((*it)->IncTrashCount() > 10000)
703 {
704 FontMap_i mi = fFontMap.find(**it);
705 assert(mi != fFontMap.end());
706 fFontMap.erase(mi);
707 delete (*it)->GetFont();
708
709 FontList_i li = it++;
710 fFontTrash.erase(li);
711 }
712 else
713 {
714 ++it;
715 }
716 }
717}
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:110
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
#define gVirtualX
Definition TVirtualX.h:337
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:490
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.
void SetManager(TGLFontManager *mng)
void SetFont(FTFont *f)
Float_t GetLineHeight() const
Get font's line-height.
EMode fMode
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
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
static const TString & GetTTFFontDir()
Get the fonts directory in the installation. Static utility function.
Definition TROOT.cxx:3244
Basic string class.
Definition TString.h:138
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:348