TGFont.h

Go to the documentation of this file.
00001 // @(#)root/gui:$Id: TGFont.h 35912 2010-09-30 13:43:06Z couet $
00002 // Author: Fons Rademakers   20/5/2003
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2003, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TGFont
00013 #define ROOT_TGFont
00014 
00015 
00016 //////////////////////////////////////////////////////////////////////////
00017 //                                                                      //
00018 // TGFont and TGFontPool                                                //
00019 //                                                                      //
00020 // Encapsulate fonts used in the GUI system.                            //
00021 // TGFontPool provides a pool of fonts.                                 //
00022 //                                                                      //
00023 //////////////////////////////////////////////////////////////////////////
00024 
00025 #ifndef ROOT_TNamed
00026 #include "TNamed.h"
00027 #endif
00028 #ifndef ROOT_TGObject
00029 #include "TGObject.h"
00030 #endif
00031 #ifndef ROOT_TRefCnt
00032 #include "TRefCnt.h"
00033 #endif
00034 
00035 class THashTable;
00036 class TObjString;
00037 class TGFont;
00038 
00039 // Flags passed to TGFont::MeasureChars and TGFont::ComputeTextLayout
00040 
00041 enum ETextLayoutFlags {
00042    kTextWholeWords = BIT(0),
00043    kTextAtLeastOne = BIT(1),
00044    kTextPartialOK  = BIT(2),
00045    kTextIgnoreTabs = BIT(3),
00046    kTextIgnoreNewlines = BIT(4)
00047 };
00048 
00049 enum EFontWeight {
00050    kFontWeightNormal = 0,
00051    kFontWeightMedium = 0,
00052    kFontWeightBold = 1,
00053    kFontWeightLight = 2,
00054    kFontWeightDemibold = 3,
00055    kFontWeightBlack = 4,
00056    kFontWeightUnknown = -1
00057 };
00058 
00059 enum EFontSlant {
00060    kFontSlantRoman = 0,
00061    kFontSlantItalic = 1,
00062    kFontSlantOblique = 2,
00063    kFontSlantUnknown = -1
00064 };
00065 
00066 
00067 struct FontMetrics_t {
00068    Int_t   fAscent;          // from baseline to top of font
00069    Int_t   fDescent;         // from baseline to bottom of font
00070    Int_t   fLinespace;       // the sum of the ascent and descent
00071    Int_t   fMaxWidth;        // width of widest character in font
00072    Bool_t  fFixed;           // true if monospace, false otherwise
00073 };
00074 
00075 
00076 struct FontAttributes_t {
00077 
00078    const char *fFamily; // Font family. The most important field.
00079    Int_t fPointsize;    // Pointsize of font, 0 for default size, or negative number meaning pixel size.
00080    Int_t fWeight;       // Weight flag; see below for def'n.
00081    Int_t fSlant;        // Slant flag; see below for def'n.
00082    Int_t fUnderline;    // Non-zero for underline font.
00083    Int_t fOverstrike;   // Non-zero for overstrike font.
00084 
00085    FontAttributes_t():  // default constructor
00086       fFamily    (0),
00087       fPointsize (0),
00088       fWeight    (kFontWeightNormal),
00089       fSlant     (kFontSlantRoman),
00090       fUnderline (0),
00091       fOverstrike(0) { }
00092 
00093    FontAttributes_t(const FontAttributes_t& f): // copy constructor
00094       fFamily    (f.fFamily),
00095       fPointsize (f.fPointsize),
00096       fWeight    (f.fWeight),
00097       fSlant     (f.fSlant),
00098       fUnderline (f.fUnderline),
00099       fOverstrike(f.fOverstrike) { }
00100 
00101    FontAttributes_t& operator=(const FontAttributes_t& f) // assignment operator
00102    {
00103       fFamily     = f.fFamily;
00104       fPointsize  = f.fPointsize;
00105       fWeight     = f.fWeight;
00106       fSlant      = f.fSlant;
00107       fUnderline  = f.fUnderline;
00108       fOverstrike = f.fOverstrike;
00109       return *this;
00110    }
00111 
00112 };
00113 
00114 
00115 
00116 struct LayoutChunk_t;
00117 
00118 
00119 class TGTextLayout : public TObject {
00120 
00121 friend class TGFont;
00122 
00123 protected:
00124    const TGFont  *fFont;                   // The font used when laying out the text.
00125    const char    *fString;            // The string that was layed out.
00126    Int_t          fWidth;                  // The maximum width of all lines in the text layout.
00127    Int_t          fNumChunks;      // Number of chunks actually used in following array.
00128    LayoutChunk_t *fChunks;       // Array of chunks. The actual size will be maxChunks.
00129 
00130    TGTextLayout(const TGTextLayout &tlayout);     // not implemented
00131    void operator=(const TGTextLayout &tlayout);   // not implemented
00132 
00133 public:
00134    TGTextLayout(): fFont(NULL), fString(""), fWidth(0), fNumChunks(0), fChunks(NULL) {}
00135    virtual ~TGTextLayout();
00136 
00137    void   DrawText(Drawable_t dst, GContext_t gc, Int_t x, Int_t y,
00138                    Int_t firstChar, Int_t lastChar) const;
00139    void   UnderlineChar(Drawable_t dst, GContext_t gc,
00140                         Int_t x, Int_t y, Int_t underline) const;
00141    Int_t  PointToChar(Int_t x, Int_t y) const;
00142    Int_t  CharBbox(Int_t index, Int_t *x, Int_t *y, Int_t *w, Int_t *h) const;
00143    Int_t  DistanceToText(Int_t x, Int_t y) const;
00144    Int_t  IntersectText(Int_t x, Int_t y, Int_t w, Int_t h) const;
00145    void   ToPostscript(TString *dst) const;
00146 
00147    ClassDef(TGTextLayout,0)   // Keep track of string  measurement information.
00148 };
00149 
00150 
00151 // The following class is used to keep track of the generic information about a font.
00152 
00153 class TGFont : public TNamed, public TRefCnt {
00154 
00155 friend class TGFontPool;
00156 friend class TGTextLayout;
00157 
00158 private:
00159    FontStruct_t     fFontStruct;      // Low level graphics fontstruct
00160    FontH_t          fFontH;           // Font handle (derived from fontstruct)
00161    FontMetrics_t    fFM;              // Cached font metrics
00162    FontAttributes_t fFA;              // Actual font attributes obtained when the font was created
00163    TObjString      *fNamedHash;       // Pointer to the named object TGFont was based on
00164    Int_t            fTabWidth;        // Width of tabs in this font (pixels).
00165    Int_t            fUnderlinePos;    // Offset from baseline to origin of underline bar
00166                                       // (used for drawing underlines on a non-underlined font).
00167    Int_t            fUnderlineHeight; // Height of underline bar (used for drawing
00168                                       // underlines on a non-underlined font).
00169    char             fTypes[256];      // Array giving types of all characters in
00170                                       // the font, used when displaying control characters.
00171    Int_t            fWidths[256];     // Array giving widths of all possible characters in the font.
00172    Int_t            fBarHeight;       // Height of underline or overstrike bar
00173                                       // (used for simulating a native underlined or strikeout font).
00174 
00175 protected:
00176    TGFont(const char *name)
00177      : TNamed(name,""), TRefCnt(), fFontStruct(0), fFontH(0), fFM(),
00178      fFA(), fNamedHash(0), fTabWidth(0), fUnderlinePos(0), fUnderlineHeight(0), fBarHeight(0)
00179    {
00180       SetRefCount(1);
00181       for (Int_t i=0; i<256; i++) {
00182          fWidths[i] = 0;
00183          fTypes[i]  = ' ';
00184       }
00185    }
00186 
00187    TGFont(const TGFont &font);           // not implemented
00188    void operator=(const TGFont &font);   // not implemented
00189 
00190    LayoutChunk_t *NewChunk(TGTextLayout *layout, int *maxPtr,
00191                            const char *start, int numChars,
00192                            int curX, int newX, int y) const;
00193 public:
00194    virtual ~TGFont();
00195 
00196    FontH_t      GetFontHandle() const { return fFontH; }
00197    FontStruct_t GetFontStruct() const { return fFontStruct; }
00198    FontStruct_t operator()() const;
00199    void         GetFontMetrics(FontMetrics_t *m) const;
00200    FontAttributes_t GetFontAttributes() const { return fFA; }
00201 
00202    Int_t  PostscriptFontName(TString *dst) const;
00203    Int_t  TextWidth(const char *string, Int_t numChars = -1) const;
00204    Int_t  XTextWidth(const char *string, Int_t numChars = -1) const;
00205    Int_t  TextHeight() const { return fFM.fLinespace; }
00206    void   UnderlineChars(Drawable_t dst, GContext_t gc,
00207                         const char *string, Int_t x, Int_t y,
00208                         Int_t firstChar, Int_t lastChar) const;
00209    TGTextLayout *ComputeTextLayout(const char *string, Int_t numChars,
00210                                   Int_t wrapLength, Int_t justify, Int_t flags,
00211                                   UInt_t *width, UInt_t *height) const;
00212    Int_t  MeasureChars(const char *source, Int_t numChars, Int_t maxLength,
00213                       Int_t flags, Int_t *length) const;
00214    void   DrawCharsExp(Drawable_t dst, GContext_t gc, const char *source,
00215                       Int_t numChars, Int_t x, Int_t y) const;
00216    void   DrawChars(Drawable_t dst, GContext_t gc, const char *source,
00217                    Int_t numChars, Int_t x, Int_t y) const;
00218 
00219    void  Print(Option_t *option="") const;
00220    virtual void SavePrimitive(ostream &out, Option_t * = "");
00221 
00222    ClassDef(TGFont,0)   // GUI font description
00223 };
00224 
00225 
00226 struct FontStateMap_t;
00227 struct XLFDAttributes_t;
00228 
00229 
00230 class TGFontPool : public TGObject {
00231 
00232 private:
00233    THashTable    *fList;       // TGFont objects pool
00234    THashTable    *fUidTable;   // Hash table for some used string values like family names, etc.
00235    THashTable    *fNamedTable; // Map a name to a set of attributes for a font
00236 
00237    TGFontPool(const TGFontPool& fp);             // not implemented
00238    TGFontPool& operator=(const TGFontPool& fp);  // not implemented
00239 
00240 protected:
00241    const char *GetUid(const char *string);
00242    Bool_t      ParseXLFD(const char *string, XLFDAttributes_t *xa);
00243    TGFont     *GetFontFromAttributes(FontAttributes_t *fa, TGFont *fontPtr);
00244    int         FindStateNum(const FontStateMap_t *map, const char *strKey);
00245    const char *FindStateString(const FontStateMap_t *map, int numKey);
00246    Bool_t      FieldSpecified(const char *field);
00247    TGFont     *GetNativeFont(const char *name, Bool_t fixedDefault = kTRUE);
00248    TGFont     *MakeFont(TGFont *font, FontStruct_t fontStruct, const char *fontName);
00249 
00250 public:
00251    TGFontPool(TGClient *client);
00252    virtual ~TGFontPool();
00253 
00254    TGFont  *GetFont(const char *font, Bool_t fixedDefault = kTRUE);
00255    TGFont  *GetFont(const TGFont *font);
00256    TGFont  *GetFont(FontStruct_t font);
00257    TGFont  *GetFont(const char *family, Int_t ptsize, Int_t weight, Int_t slant);
00258 
00259    void     FreeFont(const TGFont *font);
00260 
00261    TGFont  *FindFont(FontStruct_t font) const;
00262    TGFont  *FindFontByHandle(FontH_t font) const;
00263 
00264    char   **GetAttributeInfo(const FontAttributes_t *fa);
00265    void     FreeAttributeInfo(char **info);
00266    char   **GetFontFamilies();
00267    void     FreeFontFamilies(char **f);
00268    Bool_t   ParseFontName(const char *string, FontAttributes_t *fa);
00269    const char *NameOfFont(TGFont *font);
00270 
00271    void     Print(Option_t *option="") const;
00272 
00273    ClassDef(TGFontPool,0)  // Font pool
00274 };
00275 
00276 #endif

Generated on Tue Jul 5 14:20:00 2011 for ROOT_528-00b_version by  doxygen 1.5.1