Subversion Repositories Kolibri OS

Rev

Rev 6997 | Rev 7769 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. // Сам шрифт представляет.
  2.  
  3. // Голова:
  4. // [2 байта символы:KF]
  5. // [4 байта:указатель на название шрифта]
  6. // [1 байт:размер массива указателей на размеры шрифтов, указатель 4 байта, т.е. размер = размер массива*4]
  7. // [размер массива*4 байт:указатели..]
  8.  
  9. // Тело файла:
  10. // [4 байта:масштаб ширина][4 байта:масштаб высота][255*4 байт:указатели на символы][масштаб ширина*масштаб высота байт: данные символов..]
  11.  
  12. // Конец:
  13. // [Название шрифта:"Times New Roman"]
  14.  
  15.  
  16. #ifndef INCLUDE_LABEL_H
  17. #define INCLUDE_LABEL_H
  18.  
  19. #ifndef INCLUDE_MATH_H
  20. #include "../lib/math.h"
  21. #endif
  22.  
  23. #ifndef INCLUDE_IO_H
  24. #include "../lib/io.h"
  25. #endif
  26.  
  27. #include "../lib/patterns/rgb.h"
  28.  
  29.  
  30. #define DEFAULT_FONT "/sys/fonts/Tahoma.kf"
  31.  
  32. #ifndef KFONT_BPP
  33. #define KFONT_BPP 4
  34. #endif
  35.  
  36. :struct __SIZE
  37. {
  38.         dword width,height;
  39.         signed offset_x, offset_y;
  40.         byte pt;
  41. };
  42. :struct LABEL
  43. {
  44.         __SIZE size;
  45.         int width,height;
  46.         byte bold,smooth;
  47.         dword color, background;
  48.         dword font,font_begin;
  49.         word block;
  50.         dword raw;
  51.         dword raw_size;
  52.         //dword palette[256];
  53.  
  54.         bool init();
  55.         bool changeSIZE();
  56.         byte symbol();
  57.         byte symbol_size();
  58.         dword getsize();
  59.  
  60.         void ApplySmooth();
  61.         int WriteIntoWindow();
  62.         int WriteIntoWindowCenter();
  63.         void WriteIntoBuffer();
  64.         void ShowBuffer();
  65.         void ShowBufferPart();
  66. } kfont;
  67.  
  68. :bool LABEL::init(dword font_path)
  69. {
  70.         IO label_io;
  71.         if(font)free(font);
  72.         label_io.read(font_path);
  73.         if(!EAX) {
  74.                 //debugln(font_path);
  75.                 label_io.run("/sys/@notify", "'Error: KFONT is not loaded.' -E");
  76.                 return false;
  77.         }
  78.         font_begin = label_io.buffer_data;
  79.         changeSIZE();
  80.         smooth = true;
  81.         return true;
  82. }
  83.  
  84. :bool LABEL::changeSIZE()
  85. {
  86.         dword file_size;
  87.         dword ofs;
  88.         if(size.pt<9) size.pt = 9;
  89.         font = font_begin;
  90.         ofs = DSDWORD[calc(size.pt-8<<2+font_begin)];
  91.         if(ofs==-1)return false;
  92.         font += ofs + 156;
  93.         file_size = DSDWORD[calc(font)];
  94.         height = DSBYTE[calc(font+file_size) - 1];
  95.         width =  DSBYTE[calc(font+file_size) - 2];
  96.         block = math.ceil(height*width/32);
  97.         return true;
  98. }
  99.  
  100. :dword LABEL::getsize(byte fontSizePoints, dword text1)
  101. {
  102.         size.height = size.width = 0;
  103.         size.offset_x = size.offset_y = -1;
  104.         size.pt = fontSizePoints;
  105.         if(size.pt)if(!changeSIZE())return 0;
  106.         WHILE(DSBYTE[text1])
  107.         {
  108.                 size.width += symbol_size(DSBYTE[text1]);
  109.                 text1++;
  110.         }
  111.         $neg size.offset_y
  112.         $neg size.offset_x
  113.         size.height += size.offset_y+1;
  114.         size.width += size.offset_x+1;
  115.         return size.width;
  116. }
  117.  
  118. :byte LABEL::symbol_size(byte s)
  119. {
  120.         int chaw_width;
  121.         chaw_width = symbol(0,0, s, 0);
  122.         if(bold) chaw_width += math.ceil(size.pt/17);
  123.         return chaw_width;
  124. }
  125.  
  126. :byte LABEL::symbol(signed x,y; byte s; dword image_raw)
  127. {
  128.         dword xi,yi;
  129.         dword iii = 0;
  130.         dword offs;
  131.         dword tmp, _;
  132.         byte X;
  133.         byte chaw_width=0;
  134.         if(s==32)return width/4;
  135.         if(s==9)return width;
  136.         s = Cp866ToAnsi(s);
  137.         tmp = block*s << 2 + font;
  138.         for(yi=0; yi<height; yi++)
  139.         {
  140.                 EDI = size.offset_y + yi + y * size.width * KFONT_BPP + image_raw;
  141.                 for(xi=0; xi<width; xi++)
  142.                 {
  143.                         if(iii%32) _ >>= 1;
  144.                         else
  145.                         {
  146.                                         tmp += 4;
  147.                                         _ = DSDWORD[tmp];
  148.                         }
  149.                         if(_&1) //check does the pixel set
  150.                         {
  151.                                 if(xi>chaw_width)chaw_width=xi;
  152.                                 //in case of image_raw!=0 draw font into bug
  153.                                 //in case of image_raw==0 calculate size
  154.                                 if (image_raw)
  155.                                 {
  156.                                         offs = x + xi * KFONT_BPP + EDI;
  157.                                         DSDWORD[offs] = color;
  158.                                         if(bold) DSDWORD[offs+KFONT_BPP] = color;
  159.                                 }
  160.                                 else
  161.                                 {
  162.                                         if(size.height<yi)size.height = yi;
  163.                                         if(size.offset_y<0)size.offset_y = yi; else if(yi<size.offset_y)size.offset_y = yi;
  164.                                         if(!X) X = xi; else if(X>xi)X = xi;
  165.                                         if(size.offset_x<0)size.offset_x = X;
  166.                                 }
  167.                         }
  168.                         iii++;
  169.                 }
  170.         }
  171.         return chaw_width;
  172. }
  173.  
  174. inline fastcall Cp866ToAnsi(AL) {
  175.         if (AL>=128)&&(AL<=175) return AL+64;
  176.         if (AL>=224)&&(AL<=239) return AL+16;
  177.         if (AL==241) return 184; //e ruAL with dotAL (yo)
  178.         if (AL==240) return 168; //E ruAL with dotAL (yo)
  179.         if (AL==242) return 'E'; //E ukr (ye)
  180.         if (AL==243) return 186; //e ukr (ye)
  181.         if (AL==244) return 'I'; //I ukr (yi)
  182.         if (AL==245) return 191; //i ukr (yi)
  183.         return AL;
  184. }
  185.  
  186. /*=====================================================================================
  187. ===========================                                 ===========================
  188. ===========================               RAW               ===========================
  189. ===========================                                 ===========================
  190. =====================================================================================*/
  191.  
  192. inline fastcall dword b32(EAX) { return DSDWORD[EAX]; }
  193. :void LABEL::ApplySmooth()
  194. {
  195.         dword i,line_w,to,dark_background;
  196.         line_w = size.width * KFONT_BPP;
  197.         to = size.height - 1 * line_w + raw - KFONT_BPP;
  198.         for(i=raw; i < to; i+=KFONT_BPP)
  199.         {
  200.                 if(i-raw%line_w +KFONT_BPP == line_w) continue;
  201.                 // pixels position, where b - black, w - write
  202.                 // bw
  203.                 // wb
  204.                 if(b32(i)!=background) {
  205.                         if (b32(i+KFONT_BPP)==background)
  206.                         && (b32(i+line_w)==background) && (b32(i+KFONT_BPP+line_w)!=background)
  207.                         {
  208.                                 dark_background = MixColors(background,b32(i),200);
  209.                                 DSDWORD[i+KFONT_BPP] = dark_background;
  210.                                 DSDWORD[i+line_w] = dark_background;   
  211.                         }
  212.                 }
  213.                 // wb
  214.                 // bw
  215.                 else if (b32(i+KFONT_BPP)!=background)
  216.                 && (b32(i+line_w)!=background) && (b32(i+KFONT_BPP+line_w)==background)
  217.                 {
  218.                         dark_background = MixColors(background,b32(i+KFONT_BPP),200);
  219.                         DSDWORD[i] = dark_background;
  220.                         DSDWORD[i+KFONT_BPP+line_w] = dark_background; 
  221.                 }
  222.         }
  223. }
  224.  
  225. :void LABEL::WriteIntoBuffer(int x,y,w,h; dword _background, _color; byte fontSizePoints; dword text1)
  226. {
  227.         dword new_raw_size;
  228.         if(!text1)return;
  229.         if(size.pt)if(!changeSIZE())return;
  230.        
  231.         if (size.pt != fontSizePoints) {
  232.                 getsize(fontSizePoints, text1);
  233.                 y -= size.offset_y;
  234.         }
  235.         color = _color;
  236.         background = _background;
  237.  
  238.         size.width = w;
  239.         size.height = h;
  240.  
  241.         new_raw_size = w*h*KFONT_BPP;
  242.         if(raw_size != new_raw_size)
  243.         {
  244.                 raw_size = new_raw_size;
  245.                 free(raw);
  246.                 raw = malloc(raw_size);
  247.                 // Fill background color
  248.                 EBX = background;
  249.                 EAX = raw_size+raw;
  250.                 for (EDI=raw; EDI<EAX; EDI+=KFONT_BPP) ESDWORD[EDI] = EBX;
  251.         }
  252.         WHILE(DSBYTE[text1])
  253.         {
  254.                 x+=symbol(x,y,DSBYTE[text1], raw);
  255.                 if(bold)x+=math.ceil(size.pt/17);
  256.                 text1++;
  257.         }
  258.         return;
  259. }
  260.  
  261. :int LABEL::WriteIntoWindow(int x,y; dword _background, _color; byte fontSizePoints; dword text1)
  262. {
  263.         if(!text1)return 0;
  264.         getsize(fontSizePoints, text1);
  265.         raw_size = NULL;
  266.         WriteIntoBuffer(0, -size.offset_y, size.width-size.offset_x,
  267.                 size.height-size.offset_y, _background, _color, fontSizePoints, text1);
  268.         if (smooth) ApplySmooth();
  269.         ShowBuffer(x,y);
  270.         return size.offset_x + size.width;
  271. }
  272.  
  273. :int LABEL::WriteIntoWindowCenter(dword x,y,w,h; dword _background, _color; byte fontSizePoints; dword text1)
  274. {
  275.         getsize(fontSizePoints, text1);
  276.         return WriteIntoWindow(w-size.width/2+x-1,y, _background, _color, fontSizePoints, text1);
  277. }
  278.  
  279. :void LABEL::ShowBuffer(dword _x, _y)
  280. {
  281.         if (4==KFONT_BPP) PutPaletteImage(raw, size.width, size.height, _x, _y, 32, 0);
  282.         //if (1==KFONT_BPP) PutPaletteImage(raw, size.width, size.height, _x, _y, 8, #palette);
  283. }
  284.  
  285. :void LABEL::ShowBufferPart(dword _x, _y, _w, _h, _buf_offset)
  286. {
  287.         if (4==KFONT_BPP) PutPaletteImage(_buf_offset * KFONT_BPP + raw, _w, _h, _x, _y, 32, 0);
  288.         //if (1==KFONT_BPP) PutPaletteImage(_buf_offset * KFONT_BPP + raw, _w, _h, _x, _y, 8, #palette);
  289. }
  290.  
  291. #endif