Subversion Repositories Kolibri OS

Rev

Rev 8500 | Rev 9104 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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