Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #define STB_TRUETYPE_IMPLEMENTATION  // force following include to generate implementation
  2. #include "stb.h"
  3. #include "font_droid.h"
  4.  
  5. #define __stdcall __attribute__((stdcall))
  6.  
  7.  inline void getzone(unsigned int x, unsigned int y, unsigned int w, unsigned int h, char * image)
  8. {
  9. unsigned size, psize;
  10.  
  11. size=x*65536+y;
  12. psize=w*65536+h;
  13. __asm__ __volatile__("int $0x40"::"a"(36),"b"(image),"c"(psize),"d"(size));
  14. }
  15.            
  16. // inline void f65(unsigned x, unsigned y, unsigned w, unsigned h, char *d)
  17. //{
  18. //asm("pusha");
  19. //asm ("nop"::"D"(0), "c"(w*65536+h), "d"(x*65536+y), "b"(d));
  20. //asm ("xor %eax, %eax");
  21. //asm ("movl %eax, %ebp");
  22. //asm ("pushl $24");
  23. //asm ("popl %esi");
  24. //asm ("int $0x40"::"a"(65));
  25. //asm("popa");
  26. //}
  27.  
  28.  inline void PutImage(unsigned x, unsigned y, unsigned w, unsigned h, char * image)
  29. {
  30. unsigned size, psize;
  31. size=x*65536+y;
  32. psize=w*65536+h;
  33. __asm__ __volatile__("int $0x40"::"a"(7),"b"(image),"c"(psize),"d"(size));
  34. }
  35.  
  36.  void kputc(char *c)
  37. {
  38. asm ("int $0x40"::"a"(63), "b"(1), "c"(c));
  39. }
  40.  
  41. void kol_board_puti(int n)
  42. {
  43. char c;
  44. int i = 0;
  45. do
  46.         {
  47.         if (n<0) {n=-n;kputc("-");}
  48.         c = n % 10 + '0';
  49.         asm ("int $0x40"::"a"(63), "b"(1), "c"(c));
  50.         i++;
  51.         }
  52.         while ((n /= 10) > 0);
  53. }
  54.  
  55. void font_blit(unsigned char *dst,unsigned char *src, int x, int size) {
  56. int lp;
  57. float mixed, tp;
  58. unsigned char color1;
  59. int color2, color3, color4;
  60. color2=((x)&0x000000FF);
  61. color3=((((x)&0x00FF00)>>8)&0xFF);
  62. color4=((((x)&0xFF0000)>>16)&0xFF);
  63. //kol_board_puti(size);
  64.         for (lp=0;lp <= size;lp++) {
  65.                 //kol_board_puti(lp);          
  66.                 //kputc("\n");
  67.                 if (src[lp]>0) {
  68.                         tp=((float)src[lp])/255;
  69.                        
  70.                         color1=dst[lp*3];
  71.                         mixed=(color1 * (1-tp)+color2 * tp);   
  72.                         dst[lp*3]=(unsigned char) mixed;
  73.                        
  74.                         color1=dst[lp*3+1];
  75.                         mixed=(color1 * (1-tp)+color3 * tp);   
  76.                         dst[lp*3+1]=(unsigned char) mixed;
  77.                        
  78.                         color1=dst[lp*3+2];
  79.                         mixed=(color1 * (1-tp)+color4 * tp);   
  80.                         dst[lp*3+2]=(unsigned char) mixed;
  81.                                                
  82.                 }
  83.         }
  84.        
  85. }
  86.  
  87.  
  88. inline unsigned kol_process_info(unsigned slot, char buf1k[])
  89. {
  90. asm ("int $0x40"::"a"(9), "b"(buf1k), "c"(slot));
  91. }
  92.  
  93. void kol_board_puts(char *s)
  94. {
  95. unsigned i;
  96. i = 0;
  97. while (*(s+i))
  98.         {
  99.         asm ("int $0x40"::"a"(63),"b"(1),"c"(*(s+i)));
  100.         i++;
  101.         }
  102. }
  103.  
  104. int dos2utf (unsigned char some){
  105.        
  106.          int same;
  107.        
  108.            if (some>=0x80 && some <=0xAF) {same=(4<<8)+some-0x70;}
  109.            else if (some>=0xE0 && some <=0xEF) {same=(4<<8)+some-0xA0;}
  110.            else {same=some;}
  111.                    return same;
  112.              
  113.        
  114. }
  115.  
  116. //unsigned char screen[40*160];
  117.  
  118. int __stdcall get_width(char *text, char *buffer, int height)
  119. {
  120.  
  121. stbtt_fontinfo font;
  122.    int i,j,ascent,baseline,ch,xpos=0;
  123.    float scale=0;
  124.    
  125.    baseline=10;
  126.  
  127.   if (buffer==-1) {stbtt_InitFont(&font, pdf_font_DroidSans,stbtt_GetFontOffsetForIndex(pdf_font_DroidSans,0) );} else {stbtt_InitFont(&font, buffer,stbtt_GetFontOffsetForIndex(buffer,0) );kol_board_puts("Font loaded..\n");}
  128. kol_board_puts("Engine init..\n");
  129. //kol_board_puti(&screen);
  130.    scale = stbtt_ScaleForPixelHeight(&font, height*3/4);
  131.    int advance,lsb;
  132.    
  133.  while (dos2utf(text[ch])) {
  134. //         kol_board_puts("new symbol...\n");
  135.    
  136.        stbtt_GetCodepointHMetrics(&font, dos2utf(text[ch]), &advance, &lsb);
  137.   xpos += (advance * scale);
  138.       if (text[ch+1])
  139.          xpos += scale*stbtt_GetCodepointKernAdvance(&font, dos2utf(text[ch]),dos2utf(text[ch+1]));
  140.       ++ch;
  141.    }
  142.    stbtt_GetCodepointHMetrics(&font, dos2utf(text[ch]), &advance, &lsb);
  143.      xpos += (advance * scale);
  144.    return xpos;
  145.  
  146. }
  147.  
  148.  
  149.  
  150. int __stdcall get_length(char *text, char *buffer, int height, int max_len)
  151. {
  152.  
  153. stbtt_fontinfo font;
  154.    int i,j,ascent,baseline,ch,xpos=0;
  155.    float scale=0;
  156.    
  157.    baseline=10;
  158.  
  159.   if (buffer==-1) {stbtt_InitFont(&font, pdf_font_DroidSans,stbtt_GetFontOffsetForIndex(pdf_font_DroidSans,0) );} else {stbtt_InitFont(&font, buffer,stbtt_GetFontOffsetForIndex(buffer,0) );kol_board_puts("Font loaded..\n");}
  160. //kol_board_puts("Engine init..\n");
  161. //kol_board_puti(&screen);
  162.    scale = stbtt_ScaleForPixelHeight(&font, height*3/4);
  163.  
  164.  while (dos2utf(text[ch])) {
  165.           // kol_board_puts("new symbol...\n");
  166.       int advance,lsb;
  167.        stbtt_GetCodepointHMetrics(&font, dos2utf(text[ch]), &advance, &lsb);
  168.   xpos += (advance * scale);
  169.   if ((int)xpos>max_len) return ch;
  170.       if (text[ch+1])
  171.          xpos += scale*stbtt_GetCodepointKernAdvance(&font, dos2utf(text[ch]),dos2utf(text[ch+1]));
  172.       ++ch;
  173.       }
  174.    return ch;
  175.  
  176. }
  177.  
  178.  
  179. int __stdcall picture(char *text, char *buffer, char *screen1, int width, int height)
  180. {
  181.  
  182.  //unsigned char *screen;
  183.  //screen=zmalloc(20*78);
  184.         //kol_board_puts(screen);
  185.         //kol_board_puts("It was text\n");
  186.        
  187. stbtt_fontinfo font;
  188.    int i,j,ascent,baseline,descent,ch=0;
  189.    float scale, xpos=0;
  190.    
  191.    //baseline=10;
  192.    kol_board_puts("Font address:\n");
  193.    //kol_board_puti(buffer);
  194.  if (buffer==-1) {stbtt_InitFont(&font, pdf_font_DroidSans,stbtt_GetFontOffsetForIndex(pdf_font_DroidSans,0) );kol_board_puts("default font\n");} else {stbtt_InitFont(&font, buffer,stbtt_GetFontOffsetForIndex(buffer,0) );kol_board_puts("Font loaded..\n");}
  195.  
  196. //kol_board_puti(&screen);
  197.    scale = stbtt_ScaleForPixelHeight(&font, height*3/4);
  198.    //stbtt_GetFontVMetrics(&font, &ascent,0,0);
  199.    stbtt_GetFontVMetrics(&font, &ascent,&descent,0); //lev
  200.    //baseline = (int) (ascent*scale);
  201.    baseline = (int) ((ascent-descent)*scale); //lev
  202.  
  203.  
  204. //kol_board_puts("Text render:\n");
  205.  
  206.    while (dos2utf(text[ch])) {
  207.            //kol_board_puts("new symbol...\n");
  208.       int advance,lsb,x0,y0,x1,y1;
  209.       //float x_shift = xpos - (float) i_floor(xpos);
  210.      
  211.      // kol_board_puts("floor called!\n");
  212.       stbtt_GetCodepointHMetrics(&font, dos2utf(text[ch]), &advance, &lsb);
  213.       stbtt_GetCodepointBitmapBoxSubpixel(&font, dos2utf(text[ch]), scale,scale,0,0, &x0,&y0,&x1,&y1);
  214.  
  215.  
  216.      //10= y0, 20=y1-y0 or so
  217.       stbtt_MakeCodepointBitmapSubpixel(&font, &screen1[(baseline + y0)*width+ (int)xpos + x0], x1-x0,y1-y0, width, scale,scale,0,0, dos2utf(text[ch]));
  218.        
  219.       // note that this stomps the old data, so where character boxes overlap (e.g. 'lj') it's wrong
  220.       // because this API is really for baking character bitmaps into textures. if you want to do this,
  221.       // you need to render the bitmap to a temp buffer, then\n\t"alpha blend" that into the working buffer
  222.       xpos += (advance * scale);
  223.       if (text[ch+1])
  224.          xpos += scale*stbtt_GetCodepointKernAdvance(&font, dos2utf(text[ch]),dos2utf(text[ch+1]));
  225.       ++ch;
  226.    }
  227.    
  228.   //zmemcpy(screen1,bitmap,20*20);
  229.  
  230. //kol_board_puts("finished...\n");
  231. return 0;
  232. }
  233.  
  234. int __stdcall text_out(char *string, char *buffer, int height, int color, int x, int y) {
  235.         unsigned char *from_zone;
  236.         unsigned char *to_zone;
  237.         int px, py;
  238.         unsigned char app_data[1024];
  239.        
  240.         int width=0;
  241.         //kol_board_puts("width...\n");
  242.         //kol_board_puts("\n and now height is ...");
  243.         //kol_board_puti(height);
  244.        
  245.         width=get_width(string,buffer,height);
  246.         //kol_board_puts("\n Width is ...");
  247.         //kol_board_puti(width);
  248.         //kol_board_puts(" and height is ...");
  249.         //kol_board_puti(height);
  250.        
  251.         //kol_board_puts("\n malloc...\n");
  252.         from_zone=(char*)zmalloc(3*height*width);
  253.         to_zone=(char*)zmalloc(height*width);
  254.         //kol_board_puts("malloc done...\n");
  255.        
  256. kol_process_info(-1, app_data);
  257. //px=app_data[35]*256+app_data[34];
  258. px=app_data[35]*256+app_data[34]+app_data[55]*256+app_data[54];//lev
  259. //py=app_data[39]*256+app_data[38];
  260. py=app_data[39]*256+app_data[38]+app_data[59]*256+app_data[58];//lev
  261.  
  262. //kol_board_puts("\nzone...\n");
  263. getzone(px+x, py+y, width, height, from_zone);
  264. //kol_board_puts("render...\n");
  265. picture(string, buffer, to_zone, width, height);
  266. //kol_board_puts("blit...\n"); 
  267. font_blit(from_zone,to_zone, color, width*height);
  268. //kol_board_puts("out...\n");
  269.  //f65(x,y,width,height,from_zone);
  270.  PutImage(x,y,width,height,from_zone);//lev
  271.         zfree(from_zone);
  272.         zfree(to_zone);
  273.         return 0;
  274. }
  275.  
  276.  
  277.  
  278. int __stdcall start()
  279. {
  280.  
  281.       return 1;
  282. };
  283.  
  284. int __stdcall version_major()
  285. {
  286.       return 1;
  287. }
  288.  
  289. int __stdcall version_minor()
  290. {
  291.      return 0;
  292. }
  293.  
  294. typedef struct
  295. {
  296.   char *name;
  297.   void *f;
  298. }export_t;
  299.  
  300. char szStart[]    ="START";
  301. char szVersion[]  ="version";
  302. char szVersionM[] ="version_min";
  303. char szTrueType[] ="truetype";
  304. char szGetLength[]="get_length";
  305. char szGetWidth[] ="get_width";
  306. char szTextOut[]  ="text_out";
  307.  
  308.  
  309.  
  310. export_t EXPORTS[] __asm__("EXPORTS") =
  311.          {
  312.            { szStart,           start },
  313.            { szVersion,          version_major },
  314.            { szVersionM,         version_minor },
  315.            { szTrueType,              picture },
  316.            { szGetLength,       get_length},
  317.            { szGetWidth, get_width},
  318.            { szTextOut, text_out },
  319.            { NULL, NULL },
  320.          };
  321.