Subversion Repositories Kolibri OS

Rev

Rev 7286 | Rev 7798 | 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_KFONT_H
  17. #define INCLUDE_KFONT_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. int kfont_char_width[255];
  37.  
  38. :struct __SIZE
  39. {
  40.         dword width,height;
  41.         signed offset_x, offset_y;
  42.         byte pt;
  43. };
  44. :struct KFONT
  45. {
  46.         __SIZE size;
  47.         int width,height;
  48.         byte bold,smooth;
  49.         dword color, background;
  50.         dword font,font_begin;
  51.         word block;
  52.         dword raw;
  53.         dword raw_size;
  54.  
  55.         bool init();
  56.         bool changeSIZE();
  57.         byte symbol();
  58.         byte symbol_size();
  59.         dword getsize();
  60.         int get_label_width();
  61.  
  62.         void ApplySmooth();
  63.         int WriteIntoWindow();
  64.         int WriteIntoWindowCenter();
  65.         void WriteIntoBuffer();
  66.         void ShowBuffer();
  67.         void ShowBufferPart();
  68. } kfont;
  69.  
  70. :bool KFONT::init(dword font_path)
  71. {
  72.         IO label_io;
  73.         if(font)free(font);
  74.         label_io.read(font_path);
  75.         if(!EAX) {
  76.                 //debugln(font_path);
  77.                 label_io.run("/sys/@notify", "'Error: KFONT is not loaded.' -E");
  78.                 return false;
  79.         }
  80.         font_begin = label_io.buffer_data;
  81.         changeSIZE();
  82.         smooth = true;
  83.         return true;
  84. }
  85.  
  86. :bool KFONT::changeSIZE()
  87. {
  88.         int i;
  89.         dword file_size;
  90.         dword ofs;
  91.         if(size.pt<9) size.pt = 9;
  92.         font = font_begin;
  93.         ofs = DSDWORD[calc(size.pt-8<<2+font_begin)];
  94.         if(ofs==-1)return false;
  95.         font += ofs + 156;
  96.         file_size = DSDWORD[calc(font)];
  97.         height = DSBYTE[calc(font+file_size) - 1];
  98.         width =  DSBYTE[calc(font+file_size) - 2];
  99.         block = math.ceil(height*width/32);
  100.         for (i=0; i<256; i++) {
  101.                 kfont_char_width[i] = symbol_size((byte) i);
  102.         }
  103.         return true;
  104. }
  105.  
  106. :dword KFONT::getsize(byte font_size, dword text1)
  107. {
  108.         size.height = size.width = 0;
  109.         size.offset_x = size.offset_y = -1;
  110.         if (size.pt != font_size) {
  111.                 size.pt = font_size;
  112.                 if(!changeSIZE())return 0;
  113.         }
  114.         WHILE(DSBYTE[text1])
  115.         {
  116.                 size.width += symbol_size(DSBYTE[text1]);
  117.                 text1++;
  118.         }
  119.         $neg size.offset_y
  120.         $neg size.offset_x
  121.         size.height += size.offset_y+1;
  122.         size.width += size.offset_x+1;
  123.         return size.width;
  124. }
  125.  
  126. //WILL NOT WORK if requested font_size
  127. //is differ from precalculated kfont_char_width[]
  128. :int KFONT::get_label_width(dword _label)
  129. {
  130.         int len=0;
  131.         while (ESBYTE[_label]) {
  132.                 len += kfont_char_width[ ESBYTE[_label] ];
  133.                 _label++;
  134.         }
  135.         return len;
  136. }
  137.  
  138. :byte KFONT::symbol_size(byte s)
  139. {
  140.         int chaw_width;
  141.         chaw_width = symbol(0,0, s, 0);
  142.         if(bold) chaw_width += math.ceil(size.pt/17);
  143.         return chaw_width;
  144. }
  145.  
  146. :byte KFONT::symbol(signed x,y; byte s; dword image_raw)
  147. {
  148.         dword xi,yi;
  149.         dword iii = 0;
  150.         dword offs;
  151.         dword tmp, _;
  152.         byte X;
  153.         byte chaw_width=0;
  154.         if(s==32)return width/4;
  155.         if(s==9)return width;
  156.         s = Cp866ToAnsi(s);
  157.         tmp = block*s << 2 + font;
  158.         for(yi=0; yi<height; yi++)
  159.         {
  160.                 EDI = size.offset_y + yi + y * size.width * KFONT_BPP + image_raw;
  161.                 for(xi=0; xi<width; xi++)
  162.                 {
  163.                         if(iii%32) _ >>= 1;
  164.                         else
  165.                         {
  166.                                         tmp += 4;
  167.                                         _ = DSDWORD[tmp];
  168.                         }
  169.                         if(_&1) //check does the pixel set
  170.                         {
  171.                                 if(xi>chaw_width)chaw_width=xi;
  172.                                 //in case of image_raw!=0 draw font into bug
  173.                                 //in case of image_raw==0 calculate size
  174.                                 if (image_raw)
  175.                                 {
  176.                                         offs = x + xi * KFONT_BPP + EDI;
  177.                                         DSDWORD[offs] = color;
  178.                                         if(bold) DSDWORD[offs+KFONT_BPP] = color;
  179.                                 }
  180.                                 else
  181.                                 {
  182.                                         if(size.height<yi)size.height = yi;
  183.                                         if(size.offset_y<0)size.offset_y = yi; else if(yi<size.offset_y)size.offset_y = yi;
  184.                                         if(!X) X = xi; else if(X>xi)X = xi;
  185.                                         if(size.offset_x<0)size.offset_x = X;
  186.                                 }
  187.                         }
  188.                         iii++;
  189.                 }
  190.         }
  191.         return chaw_width;
  192. }
  193.  
  194. inline fastcall Cp866ToAnsi(AL) {
  195.         if (AL>=128)&&(AL<=175) return AL+64;
  196.         if (AL>=224)&&(AL<=239) return AL+16;
  197.         if (AL==241) return 184; //e ruAL with dotAL (yo)
  198.         if (AL==240) return 168; //E ruAL with dotAL (yo)
  199.         if (AL==242) return 'E'; //E ukr (ye)
  200.         if (AL==243) return 186; //e ukr (ye)
  201.         if (AL==244) return 'I'; //I ukr (yi)
  202.         if (AL==245) return 191; //i ukr (yi)
  203.         return AL;
  204. }
  205.  
  206. /*=====================================================================================
  207. ===========================                                 ===========================
  208. ===========================               RAW               ===========================
  209. ===========================                                 ===========================
  210. =====================================================================================*/
  211.  
  212. inline fastcall dword b32(EAX) { return DSDWORD[EAX]; }
  213. :void KFONT::ApplySmooth()
  214. {
  215.         dword i,line_w,to,dark_background;
  216.         line_w = size.width * KFONT_BPP;
  217.         to = size.height - 1 * line_w + raw - KFONT_BPP;
  218.         for(i=raw; i < to; i+=KFONT_BPP)
  219.         {
  220.                 if(i-raw%line_w +KFONT_BPP == line_w) continue;
  221.                 // pixels position, where b - black, w - write
  222.                 // bw
  223.                 // wb
  224.                 if(b32(i)!=background) {
  225.                         if (b32(i+KFONT_BPP)==background)
  226.                         && (b32(i+line_w)==background) && (b32(i+KFONT_BPP+line_w)!=background)
  227.                         {
  228.                                 dark_background = MixColors(background,b32(i),200);
  229.                                 DSDWORD[i+KFONT_BPP] = dark_background;
  230.                                 DSDWORD[i+line_w] = dark_background;   
  231.                         }
  232.                 }
  233.                 // wb
  234.                 // bw
  235.                 else if (b32(i+KFONT_BPP)!=background)
  236.                 && (b32(i+line_w)!=background) && (b32(i+KFONT_BPP+line_w)==background)
  237.                 {
  238.                         dark_background = MixColors(background,b32(i+KFONT_BPP),200);
  239.                         DSDWORD[i] = dark_background;
  240.                         DSDWORD[i+KFONT_BPP+line_w] = dark_background; 
  241.                 }
  242.         }
  243. }
  244.  
  245. :void KFONT::WriteIntoBuffer(int x,y,w,h; dword _background, _color; byte font_size; dword text1)
  246. {
  247.         dword new_raw_size;
  248.         if(!text1)return;
  249.        
  250.         if (size.pt != font_size) {
  251.                 getsize(font_size, text1);
  252.                 y -= size.offset_y;
  253.         }
  254.         color = _color;
  255.         background = _background;
  256.  
  257.         size.width = w;
  258.         size.height = h;
  259.  
  260.         new_raw_size = w*h*KFONT_BPP;
  261.         if(raw_size != new_raw_size)
  262.         {
  263.                 raw_size = new_raw_size;
  264.                 free(raw);
  265.                 raw = malloc(raw_size);
  266.                 // Fill background color
  267.                 EBX = background;
  268.                 EAX = raw_size+raw;
  269.                 for (EDI=raw; EDI<EAX; EDI+=KFONT_BPP) ESDWORD[EDI] = EBX;
  270.         }
  271.         WHILE(DSBYTE[text1])
  272.         {
  273.                 x+=symbol(x,y,DSBYTE[text1], raw);
  274.                 if(bold)x+=math.ceil(size.pt/17);
  275.                 text1++;
  276.         }
  277.         return;
  278. }
  279.  
  280. :int KFONT::WriteIntoWindow(int x,y; dword _background, _color; byte font_size; dword text1)
  281. {
  282.         if(!text1)return 0;
  283.         getsize(font_size, text1);
  284.         raw_size = NULL;
  285.         WriteIntoBuffer(0, -size.offset_y, size.width-size.offset_x,
  286.                 size.height-size.offset_y, _background, _color, font_size, text1);
  287.         if (smooth) ApplySmooth();
  288.         ShowBuffer(x,y);
  289.         return size.offset_x + size.width;
  290. }
  291.  
  292. :int KFONT::WriteIntoWindowCenter(dword x, _y,w,h, _background, _color; byte font_size; dword text1)
  293. {
  294.         getsize(font_size, text1);
  295.         return WriteIntoWindow(w-size.width/2+x-1, _y, _background, _color, font_size, text1);
  296. }
  297.  
  298. :void KFONT::ShowBuffer(dword _x, _y)
  299. {
  300.         if (4==KFONT_BPP) PutPaletteImage(raw, size.width, size.height, _x, _y, 32, 0);
  301.         //if (1==KFONT_BPP) PutPaletteImage(raw, size.width, size.height, _x, _y, 8, #palette);
  302. }
  303.  
  304. :void KFONT::ShowBufferPart(dword _x, _y, _w, _h, _buf_offset)
  305. {
  306.         if (4==KFONT_BPP) PutPaletteImage(_buf_offset * KFONT_BPP + raw, _w, _h, _x, _y, 32, 0);
  307.         //if (1==KFONT_BPP) PutPaletteImage(_buf_offset * KFONT_BPP + raw, _w, _h, _x, _y, 8, #palette);
  308. }
  309.  
  310. #endif