/*
 * vtitler.c
 *
 * MPEG2AVI
 * --------
 *  v0.16B32 renamed print_string() -> vt_printstr(), 
 *      print_number() -> vt_printnum()
 *     modified the print_string() and print_number() to allow Xposition
 *  v0.16B2 initial release
 *			numerical titler (numbers only, not alphanumeric letters)
 *
   vtitler.c allows text messages to be XOR'd into the output-video.
   Currently, the XOR operation is used.
*/


#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FONT_HEIGHT 7
#define FONT_WIDTH 1

// the font is an <4 x 7> 8bpp XOR mask
// print_char() writes the font on a 4x8 grid (1 horizontal blank scanline
// between successive lines of text)
static DWORD font_numbers[][FONT_HEIGHT]={
	{	0x00FFFFFF,		// 0
		0x00FF00FF,
		0x00FF00FF,
		0x00FF00FF,
		0x00FF00FF,
		0x00FFFFFF,
		0x00000000 },

	{	0x0000FF00,		// 1
		0x0000FFFF,
		0x0000FF00,
		0x0000FF00,
		0x0000FF00,
		0x0000FF00,
		0x00000000 },

	{	0x0000FFFF,		// 2
		0x00FF0000,
		0x0000FF00,
		0x000000FF,
		0x000000FF,
		0x00FFFFFF,
		0x00000000 },

	{	0x00FFFFFF,		// 3
		0x00FF0000,
		0x0000FF00,
		0x0000FF00,
		0x00FF0000,
		0x00FFFFFF,
		0x00000000 },

	{	0x00FF00FF,		// 4
		0x00FF00FF,
		0x00FFFFFF,
		0x00FF0000,
		0x00FF0000,
		0x00FF0000,
		0x00000000 },

	{	0x00FFFFFF,		// 5
		0x000000FF,
		0x000000FF,
		0x00FFFF00,
		0x00FF0000,
		0x0000FFFF,
		0x00000000 },

	{	0x00FFFF00,		// 6
		0x000000FF,
		0x0000FFFF,
		0x00FF00FF,
		0x00FF00FF,
		0x0000FF00,
		0x00000000 },

	{	0x00FFFFFF,		// 7
		0x00FF0000,
		0x0000FF00,
		0x0000FF00,
		0x0000FF00,
		0x0000FF00,
		0x00000000 },

	{	0x00FFFFFF,		// 8
		0x00FF00FF,
		0x0000FF00,
		0x0000FF00,
		0x00FF00FF,
		0x00FFFFFF,
		0x00000000 },

	{	0x0000FF00,		// 9
		0x00FF00FF,
		0x00FF00FF,
		0x00FFFF00,
		0x00FF0000,
		0x0000FFFF,
		0x00000000 },
};
	

// print a single char "letter", to the bitmap pbmp[]
// dwStride == # DWORDS per scanline
// dwXpos == # letters to offset from start of scanline (left-justification)
// dwYpos == # letters to offset from start of scanline (top-justification)		

static void
print_char( DWORD pbmp[], char letter, DWORD dwXpos, DWORD dwYpos,
	DWORD dwStride )
{
	DWORD addr_offset = 0;
	int y;

	for ( y = 0; y < FONT_HEIGHT; ++y )
	{
		addr_offset = ( dwYpos * (FONT_HEIGHT+1) + y ) * dwStride
			+ FONT_WIDTH * dwXpos;
		if ( letter >= '0' && letter <= '9' )
			pbmp[ addr_offset ] = font_numbers[ letter - '0' ][ y ];
		else
		{	// hmm, the char "letter" is not a number!
			pbmp[ addr_offset ] ^= 0x00FFFFFF;
		}
	}
}


// dwStride == # DWORDS per scanline
// dwYpos == # letters to offset from start of scanline (top-justification)
// dwXpos == # letters to offset from start of scanline (left-justification)
// number == the desired # to emboss into the bitmap
void
vt_printnum( DWORD pbmp[], DWORD number, DWORD dwXpos, DWORD dwYpos,
	DWORD dwStride )
{
	char szText[256];
	int x, chars_to_print;

	sprintf( szText, "%10ld", number );	// convert DWORD -> string

	for ( x = 0; x < strlen( szText ); ++x )	// convert <space> -> '0'
		if ( szText[ x ] == ' ' )
			szText[ x ] = '0';	

	// truncate the string, if it's length > dwStride, dwXpos must also
	// be considered
	chars_to_print = ( strlen( szText ) + 1 + dwXpos ) > 
		( dwStride / FONT_WIDTH) ? (dwStride /FONT_WIDTH ) : strlen( szText );

	for ( x = 0; x < chars_to_print; ++x )
	{
		print_char( pbmp, szText[ x ], x+dwXpos, dwYpos, dwStride );
	}

}


void
vt_printstr( DWORD pbmp[], char szText[], DWORD dwXpos, DWORD dwYpos,
	DWORD dwStride )
{
	int x, chars_to_print;

	// truncate the string, if it's length > dwStride
	chars_to_print = ( strlen( szText ) + 1 ) > ( dwStride / FONT_WIDTH) ? 
		(dwStride /FONT_WIDTH ): strlen( szText ) ;

	for ( x = 0; x < chars_to_print; ++x )
	{
		print_char( pbmp, szText[ x ], x+dwXpos, dwYpos, dwStride );
	}

}