Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
8439 leency 1
void TWebBrowser::SetStyle()
2
{
8444 leency 3
	if (tag.get_value_of("name")) || (tag.get_value_of("id")) {
8439 leency 4
		anchors.add(tag.value, draw_y);
5
		if (anchors.current) && (streq(tag.value, #anchors.current+1)) {
6
			list.first = draw_y;
7
			anchors.current = NULL;
8
		}
9
	}
10
	if (tag.is("a"))          { tag_a();                   return; }
11
	if (tag.is("p"))          { tag_p();                   return; }
12
	if (tag.is("img"))        { tag_img();                 return; }
13
	if (tag.is("div"))        { tag_div();                 return; }
8500 leency 14
	if (tag.is("br"))         { /*draw_x++;*/NewLine();    return; }
8439 leency 15
	if (tag.is("header"))     { NewLine();                 return; }
16
	if (tag.is("article"))    { NewLine();                 return; }
17
	if (tag.is("footer"))     { NewLine();                 return; }
18
	if (tag.is("figure"))     { NewLine();                 return; }
19
	if (tag.is("w:p"))        { NewLine();                 return; }
8462 leency 20
	if (tag.is("b"))          { style.b      = tag.opened; return; }
21
	if (tag.is("strong"))     { style.b      = tag.opened; return; }
22
	if (tag.is("big"))        { style.b      = tag.opened; return; }
23
	if (tag.is("w:b"))        { style.b      = tag.opened; return; }
24
	if (tag.is("u"))          { style.u      = tag.opened; return; }
25
	if (tag.is("ins"))        { style.u      = tag.opened; return; }
26
	if (tag.is("s"))          { style.s      = tag.opened; return; }
27
	if (tag.is("strike"))     { style.s      = tag.opened; return; }
28
	if (tag.is("del"))        { style.s      = tag.opened; return; }
29
	if (tag.is("pre"))        { style.pre    = tag.opened; return; }
30
	if (tag.is("blockquote")) { style.blq    = tag.opened; return; }
31
	if (tag.is("button"))     { style.button = tag.opened; return; }
8439 leency 32
	if (tag.is("dl"))         { if (tag.opened) NewLine(); return; }
33
	if (tag.is("w:r"))        { if (!tag.opened) style.b = false;      return; }
34
	if (tag.is("h1"))         { tag_h1234_caption();  return; }
35
	if (tag.is("h2"))         { tag_h1234_caption();  return; }
36
	if (tag.is("h3"))         { tag_h1234_caption();  return; }
37
	if (tag.is("h4"))         { tag_h1234_caption();  return; }
38
	if (tag.is("font"))       { tag_font();           return; }
39
	if (tag.is("dt"))         { tag_ol_ul_dt();       return; }
9263 leency 40
	if (tag.is("dd"))         { tag_ol_ul_dt();       return; }
8439 leency 41
	if (tag.is("ul"))         { tag_ol_ul_dt();       return; }
42
	if (tag.is("ol"))         { tag_ol_ul_dt();       return; }
43
	if (tag.is("li"))         { tag_li();             return; }
44
	if (tag.is("q"))          { tag_q();              return; }
45
	if (tag.is("hr"))         { tag_hr();             return; }
46
	if (tag.is("meta"))       { tag_meta_xml();       return; }
47
	if (tag.is("?xml"))       { tag_meta_xml();       return; }
48
	if (tag.is("code"))       { tag_code();           return; }
49
	if (tag.is("iframe"))     { tag_iframe();         return; }
50
	if (tag.is("caption"))    { tag_h1234_caption();  return; }
51
	if (tag.is("title"))      { tag_title();          return; }
52
	if (tag.is("body"))       { tag_body();           return; }
53
	if (tag.is("html"))       { t_html = tag.opened;  return; }
9103 leency 54
	tag_table();
8439 leency 55
}
56
 
57
void TWebBrowser::tag_p()
58
{
59
	IF (tag.prior[0] == 'h') || (streq(#tag.prior,"td")) || (streq(#tag.prior,"p")) return;
60
	NewLine();
61
}
62
 
63
void TWebBrowser::tag_title()
64
{
65
	style.title = tag.opened;
66
	if (!tag.opened) DrawTitle(#header);
67
}
68
 
69
void TWebBrowser::tag_font()
70
{
71
	style.font = tag.opened;
72
	if (tag.opened)
73
	{
8451 leency 74
		if (tag.get_value_of("bg")) {
75
			bg_colors.add(GetColor(tag.value));
76
		} else {
77
			bg_colors.add(bg_colors.get_last());
78
		}
8444 leency 79
		if (tag.get_value_of("color")) {
8439 leency 80
			text_colors.add(GetColor(tag.value));
81
		} else {
82
			text_colors.add(text_colors.get_last());
83
		}
84
	}
8451 leency 85
	else {
86
		text_colors.pop();
8499 leency 87
		if (bg_colors.count>1) bg_colors.pop(); //never pop the last color
8451 leency 88
	}
8439 leency 89
}
90
 
91
void TWebBrowser::tag_div()
92
{
8499 leency 93
	//if (streq(#tag.prior,"div")) && (tag.opened) return;
8490 leency 94
	if (streq(#tag.prior,"td")) return;
8499 leency 95
	if (streq(#tag.prior,"div")) return;
8439 leency 96
	if (!tag.opened) && (style.font) text_colors.pop();
97
	NewLine();
98
}
99
 
100
void TWebBrowser::tag_iframe()
101
{
8444 leency 102
	if (tag.get_value_of("src")) {
8439 leency 103
		NewLine();
8492 leency 104
		strcpy(#linebuf, "IFRAME: ");
8491 leency 105
		RenderTextbuf();
8439 leency 106
		link=true;
107
		links.add_link(tag.value);
8492 leency 108
		strncpy(#linebuf, tag.value, sizeof(TWebBrowser.linebuf)-1);
8491 leency 109
		RenderTextbuf();
8439 leency 110
		link=false;
111
		NewLine();
112
	}
113
}
114
 
115
void TWebBrowser::tag_a()
116
{
117
	if (tag.opened)
118
	{
8444 leency 119
		if (tag.get_value_of("href")) && (!strstr(tag.value,"javascript:"))
8439 leency 120
		{
121
			link = true;
122
			links.add_link(tag.value);
123
		}
124
	} else {
125
		link = false;
126
	}
127
}
128
 
129
void TWebBrowser::tag_meta_xml()
130
{
8444 leency 131
	if (custom_encoding == -1) if (tag.get_value_of("charset"))
132
	|| (tag.get_value_of("content")) || (tag.get_value_of("encoding"))
8439 leency 133
	{
134
		EDX = strrchr(tag.value, '=') + tag.value; //search in content=
135
		if (ESBYTE[EDX] == '"') EDX++;
136
		strlwr(EDX);
8492 leency 137
		EAX = get_encoding_type_by_name(EDX);
138
		if (EAX!=-1) ChangeEncoding(EAX);
8439 leency 139
	}
8444 leency 140
	if (streq(tag.get_value_of("http-equiv"), "refresh")) && (tag.get_value_of("content")) {
141
		if (tag.value = strstri(tag.value, "url")) strcpy(#redirect, tag.value);
8439 leency 142
	}
143
}
144
 
8492 leency 145
signed int get_encoding_type_by_name(dword name)
146
{
147
	EDX = name;
148
	if      (streqrp(EDX,"utf-8"))        || (streqrp(EDX,"utf8"))        return CH_UTF8;
149
	else if (streqrp(EDX,"windows-1251")) || (streqrp(EDX,"windows1251")) return CH_CP1251;
150
	else if (streqrp(EDX,"dos"))          || (streqrp(EDX,"cp-866"))      return CH_CP866;
151
	else if (streqrp(EDX,"iso-8859-5"))   || (streqrp(EDX,"iso8859-5"))   return CH_ISO8859_5;
152
	else if (streqrp(EDX,"koi8-r"))       || (streqrp(EDX,"koi8-u"))      return CH_KOI8;
153
	return -1;
154
}
155
 
8439 leency 156
void TWebBrowser::tag_code()
157
{
158
	if (style.pre = tag.opened) {
8451 leency 159
		bg_colors.add(0xe4ffcb);
8439 leency 160
	} else {
8499 leency 161
		if (bg_colors.count>1) bg_colors.pop(); //never pop the last color
8439 leency 162
	}
163
}
164
 
165
void TWebBrowser::tag_ol_ul_dt()
166
{
167
	char type = ESBYTE[#tag.name];
168
	switch(type)
169
	{
170
		case 'd':
9263 leency 171
			if (ESBYTE[#tag.name+1]=='d') style.tag_list.upd_level(tag.opened, type);
172
			if (ESBYTE[#tag.name+1]=='t') style.tag_list.upd_level(false, 'd');
8439 leency 173
			if (tag.opened) NewLine();
174
			break;
175
		case 'u':
176
		case 'o':
9263 leency 177
			style.tag_list.upd_level(tag.opened, type);
8439 leency 178
			if (!tag.opened) && (!style.pre) NewLine();
179
	}
180
}
181
 
182
void TWebBrowser::tag_li()
183
{
184
	if (tag.opened) {
185
		if (!style.tag_list.level) style.tag_list.upd_level(1, 'u');
186
		if (!style.pre) NewLine();
187
		if (style.tag_list.order_type() == 'u') {
8492 leency 188
			strcpy(#linebuf, "\31 ");
8462 leency 189
			draw_x = style.tag_list.level * 5 - 2 * list.font_w + left_gap;
8439 leency 190
		}
191
		if (style.tag_list.order_type() == 'o') {
8492 leency 192
			sprintf(#linebuf, "%i. ", style.tag_list.inc_counter());
193
			draw_x = style.tag_list.level * 5 - 1 - strlen(#linebuf) * list.font_w + left_gap;
8439 leency 194
		}
195
	}
196
}
197
 
198
void TWebBrowser::tag_hr()
199
{
9103 leency 200
	dword hrcol = 0x00777777;
8491 leency 201
	if (tag.get_value_of("color")) hrcol = GetColor(tag.value);
202
	if (draw_x != left_gap) NewLine();
9293 leency 203
	if (secondrun) canvas.DrawBar(5+left_gap, style.cur_line_h / 2 + draw_y - 1, draw_w-10, 1, hrcol);
8500 leency 204
	draw_x++;
8439 leency 205
	NewLine();
206
	return;
207
}
208
 
209
void TWebBrowser::tag_body()
210
{
211
	t_body = tag.opened;
8444 leency 212
	if (tag.get_value_of("link"))   link_color_default = GetColor(tag.value);
213
	if (tag.get_value_of("alink"))  link_color_active = GetColor(tag.value);
214
	if (tag.get_value_of("text"))   text_colors.set(0, GetColor(tag.value));
215
	if (tag.get_value_of("bgcolor")) {
8451 leency 216
		bg_colors.set(0, GetColor(tag.value));
217
		canvas.Fill(0, bg_colors.get(0));
8439 leency 218
	}
219
	// Autodetecting encoding if no encoding was set
220
	if (tag.opened) && (custom_encoding==-1) && (cur_encoding == CH_CP866) {
221
		if (strstr(bufpointer, "\208\190")) ChangeEncoding(CH_UTF8);
222
		else if (chrnum(bufpointer, '\246')>5) ChangeEncoding(CH_CP1251);
223
	}
224
}
225
 
226
void TWebBrowser::tag_q()
227
{
8492 leency 228
	chrncat(#linebuf, '\"', sizeof(TWebBrowser.linebuf));
8439 leency 229
}
230
 
231
void TWebBrowser::tag_h1234_caption()
232
{
233
	if (ESBYTE[#tag.name+1]=='4') {
234
		NewLine();
235
		NewLine();
236
		style.h = tag.opened;
237
		style.b = tag.opened;
238
	} else {
239
		style.h = tag.opened;
240
		if (tag.opened) {
241
			if (!style.pre) NewLine();
242
			draw_y += 10;
8457 leency 243
			list.SetFont(BASIC_CHAR_W*2, 14*2, 10011001b);
8439 leency 244
			list.item_h = BASIC_LINE_H * 2 - 2;
245
			if (tag.is("h1")) style.b = true;
246
		} else {
247
			if (tag.is("h1")) style.b = false;
248
			NewLine();
8457 leency 249
			list.SetFont(BASIC_CHAR_W, 14, 10011000b);
8451 leency 250
			style.cur_line_h = list.item_h = BASIC_LINE_H;
8439 leency 251
		}
252
	}
253
}
254
 
255
 
256
void TWebBrowser::tag_img()
257
{
258
	char img_path[4096]=0;
8462 leency 259
	dword base64img;
8439 leency 260
 
8454 leency 261
	if (!tag.get_value_of("data-large-image"))
262
		if (!tag.get_value_of("data-src"))
263
			if (!tag.get_value_of("src")) return;
8439 leency 264
 
265
	if (streqrp(tag.value, "data:")) {
266
		if (!strstr(tag.value, "base64,")) goto NOIMG;
267
		EDX = EAX+7;
268
		if (ESBYTE[EDX]==' ') EDX++;
8462 leency 269
		base64img = malloc(strlen(EDX));
270
		base64_decode stdcall (EDX, base64img, strlen(EDX));
271
		img_decode stdcall (base64img, EAX, 0);
8439 leency 272
		$push eax
8462 leency 273
		free(base64img);
8439 leency 274
		$pop eax
275
		if (EAX) goto IMGOK; else goto NOIMG;
276
	}
277
 
278
	if (!strcmp(tag.value + strrchr(tag.value, '.'), "svg")) goto NOIMG;
8454 leency 279
	if (!strcmp(tag.value + strrchr(tag.value, '.'), "webp")) goto NOIMG;
280
 
8439 leency 281
	strlcpy(#img_path, tag.value, sizeof(img_path)-1);
8490 leency 282
	replace_char(#img_path, ' ', '\0', sizeof(img_path));
8439 leency 283
	get_absolute_url(#img_path, history.current());
284
 
285
	if (check_is_the_adress_local(#img_path)) {
286
		img_from_file stdcall(#img_path);
287
		if (EAX) goto IMGOK; else goto NOIMG;
288
	}
289
 
290
	if (cache.has(#img_path)) {
291
		img_decode stdcall (cache.current_buf, cache.current_size, 0);
292
		if (EAX) goto IMGOK; else goto NOIMG;
293
	} else {
294
		img_url.add(#img_path);
295
		goto NOIMG;
296
	}
297
 
298
IMGOK:
8462 leency 299
	if (RenderImage(EAX)) return;
8439 leency 300
 
301
NOIMG:
8444 leency 302
	if (tag.get_value_of("title")) || (tag.get_value_of("alt")) {
8492 leency 303
		strncpy(#img_path, tag.value, sizeof(TWebBrowser.linebuf)-3);
304
		sprintf(#linebuf, "[%s]", #img_path);
8439 leency 305
	} else {
306
		if (streqrp(#img_path, "data:")) img_path=0;
307
		replace_char(#img_path, '?', NULL, strlen(#img_path));
8492 leency 308
		img_path[sizeof(TWebBrowser.linebuf)-3] = '\0'; //prevent overflow in sprintf
309
		sprintf(#linebuf, "[%s]", #img_path+strrchr(#img_path, '/'));
310
		linebuf[50]= NULL;
8439 leency 311
	}
312
	text_colors.add(0x9A6F29);
313
	style.image = true;
8491 leency 314
	RenderTextbuf();
8439 leency 315
	style.image = false;
316
	text_colors.pop();
8462 leency 317
}
318
 
319
 
320
 
321
 
9103 leency 322
 
323
 
9263 leency 324
struct TABLE {
325
	int depth;
326
	collection_int cols;
327
} table;
9103 leency 328
 
329
 
330
int tr_pos, td_pos;
331
int row_start_y;
332
int colcount;
333
dword tallest_cell_in_row;
8492 leency 334
 
335
void TWebBrowser::tag_table_reset()
336
{
9263 leency 337
	table.depth = 0;
9103 leency 338
	colcount = 0;
339
	tr_pos = 0;
340
	td_pos = 0;
8492 leency 341
}
342
 
8462 leency 343
void TWebBrowser::tag_table()
344
{
9293 leency 345
	if (!tag.opened) {
346
		if (style.font) tag_font();
347
		if (link) tag_a();
348
		style.b = false;
349
	}
350
 
9103 leency 351
	if (tag.is("table")) {
352
		if(tag.opened) {
9263 leency 353
			table.depth++;
354
			if (table.depth==1) {
355
				colcount = 0;
356
			}
9103 leency 357
		} else {
9263 leency 358
			if (table.depth>0) {
359
				table.depth--;
360
			}
361
			if (table.depth==0) {
9103 leency 362
				draw_x = left_gap = style.tag_list.level * 5 * list.font_w + BODY_MARGIN;
363
				draw_w = list.w;
364
				draw_y = math.max(draw_y+style.cur_line_h, tallest_cell_in_row);
365
				row_start_y = draw_y = tallest_cell_in_row = draw_y;
366
				style.cur_line_h = list.item_h;
367
			}
8492 leency 368
		}
9103 leency 369
	}
9263 leency 370
	if (table.depth>1) {
9103 leency 371
		if (tag.is("tr")) && (tag.opened) NewLine();
372
		return;
373
	}
374
 
375
	if (!secondrun) {
376
		if (tag.is("tr")) {
377
			if (tag.opened) {
9263 leency 378
				table.cols.add(1);
9103 leency 379
			}
9104 leency 380
			colcount = 0;
8492 leency 381
		}
9103 leency 382
		if (tag.opened) && (tag.is("td")) || (tag.is("th")) {
383
			colcount++;
9263 leency 384
			if (colcount) table.cols.set(table.cols.count-1, colcount);
9103 leency 385
			//if (tag.get_number_of("colspan")) colcount += tag.number-1;
8492 leency 386
		}
9103 leency 387
	} else {
388
		if (tag.is("tr")) {
389
			if (tag.opened) {
390
				if (draw_x==left_gap) && (draw_y==BODY_MARGIN) {
391
					row_start_y = tallest_cell_in_row = draw_y;
392
				} else {
393
					row_start_y = tallest_cell_in_row = draw_y = draw_y + style.cur_line_h;
394
				}
395
				style.cur_line_h = list.item_h;
396
				tr_pos++;
397
				td_pos = 0;
8500 leency 398
			} else {
9104 leency 399
				draw_y = math.max(draw_y, tallest_cell_in_row);
8500 leency 400
			}
9104 leency 401
			draw_x = left_gap = style.tag_list.level * 5 * list.font_w + BODY_MARGIN;
402
			draw_w = list.w;
8492 leency 403
		}
9103 leency 404
		if (tr_pos) && (tag.is("td")) || (tag.is("th"))  {
405
			tallest_cell_in_row = math.max(draw_y+style.cur_line_h-list.item_h, tallest_cell_in_row);
406
			style.cur_line_h = list.item_h;
407
			if (tag.opened) {
9263 leency 408
				draw_w = list.w - BODY_MARGIN - BODY_MARGIN - 23 / table.cols.get(tr_pos-1);
9103 leency 409
				draw_x = left_gap = draw_w * td_pos + BODY_MARGIN;
410
				//debugval(itoa(draw_x), list.w);
411
				draw_y = row_start_y;
412
				//canvas.WriteText(draw_x, draw_y, 10001001b, 0x0000FE, itoa(draw_x), NULL);
413
				td_pos++;
414
			}
8500 leency 415
		}
9103 leency 416
	}
417
}
8500 leency 418