/*
 * DESCRIPTION:
 * Glyph functions.
*/

#include "glyph.h"

/* Returns width of glyph. */
s32
glyph_width_get(Font* font, char glyph)
{
	GlyphMetrics glyphMetrics;

	glyph_metrics_get(&glyphMetrics, font, glyph);

	return glyphMetrics.maxX - glyphMetrics.minX;
}

/* Determines the kerning between two glyphs. */
s32
glyph_kerning_get(Font* font, char glyph, char nextGlyph)
{
	return TTF_GetFontKerningSizeGlyphs(font->ttf, glyph, nextGlyph);
}

/* Obtains glyph metrics, given a glyph and a font. */
void
glyph_metrics_get(GlyphMetrics* self, Font* font, char glyph)
{
	TTF_GlyphMetrics
	(
	 	font->ttf,
		glyph,
		&self->minX,
		&self->maxX,
		&self->minY,
		&self->maxY,
		&self->advance
	);
}

/* Creates a texture from a glyph. */
void
glyph_texture_init(Texture* texture, Font* font, char glyph)
{
	SDL_Surface* surface;
	SDL_Surface* textSurface;
	ivec2 size;

	size[0] = glyph_width_get(font, glyph);
	size[1] = font_height_get(font);

	surface_rgba_init(&surface, size);

	textSurface = TTF_RenderGlyph_Blended(font->ttf, glyph, GLYPH_COLOR);

	SDL_BlitSurface(textSurface, NULL, surface, NULL);

	SDL_FreeSurface(textSurface);

	texture_surface_init(texture, surface);

	SDL_FreeSurface(surface);
}

