Subversion Repositories Kolibri OS

Rev

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