Subversion Repositories Kolibri OS

Rev

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