ROOT  6.06/09
Reference Guide
TGFont.h
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: Fons Rademakers 20/5/2003
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2003, 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_TGFont
13 #define ROOT_TGFont
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TGFont and TGFontPool //
19 // //
20 // Encapsulate fonts used in the GUI system. //
21 // TGFontPool provides a pool of fonts. //
22 // //
23 //////////////////////////////////////////////////////////////////////////
24 
25 #ifndef ROOT_TNamed
26 #include "TNamed.h"
27 #endif
28 #ifndef ROOT_TGObject
29 #include "TGObject.h"
30 #endif
31 #ifndef ROOT_TRefCnt
32 #include "TRefCnt.h"
33 #endif
34 
35 class THashTable;
36 class TObjString;
37 class TGFont;
38 
39 // Flags passed to TGFont::MeasureChars and TGFont::ComputeTextLayout
40 
47 };
48 
57 };
58 
59 enum EFontSlant {
64 };
65 
66 
67 struct FontMetrics_t {
68  Int_t fAscent; // from baseline to top of font
69  Int_t fDescent; // from baseline to bottom of font
70  Int_t fLinespace; // the sum of the ascent and descent
71  Int_t fMaxWidth; // width of widest character in font
72  Bool_t fFixed; // true if monospace, false otherwise
73 };
74 
75 
77 
78  const char *fFamily; // Font family. The most important field.
79  Int_t fPointsize; // Pointsize of font, 0 for default size, or negative number meaning pixel size.
80  Int_t fWeight; // Weight flag; see below for def'n.
81  Int_t fSlant; // Slant flag; see below for def'n.
82  Int_t fUnderline; // Non-zero for underline font.
83  Int_t fOverstrike; // Non-zero for overstrike font.
84 
85  FontAttributes_t(): // default constructor
86  fFamily (0),
87  fPointsize (0),
88  fWeight (kFontWeightNormal),
89  fSlant (kFontSlantRoman),
90  fUnderline (0),
91  fOverstrike(0) { }
92 
93  FontAttributes_t(const FontAttributes_t& f): // copy constructor
94  fFamily (f.fFamily),
95  fPointsize (f.fPointsize),
96  fWeight (f.fWeight),
97  fSlant (f.fSlant),
98  fUnderline (f.fUnderline),
99  fOverstrike(f.fOverstrike) { }
100 
101  FontAttributes_t& operator=(const FontAttributes_t& f) // assignment operator
102  {
103  if (this != &f) {
104  fFamily = f.fFamily;
105  fPointsize = f.fPointsize;
106  fWeight = f.fWeight;
107  fSlant = f.fSlant;
108  fUnderline = f.fUnderline;
109  fOverstrike = f.fOverstrike;
110  }
111  return *this;
112  }
113 
114 };
115 
116 
117 
118 struct LayoutChunk_t;
119 
120 
121 class TGTextLayout : public TObject {
122 
123 friend class TGFont;
124 
125 protected:
126  const TGFont *fFont; // The font used when laying out the text.
127  const char *fString; // The string that was layed out.
128  Int_t fWidth; // The maximum width of all lines in the text layout.
129  Int_t fNumChunks; // Number of chunks actually used in following array.
130  LayoutChunk_t *fChunks; // Array of chunks. The actual size will be maxChunks.
131 
132  TGTextLayout(const TGTextLayout &tlayout); // not implemented
133  void operator=(const TGTextLayout &tlayout); // not implemented
134 
135 public:
136  TGTextLayout(): fFont(NULL), fString(""), fWidth(0), fNumChunks(0), fChunks(NULL) {}
137  virtual ~TGTextLayout();
138 
139  void DrawText(Drawable_t dst, GContext_t gc, Int_t x, Int_t y,
140  Int_t firstChar, Int_t lastChar) const;
141  void UnderlineChar(Drawable_t dst, GContext_t gc,
142  Int_t x, Int_t y, Int_t underline) const;
143  Int_t PointToChar(Int_t x, Int_t y) const;
144  Int_t CharBbox(Int_t index, Int_t *x, Int_t *y, Int_t *w, Int_t *h) const;
145  Int_t DistanceToText(Int_t x, Int_t y) const;
146  Int_t IntersectText(Int_t x, Int_t y, Int_t w, Int_t h) const;
147  void ToPostscript(TString *dst) const;
148 
149  ClassDef(TGTextLayout,0) // Keep track of string measurement information.
150 };
151 
152 
153 // The following class is used to keep track of the generic information about a font.
154 
155 class TGFont : public TNamed, public TRefCnt {
156 
157 friend class TGFontPool;
158 friend class TGTextLayout;
159 
160 private:
161  FontStruct_t fFontStruct; // Low level graphics fontstruct
162  FontH_t fFontH; // Font handle (derived from fontstruct)
163  FontMetrics_t fFM; // Cached font metrics
164  FontAttributes_t fFA; // Actual font attributes obtained when the font was created
165  TObjString *fNamedHash; // Pointer to the named object TGFont was based on
166  Int_t fTabWidth; // Width of tabs in this font (pixels).
167  Int_t fUnderlinePos; // Offset from baseline to origin of underline bar
168  // (used for drawing underlines on a non-underlined font).
169  Int_t fUnderlineHeight; // Height of underline bar (used for drawing
170  // underlines on a non-underlined font).
171  char fTypes[256]; // Array giving types of all characters in
172  // the font, used when displaying control characters.
173  Int_t fWidths[256]; // Array giving widths of all possible characters in the font.
174  Int_t fBarHeight; // Height of underline or overstrike bar
175  // (used for simulating a native underlined or strikeout font).
176 
177 protected:
178  TGFont(const char *name)
179  : TNamed(name,""), TRefCnt(), fFontStruct(0), fFontH(0), fFM(),
180  fFA(), fNamedHash(0), fTabWidth(0), fUnderlinePos(0), fUnderlineHeight(0), fBarHeight(0)
181  {
182  SetRefCount(1);
183  for (Int_t i=0; i<256; i++) {
184  fWidths[i] = 0;
185  fTypes[i] = ' ';
186  }
187  }
188 
189  TGFont(const TGFont &font); // not implemented
190  void operator=(const TGFont &font); // not implemented
191 
192  LayoutChunk_t *NewChunk(TGTextLayout *layout, int *maxPtr,
193  const char *start, int numChars,
194  int curX, int newX, int y) const;
195 public:
196  virtual ~TGFont();
197 
198  FontH_t GetFontHandle() const { return fFontH; }
200  FontStruct_t operator()() const;
201  void GetFontMetrics(FontMetrics_t *m) const;
203 
204  Int_t PostscriptFontName(TString *dst) const;
205  Int_t TextWidth(const char *string, Int_t numChars = -1) const;
206  Int_t XTextWidth(const char *string, Int_t numChars = -1) const;
207  Int_t TextHeight() const { return fFM.fLinespace; }
208  void UnderlineChars(Drawable_t dst, GContext_t gc,
209  const char *string, Int_t x, Int_t y,
210  Int_t firstChar, Int_t lastChar) const;
211  TGTextLayout *ComputeTextLayout(const char *string, Int_t numChars,
212  Int_t wrapLength, Int_t justify, Int_t flags,
213  UInt_t *width, UInt_t *height) const;
214  Int_t MeasureChars(const char *source, Int_t numChars, Int_t maxLength,
215  Int_t flags, Int_t *length) const;
216  void DrawCharsExp(Drawable_t dst, GContext_t gc, const char *source,
217  Int_t numChars, Int_t x, Int_t y) const;
218  void DrawChars(Drawable_t dst, GContext_t gc, const char *source,
219  Int_t numChars, Int_t x, Int_t y) const;
220 
221  void Print(Option_t *option="") const;
222  virtual void SavePrimitive(std::ostream &out, Option_t * = "");
223 
224  ClassDef(TGFont,0) // GUI font description
225 };
226 
227 
228 struct FontStateMap_t;
229 struct XLFDAttributes_t;
230 
231 
232 class TGFontPool : public TGObject {
233 
234 private:
235  THashTable *fList; // TGFont objects pool
236  THashTable *fUidTable; // Hash table for some used string values like family names, etc.
237  THashTable *fNamedTable; // Map a name to a set of attributes for a font
238 
239  TGFontPool(const TGFontPool& fp); // not implemented
240  TGFontPool& operator=(const TGFontPool& fp); // not implemented
241 
242 protected:
243  const char *GetUid(const char *string);
244  Bool_t ParseXLFD(const char *string, XLFDAttributes_t *xa);
246  int FindStateNum(const FontStateMap_t *map, const char *strKey);
247  const char *FindStateString(const FontStateMap_t *map, int numKey);
248  Bool_t FieldSpecified(const char *field);
249  TGFont *GetNativeFont(const char *name, Bool_t fixedDefault = kTRUE);
250  TGFont *MakeFont(TGFont *font, FontStruct_t fontStruct, const char *fontName);
251 
252 public:
253  TGFontPool(TGClient *client);
254  virtual ~TGFontPool();
255 
256  TGFont *GetFont(const char *font, Bool_t fixedDefault = kTRUE);
257  TGFont *GetFont(const TGFont *font);
258  TGFont *GetFont(FontStruct_t font);
259  TGFont *GetFont(const char *family, Int_t ptsize, Int_t weight, Int_t slant);
260 
261  void FreeFont(const TGFont *font);
262 
263  TGFont *FindFont(FontStruct_t font) const;
264  TGFont *FindFontByHandle(FontH_t font) const;
265 
266  char **GetAttributeInfo(const FontAttributes_t *fa);
267  void FreeAttributeInfo(char **info);
268  char **GetFontFamilies();
269  void FreeFontFamilies(char **f);
270  Bool_t ParseFontName(const char *string, FontAttributes_t *fa);
271  const char *NameOfFont(TGFont *font);
272 
273  void Print(Option_t *option="") const;
274 
275  ClassDef(TGFontPool,0) // Font pool
276 };
277 
278 #endif
void DrawCharsExp(Drawable_t dst, GContext_t gc, const char *source, Int_t numChars, Int_t x, Int_t y) const
Draw a string of characters on the screen.
Definition: TGFont.cxx:1448
Handle_t FontStruct_t
Definition: GuiTypes.h:40
Int_t CharBbox(Int_t index, Int_t *x, Int_t *y, Int_t *w, Int_t *h) const
Use the information in the TGTextLayout token to return the bounding box for the character specified ...
Definition: TGFont.cxx:1094
char ** GetFontFamilies()
Return information about the font families that are available on the current display.
Definition: TGFont.cxx:2258
void ToPostscript(TString *dst) const
Outputs the contents of a text layout in Postscript format.
Definition: TGFont.cxx:1319
FontAttributes_t & operator=(const FontAttributes_t &f)
Definition: TGFont.h:101
FontAttributes_t fFA
Definition: TGFont.h:164
Int_t fOverstrike
Definition: TGFont.h:83
void FreeFontFamilies(char **f)
Delete an array of families allocated GetFontFamilies() method.
Definition: TGFont.cxx:2317
virtual ~TGTextLayout()
destructor
Definition: TGFont.cxx:885
void UnderlineChars(Drawable_t dst, GContext_t gc, const char *string, Int_t x, Int_t y, Int_t firstChar, Int_t lastChar) const
This procedure draws an underline for a given range of characters in a given string.
Definition: TGFont.cxx:605
FontAttributes_t GetFontAttributes() const
Definition: TGFont.h:202
void DrawChars(Drawable_t dst, GContext_t gc, const char *source, Int_t numChars, Int_t x, Int_t y) const
Perform a quick sanity check to ensure we won't overflow the X coordinate space.
Definition: TGFont.cxx:1478
TGTextLayout * ComputeTextLayout(const char *string, Int_t numChars, Int_t wrapLength, Int_t justify, Int_t flags, UInt_t *width, UInt_t *height) const
Computes the amount of screen space needed to display a multi-line, justified string of text...
Definition: TGFont.cxx:649
Collectable string class.
Definition: TObjString.h:32
void operator=(const TGFont &font)
const char Option_t
Definition: RtypesCore.h:62
const char * NameOfFont(TGFont *font)
Given a font, return a textual string identifying it.
Definition: TGFont.cxx:2246
THashTable * fNamedTable
Definition: TGFont.h:237
#define BIT(n)
Definition: Rtypes.h:120
Int_t fWeight
Definition: TGFont.h:80
TH1 * h
Definition: legend2.C:5
EFontSlant
Definition: TGFont.h:59
TGFont * MakeFont(TGFont *font, FontStruct_t fontStruct, const char *fontName)
Helper for GetNativeFont() and GetFontFromAttributes().
Definition: TGFont.cxx:2576
Definitions for TRefCnt, base class for reference counted objects.
Definition: TRefCnt.h:29
Int_t fPointsize
Definition: TGFont.h:79
FontH_t GetFontHandle() const
Definition: TGFont.h:198
Handle_t GContext_t
Definition: GuiTypes.h:39
Int_t fBarHeight
Definition: TGFont.h:174
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Handle_t Drawable_t
Definition: GuiTypes.h:32
void Print(Option_t *option="") const
List all fonts in the pool.
Definition: TGFont.cxx:1861
const char * fString
Definition: TGFont.h:127
char ** GetAttributeInfo(const FontAttributes_t *fa)
Return information about the font attributes as an array of strings.
Definition: TGFont.cxx:1790
int FindStateNum(const FontStateMap_t *map, const char *strKey)
Given a lookup table, map a string to a number in the table.
Definition: TGFont.cxx:2189
virtual void SavePrimitive(std::ostream &out, Option_t *="")
Save the used font as a C++ statement(s) on output stream out.
Definition: TGFont.cxx:1869
void FreeAttributeInfo(char **info)
Free attributes info.
Definition: TGFont.cxx:1844
FontStruct_t operator()() const
Not inline due to a bug in g++ 2.96 20000731 (Red Hat Linux 7.0)
Definition: TGFont.cxx:288
TObjString * fNamedHash
Definition: TGFont.h:165
Handle_t FontH_t
Definition: GuiTypes.h:36
TGFont(const char *name)
Definition: TGFont.h:178
TGFont * GetFontFromAttributes(FontAttributes_t *fa, TGFont *fontPtr)
Given a desired set of attributes for a font, find a font with the closest matching attributes and cr...
Definition: TGFont.cxx:2340
TGFont * GetNativeFont(const char *name, Bool_t fixedDefault=kTRUE)
The return value is a pointer to an TGFont object that represents the native font.
Definition: TGFont.cxx:2551
THashTable implements a hash table to store TObject's.
Definition: THashTable.h:39
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:254
Int_t PostscriptFontName(TString *dst) const
Return the name of the corresponding Postscript font for this TGFont.
Definition: TGFont.cxx:331
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
Int_t DistanceToText(Int_t x, Int_t y) const
Computes the distance in pixels from the given point to the given text layout.
Definition: TGFont.cxx:1176
Bool_t FieldSpecified(const char *field)
Helper function for ParseXLFD().
Definition: TGFont.cxx:2231
void DrawText(Drawable_t dst, GContext_t gc, Int_t x, Int_t y, Int_t firstChar, Int_t lastChar) const
Use the information in the TGTextLayout object to display a multi-line, justified string of text...
Definition: TGFont.cxx:908
THashTable * fList
Definition: TGFont.h:235
virtual ~TGFont()
Delete font.
Definition: TGFont.cxx:264
void UnderlineChar(Drawable_t dst, GContext_t gc, Int_t x, Int_t y, Int_t underline) const
Use the information in the TGTextLayout object to display an underline below an individual character...
Definition: TGFont.cxx:955
char * out
Definition: TBase64.cxx:29
void operator=(const TGTextLayout &tlayout)
Int_t XTextWidth(const char *string, Int_t numChars=-1) const
Return text widht in pixels.
Definition: TGFont.cxx:578
Int_t fLinespace
Definition: TGFont.h:70
Int_t fNumChunks
Definition: TGFont.h:129
Int_t PointToChar(Int_t x, Int_t y) const
Use the information in the TGTextLayout token to determine the character closest to the given point...
Definition: TGFont.cxx:988
void SetRefCount(UInt_t r)
Definition: TRefCnt.h:41
Double_t length(const TVector2 &v)
Definition: CsgOps.cxx:347
void FreeFont(const TGFont *font)
Free font. If ref count is 0 delete font.
Definition: TGFont.cxx:1703
FontMetrics_t fFM
Definition: TGFont.h:163
TGFont * FindFontByHandle(FontH_t font) const
Find font based on its font handle. Returns 0 if font is not found.
Definition: TGFont.cxx:1746
LayoutChunk_t * NewChunk(TGTextLayout *layout, int *maxPtr, const char *start, int numChars, int curX, int newX, int y) const
Helper function for ComputeTextLayout().
Definition: TGFont.cxx:1395
virtual ~TGFontPool()
Cleanup font pool.
Definition: TGFont.cxx:1529
unsigned int UInt_t
Definition: RtypesCore.h:42
TMarker * m
Definition: textangle.C:8
Int_t fWidth
Definition: TGFont.h:128
TGTextLayout()
Definition: TGFont.h:136
Int_t fWidths[256]
Definition: TGFont.h:173
FontAttributes_t()
Definition: TGFont.h:85
TGFontPool(const TGFontPool &fp)
Int_t TextHeight() const
Definition: TGFont.h:207
Bool_t ParseFontName(const char *string, FontAttributes_t *fa)
Converts a string into a set of font attributes that can be used to construct a font.
Definition: TGFont.cxx:1947
Bool_t ParseXLFD(const char *string, XLFDAttributes_t *xa)
Break up a fully specified XLFD into a set of font attributes.
Definition: TGFont.cxx:2038
ETextLayoutFlags
Definition: TGFont.h:41
Bool_t fFixed
Definition: TGFont.h:72
TGFont * GetFont(const char *font, Bool_t fixedDefault=kTRUE)
Get the specified font.
Definition: TGFont.cxx:1543
Int_t IntersectText(Int_t x, Int_t y, Int_t w, Int_t h) const
Determines whether a text layout lies entirely inside, entirely outside, or overlaps a given rectangl...
Definition: TGFont.cxx:1242
double f(double x)
void GetFontMetrics(FontMetrics_t *m) const
Get font metrics.
Definition: TGFont.cxx:274
THashTable * fUidTable
Definition: TGFont.h:236
Definition: TGFont.h:155
TGFontPool & operator=(const TGFontPool &fp)
Double_t y[n]
Definition: legend1.C:17
Int_t TextWidth(const char *string, Int_t numChars=-1) const
A wrapper function for the more complicated interface of MeasureChars.
Definition: TGFont.cxx:563
Int_t fUnderlinePos
Definition: TGFont.h:167
Int_t fSlant
Definition: TGFont.h:81
#define name(a, b)
Definition: linkTestLib0.cpp:5
Int_t fUnderline
Definition: TGFont.h:82
FontStruct_t fFontStruct
Definition: TGFont.h:161
Mother of all ROOT objects.
Definition: TObject.h:58
FontAttributes_t(const FontAttributes_t &f)
Definition: TGFont.h:93
const char * fFamily
Definition: TGFont.h:78
const char * FindStateString(const FontStateMap_t *map, int numKey)
Given a lookup table, map a number to a string in the table.
Definition: TGFont.cxx:2212
Int_t fTabWidth
Definition: TGFont.h:166
EFontWeight
Definition: TGFont.h:49
FontStruct_t GetFontStruct() const
Definition: TGFont.h:199
FontH_t fFontH
Definition: TGFont.h:162
TGFont * FindFont(FontStruct_t font) const
Find font based on its font struct. Returns 0 if font is not found.
Definition: TGFont.cxx:1729
const TGFont * fFont
Definition: TGFont.h:126
#define NULL
Definition: Rtypes.h:82
Int_t fAscent
Definition: TGFont.h:68
Int_t fDescent
Definition: TGFont.h:69
LayoutChunk_t * fChunks
Definition: TGFont.h:130
const Bool_t kTRUE
Definition: Rtypes.h:91
Int_t MeasureChars(const char *source, Int_t numChars, Int_t maxLength, Int_t flags, Int_t *length) const
Determine the number of characters from the string that will fit in the given horizontal span...
Definition: TGFont.cxx:476
Int_t fMaxWidth
Definition: TGFont.h:71
char fTypes[256]
Definition: TGFont.h:171
Int_t fUnderlineHeight
Definition: TGFont.h:169
const char * GetUid(const char *string)
Given a string, this procedure returns a unique identifier for the string.
Definition: TGFont.cxx:1770
void Print(Option_t *option="") const
Print font info.
Definition: TGFont.cxx:296