Subversion Repositories Kolibri OS

Rev

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