Subversion Repositories Kolibri OS

Rev

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. //unsigned size, psize;
  9. //
  10. //size=x*65536+y;
  11. //psize=w*65536+h;
  12. //__asm__ __volatile__("int $0x40"::"a"(36),"b"(image),"c"(psize),"d"(size));
  13. //}
  14. //
  15.  
  16.  
  17.  
  18. // inline void f65(unsigned x, unsigned y, unsigned w, unsigned h, char *d)
  19. //{
  20. //asm("pusha");
  21. //asm ("nop"::"D"(0), "c"(w*65536+h), "d"(x*65536+y), "b"(d));
  22. //asm ("xor %eax, %eax");
  23. //asm ("movl %eax, %ebp");
  24. //asm ("pushl $24");
  25. //asm ("popl %esi");
  26. //asm ("int $0x40"::"a"(65));
  27. //asm("popa");
  28. //}
  29.  
  30. inline void PutImage(unsigned x, unsigned y, unsigned w, unsigned h, char * image){
  31. unsigned size, psize;
  32. size=x*65536+y;
  33. psize=w*65536+h;
  34. __asm__ __volatile__("int $0x40"::"a"(7),"b"(image),"c"(psize),"d"(size));
  35. }
  36.  
  37. //void kputc(char *c){
  38. //      asm ("int $0x40"::"a"(63), "b"(1), "c"(c));
  39. //}
  40. //
  41. //void kol_board_puti(int n){
  42. //      char c;
  43. //      int i = 0;
  44. //      do {
  45. //              if (n<0) {
  46. //                      n=-n;
  47. //                      kputc("-");
  48. //              }
  49. //              c = n % 10 + '0';
  50. //              asm ("int $0x40"::"a"(63), "b"(1), "c"(c));
  51. //              i++;
  52. //      }
  53. //      while ((n /= 10) > 0);
  54. //}
  55.  
  56. void font_blit(unsigned char *dst,unsigned char *src, int x, int size) {
  57.         int lp;
  58.         float mixed, tp;
  59.         unsigned char color1;
  60.         int color2, color3, color4;
  61.         color2=((x)&0x000000FF);
  62.         color3=((((x)&0x00FF00)>>8)&0xFF);
  63.         color4=((((x)&0xFF0000)>>16)&0xFF);
  64. //kol_board_puti(size);
  65.         for (lp=0;lp <= size;lp++) {
  66.                 //kol_board_puti(lp);
  67.                 //kputc("\n");
  68.                 if (src[lp]>0) {
  69.                         tp=((float)src[lp])/255;
  70.  
  71.                         color1=dst[lp*3];
  72.                         mixed=(color1 * (1-tp)+color2 * tp);
  73.                         dst[lp*3]=(unsigned char) mixed;
  74.  
  75.                         color1=dst[lp*3+1];
  76.                         mixed=(color1 * (1-tp)+color3 * tp);
  77.                         dst[lp*3+1]=(unsigned char) mixed;
  78.  
  79.                         color1=dst[lp*3+2];
  80.                         mixed=(color1 * (1-tp)+color4 * tp);
  81.                         dst[lp*3+2]=(unsigned char) mixed;
  82.  
  83.                 }
  84.         }
  85.  
  86. }
  87.  
  88.  
  89. inline unsigned kol_process_info(unsigned slot, char buf1k[]){
  90.         asm ("int $0x40"::"a"(9), "b"(buf1k), "c"(slot));
  91. }
  92.  
  93. //void kol_board_puts(char *s){
  94. //      unsigned i;
  95. //      i = 0;
  96. //      while (*(s+i))
  97. //      {
  98. //              asm ("int $0x40"::"a"(63),"b"(1),"c"(*(s+i)));
  99. //              i++;
  100. //      }
  101. //}
  102.  
  103. //unsigned char screen[40*160];
  104.  
  105. int __stdcall init_font(stbtt_fontinfo *font,unsigned char *FontData)
  106. {
  107.         //stbtt_InitFont(font, pdf_font_DroidSans,stbtt_GetFontOffsetForIndex(pdf_font_DroidSans,0) );
  108.         stbtt_InitFont(font, FontData,stbtt_GetFontOffsetForIndex(FontData,0) );
  109. //      char *yay;
  110. //      yay=font;
  111. //      int i;
  112. //      for (i=0;i<28;i++)
  113. //              kputc(*(yay+i));
  114. //      kol_board_puts("init_font\n");
  115. }
  116.  
  117. float __stdcall get_width(short *text, stbtt_fontinfo *buffer, int height)
  118. {
  119.         stbtt_fontinfo *font;
  120.         font=buffer;
  121.         int ch=0;       //baseline,i,j,ascent,  
  122.         float scale,xpos=0;
  123.  
  124.         //baseline=10;
  125.  
  126.  // 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");}
  127. //      kol_board_puts("Engine init..\n");
  128. //kol_board_puti(&screen);
  129.         scale = stbtt_ScaleForPixelHeight(font, height*3/4);
  130.         int advance,lsb;
  131.  
  132.         while (text[ch]) {
  133. //         kol_board_puts("new symbol...\n");
  134.  
  135.                 stbtt_GetCodepointHMetrics(font, text[ch], &advance, &lsb);
  136.                 xpos += (advance * scale);
  137.                 //if (text[ch+1])
  138.                 //      xpos += scale*stbtt_GetCodepointKernAdvance(font, text[ch],text[ch+1]);
  139.                 ++ch;
  140.         }
  141.         //stbtt_GetCodepointHMetrics(font, text[ch], &advance, &lsb);
  142.         //xpos += (advance * scale);
  143.         return xpos;
  144. }
  145.  
  146. int __stdcall get_length(short *text, char *buffer, int height, int max_len){
  147.         stbtt_fontinfo font;
  148.         int i,j,ascent,baseline,ch,xpos=0;
  149.         float scale=0;
  150.  
  151.         baseline=10;
  152.  
  153. //      if (buffer==-1) {
  154. //              stbtt_InitFont(&font, pdf_font_DroidSans,stbtt_GetFontOffsetForIndex(pdf_font_DroidSans,0) );
  155. //      }
  156. //      else {
  157. //              stbtt_InitFont(&font, buffer,stbtt_GetFontOffsetForIndex(buffer,0) );
  158. //              kol_board_puts("Font loaded..\n");
  159. //      }
  160. //kol_board_puts("Engine init..\n");
  161. //kol_board_puti(&screen);
  162.         scale = stbtt_ScaleForPixelHeight(&font, height*3/4);
  163.  
  164.         while (text[ch]) {
  165.           // kol_board_puts("new symbol...\n");
  166.                 int advance,lsb;
  167.                 stbtt_GetCodepointHMetrics(&font, 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, text[ch],text[ch+1]);
  172.                 ++ch;
  173.         }
  174.         return ch;
  175.  
  176. }
  177.  
  178.  
  179. int __stdcall picture(short *text, stbtt_fontinfo *buffer, char *screen1, int width, int height){
  180.  
  181.  //unsigned char *screen;
  182.  //screen=zmalloc(20*78);
  183.         //kol_board_puts(screen);
  184.         //kol_board_puts("It was text\n");
  185.  
  186.         stbtt_fontinfo *font;
  187.         font=buffer;
  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 (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, text[ch], &advance, &lsb);
  213.                 stbtt_GetCodepointBitmapBoxSubpixel(font, 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, 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, text[ch],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. void __stdcall SetBackColor(int back_color, int width, int height, char *from_zone)
  235. {
  236.         int i,j;
  237.         unsigned char bcr,bcg,bcb;
  238.        
  239.         bcr=back_color<<16;
  240.         bcg=back_color<<8;
  241.         bcb=back_color;
  242.                
  243.         for (j=0;j<height ;j++)
  244.         {
  245.                 for (i=0;i<width ;i++)
  246.                 {
  247.                         from_zone[(j*width+i)*3]=back_color;
  248.                         from_zone[(j*width+i)*3+1]=back_color>>8;
  249.                         from_zone[(j*width+i)*3+2]=back_color>>16;
  250.                 }
  251.         }
  252. }
  253.  
  254. int __stdcall text_out(short *string, char *buffer, int height, int color,int back_color, int x, int y) {
  255.         unsigned char *from_zone;
  256.         unsigned char *to_zone;
  257.         int px, py;
  258.         unsigned char app_data[1024];
  259.  
  260.         int width;
  261. //kol_board_puts("width...\n");
  262. //kol_board_puts("\n and now height is ...");
  263. //kol_board_puti(height);
  264.  
  265.         //width=600;
  266.         width = get_width(string,buffer,height);
  267.        
  268. //kol_board_puts("\n Width is ...");
  269. //kol_board_puti(width);
  270. //kol_board_puts(" and height is ...");
  271. //kol_board_puti(height);
  272.  
  273. //kol_board_puts("\n malloc...\n");
  274.         from_zone=(char*)zmalloc(3*height*width);
  275.         to_zone=(char*)zmalloc(height*width);
  276. //kol_board_puts("malloc done...\n");
  277.  
  278.         kol_process_info(-1, app_data);
  279.         //px=app_data[35]*256+app_data[34];
  280.         px=app_data[35]*256+app_data[34]+app_data[55]*256+app_data[54];//lev
  281.         //py=app_data[39]*256+app_data[38];
  282.         py=app_data[39]*256+app_data[38]+app_data[59]*256+app_data[58];//lev
  283.  
  284. //kol_board_puts("\nzone...\n");
  285.         //getzone(px+x, py+y, width, height, from_zone);
  286.         SetBackColor(back_color, width, height,from_zone);
  287. //kol_board_puts("render...\n");
  288.         picture(string, buffer, to_zone, width, height);
  289. //kol_board_puts("blit...\n");
  290.         font_blit(from_zone,to_zone, color, width*height);
  291. //kol_board_puts("out...\n");
  292.  //f65(x,y,width,height,from_zone);
  293.         PutImage(x,y,width,height,from_zone);//lev
  294.         zfree(from_zone);
  295.         zfree(to_zone);
  296.         return 0;
  297. }
  298.  
  299. unsigned char*  __stdcall text_out_mem(short *string, char *buffer, int height, int color,int back_color) {
  300.         unsigned char *from_zone;
  301.         unsigned char *to_zone;
  302.         int px, py;
  303.         unsigned char app_data[1024];
  304.         int width;
  305.  
  306.         width = get_width(string,buffer,height);
  307.         from_zone=(char*)zmalloc(3*height*width+4);
  308.         to_zone=(char*)zmalloc(height*width);
  309.         *(int*)from_zone = width;
  310.         //kol_process_info(-1, app_data);
  311.         //px=app_data[35]*256+app_data[34]+app_data[55]*256+app_data[54];//lev
  312.         //py=app_data[39]*256+app_data[38]+app_data[59]*256+app_data[58];//lev
  313.         SetBackColor(back_color, width, height,from_zone+4);
  314.         picture(string, buffer, to_zone, width, height);
  315.         font_blit(from_zone+4,to_zone, color, width*height);
  316.         //PutImage(x,y,width,height,from_zone);//lev
  317.        
  318.         //zfree(from_zone);
  319.         zfree(to_zone);
  320.         return from_zone;
  321. }
  322.  
  323.  
  324. int __stdcall start(){
  325.  
  326.       return 1;
  327. }
  328.  
  329. int __stdcall version_major(){
  330.       return 1;
  331. }
  332.  
  333. int __stdcall version_minor(){
  334.      return 0;
  335. }
  336.  
  337. typedef struct{
  338.   char *name;
  339.   void *f;
  340. }export_t;
  341.  
  342. char szStart[]          ="START";
  343. char szVersion[]        ="version";
  344. char szVersionM[]       ="version_min";
  345. char szTrueType[]       ="truetype";
  346. char szGetLength[]      ="get_length";
  347. char szGetWidth[]       ="get_width";
  348. char szTextOut[]        ="text_out";
  349. char szTextOutMem[] ="text_out_mem";
  350. char szInitFont[]       ="init_font";
  351.  
  352.  
  353. export_t EXPORTS[] __asm__("EXPORTS") =
  354.         {
  355.                 { szStart,              start },
  356.                 { szVersion,    version_major },
  357.                 { szVersionM,   version_minor },
  358.                 { szTrueType,   picture },
  359.                 { szGetLength,  get_length},
  360.                 { szGetWidth,   get_width},
  361.                 { szTextOut,    text_out },
  362.                 { szTextOutMem, text_out_mem},
  363.                 { szInitFont,   init_font},
  364.                 { NULL, NULL },
  365.         };
  366.