3cdfc299c0d64f6b853970e6c235bbd3f4e0669e
[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 #ifdef HAVE_FREETYPE2
12 typedef FTC_ImageCache FTC_Image_Cache;
13 typedef FTC_ImageTypeRec FTC_Image_Desc;
14 typedef FTC_SBitCache FTC_SBit_Cache;
15 #endif
16 #include <vector>
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
53         FTC_FaceID getFaceID(const std::string &face);
54         FT_Error getGlyphBitmap(FTC_Image_Desc *font, FT_ULong glyph_index, FTC_SBit *sbit);
55         static fontRenderClass *instance;
56 #else
57         fontRenderClass();
58         ~fontRenderClass();
59 #endif
60 public:
61         float getLineHeight(const gFont& font);
62         static fontRenderClass *getInstance();
63 #ifndef SWIG
64         std::string AddFont(const std::string &filename, const std::string &name, int scale);
65         FT_Error FTC_Face_Requester(FTC_FaceID  face_id, FT_Face* aface);
66         int getFont(ePtr<Font> &font, const std::string &face, int size, int tabwidth=-1);
67         fontRenderClass();
68         ~fontRenderClass();
69 #endif
70 };
71
72 #ifndef SWIG
73
74 #define RS_WRAP         1
75 #define RS_DOT          2
76 #define RS_DIRECT       4
77 #define RS_FADE         8
78
79 #define GS_ISSPACE  1
80 #define GS_ISFIRST  2
81 #define GS_USED                 4
82 #define GS_INVERT   8
83 #define GS_SOFTHYPHEN 16
84 #define GS_HYPHEN   32
85 #define GS_CANBREAK (GS_ISSPACE|GS_SOFTHYPHEN|GS_HYPHEN)
86
87 struct pGlyph
88 {
89         int x, y, w;
90         ePtr<Font> font;
91         FT_ULong glyph_index;
92         int flags;
93         eRect bbox;
94 };
95
96 typedef std::vector<pGlyph> glyphString;
97
98 class Font;
99 class eLCD;
100
101 class eTextPara: public iObject
102 {
103 DECLARE_REF(eTextPara);
104 private:
105         ePtr<Font> current_font, replacement_font;
106         FT_Face current_face, replacement_face;
107         int use_kerning;
108         int previous;
109         static std::string replacement_facename;
110         static std::set<int> forced_replaces;
111
112         eRect area;
113         ePoint cursor;
114         eSize maximum;
115         int left;
116         glyphString glyphs;
117
118         int appendGlyph(Font *current_font, FT_Face current_face, FT_UInt glyphIndex, int flags, int rflags);
119         void newLine(int flags);
120         void setFont(Font *font, Font *replacement_font);
121         eRect boundBox;
122         void calc_bbox();
123         int bboxValid;
124 public:
125         eTextPara(eRect area, ePoint start=ePoint(-1, -1))
126                 : current_font(0), replacement_font(0), current_face(0), replacement_face(0),
127                         area(area), cursor(start), maximum(0, 0), left(start.x()), bboxValid(0)
128         {
129         }
130         virtual ~eTextPara();
131         
132         static void setReplacementFont(std::string font) { replacement_facename=font; }
133         static void forceReplacementGlyph(int unicode) { forced_replaces.insert(unicode); }
134
135         void setFont(const gFont *font);
136         int renderString(const char *string, int flags=0);
137
138         void clear();
139
140         void blit(gDC &dc, const ePoint &offset, const gRGB &background, const gRGB &foreground);
141
142         enum
143         {
144                 dirLeft, dirRight, dirCenter, dirBlock
145         };
146
147         void realign(int dir);
148
149         const eRect & getBoundBox()
150         {
151                 if (!bboxValid)
152                         calc_bbox();
153
154                 return boundBox;
155         }
156         
157         const int size() const
158         {
159                 return glyphs.size();
160         }
161
162         const eRect& getGlyphBBox(int num) const
163         {
164                 assert(num >= 0);
165                 assert(num < (int)glyphs.size());
166                 return glyphs[num].bbox;
167         }
168         
169         void setGlyphFlag(int g, int f)
170         {
171                 assert(g >= 0);
172                 assert(g < (int)glyphs.size());
173                 glyphs[g].flags |= f;
174         }
175
176         void clearGlyphFlag(int g, int f)
177         {
178                 assert(g >= 0);
179                 assert(g < (int)glyphs.size());
180                 glyphs[g].flags |= f;
181         }
182 };
183
184 class Font: public iObject
185 {
186 DECLARE_REF(Font);
187 public:
188 #ifdef HAVE_FREETYPE2
189         FTC_ScalerRec scaler;
190 #endif
191         FTC_Image_Desc font;
192         fontRenderClass *renderer;
193         FT_Error getGlyphBitmap(FT_ULong glyph_index, FTC_SBit *sbit);
194         FT_Face face;
195         FT_Size size;
196         
197         int tabwidth;
198         int height;
199         Font(fontRenderClass *render, FTC_FaceID faceid, int isize, int tabwidth);
200         virtual ~Font();
201 };
202
203 extern fontRenderClass *font;
204
205 #endif  // !SWIG
206
207 #endif