#include "ogFont.h"
#include "objgfx30.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
ogFont::ogFont(void) {
memset(FontDataIdx,0,sizeof(FontDataIdx));
memset(CharWidthTable,0,sizeof(CharWidthTable));
memset(CharHeightTable,0,sizeof(CharHeightTable));
FontData = 0;
FontDataSize = 0;
return;
} // TFont::TFont
void
ogFont::centerTextX(ogSurface& Buf, int32 y, const char * TextString ) {
int32 x;
x = ((Buf.ogGetMaxX()+1)-textWidth(TextString)) / 2;
putText(Buf,x,y,TextString);
return;
} // TFont::centerTextX
void
ogFont::justifyText(ogSurface& Buf, uInt8 Horiz, uInt8 Vert, const char * TextString) {
uInt32 x,y;
switch (Horiz) {
case LeftText:
x = 0;
break;
case CenterText:
x = ((Buf.ogGetMaxX())-textWidth(TextString)) / 2;
break;
case RightText:
x = (Buf.ogGetMaxX())-textWidth(TextString);
break;
default:
return;
} // switch
switch (Vert) {
case TopText:
y = 0;
break;
case CenterText:
y = ((Buf.ogGetMaxY())-textHeight(TextString)) / 2;
break;
case BottomText:
y = (Buf.ogGetMaxY())-textHeight(TextString);
default:
return;
} // switch
putText(Buf,x,y,TextString);
return;
} // TFont::justifyText
bool
ogFont::load(const char* FontFile) {
return loadFrom(FontFile,0);
} // TFont::Load
bool
ogFont::loadFrom(const char* FontFile, uInt32 Offset) {
return true;
} // TFont::loadFrom
bool
ogFont::save(const char* FontFile) {
return saveTo(FontFile,0);
} // TFont::save
uInt32
ogFont::textHeight(const char * TextString) {
uInt32 size, tmpsize;
size = 0;
if (TextString)
while (*TextString) {
tmpsize = CharHeightTable[*TextString++];
if (tmpsize>size) size = tmpsize;
}
return size;
} // TFont::textHeight
uInt32
ogFont::textWidth(const char * TextString) {
uInt32 size=0;
if (TextString)
while (*TextString)
size+=CharWidthTable[*TextString++];
return size;
} // TFont::textWidth
ogFont::~ogFont(void) {
if (FontDataSize) free(FontData);
return;
} // TFont::~TFont
/* TBMFont methods */
void
ogBMFont::setColor(uInt8 r, uInt8 g, uInt8 b) {
ColourTable[1].red = r;
ColourTable[1].green = g;
ColourTable[1].blue = b;
return;
} // TBMFont::setColor
void
ogBMFont::setIdxColor(uInt8 idx, uInt8 r, uInt8 g, uInt8 b) {
if (idx>=31) return;
ColourTable[idx].red = r;
ColourTable[idx].green = g;
ColourTable[idx].blue = b;
return;
} // TBMFont::setIdxColor
void
ogBMFont::putText(ogSurface& Buf, int32 x, int32 y, const char * TextString) {
uInt32 ct[32];
uInt32 xx, yy;
uInt8 * offset;
uInt8 pix;
char ch;
if (!FontData) return;
if (!TextString) return;
for (xx=0; xx<=31; xx++)
ct[xx]=Buf.ogRGB(ColourTable[xx].red,
ColourTable[xx].green,
ColourTable[xx].blue);
while (*TextString) {
ch = *TextString++;
if ((CharWidthTable[ch]) && (ch!=' ')) {
offset=(uInt8 *)(FontData);
offset+=FontDataIdx[ch];
for (yy=0; yy<=(uInt32)(CharHeightTable[ch]-1); yy++)
for (xx=0; xx<=(uInt32)(CharWidthTable[ch]-1); xx++) {
pix = *offset++;
if ((pix) && (pix<=31)) Buf.ogSetPixel(x+xx,y+yy,ct[pix]);
} // for xx
x+=CharWidthTable[ch];
} // if charwidthtable
} // while
return;
} // TBMFont::putText
/* TDPFont methods */
bool
ogDPFont::saveTo(const char * FontFile, int32 Offset) {
return true;
} // TDPFont::saveTo
void
ogDPFont::putText(ogSurface& Buf, int32 x, int32 y, const char * TextString) {
uInt32 xx, xcount, ycount;
uInt8 * offset;
uInt8 bits = 0;
char ch;
if (!FontData) return;
if (!TextString) return;
while (*TextString) {
ch = *TextString++;
if ((CharWidthTable[ch]) && (ch!=' ')) {
offset=(uInt8 *)(FontData);
offset+=FontDataIdx[ch];
for (ycount = 0; ycount<=(uInt32)(Height-1); ycount++) {
xcount = CharWidthTable[ch];
xx = 0;
do {
if ((xx & 7)==0) bits = *(offset++);
if (bits & 128)
Buf.ogSetPixel(x+xx,y+ycount,
Buf.ogRGB(ColourTable[1].red,
ColourTable[1].green,
ColourTable[1].blue));
xx++;
bits+=bits;
} while (--xcount);
} // for
} // if
x+=CharWidthTable[ch];
} // while
} // TDPFont::putText
bool
ogDPFont::loadFrom(const char * FontFile, uInt32 Offset) {
FILE * infile;
ogDPFHeader header;
uInt32 lresult, size;
if (FontData) free(FontData);
infile = fopen(FontFile,"r");
fseek(infile,Offset,SEEK_SET);
lresult = fread(&header,sizeof(header),1,infile);
Width = header.Width;
Height = header.Height;
NumOfChars = header.NumOfChars;
if (!NumOfChars) NumOfChars = 256;
StartingChar = header.StartingChar;
memset(FontDataIdx,0,sizeof(FontDataIdx));
memset(CharWidthTable,0,sizeof(CharWidthTable));
memset(CharHeightTable,0,sizeof(CharHeightTable));
size = ((Width+7) / 8)*Height;
FontDataSize = size*NumOfChars;
for (int tmp=StartingChar; tmp<=StartingChar+NumOfChars-1; tmp++) {
CharWidthTable[tmp]=Width;
CharHeightTable[tmp]=Height;
FontDataIdx[tmp]=(size*(tmp-StartingChar));
} // for
FontData = malloc(FontDataSize);
lresult = fread(FontData,1,FontDataSize,infile);
fclose(infile);
return true;
} // TDPFont::loadFrom