[draw] hb_draw_line: add cap parameter (butt / square)

Add hb_draw_line_cap_t enum with HB_DRAW_LINE_CAP_BUTT (the
existing default behavior) and HB_DRAW_LINE_CAP_SQUARE, which
extends each endpoint along the line direction by half its
local stroke width.

Square caps let four hb_draw_line() calls form a closed
rectangle without gaps at the corners -- a meaningfully
cheaper alternative to hb_draw_rect() for thin outlined
rectangles, since each line's bbox is just stroke_width tall
instead of covering the full rectangle interior.

Round caps not implemented yet; can be added as a third enum
value when a use case shows up.

Assisted-by: Claude <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-04-15 02:35:10 -06:00
1 parent 28122c755d
commit fd2a6968ab
4 files changed
+64 -12

No files matched your search

+1
View File
@@ -239,6 +239,7 @@ hb_draw_line_to
hb_draw_quadratic_to
hb_draw_cubic_to
hb_draw_close_path
hb_draw_line_cap_t
hb_draw_line
hb_draw_rect
hb_draw_circle
+25 -9
View File
@@ -473,12 +473,18 @@ hb_draw_close_path (hb_draw_funcs_t *dfuncs, void *draw_data,
* @x1: end X coordinate
* @y1: end Y coordinate
* @w1: stroke width at the end
* @cap: end-cap shape (butt or square)
*
* Emits a tapered line segment as a filled trapezoid with butt
* caps. @w0 and @w1 are the full stroke widths at the start and
* end points respectively; they may differ for a tapered stroke
* or match for a uniform one. Pass `NaN` for @w1 to use @w0
* (uniform stroke) without repeating the value.
* Emits a tapered line segment as a filled trapezoid. @w0 and
* @w1 are the full stroke widths at the start and end points
* respectively; they may differ for a tapered stroke or match
* for a uniform one. Pass `NaN` for @w1 to use @w0 (uniform
* stroke) without repeating the value.
*
* With #HB_DRAW_LINE_CAP_SQUARE each endpoint is extended along
* the line direction by half its local stroke width, so four
* `hb_draw_line()` calls form a closed rectangle without gaps
* at the corners.
*
* XSince: REPLACEME
**/
@@ -486,18 +492,28 @@ void
hb_draw_line (hb_draw_funcs_t *dfuncs, void *draw_data,
hb_draw_state_t *st,
float x0, float y0, float w0,
float x1, float y1, float w1)
float x1, float y1, float w1,
hb_draw_line_cap_t cap)
{
if (std::isnan (w1)) w1 = w0;
float dx = x1 - x0, dy = y1 - y0;
float len = sqrtf (dx * dx + dy * dy);
if (len <= 0.f)
return;
/* Unit normal perpendicular to the line direction. */
float nx = -dy / len;
float ny = dx / len;
/* Unit tangent and normal to the line direction. */
float tx = dx / len;
float ty = dy / len;
float nx = -ty;
float ny = tx;
float h0 = 0.5f * w0;
float h1 = 0.5f * w1;
/* Square caps: extend each endpoint outward along the line
* tangent by half its local stroke width. */
if (cap == HB_DRAW_LINE_CAP_SQUARE)
{
x0 -= tx * h0; y0 -= ty * h0;
x1 += tx * h1; y1 += ty * h1;
}
/* Trapezoid corners (counter-clockwise). */
float ax = x0 + nx * h0, ay = y0 + ny * h0;
float bx = x1 + nx * h1, by = y1 + ny * h1;
+20 -1
View File
@@ -347,11 +347,30 @@ hb_draw_close_path (hb_draw_funcs_t *dfuncs, void *draw_data,
* width of the outline; NaN means "filled" (no stroke).
*/
/**
* hb_draw_line_cap_t:
* @HB_DRAW_LINE_CAP_BUTT: No cap; the line ends exactly at
* its endpoint.
* @HB_DRAW_LINE_CAP_SQUARE: Square cap; the line is extended
* past its endpoint by half the local stroke width. Useful
* for composing closed shapes from line segments (e.g. a
* rectangle made from four lines).
*
* End-cap shape for hb_draw_line().
*
* XSince: REPLACEME
**/
typedef enum {
HB_DRAW_LINE_CAP_BUTT = 0,
HB_DRAW_LINE_CAP_SQUARE = 1,
} hb_draw_line_cap_t;
HB_EXTERN void
hb_draw_line (hb_draw_funcs_t *dfuncs, void *draw_data,
hb_draw_state_t *st,
float x0, float y0, float w0,
float x1, float y1, float w1);
float x1, float y1, float w1,
hb_draw_line_cap_t cap);
HB_EXTERN void
hb_draw_rect (hb_draw_funcs_t *dfuncs, void *draw_data,
+18 -2
View File
@@ -532,7 +532,8 @@ test_shapes (void)
/* Tapered line. */
hb_draw_line (dfuncs, draw, &st,
10.f, 10.f, 4.f,
50.f, 30.f, 2.f);
50.f, 30.f, 2.f,
HB_DRAW_LINE_CAP_BUTT);
hb_glyph_extents_t ext;
hb_blob_t *blob = hb_gpu_draw_encode (draw, &ext);
g_assert_nonnull (blob);
@@ -543,11 +544,26 @@ test_shapes (void)
/* Degenerate line (zero length) is silently a no-op. */
hb_draw_line (dfuncs, draw, &st,
10.f, 10.f, 3.f,
10.f, 10.f, 3.f);
10.f, 10.f, 3.f,
HB_DRAW_LINE_CAP_BUTT);
blob = hb_gpu_draw_encode (draw, &ext);
g_assert_cmpuint (hb_blob_get_length (blob), ==, 0);
hb_blob_destroy (blob);
/* NaN second width defaults to first; square cap extends each
* end by half the stroke width along the line direction.
* Horizontal line of length 40, stroke width 4: with butt
* caps the bbox width would be 40, with square caps it's
* 40 + 4 = 44. */
hb_draw_line (dfuncs, draw, &st,
10.f, 10.f, 4.f,
50.f, 10.f, (float) std::nan (""),
HB_DRAW_LINE_CAP_SQUARE);
blob = hb_gpu_draw_encode (draw, &ext);
g_assert_nonnull (blob);
g_assert_cmpint (ext.width, ==, 44);
hb_blob_destroy (blob);
/* Filled rect (stroke_width = NaN). */
hb_draw_rect (dfuncs, draw, &st,
0.f, 0.f, 20.f, 10.f,