ac55c884fe335bad9fc7792dd3ca79b3c56faa32
[enigma2.git] / lib / gdi / font.h
1 #ifndef __FONT_H
2 #define __FONT_H
3
4 #include <freetype/freetype.h>
5 #include <freetype/ftcache.h>
6 #include <freetype/cache/ftcglyph.h>
7 #include <freetype/cache/ftcimage.h>
8 #include <freetype/cache/ftcmanag.h>
9 #include <freetype/cache/ftcsbits.h>
10 #include <freetype/cache/ftlru.h>
11 #include <vector>
12
13 #include <lib/gdi/fb.h>
14 #include <lib/gdi/esize.h>
15 #include <lib/gdi/epoint.h>
16 #include <lib/gdi/erect.h>
17 #include <lib/base/estring.h>
18
19 class FontRenderClass;
20 class Font;
21 class gPixmapDC;
22 class gFont;
23 class gRGB;
24
25 class fontRenderClass
26
27         friend class Font;
28         friend class eTextPara;
29         fbClass *fb;
30         struct fontListEntry
31         {
32                 eString filename, face;
33                 int scale; // 100 is 1:1
34                 fontListEntry *next;
35                 ~fontListEntry();
36         } *font;
37
38         FT_Library library;
39         FTC_Manager                     cacheManager;                           /* the cache manager                                                     */
40         FTC_Image_Cache imageCache;                                     /* the glyph image cache                                         */
41         FTC_SBit_Cache   sbitsCache;                            /* the glyph small bitmaps cache         */
42
43         FTC_FaceID getFaceID(const eString &face);
44         FT_Error getGlyphBitmap(FTC_Image_Desc *font, FT_ULong glyph_index, FTC_SBit *sbit);
45         static fontRenderClass *instance;
46 public:
47         float getLineHeight(const gFont& font);
48         eString AddFont(const eString &filename, const eString &name, int scale);
49         static fontRenderClass *getInstance();
50         FT_Error FTC_Face_Requester(FTC_FaceID  face_id,
51                                                                                                                         FT_Face*                aface);
52         Font *getFont(const eString &face, int size, int tabwidth=-1);
53         fontRenderClass();
54         ~fontRenderClass();
55 };
56
57 #define RS_WRAP         1
58 #define RS_DOT          2
59 #define RS_DIRECT       4
60 #define RS_FADE         8
61
62 #define GS_ISSPACE  1
63 #define GS_ISFIRST  2
64 #define GS_USED                 4
65
66 struct pGlyph
67 {
68         int x, y, w;
69         Font *font;
70         FT_ULong glyph_index;
71         int flags;
72         eRect bbox;
73 };
74
75 typedef std::vector<pGlyph> glyphString;
76
77 class Font;
78 class eLCD;
79
80 class eTextPara
81 {
82         Font *current_font, *replacement_font;
83         FT_Face current_face, replacement_face;
84         int use_kerning;
85         int previous;
86         static eString replacement_facename;
87
88         eRect area;
89         ePoint cursor;
90         eSize maximum;
91         int left;
92         glyphString glyphs;
93         int refcnt;
94
95         int appendGlyph(Font *current_font, FT_Face current_face, FT_UInt glyphIndex, int flags, int rflags);
96         void newLine(int flags);
97         void setFont(Font *font, Font *replacement_font);
98         eRect boundBox;
99         void calc_bbox();
100         int bboxValid;
101 public:
102         eTextPara(eRect area, ePoint start=ePoint(-1, -1))
103                 : current_font(0), replacement_font(0), current_face(0), replacement_face(0),
104                         area(area), cursor(start), maximum(0, 0), left(start.x()), refcnt(0), bboxValid(0)
105         {
106         }
107         ~eTextPara();
108         
109         static void setReplacementFont(eString font) { replacement_facename=font; }
110
111         void destroy();
112         eTextPara *grab();
113
114         void setFont(const gFont &font);
115         int renderString(const eString &string, int flags=0);
116
117         void clear();
118
119         void blit(gPixmapDC &dc, const ePoint &offset, const gRGB &background, const gRGB &foreground);
120
121         enum
122         {
123                 dirLeft, dirRight, dirCenter, dirBlock
124         };
125
126         void realign(int dir);
127
128         const eRect & getBoundBox()
129         {
130                 if (!bboxValid)
131                         calc_bbox();
132
133                 return boundBox;
134         }
135
136         const eRect& getGlyphBBox(int num) const
137         {
138                 return glyphs[num].bbox;
139         }
140 };
141
142 class Font
143 {
144 public:
145         FTC_Image_Desc font;
146         fontRenderClass *renderer;
147         int ref;
148         FT_Error getGlyphBitmap(FT_ULong glyph_index, FTC_SBit *sbit);
149         FT_Face face;
150         FT_Size size;
151         
152         int tabwidth;
153         int height;
154         Font(fontRenderClass *render, FTC_FaceID faceid, int isize, int tabwidth);
155         ~Font();
156         
157         void lock();
158         void unlock();  // deletes if ref==0
159 };
160
161 extern fontRenderClass *font;
162
163 #endif