Logo ROOT   6.08/07
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; }
199  FontStruct_t GetFontStruct() const { return fFontStruct; }
200  FontStruct_t operator()() const;
201  void GetFontMetrics(FontMetrics_t *m) const;
202  FontAttributes_t GetFontAttributes() const { return fFA; }
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);
245  TGFont *GetFontFromAttributes(FontAttributes_t *fa, TGFont *fontPtr);
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
Handle_t FontStruct_t
Definition: GuiTypes.h:40
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
Int_t TextHeight() const
Definition: TGFont.h:207
Collectable string class.
Definition: TObjString.h:32
void operator=(const TGFont &font)
const char Option_t
Definition: RtypesCore.h:62
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
Definitions for TRefCnt, base class for reference counted objects.
Definition: TRefCnt.h:29
Int_t fPointsize
Definition: TGFont.h:79
friend class TGTextLayout
Definition: TGFont.h:158
friend class TGFont
Definition: TGFont.h:123
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
const char * fString
Definition: TGFont.h:127
FontAttributes_t GetFontAttributes() const
Definition: TGFont.h:202
TObjString * fNamedHash
Definition: TGFont.h:165
Handle_t FontH_t
Definition: GuiTypes.h:36
TGFont(const char *name)
Definition: TGFont.h:178
THashTable implements a hash table to store TObject&#39;s.
Definition: THashTable.h:39
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:254
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
THashTable * fList
Definition: TGFont.h:235
void operator=(const TGTextLayout &tlayout)
Int_t fLinespace
Definition: TGFont.h:70
Int_t fNumChunks
Definition: TGFont.h:129
FontMetrics_t fFM
Definition: TGFont.h:163
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
FontAttributes_t()
Definition: TGFont.h:85
ETextLayoutFlags
Definition: TGFont.h:41
Bool_t fFixed
Definition: TGFont.h:72
double f(double x)
void Print(std::ostream &os, const OptionType &opt)
THashTable * fUidTable
Definition: TGFont.h:236
Definition: TGFont.h:155
FontStruct_t GetFontStruct() const
Definition: TGFont.h:199
Double_t y[n]
Definition: legend1.C:17
Int_t fUnderlinePos
Definition: TGFont.h:167
Int_t fSlant
Definition: TGFont.h:81
Int_t fUnderline
Definition: TGFont.h:82
FontStruct_t fFontStruct
Definition: TGFont.h:161
Mother of all ROOT objects.
Definition: TObject.h:37
FontAttributes_t(const FontAttributes_t &f)
Definition: TGFont.h:93
const char * fFamily
Definition: TGFont.h:78
Int_t fTabWidth
Definition: TGFont.h:166
EFontWeight
Definition: TGFont.h:49
FontH_t fFontH
Definition: TGFont.h:162
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 fMaxWidth
Definition: TGFont.h:71
Int_t fUnderlineHeight
Definition: TGFont.h:169
char name[80]
Definition: TGX11.cxx:109
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a primitive as a C++ statement(s) on output stream "out".
Definition: TObject.cxx:709
FontH_t GetFontHandle() const
Definition: TGFont.h:198