Subversion Repositories Kolibri OS

Rev

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