/*
**	Command & Conquer Red Alert(tm)
**	Copyright 2025 Electronic Arts Inc.
**
**	This program is free software: you can redistribute it and/or modify
**	it under the terms of the GNU General Public License as published by
**	the Free Software Foundation, either version 3 of the License, or
**	(at your option) any later version.
**
**	This program is distributed in the hope that it will be useful,
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
**	You should have received a copy of the GNU General Public License
**	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/***************************************************************************
 **   C O N F I D E N T I A L --- W E S T W O O D   A S S O C I A T E S   **
 ***************************************************************************
 *                                                                         *
 *                 Project Name : Westwood Library                         *
 *                                                                         *
 *                    File Name : LOADFONT.C                               *
 *                                                                         *
 *                   Programmer : Joe L. Bostic                            *
 *                                                                         *
 *                   Start Date : September 6, 1991                        *
 *                                                                         *
 *                  Last Update : June 27, 1994   [SKB]                    *
 *                                                                         *
 *-------------------------------------------------------------------------*
 * Functions:                                                              *
 *   Load_Font -- Loads a font from disk.                                  *
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

#include <wwstd.h>
#include "font.h"
#include <file.h>
#include <wwmem.h>

#if(IBM)
#include <fcntl.h>
#include <io.h>
#endif



extern "C" {
	int FontXSpacing = 0;
	int FontYSpacing = 0;
	void const *FontPtr = NULL;
}
BYTE FontWidth  = 8;
BYTE FontHeight = 8;

// only font.c and set_font.c use the following
BYTE *FontWidthBlockPtr = NULL;



/***************************************************************************
 * LOAD_FONT -- Loads a font from disk.                                    *
 *                                                                         *
 *    This loads a font from disk.  This function must be called as a    	*
 *    precursor to calling Set_Font().  You need only call this function 	*
 *    once per desired font at the beginning of your code, but AFTER     	*
 *    Prog_Init() is called.                                             	*
 *                                                                         *
 * INPUT:      name  - Pointer to font name to use (eg. "topaz.font")    	*
 *                                                                       	*
 *             fontsize - Size in points of the font loaded.             	*
 *                                                                       	*
 * OUTPUT:     Pointer to font data or NULL if unable to load.           	*
 *                                                                       	*
 * WARNINGS:   Some system memory is grabbed by this routine.            	*
 *                                                                       	*
 * HISTORY:                                                                *
 *   4/10/91    BS  : 2.0 compatibily                                     	*
 *   6/09/91    JLB : IBM and Amiga compatability.                        	*
 *   11/27/1991 JLB : Uses file I/O routines for disk access.              *
 *   01/29/1992 DRD : Modified to use new font format.                     *
 *   02/01/1992 DRD : Added font file verification.                        *
 *   06/29/1994 SKB : modified for 32 bit library                          *
 *=========================================================================*/
VOID *cdecl Load_Font(BYTE const *name)
{
	BYTE	valid;
	WORD	fh;		// DOS file handle for font file.
	UWORD	size;		// Size of the data in the file (-2);
	BYTE	*ptr = NULL;		// Pointer to newly loaded font.

	if (Find_File(name)) {
		fh = Open_File(name, READ);
		if (Read_File(fh, (BYTE *) &size, 2) != 2) return(NULL);

		ptr = (BYTE *) Alloc(size, MEM_NORMAL);
		*(WORD *)ptr = size;
		Read_File(fh, ptr + 2, size - 2);
		Close_File(fh);
	} else {
		return (NULL);
	}

	//
	// verify that the file loaded is a valid font file.
	//

	valid = FALSE;
	if (*(ptr + 2) == 0) {		// no compression
		if (*(ptr + 3) == 5) {		// currently only 5 data blocks are used.
			valid = TRUE;
		}
	}

	if ( !valid ) {
		return (NULL);
	}

   return(ptr);
}
