<commit_before>#include <psp2/kernel/sysmem.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include "vita2d.h"
#include "texture_atlas.h"
#include "bin_packing_2d.h"
#include "utils.h"
#include "shared.h"

#define ATLAS_DEFAULT_W 512
#define ATLAS_DEFAULT_H 512

typedef struct vita2d_font {
	FT_Library ftlibrary;
	FT_Face face;
	texture_atlas *tex_atlas;
} vita2d_font;

vita2d_font *vita2d_load_font_file(const char *pathname)
{
	FT_Error error;

	vita2d_font *font = malloc(sizeof(*font));
	if (!font)
		return NULL;

	error = FT_Init_FreeType(&font->ftlibrary);
	if (error != FT_Err_Ok) {
		free(font);
		return NULL;
	}

	error = FT_New_Face(
		font->ftlibrary,
		pathname,
		0,
		&font->face);
	if (error != FT_Err_Ok) {
		FT_Done_FreeType(font->ftlibrary);
		free(font);
		return NULL;
	}

	font->tex_atlas = texture_atlas_create(ATLAS_DEFAULT_W, ATLAS_DEFAULT_H);

	return font;
}

vita2d_font *vita2d_load_font_mem(const void *buffer, unsigned int size)
{
	FT_Error error;

	vita2d_font *font = malloc(sizeof(*font));
	if (!font)
		return NULL;

	error = FT_Init_FreeType(&font->ftlibrary);
	if (error != FT_Err_Ok) {
		free(font);
		return NULL;
	}

	error = FT_New_Memory_Face(
		font->ftlibrary,
		buffer,
		size,
		0,
		&font->face);
	if (error != FT_Err_Ok) {
		FT_Done_FreeType(font->ftlibrary);
		free(font);
		return NULL;
	}

	font->tex_atlas = texture_atlas_create(ATLAS_DEFAULT_W, ATLAS_DEFAULT_H);

	return font;
}

void vita2d_free_font(vita2d_font *font)
{
	texture_atlas_free(font->tex_atlas);
	FT_Done_Face(font->face);
	FT_Done_FreeType(font->ftlibrary);
	free(font);
}

static int atlas_add_glyph(texture_atlas *atlas, unsigned char character, const FT_GlyphSlot slot)
{
	const FT_Bitmap *bitmap = &slot->bitmap;

	unsigned int *buffer = malloc(bitmap->width * bitmap->rows * 4);
	unsigned int w = bitmap->width;
	unsigned int h = bitmap->rows;

	int j, k;<commit_msg>Font: set FT_ENCODING_UNICODE<commit_after>#include <psp2/kernel/sysmem.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include "vita2d.h"
#include "texture_atlas.h"
#include "bin_packing_2d.h"
#include "utils.h"
#include "shared.h"

#define ATLAS_DEFAULT_W 512
#define ATLAS_DEFAULT_H 512

typedef struct vita2d_font {
	FT_Library ftlibrary;
	FT_Face face;
	texture_atlas *tex_atlas;
} vita2d_font;

vita2d_font *vita2d_load_font_file(const char *pathname)
{
	FT_Error error;

	vita2d_font *font = malloc(sizeof(*font));
	if (!font)
		return NULL;

	error = FT_Init_FreeType(&font->ftlibrary);
	if (error != FT_Err_Ok) {
		free(font);
		return NULL;
	}

	error = FT_New_Face(
		font->ftlibrary,
		pathname,
		0,
		&font->face);
	if (error != FT_Err_Ok) {
		FT_Done_FreeType(font->ftlibrary);
		free(font);
		return NULL;
	}

	FT_Select_Charmap(font->face, FT_ENCODING_UNICODE);

	font->tex_atlas = texture_atlas_create(ATLAS_DEFAULT_W, ATLAS_DEFAULT_H);

	return font;
}

vita2d_font *vita2d_load_font_mem(const void *buffer, unsigned int size)
{
	FT_Error error;

	vita2d_font *font = malloc(sizeof(*font));
	if (!font)
		return NULL;

	error = FT_Init_FreeType(&font->ftlibrary);
	if (error != FT_Err_Ok) {
		free(font);
		return NULL;
	}

	error = FT_New_Memory_Face(
		font->ftlibrary,
		buffer,
		size,
		0,
		&font->face);
	if (error != FT_Err_Ok) {
		FT_Done_FreeType(font->ftlibrary);
		free(font);
		return NULL;
	}

	FT_Select_Charmap(font->face, FT_ENCODING_UNICODE);

	font->tex_atlas = texture_atlas_create(ATLAS_DEFAULT_W, ATLAS_DEFAULT_H);

	return font;
}

void vita2d_free_font(vita2d_font *font)
{
	texture_atlas_free(font->tex_atlas);
	FT_Done_Face(font->face);
	FT_Done_FreeType(font->ftlibrary);
	free(font);
}

static int atlas_add_glyph(texture_atlas *atlas, unsigned char character, const FT_GlyphSlot slot)
{
	const FT_Bitmap *bitmap = &slot->bitmap;

	unsigned int *buffer = malloc(bitmap->width * bitmap->rows * 4);
	unsigned int w = bitmap->width;
	unsigned int h = bitmap->rows;

	int j, k;
{'fraction_non_alphanumeric': 0.1295017266896892, 'fraction_numerical': 0.011840157868771583, 'mean_word_length': 5.267387944358578, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 51, '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': '5500820', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1661, 'n_words': 321}