Subversion Repositories Kolibri OS

Rev

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