// File:     allegro_display_Font.c
// Author:   LeqxLeqx

#include <jni.h>
#include <allegro5/allegro_font.h>

#include "allegro_display_Font.h"
#include "font_manager.h"
#include "../utilities/stable_assert.h"


/*
 * Class:     allegro_display_Font
 * Method:    initializeNonDefaultFont
 * Signature: (Ljava/lang/String;II)Z
 */
JNIEXPORT jboolean JNICALL Java_allegro_display_Font_initializeFont
  (JNIEnv * env, jclass class, jstring jpath, jint size, jint identifier)
{
	const char * path = (*env)->GetStringUTFChars(env, jpath, 0);
	FONT * font = FontManager_createFont(path, (int) size, (int) identifier);
	if(font == NULL)
		return (jboolean) 0;

	(*env)->ReleaseStringUTFChars(env, jpath, path);
	
	FontManager_addFont(font);
	return (jboolean) 1;
}


JNIEXPORT jbyteArray JNICALL Java_allegro_display_Font_getFontMetrics
  (JNIEnv * env, jclass class, jint fontID)
{
	FONT * font = FontManager_getFont((int) fontID);
	sassert(font != NULL, "Java_allegro_display_Font_getFontMetrics font is null");

	const char data [4];

	int height = al_get_font_line_height(font->font);

	((int *) data)[0] = height;

	jbyteArray ret = (*env)->NewByteArray(env, 4);
	(*env)->SetByteArrayRegion(env, ret, 0, 4, data);
	
	return ret;
}
