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