Subversion Repositories Kolibri OS

Rev

Rev 3002 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5517 hidnplayr 1
#include "glue.h"
2
#define STB_TRUETYPE_IMPLEMENTATION  // force following include to generate implementation
3
#include "stb_truetype.h"
4
 
5
#define __stdcall __attribute__((stdcall))
6
 
7
// "Fast and economical" UTF-8 to codepoint decoder by Bjoern Hoehrmann.
8
// Copyright (c) 2008-2010 Bjoern Hoehrmann 
9
// See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
10
 
11
#define UTF8_ACCEPT 0
12
#define UTF8_REJECT 12
13
 
14
static const unsigned char utf8d[] = {
15
  // The first part of the table maps bytes to character classes that
16
  // to reduce the size of the transition table and create bitmasks.
17
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
18
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
19
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
20
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
21
   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,  9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
22
   7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
23
   8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
24
  10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
25
 
26
  // The second part is a transition table that maps a combination
27
  // of a state of the automaton and a character class to a state.
28
   0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
29
  12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
30
  12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
31
  12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
32
  12,36,12,12,12,12,12,12,12,12,12,12,
33
};
34
 
35
unsigned int inline decode(unsigned int* state, unsigned int* codep, unsigned int byte) {
36
  unsigned int type = utf8d[byte];
37
 
38
  *codep = (*state != UTF8_ACCEPT) ?
39
    (byte & 0x3fu) | (*codep << 6) :
40
    (0xff >> type) & (byte);
41
 
42
  *state = utf8d[256 + *state + type];
43
  return *state;
44
}
45
 
46
inline void PutImage(unsigned x, unsigned y, unsigned w, unsigned h, char * image){
47
unsigned size, psize;
48
size=x*65536+y;
49
psize=w*65536+h;
50
__asm__ __volatile__("int $0x40"::"a"(7),"b"(image),"c"(psize),"d"(size));
51
}
52
 
53
void font_blit(unsigned char *dst,unsigned char *src, int textcolor, int size) {
54
	int lp;
55
	float mixed, tp;
56
	unsigned char color1;
57
	int red, green, blue;
58
	red=((textcolor)&0x000000FF);
59
	green=(((textcolor)&0x00FF00)>>8);
60
	blue=(((textcolor)&0xFF0000)>>16);
61
 
62
	for (lp=0;lp <= size;lp++) {
63
		if (src[lp]>0) {
64
			tp=((float)src[lp])/255;
65
 
66
			color1=dst[lp*3];
67
			mixed=(color1 * (1-tp)+red * tp);
68
			dst[lp*3]=(unsigned char) mixed;
69
 
70
			color1=dst[lp*3+1];
71
			mixed=(color1 * (1-tp)+green * tp);
72
			dst[lp*3+1]=(unsigned char) mixed;
73
 
74
			color1=dst[lp*3+2];
75
			mixed=(color1 * (1-tp)+blue * tp);
76
			dst[lp*3+2]=(unsigned char) mixed;
77
 
78
		}
79
	}
80
 
81
}
82
 
83
 
84
inline unsigned kol_process_info(unsigned slot, char buf1k[]){
85
	asm ("int $0x40"::"a"(9), "b"(buf1k), "c"(slot));
86
}
87
 
88
int __stdcall init_font(stbtt_fontinfo *font,unsigned char *FontData)
89
{
90
 	stbtt_InitFont(font, FontData,stbtt_GetFontOffsetForIndex(FontData,0) );
91
}
92
 
93
int __stdcall get_width_utf8(unsigned char *s, stbtt_fontinfo *buffer, int height)
94
{
95
	stbtt_fontinfo *font;
96
	font=buffer;
97
	float scale, xpos=0;
98
 
99
	unsigned int codepoint;
100
	unsigned int state = 0;
101
 
102
	scale = stbtt_ScaleForPixelHeight(font, height*3/4);
103
	int advance,lsb;
104
 
105
	for (; *s; s++) {
106
		if (!decode(&state, &codepoint, *s)) {
107
			stbtt_GetCodepointHMetrics(font, codepoint, &advance, &lsb);
108
			xpos += (advance * scale);
109
		}
110
	}
111
 
112
	return xpos;
113
}
114
 
115
 
116
int __stdcall get_length_utf8(unsigned char *s, char *buffer, int height, int max_len)
117
{
118
	stbtt_fontinfo *font;
119
	font=buffer;
120
	int ch=0, xpos=0;
121
	float scale=0;
122
 
123
	scale = stbtt_ScaleForPixelHeight(&font, height*3/4);
124
	int advance, lsb;
125
 
126
	unsigned int codepoint;
127
    unsigned int state = 0;
128
 
129
    for (; s[ch]; ch++) {
130
		if (!decode(&state, &codepoint, s[ch])) {
131
 
132
		stbtt_GetCodepointHMetrics(&font, codepoint, &advance, &lsb);
133
		xpos += (advance * scale);
134
 
135
		if ((int)xpos>max_len)
136
			return ch;
137
		}
138
	}
139
	return ch;
140
}
141
 
142
 
