mirror of
https://github.com/libretro/RetroArch
synced 2025-02-20 15:40:44 +00:00
(font_renderer) Cleanups
This commit is contained in:
parent
20b5543420
commit
a153d600e4
@ -54,7 +54,7 @@ static const struct font_glyph *font_renderer_bmp_get_glyph(
|
||||
static void char_to_texture(bm_renderer_t *handle, uint8_t letter,
|
||||
unsigned atlas_x, unsigned atlas_y)
|
||||
{
|
||||
unsigned y, x, xo, yo;
|
||||
unsigned y, x;
|
||||
uint8_t *target = handle->atlas.buffer + atlas_x +
|
||||
atlas_y * handle->atlas.width;
|
||||
|
||||
@ -62,14 +62,15 @@ static void char_to_texture(bm_renderer_t *handle, uint8_t letter,
|
||||
{
|
||||
for (x = 0; x < FONT_WIDTH; x++)
|
||||
{
|
||||
unsigned xo, yo;
|
||||
unsigned font_pixel = x + y * FONT_WIDTH;
|
||||
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;
|
||||
|
||||
dst += x * handle->scale_factor;
|
||||
dst += y * handle->scale_factor * handle->atlas.width;
|
||||
dst += x * handle->scale_factor;
|
||||
dst += y * handle->scale_factor * handle->atlas.width;
|
||||
|
||||
for (yo = 0; yo < handle->scale_factor; yo++)
|
||||
for (xo = 0; xo < handle->scale_factor; xo++)
|
||||
@ -89,7 +90,7 @@ static void *font_renderer_bmp_init(const char *font_path, float font_size)
|
||||
|
||||
(void)font_path;
|
||||
|
||||
handle->scale_factor = (unsigned)roundf(font_size / FONT_HEIGHT);
|
||||
handle->scale_factor = (unsigned)roundf(font_size / FONT_HEIGHT);
|
||||
if (!handle->scale_factor)
|
||||
handle->scale_factor = 1;
|
||||
|
||||
@ -99,19 +100,21 @@ static void *font_renderer_bmp_init(const char *font_path, float font_size)
|
||||
|
||||
for (i = 0; i < BMP_ATLAS_SIZE; i++)
|
||||
{
|
||||
unsigned x = (i % BMP_ATLAS_COLS) * handle->scale_factor * FONT_WIDTH;
|
||||
unsigned y = (i / BMP_ATLAS_COLS) * handle->scale_factor * FONT_HEIGHT;
|
||||
unsigned x = (i % BMP_ATLAS_COLS) *
|
||||
handle->scale_factor * FONT_WIDTH;
|
||||
unsigned y = (i / BMP_ATLAS_COLS) *
|
||||
handle->scale_factor * FONT_HEIGHT;
|
||||
|
||||
char_to_texture(handle, i, x, y);
|
||||
|
||||
handle->glyphs[i].width = FONT_WIDTH * handle->scale_factor;
|
||||
handle->glyphs[i].height = FONT_HEIGHT * handle->scale_factor;
|
||||
handle->glyphs[i].width = FONT_WIDTH * handle->scale_factor;
|
||||
handle->glyphs[i].height = FONT_HEIGHT * handle->scale_factor;
|
||||
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;
|
||||
handle->glyphs[i].advance_x = (FONT_WIDTH + 1) * handle->scale_factor;
|
||||
handle->glyphs[i].advance_y = 0;
|
||||
handle->glyphs[i].advance_x = (FONT_WIDTH + 1) * handle->scale_factor;
|
||||
handle->glyphs[i].advance_y = 0;
|
||||
}
|
||||
|
||||
return handle;
|
||||
|
@ -47,18 +47,18 @@ typedef struct coretext_atlas_slot
|
||||
|
||||
typedef struct coretext_renderer
|
||||
{
|
||||
struct font_atlas atlas;
|
||||
coretext_atlas_slot_t atlas_slots[CT_ATLAS_SIZE];
|
||||
coretext_atlas_slot_t *uc_map[0x100];
|
||||
CGFloat metrics_height;
|
||||
struct font_atlas atlas;
|
||||
coretext_atlas_slot_t atlas_slots[CT_ATLAS_SIZE];
|
||||
coretext_atlas_slot_t *uc_map[0x100];
|
||||
CGFloat metrics_height;
|
||||
} ct_font_renderer_t;
|
||||
|
||||
static struct font_atlas *font_renderer_ct_get_atlas(void *data)
|
||||
{
|
||||
ct_font_renderer_t *handle = (ct_font_renderer_t*)data;
|
||||
if (!handle)
|
||||
return NULL;
|
||||
return &handle->atlas;
|
||||
ct_font_renderer_t *handle = (ct_font_renderer_t*)data;
|
||||
if (!handle)
|
||||
return NULL;
|
||||
return &handle->atlas;
|
||||
}
|
||||
|
||||
static const struct font_glyph *font_renderer_ct_get_glyph(
|
||||
@ -71,19 +71,19 @@ static const struct font_glyph *font_renderer_ct_get_glyph(
|
||||
return NULL;
|
||||
|
||||
atlas_slot = (coretext_atlas_slot_t*)&handle->atlas_slots[charcode];
|
||||
|
||||
|
||||
return &atlas_slot->glyph;
|
||||
}
|
||||
|
||||
static void font_renderer_ct_free(void *data)
|
||||
{
|
||||
ct_font_renderer_t *handle = (ct_font_renderer_t*)data;
|
||||
ct_font_renderer_t *handle = (ct_font_renderer_t*)data;
|
||||
|
||||
if (!handle)
|
||||
return;
|
||||
if (!handle)
|
||||
return;
|
||||
|
||||
free(handle->atlas.buffer);
|
||||
free(handle);
|
||||
free(handle->atlas.buffer);
|
||||
free(handle);
|
||||
}
|
||||
|
||||
static bool coretext_font_renderer_create_atlas(CTFontRef face, ct_font_renderer_t *handle)
|
||||
@ -114,11 +114,11 @@ static bool coretext_font_renderer_create_atlas(CTFontRef face, ct_font_renderer
|
||||
|
||||
CTFontGetBoundingRectsForGlyphs(face,
|
||||
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
|
||||
kCTFontOrientationDefault,
|
||||
kCTFontOrientationDefault,
|
||||
#else
|
||||
kCTFontDefaultOrientation,
|
||||
kCTFontDefaultOrientation,
|
||||
#endif
|
||||
glyphs, bounds, CT_ATLAS_SIZE);
|
||||
glyphs, bounds, CT_ATLAS_SIZE);
|
||||
|
||||
CTFontGetAdvancesForGlyphs(face,
|
||||
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
|
||||
@ -173,7 +173,7 @@ static bool coretext_font_renderer_create_atlas(CTFontRef face, ct_font_renderer
|
||||
bytesPerRow = max_width;
|
||||
bitmapData = calloc(max_height, bytesPerRow);
|
||||
offscreen = CGBitmapContextCreate(bitmapData, max_width, max_height,
|
||||
bitsPerComponent, bytesPerRow, NULL, kCGImageAlphaOnly);
|
||||
bitsPerComponent, bytesPerRow, NULL, kCGImageAlphaOnly);
|
||||
|
||||
CGContextSetTextMatrix(offscreen, CGAffineTransformIdentity);
|
||||
|
||||
@ -191,7 +191,7 @@ static bool coretext_font_renderer_create_atlas(CTFontRef face, ct_font_renderer
|
||||
CFAttributedStringRef attrString;
|
||||
CTLineRef line;
|
||||
struct font_glyph *glyph = &handle->atlas_slots[i].glyph;
|
||||
|
||||
|
||||
if (!glyph)
|
||||
continue;
|
||||
|
||||
@ -253,9 +253,9 @@ static bool coretext_font_renderer_create_atlas(CTFontRef face, ct_font_renderer
|
||||
|
||||
static void *font_renderer_ct_init(const char *font_path, float font_size)
|
||||
{
|
||||
char err = 0;
|
||||
CFStringRef cf_font_path = NULL;
|
||||
CTFontRef face = NULL;
|
||||
char err = 0;
|
||||
CFStringRef cf_font_path = NULL;
|
||||
CTFontRef face = NULL;
|
||||
ct_font_renderer_t *handle = (ct_font_renderer_t*)
|
||||
calloc(1, sizeof(*handle));
|
||||
|
||||
@ -266,6 +266,7 @@ static void *font_renderer_ct_init(const char *font_path, float font_size)
|
||||
}
|
||||
|
||||
cf_font_path = CFStringCreateWithCString(NULL, font_path, kCFStringEncodingASCII);
|
||||
|
||||
if (!cf_font_path)
|
||||
{
|
||||
err = 1;
|
||||
@ -314,7 +315,7 @@ static const char *default_font = "Verdana";
|
||||
|
||||
static const char *font_renderer_ct_get_default_font(void)
|
||||
{
|
||||
return default_font;
|
||||
return default_font;
|
||||
}
|
||||
|
||||
static int font_renderer_ct_get_line_height(void *data)
|
||||
|
@ -66,8 +66,8 @@ static bool font_renderer_stb_create_atlas(stb_font_renderer_t *self,
|
||||
uint8_t *font_data, float font_size, unsigned width, unsigned height)
|
||||
{
|
||||
int i;
|
||||
stbtt_pack_context pc = {NULL};
|
||||
stbtt_packedchar chardata[256];
|
||||
stbtt_pack_context pc = {NULL};
|
||||
|
||||
if (width > 2048 || height > 2048)
|
||||
{
|
||||
@ -99,13 +99,13 @@ static bool font_renderer_stb_create_atlas(stb_font_renderer_t *self,
|
||||
struct font_glyph *g = &self->glyphs[i];
|
||||
stbtt_packedchar *c = &chardata[i];
|
||||
|
||||
g->advance_x = c->xadvance;
|
||||
g->atlas_offset_x = c->x0;
|
||||
g->atlas_offset_y = c->y0;
|
||||
g->draw_offset_x = c->xoff;
|
||||
g->draw_offset_y = c->yoff;
|
||||
g->width = c->x1 - c->x0;
|
||||
g->height = c->y1 - c->y0;
|
||||
g->advance_x = c->xadvance;
|
||||
g->atlas_offset_x = c->x0;
|
||||
g->atlas_offset_y = c->y0;
|
||||
g->draw_offset_x = c->xoff;
|
||||
g->draw_offset_y = c->yoff;
|
||||
g->width = c->x1 - c->x0;
|
||||
g->height = c->y1 - c->y0;
|
||||
|
||||
/* Make sure important characters fit */
|
||||
if (isalnum(i) && (!g->width || !g->height))
|
||||
@ -228,8 +228,8 @@ static const char *font_renderer_stb_get_default_font(void)
|
||||
|
||||
static int font_renderer_stb_get_line_height(void* data)
|
||||
{
|
||||
stb_font_renderer_t *handle = (stb_font_renderer_t*)data;
|
||||
return handle->line_height;
|
||||
stb_font_renderer_t *handle = (stb_font_renderer_t*)data;
|
||||
return handle->line_height;
|
||||
}
|
||||
|
||||
font_renderer_driver_t stb_font_renderer = {
|
||||
|
@ -110,10 +110,10 @@ static uint32_t font_renderer_stb_unicode_update_atlas(
|
||||
offset_y = (id / 16) * self->max_glyph_height;
|
||||
|
||||
dst = self->atlas.buffer + offset_x + offset_y
|
||||
* self->atlas.width;
|
||||
* self->atlas.width;
|
||||
|
||||
stbtt_MakeGlyphBitmap(&self->info, dst, self->max_glyph_width, self->max_glyph_height,
|
||||
self->atlas.width, self->scale_factor, self->scale_factor, glyph_index);
|
||||
self->atlas.width, self->scale_factor, self->scale_factor, glyph_index);
|
||||
|
||||
stbtt_GetGlyphHMetrics(&self->info, glyph_index, &advance_width, &left_side_bearing);
|
||||
stbtt_GetGlyphBox(&self->info, glyph_index, &x0, NULL, NULL, &y1);
|
||||
@ -162,10 +162,7 @@ static const struct font_glyph *font_renderer_stb_unicode_get_glyph(
|
||||
unsigned id;
|
||||
stb_unicode_font_renderer_t *self = (stb_unicode_font_renderer_t*)data;
|
||||
|
||||
if (!self)
|
||||
return NULL;
|
||||
|
||||
if(code > 0xFFFF)
|
||||
if(!self || code > 0xFFFF)
|
||||
return NULL;
|
||||
|
||||
id = self->uc_to_id[code];
|
||||
@ -176,7 +173,6 @@ static const struct font_glyph *font_renderer_stb_unicode_get_glyph(
|
||||
self->last_used[id] = self->usage_counter++;
|
||||
|
||||
return &self->glyphs[id];
|
||||
|
||||
}
|
||||
|
||||
static void *font_renderer_stb_unicode_init(const char *font_path, float font_size)
|
||||
@ -265,8 +261,8 @@ static const char *font_renderer_stb_unicode_get_default_font(void)
|
||||
|
||||
static int font_renderer_stb_unicode_get_line_height(void* data)
|
||||
{
|
||||
stb_unicode_font_renderer_t *handle = (stb_unicode_font_renderer_t*)data;
|
||||
return handle->line_height;
|
||||
stb_unicode_font_renderer_t *handle = (stb_unicode_font_renderer_t*)data;
|
||||
return handle->line_height;
|
||||
}
|
||||
|
||||
font_renderer_driver_t stb_unicode_font_renderer = {
|
||||
|
Loading…
x
Reference in New Issue
Block a user