Subversion Repositories Kolibri OS

Rev

Rev 6058 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. void PreparePage()
  3. {
  4.         list.SetSizes(0, TOOLBAR_H, Form.cwidth-scroll.size_x-1, Form.cheight-TOOLBAR_H, label.size.pt+2);
  5.         strcpy(#title, history.current()+strrchr(history.current(),'/'));
  6.         //get font chars width, need to increase performance
  7.         get_label_symbols_size();
  8.         ChangeCharset(charsets[encoding], "CP866", io.buffer_data);
  9.         link.clear();
  10.         if (strstri(io.buffer_data, "<html")==-1) {
  11.                 debugln("no <html> found");
  12.                 DrawProgress(STEP_2_COUNT_PAGE_HEIGHT);     ParceTxt(false);   //get page height to calculate buffer size
  13.                 DrawProgress(STEP_3_DRAW_PAGE_INTO_BUFFER); ParceTxt(true);    //draw text in buffer
  14.         } else {
  15.                 debugln("<html> tag found");
  16.                 DrawProgress(STEP_2_COUNT_PAGE_HEIGHT);     ParceHtml(false);  //get page height to calculate buffer size
  17.                 DrawProgress(STEP_3_DRAW_PAGE_INTO_BUFFER); ParceHtml(true);   //draw text in buffer
  18.         }
  19.         strcat(#title, " - Aelia");
  20.         DrawTitle(#title);
  21.         DrawProgress(STEP_4_SMOOTH_FONT);           label.apply_smooth();
  22.         DrawProgress(STEP_5_STOP);                  DrawPage();
  23. }
  24.  
  25. void ParceTxt(byte draw)
  26. {
  27. byte ch, zeroch=0;
  28. dword bufoff, buflen, line_start, srch_pos;
  29. int stroka_y=5, line_length=0;
  30.  
  31.         line_start=io.buffer_data;
  32.         buflen = strlen(io.buffer_data) + io.buffer_data;
  33.         for (bufoff=io.buffer_data; bufoff<buflen; bufoff++)
  34.         {
  35.                 ch = ESBYTE[bufoff];
  36.                 line_length += char_width[ch];
  37.                 if (line_length>=list.w-30) || (ch==10) {
  38.                         srch_pos = bufoff;
  39.                         loop()
  40.                         {
  41.                                 if (__isWhite(ESBYTE[srch_pos])) { bufoff=srch_pos+1; break; } //normal word-break
  42.                                 if (srch_pos == line_start) break; //no white space found in whole line
  43.                                 srch_pos--;
  44.                         }
  45.                         if (draw==true) {
  46.                                 ESBYTE[bufoff] >< zeroch; //set line end
  47.                                 WriteTextIntoBuf(8, stroka_y, 0x000000, line_start);
  48.                                 ESBYTE[bufoff] >< zeroch; //restore line
  49.                         }
  50.                         stroka_y += list.item_h;
  51.                         line_start = bufoff;
  52.                         line_length = 0;
  53.                 }
  54.         }
  55.         if (draw==false) {
  56.                 list.count = stroka_y/list.item_h+3;
  57.                 if (list.count < list.visible) list.count = list.visible;
  58.                 label.size.height = list.count+5*list.item_h;
  59.                 label.raw_size = 0;
  60.         }
  61.         if (draw==true) WriteTextIntoBuf(8, stroka_y, 0x000000, line_start);
  62. }
  63.  
  64.  
  65. /*========================================================
  66. =                                                        =
  67. =                        HTML                            =
  68. =                                                        =
  69. ========================================================*/
  70.  
  71. /* <title>   <meta encoding> <a hrf=""> <img src="" alt=""> <h1>..<h6> <b> <u> <s> <pre> */
  72.  
  73. struct _DOM {
  74.         dword start;
  75.         dword end;
  76.         dword len;
  77. };
  78.  
  79. struct _style {
  80.         bool b, u, i, s;
  81.         bool h1, h2, h3, h4, h5, h6;
  82.         bool a;
  83.         bool pre;
  84.         bool ignore;
  85.         dword color;
  86.         void clear();
  87. } style;
  88.  
  89. void _style::clear()
  90. {
  91.         b=u=i=s=0;
  92.         h1=h2=h3=h4=h5=h6=0;
  93.         a=0;
  94.         pre=0;
  95.         ignore=0;
  96.         color=0;
  97. }
  98.  
  99. struct _text {
  100.         dword start;
  101.         int x, y;
  102. };
  103.  
  104. struct _tag {
  105.         dword start;
  106.         dword name;
  107.         dword param[10];
  108.         dword value[10];
  109.         void parce();
  110.         int nameis();
  111.         void clear();
  112. };
  113.  
  114. void _tag::parce()
  115. {
  116.         dword o = name = start;
  117.         while (ESBYTE[o]!=' ') && (ESBYTE[o]) o++; //searching for a space after tag name
  118.         ESBYTE[o] = '\0';
  119.         strlwr(name);
  120. }
  121.  
  122. int _tag::nameis(dword _in_tag_name)
  123. {
  124.         if (name) && (strcmp(_in_tag_name, name)==0) return true;
  125.         return false;
  126. }
  127.  
  128. void _tag::clear()
  129. {
  130.         start=name=0;
  131. }
  132.  
  133. #define HTML_PADDING_X 8;
  134. #define HTML_PADDING_Y 5;
  135.  
  136.  
  137. void ParceHtml(byte draw)
  138. {
  139. int stroka_x = HTML_PADDING_X;
  140. int stroka_y = HTML_PADDING_Y;
  141. dword line_break;
  142. byte ch, zeroch;
  143. _DOM DOM;
  144. _text text;
  145. _tag tag;
  146. dword DOM_pos;
  147.  
  148.         tag.clear();
  149.         style.clear();
  150.         /* Create DOM */
  151.         debugln("creating DOM");
  152.         DOM.len = strlen(io.buffer_data);
  153.         DOM.start = malloc(DOM.len);
  154.         DOM.end = DOM.start + DOM.len;
  155.         strlcpy(DOM.start, io.buffer_data, DOM.len);
  156.         //RemoveSpecialSymbols(DOM.start, DOM.len);
  157.         //DOM.len = strlen(DOM.start);
  158.  
  159.         /* Parce DOM */
  160.         debugln("starting DOM parce...");
  161.         text.start = DOM.start;
  162.         for (DOM_pos=DOM.start; DOM_pos<DOM.end; DOM_pos++)
  163.         {
  164.                 if (ESBYTE[DOM_pos]==0x0D) || (ESBYTE[DOM_pos]==0x0A) ESBYTE[DOM_pos]=' ';
  165.                 ch = ESBYTE[DOM_pos];
  166.                 if (ch=='<') {
  167.                         ESBYTE[DOM_pos] = '\0';
  168.                         tag.start = DOM_pos + 1;
  169.                         if (style.ignore) continue;
  170.                         if (tag.nameis("title")) {
  171.                                 strcpy(#title, text.start);
  172.                                 continue;
  173.                         }
  174.                         strtrim(text.start);
  175.                         while (get_label_len(text.start) + stroka_x + 30 > list.w)
  176.                         {
  177.                                 zeroch = 0;
  178.                                 for (line_break=tag.start-1; line_break>text.start; line_break--;)
  179.                                 {
  180.                                         ESBYTE[line_break] >< zeroch; //set line end
  181.                                         if (get_label_len(text.start) + stroka_x + 30 <= list.w) break;
  182.                                         ESBYTE[line_break] >< zeroch; //restore line
  183.                                 }
  184.                                 if (draw==true) {
  185.                                         if (style.a) {
  186.                                                 link.add(stroka_x,stroka_y,get_label_len(text.start),list.item_h,text.start," ");
  187.                                                 label_draw_bar(stroka_x, stroka_y+label.size.pt+1, get_label_len(text.start), style.color);
  188.                                         }
  189.                                         WriteTextIntoBuf(stroka_x, stroka_y, style.color, text.start);
  190.                                         stroka_x+=char_width[' '];
  191.                                 }
  192.                                 ESBYTE[line_break] >< zeroch; //restore line
  193.                                 text.start = line_break;
  194.                                 stroka_x = HTML_PADDING_X;
  195.                                 stroka_y += list.item_h;
  196.                         }
  197.                         if (draw==true) {
  198.                                 if (style.a) {
  199.                                         link.add(stroka_x,stroka_y,get_label_len(text.start),list.item_h,text.start," ");      
  200.                                         label_draw_bar(stroka_x, stroka_y+label.size.pt+1, get_label_len(text.start), style.color);
  201.                                 }
  202.                                 WriteTextIntoBuf(stroka_x, stroka_y, style.color, text.start);
  203.                                 stroka_x+=char_width[' '];
  204.                         }
  205.                         stroka_x += get_label_len(text.start);
  206.                 }
  207.                 if (ch=='>') {
  208.                         ESBYTE[DOM_pos] = '\0';
  209.                         text.start = DOM_pos + 1;
  210.                         tag.parce();
  211.                         if (tag.nameis("br"))
  212.                                 || (tag.nameis("p"))
  213.                                 || (tag.nameis("div"))
  214.                                 || (tag.nameis("tr")) {
  215.                                 stroka_y+= list.item_h;
  216.                                 stroka_x = HTML_PADDING_X;
  217.                                 continue;
  218.                         }
  219.                         if      (tag.nameis("h1")) || (tag.nameis("/h1"))
  220.                                 || (tag.nameis("h2")) || (tag.nameis("/h2")) {
  221.                                         stroka_y+= list.item_h;
  222.                                         stroka_x = HTML_PADDING_X;
  223.                                         continue;                                      
  224.                                 }
  225.                         if (tag.nameis("script")) || (tag.nameis("style")) style.ignore = true;
  226.                         if (tag.nameis("/script")) || (tag.nameis("/style")) style.ignore = false;
  227.                         if (tag.nameis("a"))  { style.a = true;  style.color=0x0000FF; }
  228.                         if (tag.nameis("/a")) { style.a = false; style.color=0x000000; }
  229.                 }              
  230.         }
  231.         if (draw==false) {
  232.                 list.count = stroka_y/list.item_h+3;
  233.                 if (list.count < list.visible) list.count = list.visible;
  234.                 label.size.height = list.count+5*list.item_h;
  235.                 label.raw_size = 0;
  236.         }
  237.         free(DOM.start);
  238. }