Subversion Repositories Kolibri OS

Rev

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