//super-cool-antialias-truetype font writer
#define PM_DESIGNED 0
#define PM_CENTERED 1
#define PM_ALIGN_LEFT 2
#define PM_ALIGN_RIGHT 3

int text2buf(dword s;char color);
void outtext(int x0,y0; dword s; float size,int color,char position);
void paste_pbuf(int x0,y0,len,hgt,u0,v0,_len,_hgt);

#include "ttf.h--"

#define tlength 192
#define tbytes 1516
#define ch_hgt 49
#define ch_wid_def 40

char pbuf[1024*ch_hgt];
int ch_start,ch_wid;
char font_table[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

//locating bit
char fastcall p_ttf(ECX)
{
	$push EDI
	EDI=ECX>>3;
	ECX&=7;
	EDX=128>>CL;
	IF(ttf[EDI]&DL)AL=255;
	ELSE AL=0;
	$pop EDI
}

//getting start & width
char fastcall get_char_info(AL)
{
//founding this char in font table
	$push DS
	$pop ES
	EDI=#font_table;
	ECX=sizeof(font_table)-1;
	$repnz $scasb
// if found
	IF (ZEROFLAG) {
		$push EBX
		EBX=sizeof(font_table)-1-ECX;
//define character offset
		ESI=0;
		FOR (EDI=0; EDI<EBX; EDI++) {
			WHILE(p_ttf(ESI)==255)ESI++;
			ESI++;
		}
		ch_start=ESI;
//define character width
		ESI++;
		WHILE(p_ttf(ESI)==255)ESI++;
		ch_wid=ESI-ch_start;
		AL=1;
		$pop EBX
//success, else
	}
	ELSE{
		ch_wid=ch_wid_def;
		AL=0;
	}
}

//pasting
void paste_pbuf(int x0,y0,len,hgt,u0,v0,_len,_hgt)
{
int u,du,dv;
	IF(hgt>0)&&(len>0){
//antialias
		du=1<<16*_hgt/hgt;
		dv=1<<16*_len/len;
		u=u0;
		FOR (EDI=y0;y0+hgt>EDI;EDI++){
			EBX=v0;
			FOR (ESI=x0;x0+len>ESI;ESI++){
				ECX=u>>16<<10;
				EDX=EBX>>16;
				AL=pbuf[ECX+EDX];
				IF(AL!=0)putpixel(ESI,EDI,AL);
				EBX+=dv;
			}
			u+=du;
		}
	}
}

//filling
int text2buf(dword s;char color)
{
int k,t,x,cf;
int b_ofs;
	x=0;
	EBX=s;
	while (DSBYTE[EBX]!=0){
		cf=get_char_info(DSBYTE[EBX]);
		EBX++;
		FOR(ESI=0;ESI<ch_hgt;ESI++){
			b_ofs=ESI+1*tlength;
			FOR(EDI=0;EDI<ch_wid;EDI++){
				k=EDI+x;
				IF (EAX<1024){
					t=EDI+ch_start;
					IF(!cf)AL=0;
					ELSE{
						EAX=t;
						ECX=EAX&7;
						EDX=128>>CL;
						ECX=EAX>>3+b_ofs;
						AL=ttf[ECX]&DL;
					}
					IF(AL!=0)AL=color;
					EDX=ESI<<10+k;
					pbuf[EDX]=AL;
				}
			}
		}
		x+=ch_wid;
	}
	return(x);
}

//general
:void outtext(int x0, int y0; dword s; float size,int color,char position)
{
int len,hgt;
int i;
	len=text2buf(s,color);
	hgt=ch_hgt;
	IF (position==PM_CENTERED){
		x0-=float len*size/2;
		y0-=float hgt*size/2;
	}
	ECX=float len*size;
	EBX=float hgt*size;
	paste_pbuf(x0,y0,ECX,EBX,0,0,len,hgt);
}

