diff --git a/gfx/fonts.c b/gfx/fonts.c new file mode 100644 index 0000000000..27ff6cd5c6 --- /dev/null +++ b/gfx/fonts.c @@ -0,0 +1,129 @@ +/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes. + * Copyright (C) 2010 - Hans-Kristian Arntzen + * + * Some code herein may be based on code found in BSNES. + * + * SSNES is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with SSNES. + * If not, see . + */ + +#include "fonts.h" +#include +#include +#include +#include + +#include +#include FT_FREETYPE_H + +struct font_renderer +{ + FT_Library lib; + FT_Face face; +}; + +font_renderer_t *font_renderer_new(const char *font_path, unsigned font_size) +{ + (void)font_size; + font_renderer_t *handle = calloc(1, sizeof(*handle)); + if (!handle) + goto error; + + FT_Error err = FT_Init_FreeType(&handle->lib); + if (err) + goto error; + + err = FT_New_Face(handle->lib, font_path, 0, &handle->face); + if (err) + goto error; + + err = FT_Set_Char_Size(handle->face, 0, 64*64, 1024, 1024); + if (err) + goto error; + + err = FT_Set_Pixel_Sizes(handle->face, 0, 64); + if (err) + goto error; + + return handle; + +error: + free(handle); + if (handle->face) + FT_Done_Face(handle->face); + if (handle->lib) + FT_Done_FreeType(handle->lib); + return NULL; +} + +void font_renderer_msg(font_renderer_t *handle, const char *msg, struct font_output_list *output) +{ + output->head = NULL; + + FT_GlyphSlot slot = handle->face->glyph; + struct font_output *cur = NULL; + size_t len = strlen(msg); + int off_x = 0, off_y = 0; + + for (size_t i = 0; i < len; i++) + { + FT_Error err = FT_Load_Char(handle->face, msg[i], FT_LOAD_RENDER); + + if (!err) + { + struct font_output *tmp = calloc(1, sizeof(*tmp)); + assert(tmp); + + tmp->output = malloc(slot->bitmap.pitch * slot->bitmap.rows); + assert(tmp->output); + memcpy(tmp->output, slot->bitmap.buffer, slot->bitmap.pitch * slot->bitmap.rows); + + tmp->width = slot->bitmap.width; + tmp->height = slot->bitmap.rows; + tmp->pitch = slot->bitmap.pitch; + tmp->off_x = off_x + slot->bitmap_left; + tmp->off_y = off_y - slot->bitmap_top; + tmp->next = NULL; + + if (i == 0) + output->head = tmp; + else + cur->next = tmp; + + cur = tmp; + } + + off_x += slot->advance.x >> 6; + off_y += slot->advance.y >> 6; + } +} + +void font_renderer_free_output(struct font_output_list *output) +{ + struct font_output *itr = output->head; + struct font_output *tmp = NULL; + while (itr != NULL) + { + free(itr->output); + tmp = itr; + itr = itr->next; + free(tmp); + } + output->head = NULL; +} + +void font_renderer_free(font_renderer_t *handle) +{ + if (handle->face) + FT_Done_Face(handle->face); + if (handle->lib) + FT_Done_FreeType(handle->lib); +} diff --git a/gfx/fonts.h b/gfx/fonts.h new file mode 100644 index 0000000000..98aa97cce4 --- /dev/null +++ b/gfx/fonts.h @@ -0,0 +1,44 @@ +/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes. + * Copyright (C) 2010 - Hans-Kristian Arntzen + * + * Some code herein may be based on code found in BSNES. + * + * SSNES is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with SSNES. + * If not, see . + */ + + +#ifndef __SSNES_FONTS_H +#define __SSNES_FONTS_H + +#include + +typedef struct font_renderer font_renderer_t; + +struct font_output +{ + uint8_t *output; + unsigned width, height, pitch; + int off_x, off_y; + struct font_output *next; +}; + +struct font_output_list +{ + struct font_output *head; +}; + +font_renderer_t *font_renderer_new(const char *font_path, unsigned font_size); +void font_renderer_msg(font_renderer_t *handle, const char *msg, struct font_output_list *output); +void font_renderer_free_output(struct font_output_list *list); +void font_renderer_free(font_renderer_t *handle); + +#endif