blob: cbccf3819bab03e65a5446ad25a62e256bcdc817 [file]
/*
* Copyright © 2026 Behdad Esfahbod
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Author(s): Behdad Esfahbod
*/
#include "hb-test.h"
#include "hb-raster.h"
#include <hb-ot.h>
#include <cmath>
/* ── Helpers ─────────────────────────────────────────────────────── */
/* Draw a closed rectangle by emitting raw draw callbacks. */
static void
draw_rect (hb_raster_draw_t *rdr,
float x0, float y0, float x1, float y1)
{
hb_draw_funcs_t *df = hb_raster_draw_get_funcs (rdr);
hb_draw_state_t st = HB_DRAW_STATE_DEFAULT;
hb_draw_move_to (df, rdr, &st, x0, y0);
hb_draw_line_to (df, rdr, &st, x1, y0);
hb_draw_line_to (df, rdr, &st, x1, y1);
hb_draw_line_to (df, rdr, &st, x0, y1);
hb_draw_close_path (df, rdr, &st); /* emits closing line_to(x0,y0) */
}
/* Read one A8 pixel value; returns 0 for out-of-bounds coordinates. */
static uint8_t
pixel_at (hb_raster_image_t *img, int x, int y)
{
hb_raster_extents_t ext;
hb_raster_image_get_extents (img, &ext);
int col = x - ext.x_origin;
int row = y - ext.y_origin;
if (col < 0 || row < 0 ||
(unsigned) col >= ext.width || (unsigned) row >= ext.height)
return 0;
return hb_raster_image_get_buffer (img)[row * ext.stride + col];
}
/* ── Test 1: rectangle geometry ──────────────────────────────────── */
static void
test_rectangle (void)
{
hb_raster_draw_t *rdr = hb_raster_draw_create_or_fail ();
draw_rect (rdr, 2.f, 2.f, 30.f, 30.f);
hb_raster_image_t *img = hb_raster_draw_render (rdr);
g_assert_nonnull (img);
hb_raster_extents_t ext;
hb_raster_image_get_extents (img, &ext);
g_assert_cmpint (ext.x_origin, ==, 2);
g_assert_cmpint (ext.y_origin, ==, 2);
g_assert_cmpuint (ext.width, ==, 28);
g_assert_cmpuint (ext.height, ==, 28);
/* Center: fully inside → full coverage */
g_assert_cmpint (pixel_at (img, 16, 16), ==, 255);
/* Well outside the box → zero */
g_assert_cmpint (pixel_at (img, 0, 0), ==, 0); /* out of image */
g_assert_cmpint (pixel_at (img, 31, 31), ==, 0); /* out of image */
/* Left-most and right-most pixel columns: still fully inside
because integer-coordinate box edges align with pixel boundaries */
g_assert_cmpint (pixel_at (img, 2, 16), ==, 255);
g_assert_cmpint (pixel_at (img, 29, 16), ==, 255);
/* One pixel beyond the box right edge: out of image */
g_assert_cmpint (pixel_at (img, 30, 16), ==, 0);
hb_raster_image_destroy (img);
/* render() with no accumulated edges → empty (0×0) image */
img = hb_raster_draw_render (rdr);
g_assert_nonnull (img);
hb_raster_image_get_extents (img, &ext);
g_assert_cmpuint (ext.width, ==, 0);
g_assert_cmpuint (ext.height, ==, 0);
hb_raster_image_destroy (img);
hb_raster_draw_destroy (rdr);
}
/* ── Test 2: multi-glyph accumulation ────────────────────────────── */
static void
test_accumulate (void)
{
hb_raster_draw_t *rdr = hb_raster_draw_create_or_fail ();
/* Two non-overlapping boxes accumulated before a single render() */
draw_rect (rdr, 0.f, 0.f, 10.f, 10.f);
draw_rect (rdr, 20.f, 0.f, 30.f, 10.f);
hb_raster_image_t *img = hb_raster_draw_render (rdr);
g_assert_nonnull (img);
/* Both interiors fully covered */
g_assert_cmpint (pixel_at (img, 5, 5), ==, 255);
g_assert_cmpint (pixel_at (img, 25, 5), ==, 255);
/* Gap between the two boxes is empty */
g_assert_cmpint (pixel_at (img, 15, 5), ==, 0);
hb_raster_image_destroy (img);
hb_raster_draw_destroy (rdr);
}
/* ── Test 3: sub-pixel edge coverage ─────────────────────────────── */
static void
test_subpixel_edge (void)
{
hb_raster_draw_t *rdr = hb_raster_draw_create_or_fail ();
/* Box with a half-pixel offset so the leftmost pixel column is split */
draw_rect (rdr, 2.5f, 2.f, 30.f, 30.f);
hb_raster_image_t *img = hb_raster_draw_render (rdr);
g_assert_nonnull (img);
/* The pixel at x=2 straddles the left edge → partial coverage */
int v = pixel_at (img, 2, 16);
g_assert_cmpint (v, >, 0);
g_assert_cmpint (v, <, 255);
/* The pixel at x=3 is fully inside */
g_assert_cmpint (pixel_at (img, 3, 16), ==, 255);
hb_raster_image_destroy (img);
hb_raster_draw_destroy (rdr);
}
/* ── Test 4: wide scanline accumulators ─────────────────────────── */
static void
test_many_coincident_edges (void)
{
hb_raster_draw_t *rdr = hb_raster_draw_create_or_fail ();
/* A vertical edge at x=255/256 contributes 510*256 to one area cell.
* 16,449 such edges exceed INT32_MAX. */
for (unsigned i = 0; i < 16449; i++)
draw_rect (rdr, 0.f, 0.f, 255.f / 256.f, 1.f);
hb_raster_image_t *img = hb_raster_draw_render (rdr);
g_assert_nonnull (img);
g_assert_cmpint (pixel_at (img, 0, 0), ==, 255);
hb_raster_image_destroy (img);
/* 256 coincident unit boxes overflow the narrow cover accumulator. */
for (unsigned i = 0; i < 256; i++)
draw_rect (rdr, 0.f, 0.f, 1.f, 1.f);
img = hb_raster_draw_render (rdr);
g_assert_nonnull (img);
g_assert_cmpint (pixel_at (img, 0, 0), ==, 255);
hb_raster_image_destroy (img);
hb_raster_draw_destroy (rdr);
}
/* ── Test 5: transform ───────────────────────────────────────────── */
static void
test_transform (void)
{
hb_raster_draw_t *rdr = hb_raster_draw_create_or_fail ();
/* Scale by 2 — same unit-square box maps to (0,0)→(20,20) */
hb_raster_draw_set_transform (rdr, 2.f, 0.f, 0.f, 2.f, 0.f, 0.f);
draw_rect (rdr, 0.f, 0.f, 10.f, 10.f);
hb_raster_image_t *img = hb_raster_draw_render (rdr);
g_assert_nonnull (img);
hb_raster_extents_t ext;
hb_raster_image_get_extents (img, &ext);
g_assert_cmpuint (ext.width, ==, 20);
g_assert_cmpuint (ext.height, ==, 20);
g_assert_cmpint (pixel_at (img, 10, 10), ==, 255); /* center of scaled box */
hb_raster_image_destroy (img);
hb_raster_draw_destroy (rdr);
}
/* ── Test 6: transformed glyph extents helper ───────────────────── */
static void
test_set_glyph_extents_with_transform (void)
{
hb_raster_draw_t *rdr = hb_raster_draw_create_or_fail ();
hb_raster_draw_set_transform (rdr, 2.f, 0.f, 0.f, 3.f, 5.f, 7.f);
hb_glyph_extents_t gext = {
1, /* x_bearing */
4, /* y_bearing */
10, /* width */
-6 /* height */
};
g_assert_true (hb_raster_draw_set_glyph_extents (rdr, &gext));
draw_rect (rdr, (float) gext.x_bearing,
(float) gext.y_bearing + gext.height,
(float) gext.x_bearing + gext.width,
(float) gext.y_bearing);
hb_raster_image_t *img = hb_raster_draw_render (rdr);
g_assert_nonnull (img);
hb_raster_extents_t ext;
hb_raster_image_get_extents (img, &ext);
g_assert_cmpint (ext.x_origin, ==, 7); /* 2*1 + 5 */
g_assert_cmpint (ext.y_origin, ==, 1); /* 3*(-2) + 7 */
g_assert_cmpuint (ext.width, ==, 20); /* 2*10 */
g_assert_cmpuint (ext.height, ==, 18); /* 3*6 */
hb_raster_image_destroy (img);
hb_raster_draw_destroy (rdr);
}
/* ── Test 7: image paint under an overflowing transform ──────────── */
/* Nested scales, each representable, whose product overflows to infinity.
* The inverse transform then carries NaN into the image sampler's texel
* coordinates. Exercised for the float-to-int conversion, so this needs a
* sanitizer build to catch a regression. */
static void
test_image_nonfinite_transform (void)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
g_assert_nonnull (paint);
hb_raster_extents_t ext = {0, 0, 32, 32, 0};
hb_raster_paint_set_extents (paint, &ext);
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_paint_push_transform (funcs, paint, 1e30f, 0.f, 0.f, 1e30f, 0.f, 0.f);
hb_paint_push_transform (funcs, paint, 1e30f, 0.f, 0.f, 1e30f, 0.f, 0.f);
const unsigned w = 4, h = 4;
char pixels[w * h * 4];
memset (pixels, 0, sizeof (pixels));
hb_blob_t *blob = hb_blob_create (pixels, sizeof (pixels),
HB_MEMORY_MODE_READONLY, nullptr, nullptr);
hb_glyph_extents_t gext = {0, 32, 32, -32};
hb_paint_image (funcs, paint, blob, w, h,
HB_PAINT_IMAGE_FORMAT_BGRA, 0.f, &gext);
hb_paint_pop_transform (funcs, paint);
hb_paint_pop_transform (funcs, paint);
hb_blob_destroy (blob);
hb_raster_paint_destroy (paint);
}
/* ── Test 8: glyph extents that overflow the int grid ────────────── */
/* Glyph extents are int32, so a transform that scales them up puts the
* corner coordinates far outside the int range before they are floored
* back to the integer pixel grid. Exercised for the conversion itself,
* so this needs a sanitizer build to catch a regression. */
static void
test_set_glyph_extents_overflow (void)
{
hb_glyph_extents_t gext = {-2000000000, 2000000000, 2000000000, -2000000000};
hb_raster_draw_t *rdr = hb_raster_draw_create_or_fail ();
g_assert_nonnull (rdr);
hb_raster_draw_set_transform (rdr, 1000.f, 0.f, 0.f, 1000.f, 0.f, 0.f);
hb_raster_draw_set_glyph_extents (rdr, &gext);
hb_raster_draw_destroy (rdr);
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
g_assert_nonnull (paint);
hb_raster_paint_set_transform (paint, 1000.f, 0.f, 0.f, 1000.f, 0.f, 0.f);
hb_raster_paint_set_glyph_extents (paint, &gext);
hb_raster_paint_destroy (paint);
}
/* ── Test 9: shared work-budget lifecycle ───────────────────────── */
static void
test_budget (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/glyphs.ttf");
hb_font_t *font = hb_font_create (face);
hb_raster_draw_t *draw = hb_raster_draw_create_or_fail ();
hb_draw_funcs_t *draw_funcs = hb_raster_draw_get_funcs (draw);
g_assert_cmpint (hb_draw_get_budget (draw_funcs, draw), ==, HB_BUDGET_DEFAULT);
g_assert_cmpint (hb_draw_get_budget_remaining (draw_funcs, draw), >, 0);
g_assert_cmpint (hb_draw_get_budget_remaining (draw_funcs, draw), <, HB_BUDGET_UNLIMITED);
g_assert_true (hb_draw_set_budget (draw_funcs, draw, 1));
hb_raster_draw_glyph (draw, font, 0);
g_assert_cmpint (hb_draw_get_budget_remaining (draw_funcs, draw), <, 0);
hb_raster_draw_clear (draw);
g_assert_cmpint (hb_draw_get_budget (draw_funcs, draw), ==, 1);
g_assert_cmpint (hb_draw_get_budget_remaining (draw_funcs, draw), ==, 1);
hb_raster_draw_reset (draw);
g_assert_cmpint (hb_draw_get_budget (draw_funcs, draw), ==, HB_BUDGET_DEFAULT);
g_assert_cmpint (hb_draw_get_budget_remaining (draw_funcs, draw), >, 0);
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
hb_paint_funcs_t *paint_funcs = hb_raster_paint_get_funcs (paint);
g_assert_cmpint (hb_paint_get_budget (paint_funcs, paint), ==, HB_BUDGET_DEFAULT);
g_assert_cmpint (hb_paint_get_budget_remaining (paint_funcs, paint), >, 0);
g_assert_cmpint (hb_paint_get_budget_remaining (paint_funcs, paint), <, HB_BUDGET_UNLIMITED);
hb_raster_extents_t extents = {0, 0, 1, 1, 0};
hb_raster_paint_set_extents (paint, &extents);
g_assert_true (hb_paint_set_budget (paint_funcs, paint, 2));
hb_raster_paint_glyph (paint, font, 0);
g_assert_cmpint (hb_paint_get_budget_remaining (paint_funcs, paint), <, 0);
hb_raster_paint_clear (paint);
g_assert_cmpint (hb_paint_get_budget (paint_funcs, paint), ==, 2);
g_assert_cmpint (hb_paint_get_budget_remaining (paint_funcs, paint), ==, 2);
hb_raster_paint_reset (paint);
g_assert_cmpint (hb_paint_get_budget (paint_funcs, paint), ==, HB_BUDGET_DEFAULT);
g_assert_cmpint (hb_paint_get_budget_remaining (paint_funcs, paint), >, 0);
g_assert_true (hb_draw_set_budget (draw_funcs, draw, HB_BUDGET_UNLIMITED));
g_assert_cmpint (hb_draw_get_budget_remaining (draw_funcs, draw), ==, HB_BUDGET_UNLIMITED);
g_assert_true (hb_paint_set_budget (paint_funcs, paint, HB_BUDGET_UNLIMITED));
g_assert_cmpint (hb_paint_get_budget_remaining (paint_funcs, paint), ==, HB_BUDGET_UNLIMITED);
hb_raster_paint_destroy (paint);
hb_raster_draw_destroy (draw);
hb_font_destroy (font);
hb_face_destroy (face);
}
/* ── Test 9b: curves through NaN or infinity are not subdivided ──── */
/* A transform that overflows turns the points of an outline into NaN and
* infinity. No depth of subdivision flattens a curve through NaN, and
* none of its segments becomes an edge, so neither the clip box nor the
* edge cap stops it: it must cost nothing, not the 65,535 subdivisions
* of the depth cap. A curve through infinity costs at most one
* subdivision per level. */
static void
test_budget_nonfinite_curve (void)
{
hb_raster_draw_t *draw = hb_raster_draw_create_or_fail ();
hb_draw_funcs_t *funcs = hb_raster_draw_get_funcs (draw);
/* With the extents left to the outline, and with a clip box. */
for (unsigned fixed = 0; fixed < 2; fixed++)
{
hb_raster_draw_reset (draw);
if (fixed)
{
hb_raster_extents_t extents = {0, 0, 32, 32, 0};
hb_raster_draw_set_extents (draw, &extents);
}
g_assert_true (hb_draw_set_budget (funcs, draw, 256));
hb_draw_state_t st = HB_DRAW_STATE_DEFAULT;
hb_draw_move_to (funcs, draw, &st, NAN, NAN);
hb_draw_quadratic_to (funcs, draw, &st, NAN, NAN, NAN, NAN);
hb_draw_cubic_to (funcs, draw, &st, NAN, NAN, NAN, NAN, NAN, NAN);
/* NaN in one coordinate only, the other one flat. */
hb_draw_move_to (funcs, draw, &st, 1.f, 2.f);
hb_draw_quadratic_to (funcs, draw, &st, NAN, 3.f, 4.f, 4.f);
hb_draw_cubic_to (funcs, draw, &st, 6.f, 5.f, 8.f, NAN, 10.f, 7.f);
g_assert_cmpint (hb_draw_get_budget_remaining (funcs, draw), ==, 256);
hb_draw_move_to (funcs, draw, &st, 1.f, 2.f);
hb_draw_quadratic_to (funcs, draw, &st, 3.f, INFINITY, 4.f, 5.f);
hb_draw_quadratic_to (funcs, draw, &st, 6.f, 7.f, -INFINITY, 8.f);
hb_draw_cubic_to (funcs, draw, &st, INFINITY, 9.f, 10.f, -INFINITY, 11.f, 12.f);
hb_draw_cubic_to (funcs, draw, &st, 13.f, 14.f, 15.f, 16.f, INFINITY, INFINITY);
hb_draw_close_path (funcs, draw, &st);
g_assert_cmpint (hb_draw_get_budget_remaining (funcs, draw), >, 0);
hb_raster_image_destroy (hb_raster_draw_render (draw));
}
hb_raster_draw_destroy (draw);
}
/* ── Test 9c: a curve too large to flatten ends the outline ──────── */
/* A curve is halved until it is flat, twelve times at most. One that is
* still not flat then, as when a transform puts an outline astronomically
* far away, ends the outline: it costs a subdivision per level, and
* nothing that follows it is flattened. */
static void
test_budget_curve_too_large (void)
{
hb_raster_draw_t *draw = hb_raster_draw_create_or_fail ();
hb_draw_funcs_t *funcs = hb_raster_draw_get_funcs (draw);
const int64_t budget = 1 << 20;
/* With the extents left to the outline, and with a clip box. */
for (unsigned fixed = 0; fixed < 2; fixed++)
{
/* A hundred thousand pixels across is within reach, here from a
* point within the clip box: the curve is flattened in full, and so
* is one after it. */
hb_raster_draw_reset (draw);
if (fixed)
{
hb_raster_extents_t extents = {0, 0, 32, 32, 0};
hb_raster_draw_set_extents (draw, &extents);
}
g_assert_true (hb_draw_set_budget (funcs, draw, budget));
hb_draw_state_t st = HB_DRAW_STATE_DEFAULT;
hb_draw_move_to (funcs, draw, &st, 0.f, 0.f);
hb_draw_quadratic_to (funcs, draw, &st, 1e5f, 0.f, 1e5f, 1e5f);
int64_t left = hb_draw_get_budget_remaining (funcs, draw);
g_assert_cmpint (left, <, budget);
hb_draw_move_to (funcs, draw, &st, 0.f, 0.f);
hb_draw_cubic_to (funcs, draw, &st, 20.f, 0.f, 30.f, 10.f, 30.f, 30.f);
g_assert_cmpint (hb_draw_get_budget_remaining (funcs, draw), <, left);
/* 1e30 pixels across is not. */
hb_raster_draw_reset (draw);
if (fixed)
{
hb_raster_extents_t extents = {0, 0, 32, 32, 0};
hb_raster_draw_set_extents (draw, &extents);
}
g_assert_true (hb_draw_set_budget (funcs, draw, budget));
st = HB_DRAW_STATE_DEFAULT;
hb_draw_move_to (funcs, draw, &st, 0.f, 0.f);
hb_draw_quadratic_to (funcs, draw, &st, 1e30f, 0.f, 1e30f, 1e30f);
left = hb_draw_get_budget_remaining (funcs, draw);
g_assert_cmpint (left, >=, budget - 12);
hb_draw_move_to (funcs, draw, &st, 0.f, 0.f);
hb_draw_cubic_to (funcs, draw, &st, 20.f, 0.f, 30.f, 10.f, 30.f, 30.f);
hb_draw_quadratic_to (funcs, draw, &st, 1e30f, 0.f, 1e30f, 1e30f);
g_assert_cmpint (hb_draw_get_budget_remaining (funcs, draw), ==, left);
/* Nor is a cubic that size. */
hb_raster_draw_reset (draw);
if (fixed)
{
hb_raster_extents_t extents = {0, 0, 32, 32, 0};
hb_raster_draw_set_extents (draw, &extents);
}
g_assert_true (hb_draw_set_budget (funcs, draw, budget));
st = HB_DRAW_STATE_DEFAULT;
hb_draw_move_to (funcs, draw, &st, 0.f, 0.f);
hb_draw_cubic_to (funcs, draw, &st, 1e30f, 0.f, 1e30f, 1e30f, 0.f, 1e30f);
left = hb_draw_get_budget_remaining (funcs, draw);
g_assert_cmpint (left, >=, budget - 12);
hb_draw_move_to (funcs, draw, &st, 0.f, 0.f);
hb_draw_cubic_to (funcs, draw, &st, 20.f, 0.f, 30.f, 10.f, 30.f, 30.f);
g_assert_cmpint (hb_draw_get_budget_remaining (funcs, draw), ==, left);
hb_raster_image_destroy (hb_raster_draw_render (draw));
}
hb_raster_draw_destroy (draw);
}
/* ── Test 10: outline budget bounds a real COLR walk, apart from pixels ─ */
/* The paint session's outline and pixel budgets are separate. A 1x1
* surface makes the pixel budget effectively unbounded, so only the
* outline budget can bound a multi-layer COLR glyph here -- which
* requires the outline work of every layer to aggregate into one counter
* through the public seed/readback transfer. A broken transfer (no
* readback, or a re-armed counter) would leave the budget positive. */
static void
test_budget_colr_outline (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/COLRv0.extents.ttf");
hb_font_t *font = hb_font_create (face);
const hb_codepoint_t colr_glyph = 13; /* 5 COLR layers */
hb_raster_extents_t extents = {0, 0, 1, 1, 0};
/* Large outline budget: the walk completes, and real outline work is
* charged back (the counter drops below the configured cap). */
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_raster_paint_set_extents (paint, &extents);
g_assert_true (hb_paint_set_budget (funcs, paint, 1 << 20));
hb_raster_paint_glyph (paint, font, colr_glyph);
int64_t remaining = hb_paint_get_budget_remaining (funcs, paint);
g_assert_cmpint (remaining, <, 1 << 20); /* outline work aggregated back */
g_assert_cmpint (remaining, >=, 0); /* not depleted at this budget */
hb_raster_paint_destroy (paint);
/* Tiny outline budget: the aggregate walk is bounded by it, not by the
* (unbounded) pixel budget of the 1x1 surface. */
paint = hb_raster_paint_create_or_fail ();
funcs = hb_raster_paint_get_funcs (paint);
hb_raster_paint_set_extents (paint, &extents);
g_assert_true (hb_paint_set_budget (funcs, paint, 1));
hb_raster_paint_glyph (paint, font, colr_glyph);
g_assert_cmpint (hb_paint_get_budget_remaining (funcs, paint), <, 0);
hb_raster_paint_destroy (paint);
hb_font_destroy (font);
hb_face_destroy (face);
}
/* ── Test 11: background color is premultiplied before filling ─────── */
/* hb_raster_paint_set_background() must store the premultiplied BGRA32
* pixel in the buffer, matching the format every other paint op in this
* file assumes (see hb-raster.hh's SRC_OVER: "premultiplied src over
* premultiplied dst"), not the raw unpremultiplied channel bytes. */
static void
test_background_premultiplied (void)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
const unsigned width = 2, height = 2;
hb_raster_extents_t ext = {0, 0, width, height, 0};
hb_raster_paint_set_extents (paint, &ext);
/* Half-alpha opaque red: blue=0, green=0, red=255, alpha=128.
* Premultiplied: r = div255(255*128) = 128, a stays 128. */
hb_raster_paint_set_background (paint, HB_COLOR (0, 0, 255, 128));
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_paint_push_clip_rectangle (funcs, paint, 0.f, 0.f, (float) width, (float) height);
hb_paint_pop_clip (funcs, paint);
hb_raster_image_t *img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
hb_raster_extents_t out_ext;
hb_raster_image_get_extents (img, &out_ext);
const uint8_t *buf = hb_raster_image_get_buffer (img);
for (unsigned y = 0; y < out_ext.height; y++)
{
const uint8_t *row = buf + (size_t) y * out_ext.stride;
for (unsigned x = 0; x < out_ext.width; x++)
{
const uint8_t *pixel = row + x * 4;
g_assert_cmphex (pixel[0], ==, 0x00);
g_assert_cmphex (pixel[1], ==, 0x00);
g_assert_cmphex (pixel[2], ==, 0x80);
g_assert_cmphex (pixel[3], ==, 0x80);
}
}
hb_raster_image_destroy (img);
hb_raster_paint_destroy (paint);
}
/* ── Test 12: background pre-fill honors a stride wider than width*4 ── */
/* hb_raster_paint_set_background() must pre-fill every pixel of the
* output image with the premultiplied BGRA32 background color, honoring
* a caller-supplied stride wider than width * 4 bytes. */
static void
test_background_stride (void)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
const unsigned width = 2, height = 4;
const unsigned stride = 9; /* wider than width * 4, but not word-aligned */
hb_raster_extents_t ext = {0, 0, width, height, stride};
hb_raster_paint_set_extents (paint, &ext);
/* Opaque red: blue=0, green=0, red=255, alpha=255. */
hb_raster_paint_set_background (paint, HB_COLOR (0, 0, 255, 255));
/* Trigger initialization without painting any pixels directly, so the
* only thing that can have colored a pixel is the background fill. */
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_paint_push_clip_rectangle (funcs, paint, 0.f, 0.f, (float) width, (float) height);
hb_paint_pop_clip (funcs, paint);
hb_raster_image_t *img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
hb_raster_extents_t out_ext;
hb_raster_image_get_extents (img, &out_ext);
const uint8_t *buf = hb_raster_image_get_buffer (img);
for (unsigned y = 0; y < out_ext.height; y++)
{
const uint8_t *row = buf + (size_t) y * out_ext.stride;
for (unsigned x = 0; x < out_ext.width; x++)
{
const uint8_t *pixel = row + x * 4;
g_assert_cmphex (pixel[0], ==, 0x00); /* blue */
g_assert_cmphex (pixel[1], ==, 0x00); /* green */
g_assert_cmphex (pixel[2], ==, 0xff); /* red */
g_assert_cmphex (pixel[3], ==, 0xff); /* alpha */
}
}
hb_raster_image_destroy (img);
hb_raster_paint_destroy (paint);
}
static void
test_paint_reset (void)
{
hb_raster_paint_t *fresh = hb_raster_paint_create_or_fail ();
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
g_assert_nonnull (fresh);
g_assert_nonnull (paint);
hb_color_t background = HB_COLOR (0, 255, 0, 255);
hb_raster_paint_set_background (paint, background);
hb_raster_paint_set_palette (paint, 7);
hb_raster_paint_clear (paint);
g_assert_cmphex (hb_raster_paint_get_background (paint), ==, background);
g_assert_cmpuint (hb_raster_paint_get_palette (paint), ==, 7);
/* Populate the image cache with output using the old background. */
hb_raster_extents_t ext = {0, 0, 1, 1, 0};
hb_raster_paint_set_extents (paint, &ext);
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_paint_push_clip_rectangle (funcs, paint, 0.f, 0.f, 1.f, 1.f);
hb_paint_pop_clip (funcs, paint);
hb_raster_image_t *image = hb_raster_paint_render (paint);
g_assert_nonnull (image);
g_assert_cmpuint (hb_raster_image_get_buffer (image)[3], ==, 255);
hb_raster_paint_recycle_image (paint, image);
hb_raster_paint_reset (paint);
g_assert_cmphex (hb_raster_paint_get_background (paint), ==,
hb_raster_paint_get_background (fresh));
g_assert_cmpuint (hb_raster_paint_get_palette (paint), ==,
hb_raster_paint_get_palette (fresh));
hb_raster_paint_set_extents (paint, &ext);
hb_paint_push_clip_rectangle (funcs, paint, 0.f, 0.f, 1.f, 1.f);
hb_paint_pop_clip (funcs, paint);
image = hb_raster_paint_render (paint);
g_assert_nonnull (image);
g_assert_cmpuint (hb_raster_image_get_buffer (image)[3], ==, 0);
hb_raster_image_destroy (image);
hb_raster_paint_destroy (paint);
hb_raster_paint_destroy (fresh);
}
/* Changing extents while a paint is in progress must not shrink group
* surfaces below the root surface and initial clip they composite onto. */
static void
test_paint_recycle_null (void)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
g_assert_nonnull (paint);
/* A render that fails gives no image. Recycling that does nothing,
* and leaves nothing for the next render to trip over. */
hb_raster_image_t *image = hb_raster_paint_render (paint);
g_assert_null (image);
hb_raster_paint_recycle_image (paint, image);
hb_raster_extents_t ext = {0, 0, 1, 1, 0};
hb_raster_paint_set_extents (paint, &ext);
image = hb_raster_paint_render (paint);
g_assert_nonnull (image);
hb_raster_image_destroy (image);
hb_raster_paint_destroy (paint);
}
/* The HSL modes take their luminosity weights from the W3C
* Compositing and Blending specification: 0.3, 0.59, 0.11. */
static void
test_paint_hsl_luminosity_weights (void)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
g_assert_nonnull (paint);
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_raster_extents_t ext = {0, 0, 1, 1, 0};
hb_raster_paint_set_extents (paint, &ext);
hb_paint_color (funcs, paint, true, HB_COLOR (255, 0, 0, 255)); /* blue */
hb_paint_push_group (funcs, paint);
hb_paint_color (funcs, paint, true, HB_COLOR (0, 0, 255, 255)); /* red */
hb_paint_pop_group (funcs, paint, HB_PAINT_COMPOSITE_MODE_HSL_LUMINOSITY);
hb_raster_image_t *img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
const uint8_t *pixel = hb_raster_image_get_buffer (img);
/* Red's luminosity, 0.3, put on blue: (0.19, 0.19, 1.19), clipped
* to (0.2135, 0.2135, 1). */
g_assert_cmpuint (pixel[0], ==, 255); /* blue */
g_assert_cmpuint (pixel[1], ==, 54); /* green */
g_assert_cmpuint (pixel[2], ==, 54); /* red */
g_assert_cmpuint (pixel[3], ==, 255); /* alpha */
hb_raster_image_destroy (img);
hb_raster_paint_destroy (paint);
}
/* ── Test: a radial gradient ends at the tip of its cone ─────────── */
static unsigned
two_color_stops (hb_color_line_t *, void *,
unsigned start, unsigned *count,
hb_color_stop_t *stops, void *)
{
static const hb_color_stop_t line[2] = {
{0.f, false, HB_COLOR (0, 0, 255, 255)},
{1.f, false, HB_COLOR (255, 0, 0, 255)},
};
if (count)
{
unsigned n = 0;
for (; n < *count && start + n < 2; n++)
stops[n] = line[start + n];
*count = n;
}
return 2;
}
static hb_paint_extend_t
pad_extend (hb_color_line_t *, void *, void *)
{ return HB_PAINT_EXTEND_PAD; }
static void
test_paint_radial_gradient_tip (void)
{
/* A circle of radius 10 at x = 60 and one of radius 30 at x = 100:
* the radius is zero at x = 40, the tip of the cone they sweep.
* Past the tip the radius is negative, and a circle of negative
* radius is not painted: no mirror image of the cone. */
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
g_assert_nonnull (paint);
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_raster_extents_t extents = {0, 0, 120, 100, 0};
hb_raster_paint_set_extents (paint, &extents);
hb_color_line_t line = {};
line.get_color_stops = two_color_stops;
line.get_extend = pad_extend;
hb_paint_push_clip_rectangle (funcs, paint, 0, 0, 120, 100);
hb_paint_radial_gradient (funcs, paint, &line, 60, 50, 10, 100, 50, 30);
hb_paint_pop_clip (funcs, paint);
hb_raster_image_t *img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
hb_raster_extents_t ext;
hb_raster_image_get_extents (img, &ext);
const uint8_t *buf = hb_raster_image_get_buffer (img);
/* Alpha along the axis of the cone. */
auto alpha = [&] (unsigned x) -> unsigned { return buf[50 * ext.stride + x * 4 + 3]; };
g_assert_cmpuint (alpha (110), ==, 255);
g_assert_cmpuint (alpha (80), ==, 255);
g_assert_cmpuint (alpha (45), ==, 255);
g_assert_cmpuint (alpha (35), ==, 0);
g_assert_cmpuint (alpha (10), ==, 0);
hb_raster_image_destroy (img);
hb_raster_paint_destroy (paint);
}
static hb_paint_extend_t
extend_from_user_data (hb_color_line_t *, void *, void *user_data)
{ return *(hb_paint_extend_t *) user_data; }
/* Paints a radial gradient on 8 by 3 pixels, the middle row of which
* has its pixel centers on the line through the two centers; returns
* how many pixels of that row and of the other two are painted. */
static void
paint_radial_gradient_rows (float x0, float r0, float x1, float r1,
hb_paint_extend_t extend,
unsigned *middle, unsigned *others)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
g_assert_nonnull (paint);
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_raster_extents_t extents = {0, 0, 8, 3, 0};
hb_raster_paint_set_extents (paint, &extents);
hb_color_line_t line = {};
line.get_color_stops = two_color_stops;
line.get_extend = extend_from_user_data;
line.get_extend_user_data = &extend;
hb_paint_push_clip_rectangle (funcs, paint, 0, 0, 8, 3);
hb_paint_radial_gradient (funcs, paint, &line, x0, 1.5f, r0, x1, 1.5f, r1);
hb_paint_pop_clip (funcs, paint);
hb_raster_image_t *img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
hb_raster_extents_t ext;
hb_raster_image_get_extents (img, &ext);
const uint8_t *buf = hb_raster_image_get_buffer (img);
*middle = *others = 0;
for (unsigned y = 0; y < 3; y++)
for (unsigned x = 0; x < 8; x++)
if (buf[y * ext.stride + x * 4 + 3])
(*(y == 1 ? middle : others))++;
hb_raster_image_destroy (img);
hb_raster_paint_destroy (paint);
}
/* If the radius is zero at both ends of a radial gradient it is zero
* all along, and circles of radius zero are not painted: nothing is,
* not even on the line through the two centers, where they are. */
static void
test_paint_radial_gradient_zero_radii (void)
{
const hb_paint_extend_t extends[] = {HB_PAINT_EXTEND_PAD,
HB_PAINT_EXTEND_REPEAT,
HB_PAINT_EXTEND_REFLECT};
for (hb_paint_extend_t extend : extends)
{
unsigned middle, others;
/* Two centers, and one. */
paint_radial_gradient_rows (0.f, 0.f, 8.f, 0.f, extend, &middle, &others);
g_assert_cmpuint (middle, ==, 0);
g_assert_cmpuint (others, ==, 0);
paint_radial_gradient_rows (4.f, 0.f, 4.f, 0.f, extend, &middle, &others);
g_assert_cmpuint (middle, ==, 0);
g_assert_cmpuint (others, ==, 0);
/* A radius of zero at one end alone is the tip of a cone, or the
* middle of rings: those are painted, the pixel centered there
* too. */
paint_radial_gradient_rows (0.5f, 0.f, 7.5f, 3.f, extend, &middle, &others);
g_assert_cmpuint (middle, ==, 8);
g_assert_cmpuint (others, >, 0);
paint_radial_gradient_rows (4.5f, 0.f, 4.5f, 3.f, extend, &middle, &others);
g_assert_cmpuint (middle, ==, 8);
g_assert_cmpuint (others, ==, 16);
}
}
static void
test_set_extents_while_painting (void)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
g_assert_nonnull (paint);
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_raster_extents_t big = {0, 0, 64, 64, 0};
hb_raster_extents_t small = {0, 0, 4, 4, 0};
hb_raster_paint_set_extents (paint, &big);
hb_paint_color (funcs, paint, true, HB_COLOR (255, 0, 0, 255));
hb_raster_paint_set_extents (paint, &small);
hb_paint_push_group (funcs, paint);
hb_paint_color (funcs, paint, true, HB_COLOR (0, 0, 255, 255));
hb_paint_pop_group (funcs, paint, HB_PAINT_COMPOSITE_MODE_SRC_OVER);
hb_raster_image_t *img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
hb_raster_extents_t out_ext;
hb_raster_image_get_extents (img, &out_ext);
g_assert_cmpuint (out_ext.width, ==, big.width);
g_assert_cmpuint (out_ext.height, ==, big.height);
const uint8_t *buf = hb_raster_image_get_buffer (img);
for (unsigned y = 0; y < out_ext.height; y++)
{
const uint8_t *row = buf + (size_t) y * out_ext.stride;
for (unsigned x = 0; x < out_ext.width; x++)
{
const uint8_t *pixel = row + x * 4;
g_assert_cmphex (pixel[0], ==, 0x00); /* blue */
g_assert_cmphex (pixel[1], ==, 0x00); /* green */
g_assert_cmphex (pixel[2], ==, 0xff); /* red */
g_assert_cmphex (pixel[3], ==, 0xff); /* alpha */
}
}
hb_raster_image_destroy (img);
/* The changed extents are cleared, not deferred to another render. */
g_assert_false (hb_raster_paint_get_extents (paint, nullptr));
g_assert_null (hb_raster_paint_render (paint));
hb_raster_paint_set_extents (paint, &small);
hb_paint_color (funcs, paint, true, HB_COLOR (0, 0, 255, 255));
img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
hb_raster_image_get_extents (img, &out_ext);
g_assert_cmpuint (out_ext.width, ==, small.width);
g_assert_cmpuint (out_ext.height, ==, small.height);
hb_raster_image_destroy (img);
hb_raster_paint_destroy (paint);
}
static void
test_clip_rectangle_transformed (void)
{
const struct {
float xx, yx, xy, yy, dx, dy;
unsigned center_x;
} transforms[] = {
{1.f, 0.f, 0.2f, 1.f, 0.f, 0.f, 38},
{1.f, 0.f, 0.2f, -1.f, 0.f, 64.f, 38},
{0.f, 1.f, -1.f, 0.f, 64.f, 0.f, 32},
};
/* Cover both winding orders and intersecting with a rectangle or mask. */
for (const auto &t : transforms)
for (unsigned depth = 1; depth <= 2; depth++)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
g_assert_nonnull (paint);
hb_raster_extents_t ext = {0, 0, 64, 64, 0};
hb_raster_paint_set_extents (paint, &ext);
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_paint_push_transform (funcs, paint, t.xx, t.yx, t.xy, t.yy, t.dx, t.dy);
for (unsigned i = 0; i < depth; i++)
hb_paint_push_clip_rectangle (funcs, paint, 16.f, 16.f, 48.f, 48.f);
hb_paint_color (funcs, paint, true, HB_COLOR (0, 0, 0, 255));
for (unsigned i = 0; i < depth; i++)
hb_paint_pop_clip (funcs, paint);
hb_paint_pop_transform (funcs, paint);
hb_raster_image_t *image = hb_raster_paint_render (paint);
g_assert_nonnull (image);
hb_raster_extents_t out_ext;
hb_raster_image_get_extents (image, &out_ext);
const uint8_t *buffer = hb_raster_image_get_buffer (image);
/* The transformed center is opaque; the corner is outside the clip. */
g_assert_cmpuint (buffer[32 * out_ext.stride + t.center_x * 4 + 3], ==, 255);
g_assert_cmpuint (buffer[3], ==, 0);
hb_raster_image_destroy (image);
hb_raster_paint_destroy (paint);
}
}
/* ── Test: color lines of no length, and ill-formed gradients ─────── */
/* Paints the glyph of @unicode at 64 pixels to the em, which it fills,
* and returns the 64 by 64 picture. */
static hb_raster_image_t *
paint_em_square (hb_font_t *font, hb_codepoint_t unicode)
{
hb_codepoint_t glyph;
g_assert_true (hb_font_get_nominal_glyph (font, unicode, &glyph));
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
g_assert_nonnull (paint);
hb_raster_extents_t ext = {0, 0, 64, 64, 0};
hb_raster_paint_set_extents (paint, &ext);
hb_raster_paint_glyph (paint, font, glyph);
hb_raster_image_t *img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
hb_raster_paint_destroy (paint);
return img;
}
/* Whether the pixel of @img at (@x, @y) is red, blue, or not painted:
* 'r', 'b' or '-', and '?' for anything else. */
static char
em_square_pixel (hb_raster_image_t *img, unsigned x, unsigned y)
{
hb_raster_extents_t ext;
hb_raster_image_get_extents (img, &ext);
const uint8_t *p = hb_raster_image_get_buffer (img) + (size_t) y * ext.stride + x * 4;
/* Blue, green, red, alpha; the font's blue has a little green. */
if (p[3] == 0) return '-';
if (p[3] != 255) return '?';
if (p[2] == 255 && p[1] == 0 && p[0] == 0) return 'r';
if (p[2] == 0 && p[1] < 80 && p[0] == 255) return 'b';
return '?';
}
/* What is painted along the row across the middle of @img, at 4, 21,
* 23, 32, 39 and 42 of its 64 pixels, and in a corner. */
static void
em_square_pixels (hb_raster_image_t *img, char pixels[8])
{
static const unsigned at[7][2] = {{4, 32}, {21, 32}, {23, 32}, {32, 32},
{39, 32}, {42, 32}, {60, 4}};
for (unsigned i = 0; i < 7; i++)
pixels[i] = em_square_pixel (img, at[i][0], at[i][1]);
pixels[7] = 0;
}
/* A color line whose stops all share one offset has no length. If it
* pads it paints the first stop's color before that offset and the
* last stop's from it on, which is one color if it has one stop; if
* it repeats or reflects, nothing. A gradient along no line, or of
* two circles that are one, is ill-formed and paints nothing; so is a
* linear gradient whose P1 or P2 is at its P0. */
static void
test_paint_color_line_no_length (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/colr-color-line-no-length.ttf");
hb_font_t *font = hb_font_create (face);
hb_font_set_scale (font, 64, 64);
const struct {
char unicode;
const char *pixels;
} tests[] = {
{'a', "rrrrrrr"}, /* linear, one red stop */
{'b', "rrbbbbb"}, /* linear, red and blue at one offset: they meet at 22.4 */
{'c', "bbrrrbb"}, /* radial, the same: red within 9.6 of the center */
{'d', "-------"}, /* as b, repeating */
{'e', "-------"}, /* as c, reflecting */
{'f', "rrrrrrr"}, /* radial, one red stop */
{'g', "-------"}, /* linear, along no line: P1 at P0 */
{'h', "-------"}, /* radial, of one circle */
{'i', "rrbbbbb"}, /* as b, with a green stop between the two */
{'j', "-------"}, /* linear, P2 at P0 */
{'k', "r?????b"}, /* linear, from red at 12.8 to blue at 51.2 */
{'l', "-------"}, /* the same, P2 at P0 */
};
for (const auto &t : tests)
{
hb_raster_image_t *img = paint_em_square (font, t.unicode);
char pixels[8];
em_square_pixels (img, pixels);
g_assert_cmpstr (pixels, ==, t.pixels);
hb_raster_image_destroy (img);
}
/* The anchors are where the variation deltas put them: at the end
* of the font's axis, P2 of k is at its P0, and that of l is not. */
hb_variation_t wght = {HB_TAG ('w','g','h','t'), 1.f};
hb_font_set_variations (font, &wght, 1);
const struct {
char unicode;
const char *pixels;
} varied[] = {
{'k', "-------"},
{'l', "r?????b"},
};
for (const auto &t : varied)
{
hb_raster_image_t *img = paint_em_square (font, t.unicode);
char pixels[8];
em_square_pixels (img, pixels);
g_assert_cmpstr (pixels, ==, t.pixels);
hb_raster_image_destroy (img);
}
hb_font_destroy (font);
hb_face_destroy (face);
}
/* Raw BGRA input and SRC_OVER must use the same byte order, including
* on rows that are not word-aligned. */
static void
test_image_bgra (void)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
hb_raster_extents_t ext = {0, 0, 2, 2, 9};
hb_raster_paint_set_extents (paint, &ext);
hb_raster_paint_set_background (paint, HB_COLOR (0, 0, 0, 255));
const char pixels[] = {16, 32, 48, (char) 128, 16, 32, 48, (char) 128,
16, 32, 48, (char) 128, 16, 32, 48, (char) 128};
hb_blob_t *blob = hb_blob_create (pixels, sizeof (pixels),
HB_MEMORY_MODE_READONLY, nullptr, nullptr);
hb_glyph_extents_t gext = {0, 2, 2, -2};
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_paint_push_group (funcs, paint);
hb_paint_image (funcs, paint, blob, 2, 2,
HB_PAINT_IMAGE_FORMAT_BGRA, 0.f, &gext);
hb_paint_pop_group (funcs, paint, HB_PAINT_COMPOSITE_MODE_SRC_OVER);
hb_raster_image_t *img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
hb_raster_image_get_extents (img, &ext);
const uint8_t expected[] = {16, 32, 48, 255};
for (unsigned y = 0; y < ext.height; y++)
for (unsigned x = 0; x < ext.width; x++)
g_assert_cmpmem (hb_raster_image_get_buffer (img) + y * ext.stride + x * 4,
4, expected, 4);
hb_raster_image_destroy (img);
hb_blob_destroy (blob);
hb_raster_paint_destroy (paint);
}
/* An image is painted over all of its extents, each of its pixels over
* its own share of them: the left pixels of this one, red, over the left
* half and the right ones, blue, over the right half, one color going
* over into the other between their centers. */
static void
test_image_extents (void)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
hb_raster_extents_t ext = {0, 0, 8, 8, 0};
hb_raster_paint_set_extents (paint, &ext);
const uint8_t red[] = {0, 0, 255, 255}, blue[] = {255, 0, 0, 255};
uint8_t pixels[2 * 2 * 4];
for (unsigned i = 0; i < 4; i++)
memcpy (pixels + i * 4, i % 2 ? blue : red, 4);
hb_blob_t *blob = hb_blob_create ((const char *) pixels, sizeof (pixels),
HB_MEMORY_MODE_READONLY, nullptr, nullptr);
hb_glyph_extents_t gext = {0, 8, 8, -8};
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_paint_image (funcs, paint, blob, 2, 2,
HB_PAINT_IMAGE_FORMAT_BGRA, 0.f, &gext);
hb_raster_image_t *img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
hb_raster_image_get_extents (img, &ext);
g_assert_cmpuint (ext.width, ==, 8);
g_assert_cmpuint (ext.height, ==, 8);
for (unsigned y = 0; y < ext.height; y++)
{
const uint8_t *row = hb_raster_image_get_buffer (img) + y * ext.stride;
for (unsigned x = 0; x < ext.width; x++)
g_assert_cmpuint (row[x * 4 + 3], ==, 255);
/* The centers of the two pixels are at 2 and at 6. */
g_assert_cmpmem (row + 0 * 4, 4, red, 4);
g_assert_cmpmem (row + 1 * 4, 4, red, 4);
g_assert_cmpuint (row[3 * 4 + 2], >, row[4 * 4 + 2]);
g_assert_cmpuint (row[3 * 4 + 0], <, row[4 * 4 + 0]);
g_assert_cmpmem (row + 6 * 4, 4, blue, 4);
g_assert_cmpmem (row + 7 * 4, 4, blue, 4);
}
hb_raster_image_destroy (img);
hb_blob_destroy (blob);
hb_raster_paint_destroy (paint);
}
/* The rows of raw image data go down from where the extents of the
* image start, as those of a FreeType bitmap do: this one is red at the
* top and blue at the bottom. */
static void
test_image_bgra_rows (void)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
hb_raster_extents_t ext = {0, 0, 8, 8, 0};
hb_raster_paint_set_extents (paint, &ext);
const uint8_t red[] = {0, 0, 255, 255}, blue[] = {255, 0, 0, 255};
uint8_t pixels[2 * 2 * 4];
for (unsigned i = 0; i < 4; i++)
memcpy (pixels + i * 4, i < 2 ? red : blue, 4);
hb_blob_t *blob = hb_blob_create ((const char *) pixels, sizeof (pixels),
HB_MEMORY_MODE_READONLY, nullptr, nullptr);
hb_glyph_extents_t gext = {0, 8, 8, -8};
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_paint_image (funcs, paint, blob, 2, 2,
HB_PAINT_IMAGE_FORMAT_BGRA, 0.f, &gext);
hb_raster_image_t *img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
hb_raster_image_get_extents (img, &ext);
g_assert_cmpuint (ext.height, ==, 8);
/* The rows of an hb_raster_image_t go up. */
const uint8_t *buffer = hb_raster_image_get_buffer (img);
for (unsigned x = 0; x < ext.width; x++)
{
g_assert_cmpmem (buffer + 7 * ext.stride + x * 4, 4, red, 4);
g_assert_cmpmem (buffer + 6 * ext.stride + x * 4, 4, red, 4);
g_assert_cmpmem (buffer + 1 * ext.stride + x * 4, 4, blue, 4);
g_assert_cmpmem (buffer + 0 * ext.stride + x * 4, 4, blue, 4);
}
hb_raster_image_destroy (img);
hb_blob_destroy (blob);
hb_raster_paint_destroy (paint);
}
/* An image that is shrunk is averaged, not read at one place for each
* pixel painted: this one has every fourth column red and the rest blue,
* and each pixel painted covers eight columns of it. */
static void
test_image_shrunk (void)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
hb_raster_extents_t ext = {0, 0, 2, 2, 0};
hb_raster_paint_set_extents (paint, &ext);
const uint8_t red[] = {0, 0, 255, 255}, blue[] = {255, 0, 0, 255};
uint8_t pixels[16 * 16 * 4];
for (unsigned i = 0; i < 16 * 16; i++)
memcpy (pixels + i * 4, i % 4 ? blue : red, 4);
hb_blob_t *blob = hb_blob_create ((const char *) pixels, sizeof (pixels),
HB_MEMORY_MODE_READONLY, nullptr, nullptr);
hb_glyph_extents_t gext = {0, 2, 2, -2};
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_paint_image (funcs, paint, blob, 16, 16,
HB_PAINT_IMAGE_FORMAT_BGRA, 0.f, &gext);
hb_raster_image_t *img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
hb_raster_image_get_extents (img, &ext);
g_assert_cmpuint (ext.width, ==, 2);
g_assert_cmpuint (ext.height, ==, 2);
for (unsigned y = 0; y < ext.height; y++)
for (unsigned x = 0; x < ext.width; x++)
{
/* A quarter red and three quarters blue. Read at its center
* alone, where a red column meets a blue one, it would be half
* of each. */
const uint8_t *pixel = hb_raster_image_get_buffer (img) + y * ext.stride + x * 4;
g_assert_cmpuint (pixel[0], >=, 189);
g_assert_cmpuint (pixel[0], <=, 193);
g_assert_cmpuint (pixel[1], ==, 0);
g_assert_cmpuint (pixel[2], >=, 62);
g_assert_cmpuint (pixel[2], <=, 66);
g_assert_cmpuint (pixel[3], ==, 255);
}
hb_raster_image_destroy (img);
hb_blob_destroy (blob);
hb_raster_paint_destroy (paint);
}
/* Paints four images of one translucent color, each of @texels by
* @texels pixels over @size by @size units, two by two from the origin,
* moved by @shift to the right and down. Each is to paint the places
* on its left and top edges and none on its right and bottom ones. */
static void
paint_images_side_by_side (unsigned texels, int size, float shift)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
hb_raster_extents_t ext = {0, 0, 2 * (unsigned) size + 2, 2 * (unsigned) size + 2, 0};
hb_raster_paint_set_extents (paint, &ext);
const uint8_t color[] = {16, 32, 48, 128}, clear[] = {0, 0, 0, 0};
uint8_t pixels[16 * 16 * 4];
g_assert_cmpuint (texels, <=, 16);
for (unsigned i = 0; i < texels * texels; i++)
memcpy (pixels + i * 4, color, 4);
hb_blob_t *blob = hb_blob_create ((const char *) pixels, texels * texels * 4,
HB_MEMORY_MODE_READONLY, nullptr, nullptr);
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_paint_push_transform (funcs, paint, 1.f, 0.f, 0.f, 1.f, shift, -shift);
for (int y = 0; y < 2; y++)
for (int x = 0; x < 2; x++)
{
hb_glyph_extents_t gext = {x * size, (y + 1) * size, size, -size};
hb_paint_image (funcs, paint, blob, texels, texels,
HB_PAINT_IMAGE_FORMAT_BGRA, 0.f, &gext);
}
hb_paint_pop_transform (funcs, paint);
hb_raster_image_t *img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
hb_raster_image_get_extents (img, &ext);
for (unsigned y = 0; y < ext.height; y++)
for (unsigned x = 0; x < ext.width; x++)
g_assert_cmpmem (hb_raster_image_get_buffer (img) + y * ext.stride + x * 4, 4,
x < 2 * (unsigned) size && y < 2 * (unsigned) size ? color : clear, 4);
hb_raster_image_destroy (img);
hb_blob_destroy (blob);
hb_raster_paint_destroy (paint);
}
/* Images side by side share the edge between them. What is read on
* that edge is painted by one of them: by both it would be painted
* twice, more opaque than the images are, and by neither there would
* be a seam. So an image is not painted on the two edges across from
* where its extents start, and the four here paint as many pixels as
* they cover. */
static void
test_image_edges (void)
{
/* Pixels are centered on the edges. */
paint_images_side_by_side (1, 1, .5f);
/* The same, with a scale that is not exact in floating point. */
paint_images_side_by_side (11, 47, .5f);
/* Shrunk, with places that are read from a pixel on the edges. */
paint_images_side_by_side (4, 1, .25f);
paint_images_side_by_side (7, 1, .125f);
}
static void
test_image_png_byte_order (void)
{
#ifndef HAVE_PNG
g_test_skip ("Built without PNG support.");
#else
/* A 1x1 RGBA PNG: red=96, green=64, blue=32, alpha=128. */
static const uint8_t data[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00,
0x0d, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x48, 0x70, 0x50, 0x68,
0x00, 0x00, 0x03, 0x05, 0x01, 0x41, 0x9e, 0xec, 0x86, 0x30, 0x00, 0x00,
0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
};
hb_blob_t *png = hb_blob_create ((const char *) data, sizeof (data),
HB_MEMORY_MODE_READONLY, nullptr, nullptr);
hb_raster_image_t *img = hb_raster_image_create_or_fail ();
g_assert_nonnull (img);
g_assert_true (hb_raster_image_deserialize_from_png_or_fail (img, png));
hb_blob_destroy (png);
const uint8_t expected[] = {16, 32, 48, 128};
g_assert_cmpmem (hb_raster_image_get_buffer (img), 4, expected, 4);
png = hb_raster_image_serialize_to_png_or_fail (img);
g_assert_nonnull (png);
g_assert_true (hb_raster_image_deserialize_from_png_or_fail (img, png));
g_assert_cmpmem (hb_raster_image_get_buffer (img), 4, expected, 4);
hb_blob_destroy (png);
hb_raster_image_destroy (img);
#endif
}
/* Paint the middle of an opaque PNG image of the given width onto a 1x1
* surface; returns the resulting alpha, or -1 if built without PNG. */
static int
paint_png_image (unsigned width)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
g_assert_nonnull (paint);
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_raster_extents_t ext = {0, 0, width, 4, 0};
hb_raster_paint_set_extents (paint, &ext);
hb_paint_color (funcs, paint, false, HB_COLOR (0, 0, 0, 255));
hb_raster_image_t *img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
hb_blob_t *png = hb_raster_image_serialize_to_png_or_fail (img);
hb_raster_image_destroy (img);
if (!png)
{
hb_raster_paint_destroy (paint);
return -1;
}
ext.width = ext.height = 1;
hb_raster_paint_set_extents (paint, &ext);
hb_glyph_extents_t gext = {-2, 2, 4, -4};
hb_paint_image (funcs, paint, png, width, 4,
HB_PAINT_IMAGE_FORMAT_PNG, 0.f, &gext);
img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
int alpha = hb_raster_image_get_buffer (img)[3];
hb_raster_image_destroy (img);
hb_blob_destroy (png);
hb_raster_paint_destroy (paint);
return alpha;
}
/* The size of an embedded PNG is font data and the whole image is decoded
* whatever the size of the surface; one past the cap for surfaces sized
* from font data is not decoded at all. */
static void
test_image_png_size (void)
{
int alpha = paint_png_image (4096);
if (alpha == -1)
{
g_test_skip ("Built without PNG support.");
return;
}
g_assert_cmpint (alpha, ==, 255);
g_assert_cmpint (paint_png_image (4097), ==, 0);
}
/* The sweep costs edges times the rows they span, within the extents that
* are rendered: the fixed ones, or else those of the outline itself. The
* latter holds for the masks of a paint session too, whatever its surface. */
static void
test_budget_sweep (void)
{
hb_raster_extents_t extents = {0, 0, 1, 1, 0};
/* 20,000 edges of 4,096 rows each: over budget, so rendering fails. */
hb_raster_draw_t *rdr = hb_raster_draw_create_or_fail ();
for (unsigned i = 0; i < 10000; i++)
draw_rect (rdr, 0.f, 0.f, 1.f, 4096.f);
hb_raster_image_t *img = hb_raster_draw_render (rdr);
g_assert_null (img);
/* The same edges, clipped to one row. */
hb_raster_draw_set_extents (rdr, &extents);
for (unsigned i = 0; i < 10000; i++)
draw_rect (rdr, 0.f, 0.f, 1.f, 4096.f);
img = hb_raster_draw_render (rdr);
g_assert_nonnull (img);
g_assert_cmpint (pixel_at (img, 0, 0), ==, 255);
hb_raster_image_destroy (img);
hb_raster_draw_destroy (rdr);
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint);
hb_raster_paint_set_extents (paint, &extents);
void *draw_data;
hb_paint_push_clip_path_start (funcs, paint, &draw_data);
g_assert_nonnull (draw_data);
for (unsigned i = 0; i < 10000; i++)
draw_rect ((hb_raster_draw_t *) draw_data, 0.f, 0.f, 1.f, 4096.f);
hb_paint_push_clip_path_end (funcs, paint);
hb_paint_color (funcs, paint, false, HB_COLOR (0, 0, 0, 255));
hb_paint_pop_clip (funcs, paint);
img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
/* Paint masks crop the same edges to the visible one-pixel surface. */
g_assert_cmpuint (hb_raster_image_get_buffer (img)[3], ==, 255);
hb_raster_image_destroy (img);
hb_raster_paint_destroy (paint);
}
/* ── Test: a glyph of many clipped layers renders in full ──────────── */
static hb_raster_image_t *
paint_with_budget (hb_font_t *font, hb_codepoint_t glyph, int64_t budget)
{
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
g_assert_nonnull (paint);
g_assert_true (hb_paint_set_budget (hb_raster_paint_get_funcs (paint), paint, budget));
hb_raster_paint_glyph (paint, font, glyph);
hb_raster_image_t *img = hb_raster_paint_render (paint);
hb_raster_paint_destroy (paint);
return img;
}
/* Bitcount Ink paints a glyph from hundreds of layers, each clipped to a
* small element of it. A layer costs what it can show, not a pass over
* the surface, so even where the surface is large the glyph renders in
* full within the default budget: just as it does with none. */
static void
test_paint_clipped_layers (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/BitcountInk.H.ttf");
hb_font_t *font = hb_font_create (face);
hb_font_set_scale (font, 1024, 1024);
hb_codepoint_t glyph;
g_assert_true (hb_font_get_nominal_glyph (font, 'H', &glyph));
hb_variation_t variations[] = {{HB_TAG ('E','L','X','P'), 0.f},
{HB_TAG ('E','L','X','P'), 100.f}};
for (unsigned i = 0; i < G_N_ELEMENTS (variations); i++)
{
hb_font_set_variations (font, &variations[i], 1);
hb_raster_image_t *img = paint_with_budget (font, glyph, HB_BUDGET_DEFAULT);
hb_raster_image_t *full = paint_with_budget (font, glyph, HB_BUDGET_UNLIMITED);
g_assert_nonnull (img);
g_assert_nonnull (full);
hb_raster_extents_t ext, full_ext;
hb_raster_image_get_extents (img, &ext);
hb_raster_image_get_extents (full, &full_ext);
g_assert_cmpuint (ext.width, >, 400);
g_assert_cmpuint (ext.height, >, 400);
g_assert_cmpuint (ext.stride * ext.height, ==, full_ext.stride * full_ext.height);
g_assert_true (!memcmp (hb_raster_image_get_buffer (img),
hb_raster_image_get_buffer (full),
ext.stride * ext.height));
hb_raster_image_destroy (img);
hb_raster_image_destroy (full);
}
hb_font_destroy (font);
hb_face_destroy (face);
}
/* ── Test: a sweep whose color line has no length ──────────────────── */
/* Count the opaque blue and opaque red pixels of a painted glyph. */
static void
count_blue_red (hb_font_t *font, hb_codepoint_t unicode,
unsigned *blue, unsigned *red)
{
hb_codepoint_t glyph;
g_assert_true (hb_font_get_nominal_glyph (font, unicode, &glyph));
hb_raster_image_t *img = paint_with_budget (font, glyph, HB_BUDGET_DEFAULT);
*blue = *red = 0;
if (!img) return;
hb_raster_extents_t ext;
hb_raster_image_get_extents (img, &ext);
const uint8_t *buf = hb_raster_image_get_buffer (img);
for (unsigned y = 0; y < ext.height; y++)
for (unsigned x = 0; x < ext.width; x++)
{
uint32_t px;
memcpy (&px, buf + y * ext.stride + x * 4, 4);
if (px == 0xFF0000FFu) (*blue)++;
if (px == 0xFFFF0000u) (*red)++;
}
hb_raster_image_destroy (img);
}
/* With equal start and end angles, or all stops at one offset, a sweep's
* color line has no length. Padded, it still paints: the first color up
* to the angle and the last from it on. Repeated or reflected, it paints
* nothing. */
static void
test_paint_sweep_coincident (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/test_glyphs-glyf_colr_1.ttf");
hb_font_t *font = hb_font_create (face);
unsigned blue, red;
/* sweep_coincident_{angles,stops}_{forward,reverse}_blue_red_pad:
* a quarter of a circle in one color, the rest in the other. */
const hb_codepoint_t pad[] = {0xF1300, 0xF1306, 0xF130C, 0xF1312};
for (unsigned i = 0; i < G_N_ELEMENTS (pad); i++)
{
count_blue_red (font, pad[i], &blue, &red);
g_assert_cmpuint (MIN (blue, red), >, 10000);
g_assert_cmpuint (MAX (blue, red), >, 2 * MIN (blue, red));
}
/* ..._reflect and ..._repeat */
const hb_codepoint_t empty[] = {0xF1301, 0xF1302, 0xF130D, 0xF130E};
for (unsigned i = 0; i < G_N_ELEMENTS (empty); i++)
{
count_blue_red (font, empty[i], &blue, &red);
g_assert_cmpuint (blue, ==, 0);
g_assert_cmpuint (red, ==, 0);
}
hb_font_destroy (font);
hb_face_destroy (face);
}
/* ── main ────────────────────────────────────────────────────────── */
int
main (int argc, char **argv)
{
hb_test_init (&argc, &argv);
hb_test_add (test_rectangle);
hb_test_add (test_accumulate);
hb_test_add (test_subpixel_edge);
hb_test_add (test_many_coincident_edges);
hb_test_add (test_transform);
hb_test_add (test_set_glyph_extents_with_transform);
hb_test_add (test_image_nonfinite_transform);
hb_test_add (test_set_glyph_extents_overflow);
hb_test_add (test_budget);
hb_test_add (test_budget_nonfinite_curve);
hb_test_add (test_budget_curve_too_large);
hb_test_add (test_budget_colr_outline);
hb_test_add (test_background_premultiplied);
hb_test_add (test_background_stride);
hb_test_add (test_paint_reset);
hb_test_add (test_paint_recycle_null);
hb_test_add (test_set_extents_while_painting);
hb_test_add (test_clip_rectangle_transformed);
hb_test_add (test_paint_radial_gradient_tip);
hb_test_add (test_paint_radial_gradient_zero_radii);
hb_test_add (test_image_bgra);
hb_test_add (test_image_extents);
hb_test_add (test_image_bgra_rows);
hb_test_add (test_image_shrunk);
hb_test_add (test_image_edges);
hb_test_add (test_image_png_byte_order);
hb_test_add (test_image_png_size);
hb_test_add (test_paint_color_line_no_length);
hb_test_add (test_budget_sweep);
hb_test_add (test_paint_clipped_layers);
hb_test_add (test_paint_hsl_luminosity_weights);
hb_test_add (test_paint_sweep_coincident);
return hb_test_run ();
}