ROOT logo
// @(#)root/gui:$Id$
// Author: Fons Rademakers   20/5/2003

/*************************************************************************
 * Copyright (C) 1995-2003, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TGFont
#define ROOT_TGFont


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TGFont and TGFontPool                                                //
//                                                                      //
// Encapsulate fonts used in the GUI system.                            //
// TGFontPool provides a pool of fonts.                                 //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TNamed
#include "TNamed.h"
#endif
#ifndef ROOT_TGObject
#include "TGObject.h"
#endif
#ifndef ROOT_TRefCnt
#include "TRefCnt.h"
#endif

class THashTable;
class TObjString;
class TGFont;

// Flags passed to TGFont::MeasureChars and TGFont::ComputeTextLayout

enum ETextLayoutFlags {
   kTextWholeWords = BIT(0),
   kTextAtLeastOne = BIT(1),
   kTextPartialOK  = BIT(2),
   kTextIgnoreTabs = BIT(3),
   kTextIgnoreNewlines = BIT(4)
};

enum EFontWeight {
   kFontWeightNormal = 0,
   kFontWeightMedium = 0,
   kFontWeightBold = 1,
   kFontWeightLight = 2,
   kFontWeightDemibold = 3,
   kFontWeightBlack = 4,
   kFontWeightUnknown = -1
};

enum EFontSlant {
   kFontSlantRoman = 0,
   kFontSlantItalic = 1,
   kFontSlantOblique = 2,
   kFontSlantUnknown = -1
};


struct FontMetrics_t {
   Int_t   fAscent;          // from baseline to top of font
   Int_t   fDescent;         // from baseline to bottom of font
   Int_t   fLinespace;       // the sum of the ascent and descent
   Int_t   fMaxWidth;        // width of widest character in font
   Bool_t  fFixed;           // true if monospace, false otherwise
};


struct FontAttributes_t {

   const char *fFamily; // Font family. The most important field.
   Int_t fPointsize;    // Pointsize of font, 0 for default size, or negative number meaning pixel size.
   Int_t fWeight;       // Weight flag; see below for def'n.
   Int_t fSlant;        // Slant flag; see below for def'n.
   Int_t fUnderline;    // Non-zero for underline font.
   Int_t fOverstrike;   // Non-zero for overstrike font.

   FontAttributes_t():  // default constructor
      fFamily    (0),
      fPointsize (0),
      fWeight    (kFontWeightNormal),
      fSlant     (kFontSlantRoman),
      fUnderline (0),
      fOverstrike(0) { }

   FontAttributes_t(const FontAttributes_t& f): // copy constructor
      fFamily    (f.fFamily),
      fPointsize (f.fPointsize),
      fWeight    (f.fWeight),
      fSlant     (f.fSlant),
      fUnderline (f.fUnderline),
      fOverstrike(f.fOverstrike) { }

   FontAttributes_t& operator=(const FontAttributes_t& f) // assignment operator
   {
      if (this != &f) {
         fFamily     = f.fFamily;
         fPointsize  = f.fPointsize;
         fWeight     = f.fWeight;
         fSlant      = f.fSlant;
         fUnderline  = f.fUnderline;
         fOverstrike = f.fOverstrike;
      }
      return *this;
   }

};



struct LayoutChunk_t;


class TGTextLayout : public TObject {

friend class TGFont;

protected:
   const TGFont  *fFont;         // The font used when laying out the text.
   const char    *fString;       // The string that was layed out.
   Int_t          fWidth;        // The maximum width of all lines in the text layout.
   Int_t          fNumChunks;    // Number of chunks actually used in following array.
   LayoutChunk_t *fChunks;       // Array of chunks. The actual size will be maxChunks.

   TGTextLayout(const TGTextLayout &tlayout);     // not implemented
   void operator=(const TGTextLayout &tlayout);   // not implemented

public:
   TGTextLayout(): fFont(NULL), fString(""), fWidth(0), fNumChunks(0), fChunks(NULL) {}
   virtual ~TGTextLayout();

   void   DrawText(Drawable_t dst, GContext_t gc, Int_t x, Int_t y,
                   Int_t firstChar, Int_t lastChar) const;
   void   UnderlineChar(Drawable_t dst, GContext_t gc,
                        Int_t x, Int_t y, Int_t underline) const;
   Int_t  PointToChar(Int_t x, Int_t y) const;
   Int_t  CharBbox(Int_t index, Int_t *x, Int_t *y, Int_t *w, Int_t *h) const;
   Int_t  DistanceToText(Int_t x, Int_t y) const;
   Int_t  IntersectText(Int_t x, Int_t y, Int_t w, Int_t h) const;
   void   ToPostscript(TString *dst) const;

   ClassDef(TGTextLayout,0)   // Keep track of string  measurement information.
};


// The following class is used to keep track of the generic information about a font.

class TGFont : public TNamed, public TRefCnt {

friend class TGFontPool;
friend class TGTextLayout;

private:
   FontStruct_t     fFontStruct;      // Low level graphics fontstruct
   FontH_t          fFontH;           // Font handle (derived from fontstruct)
   FontMetrics_t    fFM;              // Cached font metrics
   FontAttributes_t fFA;              // Actual font attributes obtained when the font was created
   TObjString      *fNamedHash;       // Pointer to the named object TGFont was based on
   Int_t            fTabWidth;        // Width of tabs in this font (pixels).
   Int_t            fUnderlinePos;    // Offset from baseline to origin of underline bar
                                      // (used for drawing underlines on a non-underlined font).
   Int_t            fUnderlineHeight; // Height of underline bar (used for drawing
                                      // underlines on a non-underlined font).
   char             fTypes[256];      // Array giving types of all characters in
                                      // the font, used when displaying control characters.
   Int_t            fWidths[256];     // Array giving widths of all possible characters in the font.
   Int_t            fBarHeight;       // Height of underline or overstrike bar
                                      // (used for simulating a native underlined or strikeout font).

protected:
   TGFont(const char *name)
     : TNamed(name,""), TRefCnt(), fFontStruct(0), fFontH(0), fFM(),
     fFA(), fNamedHash(0), fTabWidth(0), fUnderlinePos(0), fUnderlineHeight(0), fBarHeight(0)
   {
      SetRefCount(1);
      for (Int_t i=0; i<256; i++) {
         fWidths[i] = 0;
         fTypes[i]  = ' ';
      }
   }

   TGFont(const TGFont &font);           // not implemented
   void operator=(const TGFont &font);   // not implemented

   LayoutChunk_t *NewChunk(TGTextLayout *layout, int *maxPtr,
                           const char *start, int numChars,
                           int curX, int newX, int y) const;
public:
   virtual ~TGFont();

   FontH_t      GetFontHandle() const { return fFontH; }
   FontStruct_t GetFontStruct() const { return fFontStruct; }
   FontStruct_t operator()() const;
   void         GetFontMetrics(FontMetrics_t *m) const;
   FontAttributes_t GetFontAttributes() const { return fFA; }

   Int_t  PostscriptFontName(TString *dst) const;
   Int_t  TextWidth(const char *string, Int_t numChars = -1) const;
   Int_t  XTextWidth(const char *string, Int_t numChars = -1) const;
   Int_t  TextHeight() const { return fFM.fLinespace; }
   void   UnderlineChars(Drawable_t dst, GContext_t gc,
                        const char *string, Int_t x, Int_t y,
                        Int_t firstChar, Int_t lastChar) const;
   TGTextLayout *ComputeTextLayout(const char *string, Int_t numChars,
                                  Int_t wrapLength, Int_t justify, Int_t flags,
                                  UInt_t *width, UInt_t *height) const;
   Int_t  MeasureChars(const char *source, Int_t numChars, Int_t maxLength,
                      Int_t flags, Int_t *length) const;
   void   DrawCharsExp(Drawable_t dst, GContext_t gc, const char *source,
                      Int_t numChars, Int_t x, Int_t y) const;
   void   DrawChars(Drawable_t dst, GContext_t gc, const char *source,
                   Int_t numChars, Int_t x, Int_t y) const;

   void  Print(Option_t *option="") const;
   virtual void SavePrimitive(ostream &out, Option_t * = "");

   ClassDef(TGFont,0)   // GUI font description
};


struct FontStateMap_t;
struct XLFDAttributes_t;


class TGFontPool : public TGObject {

private:
   THashTable    *fList;       // TGFont objects pool
   THashTable    *fUidTable;   // Hash table for some used string values like family names, etc.
   THashTable    *fNamedTable; // Map a name to a set of attributes for a font

   TGFontPool(const TGFontPool& fp);             // not implemented
   TGFontPool& operator=(const TGFontPool& fp);  // not implemented

protected:
   const char *GetUid(const char *string);
   Bool_t      ParseXLFD(const char *string, XLFDAttributes_t *xa);
   TGFont     *GetFontFromAttributes(FontAttributes_t *fa, TGFont *fontPtr);
   int         FindStateNum(const FontStateMap_t *map, const char *strKey);
   const char *FindStateString(const FontStateMap_t *map, int numKey);
   Bool_t      FieldSpecified(const char *field);
   TGFont     *GetNativeFont(const char *name, Bool_t fixedDefault = kTRUE);
   TGFont     *MakeFont(TGFont *font, FontStruct_t fontStruct, const char *fontName);

public:
   TGFontPool(TGClient *client);
   virtual ~TGFontPool();

   TGFont  *GetFont(const char *font, Bool_t fixedDefault = kTRUE);
   TGFont  *GetFont(const TGFont *font);
   TGFont  *GetFont(FontStruct_t font);
   TGFont  *GetFont(const char *family, Int_t ptsize, Int_t weight, Int_t slant);

   void     FreeFont(const TGFont *font);

   TGFont  *FindFont(FontStruct_t font) const;
   TGFont  *FindFontByHandle(FontH_t font) const;

   char   **GetAttributeInfo(const FontAttributes_t *fa);
   void     FreeAttributeInfo(char **info);
   char   **GetFontFamilies();
   void     FreeFontFamilies(char **f);
   Bool_t   ParseFontName(const char *string, FontAttributes_t *fa);
   const char *NameOfFont(TGFont *font);

   void     Print(Option_t *option="") const;

   ClassDef(TGFontPool,0)  // Font pool
};

#endif
 TGFont.h:1
 TGFont.h:2
 TGFont.h:3
 TGFont.h:4
 TGFont.h:5
 TGFont.h:6
 TGFont.h:7
 TGFont.h:8
 TGFont.h:9
 TGFont.h:10
 TGFont.h:11
 TGFont.h:12
 TGFont.h:13
 TGFont.h:14
 TGFont.h:15
 TGFont.h:16
 TGFont.h:17
 TGFont.h:18
 TGFont.h:19
 TGFont.h:20
 TGFont.h:21
 TGFont.h:22
 TGFont.h:23
 TGFont.h:24
 TGFont.h:25
 TGFont.h:26
 TGFont.h:27
 TGFont.h:28
 TGFont.h:29
 TGFont.h:30
 TGFont.h:31
 TGFont.h:32
 TGFont.h:33
 TGFont.h:34
 TGFont.h:35
 TGFont.h:36
 TGFont.h:37
 TGFont.h:38
 TGFont.h:39
 TGFont.h:40
 TGFont.h:41
 TGFont.h:42
 TGFont.h:43
 TGFont.h:44
 TGFont.h:45
 TGFont.h:46
 TGFont.h:47
 TGFont.h:48
 TGFont.h:49
 TGFont.h:50
 TGFont.h:51
 TGFont.h:52
 TGFont.h:53
 TGFont.h:54
 TGFont.h:55
 TGFont.h:56
 TGFont.h:57
 TGFont.h:58
 TGFont.h:59
 TGFont.h:60
 TGFont.h:61
 TGFont.h:62
 TGFont.h:63
 TGFont.h:64
 TGFont.h:65
 TGFont.h:66
 TGFont.h:67
 TGFont.h:68
 TGFont.h:69
 TGFont.h:70
 TGFont.h:71
 TGFont.h:72
 TGFont.h:73
 TGFont.h:74
 TGFont.h:75
 TGFont.h:76
 TGFont.h:77
 TGFont.h:78
 TGFont.h:79
 TGFont.h:80
 TGFont.h:81
 TGFont.h:82
 TGFont.h:83
 TGFont.h:84
 TGFont.h:85
 TGFont.h:86
 TGFont.h:87
 TGFont.h:88
 TGFont.h:89
 TGFont.h:90
 TGFont.h:91
 TGFont.h:92
 TGFont.h:93
 TGFont.h:94
 TGFont.h:95
 TGFont.h:96
 TGFont.h:97
 TGFont.h:98
 TGFont.h:99
 TGFont.h:100
 TGFont.h:101
 TGFont.h:102
 TGFont.h:103
 TGFont.h:104
 TGFont.h:105
 TGFont.h:106
 TGFont.h:107
 TGFont.h:108
 TGFont.h:109
 TGFont.h:110
 TGFont.h:111
 TGFont.h:112
 TGFont.h:113
 TGFont.h:114
 TGFont.h:115
 TGFont.h:116
 TGFont.h:117
 TGFont.h:118
 TGFont.h:119
 TGFont.h:120
 TGFont.h:121
 TGFont.h:122
 TGFont.h:123
 TGFont.h:124
 TGFont.h:125
 TGFont.h:126
 TGFont.h:127
 TGFont.h:128
 TGFont.h:129
 TGFont.h:130
 TGFont.h:131
 TGFont.h:132
 TGFont.h:133
 TGFont.h:134
 TGFont.h:135
 TGFont.h:136
 TGFont.h:137
 TGFont.h:138
 TGFont.h:139
 TGFont.h:140
 TGFont.h:141
 TGFont.h:142
 TGFont.h:143
 TGFont.h:144
 TGFont.h:145
 TGFont.h:146
 TGFont.h:147
 TGFont.h:148
 TGFont.h:149
 TGFont.h:150
 TGFont.h:151
 TGFont.h:152
 TGFont.h:153
 TGFont.h:154
 TGFont.h:155
 TGFont.h:156
 TGFont.h:157
 TGFont.h:158
 TGFont.h:159
 TGFont.h:160
 TGFont.h:161
 TGFont.h:162
 TGFont.h:163
 TGFont.h:164
 TGFont.h:165
 TGFont.h:166
 TGFont.h:167
 TGFont.h:168
 TGFont.h:169
 TGFont.h:170
 TGFont.h:171
 TGFont.h:172
 TGFont.h:173
 TGFont.h:174
 TGFont.h:175
 TGFont.h:176
 TGFont.h:177
 TGFont.h:178
 TGFont.h:179
 TGFont.h:180
 TGFont.h:181
 TGFont.h:182
 TGFont.h:183
 TGFont.h:184
 TGFont.h:185
 TGFont.h:186
 TGFont.h:187
 TGFont.h:188
 TGFont.h:189
 TGFont.h:190
 TGFont.h:191
 TGFont.h:192
 TGFont.h:193
 TGFont.h:194
 TGFont.h:195
 TGFont.h:196
 TGFont.h:197
 TGFont.h:198
 TGFont.h:199
 TGFont.h:200
 TGFont.h:201
 TGFont.h:202
 TGFont.h:203
 TGFont.h:204
 TGFont.h:205
 TGFont.h:206
 TGFont.h:207
 TGFont.h:208
 TGFont.h:209
 TGFont.h:210
 TGFont.h:211
 TGFont.h:212
 TGFont.h:213
 TGFont.h:214
 TGFont.h:215
 TGFont.h:216
 TGFont.h:217
 TGFont.h:218
 TGFont.h:219
 TGFont.h:220
 TGFont.h:221
 TGFont.h:222
 TGFont.h:223
 TGFont.h:224
 TGFont.h:225
 TGFont.h:226
 TGFont.h:227
 TGFont.h:228
 TGFont.h:229
 TGFont.h:230
 TGFont.h:231
 TGFont.h:232
 TGFont.h:233
 TGFont.h:234
 TGFont.h:235
 TGFont.h:236
 TGFont.h:237
 TGFont.h:238
 TGFont.h:239
 TGFont.h:240
 TGFont.h:241
 TGFont.h:242
 TGFont.h:243
 TGFont.h:244
 TGFont.h:245
 TGFont.h:246
 TGFont.h:247
 TGFont.h:248
 TGFont.h:249
 TGFont.h:250
 TGFont.h:251
 TGFont.h:252
 TGFont.h:253
 TGFont.h:254
 TGFont.h:255
 TGFont.h:256
 TGFont.h:257
 TGFont.h:258
 TGFont.h:259
 TGFont.h:260
 TGFont.h:261
 TGFont.h:262
 TGFont.h:263
 TGFont.h:264
 TGFont.h:265
 TGFont.h:266
 TGFont.h:267
 TGFont.h:268
 TGFont.h:269
 TGFont.h:270
 TGFont.h:271
 TGFont.h:272
 TGFont.h:273
 TGFont.h:274
 TGFont.h:275
 TGFont.h:276
 TGFont.h:277
 TGFont.h:278