diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index fa3e1094e..93b221181 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -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 diff --git a/src/hb-draw.cc b/src/hb-draw.cc index 6e5b05771..1dc14ba90 100644 --- a/src/hb-draw.cc +++ b/src/hb-draw.cc @@ -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; diff --git a/src/hb-draw.h b/src/hb-draw.h index b830b6dd4..2fbcbd5ad 100644 --- a/src/hb-draw.h +++ b/src/hb-draw.h @@ -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, diff --git a/test/api/test-gpu.cc b/test/api/test-gpu.cc index 533d03c5e..20178ee4a 100644 --- a/test/api/test-gpu.cc +++ b/test/api/test-gpu.cc @@ -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,