Support turbo2.
[vuplus_dvbapp] / lib / gdi / font.h
1 #ifndef __FONT_H
2 #define __FONT_H
3
4 #ifndef SWIG
5
6 #include <ft2build.h>
7 #include FT_FREETYPE_H
8 #include FT_CACHE_H
9 #include FT_CACHE_IMAGE_H
10 #include FT_CACHE_SMALL_BITMAPS_H
11 #include FT_STROKER_H
12 typedef FTC_ImageCache FTC_Image_Cache;
13 typedef FTC_ImageTypeRec FTC_Image_Desc;
14 typedef FTC_SBitCache FTC_SBit_Cache;
15 #include <vector>
16 #include <list>
17
18 #include <lib/gdi/fb.h>
19 #include <lib/gdi/esize.h>
20 #include <lib/gdi/epoint.h>
21 #include <lib/gdi/erect.h>
22 #include <string>
23 #include <lib/base/object.h> 
24
25 #include <set>
26
27 class FontRenderClass;
28 class Font;
29 class gDC;
30 class gFont;
31 class gRGB;
32
33 #endif
34 class fontRenderClass
35
36 #ifndef SWIG
37         friend class Font;
38         friend class eTextPara;
39         fbClass *fb;
40         struct fontListEntry
41         {
42                 std::string filename, face;
43                 int scale; // 100 is 1:1
44                 fontListEntry *next;
45                 ~fontListEntry();
46         } *font;
47
48         FT_Library library;
49         FTC_Manager                     cacheManager;                           /* the cache manager                                                     */
50         FTC_Image_Cache imageCache;                                     /* the glyph image cache                                         */
51         FTC_SBit_Cache   sbitsCache;                            /* the glyph small bitmaps cache         */
52         FT_Stroker stroker;
53         int strokerRadius;
54
55         FTC_FaceID getFaceID(const std::string &face);
56         FT_Error getGlyphBitmap(FTC_Image_Desc *font, FT_ULong glyph_index, FTC_SBit *sbit);
57         FT_Error getGlyphImage(FTC_Image_Desc *font, FT_ULong glyph_index, FT_Glyph *glyph, FT_Glyph *borderglyph, int bordersize);
58         static fontRenderClass *instance;
59 #else
60         fontRenderClass();
61         ~fontRenderClass();
62 #endif
63 public:
64         float getLineHeight(const gFont& font);
65         static fontRenderClass *getInstance();
66 #ifndef SWIG
67         std::string AddFont(const std::string &filename, const std::string &name, int scale);
68         FT_Error FTC_Face_Requester(FTC_FaceID  face_id, FT_Face* aface);
69         int getFont(ePtr<Font> &font, const std::string &face, int size, int tabwidth=-1);
70         fontRenderClass();
71         ~fontRenderClass();
72 #endif
73 };
74
75 #ifndef SWIG
76
77 #define RS_WRAP         1
78 #define RS_DOT          2
79 #define RS_DIRECT       4
80 #define RS_FADE         8
81
82 #define GS_ISSPACE  1
83 #define GS_ISFIRST  2
84 #define GS_USED                 4
85 #define GS_INVERT   8
86 #define GS_SOFTHYPHEN 16
87 #define GS_HYPHEN   32
88 #define GS_COLORCHANGE 64
89 #define GS_CANBREAK (GS_ISSPACE|GS_SOFTHYPHEN|GS_HYPHEN)
90
91 struct pGlyph
92 {
93         int x, y, w;
94         unsigned long newcolor;
95         ePtr<Font> font;
96         FT_ULong glyph_index;
97         int flags;
98         eRect bbox;
99         FT_Glyph image, borderimage;
100         pGlyph()
101         {
102                 image = NULL;
103                 borderimage = NULL;
104         }
105 };
106
107 typedef std::vector<pGlyph> glyphString;
108
109 class Font;
110 class eLCD;
111
112 class eTextPara: public iObject
113 {
114         DECLARE_REF(eTextPara);
115         ePtr<Font> current_font, replacement_font;
116         FT_Face current_face, replacement_face;
117         int use_kerning;
118         int previous;
119         static std::string replacement_facename;
120         static std::set<int> forced_replaces;
121
122         eRect area;
123         ePoint cursor;
124         eSize maximum;
125         int left;
126         glyphString glyphs;
127         std::list<int> lineOffsets;
128         std::list<int> lineChars;
129         int charCount;
130         bool doTopBottomReordering;
131
132         int appendGlyph(Font *current_font, FT_Face current_face, FT_UInt glyphIndex, int flags, int rflags, int border, bool activate_newcolor, unsigned long newcolor);
133         void newLine(int flags);
134         void setFont(Font *font, Font *replacement_font);
135         eRect boundBox;
136         void calc_bbox();
137         int bboxValid;
138         void clear();
139 public:
140         eTextPara(eRect area, ePoint start=ePoint(-1, -1))
141                 :current_font(0), replacement_font(0), current_face(0), replacement_face(0)
142                 ,area(area), cursor(start), maximum(0, 0), left(start.x()), charCount(0)
143                 ,doTopBottomReordering(false), bboxValid(0)
144         {
145         }
146         virtual ~eTextPara();
147         
148         static void setReplacementFont(std::string font) { replacement_facename=font; }
149         static void forceReplacementGlyph(int unicode) { forced_replaces.insert(unicode); }
150
151         void setFont(const gFont *font);
152         int renderString(const char *string, int flags=0, int border=0);
153
154
155
156         void blit(gDC &dc, const ePoint &offset, const gRGB &background, const gRGB &foreground, bool border = false);
157
158         enum
159         {
160                 dirLeft, dirRight, dirCenter, dirBlock
161         };
162
163         void realign(int dir);
164
165         const eRect & getBoundBox()
166         {
167                 if (!bboxValid)
168                         calc_bbox();
169
170                 return boundBox;
171         }
172         
173         const int size() const
174         {
175                 return glyphs.size();
176         }
177
178         const eRect& getGlyphBBox(int num) const
179         {
180                 ASSERT(num >= 0);
181                 ASSERT(num < (int)glyphs.size());
182                 return glyphs[num].bbox;
183         }
184         
185         void setGlyphFlag(int g, int f)
186         {
187                 ASSERT(g >= 0);
188                 ASSERT(g < (int)glyphs.size());
189                 glyphs[g].flags |= f;
190         }
191
192         void clearGlyphFlag(int g, int f)
193         {
194                 ASSERT(g >= 0);
195                 ASSERT(g < (int)glyphs.size());
196                 glyphs[g].flags |= f;
197         }
198 };
199
200 class Font: public iObject
201 {
202         DECLARE_REF(Font);
203 public:
204         FTC_ScalerRec scaler;
205         FTC_Image_Desc font;
206         fontRenderClass *renderer;
207         FT_Error getGlyphBitmap(FT_ULong glyph_index, FTC_SBit *sbit);
208         FT_Error getGlyphImage(FT_ULong glyph_index, FT_Glyph *glyph, FT_Glyph *borderglyph, int bordersize);
209         FT_Face face;
210         FT_Size size;
211         
212         int tabwidth;
213         int height;
214         Font(fontRenderClass *render, FTC_FaceID faceid, int isize, int tabwidth);
215         virtual ~Font();
216 };
217
218 extern fontRenderClass *font;
219
220 #endif  // !SWIG
221
222 #endif