////////////////////////////////////////////////////////////////////////////////
// Font.h
// Copyright by Bjoern Ganster
// The roots of this code go way back in time ...
////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "BGBase.h"
#include "Texture.h"

class Font: public VirtualClass {
public:
	// Load a font from file
	static Font* loadFromFile(const PathChar* fontFN, int width, int height, int spacing);

	// Build a font texture by rendering a font into a texture
	static Font* loadFromSystem(const char* fontName, int height, int spacing);

	// Load font from TTF file
	static Font* loadFromTTF(void* fontFileData, size_t fontFileSize, int fontHeight);

	// Construct font from texture
	Font(const Texture& texture, int charWidth, int charHeight, int spacing);

	// Render text using the font directly
	void renderText(const BGCPString& str, int x1, int y1, int x2);

	// Write text into a target texture
	void renderText(Texture* target, const BGCPString& text, int x1, int y1, int x2, int y2, 
		unsigned char r = 0, unsigned char g = 0, unsigned char b = 0);

	// Get character data
	int getCharWidth(char c) const;
	int getMaxCharWidth() const;
	int getCharHeight() const;
	int getCharSpacing() const;

	// Obtain width of text on screen
	int textWidth (const BGCPString& text);

private:
	// The texture that actually stores the font
	Texture mFontTexture;

	int mCharHeight, mCharSpacing, mMaxCharWidth;
	int mU1[256];
	int mV1[256];
	int mU2[256];
	int mV2[256];

	// Default constructor is private. Use loadFromFile or loadFromSystem instead.
	Font();

	// Small help function to determine the width of a single character
	void findCharDims(unsigned char c, int maxCharWidth);
};

// Access default font
void setDefaultFont(Font* newVal);
Font* getDefaultFont();
