Subversion Repositories Kolibri OS

Rev

Rev 5517 | 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
 
5518 leency 173
 
5517 hidnplayr 174
void __stdcall SetBackColor(int back_color, int width, int height, char *from_zone)
175
{
5518 leency 176
	unsigned char bcr = back_color>>16;
177
	unsigned char bcg = back_color>>8;
178
	unsigned char bcb = back_color;
179
 
180
	unsigned int i;
181
	unsigned int max_i = width * height * 3;
182
	for (i=0; i < max_i; i+=3)
5517 hidnplayr 183
	{
5518 leency 184
		from_zone[i]   = bcb;
185
		from_zone[i+1] = bcg;
186
		from_zone[i+2] = bcr;
5517 hidnplayr 187
	}
5518 leency 188
 
5517 hidnplayr 189
}
190
 
5518 leency 191
 
192
 
193
 
5517 hidnplayr 194
int __stdcall text_out(char *string, char *buffer, int height, int color,int back_color, int x, int y) {
195
	unsigned char *from_zone;
196
	unsigned char *to_zone;
197
	int px, py;
198
	unsigned char app_data[1024];
199
	int width;
200
 
201
	width = get_width_utf8(string,buffer,height);
202
 
203
	from_zone=(char*)zmalloc(3*height*width);
204
	to_zone=(char*)zmalloc(height*width);
205
 
206
	kol_process_info(-1, app_data);
207
	//px=app_data[35]*256+app_data[34];
208
	px=app_data[35]*256+app_data[34]+app_data[55]*256+app_data[54];//lev
209
	//py=app_data[39]*256+app_data[38];
210
	py=app_data[39]*256+app_data[38]+app_data[59]*256+app_data[58];//lev
211
 
212
	//getzone(px+x, py+y, width, height, from_zone);
213
	SetBackColor(back_color, width, height,from_zone);
214
	picture_utf8(string, buffer, to_zone, width, height);
215
	font_blit(from_zone,to_zone, color, width*height);
216
//f65(x,y,width,height,from_zone);
217
	PutImage(x,y,width,height,from_zone);//lev
218
 
219
	zfree(from_zone);
220
	zfree(to_zone);
221
	return 0;
222
}
223
 
224
unsigned char*  __stdcall text_out_mem(short *string, stbtt_fontinfo *buffer, int height, int color,int back_color) {
225
	unsigned char *from_zone;
226
	unsigned char *to_zone;
227
	int width;
228
 
229
	width = get_width_utf8(string,buffer,height);
230
	from_zone=(char*)zmalloc(3*height*width+8);
231
	to_zone=(char*)zmalloc(height*width);
232
	*(int*)from_zone = width;
233
	*(int*)(from_zone+4) = height;
234
	SetBackColor(back_color, width, height,from_zone+8);
235
	picture_utf8(string, buffer, to_zone, width, height);
236
	font_blit(from_zone+8, to_zone, color, width*height);
237
 
238
	zfree(to_zone);
239
	return from_zone;
240
}
241
 
242
int __stdcall start(){
243
      return 1;
244
}
245
 
246
int __stdcall version_major(){
247
      return 1;
248
}
249
 
250
int __stdcall version_minor(){
251
     return 0;
252
}
253
 
254
typedef struct{
255
  char *name;
256
  void *f;
257
}export_t;
258
 
259
char szStart[]		="START";
260
char szVersion[]	="version";
261
char szVersionM[]	="version_min";
262
char szTrueType[]	="truetype";
263
char szGetLength[]	="get_length";
264
char szGetWidth[]	="get_width";
265
char szTextOut[]	="text_out";
266
char szTextOutMem[] ="text_out_mem";
267
char szInitFont[]	="init_font";
268
 
269
 
270
export_t EXPORTS[] __asm__("EXPORTS") =
271
	{
272
		{ szStart,		start },
273
		{ szVersion,	version_major },
274
		{ szVersionM,	version_minor },
275
		{ szTrueType,	picture_utf8 },
276
		{ szGetLength,	get_length_utf8},
277
		{ szGetWidth,	get_width_utf8},
278
		{ szTextOut,	text_out },
279
		{ szTextOutMem, text_out_mem},
280
		{ szInitFont,	init_font},
281
		{ NULL, NULL },
282
	};