Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
7285 leency 1
void ParseTxt()
6043 leency 2
{
7285 leency 3
_canvas canvas;
6043 leency 4
byte ch, zeroch=0;
5
dword bufoff, buflen, line_start, srch_pos;
6
int stroka_y=5, line_length=0;
7
 
8
	line_start=io.buffer_data;
9
	buflen = strlen(io.buffer_data) + io.buffer_data;
10
	for (bufoff=io.buffer_data; bufoff
11
	{
12
		ch = ESBYTE[bufoff];
13
		line_length += char_width[ch];
14
		if (line_length>=list.w-30) || (ch==10) {
15
			srch_pos = bufoff;
16
			loop()
17
			{
18
				if (__isWhite(ESBYTE[srch_pos])) { bufoff=srch_pos+1; break; } //normal word-break
19
				if (srch_pos == line_start) break; //no white space found in whole line
20
				srch_pos--;
21
			}
7285 leency 22
			if (kfont.size.height) {
6043 leency 23
				ESBYTE[bufoff] >< zeroch; //set line end
7285 leency 24
				canvas.write_text(8, stroka_y, 0x000000, line_start);
6043 leency 25
				ESBYTE[bufoff] >< zeroch; //restore line
26
			}
27
			stroka_y += list.item_h;
28
			line_start = bufoff;
29
			line_length = 0;
30
		}
31
	}
7285 leency 32
	if (!kfont.size.height) {
6059 leency 33
		list.count = stroka_y/list.item_h+3;
6043 leency 34
		if (list.count < list.visible) list.count = list.visible;
6806 leency 35
		kfont.size.height = list.count+5*list.item_h;
36
		kfont.raw_size = 0;
7285 leency 37
		ParseTxt();
6043 leency 38
	}
7285 leency 39
	else canvas.write_text(8, stroka_y, 0x000000, line_start);
6043 leency 40
}
41
 
42
/*========================================================
43
=                                                        =
7285 leency 44
=                        STYLE                           =
6043 leency 45
=                                                        =
46
========================================================*/
6053 leency 47
#define HTML_PADDING_X 8;
48
#define HTML_PADDING_Y 5;
6043 leency 49
 
7285 leency 50
struct _style {
51
	bool b, u, i, s;
52
	bool h1, h2, h3, h4, h5, h6;
53
	bool a;
54
	bool pre;
55
	bool ignore;
56
	dword color;
57
	void clear();
58
};
6053 leency 59
 
7285 leency 60
void _style::clear()
61
{
62
	b=u=i=s=0;
63
	h1=h2=h3=h4=h5=h6=0;
64
	a=0;
65
	pre=0;
66
	ignore=0;
67
	color=0;
68
}
69
 
70
/*========================================================
71
=                                                        =
72
=                         TAG                            =
73
=                                                        =
74
========================================================*/
75
struct _tag {
76
	dword start;
77
	dword end;
78
	dword name;
79
	dword param[10];
80
	dword value[10];
81
	void parse();
82
	int nameis();
83
};
84
 
85
void _tag::parse()
86
{
87
	strlwr(name);
88
}
89
 
90
int _tag::nameis(dword _in_tag_name)
91
{
92
	if (name) && (strcmp(_in_tag_name, start)==0) return true;
93
	return false;
94
}
95
 
96
/*========================================================
97
=                                                        =
98
=                         DRAW                           =
99
=                                                        =
100
========================================================*/
101
struct _draw
102
{
103
	dword x;
104
	dword y;
105
	void init();
106
	void line_break();
107
};
108
 
109
void _draw::init()
110
{
111
	x = HTML_PADDING_X;
112
	y = HTML_PADDING_Y;
113
}
114
void _draw::line_break()
115
{
116
	y+= list.item_h;
117
	x = HTML_PADDING_X;
118
}
119
 
120
/*========================================================
121
=                                                        =
122
=                         BUF                            =
123
=                                                        =
124
========================================================*/
125
struct _buf
126
{
127
	dword pointer;
128
	dword len;
129
	dword start;
130
	dword end;
131
	void init();
132
};
133
 
134
void _buf::init(dword _buf_pointer, _buf_len)
135
{
136
	pointer = _buf_pointer;
137
	len = _buf_len;
138
	start = malloc(len);
139
	end = start + len;
140
	strlcpy(start, pointer, len);
141
}
142
 
143
/*========================================================
144
=                                                        =
145
=                         TEXT                           =
146
=                                                        =
147
========================================================*/
148
struct _text {
149
	int size_pt_change;
150
	dword start;
151
	dword end;
152
};
153
 
154
/*
6053 leency 155
dword line_break;
7285 leency 156
byte char_holder;
6043 leency 157
 
7285 leency 158
if (ESBYTE[buf.pos]==0x0A) {
159
	if (style.pre) {
160
		draw.line_break();
161
		continue;
162
	}
163
}
164
 
165
while (get_label_len(text.start) + draw.x + 30 > list.w)
166
{
167
	for (line_break=tag.start-1; line_break>text.start; line_break--;)
6043 leency 168
	{
7285 leency 169
		char_holder = ESBYTE[line_break]; //set line end
170
		ESBYTE[line_break] = '\0';
171
		if (get_label_len(text.start) + draw.x + 30 <= list.w) break;
172
		ESBYTE[line_break] = char_holder; //restore line
173
	}
174
	if (draw_on) {
175
		if (style.a) {
176
			link.add(draw.x,draw.y + size_pt_change,get_label_len(text.start),list.item_h,text.start," ");
177
			label_draw_bar(draw.x, draw.y+kfont.size.pt+1, get_label_len(text.start), style.color);
178
		}
179
		WriteTextIntoBuf(draw.x, draw.y, style.color, text.start);
180
	}
181
	draw.x+=char_width[' '];
182
	ESBYTE[line_break] = char_holder; //restore line
183
	text.start = line_break;
184
	draw.line_break();
185
}
186
if (draw_on) {
187
	if (style.a) {
188
		link.add(draw.x,draw.y + size_pt_change,get_label_len(text.start),list.item_h,text.start," ");
189
		label_draw_bar(draw.x, draw.y+kfont.size.pt+1, get_label_len(text.start), style.color);
190
	}
191
	WriteTextIntoBuf(draw.x, draw.y, style.color, text.start);
192
}
193
draw.x += char_width[' '];
194
draw.x += get_label_len(text.start);
195
*/
6375 guillem 196
 
7285 leency 197
 
198
/*========================================================
199
=                                                        =
200
=                         DOM                            =
201
=                                                        =
202
========================================================*/
203
struct _dom
204
{
205
	_tag tag;
206
	_style style;
207
	_draw draw;
208
	_buf buf;
209
	_text text;
210
	_canvas canvas;
211
	void init();
212
	void set_style();
213
	void parse();
214
	void apply_text();
215
};
216
 
217
void _dom::init()
218
{
219
	style.clear();
220
	draw.init();
221
	buf.init(io.buffer_data, strlen(io.buffer_data));
222
}
223
 
224
void _dom::set_style()
225
{
226
	/*
227
	if (tag.nameis("pre")) style.pre = true;
228
	if (tag.nameis("/pre")) style.pre = false;
229
	if (tag.nameis("script")) || (tag.nameis("style")) style.ignore = true;
230
	if (tag.nameis("/script")) || (tag.nameis("/style")) style.ignore = false;
231
	if (tag.nameis("a"))  { style.a = true;  style.color=0x0000FF; }
232
	if (tag.nameis("/a")) { style.a = false; style.color=0x000000; }
233
 
234
	if (tag.nameis("br"))
235
		|| (tag.nameis("p"))
236
		|| (tag.nameis("div"))
237
		|| (tag.nameis("tr")) {
238
		draw.line_break();
239
		return;
240
	}
241
 
242
	if (dom.tag.nameis("title")) {
243
		strcpy(#title, text.start);
244
		strcat(#title, " - Aelia");
245
		DrawTitle(#title);
246
	}
247
 
248
	if 	(tag.nameis("h1")) || (tag.nameis("/h1")) ||
249
		(tag.nameis("h2")) || (tag.nameis("/h2")) ||
250
		(tag.nameis("h3")) || (tag.nameis("/h3")) {
251
		if (tag.nameis("h1")) {
252
			text.size_pt_change = 8;
253
		} else if (tag.nameis("/h1")) {
254
			text.size_pt_change = -8;
255
		} else if (tag.nameis("h2")) {
256
			text.size_pt_change = 6;
257
		} else if (tag.nameis("/h2")) {
258
			text.size_pt_change = -6;
259
		} else if (tag.nameis("h3")) {
260
			text.size_pt_change = 4;
261
		} else if (tag.nameis("/h3")) {
262
			text.size_pt_change = -4;
6043 leency 263
		}
7285 leency 264
		kfont.size.pt += text.size_pt_change;
265
		get_label_symbols_size();
266
		if (text.size_pt_change > 0) {
267
			draw.y+= list.item_h;//что если будет очень длинная строка в теге?
268
		} else {//коммент выше и коммент ниже связаны
269
			draw.y+= list.item_h - text.size_pt_change;//не очень понятна логика этого места
270
			text.size_pt_change = 0;
271
		}
272
		draw.x = HTML_PADDING_X;
273
		return;
6043 leency 274
	}
7285 leency 275
	*/
276
}
277
 
278
void _dom::apply_text()
279
{
280
	if (kfont.size.height) canvas.write_text(draw.x, draw.y, style.color, text.start);
281
	draw.line_break();
282
}
283
 
284
void _dom::parse()
285
{
286
	dword i;
287
	init();
288
 
289
	text.start = buf.start;
290
	tag.start = buf.start;
291
	for ( i=buf.start; i
292
	{
293
		if (ESBYTE[i]=='<') {
294
			tag.start = i+1;
295
			text.end = i-1;
296
			ESBYTE[i] = '\0';
297
			debug("TXT "); debugln(text.start);
298
			apply_text();
299
		}
300
		if (ESBYTE[i]=='>') {
301
			tag.end = i-1;
302
			text.start = i+1;
303
			ESBYTE[i] = '\0';
304
			debug("TAG "); debugln(tag.start);
305
			//tag.parse();
306
			//set_style();
307
		}
308
	}
309
 
310
	free(buf.start);
311
	if (!kfont.size.height) {
312
		list.count = draw.y/list.item_h+3;
6053 leency 313
		if (list.count < list.visible) list.count = list.visible;
6806 leency 314
		kfont.size.height = list.count+5*list.item_h;
315
		kfont.raw_size = 0;
7285 leency 316
		parse();
6053 leency 317
	}
7285 leency 318
}
319
 
320
 
321
 
322
/*========================================================
323
=                                                        =
324
=                       PREPARE                          =
325
=                                                        =
326
========================================================*/
327
void PreparePage()
328
{
329
	_dom dom;
330
	list.SetSizes(0, TOOLBAR_H, Form.cwidth-scroll.size_x-1, Form.cheight-TOOLBAR_H, kfont.size.pt+4);
331
	strcpy(#title, history.current()+strrchr(history.current(),'/'));
332
	get_label_symbols_size(); //get font chars width, need to increase performance
333
	ChangeCharset(charsets[encoding], "CP866", io.buffer_data);
334
	link.clear();
335
 
336
	kfont.size.height = 0;
337
 
338
	if ( strstri(io.buffer_data, "
339
 
340
	kfont.ApplySmooth();
341
 
342
	DrawPage();
6043 leency 343
}