From dcf6b06629f03e780feb7b8c67cedf53f6c14626 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Sat, 6 Jun 2020 13:08:39 +0200 Subject: [PATCH] Cleanup debug code --- lib/font_renderer/font_renderer.cpp | 27 ++++------------ src/renderer.c | 48 ++++++++++++++++------------- 2 files changed, 32 insertions(+), 43 deletions(-) diff --git a/lib/font_renderer/font_renderer.cpp b/lib/font_renderer/font_renderer.cpp index 0ebf4af0..593b1e1b 100644 --- a/lib/font_renderer/font_renderer.cpp +++ b/lib/font_renderer/font_renderer.cpp @@ -146,28 +146,14 @@ static void glyph_lut_convolution(agg::rendering_buffer ren_buf, agg::lcd_distri const int len = (x1 - x0) * subpixel; const int height = ren_buf.height(); for (int y = y0; y < y1; y++) { - // FIXME: clarify why we do not use height - 1 below. agg::int8u *covers = ren_buf.row_ptr(height - 1 - y) + x0 * subpixel; memcpy(covers_buf, covers, len); -#if 0 - if (len >= 24) { - debug_print_covers("BUF ", covers_buf, 0, len + 2 * subpixel, 0); - debug_print_covers("BEFORE", covers - subpixel, 0, len + 2 * subpixel, 0); - } -#endif for (int x = x0 - 1; x < x1 + 1; x++) { for (int i = 0; i < subpixel; i++) { const int cx = (x - x0) * subpixel + i; covers[cx] = lcd_lut.convolution(covers, cx, 0, len - 1); } } -#if 0 - if (len >= 24) { - debug_print_covers("AFTER ", covers - subpixel, 0, len + 2 * subpixel, 0); - char c; - scanf("%c", &c); - } -#endif } gli.x0 -= 1; gli.x1 += 1; @@ -203,7 +189,7 @@ int FontRendererBakeFontBitmap(FontRenderer *font_renderer, int font_height, // When using subpixel font rendering it is needed to leave a padding pixel on the left and on the right. // Since each pixel is composed by n subpixel we set below x_start to subpixel_scale instead than zero. const int x_start = subpixel_scale; - int x = x_start, y = pixels_height - 1; // - 20; // -20 is for debug + int x = x_start, y = pixels_height - 1; int res = 0; const agg::alpha8 text_color(0xff); #ifdef FONT_RENDERER_HEIGHT_HACK @@ -211,7 +197,7 @@ int FontRendererBakeFontBitmap(FontRenderer *font_renderer, int font_height, #else const int font_height_reduced = font_height; #endif - fprintf(stderr, "FONT HEIGHT %d, ASCENDER: %d DESCENDER: %d\n", font_height, ascender_px, descender_px); + agg::int8u *cover_swap_buffer = new agg::int8u[pixels_width * subpixel_scale]; for (int i = 0; i < num_chars; i++) { int codepoint = first_char + i; if (x + font_height * subpixel_scale > pixels_width * subpixel_scale) { @@ -225,7 +211,6 @@ int FontRendererBakeFontBitmap(FontRenderer *font_renderer, int font_height, const int y_baseline = y - pad_y - ascender_px; double x_next = x, y_next = y_baseline; - fprintf(stderr, "GLYPH (%d, %d)\n", x, y_baseline); renderer_alpha.render_codepoint(ren_buf, font_height_reduced, text_color, x_next, y_next, codepoint, subpixel_scale); int x_next_i = (subpixel_scale == 1 ? int(x_next + 1.0) : ceil_to_multiple(x_next + 0.5, subpixel_scale)); @@ -241,14 +226,14 @@ int FontRendererBakeFontBitmap(FontRenderer *font_renderer, int font_height, glyph_info.xadvance = (x_next - x) / subpixel_scale; if (glyph_info.x1 > glyph_info.x0) { - agg::int8u *covers_buf = ren_buf.row_ptr(0); - glyph_lut_convolution(ren_buf, lcd_lut, covers_buf, glyph_info); + glyph_lut_convolution(ren_buf, lcd_lut, cover_swap_buffer, glyph_info); } - // glyph_trim_rect(ren_buf, glyph_info, subpixel_scale); + glyph_trim_rect(ren_buf, glyph_info, subpixel_scale); - // When subpixel is activated we need at least two more subpixels on the right. + // When subpixel is activated we need one padding pixel on the left and on the right. x = x_next_i + 2 * subpixel_scale; } + delete [] cover_swap_buffer; return res; } diff --git a/src/renderer.c b/src/renderer.c index 7a360af5..f8786edf 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -126,6 +126,7 @@ void ren_free_coverage(RenCoverageImage *coverage) { free(coverage); } +#ifdef FONT_RENDERER_DEBUG void debug_bitmap_draw_rect(RenImage *image, GlyphBitmapInfo *glyph) { const int subpixel = 3; const int x0 = glyph->x0, x1 = glyph->x1; @@ -152,6 +153,7 @@ void debug_bitmap_draw_rect(RenImage *image, GlyphBitmapInfo *glyph) { c[y * w].b = c[y * w].b >> 1; } } +#endif static GlyphSet* load_glyphset(RenFont *font, int idx) { GlyphSet *set = check_alloc(calloc(1, sizeof(GlyphSet))); @@ -176,32 +178,34 @@ retry: goto retry; } +#ifdef FONT_RENDERER_DEBUG static int debug_image_index = 1; if (idx == 0) { - // DEBUG CODE - RenImage *debug_cov_image = ren_new_image(width * subpixel_scale, height); - for (int h = 0; h < height; h++) { - RenColor *d = debug_cov_image->pixels + h * width * subpixel_scale; - uint8_t *s = set->coverage->pixels + h * subpixel_scale * width; - for (int w = 0; w < width * subpixel_scale; w++) { - uint8_t cover = s[w]; - d[w] = (RenColor) {.b = 0xff - cover, .g = 0xff - cover, .r = 0xff - cover, .a = 0xff}; + RenImage *debug_cov_image = ren_new_image(width * subpixel_scale, height); + for (int h = 0; h < height; h++) { + RenColor *d = debug_cov_image->pixels + h * width * subpixel_scale; + uint8_t *s = set->coverage->pixels + h * subpixel_scale * width; + for (int w = 0; w < width * subpixel_scale; w++) { + uint8_t cover = s[w]; + d[w] = (RenColor) {.b = 0xff - cover, .g = 0xff - cover, .r = 0xff - cover, .a = 0xff}; + } } + for (int i = 0; i < 256; i++) { + debug_bitmap_draw_rect(debug_cov_image, &set->glyphs[i]); + } + SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormatFrom( + debug_cov_image->pixels, + width * subpixel_scale, height, 32, width * subpixel_scale * 4, + SDL_PIXELFORMAT_RGBA32); + char img_filename[64]; + sprintf(img_filename, "agg-glyphset-%03d.bmp", debug_image_index); + SDL_SaveBMP(surface, img_filename); + SDL_FreeSurface(surface); + ren_free_image(debug_cov_image); + debug_image_index++; } - for (int i = 0; i < 256; i++) { - debug_bitmap_draw_rect(debug_cov_image, &set->glyphs[i]); - } - SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormatFrom( - debug_cov_image->pixels, - width * subpixel_scale, height, 32, width * subpixel_scale * 4, - SDL_PIXELFORMAT_RGBA32); - char img_filename[64]; - sprintf(img_filename, "agg-glyphset-%03d.bmp", debug_image_index); - SDL_SaveBMP(surface, img_filename); - SDL_FreeSurface(surface); - ren_free_image(debug_cov_image); - debug_image_index++; - } +#endif + /* adjust glyph's xadvance */ for (int i = 0; i < 256; i++) { set->glyphs[i].xadvance = floor(set->glyphs[i].xadvance + 0.5);