Subversion Repositories Kolibri OS

Rev

Rev 7756 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include "..\TWB\colors.h"
  2. #include "..\TWB\anchors.h"
  3. #include "..\TWB\parce_tag.h"
  4. #include "..\TWB\absolute_url.h"
  5. #include "..\TWB\unicode_tags.h"
  6. #include "..\TWB\img_cache.h"
  7. dword page_bg;
  8. dword link_color_inactive;
  9. dword link_color_active;
  10. #include "..\TWB\links.h"
  11.  
  12. enum { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT};
  13.  
  14. struct _style {
  15.         bool
  16.         b, u, s, h,
  17.         pre,
  18.         blq,
  19.         li,
  20.         li_tab,
  21.         button,
  22.         image,
  23.         align;
  24.         dword bg_color;
  25. };
  26.  
  27. struct TWebBrowser {
  28.         llist list;
  29.         _style style;
  30.         DrawBufer DrawBuf;
  31.         dword draw_y, stolbec;
  32.         int zoom;
  33.         dword o_bufpointer;
  34.         void SetPageDefaults();
  35.         void AddCharToTheLine();
  36.         void ParseHtml();
  37.         void SetStyle();
  38.         void DrawStyle();
  39.         void DrawPage();
  40.         void DrawScroller();
  41.         void NewLine();
  42.         bool CheckForLineBreak();
  43.         void BufEncode();
  44. } WB1;
  45.  
  46. char line[500];
  47.  
  48. bool link, cur_encoding, t_html, t_body;
  49.  
  50.  
  51. dword bufpointer=0;
  52. dword bufsize=0;
  53.  
  54. char header[150];
  55.  
  56. int body_magrin=6;
  57. int basic_line_h=22;
  58.  
  59. scroll_bar scroll_wv = { 15,NULL,NULL,NULL,0,2,NULL,0,0,0xeeeeee,0xBBBbbb,0xeeeeee};
  60.  
  61. //============================================================================================
  62. void TWebBrowser::DrawStyle()
  63. {
  64.         dword start_x, line_length, stolbec_len;
  65.        
  66.         if (!header)
  67.         {
  68.                 strncpy(#header, #line, sizeof(header)-1);
  69.                 line = 0;
  70.                 return;
  71.         }
  72.         if (t_html) && (!t_body) return;
  73.        
  74.         if (line)
  75.         {
  76.                 start_x = stolbec * list.font_w + body_magrin + list.x;
  77.                 stolbec_len = strlen(#line) * zoom;
  78.                 line_length = stolbec_len * list.font_w;
  79.  
  80.                 if (debug_mode) {
  81.                         DrawBuf.DrawBar(start_x, draw_y, line_length, list.item_h, 0xDDDddd);
  82.                 }
  83.  
  84.                 if (style.bg_color!=page_bg) {
  85.                         DrawBuf.DrawBar(start_x, draw_y, line_length, list.item_h, style.bg_color);
  86.                 }
  87.  
  88.                 if (style.image) {
  89.                         DrawBuf.DrawBar(start_x, draw_y, line_length, list.item_h-1, 0xF9DBCB);
  90.                 }
  91.                 if (style.button) {
  92.                         DrawBuf.DrawBar(start_x, draw_y, line_length, list.item_h - calc(zoom*2), 0xCCCccc);
  93.                         DrawBuf.DrawBar(start_x, draw_y + list.item_h - calc(zoom*2), line_length, zoom, 0x999999);
  94.                 }
  95.  
  96.                 DrawBuf.WriteText(start_x, draw_y, list.font_type, text_colors[text_color_index], #line);
  97.                 if (style.b) DrawBuf.WriteText(start_x+1, draw_y, list.font_type, text_colors[text_color_index], #line);
  98.                 if (style.s) DrawBuf.DrawBar(start_x, list.item_h / 2 - zoom + draw_y, line_length, zoom, text_colors[text_color_index]);
  99.                 if (style.u) DrawBuf.DrawBar(start_x, list.item_h - zoom - zoom + draw_y, line_length, zoom, text_colors[text_color_index]);
  100.                 if (link) {
  101.                         if (line[0]==' ') && (line[1]==NULL) {} else {
  102.                                 DrawBuf.DrawBar(start_x, draw_y + list.item_h - calc(zoom*2), line_length, zoom, link_color_inactive);
  103.                                 PageLinks.AddText(start_x, draw_y + list.y, line_length, list.item_h - calc(zoom*2), UNDERLINE, zoom);                         
  104.                         }
  105.                 }
  106.                 stolbec += stolbec_len;
  107.                 if (debug_mode) debug(#line);
  108.                 line = NULL;
  109.         }
  110. }
  111. //============================================================================================
  112. void TWebBrowser::SetPageDefaults()
  113. {
  114.         style.b = style.u = style.s = style.h = style.blq = t_html = t_body = style.pre =
  115.         style.li = link = text_color_index = text_colors[0] = style.li_tab = false;
  116.         style.align = ALIGN_LEFT;
  117.         link_color_inactive = 0x0000FF;
  118.         link_color_active = 0xFF0000;
  119.         page_bg = 0xFFFFFF;
  120.         style.bg_color = page_bg;
  121.         DrawBuf.Fill(0, page_bg);
  122.         PageLinks.Clear();
  123.         anchors.clear();
  124.         strncpy(#header, #version, sizeof(header)-1);
  125.         cur_encoding = CH_NULL;
  126.         draw_y = body_magrin;
  127.         stolbec = 0;
  128.         line = 0;
  129.         zoom = 1;
  130.         //hold original buffer
  131.         if (o_bufpointer) o_bufpointer=free(o_bufpointer);
  132.         o_bufpointer = malloc(bufsize);
  133.         memmov(o_bufpointer, bufpointer, bufsize);
  134. }
  135. //============================================================================================
  136. void TWebBrowser::AddCharToTheLine(unsigned char _char)
  137. {
  138.         dword line_len;
  139.         if (_char<=15) _char=' ';
  140.         line_len = strlen(#line);
  141.         if (!style.pre) && (_char == ' ')
  142.         {
  143.                 if (line[line_len-1]==' ') return; //no double spaces
  144.                 if (!stolbec) && (!line) return; //no paces at the beginning of the line
  145.         }
  146.         if (line_len < sizeof(line)) chrcat(#line, _char);
  147.         CheckForLineBreak();
  148. }
  149. //============================================================================================
  150. void TWebBrowser::ParseHtml(){
  151.         word bukva[2];
  152.         char unicode_symbol[10];
  153.         dword unicode_symbol_result;
  154.         dword j;
  155.         bool ignor_param=false;
  156.         int tab_len;
  157.         dword bufpos;
  158.         SetPageDefaults();
  159.         if (strstri(bufpointer, "<body")==-1) {
  160.                 t_body = true;
  161.                 if (strstri(bufpointer, "<html")==-1)  style.pre = true; //show linebreaks for a plaint text
  162.         }
  163.         for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;)
  164.         {
  165.                 bukva = ESBYTE[bufpos];
  166.                 switch (bukva)
  167.                 {
  168.                 case 0x0a:
  169.                         if (style.pre) {
  170.                                 DrawStyle();
  171.                                 NewLine();
  172.                         } else {
  173.                                 AddCharToTheLine(0x0a);
  174.                         }
  175.                         break;
  176.                 case 0x09:
  177.                         if (style.pre) {
  178.                                 tab_len = strlen(#line) + stolbec % 4;
  179.                                 if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len;
  180.                                 for (j=0; j<tab_len; j++;) chrcat(#line,' ');
  181.                         } else {
  182.                                 AddCharToTheLine(0x09);
  183.                         }
  184.                         break;
  185.                 case '&': //&nbsp; and so on
  186.                         for (j=1, unicode_symbol=0; (ESBYTE[bufpos+j]<>';') && (j<8); j++)
  187.                         {
  188.                                 bukva = ESBYTE[bufpos+j];
  189.                                 chrcat(#unicode_symbol, bukva);
  190.                         }
  191.                         if (GetUnicodeSymbol(#line, #unicode_symbol, sizeof(line)-1)) {
  192.                                 bufpos += j;
  193.                                 CheckForLineBreak();
  194.                         } else {
  195.                                 AddCharToTheLine('&');
  196.                         }
  197.                         break;
  198.                 case '<':
  199.                         bufpos++;
  200.                         if (!strncmp(bufpos,"!--",3))
  201.                         {
  202.                                 bufpos+=3;
  203.                                 while (strncmp(bufpos,"-->",3)!=0) && (bufpos < bufpointer + bufsize)
  204.                                 {
  205.                                         bufpos++;
  206.                                 }
  207.                                 bufpos+=2;
  208.                                 break;
  209.                         }
  210.                         tag.reset();
  211.                         if (ESBYTE[bufpos] == '/') {
  212.                                 tag.opened = false;
  213.                                 bufpos++;
  214.                         }
  215.  
  216.                         ignor_param=false;
  217.                         while (ESBYTE[bufpos] !='>') && (bufpos < bufpointer + bufsize) //ïîëó÷àåì òåã è åãî ïàðàìåòðû
  218.                         {
  219.                                 bukva = ESBYTE[bufpos];
  220.                                 if (bukva == '\9') || (bukva == '\x0a') || (bukva == '\x0d') bukva = ' ';
  221.                                 if (!ignor_param) && (bukva <>' ')
  222.                                 {
  223.                                         if (strlen(#tag.name)+1<sizeof(tag.name)) chrcat(#tag.name, bukva);
  224.                                 }
  225.                                 else
  226.                                 {
  227.                                         ignor_param = true;
  228.                                         if (strlen(#tag.params)+1<sizeof(tag.params)) strcat(#tag.params, #bukva);
  229.                                         //      chrncat(#tag.params, bukva, sizeof(tag.params)-1);
  230.                                 }
  231.                                 bufpos++;
  232.                         }
  233.                         strlwr(#tag.name);
  234.  
  235.                         // ignore text inside the next tags
  236.                         if (tag.is("script")) || (tag.is("style")) || (tag.is("binary")) || (tag.is("select"))  {
  237.                                 sprintf(#tag.params, "</%s>", #tag.name);
  238.                                 j = strstri(bufpos, #tag.params);
  239.                                 if (j!=-1) bufpos = j-1;
  240.                                 break;
  241.                         }
  242.  
  243.                         if (tag.name[strlen(#tag.name)-1]=='/') tag.name[strlen(#tag.name)-1]=NULL; //for br/ !!!!!!!!
  244.                         if (tag.params) tag.parse_params();
  245.  
  246.                         if (tag.name) {
  247.                                 CheckForLineBreak();
  248.                                 DrawStyle();
  249.                                 if (tag.name) SetStyle();
  250.                         }
  251.                         break;
  252.                 default:
  253.                         AddCharToTheLine(ESBYTE[bufpos]);
  254.                 }
  255.         }
  256.         DrawStyle();
  257.         NewLine();
  258.         list.count = draw_y;
  259.         list.CheckDoesValuesOkey();
  260.         anchors.current = NULL;
  261. }
  262. //============================================================================================
  263. bool TWebBrowser::CheckForLineBreak()
  264. {
  265.         int line_break_pos;
  266.         char new_line_text[4096];
  267.         if (strlen(#line)*zoom + stolbec < list.column_max) return false;
  268.         line_break_pos = strrchr(#line, ' ');
  269.         if (line_break_pos*zoom + stolbec > list.column_max) {
  270.                 line_break_pos = list.column_max/zoom - stolbec;
  271.                 while(line_break_pos) && (line[line_break_pos]!=' ') line_break_pos--;
  272.         }
  273.         if (!line_break_pos) && (strlen(#line)*zoom>list.column_max) {
  274.                 line_break_pos=list.column_max/zoom;
  275.                 if (!stolbec)&&(style.pre) draw_y-=list.item_h; //hack to fix https://prnt.sc/rk3kyt
  276.         }
  277.         strcpy(#new_line_text, #line + line_break_pos);
  278.         line[line_break_pos] = 0x00;
  279.         DrawStyle();
  280.         strcpy(#line, #new_line_text);
  281.         NewLine();
  282.         //if (strlen(#line)*zoom + stolbec > list.column_max)CheckForLineBreak();
  283.         return true;
  284. }
  285. //============================================================================================
  286. void TWebBrowser::SetStyle() {
  287.         char img_path[4096]=0;
  288.         int meta_encoding;
  289.  
  290.         dword value;
  291.  
  292.         if (value = tag.get_value_of("name=")) || (value = tag.get_value_of("id=")) {
  293.                 anchors.add(value, draw_y);
  294.                 if (anchors.current) && (streq(value, #anchors.current+1)) {
  295.                         list.first = draw_y;
  296.                         anchors.current = NULL;
  297.                 }
  298.         }      
  299.  
  300.         if (tag.is("html")) {
  301.                 t_html = tag.opened;
  302.                 return;
  303.         }
  304.         if (tag.is("title")) {
  305.                 if (tag.opened) header=NULL;
  306.                 return;
  307.         }
  308.        
  309.         if (tag.is("q"))
  310.         {
  311.                 if (tag.opened) {
  312.                         meta_encoding = strlen(#line);
  313.                         if (line[meta_encoding-1] != ' ') chrcat(#line, ' ');
  314.                         chrcat(#line, '\"');
  315.                 }
  316.                 if (!tag.opened) strcat(#line, "\" ");
  317.                 return;
  318.         }
  319.         if (tag.is("body")) {
  320.                 t_body = tag.opened;
  321.                 if (value = tag.get_value_of("link="))  link_color_inactive = GetColor(value);
  322.                 if (value = tag.get_value_of("alink=")) link_color_active = GetColor(value);
  323.                 if (value = tag.get_value_of("text="))  text_colors[0]=GetColor(value);
  324.                 if (value = tag.get_value_of("bgcolor=")) {
  325.                         style.bg_color = page_bg = GetColor(value);
  326.                         DrawBuf.Fill(0, page_bg);
  327.                 }
  328.                 if (tag.opened) {
  329.                         if (cur_encoding==CH_NULL) {
  330.                                 cur_encoding = CH_CP866;
  331.                                 //BufEncode(CH_UTF8);
  332.                                 debugln("Document has no information about encoding!");
  333.                         }
  334.                         if (!streq(#header, #version)) {
  335.                                 ChangeCharset(charsets[cur_encoding], "CP866", #header);
  336.                                 strncat(#header, " - ", sizeof(header)-1);
  337.                                 strncat(#header, #version, sizeof(header)-1);
  338.                         }
  339.                         DrawTitle(#header);
  340.                 }
  341.                 return;
  342.         }
  343.         if (tag.is("a")) {
  344.                 if (tag.opened)
  345.                 {
  346.                         if (link) IF(text_color_index > 0) text_color_index--; //åñëè ïðåäûäóùèé òåã à íå áûë çàêðûò
  347.                         if (value = tag.get_value_of("href=")) && (!strstr(value,"javascript:"))
  348.                         {
  349.                                 text_color_index++;
  350.                                 text_colors[text_color_index] = text_colors[text_color_index-1];
  351.                                 link = 1;
  352.                                 text_colors[text_color_index] = link_color_inactive;
  353.                                 PageLinks.AddLink(value);
  354.                         }
  355.                 } else {
  356.                         link = 0;
  357.                         IF(text_color_index > 0) text_color_index--;
  358.                 }
  359.                 return;
  360.         }
  361.         if (tag.is("font")) {
  362.                 style.bg_color = page_bg;
  363.                 if (tag.opened)
  364.                 {
  365.                         text_color_index++;
  366.                         text_colors[text_color_index] = text_colors[text_color_index-1];
  367.                         if (value = tag.get_value_of("color=")) text_colors[text_color_index] = GetColor(value);
  368.                         if (value = tag.get_value_of("bg=")) style.bg_color = GetColor(value);
  369.                 }
  370.                 else if (text_color_index > 0) text_color_index--;
  371.                 return;
  372.         }
  373.         if (tag.is("div")) {
  374.                 if (streq(#tag.prior,"div")) && (tag.opened) return;
  375.                 NewLine();
  376.                 return;
  377.         }
  378.         if (tag.is("header")) || (tag.is("article")) || (tag.is("footer")) || (tag.is("figure")) {
  379.                 NewLine();
  380.                 return;
  381.         }
  382.         if (tag.is("p")) {
  383.                 IF (tag.prior[0] == 'h') || (streq(#tag.prior,"td")) || (streq(#tag.prior,"p")) return;
  384.                 NewLine();
  385.                 return;
  386.         }
  387.         if (tag.is("br")) { NewLine(); return; }
  388.         if (tag.is("td")) { if (tag.opened) AddCharToTheLine(' '); return; }
  389.         if (tag.is("tr")) { if (tag.opened) NewLine(); return; }
  390.         if (tag.is("b")) || (tag.is("strong")) || (tag.is("big")) { style.b = tag.opened; return; }
  391.         if (tag.is("button")) { style.button = tag.opened; stolbec++; return; }
  392.         if (tag.is("u")) || (tag.is("ins")) { style.u=tag.opened; return;}
  393.         if (tag.is("s")) || (tag.is("strike")) || (tag.is("del")) { style.s=tag.opened; return; }
  394.         if (tag.is("dd")) { stolbec += 5; return; }
  395.         if (tag.is("blockquote")) { style.blq = tag.opened; return; }
  396.         if (tag.is("pre")) || (tag.is("code")) { style.pre = tag.opened; return; }
  397.         if (tag.is("img")) {
  398.                 if (value = tag.get_value_of("src=")) strlcpy(#img_path, value, sizeof(img_path)-1);
  399.                 if (value = tag.get_value_of("title=")) && (strlen(value)<sizeof(line)-3) && (value) sprintf(#line, "[%s]", value);
  400.                 if (value = tag.get_value_of("alt=")) && (strlen(value)<sizeof(line)-3) && (value) sprintf(#line, "[%s]", value);
  401.                 if (!img_path) { line=0; return; }
  402.                 style.image = true;
  403.                 text_color_index++;
  404.                 text_colors[text_color_index] = 0x9A6F29;
  405.                 if (!line) {
  406.                         if (!strncmp(#img_path, "data:", 5)) img_path=0;
  407.                         replace_char(#img_path, '?', NULL, strlen(#img_path));
  408.                         sprintf(#line, "[%s]", #img_path+strrchr(#img_path, '/'));
  409.                         line[50]= NULL;
  410.                 }
  411.                 while (CheckForLineBreak()) {};
  412.                 DrawStyle();
  413.                 text_color_index--;
  414.                 style.image = false;
  415.                 //ImgCache.Images( list.x, draw_y, WB1.list.w);
  416.                 return;
  417.         }
  418.         if (tag.is("h1")) || (tag.is("h2")) || (tag.is("h3")) || (tag.is("caption")) {
  419.                 style.h = tag.opened;
  420.                 if (tag.opened)
  421.                 {
  422.                         NewLine();
  423.                         draw_y += 10;
  424.                         WB1.zoom=2;
  425.                         WB1.list.font_type |= 10011001b;
  426.                         if (value = tag.get_value_of("align=")) {
  427.                                 if (streq(value, "center")) style.align = ALIGN_CENTER;
  428.                                 if (streq(value, "right")) style.align = ALIGN_RIGHT;
  429.                         }
  430.                         list.item_h = basic_line_h * 2;
  431.                         if (tag.is("h1")) style.b = true;
  432.                 }
  433.                 else
  434.                 {
  435.                         if (tag.is("h1")) style.b = false;
  436.                         NewLine();
  437.                         WB1.zoom=1;
  438.                         WB1.list.font_type = 10011000b;
  439.                         style.align = ALIGN_LEFT;
  440.                         list.item_h = basic_line_h;
  441.                 }
  442.                 return;
  443.         }
  444.         if (tag.is("dt")) {
  445.                 style.li = tag.opened;
  446.                 if (tag.opened) NewLine();
  447.                 return;
  448.         }
  449.         if (tag.is("li")) || (tag.is("dt"))
  450.         {
  451.                 style.li = tag.opened;
  452.                 if (tag.opened)
  453.                 {
  454.                         NewLine();
  455.                         stolbec = style.li_tab * 5 - 2;
  456.                         strcpy(#line, "\31 ");
  457.                         //stolbec-=2;
  458.                 }
  459.                 return;
  460.         }
  461.         if (tag.is("ul")) || (tag.is("ol")) {
  462.                 if (!tag.opened)
  463.                 {
  464.                         style.li = false;
  465.                         style.li_tab--;
  466.                         NewLine();
  467.                 }
  468.                 else style.li_tab++;
  469.         }
  470.         if (tag.is("hr")) {
  471.                 if (value = tag.get_value_of("color=")) EDI = GetColor(value); else EDI = 0x999999;
  472.                 $push edi;
  473.                 NewLine();
  474.                 $pop edi;
  475.                 draw_y += 10;
  476.                 DrawBuf.DrawBar(5, draw_y - 1, list.w-10, 1, EDI);
  477.                 NewLine();
  478.                 draw_y += 10;
  479.                 return;
  480.         }
  481.         if (tag.is("meta")) || (tag.is("?xml")) {
  482.                 meta_encoding = CH_NULL;
  483.                 if (value = tag.get_value_of("charset=")) || (value = tag.get_value_of("content=")) || (value = tag.get_value_of("encoding="))
  484.                 {
  485.                         value += strrchr(value, '='); //search in content=
  486.                         strlwr(value);
  487.                         if      (streq(value,"utf-8"))        || (streq(value,"utf8"))        meta_encoding = CH_UTF8;
  488.                         else if (streq(value,"windows-1251")) || (streq(value,"windows1251")) meta_encoding = CH_CP1251;
  489.                         else if (streq(value,"dos"))          || (streq(value,"cp-866"))      meta_encoding = CH_CP866;
  490.                         else if (streq(value,"iso-8859-5"))   || (streq(value,"iso8859-5"))   meta_encoding = CH_ISO8859_5;
  491.                         else if (streq(value,"koi8-r"))       || (streq(value,"koi8-u"))      meta_encoding = CH_KOI8;
  492.                 }
  493.                 if (meta_encoding!=CH_NULL) BufEncode(meta_encoding);
  494.                 return;
  495.         }
  496. }
  497. //============================================================================================
  498. void TWebBrowser::BufEncode(dword set_new_encoding)
  499. {
  500.         if (cur_encoding != set_new_encoding) {
  501.                 cur_encoding = set_new_encoding;
  502.                 bufpointer = ChangeCharset(charsets[cur_encoding], "CP866", bufpointer);               
  503.         }
  504. }
  505. //============================================================================================
  506. void TWebBrowser::DrawScroller()
  507. {
  508.         scroll_wv.max_area = list.count;
  509.         scroll_wv.cur_area = list.visible;
  510.         scroll_wv.position = list.first;
  511.         scroll_wv.all_redraw = 0;
  512.         scroll_wv.start_x = list.x + list.w;
  513.         scroll_wv.start_y = list.y;
  514.         scroll_wv.size_y = list.h;
  515.         scrollbar_v_draw(#scroll_wv);
  516. }
  517. //============================================================================================
  518. void TWebBrowser::NewLine()
  519. {
  520.         dword onleft, ontop;
  521.         static int empty_line=0;
  522.  
  523.         if (!stolbec) && (draw_y==body_magrin) return;
  524.        
  525.         if (style.li) && (stolbec == style.li_tab * 5) {
  526.                 if (empty_line<1) empty_line++;
  527.                 else return;
  528.         } else if (!stolbec) {
  529.                 if (empty_line<1) empty_line++;
  530.                 else return;
  531.         } else {
  532.                 empty_line=0;
  533.         }
  534.  
  535.         onleft = list.x + body_magrin;
  536.         ontop = draw_y + list.y;
  537.         if (t_html) && (!t_body) return;
  538.         draw_y += list.item_h;
  539.         if (style.blq) stolbec = 6; else stolbec = 0;
  540.         if (style.li) stolbec = style.li_tab * 5;
  541.         if (debug_mode) debugln(NULL);
  542. }
  543. //============================================================================================
  544. void TWebBrowser::DrawPage()
  545. {
  546.         PutPaletteImage(list.first * DrawBuf.bufw * 4 + buf_data+8, DrawBuf.bufw, list.h, DrawBuf.bufx, DrawBuf.bufy, 32, 0);  
  547.         DrawScroller();
  548. }