2012-12-13 15:12:06 -05:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2014-01-01 01:50:59 +01:00
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2017-01-22 13:40:32 +01:00
|
|
|
* Copyright (C) 2011-2017 - Daniel De Matteis
|
2017-12-11 23:55:31 -08:00
|
|
|
*
|
2012-12-13 15:12:06 -05: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 <stdlib.h>
|
2015-10-31 21:43:30 +01:00
|
|
|
#include <string.h>
|
2013-01-12 00:11:41 +01:00
|
|
|
#include <math.h>
|
2015-10-31 21:43:30 +01:00
|
|
|
|
2014-10-21 05:05:52 +02:00
|
|
|
#include <boolean.h>
|
2012-12-13 15:12:06 -05:00
|
|
|
|
2015-10-31 21:43:30 +01:00
|
|
|
#include "bitmap.h"
|
|
|
|
|
2017-01-12 10:26:12 +01:00
|
|
|
#include "../font_driver.h"
|
|
|
|
|
2015-02-20 23:34:19 -03:00
|
|
|
#define BMP_ATLAS_COLS 16
|
|
|
|
#define BMP_ATLAS_ROWS 16
|
|
|
|
#define BMP_ATLAS_SIZE (BMP_ATLAS_COLS * BMP_ATLAS_ROWS)
|
2014-06-07 23:40:06 +02:00
|
|
|
|
|
|
|
typedef struct bm_renderer
|
2012-12-13 15:12:06 -05:00
|
|
|
{
|
|
|
|
unsigned scale_factor;
|
2015-02-20 23:34:19 -03:00
|
|
|
struct font_glyph glyphs[BMP_ATLAS_SIZE];
|
2014-06-07 23:40:06 +02:00
|
|
|
struct font_atlas atlas;
|
|
|
|
} bm_renderer_t;
|
|
|
|
|
2016-10-21 17:28:44 +01:00
|
|
|
static struct font_atlas *font_renderer_bmp_get_atlas(void *data)
|
2014-06-07 23:40:06 +02:00
|
|
|
{
|
|
|
|
bm_renderer_t *handle = (bm_renderer_t*)data;
|
2015-01-10 16:46:58 +01:00
|
|
|
if (!handle)
|
|
|
|
return NULL;
|
2014-06-07 23:40:06 +02:00
|
|
|
return &handle->atlas;
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:33:43 +02:00
|
|
|
static const struct font_glyph *font_renderer_bmp_get_glyph(
|
2014-09-09 06:59:17 +02:00
|
|
|
void *data, uint32_t code)
|
2014-06-07 23:40:06 +02:00
|
|
|
{
|
|
|
|
bm_renderer_t *handle = (bm_renderer_t*)data;
|
2015-01-10 16:46:58 +01:00
|
|
|
if (!handle)
|
|
|
|
return NULL;
|
2015-02-20 23:34:19 -03:00
|
|
|
return code < BMP_ATLAS_SIZE ? &handle->glyphs[code] : NULL;
|
2014-06-07 23:40:06 +02:00
|
|
|
}
|
2012-12-13 15:12:06 -05:00
|
|
|
|
2014-09-09 06:59:17 +02:00
|
|
|
static void char_to_texture(bm_renderer_t *handle, uint8_t letter,
|
|
|
|
unsigned atlas_x, unsigned atlas_y)
|
2012-12-13 15:12:06 -05:00
|
|
|
{
|
2017-01-10 21:50:59 +01:00
|
|
|
unsigned y, x;
|
2017-12-11 23:55:31 -08:00
|
|
|
uint8_t *target = handle->atlas.buffer + atlas_x +
|
2014-09-09 06:59:17 +02:00
|
|
|
atlas_y * handle->atlas.width;
|
2014-06-07 23:40:06 +02:00
|
|
|
|
2013-10-22 15:08:17 +02:00
|
|
|
for (y = 0; y < FONT_HEIGHT; y++)
|
2012-12-13 15:12:06 -05:00
|
|
|
{
|
2013-10-22 15:08:17 +02:00
|
|
|
for (x = 0; x < FONT_WIDTH; x++)
|
2012-12-13 15:12:06 -05:00
|
|
|
{
|
2017-01-10 21:50:59 +01:00
|
|
|
unsigned xo, yo;
|
2014-06-07 23:40:06 +02:00
|
|
|
unsigned font_pixel = x + y * FONT_WIDTH;
|
2015-01-10 16:46:58 +01:00
|
|
|
uint8_t rem = 1 << (font_pixel & 7);
|
|
|
|
unsigned offset = font_pixel >> 3;
|
|
|
|
uint8_t col = (bitmap_bin[FONT_OFFSET(letter) + offset] & rem) ? 0xff : 0;
|
|
|
|
uint8_t *dst = target;
|
2014-06-07 23:40:06 +02:00
|
|
|
|
2017-01-10 21:50:59 +01:00
|
|
|
dst += x * handle->scale_factor;
|
|
|
|
dst += y * handle->scale_factor * handle->atlas.width;
|
2014-06-07 23:40:06 +02:00
|
|
|
|
|
|
|
for (yo = 0; yo < handle->scale_factor; yo++)
|
|
|
|
for (xo = 0; xo < handle->scale_factor; xo++)
|
|
|
|
dst[xo + yo * handle->atlas.width] = col;
|
2012-12-13 15:12:06 -05:00
|
|
|
}
|
|
|
|
}
|
2016-10-21 17:25:21 +01:00
|
|
|
handle->atlas.dirty = true;
|
2012-12-13 15:12:06 -05:00
|
|
|
}
|
|
|
|
|
2014-10-08 18:19:05 +02:00
|
|
|
static void *font_renderer_bmp_init(const char *font_path, float font_size)
|
2012-12-13 15:12:06 -05:00
|
|
|
{
|
2013-10-22 15:08:17 +02:00
|
|
|
unsigned i;
|
2014-06-07 23:40:06 +02:00
|
|
|
bm_renderer_t *handle = (bm_renderer_t*)calloc(1, sizeof(*handle));
|
2015-01-10 16:46:58 +01:00
|
|
|
|
2012-12-13 15:12:06 -05:00
|
|
|
if (!handle)
|
|
|
|
return NULL;
|
|
|
|
|
2015-01-10 16:46:58 +01:00
|
|
|
(void)font_path;
|
|
|
|
|
2017-01-10 21:50:59 +01:00
|
|
|
handle->scale_factor = (unsigned)roundf(font_size / FONT_HEIGHT);
|
2012-12-13 15:12:06 -05:00
|
|
|
if (!handle->scale_factor)
|
|
|
|
handle->scale_factor = 1;
|
2012-12-14 15:50:59 -05:00
|
|
|
|
2015-02-20 23:34:19 -03:00
|
|
|
handle->atlas.width = FONT_WIDTH * handle->scale_factor * BMP_ATLAS_COLS;
|
|
|
|
handle->atlas.height = FONT_HEIGHT * handle->scale_factor * BMP_ATLAS_ROWS;
|
2014-06-07 23:40:06 +02:00
|
|
|
handle->atlas.buffer = (uint8_t*)calloc(handle->atlas.width * handle->atlas.height, 1);
|
2012-12-14 15:50:59 -05:00
|
|
|
|
2015-02-20 23:34:19 -03:00
|
|
|
for (i = 0; i < BMP_ATLAS_SIZE; i++)
|
2012-12-14 15:50:59 -05:00
|
|
|
{
|
2017-12-11 23:55:31 -08:00
|
|
|
unsigned x = (i % BMP_ATLAS_COLS) *
|
2017-01-10 21:50:59 +01:00
|
|
|
handle->scale_factor * FONT_WIDTH;
|
2017-12-11 23:55:31 -08:00
|
|
|
unsigned y = (i / BMP_ATLAS_COLS) *
|
2017-01-10 21:50:59 +01:00
|
|
|
handle->scale_factor * FONT_HEIGHT;
|
2015-01-10 16:46:58 +01:00
|
|
|
|
2014-06-07 23:40:06 +02:00
|
|
|
char_to_texture(handle, i, x, y);
|
|
|
|
|
2017-01-10 21:50:59 +01:00
|
|
|
handle->glyphs[i].width = FONT_WIDTH * handle->scale_factor;
|
|
|
|
handle->glyphs[i].height = FONT_HEIGHT * handle->scale_factor;
|
2014-06-07 23:40:06 +02:00
|
|
|
handle->glyphs[i].atlas_offset_x = x;
|
|
|
|
handle->glyphs[i].atlas_offset_y = y;
|
|
|
|
handle->glyphs[i].draw_offset_x = 0;
|
|
|
|
handle->glyphs[i].draw_offset_y = -FONT_HEIGHT_BASELINE * (int)handle->scale_factor;
|
2017-01-10 21:50:59 +01:00
|
|
|
handle->glyphs[i].advance_x = (FONT_WIDTH + 1) * handle->scale_factor;
|
|
|
|
handle->glyphs[i].advance_y = 0;
|
2012-12-14 15:50:59 -05:00
|
|
|
}
|
2012-12-13 15:12:06 -05:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2014-10-08 18:19:05 +02:00
|
|
|
static void font_renderer_bmp_free(void *data)
|
2012-12-13 15:12:06 -05:00
|
|
|
{
|
2014-06-07 23:40:06 +02:00
|
|
|
bm_renderer_t *handle = (bm_renderer_t*)data;
|
|
|
|
if (!handle)
|
|
|
|
return;
|
|
|
|
free(handle->atlas.buffer);
|
2012-12-13 15:12:06 -05:00
|
|
|
free(handle);
|
|
|
|
}
|
|
|
|
|
2014-10-08 18:19:05 +02:00
|
|
|
static const char *font_renderer_bmp_get_default_font(void)
|
2012-12-13 15:12:06 -05:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2012-12-14 20:25:40 +01:00
|
|
|
|
2015-05-27 12:20:31 +02:00
|
|
|
static int font_renderer_bmp_get_line_height(void* data)
|
|
|
|
{
|
|
|
|
bm_renderer_t *handle = (bm_renderer_t*)data;
|
2017-12-11 23:55:31 -08:00
|
|
|
|
2015-05-27 12:20:31 +02:00
|
|
|
if (!handle)
|
|
|
|
return FONT_HEIGHT;
|
2017-12-11 23:55:31 -08:00
|
|
|
|
2015-05-27 12:20:31 +02:00
|
|
|
return FONT_HEIGHT * handle->scale_factor;
|
|
|
|
}
|
|
|
|
|
2014-09-12 02:24:09 +02:00
|
|
|
font_renderer_driver_t bitmap_font_renderer = {
|
2014-10-08 18:19:05 +02:00
|
|
|
font_renderer_bmp_init,
|
|
|
|
font_renderer_bmp_get_atlas,
|
|
|
|
font_renderer_bmp_get_glyph,
|
|
|
|
font_renderer_bmp_free,
|
|
|
|
font_renderer_bmp_get_default_font,
|
2012-12-14 20:25:40 +01:00
|
|
|
"bitmap",
|
2015-05-27 12:20:31 +02:00
|
|
|
font_renderer_bmp_get_line_height,
|
2012-12-14 20:25:40 +01:00
|
|
|
};
|
2014-06-07 23:40:06 +02:00
|
|
|
|