int32 func__loadfont(qbs *f,int32 size,qbs *requirements,int32 passed){
//f=_LOADFONT(ttf_filename$,height[,"bold,italic,underline,monospace,dontblend,unicode"])
if (new_error) return NULL;
qbs *s1=NULL; s1=qbs_new(0,0);
qbs *req=NULL; req=qbs_new(0,0);
qbs *s3=NULL; s3=qbs_new(0,0);
uint8 r[32];
int32 i,i2,i3;
static int32 recall;
//validate size
if (size<1){error(5); return NULL;}
if (size>2048) return -1;
//load the file
if (!f->len) return -1;//return invalid handle if null length string
int32 fh,result;
int64 bytes;
fh=gfs_open(f,1,0,0);
#ifdef QB64_WINDOWS //rather than just immediately tossing an error, let's try looking in the default OS folder for the font first in case the user left off the filepath.
if (fh<0&&recall==0) {
recall=-1; //to set a flag so we don't get trapped endlessly recalling the routine when the font actually doesn't exist
i=func__loadfont(qbs_add(qbs_new_txt("C:/Windows/Fonts/"),f), size, requirements,passed); //Look in the default windows font location
return i;
}
#endif
recall=0;
if (fh<0) return -1; //If we still can't load the font, then we just can't load the font... Send an error code back.
//check requirements
memset(r,0,32);
if (passed){
if (requirements->len){
i=1;
qbs_set(req,qbs_ucase(requirements));//convert tmp str to perm str
nextrequirement:
i2=func_instr(i,req,qbs_new_txt(","),1);
if (i2){
qbs_set(s1,func_mid(req,i,i2-i,1));
}else{
qbs_set(s1,func_mid(req,i,req->len-i+1,1));
}
qbs_set(s1,qbs_rtrim(qbs_ltrim(s1)));
if (qbs_equal(s1,qbs_new_txt("BOLD"))){r[0]++; goto valid;}
if (qbs_equal(s1,qbs_new_txt("ITALIC"))){r[1]++; goto valid;}
if (qbs_equal(s1,qbs_new_txt("UNDERLINE"))){r[2]++; goto valid;}
if (qbs_equal(s1,qbs_new_txt("DONTBLEND"))){r[3]++; goto valid;}
if (qbs_equal(s1,qbs_new_txt("MONOSPACE"))){r[4]++; goto valid;}
if (qbs_equal(s1,qbs_new_txt("UNICODE"))){r[5]++; goto valid;}
error(5); return NULL;//invalid requirements
valid:
if (i2){i=i2+1; goto nextrequirement;}
for (i=0;i<32;i++) if (r[i]>1){error(5); return NULL;}//cannot define requirements twice
}//->len
}//passed
int32 options;
options=r[0]+(r[1]<<1)+(r[2]<<2)+(r[3]<<3)+(r[4]<<4)+(r[5]<<5);
//1 bold TTF_STYLE_BOLD
//2 italic TTF_STYLE_ITALIC
//4 underline TTF_STYLE_UNDERLINE
//8 dontblend (blending is the default in 32-bit alpha-enabled modes)
//16 monospace
//32 unicode
bytes=gfs_lof(fh);
static uint8* content;
content=(uint8*)malloc(bytes); if (!content){gfs_close(fh); return -1;}
result=gfs_read(fh,-1,content,bytes);
gfs_close(fh);
if (result<0){free(content); return -1;}
//open the font
//get free font handle
for (i=32;i<=lastfont;i++){
if (!font[i]) goto got_font_index;
}
//increase handle range
lastfont++;
font=(int32*)realloc(font,4*(lastfont+1)); font[lastfont]=NULL;
fontheight=(int32*)realloc(fontheight,4*(lastfont+1));
fontwidth=(int32*)realloc(fontwidth,4*(lastfont+1));
fontflags=(int32*)realloc(fontflags,4*(lastfont+1));
i=lastfont;
got_font_index:
static int32 h;
h=FontLoad(content,bytes,size,-1,options);
free(content);
if (!h) return -1;
font[i]=h;
fontheight[i]=size;
fontwidth[i]=FontWidth(h);
fontflags[i]=options;
return i;
}