//-----------------------------------------------------------------------------------------------
// BitmapFont.hpp
//
#pragma once
#ifndef include_BitmapFont
#define include_BitmapFont

//-----------------------------------------------------------------------------------------------
// #include "Engine/Core/Vector2.hpp"
#include "Engine/Core/EngineCommon.hpp"
#include "Engine/Renderer/Texture.hpp"


//-----------------------------------------------------------------------------------------------
const int GLYPH_UCS_MAX = 256; // TEMP HACK: Currently only extended ASCII (Latin-1 / ISO 8859-1) characters supported only


/////////////////////////////////////////////////////////////////////////////////////////////////
struct GlyphData
{
	Vector2		m_texCoordMins;
	Vector2		m_texCoordMaxs;
	float		m_cellHeightFractionBeforeGlyph; // The "A" value in TTF ABC terminology
	float		m_cellHeightFractionDuringGlyph; // The "B" value in TTF ABC terminology
	float		m_cellHeightFractionAfterGlyph;  // The "C" value in TTF ABC terminology
};


/////////////////////////////////////////////////////////////////////////////////////////////////
class BitmapFont
{
public:
	BitmapFont();
	~BitmapFont();
	float CalcTextStringWidth( const char* latinChars, float cellHeight );
	void AdvanceToEndOfLineByFit( const char*& scanChar, float maxWidth, float cellHeight );
	void PopulateMetaData( const char* fontMetaDataFilePathIfPresent );
	bool IsValid() const { return m_glyphsTexture != nullptr; }

public:
	Texture*		m_glyphsTexture;
	GlyphData		m_glyphsData[ GLYPH_UCS_MAX ];
};


/////////////////////////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------
inline float BitmapFont::CalcTextStringWidth( const char* latinChars, float cellHeight )
{
	float totalWidth = 0.f;
	for( const char* scan = latinChars; *scan != '\0'; ++ scan )
	{
		unsigned char ucsIndex = static_cast< unsigned char >( *scan );
		GlyphData& glyphData = m_glyphsData[ ucsIndex ];
		totalWidth += glyphData.m_cellHeightFractionBeforeGlyph;
		totalWidth += glyphData.m_cellHeightFractionDuringGlyph;
		totalWidth += glyphData.m_cellHeightFractionAfterGlyph;
	}

	totalWidth *= cellHeight;
	return totalWidth;
}



#endif // include_BitmapFont
