-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttf_font.h
executable file
·232 lines (205 loc) · 5.63 KB
/
ttf_font.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#ifndef __TTF_FONT_H__
#define __TTF_FONT_H__
#include "codec.h"
#include "trueType/ft2build.h"
#include "trueType/freetype.h"
#include "trueType/ftoutln.h"
#include "trueType/ftsynth.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iconv.h>
#include <map>
#include "hulib.h"
using namespace std;
#define FONT_NORMAL 0
#define FONT_ITALIC 1
#define FONT_BOLD 2
typedef unsigned short U16;
class text;
class FontDev: public Mutex
{ //FontDev字体绘制器 ,阻塞绘制元素,同BLT功能一般
public:
FontDev()
{
face = NULL;
ft_Lib = NULL;
}
~FontDev()
{
if (face != NULL)
FT_Done_Face(face);
if (ft_Lib != NULL)
FT_Done_FreeType(ft_Lib);
}
int TTF_Init(const char * path, unsigned char bitmap)
{
FT_UInt glyph_index;
int i;
ft_error = FT_Init_FreeType(&ft_Lib);
if (ft_error)
{
printf("Error at init ft_Lib!\n");
FT_Done_FreeType(ft_Lib);
return -1;
}
ft_error = FT_New_Face(ft_Lib, path, 0, &face);
if (ft_error)
{
printf("New face error!\n");
FT_Done_FreeType(ft_Lib);
return -1;
}
TTF_bitmap_type = bitmap;
matrix.xx = 1 << 16;
matrix.xy = 0x5800;
matrix.yx = 0;
matrix.yy = 1 << 16;
printf("TTF_Init succssed!!!!!!!!!!!!!!!!!\n");
// logMsg("init ok.\n");
return 0;
}
int setPixelSize(int fontWidth, int fontHeight)
{
Font_H = fontHeight;
ft_error = FT_Set_Pixel_Sizes(face, fontWidth, fontHeight);
if (ft_error)
{
errexitf("Error at set fontSize %dx%d \n", fontWidth, fontHeight);
}
}
int setStyle(unsigned char bitmap)
{
TTF_bitmap_type = bitmap;
return 0;
}
ssize_t convert(const char *tocode, const char *fromcode, char *inbufp, size_t inbytesleft, char *outbufp,
size_t outbytesleft);
void DrawText(text * ptext, const char *encode, char * showtxt,int buff_width,int buff_height,
unsigned int txt_len);
int TTF_DisplayAscii(text * ptext, const unsigned char *text, int num,
unsigned int color, unsigned char style);
int TTF_DisplayUnicode(text * ptext, const wchar_t *text, int num,
unsigned int color, unsigned char style,int buff_width,int buff_height);
FT_Library ft_Lib;
FT_Face face;
FT_UInt glyph_index;
FT_Vector pen;
FT_Matrix matrix;
FT_Error ft_error;
unsigned char TTF_bitmap_type;
unsigned int Font_H;
};
class text: public image
{
private:
//void LCD_PutPixel(unsigned int x, unsigned int y, unsigned int color, unsigned char gray);
//void ft_draw_bitmap(FT_Bitmap *bitmap, int x, int y, unsigned int color);
public:
//以下几个变量需要外部赋值
FontDev * m_font;
unsigned int fontWidth;
unsigned int fontHeight;
unsigned int color;
unsigned char style;
//void *text_buffer;
text()
{
fontWidth = 0;
fontHeight = 16;
color = 0;
style = 0;
m_font = NULL;
printf("truetype\n");
}
~text()
{
destroy();
}
void destroy()
{
printf("destroy\n");
}
void LCD_PutPixel(unsigned int x, unsigned int y, unsigned int color,
unsigned char gray)
{
((IMG_PIX *) pSrcBuffer + y * u32Width + x)->u8Red = color >> 16
& 0xff;
((IMG_PIX *) pSrcBuffer + y * u32Width + x)->u8Green = color >> 8
& 0xff;
((IMG_PIX *) pSrcBuffer + y * u32Width + x)->u8Blue = color
& 0xff;
((IMG_PIX *) pSrcBuffer + y * u32Width + x)->u8Alpha = gray;
}
void ft_draw_bitmap(FT_Bitmap *bitmap, int dst_x, int dst_y,
unsigned int color)
{
int y, x;
unsigned char gray;
if (dst_x + bitmap->width > u32Width)
{
printf("warning ft_draw_bitmap u32Width[%d] to low\r\n",u32Width);
return;
}
if (dst_y + bitmap->rows > u32Height)
{
printf("warning ft_draw_bitmap u32Height[%d] to low\r\n",u32Height);
return;
}
for (y = 0; y < bitmap->rows; y++)
{
if (dst_y + y > u32Height)
{
break;
}
for (x = 0; x < bitmap->width; x++)
{
if (dst_x + x > u32Width)
{
break;
}
gray = bitmap->buffer[y * bitmap->pitch + x];
// printf("gray=%d\n",gray);
if (gray > 0)
{
LCD_PutPixel(dst_x + x, dst_y + y, color, gray);
}
}
}
}
//int added_txt(unsigned int w, unsigned int h, unsigned int color);
//void free_text();
//void Disp_txt(unsigned int * pSrcBuffer, unsigned int SrcBuffer_W, unsigned int x, unsigned int y, unsigned int w,unsigned int h);
//int TTF_Init(const char* path, int fontWidth, int fontHeight, unsigned char bitmap);
//int ft_set_style(int fontWidth, int fontHeight, unsigned char bitmap);
//int TTF_DisplayAscii(int x, int y, const unsigned char *text, int num, unsigned int color, unsigned char style);
//int TTF_DisplayUnicode(int x, int y, const wchar_t *text, int num, unsigned int color, unsigned char style);
void DrawText(const char *encode, char * showtxt, unsigned int txt_len)
{
if (m_font != NULL)
{
// if (fontHeight > u32Height)
// {
// errexitf("DrawText fontHeight[%d] > u32Height[%d]\r\n",
// fontHeight, u32Height);
// }
m_font->DrawText(this, encode, showtxt, u32Width,u32Height,txt_len);
}
}
int DrawText_unicode( wchar_t * showtxt, unsigned int txt_len)
{
if (m_font != NULL)
{
// if (fontHeight > u32Height)
// {
// errexitf("DrawText fontHeight[%d] > u32Height[%d]\r\n",
// fontHeight, u32Height);
// }
return m_font->TTF_DisplayUnicode(this,showtxt,txt_len, this->color, this->style,u32Width,u32Height);
}
}
//format convert
//eg:convert("UTF-8", "wchar_t", (char *)out_buf1, rc, out_buf2, out_len);
};
extern map<hustr, FontDev> font_mp;
#endif /* __TTF_FUNC_H__ */