Subversion Repositories Kolibri OS

Compare Revisions

Regard whitespace Rev 7751 → Rev 7752

/programs/cmm/TWB/TWB.c
1,6 → 1,10
#include "..\TWB\colors.h"
#include "..\TWB\anchors.h"
#include "..\TWB\parce_tag.h"
#include "..\TWB\absolute_url.h"
char line[500];
#include "..\TWB\unicode_tags.h"
 
scroll_bar scroll_wv = { 15,200,398,44,0,2,115,15,0,0xeeeeee,0xBBBbbb,0xeeeeee,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1};
 
enum { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT};
 
struct _style {
13,8 → 17,7
button,
image,
align;
dword
bg_color;
dword bg_color;
};
 
struct TWebBrowser {
21,10 → 24,12
llist list;
_style style;
DrawBufer DrawBuf;
dword draw_y, stolbec;
int zoom;
bool opened; //is this a "start tag" or "end tag"
dword o_bufpointer;
void SetPageDefaults();
void Prepare();
void AddCharToTheLine();
void ParseHtml();
void SetStyle();
void DrawStyle();
void DrawPage();
34,6 → 39,9
void BufEncode();
} WB1;
 
dword link_color_inactive;
dword link_color_active;
dword page_bg;
 
bool
link,
41,41 → 49,20
t_html,
t_body;
 
#include "..\TWB\img_cache.h"
#include "..\TWB\links.h"
 
dword bufpointer=0;
dword o_bufpointer=0;
dword bufsize=0;
 
dword text_colors[300];
dword text_color_index;
dword link_color_inactive;
dword link_color_active;
dword page_bg;
 
int draw_y;
int stolbec;
int tab_len;
 
int body_magrin=5;
int body_magrin=6;
int basic_line_h=22;
 
char URL[10000];
char header[2048];
char line[500];
char tagparam[10000];
char tag[100];
char header[150];
char oldtag[100];
char attr[1200];
char val[4096];
 
