diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 681229ca5..93211ecd1 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -1068,6 +1068,10 @@ hb_cairo_font_face_get_scale_factor (cairo_font_face_t *font_face) * If the value @clusters points at has changed after the call, the caller * will free the allocated cluster array using cairo_text_cluster_free(). * + * Cairo can only represent monotone clusters. If the cluster values in + * @buffer are not monotone in the buffer's direction, a single cluster + * mapping all of @utf8 to all of the glyphs is returned. + * * See hb_cairo_font_face_set_scale_factor() for the details of * the @scale_factor argument. * @@ -1102,13 +1106,26 @@ hb_cairo_glyphs_from_buffer (hb_buffer_t *buffer, if (orig_num_glyphs < *num_glyphs + 1) *glyphs = cairo_glyph_allocate (*num_glyphs + 1); + hb_bool_t backward = HB_DIRECTION_IS_BACKWARD (hb_buffer_get_direction (buffer)); + bool monotone = true; if (clusters && utf8) { unsigned orig_num_clusters = *num_clusters; *num_clusters = *num_glyphs ? 1 : 0; for (unsigned int i = 1; i < *num_glyphs; i++) if (hb_glyph[i].cluster != hb_glyph[i-1].cluster) + { + if (unlikely ((hb_glyph[i].cluster < hb_glyph[i-1].cluster) != (bool) backward)) + { + /* Cairo can only represent monotone clusters. Non-monotone + * cluster levels, shaper bugs, or shaping that failed midway + * can all get us here. Map all of the text to all of the glyphs. */ + monotone = false; + *num_clusters = 1; + break; + } (*num_clusters)++; + } if (orig_num_clusters < *num_clusters) *clusters = cairo_text_cluster_allocate (*num_clusters); } @@ -1134,12 +1151,16 @@ hb_cairo_glyphs_from_buffer (hb_buffer_t *buffer, if (clusters && *num_clusters && utf8) { hb_memset ((void *) *clusters, 0, *num_clusters * sizeof ((*clusters)[0])); - hb_bool_t backward = HB_DIRECTION_IS_BACKWARD (hb_buffer_get_direction (buffer)); *cluster_flags = backward ? CAIRO_TEXT_CLUSTER_FLAG_BACKWARD : (cairo_text_cluster_flags_t) 0; unsigned int cluster = 0; const char *start = utf8, *end; (*clusters)[cluster].num_glyphs++; - if (backward) + if (unlikely (!monotone)) + { + (*clusters)[cluster].num_glyphs = *num_glyphs; + (*clusters)[cluster].num_bytes = utf8_len; + } + else if (backward) { for (i = *num_glyphs - 2; i >= 0; i--) { diff --git a/test/api/test-cairo.c b/test/api/test-cairo.c index 8074531d2..1c3f46e06 100644 --- a/test/api/test-cairo.c +++ b/test/api/test-cairo.c @@ -151,6 +151,68 @@ test_hb_cairo_face_lifecycle (void) g_assert_true (destroyed); } +static void +test_hb_cairo_glyphs_from_buffer_clusters (void) +{ + /* Non-monotone clusters come out of hb_shape() for some inputs + * (HB_BUFFER_CLUSTER_LEVEL_CHARACTERS, shaping that fails midway, + * shaper bugs). Cairo can only represent monotone clusters; make + * sure such buffers map to one cluster instead of aborting. */ + static const struct { + hb_direction_t direction; + unsigned clusters[4]; + unsigned num_clusters; + } tests[] = { + {HB_DIRECTION_LTR, {0, 1, 1, 3}, 3}, + {HB_DIRECTION_RTL, {3, 1, 1, 0}, 3}, + /* Not monotone. */ + {HB_DIRECTION_LTR, {0, 2, 0, 0}, 1}, + {HB_DIRECTION_LTR, {3, 1, 1, 0}, 1}, + {HB_DIRECTION_RTL, {0, 0, 2, 0}, 1}, + {HB_DIRECTION_RTL, {0, 1, 1, 3}, 1}, + }; + const char *utf8 = "abcd"; + + for (unsigned t = 0; t < G_N_ELEMENTS (tests); t++) + for (unsigned utf8_clusters = 0; utf8_clusters < 2; utf8_clusters++) + { + hb_buffer_t *buffer = hb_buffer_create (); + hb_buffer_set_direction (buffer, tests[t].direction); + for (unsigned i = 0; i < 4; i++) + hb_buffer_add (buffer, i, tests[t].clusters[i]); + + cairo_glyph_t *glyphs = NULL; + unsigned num_glyphs = 0; + cairo_text_cluster_t *clusters = NULL; + unsigned num_clusters = 0; + cairo_text_cluster_flags_t cluster_flags; + hb_cairo_glyphs_from_buffer (buffer, utf8_clusters, + 1., 1., 0., 0., + utf8, -1, + &glyphs, &num_glyphs, + &clusters, &num_clusters, + &cluster_flags); + + g_assert_cmpuint (num_glyphs, ==, 4); + g_assert_cmpuint (num_clusters, ==, tests[t].num_clusters); + + int total_bytes = 0, total_glyphs = 0; + for (unsigned i = 0; i < num_clusters; i++) + { + g_assert_cmpint (clusters[i].num_bytes, >=, 0); + g_assert_cmpint (clusters[i].num_glyphs, >, 0); + total_bytes += clusters[i].num_bytes; + total_glyphs += clusters[i].num_glyphs; + } + g_assert_cmpint (total_bytes, ==, 4); + g_assert_cmpint (total_glyphs, ==, 4); + + cairo_glyph_free (glyphs); + cairo_text_cluster_free (clusters); + hb_buffer_destroy (buffer); + } +} + int main (int argc, char **argv) { @@ -158,6 +220,7 @@ main (int argc, char **argv) hb_test_add (test_hb_cairo_budget); hb_test_add (test_hb_cairo_color_glyph_cycle); hb_test_add (test_hb_cairo_face_lifecycle); + hb_test_add (test_hb_cairo_glyphs_from_buffer_clusters); return hb_test_run (); }