Cleanup (#826)
* Update meson.build - add logic to loop over more lua names (in the future more names might be discovered) - disable warnings and errors on dependencies * adding missing includes and checks, correct data types, pointer mess […] - various functions from string.h were used but never defined - logic was done across multiple different data types with different signedness, got all of them up to snuff - give 0 sized array size of 1 (array of size 0 is illegal, but rewriting the code is out of the scope of this commit) - add preprocessor that marks possibly unused argument as such (does not mean they will get optimized out or anything) - correctly initialize structs with all data needed All these were found by generating the project using `meson -Dwarning_level=3 -Dwerror=true` * remove undefined behavior, correct data types * Comment manual bit manipulation to be investigated * check for more edge cases, replace multiple cleanups with goto * remove system specific includes
This commit is contained in:
+46
-19
@@ -43,27 +43,28 @@ typedef struct RenFont {
|
||||
FT_Face face;
|
||||
GlyphSet* sets[SUBPIXEL_BITMAPS_CACHED][MAX_LOADABLE_GLYPHSETS];
|
||||
float size, space_advance, tab_advance;
|
||||
short max_height, baseline, height;
|
||||
unsigned short max_height, baseline, height;
|
||||
ERenFontAntialiasing antialiasing;
|
||||
ERenFontHinting hinting;
|
||||
unsigned char style;
|
||||
char path[0];
|
||||
char path[1];
|
||||
} RenFont;
|
||||
|
||||
static const char* utf8_to_codepoint(const char *p, unsigned *dst) {
|
||||
const unsigned char *up = (unsigned char*)p;
|
||||
unsigned res, n;
|
||||
switch (*p & 0xf0) {
|
||||
case 0xf0 : res = *p & 0x07; n = 3; break;
|
||||
case 0xe0 : res = *p & 0x0f; n = 2; break;
|
||||
case 0xf0 : res = *up & 0x07; n = 3; break;
|
||||
case 0xe0 : res = *up & 0x0f; n = 2; break;
|
||||
case 0xd0 :
|
||||
case 0xc0 : res = *p & 0x1f; n = 1; break;
|
||||
default : res = *p; n = 0; break;
|
||||
case 0xc0 : res = *up & 0x1f; n = 1; break;
|
||||
default : res = *up; n = 0; break;
|
||||
}
|
||||
while (n--) {
|
||||
res = (res << 6) | (*(++p) & 0x3f);
|
||||
res = (res << 6) | (*(++up) & 0x3f);
|
||||
}
|
||||
*dst = res;
|
||||
return p + 1;
|
||||
return (const char*)up + 1;
|
||||
}
|
||||
|
||||
static int font_set_load_options(RenFont* font) {
|
||||
@@ -136,7 +137,7 @@ static void font_load_glyphset(RenFont* font, int idx) {
|
||||
if (pen_x == 0)
|
||||
continue;
|
||||
set->surface = check_alloc(SDL_CreateRGBSurface(0, pen_x, font->max_height, font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? 24 : 8, 0, 0, 0, 0));
|
||||
unsigned char* pixels = set->surface->pixels;
|
||||
uint8_t* pixels = set->surface->pixels;
|
||||
for (int i = 0; i < MAX_GLYPHSET; ++i) {
|
||||
int glyph_index = FT_Get_Char_Index(font->face, i + idx * MAX_GLYPHSET);
|
||||
if (!glyph_index || FT_Load_Glyph(font->face, glyph_index, load_option))
|
||||
@@ -145,11 +146,11 @@ static void font_load_glyphset(RenFont* font, int idx) {
|
||||
font_set_style(&slot->outline, (64 / bitmaps_cached) * j, font->style);
|
||||
if (FT_Render_Glyph(slot, render_option))
|
||||
continue;
|
||||
for (int line = 0; line < slot->bitmap.rows; ++line) {
|
||||
for (unsigned int line = 0; line < slot->bitmap.rows; ++line) {
|
||||
int target_offset = set->surface->pitch * line + set->metrics[i].x0 * byte_width;
|
||||
int source_offset = line * slot->bitmap.pitch;
|
||||
if (font->antialiasing == FONT_ANTIALIASING_NONE) {
|
||||
for (int column = 0; column < slot->bitmap.width; ++column) {
|
||||
for (unsigned int column = 0; column < slot->bitmap.width; ++column) {
|
||||
int current_source_offset = source_offset + (column / 8);
|
||||
int source_pixel = slot->bitmap.buffer[current_source_offset];
|
||||
pixels[++target_offset] = ((source_pixel >> (7 - (column % 8))) & 0x1) << 7;
|
||||
@@ -169,6 +170,9 @@ static GlyphSet* font_get_glyphset(RenFont* font, unsigned int codepoint, int su
|
||||
}
|
||||
|
||||
static RenFont* font_group_get_glyph(GlyphSet** set, GlyphMetric** metric, RenFont** fonts, unsigned int codepoint, int bitmap_index) {
|
||||
if (!metric) {
|
||||
return NULL;
|
||||
}
|
||||
if (bitmap_index < 0)
|
||||
bitmap_index += SUBPIXEL_BITMAPS_CACHED;
|
||||
for (int i = 0; i < FONT_FALLBACK_MAX && fonts[i]; ++i) {
|
||||
@@ -177,7 +181,7 @@ static RenFont* font_group_get_glyph(GlyphSet** set, GlyphMetric** metric, RenFo
|
||||
if ((*metric)->loaded || codepoint < 0xFF)
|
||||
return fonts[i];
|
||||
}
|
||||
if (!(*metric)->loaded && codepoint > 0xFF && codepoint != 0x25A1)
|
||||
if (*metric && !(*metric)->loaded && codepoint > 0xFF && codepoint != 0x25A1)
|
||||
return font_group_get_glyph(set, metric, fonts, 0x25A1, bitmap_index);
|
||||
return fonts[0];
|
||||
}
|
||||
@@ -233,7 +237,11 @@ void ren_font_group_set_tab_size(RenFont **fonts, int n) {
|
||||
}
|
||||
|
||||
int ren_font_group_get_tab_size(RenFont **fonts) {
|
||||
return font_get_glyphset(fonts[0], '\t', 0)->metrics['\t'].xadvance / fonts[0]->space_advance;
|
||||
int advance = font_get_glyphset(fonts[0], '\t', 0)->metrics['\t'].xadvance;
|
||||
if (fonts[0]->space_advance) {
|
||||
advance /= fonts[0]->space_advance;
|
||||
}
|
||||
return advance;
|
||||
}
|
||||
|
||||
float ren_font_group_get_size(RenFont **fonts) {
|
||||
@@ -251,6 +259,8 @@ float ren_font_group_get_width(RenFont **fonts, const char *text) {
|
||||
unsigned int codepoint;
|
||||
text = utf8_to_codepoint(text, &codepoint);
|
||||
RenFont* font = font_group_get_glyph(&set, &metric, fonts, codepoint, 0);
|
||||
if (!metric)
|
||||
break;
|
||||
width += (!font || metric->xadvance) ? metric->xadvance : fonts[0]->space_advance;
|
||||
}
|
||||
const int surface_scale = renwin_surface_scale(&window_renderer);
|
||||
@@ -266,7 +276,7 @@ float ren_draw_text(RenFont **fonts, const char *text, float x, int y, RenColor
|
||||
y *= surface_scale;
|
||||
int bytes_per_pixel = surface->format->BytesPerPixel;
|
||||
const char* end = text + strlen(text);
|
||||
unsigned char* destination_pixels = surface->pixels;
|
||||
uint8_t* destination_pixels = surface->pixels;
|
||||
int clip_end_x = clip.x + clip.width, clip_end_y = clip.y + clip.height;
|
||||
|
||||
while (text < end) {
|
||||
@@ -274,13 +284,15 @@ float ren_draw_text(RenFont **fonts, const char *text, float x, int y, RenColor
|
||||
text = utf8_to_codepoint(text, &codepoint);
|
||||
GlyphSet* set = NULL; GlyphMetric* metric = NULL;
|
||||
RenFont* font = font_group_get_glyph(&set, &metric, fonts, codepoint, (int)(fmod(pen_x, 1.0) * SUBPIXEL_BITMAPS_CACHED));
|
||||
if (!metric)
|
||||
break;
|
||||
int start_x = floor(pen_x) + metric->bitmap_left;
|
||||
int end_x = (metric->x1 - metric->x0) + start_x;
|
||||
int glyph_end = metric->x1, glyph_start = metric->x0;
|
||||
if (!metric->loaded && codepoint > 0xFF)
|
||||
ren_draw_rect((RenRect){ start_x + 1, y, font->space_advance - 1, ren_font_group_get_height(fonts) }, color);
|
||||
if (set->surface && color.a > 0 && end_x >= clip.x && start_x < clip_end_x) {
|
||||
unsigned char* source_pixels = set->surface->pixels;
|
||||
uint8_t* source_pixels = set->surface->pixels;
|
||||
for (int line = metric->y0; line < metric->y1; ++line) {
|
||||
int target_y = line + y - metric->bitmap_top + font->baseline * surface_scale;
|
||||
if (target_y < clip.y)
|
||||
@@ -294,15 +306,30 @@ float ren_draw_text(RenFont **fonts, const char *text, float x, int y, RenColor
|
||||
start_x += offset;
|
||||
glyph_start += offset;
|
||||
}
|
||||
unsigned int* destination_pixel = (unsigned int*)&destination_pixels[surface->pitch * target_y + start_x * bytes_per_pixel];
|
||||
unsigned char* source_pixel = &source_pixels[line * set->surface->pitch + glyph_start * (font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? 3 : 1)];
|
||||
uint32_t* destination_pixel = (uint32_t*)&(destination_pixels[surface->pitch * target_y + start_x * bytes_per_pixel]);
|
||||
uint8_t* source_pixel = &source_pixels[line * set->surface->pitch + glyph_start * (font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? 3 : 1)];
|
||||
for (int x = glyph_start; x < glyph_end; ++x) {
|
||||
unsigned int destination_color = *destination_pixel;
|
||||
uint32_t destination_color = *destination_pixel;
|
||||
// the standard way of doing this would be SDL_GetRGBA, but that introduces a performance regression. needs to be investigated
|
||||
SDL_Color dst = { (destination_color & surface->format->Rmask) >> surface->format->Rshift, (destination_color & surface->format->Gmask) >> surface->format->Gshift, (destination_color & surface->format->Bmask) >> surface->format->Bshift, (destination_color & surface->format->Amask) >> surface->format->Ashift };
|
||||
SDL_Color src = { *(font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? source_pixel++ : source_pixel), *(font->antialiasing == FONT_ANTIALIASING_SUBPIXEL ? source_pixel++ : source_pixel), *source_pixel++ };
|
||||
SDL_Color src;
|
||||
|
||||
if (font->antialiasing == FONT_ANTIALIASING_SUBPIXEL) {
|
||||
src.r = *(source_pixel++);
|
||||
src.g = *(source_pixel++);
|
||||
}
|
||||
else {
|
||||
src.r = *(source_pixel);
|
||||
src.g = *(source_pixel);
|
||||
}
|
||||
|
||||
src.b = *(source_pixel++);
|
||||
src.a = 0xFF;
|
||||
|
||||
r = (color.r * src.r * color.a + dst.r * (65025 - src.r * color.a) + 32767) / 65025;
|
||||
g = (color.g * src.g * color.a + dst.g * (65025 - src.g * color.a) + 32767) / 65025;
|
||||
b = (color.b * src.b * color.a + dst.b * (65025 - src.b * color.a) + 32767) / 65025;
|
||||
// the standard way of doing this would be SDL_GetRGBA, but that introduces a performance regression. needs to be investigated
|
||||
*destination_pixel++ = dst.a << surface->format->Ashift | r << surface->format->Rshift | g << surface->format->Gshift | b << surface->format->Bshift;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user