#include "..\TWB\absolute_url.h"
#include "..\TWB\links.h"
#include "..\TWB\anchors.h"
#include "..\TWB\colors.h"
#include "..\TWB\unicode_tags.h"
#include "..\TWB\img_cache.h"
#include "..\TWB\parce_tag.h"
scroll_bar scroll_wv = { 15,200,398,44,0,2,115,15,0,0xeeeeee,0xBBBbbb,0xeeeeee,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1};
 
 
//============================================================================================
void TWebBrowser::DrawStyle()
{
83,7 → 70,7
if (!header)
{
strcpy(#header, #line);
strncpy(#header, #line, sizeof(header)-1);
line = 0;
return;
}
138,20 → 125,39
style.bg_color = page_bg;
DrawBuf.Fill(0, page_bg);
PageLinks.Clear();
strcpy(#header, #version);
anchors.clear();
strncpy(#header, #version, sizeof(header)-1);
cur_encoding = CH_NULL;
draw_y = body_magrin;
stolbec = 0;
line = 0;
zoom = 1;
if (o_bufpointer) free(o_bufpointer);
o_bufpointer = 0;
}
//============================================================================================
void TWebBrowser::Prepare(){
void TWebBrowser::AddCharToTheLine(unsigned char _char)
{
dword line_len;
if (_char<=15) _char=' ';
line_len = strlen(#line);
if (!style.pre) && (_char == ' ')
{
if (line[line_len-1]==' ') return; //no double spaces
if (!stolbec) && (!line) return; //no paces at the beginning of the line
}
if (line_len < sizeof(line)) chrcat(#line, _char);
CheckForLineBreak();
}
//============================================================================================
void TWebBrowser::ParseHtml(){
word bukva[2];
char unicode_symbol[10];
dword unicode_symbol_result;
dword j;
bool ignor_param;
bool ignor_param=false;
int tab_len;
dword bufpos;
dword line_len;
SetPageDefaults();
if (strstri(bufpointer, "<body")==-1) {
t_body = true;
163,34 → 169,33
switch (bukva)
{
case 0x0a:
if (style.pre)
{
if (style.pre) {
DrawStyle();
NewLine();
} else {
AddCharToTheLine(0x0a);
}
break;
case 0x09:
if (style.pre) {
tab_len = strlen(#line) + stolbec % 4;
if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len;
for (j=0; j<tab_len; j++;) chrcat(#line,' ');
} else {
AddCharToTheLine(0x09);
}
goto DEFAULT_MARK;
case '\9':
if (style.pre) //otherwise go to 0x0d
{
tab_len = strlen(#line) % 4;
if (!tab_len) tab_len = 4;
for (j=0; j<tab_len; j++;) chrcat(#line,' ');
break;
}
goto DEFAULT_MARK;
case '&': //&nbsp; and so on
for (j=1, tag=0; (ESBYTE[bufpos+j]<>';') && (j<8); j++)
for (j=1, unicode_symbol=0; (ESBYTE[bufpos+j]<>';') && (j<8); j++)
{
bukva = ESBYTE[bufpos+j];
chrcat(#tag, bukva);
chrcat(#unicode_symbol, bukva);
}
if (bukva = GetUnicodeSymbol(#tag)) {
if (bukva = GetUnicodeSymbol(#unicode_symbol)) {
bufpos += j;
CheckForLineBreak();
} else {
bukva = '&';
goto DEFAULT_MARK;
AddCharToTheLine('&');
}
break;
case '<':
205,7 → 210,13
bufpos+=2;
break;
}
tag = attr = tagparam = ignor_param = NULL;
tag.reset();
if (ESBYTE[bufpos] == '/') {
tag.opened = false;
bufpos++;
}
 
ignor_param=false;
while (ESBYTE[bufpos] !='>') && (bufpos < bufpointer + bufsize) //ïîëó÷àåì òåã è åãî ïàðàìåòðû
{
bukva = ESBYTE[bufpos];
212,63 → 223,43
if (bukva == '\9') || (bukva == '\x0a') || (bukva == '\x0d') bukva = ' ';
if (!ignor_param) && (bukva <>' ')
{
if (strlen(#tag)+1<sizeof(tag)) chrcat(#tag, bukva);
if (strlen(#tag.name)+1<sizeof(tag.name)) chrcat(#tag.name, bukva);
}
else
{
ignor_param = true;
if (strlen(#tagparam)+1<sizeof(tagparam)) strcat(#tagparam, #bukva);
// chrncat(#tagparam, bukva, sizeof(tagparam)-1);
if (strlen(#tag.params)+1<sizeof(tag.params)) strcat(#tag.params, #bukva);
// chrncat(#tag.params, bukva, sizeof(tag.params)-1);
}
bufpos++;
}
strlwr(#tag);
strlwr(#tag.name);
 
// ignore text inside the next tags
if (istag("script")) || (istag("style")) || (istag("binary")) || (istag("select")) {
sprintf(#tagparam, "</%s>", #tag);
j = strstri(bufpos, #tagparam);
if (j!=-1) {
bufpos = j-1;
}
tag = tagparam = NULL;
if (tag.is("script")) || (tag.is("style")) || (tag.is("binary")) || (tag.is("select")) {
sprintf(#tag.params, "</%s>", #tag.name);
j = strstri(bufpos, #tag.params);
if (j!=-1) bufpos = j-1;
break;
}
 
if (tag[strlen(#tag)-1]=='/') tag[strlen(#tag)-1]=NULL; //for br/
if (tagparam) GetNextParam();
if (tag.name[strlen(#tag.name)-1]=='/') tag.name[strlen(#tag.name)-1]=NULL; //for br/ !!!!!!!!
if (tag.params) tag.parse_params();
 
if (tag[0] == '/')
{
opened = 0;
strcpy(#tag, #tag+1);
}
else opened = 1;
 
if (tag) && (!istag("span")) && (!istag("i")) && (!istag("svg")) {
if (tag.name) && (!tag.is("i")) && (!tag.is("svg")) {
CheckForLineBreak();
DrawStyle();
if (tag) SetStyle();
if (tag.name) SetStyle();
}
strlcpy(#oldtag, #tag, sizeof(oldtag)-1);
tag = attr = tagparam = ignor_param = NULL;
strncpy(#oldtag, #tag.name, sizeof(oldtag)-1);
break;
default:
DEFAULT_MARK:
if (bukva<=15) bukva=' ';
line_len = strlen(#line);
if (!style.pre) && (bukva == ' ')
{
if (line[line_len-1]==' ') break; //no double spaces
if (!stolbec) && (!line) break; //no paces at the beginning of the line
AddCharToTheLine(ESBYTE[bufpos]);
}
if (line_len < sizeof(line)) chrcat(#line, bukva);
CheckForLineBreak();
}
}
DrawStyle();
NewLine();
if (list.first == 0) list.count = draw_y;
list.count = draw_y;
DrawPage();
}
//============================================================================================
297,122 → 288,115
//============================================================================================
void TWebBrowser::SetStyle() {
char img_path[4096]=0;
int left1 = body_magrin + list.x;
int meta_encoding;
if (istag("html")) {
t_html = opened;
 
dword value;
 
if (tag.is("html")) {
t_html = tag.opened;
return;
}
if(istag("title")) {
if (opened) header=NULL;
if(tag.is("title")) {
if (tag.opened) header=NULL;
return;
}
IF(istag("q"))
IF(tag.is("q"))
{
if (opened) {
if (tag.opened) {
meta_encoding = strlen(#line);
if (line[meta_encoding-1] != ' ') chrcat(#line, ' ');
chrcat(#line, '\"');
}
if (!opened) strcat(#line, "\" ");
if (!tag.opened) strcat(#line, "\" ");
return;
}
if (isattr("id=")) || (isattr("name=")) { // TO FIX: works only if the param is the last
anchors.add(#val, draw_y);
if (value = tag.get_value_of("name=")) || (value = tag.get_value_of("id=")) {
anchors.add(value, draw_y);
}
if (istag("body")) {
t_body = opened;
do{
if (isattr("link=")) link_color_inactive = GetColor(#val);
if (isattr("alink=")) link_color_active = GetColor(#val);
if (isattr("text=")) text_colors[0]=GetColor(#val);
if (isattr("bgcolor="))
{
style.bg_color = page_bg = GetColor(#val);
if (tag.is("body")) {
t_body = tag.opened;
if (value = tag.get_value_of("link=")) link_color_inactive = GetColor(value);
if (value = tag.get_value_of("alink=")) link_color_active = GetColor(value);
if (value = tag.get_value_of("text=")) text_colors[0]=GetColor(value);
if (value = tag.get_value_of("bgcolor=")) {
style.bg_color = page_bg = GetColor(value);
DrawBuf.Fill(0, page_bg);
}
} while(GetNextParam());
if (opened) && (cur_encoding==CH_NULL) {
if (tag.opened) {
if (cur_encoding==CH_NULL) {
cur_encoding = CH_CP866;
//BufEncode(CH_UTF8);
debugln("Document has no information about encoding!");
}
if (opened) {
if (strcmp(#header, #version) != 0) {
if (!streq(#header, #version)) {
ChangeCharset(charsets[cur_encoding], "CP866", #header);
sprintf(#header, "%s - %s", #header, #version);
strncat(#header, " - ", sizeof(header)-1);
strncat(#header, #version, sizeof(header)-1);
}
DrawTitle(#header);
}
return;
}
if (istag("a")) {
if (opened)
if (tag.is("a")) {
if (tag.opened)
{
if (link) IF(text_color_index > 0) text_color_index--; //åñëè ïðåäûäóùèé òåã à íå áûë çàêðûò
do{
if (isattr("href=")) && (!strstr(#val,"javascript:"))
if (value = tag.get_value_of("href=")) && (!strstr(value,"javascript:"))
{
text_color_index++;
text_colors[text_color_index] = text_colors[text_color_index-1];
link = 1;
text_colors[text_color_index] = link_color_inactive;
PageLinks.AddLink(#val);
PageLinks.AddLink(value);
}
} while(GetNextParam());
}
else {
} else {
link = 0;
IF(text_color_index > 0) text_color_index--;
}
return;
}
if (istag("font")) {
if (tag.is("font")) {
style.bg_color = page_bg;
if (opened)
if (tag.opened)
{
text_color_index++;
text_colors[text_color_index] = text_colors[text_color_index-1];
do{
if (isattr("color=")) text_colors[text_color_index] = GetColor(#val);
if (isattr("bg=")) style.bg_color = GetColor(#val);
} while(GetNextParam());
if (value = tag.get_value_of("color=")) text_colors[text_color_index] = GetColor(value);
if (value = tag.get_value_of("bg=")) style.bg_color = GetColor(value);
}
else if (text_color_index > 0) text_color_index--;
return;
}
if (istag("div")) {
if (streq(#oldtag,"div")) && (opened) return;
if (tag.is("div")) {
if (streq(#oldtag,"div")) && (tag.opened) return;
NewLine();
//IF (oldtag[0] != 'h')
return;
}
if (istag("header")) || (istag("article")) || (istag("footer")) || (istag("figure")) {
if (tag.is("header")) || (tag.is("article")) || (tag.is("footer")) || (tag.is("figure")) {
NewLine();
return;
}
if (istag("p")) {
if (tag.is("p")) {
IF (oldtag[0] == 'h') || (streq(#oldtag,"td")) || (streq(#oldtag,"p")) return;
NewLine();
//IF(opened) NewLine();
//IF(tag.opened) NewLine();
return;
}
if (istag("br")) { NewLine(); return; }
if (istag("tr")) { if (opened) NewLine(); return; }
if (istag("b")) || (istag("strong")) || (istag("big")) { style.b = opened; return; }
if (istag("button")) { style.button = opened; stolbec++; return; }
if (istag("u")) || (istag("ins")) { style.u=opened; return;}
if (istag("s")) || (istag("strike")) || (istag("del")) { style.s=opened; return; }
if (istag("dd")) { stolbec += 5; return; }
if (istag("blockquote")) { style.blq = opened; return; }
if (istag("pre")) || (istag("code")) { style.pre = opened; return; }
if (istag("img")) {
do{
if (isattr("src=")) strlcpy(#img_path, #val, sizeof(img_path)-1);
if (isattr("title=")) && (strlen(#val)<sizeof(line)-3) && (val) sprintf(#line, "[%s]", #val);
if (isattr("alt=")) && (strlen(#val)<sizeof(line)-3) && (val) sprintf(#line, "[%s]", #val);
} while(GetNextParam());
if (tag.is("br")) { NewLine(); return; }
if (tag.is("tr")) { if (tag.opened) NewLine(); return; }
if (tag.is("b")) || (tag.is("strong")) || (tag.is("big")) { style.b = tag.opened; return; }
if (tag.is("button")) { style.button = tag.opened; stolbec++; return; }
if (tag.is("u")) || (tag.is("ins")) { style.u=tag.opened; return;}
if (tag.is("s")) || (tag.is("strike")) || (tag.is("del")) { style.s=tag.opened; return; }
if (tag.is("dd")) { stolbec += 5; return; }
if (tag.is("blockquote")) { style.blq = tag.opened; return; }
if (tag.is("pre")) || (tag.is("code")) { style.pre = tag.opened; return; }
if (tag.is("img")) {
if (value = tag.get_value_of("src=")) strlcpy(#img_path, value, sizeof(img_path)-1);
if (value = tag.get_value_of("title=")) && (strlen(value)<sizeof(line)-3) && (value) sprintf(#line, "[%s]", value);
if (value = tag.get_value_of("alt=")) && (strlen(value)<sizeof(line)-3) && (value) sprintf(#line, "[%s]", value);
if (!img_path) { line=0; return; }
style.image = true;
text_color_index++;
427,25 → 411,27
DrawStyle();
text_color_index--;
style.image = false;
//ImgCache.Images( left1, draw_y, WB1.list.w);
//ImgCache.Images( list.x, draw_y, WB1.list.w);
return;
}
if (istag("h1")) || (istag("h2")) || (istag("h3")) || (istag("caption")) {
style.h = opened;
if (opened)
if (tag.is("h1")) || (tag.is("h2")) || (tag.is("h3")) || (tag.is("caption")) {
style.h = tag.opened;
if (tag.opened)
{
NewLine();
draw_y += 10;
WB1.zoom=2;
WB1.list.font_type |= 10011001b;
if (isattr("align=")) && (isval("center")) style.align = ALIGN_CENTER;
if (isattr("align=")) && (isval("right")) style.align = ALIGN_RIGHT;
if (value = tag.get_value_of("align=")) {
if (streq(value, "center")) style.align = ALIGN_CENTER;
if (streq(value, "right")) style.align = ALIGN_RIGHT;
}
list.item_h = basic_line_h * 2;
if (istag("h1")) style.b = true;
if (tag.is("h1")) style.b = true;
}
else
{
if (istag("h1")) style.b = false;
if (tag.is("h1")) style.b = false;
NewLine();
WB1.zoom=1;
WB1.list.font_type = 10011000b;
454,15 → 440,15
}
return;
}
if (istag("dt")) {
style.li = opened;
if (opened) NewLine();
if (tag.is("dt")) {
style.li = tag.opened;
if (tag.opened) NewLine();
return;
}
if (istag("li")) || (istag("dt"))
if (tag.is("li")) || (tag.is("dt"))
{
style.li = opened;
if (opened)
style.li = tag.opened;
if (tag.opened)
{
NewLine();
stolbec = style.li_tab * 5 - 2;
471,17 → 457,17
}
return;
}
if (istag("ul")) || (istag("ol")) {
if (!opened)
if (tag.is("ul")) || (tag.is("ol")) {
if (!tag.opened)
{
style.li = opened;
style.li = tag.opened;
style.li_tab--;
NewLine();
}
else style.li_tab++;
}
if (istag("hr")) {
if (isattr("color=")) EDI = GetColor(#val); else EDI = 0x999999;
if (tag.is("hr")) {
if (value = tag.get_value_of("color=")) EDI = GetColor(value); else EDI = 0x999999;
$push edi;
NewLine();
$pop edi;
491,20 → 477,18
draw_y += 10;
return;
}
if (istag("meta")) || (istag("?xml")) {
if (tag.is("meta")) || (tag.is("?xml")) {
meta_encoding = CH_NULL;
do{
if (isattr("charset=")) || (isattr("content=")) || (isattr("encoding="))
if (value = tag.get_value_of("charset=")) || (value = tag.get_value_of("content=")) || (value = tag.get_value_of("encoding="))
{
strcpy(#val, #val[strrchr(#val, '=')]); //search in content=
strlwr(#val);
if (isval("utf-8")) || (isval("utf8")) meta_encoding = CH_UTF8;
else if (isval("koi8-r")) || (isval("koi8-u")) meta_encoding = CH_KOI8;
else if (isval("windows-1251")) || (isval("windows1251")) meta_encoding = CH_CP1251;
else if (isval("iso-8859-5")) || (isval("iso8859-5")) meta_encoding = CH_ISO8859_5;
else if (isval("dos")) || (isval("cp-866")) meta_encoding = CH_CP866;
value += strrchr(value, '='); //search in content=
strlwr(value);
if (streq(value,"utf-8")) || (streq(value,"utf8")) meta_encoding = CH_UTF8;
else if (streq(value,"windows-1251")) || (streq(value,"windows1251")) meta_encoding = CH_CP1251;
else if (streq(value,"dos")) || (streq(value,"cp-866")) meta_encoding = CH_CP866;
else if (streq(value,"iso-8859-5")) || (streq(value,"iso8859-5")) meta_encoding = CH_ISO8859_5;
else if (streq(value,"koi8-r")) || (streq(value,"koi8-u")) meta_encoding = CH_KOI8;
}
} while(GetNextParam());
if (meta_encoding!=CH_NULL) BufEncode(meta_encoding);
return;
}
513,13 → 497,10
void TWebBrowser::BufEncode(dword set_new_encoding)
{
if (cur_encoding == set_new_encoding) return;
if (o_bufpointer==0)
{
if (o_bufpointer==0) {
o_bufpointer = malloc(bufsize);
strcpy(o_bufpointer, bufpointer);
}
else
{
} else {
strcpy(bufpointer, o_bufpointer);
}
//debugval("cur_encoding ", cur_encoding);
566,10 → 547,6
if (debug_mode) debugln(NULL);
}
//============================================================================================
bool istag(dword text) { if (!strcmp(#tag,text)) return true; else return false; }
bool isattr(dword text) { if (!strcmp(#attr,text)) return true; else return false; }
bool isval(dword text) { if (!strcmp(#val,text)) return true; else return false; }
//============================================================================================
void TWebBrowser::DrawPage()
{
PutPaletteImage(list.first * DrawBuf.bufw * 4 + buf_data+8, DrawBuf.bufw, list.h, DrawBuf.bufx, DrawBuf.bufy, 32, 0);