<commit_before>/* Copyright (c) 2013-2015 <NAME>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <mgba-util/gui/font.h>
#include <mgba-util/gui/font-metrics.h>
#include <mgba-util/png-io.h>
#include <mgba-util/vfs.h>
#include "icons.h"

#include "ctr-gpu.h"

#define FONT_SIZE 15.6f

struct GUIFont {
	C3D_Tex* sheets;
	C3D_Tex icons;
	float size;
};

struct GUIFont* GUIFontCreate(void) {
	fontEnsureMapped();
	struct GUIFont* guiFont = malloc(sizeof(struct GUIFont));
	if (!guiFont) {
		return 0;
	}
	C3D_Tex* tex;

	TGLP_s* glyphInfo = fontGetGlyphInfo();
	guiFont->size = FONT_SIZE / glyphInfo->cellHeight;
	guiFont->sheets = malloc(sizeof(*guiFont->sheets) * glyphInfo->nSheets);

	int i;
	for (i = 0; i < glyphInfo->nSheets; ++i) {
		tex = &guiFont->sheets[i];
		tex->data = fontGetGlyphSheetTex(i);
		tex->fmt = glyphInfo->sheetFmt;
		tex->size = glyphInfo->sheetSize;
		tex->width = glyphInfo->sheetWidth;
		tex->height = glyphInfo->sheetHeight;
		tex->param = GPU_TEXTURE_MAG_FILTER(GPU_LINEAR) | GPU_TEXTURE_MIN_FILTER(GPU_LINEAR) | GPU_TEXTURE_WRAP_S(GPU_CLAMP_TO_EDGE) | GPU_TEXTURE_WRAP_T(GPU_CLAMP_TO_EDGE);
	}

	tex = &guiFont->icons;
	C3D_TexInitVRAM(tex, 256, 64, GPU_RGBA5551);

	GSPGPU_FlushDataCache(icons, icons_size);
	GX_RequestDma((u32*) icons, tex->data, icons_size);
	gspWaitForDMA();

	return guiFont;
}

void GUIFontDestroy(struct GUIFont* font) {
	free(font->sheets);
	C3D_TexDelete(&font->icons);
	free(font);
}

unsigned GUIFontHeight(const struct GUIFont* font) {
	return fontGetInfo()->lineFeed * font->size;
}

unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
	int index = fontGlyphIndexFromCodePoint(glyph);
	charWidthInfo_s* info = fontGetCharWidthInfo(index);
	if (info) {
		return info->charWidth * font->size;
	}
	return 0;
}

void GUIFontIconMetrics(const struct GUIFont* font, enum GUIIcon icon, unsigned* w, unsigned* h) {
	UNUSED(font);
	if (icon >= GUI_ICON_MAX) {
		if (w) {
			*w = 0;
		}
		if (h) {
			*h = 0;
		}
	} else {
		if (w) {
			*w = defaultIconMetrics[icon].width;
		}
		if (h) {
			*h = defaultIconMetrics[icon].height;
		}
	}
}

void GUIFontDrawGlyph(const struct GUIFont* font, int glyph_x, int glyph_y, uint32_t color, uint32_t glyph) {
	int index = fontGlyphIndexFromCodePoint(glyph);
	fontGlyphPos_s data;
	fontCalcGlyphPos(&data, index, 0, 1.0, 1.0);

	C3D_Tex* tex = &font->sheets[data.sheetIndex];
	ctrActivateTexture(tex);

	float width = data.texcoord.right - data.texcoord.left;
	float height = data.texcoord.top - data.texcoord.bottom;
	u16 x = glyph_x;
	u16 y = glyph_y + tex->height * height / 8;
	u16 u = tex->width * data.texcoord.left;<commit_msg>3DS: Update font code for ctrulib 1.6<commit_after>/* Copyright (c) 2013-2015 <NAME>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <mgba-util/gui/font.h>
#include <mgba-util/gui/font-metrics.h>
#include <mgba-util/png-io.h>
#include <mgba-util/vfs.h>
#include "icons.h"

#include "ctr-gpu.h"

#define FONT_SIZE 15.6f

struct GUIFont {
	CFNT_s* font;
	C3D_Tex* sheets;
	C3D_Tex icons;
	float size;
};

struct GUIFont* GUIFontCreate(void) {
	fontEnsureMapped();
	struct GUIFont* guiFont = malloc(sizeof(struct GUIFont));
	if (!guiFont) {
		return 0;
	}
	C3D_Tex* tex;

	guiFont->font = fontGetSystemFont();
	TGLP_s* glyphInfo = fontGetGlyphInfo(guiFont->font);
	guiFont->size = FONT_SIZE / glyphInfo->cellHeight;
	guiFont->sheets = malloc(sizeof(*guiFont->sheets) * glyphInfo->nSheets);

	int i;
	for (i = 0; i < glyphInfo->nSheets; ++i) {
		tex = &guiFont->sheets[i];
		tex->data = fontGetGlyphSheetTex(guiFont->font, i);
		tex->fmt = glyphInfo->sheetFmt;
		tex->size = glyphInfo->sheetSize;
		tex->width = glyphInfo->sheetWidth;
		tex->height = glyphInfo->sheetHeight;
		tex->param = GPU_TEXTURE_MAG_FILTER(GPU_LINEAR) | GPU_TEXTURE_MIN_FILTER(GPU_LINEAR) | GPU_TEXTURE_WRAP_S(GPU_CLAMP_TO_EDGE) | GPU_TEXTURE_WRAP_T(GPU_CLAMP_TO_EDGE);
	}

	tex = &guiFont->icons;
	C3D_TexInitVRAM(tex, 256, 64, GPU_RGBA5551);

	GSPGPU_FlushDataCache(icons, icons_size);
	GX_RequestDma((u32*) icons, tex->data, icons_size);
	gspWaitForDMA();

	return guiFont;
}

void GUIFontDestroy(struct GUIFont* font) {
	free(font->sheets);
	C3D_TexDelete(&font->icons);
	free(font);
}

unsigned GUIFontHeight(const struct GUIFont* font) {
	return fontGetInfo(font->font)->lineFeed * font->size;
}

unsigned GUIFontGlyphWidth(const struct GUIFont* font, uint32_t glyph) {
	int index = fontGlyphIndexFromCodePoint(font->font, glyph);
	charWidthInfo_s* info = fontGetCharWidthInfo(font->font, index);
	if (info) {
		return info->charWidth * font->size;
	}
	return 0;
}

void GUIFontIconMetrics(const struct GUIFont* font, enum GUIIcon icon, unsigned* w, unsigned* h) {
	UNUSED(font);
	if (icon >= GUI_ICON_MAX) {
		if (w) {
			*w = 0;
		}
		if (h) {
			*h = 0;
		}
	} else {
		if (w) {
			*w = defaultIconMetrics[icon].width;
		}
		if (h) {
			*h = defaultIconMetrics[icon].height;
		}
	}
}

void GUIFontDrawGlyph(const struct GUIFont* font, int glyph_x, int glyph_y, uint32_t color, uint32_t glyph) {
	int index = fontGlyphIndexFromCodePoint(font->font, glyph);
	fontGlyphPos_s data;
	fontCalcGlyphPos(&data, font->font, index, 0, 1.0, 1.0);

	C3D_Tex* tex = &font->sheets[data.sheetIndex];
	ctrActivateTexture(tex);

	float width = data.texcoord.right - data.texcoord.left;
	float height = data.texcoord.top - data.texcoord.bottom;
	u16 x = glyph_x;
	u16 y = glyph_y + tex->height * height / 8;
	u16 u = tex->width * data.texcoord.left;
{'fraction_non_alphanumeric': 0.14206801311928188, 'fraction_numerical': 0.019506300707750734, 'mean_word_length': 4.549808429118774, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3367803', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2299, 'n_words': 498}