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 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.6 committed 2026-02-22 14:21:09 -03:00
1 parent 9683497463
commit 917d18735f
2 files changed
+32 -10

No files matched your search

+10 -4
View File
@@ -65,7 +65,7 @@ struct ft_raster_output_t : output_options_t<true>
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<true>
FT_Outline_Render (ft_lib, outline, &params);
}
/* 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<uint8_t> 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 ();
}
+22 -6
View File
@@ -51,7 +51,7 @@ struct raster_output_t : output_options_t<true>
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<true>
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<true>
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<uint8_t> 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. */