Subversion Repositories Kolibri OS

Rev

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