Listing 3 Algorithm to render a single character

/*

*This function draws a single character in the color specified.#

*/

void fontDrawMono(const CharInfo *chInfoPtr,

int x, int y,

unsigned char color)

{

unsigned int i,j;

unsigned char eightBits;

/*

* We use index as well as the loop counters because the number

* of bytes per line varies

*/

int index = 0;

for (i = 0; i < chInfoPtr->height; i++)

{

for (j = 0; j < chInfoPtr->width; j++)

{

if (j%8 == 0)

{

/*

* We are starting a new byte so get the value into eightBits

*/

eightBits = chInfoPtr->bits[index];

index++;

}

/*

* We plot the pixel if the bit is set. Note that we do not draw

* the unset bits in the background color. We assume that the

* background was cleared if necessary.

*/

if (eightBits & 0x80)

{

/*

* The bit is set so plot the pixel

*/

drawDot(x + j, y + i, color);

}

/*

* Move on to the next pixel in the character.

*/

eightBits <<= 1;

}

}

}

/*

* x,y position passed in is the start of the baseline.

* x is increased as the string is rendered from left to

* right, but y stays constant.

*/

void fontRenderString(const char *string,

const CharInfo *font,

int x, int y,

unsigned char color)

{

int yForThisChar;

const CharInfo *chInfoPtr;

while (*string)

{

chInfoPtr = &font[*string];

x = x + chInfoPtr->offsetX ;

yForThisChar = y - chInfoPtr->offsetY - chInfoPtr->height;

/*

* Draw the character as a monochrome bitmap

*/

fontDrawMono(chInfoPtr, x, yForThisChar, color);

x += chInfoPtr->width;

string++;

}

}

Back