Logo ROOT   6.08/07
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 #ifndef ROOT_XLFDParser
23 #include "XLFDParser.h"
24 #endif
25 #ifndef ROOT_GuiTypes
26 #include "GuiTypes.h"
27 #endif
28 
29 #include "CocoaUtils.h"
30 
31 //////////////////////////////////////////////////////////////////
32 // //
33 // FontCache class: //
34 // ROOT's GUI relies on TVirtualX to create and use fonts, //
35 // fonts are referenced by integer identifiers. //
36 // //
37 //////////////////////////////////////////////////////////////////
38 
39 namespace ROOT {
40 namespace MacOSX {
41 namespace Details {
42 
43 class FontCache {
44 public:
45  enum Details {
46  nPadFonts = 15
47  };
48 
49  FontCache();
50 
51  FontStruct_t LoadFont(const X11::XLFDName &xlfd);
52  void UnloadFont(FontStruct_t font);
53 
54  char **ListFonts(const X11::XLFDName &xlfd, int maxNames, int &count);
55  void FreeFontNames(char **fontList);
56 
57  unsigned GetTextWidth(FontStruct_t font, const char *text, int nChars);
58  void GetFontProperties(FontStruct_t font, int &maxAscent, int &maxDescent);
59 
60  //Select the existing font or create a new one and select it.
61  CTFontRef SelectFont(Font_t fontIndex, Float_t fontSize);
62 
63  //Typographical bounds (whatever it means),
64  //for the current selected font and text.
65  void GetTextBounds(UInt_t &w, UInt_t &h, const char *text)const;
66  void GetTextBounds(UInt_t &w, UInt_t &h, const std::vector<UniChar> & unichars)const;
67  //
68  double GetAscent()const;
69  double GetAscent(const char *text)const;
70  double GetAscent(const std::vector<UniChar> & unichars)const;
71  double GetDescent()const;
72  double GetDescent(const char *text)const;
73  double GetDescent(const std::vector<UniChar> & unichars)const;
74  double GetLeading()const;
75 
76 private:
77 
78  //We have "two symbolic" fonts, both of them use the same symbol.ttf (index 11),
79  //but the second one (index
80  CTFontRef SelectSymbolFont(Float_t fontSize, unsigned fontIndex);
81 
83 
84  //These are fonts for GUI. Weird map, as I can see now.
85  std::map<CTFontRef, CTFontGuard_t> fLoadedFonts;
86  typedef std::map<CTFontRef, CTFontGuard_t>::iterator font_iterator;
87  typedef std::map<CTFontRef, CTFontGuard_t>::const_iterator const_font_iterator;
88 
89  //Fonts for TPad's graphics.
90  typedef std::map<UInt_t, CTFontGuard_t> FontMap_t;
91  typedef FontMap_t::iterator font_map_iterator;
92  typedef FontMap_t::const_iterator const_font_map_iterator;
93 
94  FontMap_t fFonts[nPadFonts];
95  CTFontRef fSelectedFont;
96 
97  //FontList can be requested by TGCocoa::ListFonts,
98  //the return value is char **, and later it's freed by
99  //TGCocoa::FreeFontNames, again using char **.
100  //In my case, I have to somehow map char ** to two
101  //data sets - char ** itself + real strings, whose
102  //addresses are in char **.
103  //fList, after it's filled and returned by TGCocoa,
104  //is immutable, so later I can find this FontList
105  //comparing char ** and &fList[0].
106  struct FontList {
107  std::vector<char *> fList;
108  std::vector<char> fStringData;
109  };
110 
111  std::list<FontList> fFontLists;//list of "lists" of fonts :)
113 
114  typedef std::map<std::string, std::string> PSNameMap_t;
116 
117  FontCache(const FontCache &rhs);
118  FontCache &operator = (const FontCache &rhs);
119 
121 };
122 
123 }
124 }
125 }
126 
127 #endif
char ** ListFonts(const X11::XLFDName &xlfd, int maxNames, int &count)
Definition: FontCache.mm:298
Handle_t FontStruct_t
Definition: GuiTypes.h:40
Util::CFStrongReference< CTFontRef > CTFontGuard_t
Definition: FontCache.h:82
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
FontStruct_t LoadFont(const X11::XLFDName &xlfd)
Definition: FontCache.mm:243
FontMap_t fFonts[nPadFonts]
Definition: FontCache.h:94
float Float_t
Definition: RtypesCore.h:53
std::map< std::string, std::string > PSNameMap_t
Definition: FontCache.h:114
FontCache & operator=(const FontCache &rhs)
TH1 * h
Definition: legend2.C:5
std::map< CTFontRef, CTFontGuard_t > fLoadedFonts
Definition: FontCache.h:85
unsigned GetTextWidth(FontStruct_t font, const char *text, int nChars)
Definition: FontCache.mm:417
short Font_t
Definition: RtypesCore.h:75
std::map< CTFontRef, CTFontGuard_t >::const_iterator const_font_iterator
Definition: FontCache.h:87
std::map< CTFontRef, CTFontGuard_t >::iterator font_iterator
Definition: FontCache.h:86
FontMap_t::const_iterator const_font_map_iterator
Definition: FontCache.h:92
void FreeFontNames(char **fontList)
Definition: FontCache.mm:401
std::map< UInt_t, CTFontGuard_t > FontMap_t
Definition: FontCache.h:90
unsigned int UInt_t
Definition: RtypesCore.h:42
std::list< FontList > fFontLists
Definition: FontCache.h:111
FontMap_t::iterator font_map_iterator
Definition: FontCache.h:91
void GetFontProperties(FontStruct_t font, int &maxAscent, int &maxDescent)
Definition: FontCache.mm:447
TText * text
CTFontRef SelectFont(Font_t fontIndex, Float_t fontSize)
Definition: FontCache.mm:463
void GetTextBounds(UInt_t &w, UInt_t &h, const char *text) const
Definition: FontCache.mm:572
void UnloadFont(FontStruct_t font)
Definition: FontCache.mm:287
CTFontRef SelectSymbolFont(Float_t fontSize, unsigned fontIndex)
Definition: FontCache.mm:499