mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-25 17:01:17 +01:00
Two paint-encoder bugs and two demo-plumbing bugs together prevented end-to-end rendering. With all four fixed, BungeeTint and similar COLRv0 fonts now render through the GL demo in their proper palette colors. ### Paint encoder 1. hb-gpu-paint-fragment.glsl: the LAYER_SOLID op's payload is a blob-relative texel offset, but the shader was passing it to hb_gpu_draw() as-is -- which treats its argument as an absolute atlas offset. Add the glyph's `base` to the payload before the draw-coverage call so the sub-blob reference resolves inside the paint blob instead of elsewhere in the atlas. 2. hb-gpu-paint.cc push_clip_glyph callback: was resetting pending_palette_index to 0. But harfbuzz's COLRv0 paint emitter calls custom_palette_color BEFORE push_clip_glyph for each layer, so the index set by custom_palette_color was being clobbered before the color callback consumed it -- every layer ended up tagged palette_index=0. Leave pending_palette_index alone in push_clip_glyph. ### Demo plumbing 3. util/gpu/demo-glstate.cc: demo_glstate_create now glUseProgram's its program immediately. The palette uniform upload reads GL_CURRENT_PROGRAM, and glyph upload happens from demo_buffer_current_line() -- which runs before demo_view_setup() would have bound the program. Without this, glGetIntegerv returned 0 and the palette was never uploaded. 4. util/gpu/demo-font.cc: adds lazy demo_font_upload_palette() on first glyph request. Also tries both "hb_gpu_palette" and "hb_gpu_palette[0]" for the uniform lookup since drivers disagree. ### Bonus util/hb-gpu-encode-all.cc: reports paint-encoded and draw-fallback counts / sizes / timings separately so you can tell where time goes in mixed-content fonts. Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
180 lines
4.3 KiB
C++
180 lines
4.3 KiB
C++
/*
|
|
* Copyright 2026 Behdad Esfahbod. All Rights Reserved.
|
|
*/
|
|
|
|
#include "demo-font.h"
|
|
|
|
#include <hb-ot.h>
|
|
|
|
#include <map>
|
|
|
|
typedef std::map<unsigned int, glyph_info_t> glyph_cache_t;
|
|
|
|
struct demo_font_t {
|
|
hb_face_t *face;
|
|
hb_font_t *font;
|
|
glyph_cache_t *glyph_cache;
|
|
demo_atlas_t *atlas;
|
|
hb_gpu_paint_t *p;
|
|
|
|
bool palette_uploaded;
|
|
|
|
unsigned int num_glyphs;
|
|
unsigned int sum_bytes;
|
|
};
|
|
|
|
static void
|
|
demo_font_upload_palette (hb_face_t *face)
|
|
{
|
|
GLint program;
|
|
glGetIntegerv (GL_CURRENT_PROGRAM, &program);
|
|
if (!program) return;
|
|
|
|
GLint loc = glGetUniformLocation (program, "hb_gpu_palette");
|
|
if (loc < 0)
|
|
loc = glGetUniformLocation (program, "hb_gpu_palette[0]");
|
|
if (loc < 0) return;
|
|
|
|
unsigned count = hb_ot_color_palette_get_colors (face, 0, 0, NULL, NULL);
|
|
if (!count) return;
|
|
if (count > 256) count = 256;
|
|
|
|
hb_color_t colors[256];
|
|
unsigned returned = count;
|
|
hb_ot_color_palette_get_colors (face, 0, 0, &returned, colors);
|
|
|
|
float palette[256 * 4] = {0};
|
|
for (unsigned i = 0; i < returned; i++)
|
|
{
|
|
palette[i * 4 + 0] = hb_color_get_red (colors[i]) / 255.f;
|
|
palette[i * 4 + 1] = hb_color_get_green (colors[i]) / 255.f;
|
|
palette[i * 4 + 2] = hb_color_get_blue (colors[i]) / 255.f;
|
|
palette[i * 4 + 3] = hb_color_get_alpha (colors[i]) / 255.f;
|
|
}
|
|
|
|
glUniform4fv (loc, 256, palette);
|
|
}
|
|
|
|
demo_font_t *
|
|
demo_font_create (hb_font_t *hb_font,
|
|
demo_atlas_t *atlas)
|
|
{
|
|
demo_font_t *font = (demo_font_t *) calloc (1, sizeof (demo_font_t));
|
|
|
|
font->face = hb_face_reference (hb_font_get_face (hb_font));
|
|
font->font = hb_font_reference (hb_font);
|
|
font->glyph_cache = new glyph_cache_t ();
|
|
font->atlas = demo_atlas_reference (atlas);
|
|
font->p = hb_gpu_paint_create_or_fail ();
|
|
/* Palette uniform needs a bound GL program; demo_font_create is
|
|
* called before the renderer's program is made current. Upload
|
|
* lazily on the first glyph request. */
|
|
font->palette_uploaded = false;
|
|
|
|
return font;
|
|
}
|
|
|
|
void
|
|
demo_font_destroy (demo_font_t *font)
|
|
{
|
|
if (!font)
|
|
return;
|
|
|
|
hb_gpu_paint_destroy (font->p);
|
|
demo_atlas_destroy (font->atlas);
|
|
delete font->glyph_cache;
|
|
hb_font_destroy (font->font);
|
|
hb_face_destroy (font->face);
|
|
free (font);
|
|
}
|
|
|
|
|
|
hb_face_t *
|
|
demo_font_get_face (demo_font_t *font)
|
|
{
|
|
return font->face;
|
|
}
|
|
|
|
hb_font_t *
|
|
demo_font_get_font (demo_font_t *font)
|
|
{
|
|
return font->font;
|
|
}
|
|
|
|
|
|
static void
|
|
_demo_font_upload_glyph (demo_font_t *font,
|
|
unsigned int glyph_index,
|
|
glyph_info_t *glyph_info)
|
|
{
|
|
if (!font->palette_uploaded)
|
|
{
|
|
demo_font_upload_palette (font->face);
|
|
font->palette_uploaded = true;
|
|
}
|
|
|
|
hb_gpu_paint_clear (font->p);
|
|
hb_gpu_paint_glyph (font->p, font->font, glyph_index);
|
|
|
|
hb_glyph_extents_t hb_ext;
|
|
hb_blob_t *blob = hb_gpu_paint_encode (font->p, &hb_ext);
|
|
if (!blob)
|
|
die ("Failed encoding glyph");
|
|
|
|
unsigned int len = hb_blob_get_length (blob);
|
|
|
|
glyph_info->extents.min_x = hb_ext.x_bearing;
|
|
glyph_info->extents.max_x = hb_ext.x_bearing + hb_ext.width;
|
|
glyph_info->extents.max_y = hb_ext.y_bearing;
|
|
glyph_info->extents.min_y = hb_ext.y_bearing + hb_ext.height;
|
|
glyph_info->advance = hb_font_get_glyph_h_advance (font->font, glyph_index);
|
|
int x_scale, y_scale;
|
|
hb_font_get_scale (font->font, &x_scale, &y_scale);
|
|
glyph_info->upem = y_scale;
|
|
glyph_info->is_empty = (len == 0);
|
|
|
|
if (!glyph_info->is_empty)
|
|
glyph_info->atlas_offset = demo_atlas_alloc (font->atlas,
|
|
hb_blob_get_data (blob, NULL),
|
|
len);
|
|
|
|
font->num_glyphs++;
|
|
font->sum_bytes += len;
|
|
|
|
hb_gpu_paint_recycle_blob (font->p, blob);
|
|
}
|
|
|
|
void
|
|
demo_font_lookup_glyph (demo_font_t *font,
|
|
unsigned int glyph_index,
|
|
glyph_info_t *glyph_info)
|
|
{
|
|
if (font->glyph_cache->find (glyph_index) == font->glyph_cache->end ()) {
|
|
_demo_font_upload_glyph (font, glyph_index, glyph_info);
|
|
(*font->glyph_cache)[glyph_index] = *glyph_info;
|
|
} else
|
|
*glyph_info = (*font->glyph_cache)[glyph_index];
|
|
}
|
|
|
|
void
|
|
demo_font_clear_cache (demo_font_t *font)
|
|
{
|
|
font->glyph_cache->clear ();
|
|
font->num_glyphs = 0;
|
|
font->sum_bytes = 0;
|
|
}
|
|
|
|
void
|
|
demo_font_print_stats (demo_font_t *font)
|
|
{
|
|
double atlas_used_kb = demo_atlas_get_used (font->atlas) * 8 / 1024.;
|
|
|
|
if (!font->num_glyphs)
|
|
return;
|
|
|
|
LOGI ("%3d glyphs; avg %5.2fkb per glyph; atlas used %5.2fkb\n",
|
|
font->num_glyphs,
|
|
font->sum_bytes / 1024. / font->num_glyphs,
|
|
atlas_used_kb);
|
|
}
|