Subversion Repositories Kolibri OS

Rev

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