forked from KolibriOS/kolibrios
Separate original stb_truetype.h from kolibri library code, updated stb_truetype to version 1.02, added utf8 support for library functions, added compile.bat for windows users.
git-svn-id: svn://kolibrios.org@5517 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
parent
e2f24f546c
commit
c0c80a0aca
3
programs/develop/libraries/truetype/trunk/compile.bat
Normal file
3
programs/develop/libraries/truetype/trunk/compile.bat
Normal file
@ -0,0 +1,3 @@
|
||||
@echo off
|
||||
gcc -c truetype.c -o truetype.obj -O2 -nostdlib
|
||||
pause
|
315
programs/develop/libraries/truetype/trunk/glue.h
Normal file
315
programs/develop/libraries/truetype/trunk/glue.h
Normal file
@ -0,0 +1,315 @@
|
||||
#define NULL 0
|
||||
typedef unsigned int size_t;
|
||||
|
||||
|
||||
// sort
|
||||
#define THRESH 4 /* threshold for insertion */
|
||||
#define MTHRESH 6 /* threshold for median */
|
||||
|
||||
static int (*qcmp)(const void *, const void *); /* the comparison routine */
|
||||
static int qsz; /* size of each record */
|
||||
static int thresh; /* THRESHold in chars */
|
||||
static int mthresh; /* MTHRESHold in chars */
|
||||
|
||||
/*
|
||||
* qst:
|
||||
* Do a quicksort
|
||||
* First, find the median element, and put that one in the first place as the
|
||||
* discriminator. (This "median" is just the median of the first, last and
|
||||
* middle elements). (Using this median instead of the first element is a big
|
||||
* win). Then, the usual partitioning/swapping, followed by moving the
|
||||
* discriminator into the right place. Then, figure out the sizes of the two
|
||||
* partions, do the smaller one recursively and the larger one via a repeat of
|
||||
* this code. Stopping when there are less than THRESH elements in a partition
|
||||
* and cleaning up with an insertion sort (in our caller) is a huge win.
|
||||
* All data swaps are done in-line, which is space-losing but time-saving.
|
||||
* (And there are only three places where this is done).
|
||||
*/
|
||||
|
||||
static void qst(char *base, char *max)
|
||||
{
|
||||
char c, *i, *j, *jj;
|
||||
int ii;
|
||||
char *mid, *tmp;
|
||||
int lo, hi;
|
||||
|
||||
/*
|
||||
* At the top here, lo is the number of characters of elements in the
|
||||
* current partition. (Which should be max - base).
|
||||
* Find the median of the first, last, and middle element and make
|
||||
* that the middle element. Set j to largest of first and middle.
|
||||
* If max is larger than that guy, then it's that guy, else compare
|
||||
* max with loser of first and take larger. Things are set up to
|
||||
* prefer the middle, then the first in case of ties.
|
||||
*/
|
||||
lo = max - base; /* number of elements as chars */
|
||||
do {
|
||||
mid = i = base + qsz * ((lo / qsz) >> 1);
|
||||
if (lo >= mthresh)
|
||||
{
|
||||
j = (qcmp((jj = base), i) > 0 ? jj : i);
|
||||
if (qcmp(j, (tmp = max - qsz)) > 0)
|
||||
{
|
||||
/* switch to first loser */
|
||||
j = (j == jj ? i : jj);
|
||||
if (qcmp(j, tmp) < 0)
|
||||
j = tmp;
|
||||
}
|
||||
if (j != i)
|
||||
{
|
||||
ii = qsz;
|
||||
do {
|
||||
c = *i;
|
||||
*i++ = *j;
|
||||
*j++ = c;
|
||||
} while (--ii);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Semi-standard quicksort partitioning/swapping
|
||||
*/
|
||||
for (i = base, j = max - qsz; ; )
|
||||
{
|
||||
while (i < mid && qcmp(i, mid) <= 0)
|
||||
i += qsz;
|
||||
while (j > mid)
|
||||
{
|
||||
if (qcmp(mid, j) <= 0)
|
||||
{
|
||||
j -= qsz;
|
||||
continue;
|
||||
}
|
||||
tmp = i + qsz; /* value of i after swap */
|
||||
if (i == mid)
|
||||
{
|
||||
/* j <-> mid, new mid is j */
|
||||
mid = jj = j;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* i <-> j */
|
||||
jj = j;
|
||||
j -= qsz;
|
||||
}
|
||||
goto swap;
|
||||
}
|
||||
if (i == mid)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* i <-> mid, new mid is i */
|
||||
jj = mid;
|
||||
tmp = mid = i; /* value of i after swap */
|
||||
j -= qsz;
|
||||
}
|
||||
swap:
|
||||
ii = qsz;
|
||||
do {
|
||||
c = *i;
|
||||
*i++ = *jj;
|
||||
*jj++ = c;
|
||||
} while (--ii);
|
||||
i = tmp;
|
||||
}
|
||||
/*
|
||||
* Look at sizes of the two partitions, do the smaller
|
||||
* one first by recursion, then do the larger one by
|
||||
* making sure lo is its size, base and max are update
|
||||
* correctly, and branching back. But only repeat
|
||||
* (recursively or by branching) if the partition is
|
||||
* of at least size THRESH.
|
||||
*/
|
||||
i = (j = mid) + qsz;
|
||||
if ((lo = j - base) <= (hi = max - i))
|
||||
{
|
||||
if (lo >= thresh)
|
||||
qst(base, j);
|
||||
base = i;
|
||||
lo = hi;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hi >= thresh)
|
||||
qst(i, max);
|
||||
max = j;
|
||||
}
|
||||
} while (lo >= thresh);
|
||||
}
|
||||
|
||||
/*
|
||||
* qsort:
|
||||
* First, set up some global parameters for qst to share. Then, quicksort
|
||||
* with qst(), and then a cleanup insertion sort ourselves. Sound simple?
|
||||
* It's not...
|
||||
*/
|
||||
|
||||
void qsort_g(void *base0, size_t n, size_t size, int (*compar)(const void *, const void *))
|
||||
{
|
||||
char *base = (char *)base0;
|
||||
char c, *i, *j, *lo, *hi;
|
||||
char *min, *max;
|
||||
|
||||
if (n <= 1)
|
||||
return;
|
||||
qsz = size;
|
||||
qcmp = compar;
|
||||
thresh = qsz * THRESH;
|
||||
mthresh = qsz * MTHRESH;
|
||||
max = base + n * qsz;
|
||||
if (n >= THRESH)
|
||||
{
|
||||
qst(base, max);
|
||||
hi = base + thresh;
|
||||
}
|
||||
else
|
||||
{
|
||||
hi = max;
|
||||
}
|
||||
/*
|
||||
* First put smallest element, which must be in the first THRESH, in
|
||||
* the first position as a sentinel. This is done just by searching
|
||||
* the first THRESH elements (or the first n if n < THRESH), finding
|
||||
* the min, and swapping it into the first position.
|
||||
*/
|
||||
for (j = lo = base; (lo += qsz) < hi; )
|
||||
if (qcmp(j, lo) > 0)
|
||||
j = lo;
|
||||
if (j != base)
|
||||
{
|
||||
/* swap j into place */
|
||||
for (i = base, hi = base + qsz; i < hi; )
|
||||
{
|
||||
c = *j;
|
||||
*j++ = *i;
|
||||
*i++ = c;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* With our sentinel in place, we now run the following hyper-fast
|
||||
* insertion sort. For each remaining element, min, from [1] to [n-1],
|
||||
* set hi to the index of the element AFTER which this one goes.
|
||||
* Then, do the standard insertion sort shift on a character at a time
|
||||
* basis for each element in the frob.
|
||||
*/
|
||||
for (min = base; (hi = min += qsz) < max; )
|
||||
{
|
||||
while (qcmp(hi -= qsz, min) > 0)
|
||||
/* void */;
|
||||
if ((hi += qsz) != min) {
|
||||
for (lo = min + qsz; --lo >= min; )
|
||||
{
|
||||
c = *lo;
|
||||
for (i = j = lo; (j -= qsz) >= hi; i = j)
|
||||
*i = *j;
|
||||
*i = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define STBTT_sort(data,num_items,item_size,compare_func) qsort_g(data,num_items,item_size,compare_func)
|
||||
|
||||
asm ("_floor: \n\t"
|
||||
"pushl %ebp\n\t"
|
||||
"movl %esp,%ebp\n\t"
|
||||
"subl $8,%esp\n\t"
|
||||
"fstcw -4(%ebp)\n\t"
|
||||
"fwait\n\t"
|
||||
"movw -4(%ebp),%ax\n\t"
|
||||
"andw $0xf3ff,%ax\n\t"
|
||||
"orw $0x0400,%ax\n\t"
|
||||
"movw %ax,-2(%ebp)\n\t"
|
||||
"fldcw -2(%ebp)\n\t"
|
||||
"fldl 8(%ebp)\n\t"
|
||||
"frndint\n\t"
|
||||
"fldcw -4(%ebp)\n\t"
|
||||
"movl %ebp,%esp\n\t"
|
||||
"popl %ebp\n\t"
|
||||
"ret");
|
||||
|
||||
|
||||
int i_floor (float x) {
|
||||
int z;
|
||||
z=x;
|
||||
if (z+1>x) {return z;} else {return (z+1);}
|
||||
}
|
||||
|
||||
int i_ceil (float x) {
|
||||
int z;
|
||||
z=x;
|
||||
if (z>x) {return z;} else {return (z+1);}
|
||||
}
|
||||
|
||||
|
||||
double sqrt (double x)
|
||||
{
|
||||
if (x < 0.0F )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
double res;
|
||||
asm ("fsqrt" : "=t" (res) : "0" (x));
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
#define STBTT_ifloor(x) ((int) i_floor(x))
|
||||
#define STBTT_iceil(x) ((int) i_ceil(x))
|
||||
|
||||
static inline void *zmalloc(size_t size) {
|
||||
void *val;
|
||||
__asm__ __volatile__( "int $0x40":"=a"(val):"a"(68),"b"(12),"c"(size));
|
||||
return val;
|
||||
}
|
||||
|
||||
static inline void zfree(void *p)
|
||||
{
|
||||
size_t foo;
|
||||
asm volatile ("int $0x40":"=a"(foo):"a"(68), "b"(13), "c"(p));
|
||||
return;
|
||||
}
|
||||
|
||||
#define STBTT_malloc(x,u) zmalloc(x)
|
||||
#define STBTT_free(x,u) zfree(x)
|
||||
|
||||
#define assert_g(ignore)((void) 0)
|
||||
|
||||
#define STBTT_assert(x) assert_g(x)
|
||||
|
||||
int strlen_g(const char* string)
|
||||
{
|
||||
int i;
|
||||
i=0;
|
||||
while (*string++) i++;
|
||||
return i;
|
||||
}
|
||||
|
||||
#define STBTT_strlen(x) strlen_g(x)
|
||||
|
||||
void* zmemset(void *mem, int c, unsigned size)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
for ( i = 0; i < size; i++ )
|
||||
*((char *)mem+i) = (char) c;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void* zmemcpy(void *dst, const void *src, unsigned size)
|
||||
{
|
||||
|
||||
unsigned i;
|
||||
|
||||
for ( i = 0; i < size; i++)
|
||||
*(char *)(dst+i) = *(char *)(src+i);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define STBTT_memcpy zmemcpy
|
||||
#define STBTT_memset zmemset
|
@ -1,320 +0,0 @@
|
||||
#define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation
|
||||
#include "stb.h"
|
||||
#include "font_droid.h"
|
||||
|
||||
#define __stdcall __attribute__((stdcall))
|
||||
|
||||
inline void getzone(unsigned int x, unsigned int y, unsigned int w, unsigned int h, char * image)
|
||||
{
|
||||
unsigned size, psize;
|
||||
|
||||
size=x*65536+y;
|
||||
psize=w*65536+h;
|
||||
__asm__ __volatile__("int $0x40"::"a"(36),"b"(image),"c"(psize),"d"(size));
|
||||
}
|
||||
|
||||
// inline void f65(unsigned x, unsigned y, unsigned w, unsigned h, char *d)
|
||||
//{
|
||||
//asm("pusha");
|
||||
//asm ("nop"::"D"(0), "c"(w*65536+h), "d"(x*65536+y), "b"(d));
|
||||
//asm ("xor %eax, %eax");
|
||||
//asm ("movl %eax, %ebp");
|
||||
//asm ("pushl $24");
|
||||
//asm ("popl %esi");
|
||||
//asm ("int $0x40"::"a"(65));
|
||||
//asm("popa");
|
||||
//}
|
||||
|
||||
inline void PutImage(unsigned x, unsigned y, unsigned w, unsigned h, char * image)
|
||||
{
|
||||
unsigned size, psize;
|
||||
size=x*65536+y;
|
||||
psize=w*65536+h;
|
||||
__asm__ __volatile__("int $0x40"::"a"(7),"b"(image),"c"(psize),"d"(size));
|
||||
}
|
||||
|
||||
void kputc(char *c)
|
||||
{
|
||||
asm ("int $0x40"::"a"(63), "b"(1), "c"(c));
|
||||
}
|
||||
|
||||
void kol_board_puti(int n)
|
||||
{
|
||||
char c;
|
||||
int i = 0;
|
||||
do
|
||||
{
|
||||
if (n<0) {n=-n;kputc("-");}
|
||||
c = n % 10 + '0';
|
||||
asm ("int $0x40"::"a"(63), "b"(1), "c"(c));
|
||||
i++;
|
||||
}
|
||||
while ((n /= 10) > 0);
|
||||
}
|
||||
|
||||
void font_blit(unsigned char *dst,unsigned char *src, int x, int size) {
|
||||
int lp;
|
||||
float mixed, tp;
|
||||
unsigned char color1;
|
||||
int color2, color3, color4;
|
||||
color2=((x)&0x000000FF);
|
||||
color3=((((x)&0x00FF00)>>8)&0xFF);
|
||||
color4=((((x)&0xFF0000)>>16)&0xFF);
|
||||
//kol_board_puti(size);
|
||||
for (lp=0;lp <= size;lp++) {
|
||||
//kol_board_puti(lp);
|
||||
//kputc("\n");
|
||||
if (src[lp]>0) {
|
||||
tp=((float)src[lp])/255;
|
||||
|
||||
color1=dst[lp*3];
|
||||
mixed=(color1 * (1-tp)+color2 * tp);
|
||||
dst[lp*3]=(unsigned char) mixed;
|
||||
|
||||
color1=dst[lp*3+1];
|
||||
mixed=(color1 * (1-tp)+color3 * tp);
|
||||
dst[lp*3+1]=(unsigned char) mixed;
|
||||
|
||||
color1=dst[lp*3+2];
|
||||
mixed=(color1 * (1-tp)+color4 * tp);
|
||||
dst[lp*3+2]=(unsigned char) mixed;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
inline unsigned kol_process_info(unsigned slot, char buf1k[])
|
||||
{
|
||||
asm ("int $0x40"::"a"(9), "b"(buf1k), "c"(slot));
|
||||
}
|
||||
|
||||
void kol_board_puts(char *s)
|
||||
{
|
||||
unsigned i;
|
||||
i = 0;
|
||||
while (*(s+i))
|
||||
{
|
||||
asm ("int $0x40"::"a"(63),"b"(1),"c"(*(s+i)));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
int dos2utf (unsigned char some){
|
||||
|
||||
int same;
|
||||
|
||||
if (some>=0x80 && some <=0xAF) {same=(4<<8)+some-0x70;}
|
||||
else if (some>=0xE0 && some <=0xEF) {same=(4<<8)+some-0xA0;}
|
||||
else {same=some;}
|
||||
return same;
|
||||
|
||||
|
||||
}
|
||||
|
||||
//unsigned char screen[40*160];
|
||||
|
||||
int __stdcall get_width(char *text, char *buffer, int height)
|
||||
{
|
||||
|
||||
stbtt_fontinfo font;
|
||||
int i,j,ascent,baseline,ch,xpos=0;
|
||||
float scale=0;
|
||||
|
||||
baseline=10;
|
||||
|
||||
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");}
|
||||
kol_board_puts("Engine init..\n");
|
||||
//kol_board_puti(&screen);
|
||||
scale = stbtt_ScaleForPixelHeight(&font, height*3/4);
|
||||
int advance,lsb;
|
||||
|
||||
while (dos2utf(text[ch])) {
|
||||
// kol_board_puts("new symbol...\n");
|
||||
|
||||
stbtt_GetCodepointHMetrics(&font, dos2utf(text[ch]), &advance, &lsb);
|
||||
xpos += (advance * scale);
|
||||
if (text[ch+1])
|
||||
xpos += scale*stbtt_GetCodepointKernAdvance(&font, dos2utf(text[ch]),dos2utf(text[ch+1]));
|
||||
++ch;
|
||||
}
|
||||
stbtt_GetCodepointHMetrics(&font, dos2utf(text[ch]), &advance, &lsb);
|
||||
xpos += (advance * scale);
|
||||
return xpos;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int __stdcall get_length(char *text, char *buffer, int height, int max_len)
|
||||
{
|
||||
|
||||
stbtt_fontinfo font;
|
||||
int i,j,ascent,baseline,ch,xpos=0;
|
||||
float scale=0;
|
||||
|
||||
baseline=10;
|
||||
|
||||
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");}
|
||||
//kol_board_puts("Engine init..\n");
|
||||
//kol_board_puti(&screen);
|
||||
scale = stbtt_ScaleForPixelHeight(&font, height*3/4);
|
||||
|
||||
while (dos2utf(text[ch])) {
|
||||
// kol_board_puts("new symbol...\n");
|
||||
int advance,lsb;
|
||||
stbtt_GetCodepointHMetrics(&font, dos2utf(text[ch]), &advance, &lsb);
|
||||
xpos += (advance * scale);
|
||||
if ((int)xpos>max_len) return ch;
|
||||
if (text[ch+1])
|
||||
xpos += scale*stbtt_GetCodepointKernAdvance(&font, dos2utf(text[ch]),dos2utf(text[ch+1]));
|
||||
++ch;
|
||||
}
|
||||
return ch;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int __stdcall picture(char *text, char *buffer, char *screen1, int width, int height)
|
||||
{
|
||||
|
||||
//unsigned char *screen;
|
||||
//screen=zmalloc(20*78);
|
||||
//kol_board_puts(screen);
|
||||
//kol_board_puts("It was text\n");
|
||||
|
||||
stbtt_fontinfo font;
|
||||
int i,j,ascent,baseline,descent,ch=0;
|
||||
float scale, xpos=0;
|
||||
|
||||
//baseline=10;
|
||||
kol_board_puts("Font address:\n");
|
||||
//kol_board_puti(buffer);
|
||||
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");}
|
||||
|
||||
//kol_board_puti(&screen);
|
||||
scale = stbtt_ScaleForPixelHeight(&font, height*3/4);
|
||||
//stbtt_GetFontVMetrics(&font, &ascent,0,0);
|
||||
stbtt_GetFontVMetrics(&font, &ascent,&descent,0); //lev
|
||||
//baseline = (int) (ascent*scale);
|
||||
baseline = (int) ((ascent-descent)*scale); //lev
|
||||
|
||||
|
||||
//kol_board_puts("Text render:\n");
|
||||
|
||||
while (dos2utf(text[ch])) {
|
||||
//kol_board_puts("new symbol...\n");
|
||||
int advance,lsb,x0,y0,x1,y1;
|
||||
//float x_shift = xpos - (float) i_floor(xpos);
|
||||
|
||||
// kol_board_puts("floor called!\n");
|
||||
stbtt_GetCodepointHMetrics(&font, dos2utf(text[ch]), &advance, &lsb);
|
||||
stbtt_GetCodepointBitmapBoxSubpixel(&font, dos2utf(text[ch]), scale,scale,0,0, &x0,&y0,&x1,&y1);
|
||||
|
||||
|
||||
//10= y0, 20=y1-y0 or so
|
||||
stbtt_MakeCodepointBitmapSubpixel(&font, &screen1[(baseline + y0)*width+ (int)xpos + x0], x1-x0,y1-y0, width, scale,scale,0,0, dos2utf(text[ch]));
|
||||
|
||||
// note that this stomps the old data, so where character boxes overlap (e.g. 'lj') it's wrong
|
||||
// because this API is really for baking character bitmaps into textures. if you want to do this,
|
||||
// you need to render the bitmap to a temp buffer, then\n\t"alpha blend" that into the working buffer
|
||||
xpos += (advance * scale);
|
||||
if (text[ch+1])
|
||||
xpos += scale*stbtt_GetCodepointKernAdvance(&font, dos2utf(text[ch]),dos2utf(text[ch+1]));
|
||||
++ch;
|
||||
}
|
||||
|
||||
//zmemcpy(screen1,bitmap,20*20);
|
||||
|
||||
//kol_board_puts("finished...\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int __stdcall text_out(char *string, char *buffer, int height, int color, int x, int y) {
|
||||
unsigned char *from_zone;
|
||||
unsigned char *to_zone;
|
||||
int px, py;
|
||||
unsigned char app_data[1024];
|
||||
|
||||
int width=0;
|
||||
//kol_board_puts("width...\n");
|
||||
//kol_board_puts("\n and now height is ...");
|
||||
//kol_board_puti(height);
|
||||
|
||||
width=get_width(string,buffer,height);
|
||||
//kol_board_puts("\n Width is ...");
|
||||
//kol_board_puti(width);
|
||||
//kol_board_puts(" and height is ...");
|
||||
//kol_board_puti(height);
|
||||
|
||||
//kol_board_puts("\n malloc...\n");
|
||||
from_zone=(char*)zmalloc(3*height*width);
|
||||
to_zone=(char*)zmalloc(height*width);
|
||||
//kol_board_puts("malloc done...\n");
|
||||
|
||||
kol_process_info(-1, app_data);
|
||||
//px=app_data[35]*256+app_data[34];
|
||||
px=app_data[35]*256+app_data[34]+app_data[55]*256+app_data[54];//lev
|
||||
//py=app_data[39]*256+app_data[38];
|
||||
py=app_data[39]*256+app_data[38]+app_data[59]*256+app_data[58];//lev
|
||||
|
||||
//kol_board_puts("\nzone...\n");
|
||||
getzone(px+x, py+y, width, height, from_zone);
|
||||
//kol_board_puts("render...\n");
|
||||
picture(string, buffer, to_zone, width, height);
|
||||
//kol_board_puts("blit...\n");
|
||||
font_blit(from_zone,to_zone, color, width*height);
|
||||
//kol_board_puts("out...\n");
|
||||
//f65(x,y,width,height,from_zone);
|
||||
PutImage(x,y,width,height,from_zone);//lev
|
||||
zfree(from_zone);
|
||||
zfree(to_zone);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int __stdcall start()
|
||||
{
|
||||
|
||||
return 1;
|
||||
};
|
||||
|
||||
int __stdcall version_major()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int __stdcall version_minor()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char *name;
|
||||
void *f;
|
||||
}export_t;
|
||||
|
||||
char szStart[] ="START";
|
||||
char szVersion[] ="version";
|
||||
char szVersionM[] ="version_min";
|
||||
char szTrueType[] ="truetype";
|
||||
char szGetLength[]="get_length";
|
||||
char szGetWidth[] ="get_width";
|
||||
char szTextOut[] ="text_out";
|
||||
|
||||
|
||||
|
||||
export_t EXPORTS[] __asm__("EXPORTS") =
|
||||
{
|
||||
{ szStart, start },
|
||||
{ szVersion, version_major },
|
||||
{ szVersionM, version_minor },
|
||||
{ szTrueType, picture },
|
||||
{ szGetLength, get_length},
|
||||
{ szGetWidth, get_width},
|
||||
{ szTextOut, text_out },
|
||||
{ NULL, NULL },
|
||||
};
|
File diff suppressed because it is too large
Load Diff
281
programs/develop/libraries/truetype/trunk/truetype.c
Normal file
281
programs/develop/libraries/truetype/trunk/truetype.c
Normal file
@ -0,0 +1,281 @@
|
||||
#include "glue.h"
|
||||
#define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation
|
||||
#include "stb_truetype.h"
|
||||
|
||||
#define __stdcall __attribute__((stdcall))
|
||||
|
||||
// "Fast and economical" UTF-8 to codepoint decoder by Bjoern Hoehrmann.
|
||||
// Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
|
||||
// See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
|
||||
|
||||
#define UTF8_ACCEPT 0
|
||||
#define UTF8_REJECT 12
|
||||
|
||||
static const unsigned char utf8d[] = {
|
||||
// The first part of the table maps bytes to character classes that
|
||||
// to reduce the size of the transition table and create bitmasks.
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
|
||||
10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
|
||||
|
||||
// The second part is a transition table that maps a combination
|
||||
// of a state of the automaton and a character class to a state.
|
||||
0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
|
||||
12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
|
||||
12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
|
||||
12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
|
||||
12,36,12,12,12,12,12,12,12,12,12,12,
|
||||
};
|
||||
|
||||
unsigned int inline decode(unsigned int* state, unsigned int* codep, unsigned int byte) {
|
||||
unsigned int type = utf8d[byte];
|
||||
|
||||
*codep = (*state != UTF8_ACCEPT) ?
|
||||
(byte & 0x3fu) | (*codep << 6) :
|
||||
(0xff >> type) & (byte);
|
||||
|
||||
*state = utf8d[256 + *state + type];
|
||||
return *state;
|
||||
}
|
||||
|
||||
inline void PutImage(unsigned x, unsigned y, unsigned w, unsigned h, char * image){
|
||||
unsigned size, psize;
|
||||
size=x*65536+y;
|
||||
psize=w*65536+h;
|
||||
__asm__ __volatile__("int $0x40"::"a"(7),"b"(image),"c"(psize),"d"(size));
|
||||
}
|
||||
|
||||
void font_blit(unsigned char *dst,unsigned char *src, int textcolor, int size) {
|
||||
int lp;
|
||||
float mixed, tp;
|
||||
unsigned char color1;
|
||||
int red, green, blue;
|
||||
red=((textcolor)&0x000000FF);
|
||||
green=(((textcolor)&0x00FF00)>>8);
|
||||
blue=(((textcolor)&0xFF0000)>>16);
|
||||
|
||||
for (lp=0;lp <= size;lp++) {
|
||||
if (src[lp]>0) {
|
||||
tp=((float)src[lp])/255;
|
||||
|
||||
color1=dst[lp*3];
|
||||
mixed=(color1 * (1-tp)+red * tp);
|
||||
dst[lp*3]=(unsigned char) mixed;
|
||||
|
||||
color1=dst[lp*3+1];
|
||||
mixed=(color1 * (1-tp)+green * tp);
|
||||
dst[lp*3+1]=(unsigned char) mixed;
|
||||
|
||||
color1=dst[lp*3+2];
|
||||
mixed=(color1 * (1-tp)+blue * tp);
|
||||
dst[lp*3+2]=(unsigned char) mixed;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
inline unsigned kol_process_info(unsigned slot, char buf1k[]){
|
||||
asm ("int $0x40"::"a"(9), "b"(buf1k), "c"(slot));
|
||||
}
|
||||
|
||||
int __stdcall init_font(stbtt_fontinfo *font,unsigned char *FontData)
|
||||
{
|
||||
stbtt_InitFont(font, FontData,stbtt_GetFontOffsetForIndex(FontData,0) );
|
||||
}
|
||||
|
||||
int __stdcall get_width_utf8(unsigned char *s, stbtt_fontinfo *buffer, int height)
|
||||
{
|
||||
stbtt_fontinfo *font;
|
||||
font=buffer;
|
||||
float scale, xpos=0;
|
||||
|
||||
unsigned int codepoint;
|
||||
unsigned int state = 0;
|
||||
|
||||
scale = stbtt_ScaleForPixelHeight(font, height*3/4);
|
||||
int advance,lsb;
|
||||
|
||||
for (; *s; s++) {
|
||||
if (!decode(&state, &codepoint, *s)) {
|
||||
stbtt_GetCodepointHMetrics(font, codepoint, &advance, &lsb);
|
||||
xpos += (advance * scale);
|
||||
}
|
||||
}
|
||||
|
||||
return xpos;
|
||||
}
|
||||
|
||||
|
||||
int __stdcall get_length_utf8(unsigned char *s, char *buffer, int height, int max_len)
|
||||
{
|
||||
stbtt_fontinfo *font;
|
||||
font=buffer;
|
||||
int ch=0, xpos=0;
|
||||
float scale=0;
|
||||
|
||||
scale = stbtt_ScaleForPixelHeight(&font, height*3/4);
|
||||
int advance, lsb;
|
||||
|
||||
unsigned int codepoint;
|
||||
unsigned int state = 0;
|
||||
|
||||
for (; s[ch]; ch++) {
|
||||
if (!decode(&state, &codepoint, s[ch])) {
|
||||
|
||||
stbtt_GetCodepointHMetrics(&font, codepoint, &advance, &lsb);
|
||||
xpos += (advance * scale);
|
||||
|
||||
if ((int)xpos>max_len)
|
||||
return ch;
|
||||
}
|
||||
}
|
||||
return ch;
|
||||
}
|
||||
|
||||
|
||||
int __stdcall picture_utf8(unsigned char *s, stbtt_fontinfo *buffer, char *screen1, int width, int height)
|
||||
{
|
||||
stbtt_fontinfo *font;
|
||||
font=buffer;
|
||||
int ascent, baseline, descent;
|
||||
int advance, lsb, x0, y0, x1, y1;
|
||||
float scale, xpos=0;
|
||||
|
||||
scale = stbtt_ScaleForPixelHeight(font, height*3/4);
|
||||
stbtt_GetFontVMetrics(font, &ascent, &descent,0);
|
||||
baseline = (int) ((ascent-descent)*scale);
|
||||
|
||||
unsigned int codepoint;
|
||||
unsigned int state = 0;
|
||||
|
||||
for (; *s; s++) {
|
||||
if (!decode(&state, &codepoint, *s)) {
|
||||
stbtt_GetCodepointHMetrics(font, codepoint, &advance, &lsb); ///////////////////////////////////
|
||||
stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale, scale, 0, 0, &x0, &y0, &x1, &y1); ///////////////////////////////////
|
||||
stbtt_MakeCodepointBitmapSubpixel(font, &screen1[(baseline+y0)*width+(int)xpos+x0], x1-x0, y1-y0, width, scale, scale, 0, 0, codepoint);
|
||||
|
||||
xpos += (advance*scale);
|
||||
}
|
||||
//if (state != UTF8_ACCEPT)
|
||||
//kol_board_puts("The string is not well-formed\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void __stdcall SetBackColor(int back_color, int width, int height, char *from_zone)
|
||||
{
|
||||
int i,j;
|
||||
unsigned char bcr,bcg,bcb;
|
||||
|
||||
bcr=back_color<<16;
|
||||
bcg=back_color<<8;
|
||||
bcb=back_color;
|
||||
|
||||
for (j=0;j<height ;j++)
|
||||
{
|
||||
for (i=0;i<width ;i++)
|
||||
{
|
||||
from_zone[(j*width+i)*3]=back_color;
|
||||
from_zone[(j*width+i)*3+1]=back_color>>8;
|
||||
from_zone[(j*width+i)*3+2]=back_color>>16;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int __stdcall text_out(char *string, char *buffer, int height, int color,int back_color, int x, int y) {
|
||||
unsigned char *from_zone;
|
||||
unsigned char *to_zone;
|
||||
int px, py;
|
||||
unsigned char app_data[1024];
|
||||
int width;
|
||||
|
||||
width = get_width_utf8(string,buffer,height);
|
||||
|
||||
from_zone=(char*)zmalloc(3*height*width);
|
||||
to_zone=(char*)zmalloc(height*width);
|
||||
|
||||
kol_process_info(-1, app_data);
|
||||
//px=app_data[35]*256+app_data[34];
|
||||
px=app_data[35]*256+app_data[34]+app_data[55]*256+app_data[54];//lev
|
||||
//py=app_data[39]*256+app_data[38];
|
||||
py=app_data[39]*256+app_data[38]+app_data[59]*256+app_data[58];//lev
|
||||
|
||||
//getzone(px+x, py+y, width, height, from_zone);
|
||||
SetBackColor(back_color, width, height,from_zone);
|
||||
picture_utf8(string, buffer, to_zone, width, height);
|
||||
font_blit(from_zone,to_zone, color, width*height);
|
||||
//f65(x,y,width,height,from_zone);
|
||||
PutImage(x,y,width,height,from_zone);//lev
|
||||
|
||||
zfree(from_zone);
|
||||
zfree(to_zone);
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned char* __stdcall text_out_mem(short *string, stbtt_fontinfo *buffer, int height, int color,int back_color) {
|
||||
unsigned char *from_zone;
|
||||
unsigned char *to_zone;
|
||||
int width;
|
||||
|
||||
width = get_width_utf8(string,buffer,height);
|
||||
from_zone=(char*)zmalloc(3*height*width+8);
|
||||
to_zone=(char*)zmalloc(height*width);
|
||||
*(int*)from_zone = width;
|
||||
*(int*)(from_zone+4) = height;
|
||||
SetBackColor(back_color, width, height,from_zone+8);
|
||||
picture_utf8(string, buffer, to_zone, width, height);
|
||||
font_blit(from_zone+8, to_zone, color, width*height);
|
||||
|
||||
zfree(to_zone);
|
||||
return from_zone;
|
||||
}
|
||||
|
||||
int __stdcall start(){
|
||||
return 1;
|
||||
}
|
||||
|
||||
int __stdcall version_major(){
|
||||
return 1;
|
||||
}
|
||||
|
||||
int __stdcall version_minor(){
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct{
|
||||
char *name;
|
||||
void *f;
|
||||
}export_t;
|
||||
|
||||
char szStart[] ="START";
|
||||
char szVersion[] ="version";
|
||||
char szVersionM[] ="version_min";
|
||||
char szTrueType[] ="truetype";
|
||||
char szGetLength[] ="get_length";
|
||||
char szGetWidth[] ="get_width";
|
||||
char szTextOut[] ="text_out";
|
||||
char szTextOutMem[] ="text_out_mem";
|
||||
char szInitFont[] ="init_font";
|
||||
|
||||
|
||||
export_t EXPORTS[] __asm__("EXPORTS") =
|
||||
{
|
||||
{ szStart, start },
|
||||
{ szVersion, version_major },
|
||||
{ szVersionM, version_minor },
|
||||
{ szTrueType, picture_utf8 },
|
||||
{ szGetLength, get_length_utf8},
|
||||
{ szGetWidth, get_width_utf8},
|
||||
{ szTextOut, text_out },
|
||||
{ szTextOutMem, text_out_mem},
|
||||
{ szInitFont, init_font},
|
||||
{ NULL, NULL },
|
||||
};
|
Loading…
Reference in New Issue
Block a user