Logo ROOT   6.10/00
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "Riostream.h"
13 #include "RConfigure.h"
14 #include "TGLFontManager.h"
15 
16 
17 #include "TROOT.h"
18 #include "TVirtualX.h"
19 #include "TMath.h"
20 #include "TSystem.h"
21 #include "TEnv.h"
22 #include "TObjString.h"
23 #include "TGLUtil.h"
24 #include "TGLIncludes.h"
25 
26 // Direct inclusion of FTGL headers is deprecated in ftgl-2.1.3 while
27 // ftgl-2.1.2 shipped with ROOT requires manual inclusion.
28 #ifndef BUILTIN_FTGL
29 # include <FTGL/ftgl.h>
30 #else
31 # include "FTFont.h"
32 # include "FTGLExtrdFont.h"
33 # include "FTGLOutlineFont.h"
34 # include "FTGLPolygonFont.h"
35 # include "FTGLTextureFont.h"
36 # include "FTGLPixmapFont.h"
37 # include "FTGLBitmapFont.h"
38 #endif
39 
40 #include <fontconfig/fontconfig.h>
41 
42 /** \class TGLFont
43 \ingroup opengl
44 A wrapper class for FTFont.
45 Holds pointer to FTFont object and its description: face size, font file
46 and class ID. It wraps Render and BBox functions.
47 */
48 
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Constructor.
53 
55  fFont(0), fManager(0), fDepth(0),
56  fSize(0), fFile(0), fMode(kUndef),
57  fTrashCount(0)
58 {
59 }
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// Constructor.
63 
64 TGLFont::TGLFont(Int_t size, Int_t font, EMode mode, FTFont* f, TGLFontManager* mng):
65  fFont(f), fManager(mng), fDepth(0),
66  fSize(size), fFile(font), fMode(mode),
67  fTrashCount(0)
68 {
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Assignment operator.
73 
75  fFont(0), fManager(0), fDepth(0), fTrashCount(0)
76 {
77  fFont = (FTFont*)o.GetFont();
78 
79  fSize = o.fSize;
80  fFile = o.fFile;
81  fMode = o.fMode;
82 
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 ///Destructor
88 
90 {
91  if (fManager) fManager->ReleaseFont(*this);
92 }
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Assignment operator.
96 
98 {
99  SetFont(o.fFont);
100  SetManager(o.fManager);
101 
102  SetDepth(o.fDepth);
103 
104  fSize = o.fSize;
105  fFile = o.fFile;
106  fMode = o.fMode;
107 
109 }
110 
111 
112 /******************************************************************************/
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 /// Get font's ascent.
116 
118 {
119  return fFont->Ascender();
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// Get font's descent. The returned value is positive.
124 
126 {
127  return -fFont->Descender();
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Get font's line-height.
132 
134 {
135  return fFont->LineHeight();
136 }
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 /// Measure font's base-line parameters from the passed text.
140 /// Note that the measured parameters are not the same as the ones
141 /// returned by get-functions - those were set by the font designer.
142 
143 void TGLFont::MeasureBaseLineParams(Float_t& ascent, Float_t& descent, Float_t& line_height,
144  const char* txt) const
145 {
146  Float_t dum, lly, ury;
147  const_cast<FTFont*>(fFont)->BBox(txt, dum, lly, dum, dum, ury, dum);
148  ascent = ury;
149  descent = -lly;
150  line_height = ury - lly;
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Get bounding box.
155 
156 void TGLFont::BBox(const char* txt,
157  Float_t& llx, Float_t& lly, Float_t& llz,
158  Float_t& urx, Float_t& ury, Float_t& urz) const
159 {
160  // FTGL is not const correct.
161  const_cast<FTFont*>(fFont)->BBox(txt, llx, lly, llz, urx, ury, urz);
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// Get bounding box.
166 
167 void TGLFont::BBox(const wchar_t* txt,
168  Float_t& llx, Float_t& lly, Float_t& llz,
169  Float_t& urx, Float_t& ury, Float_t& urz) const
170 {
171  // FTGL is not const correct.
172  const_cast<FTFont*>(fFont)->BBox(txt, llx, lly, llz, urx, ury, urz);
173 }
174 
175 ////////////////////////////////////////////////////////////////////////////////
176 ///mgn is simply ignored, because ROOT's TVirtualX TGX11 are complete mess with
177 ///painting attributes.
178 
179 template<class Char>
180 void TGLFont::RenderHelper(const Char *txt, Double_t x, Double_t y, Double_t angle, Double_t /*mgn*/) const
181 {
182  glPushMatrix();
183  //glLoadIdentity();
184 
185  // FTGL is not const correct.
186  Float_t llx = 0.f, lly = 0.f, llz = 0.f, urx = 0.f, ury = 0.f, urz = 0.f;
187  BBox(txt, llx, lly, llz, urx, ury, urz);
188 
189  /*
190  V\H | left | center | right
191  _______________________________
192  bottom | 7 | 8 | 9
193  _______________________________
194  center | 4 | 5 | 6
195  _______________________________
196  top | 1 | 2 | 3
197  */
198  const Double_t dx = urx - llx, dy = ury - lly;
199  Double_t xc = 0., yc = 0.;
200  const UInt_t align = gVirtualX->GetTextAlign();
201 
202  //Here's the nice X11 bullshido: you call gVirtualX->SetTextAlign(11),
203  //later gVirtualX->GetTextAling() will give you 7. Brilliant!
204  //But with Cocoa you'll have 11. As it should be, of course.
205 
206  if (gVirtualX->InheritsFrom("TGCocoa")) {
207  const UInt_t hAlign = 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  const UInt_t vAlign = UInt_t(align % 10);
220  switch (vAlign) {
221  case 1:
222  yc = 0.5 * dy;
223  break;
224  case 2:
225  break;
226  case 3:
227  yc = -0.5 * dy;
228  break;
229  }
230  } else {
231  switch (align) {
232  case 7:
233  xc += 0.5 * dx;
234  yc += 0.5 * dy;
235  break;
236  case 8:
237  yc += 0.5 * dy;
238  break;
239  case 9:
240  xc -= 0.5 * dx;
241  yc += 0.5 * dy;
242  break;
243  case 4:
244  xc += 0.5 * dx;
245  break;
246  case 5:
247  break;
248  case 6:
249  xc = -0.5 * dx;
250  break;
251  case 1:
252  xc += 0.5 * dx;
253  yc -= 0.5 * dy;
254  break;
255  case 2:
256  yc -= 0.5 * dy;
257  break;
258  case 3:
259  xc -= 0.5 * dx;
260  yc -= 0.5 * dy;
261  break;
262  }
263  }
264 
265  glTranslated(x, y, 0.);
266  glRotated(angle, 0., 0., 1.);
267  glTranslated(xc, yc, 0.);
268  glTranslated(-0.5 * dx, -0.5 * dy, 0.);
269  //glScaled(mgn, mgn, 1.);
270 
271  const_cast<FTFont*>(fFont)->Render(txt);
272 
273  glPopMatrix();
274 }
275 
276 ////////////////////////////////////////////////////////////////////////////////
277 
278 void TGLFont::Render(const wchar_t* txt, Double_t x, Double_t y, Double_t angle, Double_t mgn) const
279 {
280  RenderHelper(txt, x, y, angle, mgn);
281 }
282 
283 ////////////////////////////////////////////////////////////////////////////////
284 
285 void TGLFont::Render(const char* txt, Double_t x, Double_t y, Double_t angle, Double_t mgn) const
286 {
287  RenderHelper(txt, x, y, angle, mgn);
288 }
289 
290 ////////////////////////////////////////////////////////////////////////////////
291 /// Render text.
292 
293 void TGLFont::Render(const TString &txt) const
294 {
295  Bool_t scaleDepth = (fMode == kExtrude && fDepth != 1.0f);
296 
297  if (scaleDepth) {
298  glPushMatrix();
299  // !!! 0.2*fSize is hard-coded in TGLFontManager::GetFont(), too.
300  glTranslatef(0.0f, 0.0f, 0.5f*fDepth * 0.2f*fSize);
301  glScalef(1.0f, 1.0f, fDepth);
302  }
303 
304  // FTGL is not const correct.
305  const_cast<FTFont*>(fFont)->Render(txt);
306 
307  if (scaleDepth) {
308  glPopMatrix();
309  }
310 }
311 
312 ////////////////////////////////////////////////////////////////////////////////
313 /// Render text with given alignmentrepl and at given position.
314 
316  ETextAlignH_e alignH, ETextAlignV_e alignV) const
317 {
318  glPushMatrix();
319 
320  glTranslatef(x, y, z);
321 
322  x=0, y=0;
323  Float_t llx, lly, llz, urx, ury, urz;
324  BBox(txt, llx, lly, llz, urx, ury, urz);
325 
326  switch (alignH)
327  {
328  case TGLFont::kRight:
329  x = -urx;
330  break;
331 
332  case TGLFont::kCenterH:
333  x = -urx*0.5;
334  break;
335  default:
336  break;
337  };
338 
339  switch (alignV)
340  {
341  case TGLFont::kBottom:
342  y = -ury;
343  break;
344  case TGLFont::kCenterV:
345  y = -ury*0.5;
346  break;
347  default:
348  break;
349  };
350 
352  {
353  glRasterPos2i(0, 0);
354  glBitmap(0, 0, 0, 0, x, y, 0);
355  }
356  else
357  {
358  glTranslatef(x, y, 0);
359  }
360  Render(txt);
361  glPopMatrix();
362 }
363 
364 ////////////////////////////////////////////////////////////////////////////////
365 /// Set-up GL state before FTFont rendering.
366 
367 void TGLFont::PreRender(Bool_t autoLight, Bool_t lightOn) const
368 {
369  switch (fMode)
370  {
371  case kBitmap:
372  case kPixmap:
373  glPushAttrib(GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT);
374  glEnable(GL_ALPHA_TEST);
375  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
376  glAlphaFunc(GL_GEQUAL, 0.0625);
377  break;
378  case kTexture:
379  glPushAttrib(GL_POLYGON_BIT | GL_ENABLE_BIT);
380  glEnable(GL_TEXTURE_2D);
381  glDisable(GL_CULL_FACE);
382  glEnable(GL_ALPHA_TEST);
383  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
384  glAlphaFunc(GL_GEQUAL, 0.0625);
385  break;
386  case kOutline:
387  case kPolygon:
388  case kExtrude:
389  glPushAttrib(GL_POLYGON_BIT | GL_ENABLE_BIT);
390  glEnable(GL_NORMALIZE);
391  glDisable(GL_CULL_FACE);
392  break;
393  default:
394  Warning("TGLFont::PreRender", "Font mode undefined.");
395  glPushAttrib(GL_LIGHTING_BIT);
396  break;
397  }
398 
399  if ((autoLight && fMode > TGLFont::kOutline) || (!autoLight && lightOn))
400  glEnable(GL_LIGHTING);
401  else
402  glDisable(GL_LIGHTING);
403 }
404 
405 ////////////////////////////////////////////////////////////////////////////////
406 /// Reset GL state after FTFont rendering.
407 
409 {
410  glPopAttrib();
411 }
412 
413 /** \class TGLFontManager
414 \ingroup opengl
415 A FreeType GL font manager.
416 
417 Each GL rendering context has an instance of FTGLManager.
418 This enables FTGL fonts to be shared same way as textures and display lists.
419 */
420 
422 
427 
428 ////////////////////////////////////////////////////////////////////////////////
429 /// Destructor.
430 
432 {
433  FontMap_i it = fFontMap.begin();
434  while (it != fFontMap.end()) {
435  delete it->first.GetFont();
436  it++;
437  }
438  fFontMap.clear();
439 }
440 
441 ////////////////////////////////////////////////////////////////////////////////
442 /// Provide font with given size, file and FTGL class.
443 
445 {
447 
448  Int_t size = GetFontSize(sizeIn);
449  if (mode == out.GetMode() && fileID == out.GetFile() && size == out.GetSize())
450  return;
451 
452  FontMap_i it = fFontMap.find(TGLFont(size, fileID, mode));
453  if (it == fFontMap.end())
454  {
455  char *file = NULL;
456  int ttindex = 0;
457 
458  FcPattern *pat, *match;
459  FcCharSet *set = NULL;
460  FcResult result;
461 
462  pat = FcPatternCreate ();
463 
464  switch (fileID) {
465  case 0:
466  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freeserif");
467  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
468  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ITALIC);
469  break;
470  case 1:
471  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freeserif");
472  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
473  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
474  break;
475  case 2:
476  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freeserif");
477  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
478  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ITALIC);
479  break;
480  case 3:
481  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freesans");
482  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
483  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
484  break;
485  case 4:
486  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freesans");
487  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
488  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ITALIC);
489  break;
490  case 5:
491  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freesans");
492  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
493  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
494  break;
495  case 6:
496  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freesans");
497  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
498  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ITALIC);
499  break;
500  case 7:
501  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freemono");
502  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
503  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
504  break;
505  case 8:
506  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freemono");
507  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
508  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ITALIC);
509  break;
510  case 9:
511  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freemono");
512  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
513  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
514  break;
515  case 10:
516  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freemono");
517  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
518  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ITALIC);
519  break;
520  case 11:
521  case 14:
522  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"symbol");
523  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
524  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
525  break;
526  case 12:
527  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"freeserif");
528  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
529  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
530  break;
531  case 13:
532  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"dingbats");
533  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
534  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
535  break;
536  case 15:
537  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"stixgeneral");
538  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
539  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
540  break;
541  case 16:
542  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"stixgeneral");
543  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
544  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ITALIC);
545  break;
546  case 17:
547  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"stixgeneral");
548  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
549  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
550  break;
551  case 18:
552  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"stixgeneral");
553  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
554  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ITALIC);
555  break;
556  case 19:
557  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"stixsize1");
558  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
559  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
560  break;
561  case 20:
562  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"stixsize1");
563  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
564  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
565  break;
566  case 21:
567  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"stixsize2");
568  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
569  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
570  break;
571  case 22:
572  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"stixsize2");
573  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
574  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
575  break;
576  case 23:
577  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"stixsize3");
578  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
579  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
580  break;
581  case 24:
582  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"stixsize3");
583  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
584  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
585  break;
586  case 25:
587  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"stixsize4");
588  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
589  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
590  break;
591  case 26:
592  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"stixsize4");
593  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_BOLD);
594  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
595  break;
596  case 27:
597  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"stixsize5");
598  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
599  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
600  break;
601  case 28:
602  case 29:
603  case 30:
604  FcPatternAddString (pat, FC_FAMILY, (const FcChar8*)"droidsansfallback");
605  FcPatternAddInteger (pat, FC_WEIGHT, FC_WEIGHT_REGULAR);
606  FcPatternAddInteger (pat, FC_SLANT, FC_SLANT_ROMAN);
607  set = FcCharSetCreate ();
608  FcCharSetAddChar (set, 0x0410); // Cyrillic
609  FcCharSetAddChar (set, 0x4e00); // CJK
610  FcPatternAddCharSet (pat, FC_CHARSET, set);
611  }
612 
613  FcConfigSubstitute (NULL, pat, FcMatchPattern);
614  FcDefaultSubstitute (pat);
615  match = FcFontMatch (NULL, pat, &result);
616  char *ttfnt;
617  FcPatternGetString (match, FC_FILE, 0, (FcChar8**)&ttfnt);
618  file = StrDup (ttfnt);
619  FcPatternGetInteger (match, FC_INDEX, 0, &ttindex);
620  FcPatternDestroy (match);
621  FcPatternDestroy (pat);
622  if (set) FcCharSetDestroy (set);
623 
624  FTFont* ftfont = 0;
625  switch (mode)
626  {
627  case TGLFont::kBitmap:
628  ftfont = new FTGLBitmapFont(file);
629  break;
630  case TGLFont::kPixmap:
631  ftfont = new FTGLPixmapFont(file);
632  break;
633  case TGLFont::kOutline:
634  ftfont = new FTGLOutlineFont(file);
635  break;
636  case TGLFont::kPolygon:
637  ftfont = new FTGLPolygonFont(file);
638  break;
639  case TGLFont::kExtrude:
640  ftfont = new FTGLExtrdFont(file);
641  ftfont->Depth(0.2*size);
642  break;
643  case TGLFont::kTexture:
644  ftfont = new FTGLTextureFont(file);
645  break;
646  default:
647  delete [] file;
648  Error("TGLFontManager::RegisterFont", "invalid FTGL type");
649  return;
650  break;
651  }
652 
653  delete [] file;
654 
655  ftfont->FaceSize(size);
656  const TGLFont &mf = fFontMap.insert(std::make_pair(TGLFont(size, fileID, mode, ftfont, 0), 1)).first->first;
657  out.CopyAttributes(mf);
658  }
659  else
660  {
661  if (it->first.GetTrashCount() > 0) {
662  fFontTrash.remove(&(it->first));
663  it->first.SetTrashCount(0);
664  }
665  ++(it->second);
666  out.CopyAttributes(it->first);
667  }
668  out.SetManager(this);
669 }
670 
671 ////////////////////////////////////////////////////////////////////////////////
672 /// Get mapping from ttf id to font names. Table taken from TTF.cxx.
673 
674 void TGLFontManager::RegisterFont(Int_t size, const char* name, TGLFont::EMode mode, TGLFont &out)
675 {
676  TObjArray* farr = GetFontFileArray();
677  TIter next(farr);
678  TObjString* os;
679  Int_t cnt = 0;
680  while ((os = (TObjString*) next()) != 0)
681  {
682  if (os->String() == name)
683  break;
684  cnt++;
685  }
686 
687  if (cnt < farr->GetEntries())
688  RegisterFont(size, cnt, mode, out);
689  else
690  Error("TGLFontManager::RegisterFont", "unknown font name %s", name);
691 }
692 
693 ////////////////////////////////////////////////////////////////////////////////
694 /// Release font with given attributes. Returns false if font has
695 /// not been found in the managers font set.
696 
698 {
699  FontMap_i it = fFontMap.find(font);
700 
701  if (it != fFontMap.end())
702  {
703  --(it->second);
704  if (it->second == 0)
705  {
706  assert(it->first.GetTrashCount() == 0);
707  it->first.IncTrashCount();
708  fFontTrash.push_back(&it->first);
709  }
710  }
711 }
712 
713 ////////////////////////////////////////////////////////////////////////////////
714 /// Get id to file name map.
715 
717 {
719  return &fgFontFileArray;
720 }
721 
722 ////////////////////////////////////////////////////////////////////////////////
723 /// Get valid font size vector.
724 
726 {
728  return &fgFontSizeArray;
729 }
730 
731 ////////////////////////////////////////////////////////////////////////////////
732 
734 {
736 
737  return fgExtendedFontStart;
738 }
739 
740 ////////////////////////////////////////////////////////////////////////////////
741 /// Get availabe font size.
742 
744 {
746 
748  TMath::CeilNint(ds));
749 
750  if (idx < 0) idx = 0;
751  return fgFontSizeArray[idx];
752 }
753 
754 ////////////////////////////////////////////////////////////////////////////////
755 /// Get availabe font size.
756 
758 {
759  if (ds < min) ds = min;
760  if (ds > max) ds = max;
761  return GetFontSize(ds);
762 }
763 
764 ////////////////////////////////////////////////////////////////////////////////
765 /// Get font name from TAttAxis font id.
766 
768 {
770 
771  Int_t fontIndex = id / 10;
772 
773  if (fontIndex > fgFontFileArray.GetEntries() || !fontIndex)
774  fontIndex = 5;//arialbd
775  else
776  fontIndex -= 1;
777 
778  TObjString* os = (TObjString*)fgFontFileArray[fontIndex];
779  return os->String().Data();
780 }
781 
782 ////////////////////////////////////////////////////////////////////////////////
783 /// Create a list of available font files and allowed font sizes.
784 
786 {
787  fgFontFileArray.Add(new TObjString("timesi")); // 10
788  fgFontFileArray.Add(new TObjString("timesbd")); // 20
789  fgFontFileArray.Add(new TObjString("timesbi")); // 30
790 
791  fgFontFileArray.Add(new TObjString("arial")); // 40
792  fgFontFileArray.Add(new TObjString("ariali")); // 50
793  fgFontFileArray.Add(new TObjString("arialbd")); // 60
794  fgFontFileArray.Add(new TObjString("arialbi")); // 70
795 
796  fgFontFileArray.Add(new TObjString("cour")); // 80
797  fgFontFileArray.Add(new TObjString("couri")); // 90
798  fgFontFileArray.Add(new TObjString("courbd")); // 100
799  fgFontFileArray.Add(new TObjString("courbi")); // 110
800 
801  fgFontFileArray.Add(new TObjString("symbol")); // 120
802  fgFontFileArray.Add(new TObjString("times")); // 130
803  fgFontFileArray.Add(new TObjString("wingding")); // 140
804  fgFontFileArray.Add(new TObjString("symbol")); // 150
805 
806  fgFontFileArray.Add(new TObjString("STIXGeneral.otf")); // 200
807  fgFontFileArray.Add(new TObjString("STIXGeneralItalic.otf")); // 210
808  fgFontFileArray.Add(new TObjString("STIXGeneralBol.otf")); // 220
809  fgFontFileArray.Add(new TObjString("STIXGeneralBolIta.otf")); // 230
810 
811  fgFontFileArray.Add(new TObjString("STIXSiz1Sym.otf")); // 240
812  fgFontFileArray.Add(new TObjString("STIXSiz1SymBol.otf")); // 250
813  fgFontFileArray.Add(new TObjString("STIXSiz2Sym.otf")); // 260
814  fgFontFileArray.Add(new TObjString("STIXSiz2SymBol.otf")); // 270
815 
816  fgFontFileArray.Add(new TObjString("STIXSiz3Sym.otf")); // 280
817  fgFontFileArray.Add(new TObjString("STIXSiz3SymBol.otf")); // 290
818  fgFontFileArray.Add(new TObjString("STIXSiz4Sym.otf")); // 300
819  fgFontFileArray.Add(new TObjString("STIXSiz4SymBol.otf")); // 310
820 
821  fgFontFileArray.Add(new TObjString("STIXSiz5Sym.otf")); // 320
822  fgFontFileArray.Add(new TObjString("DroidSansFallback.ttf")); // 330
823  fgFontFileArray.Add(new TObjString("DroidSansFallback.ttf")); // 340
824  fgFontFileArray.Add(new TObjString("DroidSansFallback.ttf")); // 350
825 
826  for (Int_t i = 10; i <= 20; i+=2)
827  fgFontSizeArray.push_back(i);
828  for (Int_t i = 24; i <= 64; i+=4)
829  fgFontSizeArray.push_back(i);
830  for (Int_t i = 72; i <= 128; i+=8)
831  fgFontSizeArray.push_back(i);
832 
834 }
835 
836 ////////////////////////////////////////////////////////////////////////////////
837 /// Delete FTFFont objects registered for destruction.
838 
840 {
841  FontList_i it = fFontTrash.begin();
842  while (it != fFontTrash.end())
843  {
844  if ((*it)->IncTrashCount() > 10000)
845  {
846  FontMap_i mi = fFontMap.find(**it);
847  assert(mi != fFontMap.end());
848  fFontMap.erase(mi);
849  delete (*it)->GetFont();
850 
851  FontList_i li = it++;
852  fFontTrash.erase(li);
853  }
854  else
855  {
856  ++it;
857  }
858  }
859 }
An array of TObjects.
Definition: TObjArray.h:37
Int_t GetSize() const
static const char * GetFontNameFromId(Int_t)
Get font name from TAttAxis font id.
Collectable string class.
Definition: TObjString.h:28
float Float_t
Definition: RtypesCore.h:53
Int_t fSize
FTFont * fFont
FontList_t fFontTrash
void ReleaseFont(TGLFont &font)
Release font with given attributes.
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const char * Char
#define NULL
Definition: RtypesCore.h:88
void CopyAttributes(const TGLFont &o)
Assignment operator.
static void InitStatics()
Create a list of available font files and allowed font sizes.
const char * Data() const
Definition: TString.h:344
void RegisterFont(Int_t size, Int_t file, TGLFont::EMode mode, TGLFont &out)
Provide font with given size, file and FTGL class.
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.
Double_t x[n]
Definition: legend1.C:17
void Render(const char *txt, Double_t x, Double_t y, Double_t angle, Double_t mgn) const
static TObjArray fgFontFileArray
std::vector< Int_t > FontSizeVec_t
static Int_t GetFontSize(Int_t ds)
Get availabe font size.
Float_t GetLineHeight() const
Get font&#39;s line-height.
std::list< const TGLFont * >::iterator FontList_i
Int_t fTrashCount
void Error(const char *location, const char *msgfmt,...)
EMode GetMode() const
Float_t GetAscent() const
Get font&#39;s ascent.
void MeasureBaseLineParams(Float_t &ascent, Float_t &descent, Float_t &line_height, const char *txt="Xj") const
Measure font&#39;s base-line parameters from the passed text.
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void PostRender() const
Reset GL state after FTFont rendering.
Float_t fDepth
static Int_t fgExtendedFontStart
Float_t GetDescent() const
Get font&#39;s descent. The returned value is positive.
const FTFont * GetFont() const
void Warning(const char *location, const char *msgfmt,...)
TString & String()
Definition: TObjString.h:49
Int_t fFile
#define gVirtualX
Definition: TVirtualX.h:350
char * StrDup(const char *str)
Duplicate the string str.
Definition: TString.cxx:2524
const Bool_t kFALSE
Definition: RtypesCore.h:92
std::map< TGLFont, Int_t >::iterator FontMap_i
#define ClassImp(name)
Definition: Rtypes.h:336
double f(double x)
double Double_t
Definition: RtypesCore.h:55
void SetManager(TGLFontManager *mng)
Double_t y[n]
Definition: legend1.C:17
Int_t GetFile() const
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:494
TGLFontManager * fManager
static Bool_t fgStaticInitDone
void SetFont(FTFont *f)
virtual ~TGLFontManager()
Destructor.
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
FontMap_t fFontMap
EMode fMode
A FreeType GL font manager.
void RenderHelper(const Char *txt, Double_t x, Double_t y, Double_t angle, Double_t) const
mgn is simply ignored, because ROOT&#39;s TVirtualX TGX11 are complete mess with painting attributes...
TGLFont()
Constructor.
virtual void PreRender(Bool_t autoLight=kTRUE, Bool_t lightOn=kFALSE) const
Set-up GL state before FTFont rendering.
static FontSizeVec_t * GetFontSizeArray()
Get valid font size vector.
static FontSizeVec_t fgFontSizeArray
void Add(TObject *obj)
Definition: TObjArray.h:73
double result[121]
static TObjArray * GetFontFileArray()
Get id to file name map.
A wrapper class for FTFont.
static Int_t GetExtendedFontStartIndex()
void ClearFontTrash()
Delete FTFFont objects registered for destruction.
const Bool_t kTRUE
Definition: RtypesCore.h:91
void SetDepth(Float_t d)
virtual ~TGLFont()
Destructor.
Int_t CeilNint(Double_t x)
Definition: TMath.h:597
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Definition: TMath.h:1093
char name[80]
Definition: TGX11.cxx:109
const char * cnt
Definition: TXMLSetup.cxx:75