RetroArch/gfx/fonts/bitmapfont.c

149 lines
4.1 KiB
C
Raw Normal View History

2012-12-13 20:12:06 +00:00
/* RetroArch - A frontend for libretro.
2013-01-01 00:37:37 +00:00
* Copyright (C) 2010-2013 - Hans-Kristian Arntzen
2012-12-13 20:12:06 +00:00
*
* RetroArch 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.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "fonts.h"
#include "bitmap.h"
#include <string.h>
#include <stdlib.h>
2013-01-11 23:11:41 +00:00
#include <math.h>
#include "../../msvc/msvc_compat.h"
2012-12-15 02:59:31 +00:00
#include "../../boolean.h"
2012-12-13 20:12:06 +00:00
struct font_renderer
{
unsigned scale_factor;
2012-12-14 20:50:59 +00:00
uint8_t *bitmap_chars[256];
uint8_t *bitmap_alloc;
2012-12-13 20:12:06 +00:00
};
2012-12-14 20:50:59 +00:00
static void char_to_texture(font_renderer_t *handle, uint8_t letter)
2012-12-13 20:12:06 +00:00
{
unsigned y, x, xo, yo;
2012-12-14 20:50:59 +00:00
handle->bitmap_chars[letter] = &handle->bitmap_alloc[letter * FONT_WIDTH * FONT_HEIGHT * handle->scale_factor * handle->scale_factor];
for (y = 0; y < FONT_HEIGHT; y++)
2012-12-13 20:12:06 +00:00
{
for (x = 0; x < FONT_WIDTH; x++)
2012-12-13 20:12:06 +00:00
{
2012-12-14 20:50:59 +00:00
uint8_t rem = 1 << ((x + y * FONT_WIDTH) & 7);
unsigned offset = (x + y * FONT_WIDTH) >> 3;
uint8_t col = (bitmap_bin[FONT_OFFSET(letter) + offset] & rem) ? 0xFF : 0;
2012-12-13 20:12:06 +00:00
for (xo = 0; xo < handle->scale_factor; xo++)
for (yo = 0; yo < handle->scale_factor; yo++)
2012-12-14 20:50:59 +00:00
handle->bitmap_chars[letter][x * handle->scale_factor + xo + (y * handle->scale_factor + yo) * FONT_WIDTH * handle->scale_factor] = col;
2012-12-13 20:12:06 +00:00
}
}
}
2013-01-11 23:11:41 +00:00
static void *font_renderer_init(const char *font_path, float font_size)
2012-12-13 20:12:06 +00:00
{
unsigned i;
2012-12-13 20:12:06 +00:00
font_renderer_t *handle = (font_renderer_t*)calloc(1, sizeof(*handle));
if (!handle)
return NULL;
2013-01-11 23:33:06 +00:00
handle->scale_factor = (unsigned)roundf(font_size / FONT_HEIGHT);
2012-12-13 20:12:06 +00:00
if (!handle->scale_factor)
handle->scale_factor = 1;
2012-12-14 20:50:59 +00:00
2012-12-15 02:59:31 +00:00
handle->bitmap_alloc = (uint8_t*)malloc(FONT_WIDTH * FONT_HEIGHT * handle->scale_factor * handle->scale_factor * 256);
2012-12-14 20:50:59 +00:00
if (!handle->bitmap_alloc)
{
free(handle);
return NULL;
}
2012-12-13 20:12:06 +00:00
for (i = 0; i < 256; i++)
2012-12-14 20:50:59 +00:00
char_to_texture(handle, i);
2012-12-13 20:12:06 +00:00
return handle;
}
static void font_renderer_msg(void *data, const char *msg, struct font_output_list *output)
2012-12-13 20:12:06 +00:00
{
size_t i;
font_renderer_t *handle = (font_renderer_t*)data;
2012-12-13 20:12:06 +00:00
output->head = NULL;
struct font_output *cur = NULL;
size_t len = strlen(msg);
int off_x = 0;
for (i = 0; i < len; i++)
2012-12-13 20:12:06 +00:00
{
struct font_output *tmp = (struct font_output*)calloc(1, sizeof(*tmp));
if (!tmp)
break;
2012-12-14 20:50:59 +00:00
tmp->output = handle->bitmap_chars[(unsigned) msg[i]];
2012-12-13 20:12:06 +00:00
tmp->width = FONT_WIDTH * handle->scale_factor;
tmp->height = FONT_HEIGHT * handle->scale_factor;
tmp->pitch = tmp->width;
tmp->advance_x = tmp->width;
tmp->advance_y = tmp->height;
tmp->char_off_x = 0;
tmp->char_off_y = tmp->height;
tmp->off_x = off_x;
tmp->off_y = 0;
tmp->next = NULL;
if (i == 0)
output->head = tmp;
else
cur->next = tmp;
cur = tmp;
off_x += FONT_WIDTH_STRIDE * handle->scale_factor;
}
}
static void font_renderer_free_output(void *data, struct font_output_list *output)
2012-12-13 20:12:06 +00:00
{
(void)data;
2012-12-13 20:12:06 +00:00
struct font_output *itr = output->head;
struct font_output *tmp = NULL;
while (itr != NULL)
{
tmp = itr;
itr = itr->next;
free(tmp);
}
output->head = NULL;
}
static void font_renderer_free(void *data)
2012-12-13 20:12:06 +00:00
{
font_renderer_t *handle = (font_renderer_t*)data;
2012-12-14 20:50:59 +00:00
free(handle->bitmap_alloc);
2012-12-13 20:12:06 +00:00
free(handle);
}
static const char *font_renderer_get_default_font(void)
2012-12-13 20:12:06 +00:00
{
return "";
}
const font_renderer_driver_t bitmap_font_renderer = {
font_renderer_init,
font_renderer_msg,
font_renderer_free_output,
font_renderer_free,
font_renderer_get_default_font,
"bitmap",
};