RetroArch/gfx/fonts/fonts.c

172 lines
4.4 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2012-01-08 00:08:18 +00:00
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
2011-01-22 21:24:52 +00:00
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-01-22 21:24:52 +00:00
* 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.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-01-22 21:24:52 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-01-22 21:24:52 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "fonts.h"
2012-05-27 00:04:50 +00:00
#include "../../file.h"
2011-01-22 21:24:52 +00:00
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <ft2build.h>
#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;
2011-12-24 12:46:12 +00:00
FT_Error err;
font_renderer_t *handle = (font_renderer_t*)calloc(1, sizeof(*handle));
2011-01-22 21:24:52 +00:00
if (!handle)
goto error;
2011-12-24 12:46:12 +00:00
err = FT_Init_FreeType(&handle->lib);
2011-01-22 21:24:52 +00:00
if (err)
goto error;
err = FT_New_Face(handle->lib, font_path, 0, &handle->face);
if (err)
goto error;
2011-01-22 23:27:20 +00:00
err = FT_Set_Pixel_Sizes(handle->face, 0, font_size);
2011-01-22 21:24:52 +00:00
if (err)
goto error;
return handle;
error:
2011-01-24 22:33:12 +00:00
if (handle)
{
if (handle->face)
FT_Done_Face(handle->face);
if (handle->lib)
FT_Done_FreeType(handle->lib);
free(handle);
}
2011-01-22 21:24:52 +00:00
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)
{
2011-12-24 12:46:12 +00:00
struct font_output *tmp = (struct font_output*)calloc(1, sizeof(*tmp));
2011-10-05 20:39:54 +00:00
if (!tmp)
break;
2011-01-22 23:27:20 +00:00
2011-12-24 12:46:12 +00:00
tmp->output = (uint8_t*)malloc(slot->bitmap.pitch * slot->bitmap.rows);
2011-10-05 20:39:54 +00:00
if (!tmp->output)
{
free(tmp);
break;
}
2011-01-22 21:24:52 +00:00
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->advance_x = slot->advance.x >> 6;
tmp->advance_y = slot->advance.y >> 6;
tmp->char_off_x = slot->bitmap_left;
tmp->char_off_y = slot->bitmap_top - slot->bitmap.rows;
tmp->off_x = off_x + tmp->char_off_x;
tmp->off_y = off_y + tmp->char_off_y;
2011-01-22 21:24:52 +00:00
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);
}
2011-10-06 17:06:38 +00:00
// Not the cleanest way to do things for sure, but should hopefully work ... :)
#if defined(_WIN32)
static const char *font_paths[] = {
"C:\\Windows\\Fonts\\consola.ttf",
"C:\\Windows\\Fonts\\verdana.ttf",
#elif defined(__APPLE__)
static const char *font_paths[] = {
2011-10-06 19:12:11 +00:00
"/Library/Fonts/Microsoft/Candara.ttf",
2011-11-22 17:52:20 +00:00
"/Library/Fonts/Verdana.ttf",
"/Library/Fonts/Tahoma.ttf",
2011-10-06 17:06:38 +00:00
#else
static const char *font_paths[] = {
"/usr/share/fonts/TTF/DejaVuSansMono.ttf",
"/usr/share/fonts/TTF/DejaVuSans.ttf",
"/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf",
"/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf",
2011-10-06 17:06:38 +00:00
#endif
"osd-font.ttf", // Magic font to search for, useful for distribution.
};
// Highly OS/platform dependent.
const char *font_renderer_get_default_font(void)
{
2011-12-25 23:06:41 +00:00
for (size_t i = 0; i < sizeof(font_paths) / sizeof(font_paths[0]); i++)
2011-10-06 17:06:38 +00:00
{
if (path_file_exists(font_paths[i]))
return font_paths[i];
}
return NULL;
}