From 917d18735fcab8973600cfd80642b5cf2aa33606 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 22 Feb 2026 14:20:54 -0300 Subject: [PATCH] raster: always emit PPM for consistent output format Both hb-raster and hb-ft-raster now always produce PPM (color) output, compositing alpha over foreground/background colors. This makes the output format consistent regardless of whether the font has color data. Co-Authored-By: Claude Opus 4.6 --- util/hb-ft-raster.cc | 14 ++++++++++---- util/raster-output.hh | 28 ++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/util/hb-ft-raster.cc b/util/hb-ft-raster.cc index 28a393284..cd1abe6f9 100644 --- a/util/hb-ft-raster.cc +++ b/util/hb-ft-raster.cc @@ -65,7 +65,7 @@ struct ft_raster_output_t : output_options_t void add_options (option_parser_t *parser) { parser->set_summary ("Rasterize text with FreeType rasterizer."); - parser->set_description ("Renders shaped text as a PGM raster image using FreeType."); + parser->set_description ("Renders shaped text as a PPM raster image using FreeType."); output_options_t::add_options (parser); } @@ -275,10 +275,16 @@ struct ft_raster_output_t : output_options_t FT_Outline_Render (ft_lib, outline, ¶ms); } - /* Write PGM, y-flipped so text reads correctly in viewers. */ - fprintf (out_fp, "P5\n%u %u\n255\n", width, height); + /* Write PPM (black on white), y-flipped so text reads correctly. */ + fprintf (out_fp, "P6\n%u %u\n255\n", width, height); + std::vector rgb_row (width * 3); for (unsigned row = 0; row < height; row++) - fwrite (pixels.data () + row * stride, 1, width, out_fp); + { + const uint8_t *src = pixels.data () + row * stride; + for (unsigned x = 0; x < width; x++) + rgb_row[x * 3 + 0] = rgb_row[x * 3 + 1] = rgb_row[x * 3 + 2] = (uint8_t) (255 - src[x]); + fwrite (rgb_row.data (), 1, width * 3, out_fp); + } cleanup (); } diff --git a/util/raster-output.hh b/util/raster-output.hh index 22e45a29c..e64096be4 100644 --- a/util/raster-output.hh +++ b/util/raster-output.hh @@ -51,7 +51,7 @@ struct raster_output_t : output_options_t void add_options (option_parser_t *parser) { parser->set_summary ("Rasterize text with given font."); - parser->set_description ("Renders shaped text as a raster image (PGM for outlines, PPM for color)."); + parser->set_description ("Renders shaped text as a PPM raster image."); output_options_t::add_options (parser); GOptionEntry entries[] = @@ -206,7 +206,7 @@ struct raster_output_t : output_options_t hb_raster_image_t *img = hb_raster_draw_render (rdr); if (img) { - write_pgm (img); + write_a8_as_ppm (img); hb_raster_image_destroy (img); } } @@ -337,17 +337,33 @@ struct raster_output_t : output_options_t write_ppm (out_buf.data (), w, h, stride); } - /* Write a PGM file; Y-flipped so text reads correctly in viewers. */ - void write_pgm (hb_raster_image_t *img) + /* Write an A8 alpha image as PPM; composited over fg/bg, Y-flipped. */ + void write_a8_as_ppm (hb_raster_image_t *img) { hb_raster_extents_t ext; hb_raster_image_get_extents (img, &ext); if (!ext.width || !ext.height) return; + uint8_t fg_r = hb_color_get_red (fg_color); + uint8_t fg_g = hb_color_get_green (fg_color); + uint8_t fg_b = hb_color_get_blue (fg_color); + const uint8_t *buf = hb_raster_image_get_buffer (img); - fprintf (out_fp, "P5\n%u %u\n255\n", ext.width, ext.height); + fprintf (out_fp, "P6\n%u %u\n255\n", ext.width, ext.height); + std::vector row_buf (ext.width * 3); for (unsigned row = 0; row < ext.height; row++) - fwrite (buf + (ext.height - 1 - row) * ext.stride, 1, ext.width, out_fp); + { + const uint8_t *src = buf + (ext.height - 1 - row) * ext.stride; + for (unsigned x = 0; x < ext.width; x++) + { + uint8_t a = src[x]; + unsigned inv_a = 255 - a; + row_buf[x * 3 + 0] = (uint8_t) ((fg_r * a + bg_r * inv_a + 127) / 255); + row_buf[x * 3 + 1] = (uint8_t) ((fg_g * a + bg_g * inv_a + 127) / 255); + row_buf[x * 3 + 2] = (uint8_t) ((fg_b * a + bg_b * inv_a + 127) / 255); + } + fwrite (row_buf.data (), 1, ext.width * 3, out_fp); + } } /* Write a PPM file from BGRA32 buffer; Y-flipped, composited over white. */