143
int __stdcall picture_utf8(unsigned char *s, stbtt_fontinfo *buffer, char *screen1, int width, int height)
144
{
145
	stbtt_fontinfo *font;
146
	font=buffer;
147
	int ascent, baseline, descent;
148
	int advance, lsb, x0, y0, x1, y1;
149
	float scale, xpos=0;
150
 
151
	scale = stbtt_ScaleForPixelHeight(font, height*3/4);
152
	stbtt_GetFontVMetrics(font, &ascent, &descent,0);
153
	baseline = (int) ((ascent-descent)*scale);
154
 
155
	unsigned int codepoint;
156
    unsigned int state = 0;
157
 
158
    for (; *s; s++) {
159
		if (!decode(&state, &codepoint, *s)) {
160
			stbtt_GetCodepointHMetrics(font, codepoint, &advance, &lsb);									///////////////////////////////////
161
			stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale, scale, 0, 0, &x0, &y0, &x1, &y1);	///////////////////////////////////
162
			stbtt_MakeCodepointBitmapSubpixel(font, &screen1[(baseline+y0)*width+(int)xpos+x0], x1-x0, y1-y0, width, scale, scale, 0, 0, codepoint);
163
 
164
			xpos += (advance*scale);
165
		}
166
		//if (state != UTF8_ACCEPT)
167
			//kol_board_puts("The string is not well-formed\n");
168
	}
169
 
170
	return 0;
171
}
172
 
173
void __stdcall SetBackColor(int back_color, int width, int height, char *from_zone)
174
{
175
	int i,j;
176
	unsigned char bcr,bcg,bcb;
177
 
178
	bcr=back_color<<16;
179
	bcg=back_color<<8;
180
	bcb=back_color;
181
 
182
	for (j=0;j
183
	{
184
		for (i=0;i
185
		{
186
			from_zone[(j*width+i)*3]=back_color;
187
			from_zone[(j*width+i)*3+1]=back_color>>8;
188
			from_zone[(j*width+i)*3+2]=back_color>>16;
189
		}
190
	}
191
}
192
 
193
int __stdcall text_out(char *string, char *buffer, int height, int color,int back_color, int x, int y) {
194
	unsigned char *from_zone;
195
	unsigned char *to_zone;
196
	int px, py;
197
	unsigned char app_data[1024];
198
	int width;
199
 
200
	width = get_width_utf8(string,buffer,height);
201
 
202
	from_zone=(char*)zmalloc(3*height*width);
203
	to_zone=(char*)zmalloc(height*width);
204
 
205
	kol_process_info(-1, app_data);
206
	//px=app_data[35]*256+app_data[34];
207
	px=app_data[35]*256+app_data[34]+app_data[55]*256+app_data[54];//lev
208
	//py=app_data[39]*256+app_data[38];
209
	py=app_data[39]*256+app_data[38]+app_data[59]*256+app_data[58];//lev
210
 
211
	//getzone(px+x, py+y, width, height, from_zone);
212
	SetBackColor(back_color, width, height,from_zone);
213
	picture_utf8(string, buffer, to_zone, width, height);
214
	font_blit(from_zone,to_zone, color, width*height);
215
//f65(x,y,width,height,from_zone);
216
	PutImage(x,y,width,height,from_zone);//lev
217
 
218
	zfree(from_zone);
219
	zfree(to_zone);
220
	return 0;
221
}
222
 
223
unsigned char*  __stdcall text_out_mem(short *string, stbtt_fontinfo *buffer, int height, int color,int back_color) {
224
	unsigned char *from_zone;
225
	unsigned char *to_zone;
226
	int width;
227
 
228
	width = get_width_utf8(string,buffer,height);
229
	from_zone=(char*)zmalloc(3*height*width+8);
230
	to_zone=(char*)zmalloc(height*width);
231
	*(int*)from_zone = width;
232
	*(int*)(from_zone+4) = height;
233
	SetBackColor(back_color, width, height,from_zone+8);
234
	picture_utf8(string, buffer, to_zone, width, height);
235
	font_blit(from_zone+8, to_zone, color, width*height);
236
 
237
	zfree(to_zone);
238
	return from_zone;
239
}
240
 
241
int __stdcall start(){
242
      return 1;
243
}
244
 
245
int __stdcall version_major(){
246
      return 1;
247
}
248
 
249
int __stdcall version_minor(){
250
     return 0;
251
}
252
 
253
typedef struct{
254
  char *name;
255
  void *f;
256
}export_t;
257
 
258
char szStart[]		="START";
259
char szVersion[]	="version";
260
char szVersionM[]	="version_min";
261
char szTrueType[]	="truetype";
262
char szGetLength[]	="get_length";
263
char szGetWidth[]	="get_width";
264
char szTextOut[]	="text_out";
265
char szTextOutMem[] ="text_out_mem";
266
char szInitFont[]	="init_font";
267
 
268
 
269
export_t EXPORTS[] __asm__("EXPORTS") =
270
	{
271
		{ szStart,		start },
272
		{ szVersion,	version_major },
273
		{ szVersionM,	version_minor },
274
		{ szTrueType,	picture_utf8 },
275
		{ szGetLength,	get_length_utf8},
276
		{ szGetWidth,	get_width_utf8},
277
		{ szTextOut,	text_out },
278
		{ szTextOutMem, text_out_mem},
279
		{ szInitFont,	init_font},
280
		{ NULL, NULL },
281
	};