Subversion Repositories Kolibri OS

Rev

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