Logo ROOT   6.10/09
Reference Guide
FontCache.h
Go to the documentation of this file.
1 // @(#)root/graf2d:$Id$
2 // Author: Timur Pocheptsov 19/03/2012
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2012, 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 #ifndef ROOT_FontCache
13 #define ROOT_FontCache
14 
15 #include <string>
16 #include <vector>
17 #include <list>
18 #include <map>
19 
20 #include <ApplicationServices/ApplicationServices.h>
21 
22 #include "XLFDParser.h"
23 #include "GuiTypes.h"
24 
25 #include "CocoaUtils.h"
26 
27 //////////////////////////////////////////////////////////////////
28 // //
29 // FontCache class: //
30 // ROOT's GUI relies on TVirtualX to create and use fonts, //
31 // fonts are referenced by integer identifiers. //
32 // //
33 //////////////////////////////////////////////////////////////////
34 
35 namespace ROOT {
36 namespace MacOSX {
37 namespace Details {
38 
39 class FontCache {
40 public:
41  enum Details {
42  nPadFonts = 15
43  };
44 
45  FontCache();
46 
47  FontStruct_t LoadFont(const X11::XLFDName &xlfd);
48  void UnloadFont(FontStruct_t font);
49 
50  char **ListFonts(const X11::XLFDName &xlfd, int maxNames, int &count);
51  void FreeFontNames(char **fontList);
52 
53  unsigned GetTextWidth(FontStruct_t font, const char *text, int nChars);
54  void GetFontProperties(FontStruct_t font, int &maxAscent, int &maxDescent);
55 
56  //Select the existing font or create a new one and select it.
57  CTFontRef SelectFont(Font_t fontIndex, Float_t fontSize);
58 
59  //Typographical bounds (whatever it means),
60  //for the current selected font and text.
61  void GetTextBounds(UInt_t &w, UInt_t &h, const char *text)const;
62  void GetTextBounds(UInt_t &w, UInt_t &h, const std::vector<UniChar> & unichars)const;
63  //
64  double GetAscent()const;
65  double GetAscent(const char *text)const;
66  double GetAscent(const std::vector<UniChar> & unichars)const;
67  double GetDescent()const;
68  double GetDescent(const char *text)const;
69  double GetDescent(const std::vector<UniChar> & unichars)const;
70  double GetLeading()const;
71 
72 private:
73 
74  //We have "two symbolic" fonts, both of them use the same symbol.ttf (index 11),
75  //but the second one (index
76  CTFontRef SelectSymbolFont(Float_t fontSize, unsigned fontIndex);
77 
79 
80  //These are fonts for GUI. Weird map, as I can see now.
81  std::map<CTFontRef, CTFontGuard_t> fLoadedFonts;
82  typedef std::map<CTFontRef, CTFontGuard_t>::iterator font_iterator;
83  typedef std::map<CTFontRef, CTFontGuard_t>::const_iterator const_font_iterator;
84 
85  //Fonts for TPad's graphics.
86  typedef std::map<UInt_t, CTFontGuard_t> FontMap_t;
87  typedef FontMap_t::iterator font_map_iterator;
88  typedef FontMap_t::const_iterator const_font_map_iterator;
89 
90  FontMap_t fFonts[nPadFonts];
91  CTFontRef fSelectedFont;
92 
93  //FontList can be requested by TGCocoa::ListFonts,
94  //the return value is char **, and later it's freed by
95  //TGCocoa::FreeFontNames, again using char **.
96  //In my case, I have to somehow map char ** to two
97  //data sets - char ** itself + real strings, whose
98  //addresses are in char **.
99  //fList, after it's filled and returned by TGCocoa,
100  //is immutable, so later I can find this FontList
101  //comparing char ** and &fList[0].
102  struct FontList {
103  std::vector<char *> fList;
104  std::vector<char> fStringData;
105  };
106 
107  std::list<FontList> fFontLists;//list of "lists" of fonts :)
109 
110  typedef std::map<std::string, std::string> PSNameMap_t;
112 
113  FontCache(const FontCache &rhs);
114  FontCache &operator = (const FontCache &rhs);
115 
117 };
118 
119 }
120 }
121 }
122 
123 #endif
char ** ListFonts(const X11::XLFDName &xlfd, int maxNames, int &count)
Definition: FontCache.mm:299
Handle_t FontStruct_t
Definition: GuiTypes.h:38
Util::CFStrongReference< CTFontRef > CTFontGuard_t
Definition: FontCache.h:78
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
FontStruct_t LoadFont(const X11::XLFDName &xlfd)
Definition: FontCache.mm:244
FontMap_t fFonts[nPadFonts]
Definition: FontCache.h:90
float Float_t
Definition: RtypesCore.h:53
std::map< std::string, std::string > PSNameMap_t
Definition: FontCache.h:110
FontCache & operator=(const FontCache &rhs)
TH1 * h
Definition: legend2.C:5
std::map< CTFontRef, CTFontGuard_t > fLoadedFonts
Definition: FontCache.h:81
unsigned GetTextWidth(FontStruct_t font, const char *text, int nChars)
Definition: FontCache.mm:418
short Font_t
Definition: RtypesCore.h:75
std::map< CTFontRef, CTFontGuard_t >::const_iterator const_font_iterator
Definition: FontCache.h:83
std::map< CTFontRef, CTFontGuard_t >::iterator font_iterator
Definition: FontCache.h:82
FontMap_t::const_iterator const_font_map_iterator
Definition: FontCache.h:88
void FreeFontNames(char **fontList)
Definition: FontCache.mm:402
std::map< UInt_t, CTFontGuard_t > FontMap_t
Definition: FontCache.h:86
unsigned int UInt_t
Definition: RtypesCore.h:42
std::list< FontList > fFontLists
Definition: FontCache.h:107
FontMap_t::iterator font_map_iterator
Definition: FontCache.h:87
void GetFontProperties(FontStruct_t font, int &maxAscent, int &maxDescent)
Definition: FontCache.mm:448
TText * text
CTFontRef SelectFont(Font_t fontIndex, Float_t fontSize)
Definition: FontCache.mm:464
void GetTextBounds(UInt_t &w, UInt_t &h, const char *text) const
Definition: FontCache.mm:573
void UnloadFont(FontStruct_t font)
Definition: FontCache.mm:288
CTFontRef SelectSymbolFont(Float_t fontSize, unsigned fontIndex)
Definition: FontCache.mm:500