Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

GFont.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 #ifndef _GDCFONT_H_
00005 #define _GDCFONT_H_
00006 
00007 #include "string.h"
00008 #include "GRect.h"
00009 #include "GProperties.h"
00010 #include "LgiOsClasses.h"
00011 
00013 // Defines
00014 
00015 #ifndef WIN32
00016 
00017 // font weights
00018 #define FW_DONTCARE                     0 
00019 #define FW_THIN                         100
00020 #define FW_EXTRALIGHT                   200
00021 #define FW_ULTRALIGHT                   200
00022 #define FW_LIGHT                        300
00023 
00024 #define FW_NORMAL                       400
00025 #define FW_REGULAR                      400
00026 #define FW_MEDIUM                       500
00027 #define FW_SEMIBOLD                     600
00028 #define FW_DEMIBOLD                     600
00029 
00030 #define FW_BOLD                         700
00031 #define FW_EXTRABOLD                    800
00032 #define FW_ULTRABOLD                    800
00033 #define FW_HEAVY                        900
00034 #define FW_BLACK                        900
00035 
00036 // quality
00038 #define DEFAULT_QUALITY                 0
00039 
00040 #define ANTIALIASED_QUALITY             1
00041 
00042 #define NONANTIALIASED_QUALITY          2
00043 
00044 #elif defined WIN32
00045 
00046 #define WESTEUROPE_CHARSET              BALTIC_CHARSET      // ??? don't know
00047 
00048 #endif
00049 
00050 // OS specific font type
00051 #if defined WIN32
00052 
00053     typedef HFONT OsFont;
00054     #define PrevOsChar(Ptr) Ptr--
00055     #define NextOsChar(Ptr) Ptr++
00056 
00057 #elif defined BEOS
00058 
00059     typedef BFont *OsFont;
00060     #define PrevOsChar(Ptr) LgiPrevUtf8((char*&)Ptr)
00061     #define NextOsChar(Ptr) LgiNextUtf8((char*&)Ptr)
00062 
00063 #elif defined POSIX
00064 
00065     #include "LgiOsClasses.h"
00066     #define PrevOsChar(Ptr) Ptr--
00067     #define NextOsChar(Ptr) Ptr++
00068 
00069 #endif
00070 
00071 #define MAX_UNICODE                     0xffff // maximum unicode char I can handle
00072 #define _HasUnicodeGlyph(map, u)        ( (map[(u)>>3] & (1 << ((u) & 7))) != 0  )
00073 
00075 // Functions
00076 #define IsUtf8_1Byte(c)     (((c) & 0x80) == 0)
00077 #define IsUtf8_2Byte(c)     (((c) & 0xe0) == 0xc0)
00078 #define IsUtf8_3Byte(c)     (((c) & 0xf0) == 0xe0)
00079 #define IsUtf8_4Byte(c)     (((c) & 0xf8) == 0xf0)
00080 
00081 #define IsUtf8_Lead(c)      ( ((c) & 0xc0) == 0xc0 )
00082 #define IsUtf8_Trail(c)     ( ((c) & 0xc0) == 0x80 )
00083 
00085 inline char16 LgiUtf8To32(uint8 *&i, int &Len)
00086 {
00087     uint32 Out = 0;
00088 
00089     // #define InvalidUtf()     { Len--; i++; return '?'; }
00090     #define InvalidUtf()        ;
00091 
00092     if (Len > 0)
00093     {
00094         if (NOT *i)
00095         {
00096             Len = 0;
00097             return 0;
00098         }
00099 
00100         if (IsUtf8_1Byte(*i))
00101         {
00102             // 1 byte UTF-8
00103             Len--;
00104             return *i++;
00105         }
00106         else if (IsUtf8_2Byte(*i))
00107         {
00108             // 2 byte UTF-8
00109             if (Len > 1)
00110             {
00111                 Out = ((int)(*i++ & 0x1f)) << 6;
00112                 Len--;
00113 
00114                 if (IsUtf8_Trail(*i))
00115                 {
00116                     Out |= *i++ & 0x3f;
00117                     Len--;
00118                 }
00119                 else InvalidUtf()
00120             }
00121         }
00122         else if (IsUtf8_3Byte(*i))
00123         {
00124             // 3 byte UTF-8
00125             if (Len > 2)
00126             {
00127                 Out = ((int)(*i++ & 0x0f)) << 12;
00128                 Len--;
00129 
00130                 if (IsUtf8_Trail(*i))
00131                 {
00132                     Out |= ((int)(*i++ & 0x3f)) << 6;
00133                     Len--;
00134 
00135                     if (IsUtf8_Trail(*i))
00136                     {
00137                         Out |= *i++ & 0x3f;
00138                         Len--;
00139                     }
00140                     else InvalidUtf()
00141                 }
00142                 else InvalidUtf()
00143             }
00144         }
00145         else if (IsUtf8_4Byte(*i))
00146         {
00147             // 4 byte UTF-8
00148             if (Len > 3)
00149             {
00150                 Out = ((int)(*i++ & 0x07)) << 18;
00151                 Len--;
00152 
00153                 if (IsUtf8_Trail(*i))
00154                 {
00155                     Out |= ((int)(*i++ & 0x3f)) << 12;
00156                     Len--;
00157 
00158                     if (IsUtf8_Trail(*i))
00159                     {
00160                         Out |= ((int)(*i++ & 0x3f)) << 6;
00161                         Len--;
00162 
00163                         if (IsUtf8_Trail(*i))
00164                         {
00165                             Out |= *i++ & 0x3f;
00166                             Len--;
00167                         }
00168                         else InvalidUtf()
00169                     }
00170                     else InvalidUtf()
00171                 }
00172                 else InvalidUtf()
00173             }
00174         }
00175         else
00176             InvalidUtf()
00177     }
00178 
00179     return Out;
00180 }
00181 
00183 inline void LgiUtf32To8(uint32 c, uint8 *&i, int &Len)
00184 {
00185     if ((c & ~0x7f) == 0)
00186     {
00187         if (Len > 0)
00188         {
00189             *i++ = c;
00190             Len--;
00191         }
00192     }
00193     else if ((c & ~0x7ff) == 0)
00194     {
00195         if (Len > 1)
00196         {
00197             *i++ = 0xc0 | (c >> 6);
00198             *i++ = 0x80 | (c & 0x3f);
00199             Len -= 2;
00200         }
00201     }
00202     else if ((c & 0xffff0000) == 0)
00203     {
00204         if (Len > 2)
00205         {
00206             *i++ = 0xe0 | (c >> 12);
00207             *i++ = 0x80 | ((c & 0x0fc0) >> 6);
00208             *i++ = 0x80 | (c & 0x3f);
00209             Len -= 3;
00210         }
00211     }
00212     else
00213     {
00214         if (Len > 3)
00215         {
00216             *i++ = 0xf0 | (c >> 18);
00217             *i++ = 0x80 | ((c&0x3f000) >> 12);
00218             *i++ = 0x80 | ((c&0xfc0) >> 6);
00219             *i++ = 0x80 | (c&0x3f);
00220             Len -= 4;
00221         }
00222     }
00223 }
00224 
00226 inline uint32 LgiUtf16To32(char16 *&i, int &Len)
00227 {
00228     if (Len > 1)
00229     {
00230         if (NOT *i)
00231         {
00232             Len = 0;
00233             return 0;
00234         }
00235 
00236         int n = *i & 0xfc00;
00237         if (n == 0xd800 || n == 0xdc00)
00238         {
00239             // 2 word UTF
00240             if (Len > 3)
00241             {
00242                 Len -= sizeof(char16)<<1;
00243                 int w = (*i & 0x3c0) >> 6;
00244                 int zy = *i++ & 0x3f;
00245                 return ((w + 1) << 16) | (zy << 10) | (*i++ & 0x3ff);
00246             }
00247         }
00248 
00249         // 1 word UTF
00250         Len -= sizeof(char16);
00251         return *i++;
00252     }
00253 
00254     return 0;
00255 }
00256 
00258 inline void LgiUtf32To16(uint32 c, char16 *&i, int &Len)
00259 {
00260     if (c & 0xffff0000)
00261     {
00262         // 2 word UTF
00263         if (Len > 3)
00264         {
00265             int w = (c >> 16) - 1;
00266             *i++ = 0xd800 | (w << 6) | ((c & 0xfc00) >> 10);
00267             *i++ = 0xdc00 | (c & 0x3ff);
00268             Len -= sizeof(char16) << 1;
00269         }
00270     }
00271 
00272     // 1 word UTF
00273     if (Len > 1)
00274     {
00275         *i++ = c;
00276         Len -= sizeof(char16);
00277     }
00278 }
00279 
00281 inline void LgiNextUtf8(char *&p)
00282 {
00283     if (IsUtf8_Lead(*p++))
00284     {
00285         while (IsUtf8_Trail(*p)) p++;
00286     }
00287 }
00288 
00290 inline void LgiPrevUtf8(char *&p)
00291 {
00292     p--;
00293     while (IsUtf8_Trail(*p)) p--;
00294 }
00295 
00297 // Classes
00298 class GFontType;
00299 class GDisplayString;
00300 
00302 class LgiClass GTypeFace
00303 {
00304 protected:
00305     class GTypeFacePrivate *d;
00306 
00307     // Methods
00308     virtual void _OnPropChange(bool c) {} // if false then it's just a property change
00309 
00310 public:
00311     GTypeFace();
00312     virtual ~GTypeFace();
00313 
00315     void Face(char *s);
00317     void PointSize(int i);
00319     void TabSize(int i);
00321     void Quality(int i);
00323     void Fore(COLOUR c);
00328     void Back(COLOUR c);
00330     void SetWeight(int Weight);
00332     void Bold(bool i) { SetWeight(i ? FW_BOLD : FW_NORMAL); }
00334     void Italic(bool i);
00336     void Underline(bool i);
00338     void Transparent(bool i);
00340     void SubGlyphs(bool i);
00341 
00343     char *Face();
00345     int PointSize();
00347     int TabSize();
00349     int Quality();
00351     COLOUR Fore();
00353     COLOUR Back();
00355     int GetWeight();
00357     bool Bold() { return GetWeight() >= FW_BOLD; }
00359     bool Italic();
00361     bool Underline();
00363     bool Transparent();
00365     bool SubGlyphs();
00367     double Ascent();
00369     double Descent();
00370 
00371     /* Deprecated
00372     void Width(int i);
00373     int Width();
00374     char *CodePage();
00375     void CodePage(char *i);
00376     */
00377 
00380     virtual void Colour(COLOUR Fore, COLOUR Back = 0xFFFFFFFF);
00381 };
00382 
00421 class LgiClass GFont :
00422     public GTypeFace
00423 {
00424     friend class GFontSystem;
00425     friend class GDisplayString;
00426 
00427     class GFontPrivate *d;
00428 
00429     // Methods
00430     bool IsValid();
00431     void _OnPropChange(bool Change);
00432     char16 *_ToUnicode(char *In, int &Len);
00433     bool GetOwnerUnderline();
00434     uchar *GetGlyphMap();
00435 
00437     void _Measure
00438     (
00440         int &x,
00442         int &y,
00444         OsChar *Str,
00446         int Len
00447     );
00450     int _CharAt
00451     (
00453         int x,
00455         OsChar *Str,
00457         int Len
00458     );
00460     void _Draw
00461     (
00463         GSurface *pDC,
00465         int x,
00467         int y,
00469         OsChar *Str,
00471         int Len,
00473         GRect *r
00474     );
00475 
00476 public:
00478     GFont
00479     (
00481         char *face = 0,
00483         int point = -1
00484     );
00486     GFont(OsFont Handle);
00488     GFont(GFontType &Type);
00490     GFont(GFont &Fnt);
00491     ~GFont();
00492 
00494     bool Create
00495     (
00497         char *Face = 0,
00499         int PtSize = -1,
00502         int Param = 0
00503     );
00505     bool Create(GFontType *Type, int Param = 0);
00507     OsFont Handle();
00509     GFont &operator =(GFont &f);
00511     int GetHeight();
00513     int GetParam();
00514 
00516     void Text
00517     (
00519         GSurface *pDC,
00521         int x,
00523         int y,
00525         char *Str,
00527         int Len = -1,
00529         GRect *r = 0,
00531         int TabOrigin = 0
00532     );
00534     void Size
00535     (
00537         int *x,
00539         int *y,
00541         char *Str,
00543         int Len = -1,
00545         int Flags = 0
00546     );
00548     int X(char *Str, int Len = -1);
00550     int Y(char *Str, int Len = -1);
00552     int CharAt
00553     (
00555         int x,
00557         char *Str,
00559         int Len = -1,
00561         int TabOffset = 0
00562     );
00563 
00565     void TextW
00566     (
00568         GSurface *pDC,
00570         int x,
00572         int y,
00574         char16 *Str,
00576         int Len = -1,
00578         GRect *r = 0,
00580         int TabOrigin = 0
00581     );
00583     void SizeW
00584     (
00586         int *x,
00588         int *y,
00590         char16 *Str,
00592         int Len = -1,
00594         int Flags = 0
00595     );
00597     int XW(char16 *Str, int Len = -1);
00599     int YW(char16 *Str, int Len = -1);
00601     int CharAtW
00602     (
00604         int x,
00606         char16 *Str,
00608         int Len = -1,
00610         int TabOffset = 0
00611     );
00612 };
00613 
00615 class LgiClass GFontType
00616 {
00617     friend class GFont;
00618     friend class GTypeFace;
00619 
00620 protected:
00621     #if defined WIN32
00622     LOGFONT Info;
00623     #else
00624     GTypeFace Info;
00625     #endif
00626 
00627 public:
00628     GFontType(char *face = 0, int pointsize = 0);
00629     virtual ~GFontType();
00630 
00631     #ifdef WIN32
00632     LOGFONT *Handle() { return &Info; }
00633     #else
00634     GTypeFace *Handle() { return &Info; }
00635     #endif
00636 
00638     char *GetFace();
00639     
00641     void SetFace(char *Face);
00642     
00644     int GetPointSize();
00645     
00647     void SetPointSize(int PointSize);
00648 
00650     bool DoUI(GView *Parent);
00651     
00653     bool GetDescription(char *Str);
00654     
00656     bool Serialize(ObjProperties *Options, char *OptName, bool Write);
00657 
00659     bool GetConfigFont(char *Tag);
00660     
00662     bool GetSystemFont(char *Which);
00663 
00665     bool GetFromRef(OsFont Handle);
00666 
00668     virtual GFont *Create(int Param = 0);
00669 };
00670 
00672 enum GCharSetType
00673 {
00674     CpNone,
00675     CpMapped,
00676     CpUtf8,
00677     CpWide,
00678     CpIconv,
00679     CpWindowsDb
00680 };
00681 
00683 class LgiClass GCharset
00684 {
00685 public:
00687     char *Charset;
00689     char *Description;
00691     short *UnicodeMap;
00693     char *IconvName;
00695     char *AlternateNames;
00697     GCharSetType Type;
00698 
00700     GCharset(char *cp = 0, char *des = 0, short *map = 0, char *alt = 0);
00701 
00703     bool IsUnicode();
00705     char *GetIconvName();
00707     bool IsAvailable();
00708 };
00709 
00724 class LgiClass GDisplayString
00725 {
00726     OsChar *Str;
00727     GFont *Font;
00728     int x, y, len, TabOrigin;
00729     int Blocks;
00730     class CharInfo *Info;
00731     uint8 Flags;
00732 
00733     void Layout();
00734 
00735 public:
00737     GDisplayString
00738     (
00740         GFont *f,
00742         char *s,
00744         int l = -1,
00745         int tabOrigin = 0
00746     );
00748     GDisplayString
00749     (
00751         GFont *f,
00753         char16 *s,
00755         int l = -1,
00756         int tabOrigin = 0
00757     );
00758     virtual ~GDisplayString();
00759     
00762     bool ShowVisibleTab();
00765     void ShowVisibleTab(bool i);
00766     GFont *GetFont() { return Font; };
00767 
00769     void TruncateWithDots
00770     (
00772         int Width
00773     );
00775     bool IsTruncated();
00776 
00778     int Length();
00780     void Length(int NewLen);
00781 
00783     operator OsChar*() { return Str; }
00784 
00786     int X();
00788     int Y();
00790     void Size(int *x, int *y);
00792     int CharAt(int x);
00793 
00795     void Draw
00796     (
00798         GSurface *pDC,
00800         int x,
00802         int y,
00804         GRect *r = 0
00805     );
00806 };
00807 
00809 class LgiClass GCharsetSystem
00810 {
00811     // Charset info
00812     GHashTable *Charsets;
00813 
00814 public:
00815     GCharsetSystem();
00816     ~GCharsetSystem();
00817 
00818     // Get the charset info
00819     GCharset *GetCsInfo(char *Cp);
00820     GCharset *GetCsList();
00821 };
00822 
00824 LgiFunc GCharset *LgiGetCsInfo(char *Cs);
00827 LgiFunc GCharset *LgiGetCsList();
00828 
00830 class LgiClass GFontSystem
00831 {
00832     friend class GApp;
00833     friend class GDisplayString;
00834 
00835     static GFontSystem *Me;
00836 
00837 private:
00838     // System Font List
00839     List<char> AllFonts;
00840     List<char> SubFonts; // Fonts yet to be scanned for substitution
00841 
00842     // Missing Glyph Substitution
00843     uchar Lut[MAX_UNICODE+1];
00844     GFont *Font[256];
00845     class GFontSystemPrivate *d;
00846 
00847 public:
00849     static GFontSystem *Inst();
00850 
00851     // Object
00852     GFontSystem();
00853     ~GFontSystem();
00854 
00856     bool EnumerateFonts(List<char> &Fonts);
00857 
00859     bool GetGlyphSubSupport();
00861     bool GetDefaultGlyphSub();
00863     void SetDefaultGlyphSub(bool i);
00865     GFont *GetGlyph
00866     (
00868         int u,
00870         GFont *UserFont
00871     );
00875     GFont *GetBestFont(char *Str);
00876 };
00877 
00878 #endif

Generated on Tue May 2 10:24:41 2006 for Lgi by  doxygen 1.4.1