Subversion Repositories Kolibri OS

Rev

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

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