From ee4822f9696d2a40351a26d73257667a77af78ca Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 28 Feb 2023 09:39:32 -0700 Subject: [PATCH 1/9] [algs] Add solve_itp method Port from kurbo. --- src/hb-algs.hh | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index 8c7ad434d..1bed5e499 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -1344,4 +1344,57 @@ struct HB_FUNCOBJ (hb_dec); +/* For documentation and implementation see: + * + * [ITP method]: https://en.wikipedia.org/wiki/ITP_Method + * [An Enhancement of the Bisection Method Average Performance Preserving Minmax Optimality]: https://dl.acm.org/doi/10.1145/3423597 + * https://docs.rs/kurbo/0.8.1/kurbo/common/fn.solve_itp.html + * https://github.com/linebender/kurbo/blob/fd839c25ea0c98576c7ce5789305822675a89938/src/common.rs#L162-L248 + */ +template +double solve_itp (func_t f, + double a, double b, + double epsilon, + unsigned n0, + double k1, + double &ya, double yb) +{ + unsigned n1_2 = (unsigned) (hb_max (ceil (log2 ((b - a) / epsilon)) - 1.0, 0.0)); + unsigned nmax = n0 + n1_2; + double scaled_epsilon = epsilon * double (1llu << nmax); + double _2_epsilon = 2.0 * epsilon; + while (b - a > _2_epsilon) + { + double x1_2 = 0.5 * (a + b); + double r = scaled_epsilon - 0.5 * (b - a); + double xf = (yb * a - ya * b) / (yb - ya); + double sigma = x1_2 - xf; + // This has k2 = 2 hardwired for efficiency. + double b_a = b - a; + double b_a_k2 = b_a * b_a; + double delta = k1 * b_a_k2; + int sigma_sign = sigma >= 0 ? +1 : -1; + double xt = delta <= fabs (x1_2 - xf) ? xf + delta * sigma_sign : x1_2; + double xitp = fabs (xt - x1_2) <= r ? xt : x1_2 - r * sigma_sign; + double yitp = f (xitp); + if (yitp > 0.0) + { + b = xitp; + yb = yitp; + } + else if (yitp < 0.0) + { + a = xitp; + ya = yitp; + } + else + { + return xitp; + } + scaled_epsilon *= 0.5; + } + return 0.5 * (a + b); +} + + #endif /* HB_ALGS_HH */ From 6e483c4061b526c6c22db198194d4f8b2cfb3a86 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 28 Feb 2023 12:25:32 -0700 Subject: [PATCH 2/9] [shape] Add hb_shape_justify() and hb-view --width --- src/hb-algs.hh | 2 +- src/hb-shape.cc | 173 ++++++++++++++++++++++++++++++++++++++++++ src/hb-shape.h | 11 +++ util/shape-options.hh | 30 ++++++-- 4 files changed, 210 insertions(+), 6 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index 1bed5e499..05ca3ec9d 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -1357,7 +1357,7 @@ double solve_itp (func_t f, double epsilon, unsigned n0, double k1, - double &ya, double yb) + double &ya, double &yb) { unsigned n1_2 = (unsigned) (hb_max (ceil (log2 ((b - a) / epsilon)) - 1.0, 0.0)); unsigned nmax = n0 + n1_2; diff --git a/src/hb-shape.cc b/src/hb-shape.cc index 7b5bf2c5e..e580298d1 100644 --- a/src/hb-shape.cc +++ b/src/hb-shape.cc @@ -196,4 +196,177 @@ hb_shape (hb_font_t *font, } +static float +buffer_width (hb_buffer_t *buffer) +{ + float w = 0; + auto *pos = buffer->pos; + unsigned count = buffer->len; + if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction)) + for (unsigned i = 0; i < count; i++) + w += pos[i].x_advance; + else + for (unsigned i = 0; i < count; i++) + w += pos[i].y_advance; + return w; +} + +static void +reset_buffer (hb_buffer_t *buffer, + hb_array_t text) +{ + assert (buffer->ensure (text.length)); + buffer->have_positions = false; + buffer->len = text.length; + memcpy (buffer->info, text.arrayZ, text.length * sizeof (buffer->info[0])); + hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE); +} + +hb_bool_t +hb_shape_justify (hb_font_t *font, + hb_buffer_t *buffer, + const hb_feature_t *features, + unsigned int num_features, + const char * const *shaper_list, + float target_width, + float *width, /* IN/OUT */ + hb_tag_t *var_tag, /* OUT */ + float *var_value /* OUT */) +{ + // TODO Negative font scales? + + if (target_width == *width) + return hb_shape_full (font, buffer, + features, num_features, + shaper_list); + + hb_face_t *face = font->face; + + hb_tag_t tag = HB_TAG_NONE; + hb_ot_var_axis_info_t axis_info; + + hb_tag_t tags[] = + { + HB_TAG ('j','s','t','f'), + HB_TAG ('w','d','t','h'), + }; + for (unsigned i = 0; i < ARRAY_LENGTH (tags); i++) + if (hb_ot_var_find_axis_info (face, tags[i], &axis_info)) + { + tag = *var_tag = tags[i]; + break; + } + + if (!tag) + { + if (hb_shape_full (font, buffer, + features, num_features, + shaper_list)) + { + *width = buffer_width (buffer); + return true; + } + else + return false; + } + + unsigned text_len = buffer->len; + auto *text_info = (hb_glyph_info_t *) hb_malloc (text_len * sizeof (buffer->info[0])); + if (unlikely (text_len && !text_info)) + return false; + hb_memcpy (text_info, buffer->info, text_len * sizeof (buffer->info[0])); + auto text = hb_array (text_info, text_len); + + if (!*width) + { + hb_font_set_variation (font, tag, axis_info.default_value); + if (!hb_shape_full (font, buffer, + features, num_features, + shaper_list)) + return false; + *width = buffer_width (buffer); + } + + if (target_width == *width) + return true; + + double a, b, ya, yb; + if (*width < target_width) + { + ya = *width - target_width; + a = axis_info.default_value; + b = axis_info.max_value; + + hb_font_set_variation (font, tag, (float) b); + reset_buffer (buffer, text); + if (!hb_shape_full (font, buffer, + features, num_features, + shaper_list)) + return false; + yb = buffer_width (buffer) - target_width; + if (yb <= 0) + { + *width = (float) yb + target_width; + return true; + } + } + else + { + yb = *width - target_width; + a = axis_info.min_value; + b = axis_info.default_value; + + hb_font_set_variation (font, tag, (float) a); + reset_buffer (buffer, text); + if (!hb_shape_full (font, buffer, + features, num_features, + shaper_list)) + return false; + ya = buffer_width (buffer) - target_width; + if (ya >= 0) + { + *width = (float) ya + target_width; + return true; + } + } + + double epsilon = (b - a) / (1<<14); + const unsigned n0 = 1; + const double k1 = 0.2 / (b - a); + bool failed = false; + + auto f = [&] (double x) + { + hb_font_set_variation (font, tag, (float) x); + reset_buffer (buffer, text); + if (unlikely (!hb_shape_full (font, buffer, + features, num_features, + shaper_list))) + { + failed = true; + return target_width; + } + + return buffer_width (buffer) - target_width; + }; + + double itp = solve_itp (f, + a, b, + epsilon, + n0, + k1, + ya, yb); + + hb_free (text_info); + + if (failed) + return false; + + *width = (float) (ya + yb) * 0.5f + target_width; + *var_value = (float) itp; + + return true; +} + + #endif diff --git a/src/hb-shape.h b/src/hb-shape.h index 922f8c011..821b6874b 100644 --- a/src/hb-shape.h +++ b/src/hb-shape.h @@ -53,6 +53,17 @@ hb_shape_full (hb_font_t *font, unsigned int num_features, const char * const *shaper_list); +HB_EXTERN hb_bool_t +hb_shape_justify (hb_font_t *font, + hb_buffer_t *buffer, + const hb_feature_t *features, + unsigned int num_features, + const char * const *shaper_list, + float target_width, + float *width, /* IN/OUT */ + hb_tag_t *var_tag, /* OUT */ + float *var_value /* OUT */); + HB_EXTERN const char ** hb_shape_list_shapers (void); diff --git a/util/shape-options.hh b/util/shape-options.hh index b4d9b2c00..55a1f4c53 100644 --- a/util/shape-options.hh +++ b/util/shape-options.hh @@ -161,11 +161,28 @@ struct shape_options_t } else { - if (!hb_shape_full (font, buffer, features, num_features, shapers)) + if (!width) { - if (error) - *error = "Shaping failed."; - goto fail; + if (!hb_shape_full (font, buffer, features, num_features, shapers)) + { + if (error) + *error = "Shaping failed."; + goto fail; + } + } + else + { + float target_width = width * (1 << SUBPIXEL_BITS); + float w = 0; + hb_tag_t var_tag; + float var_value; + if (!hb_shape_justify (font, buffer, features, num_features, shapers, + target_width, &w, &var_tag, &var_value)) + { + if (error) + *error = "Shaping failed."; + goto fail; + } } } @@ -202,6 +219,7 @@ struct shape_options_t hb_feature_t *features = nullptr; unsigned int num_features = 0; char **shapers = nullptr; + unsigned width = 0; hb_bool_t utf8_clusters = false; hb_codepoint_t invisible_glyph = 0; hb_codepoint_t not_found_glyph = 0; @@ -328,6 +346,8 @@ shape_options_t::add_options (option_parser_t *parser) {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"}, {"bot", 0, 0, G_OPTION_ARG_NONE, &this->bot, "Treat text as beginning-of-paragraph", nullptr}, {"eot", 0, 0, G_OPTION_ARG_NONE, &this->eot, "Treat text as end-of-paragraph", nullptr}, + {"width", 'w',0, + G_OPTION_ARG_INT, &this->width, "Target width to justify to", "WIDTH"}, {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->preserve_default_ignorables, "Preserve Default-Ignorable characters", nullptr}, {"remove-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->remove_default_ignorables, "Remove Default-Ignorable characters", nullptr}, {"invisible-glyph", 0, 0, G_OPTION_ARG_INT, &this->invisible_glyph, "Glyph value to replace Default-Ignorables with", nullptr}, @@ -339,7 +359,7 @@ shape_options_t::add_options (option_parser_t *parser) {"safe-to-insert-tatweel",0, 0, G_OPTION_ARG_NONE, &this->safe_to_insert_tatweel, "Produce safe-to-insert-tatweel glyph flag", nullptr}, {"glyphs", 0, 0, G_OPTION_ARG_NONE, &this->glyphs, "Interpret input as glyph string", nullptr}, {"verify", 0, 0, G_OPTION_ARG_NONE, &this->verify, "Perform sanity checks on shaping results", nullptr}, - {"num-iterations", 'n', G_OPTION_FLAG_IN_MAIN, + {"num-iterations", 'n',G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT, &this->num_iterations, "Run shaper N times (default: 1)", "N"}, {nullptr} }; From b937edfb148d28421f97db7c3c81e2253019e469 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 1 Mar 2023 10:44:57 -0700 Subject: [PATCH 3/9] [justify] Add min/max target_width Speeds up solving when some slack available. --- src/hb-algs.hh | 8 +++++--- src/hb-shape.cc | 32 ++++++++++++++++++-------------- src/hb-shape.h | 3 ++- util/shape-options.hh | 6 ++++-- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index 05ca3ec9d..cf9403134 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -1357,7 +1357,8 @@ double solve_itp (func_t f, double epsilon, unsigned n0, double k1, - double &ya, double &yb) + double min_y, double max_y, + double &ya, double &yb, double &y) { unsigned n1_2 = (unsigned) (hb_max (ceil (log2 ((b - a) / epsilon)) - 1.0, 0.0)); unsigned nmax = n0 + n1_2; @@ -1377,18 +1378,19 @@ double solve_itp (func_t f, double xt = delta <= fabs (x1_2 - xf) ? xf + delta * sigma_sign : x1_2; double xitp = fabs (xt - x1_2) <= r ? xt : x1_2 - r * sigma_sign; double yitp = f (xitp); - if (yitp > 0.0) + if (yitp > max_y) { b = xitp; yb = yitp; } - else if (yitp < 0.0) + else if (yitp < min_y) { a = xitp; ya = yitp; } else { + y = yitp; return xitp; } scaled_epsilon *= 0.5; diff --git a/src/hb-shape.cc b/src/hb-shape.cc index e580298d1..d04cc214b 100644 --- a/src/hb-shape.cc +++ b/src/hb-shape.cc @@ -228,14 +228,15 @@ hb_shape_justify (hb_font_t *font, const hb_feature_t *features, unsigned int num_features, const char * const *shaper_list, - float target_width, + float min_target_width, + float max_target_width, float *width, /* IN/OUT */ hb_tag_t *var_tag, /* OUT */ float *var_value /* OUT */) { // TODO Negative font scales? - if (target_width == *width) + if (min_target_width <= *width && *width <= max_target_width) return hb_shape_full (font, buffer, features, num_features, shaper_list); @@ -287,13 +288,13 @@ hb_shape_justify (hb_font_t *font, *width = buffer_width (buffer); } - if (target_width == *width) + if (min_target_width <= *width && *width <= max_target_width) return true; double a, b, ya, yb; - if (*width < target_width) + if (*width < min_target_width) { - ya = *width - target_width; + ya = *width; a = axis_info.default_value; b = axis_info.max_value; @@ -303,16 +304,16 @@ hb_shape_justify (hb_font_t *font, features, num_features, shaper_list)) return false; - yb = buffer_width (buffer) - target_width; + yb = buffer_width (buffer); if (yb <= 0) { - *width = (float) yb + target_width; + *width = (float) yb; return true; } } else { - yb = *width - target_width; + yb = *width; a = axis_info.min_value; b = axis_info.default_value; @@ -322,10 +323,10 @@ hb_shape_justify (hb_font_t *font, features, num_features, shaper_list)) return false; - ya = buffer_width (buffer) - target_width; + ya = buffer_width (buffer); if (ya >= 0) { - *width = (float) ya + target_width; + *width = (float) ya; return true; } } @@ -344,26 +345,29 @@ hb_shape_justify (hb_font_t *font, shaper_list))) { failed = true; - return target_width; + return min_target_width; } - return buffer_width (buffer) - target_width; + printf ("%g\n", x); + return buffer_width (buffer); }; + double y = 0; double itp = solve_itp (f, a, b, epsilon, n0, k1, - ya, yb); + min_target_width, max_target_width, + ya, yb, y); hb_free (text_info); if (failed) return false; - *width = (float) (ya + yb) * 0.5f + target_width; *var_value = (float) itp; + *width = (float) y; return true; } diff --git a/src/hb-shape.h b/src/hb-shape.h index 821b6874b..8c2062c6a 100644 --- a/src/hb-shape.h +++ b/src/hb-shape.h @@ -59,7 +59,8 @@ hb_shape_justify (hb_font_t *font, const hb_feature_t *features, unsigned int num_features, const char * const *shaper_list, - float target_width, + float min_target_width, + float max_target_width, float *width, /* IN/OUT */ hb_tag_t *var_tag, /* OUT */ float *var_value /* OUT */); diff --git a/util/shape-options.hh b/util/shape-options.hh index 55a1f4c53..0d8977cd6 100644 --- a/util/shape-options.hh +++ b/util/shape-options.hh @@ -172,12 +172,14 @@ struct shape_options_t } else { - float target_width = width * (1 << SUBPIXEL_BITS); + float unit = (1 << SUBPIXEL_BITS); + float target_width = width * unit; float w = 0; hb_tag_t var_tag; float var_value; if (!hb_shape_justify (font, buffer, features, num_features, shapers, - target_width, &w, &var_tag, &var_value)) + target_width - unit * 0.5f, target_width + unit * 0.5f, + &w, &var_tag, &var_value)) { if (error) *error = "Shaping failed."; From 93252c6fc3585f6c226514e9c476af82b7c55d86 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 1 Mar 2023 10:59:04 -0700 Subject: [PATCH 4/9] [justify] Debug output --- src/hb-debug.hh | 4 ++++ src/hb-shape.cc | 11 ++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/hb-debug.hh b/src/hb-debug.hh index efab37464..0ac4515fa 100644 --- a/src/hb-debug.hh +++ b/src/hb-debug.hh @@ -373,6 +373,10 @@ struct hb_no_trace_t { #define HB_DEBUG_FT (HB_DEBUG+0) #endif +#ifndef HB_DEBUG_JUSTIFY +#define HB_DEBUG_JUSTIFY (HB_DEBUG+0) +#endif + #ifndef HB_DEBUG_OBJECT #define HB_DEBUG_OBJECT (HB_DEBUG+0) #endif diff --git a/src/hb-shape.cc b/src/hb-shape.cc index d04cc214b..5aae008d7 100644 --- a/src/hb-shape.cc +++ b/src/hb-shape.cc @@ -26,6 +26,8 @@ * Google Author(s): Behdad Esfahbod */ +#define HB_DEBUG_JUSTIFY 1 + #include "hb.hh" #include "hb-shaper.hh" @@ -345,11 +347,14 @@ hb_shape_justify (hb_font_t *font, shaper_list))) { failed = true; - return min_target_width; + return (double) min_target_width; } - printf ("%g\n", x); - return buffer_width (buffer); + double w = buffer_width (buffer); + DEBUG_MSG (JUSTIFY, nullptr, "Trying '%c%c%c%c' axis parameter %f. Width %g. Target: min %g max %g", + HB_UNTAG (tag), x, w, + (double) min_target_width, (double) max_target_width); + return w; }; double y = 0; From aa10deaf4283822f8c368ecbdebd01330dd76fe5 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 1 Mar 2023 11:08:32 -0700 Subject: [PATCH 5/9] [justify] Print default buffer width in hb-shape --width=-1 --- util/shape-options.hh | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/util/shape-options.hh b/util/shape-options.hh index 0d8977cd6..8e38244f0 100644 --- a/util/shape-options.hh +++ b/util/shape-options.hh @@ -161,7 +161,7 @@ struct shape_options_t } else { - if (!width) + if (width <= 0) { if (!hb_shape_full (font, buffer, features, num_features, shapers)) { @@ -169,6 +169,25 @@ struct shape_options_t *error = "Shaping failed."; goto fail; } + + if (width < 0) + { + float unit = (1 << SUBPIXEL_BITS); + + /* Calculate buffer width */ + float w = 0; + unsigned count = 0; + hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &count); + if (HB_DIRECTION_IS_HORIZONTAL (hb_buffer_get_direction (buffer))) + for (unsigned i = 0; i < count; i++) + w += pos[i].x_advance; + else + for (unsigned i = 0; i < count; i++) + w += pos[i].y_advance; + + printf ("Default width: %u\n", (unsigned) roundf (w / unit)); + exit (0); + } } else { @@ -221,7 +240,7 @@ struct shape_options_t hb_feature_t *features = nullptr; unsigned int num_features = 0; char **shapers = nullptr; - unsigned width = 0; + signed width = 0; hb_bool_t utf8_clusters = false; hb_codepoint_t invisible_glyph = 0; hb_codepoint_t not_found_glyph = 0; @@ -349,7 +368,7 @@ shape_options_t::add_options (option_parser_t *parser) {"bot", 0, 0, G_OPTION_ARG_NONE, &this->bot, "Treat text as beginning-of-paragraph", nullptr}, {"eot", 0, 0, G_OPTION_ARG_NONE, &this->eot, "Treat text as end-of-paragraph", nullptr}, {"width", 'w',0, - G_OPTION_ARG_INT, &this->width, "Target width to justify to", "WIDTH"}, + G_OPTION_ARG_INT, &this->width, "Target width to justify to", "WIDTH, or -1"}, {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->preserve_default_ignorables, "Preserve Default-Ignorable characters", nullptr}, {"remove-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->remove_default_ignorables, "Remove Default-Ignorable characters", nullptr}, {"invisible-glyph", 0, 0, G_OPTION_ARG_INT, &this->invisible_glyph, "Glyph value to replace Default-Ignorables with", nullptr}, From d29d7b7a3dd2cfca151ce667a3290359d028911c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 1 Mar 2023 13:10:11 -0700 Subject: [PATCH 6/9] [algs] Adjust solve_itp --- src/hb-algs.hh | 11 +++++++---- src/hb-shape.cc | 4 ---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index cf9403134..13587eac0 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -1344,7 +1344,10 @@ struct HB_FUNCOBJ (hb_dec); -/* For documentation and implementation see: +/* Adapted from kurbo implementation with extra parameters added, + * and finding for a particular range instead of 0. + * + * For documentation and implementation see: * * [ITP method]: https://en.wikipedia.org/wiki/ITP_Method * [An Enhancement of the Bisection Method Average Performance Preserving Minmax Optimality]: https://dl.acm.org/doi/10.1145/3423597 @@ -1355,12 +1358,12 @@ template double solve_itp (func_t f, double a, double b, double epsilon, - unsigned n0, - double k1, double min_y, double max_y, double &ya, double &yb, double &y) { unsigned n1_2 = (unsigned) (hb_max (ceil (log2 ((b - a) / epsilon)) - 1.0, 0.0)); + const unsigned n0 = 1; // Hardwired + const double k1 = 0.2 / (b - a); // Hardwired. unsigned nmax = n0 + n1_2; double scaled_epsilon = epsilon * double (1llu << nmax); double _2_epsilon = 2.0 * epsilon; @@ -1370,8 +1373,8 @@ double solve_itp (func_t f, double r = scaled_epsilon - 0.5 * (b - a); double xf = (yb * a - ya * b) / (yb - ya); double sigma = x1_2 - xf; - // This has k2 = 2 hardwired for efficiency. double b_a = b - a; + // This has k2 = 2 hardwired for efficiency. double b_a_k2 = b_a * b_a; double delta = k1 * b_a_k2; int sigma_sign = sigma >= 0 ? +1 : -1; diff --git a/src/hb-shape.cc b/src/hb-shape.cc index 5aae008d7..3504288e4 100644 --- a/src/hb-shape.cc +++ b/src/hb-shape.cc @@ -334,8 +334,6 @@ hb_shape_justify (hb_font_t *font, } double epsilon = (b - a) / (1<<14); - const unsigned n0 = 1; - const double k1 = 0.2 / (b - a); bool failed = false; auto f = [&] (double x) @@ -361,8 +359,6 @@ hb_shape_justify (hb_font_t *font, double itp = solve_itp (f, a, b, epsilon, - n0, - k1, min_target_width, max_target_width, ya, yb, y); From 96d4ed093123293114d65800e8629deb1fff2218 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 1 Mar 2023 14:08:16 -0700 Subject: [PATCH 7/9] [justify] Document API --- docs/harfbuzz-sections.txt | 1 + src/hb-shape.cc | 78 +++++++++++++++++++++++++------------- src/hb-shape.h | 6 +-- 3 files changed, 56 insertions(+), 29 deletions(-) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index b475bbae3..4cf1aeb32 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -780,6 +780,7 @@ hb_set_t hb-shape hb_shape hb_shape_full +hb_shape_justify hb_shape_list_shapers diff --git a/src/hb-shape.cc b/src/hb-shape.cc index 3504288e4..b17699684 100644 --- a/src/hb-shape.cc +++ b/src/hb-shape.cc @@ -199,18 +199,18 @@ hb_shape (hb_font_t *font, static float -buffer_width (hb_buffer_t *buffer) +buffer_advance (hb_buffer_t *buffer) { - float w = 0; + float a = 0; auto *pos = buffer->pos; unsigned count = buffer->len; if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction)) for (unsigned i = 0; i < count; i++) - w += pos[i].x_advance; + a += pos[i].x_advance; else for (unsigned i = 0; i < count; i++) - w += pos[i].y_advance; - return w; + a += pos[i].y_advance; + return a; } static void @@ -224,21 +224,47 @@ reset_buffer (hb_buffer_t *buffer, hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE); } +/** + * hb_shape_justify: + * @font: a mutable #hb_font_t to use for shaping + * @buffer: an #hb_buffer_t to shape + * @features: (array length=num_features) (nullable): an array of user + * specified #hb_feature_t or `NULL` + * @num_features: the length of @features array + * @shaper_list: (array zero-terminated=1) (nullable): a `NULL`-terminated + * array of shapers to use or `NULL` + * @min_target_advance: Minimum advance width/height to aim for. + * @max_target_advance: Maximum advance width/height to aim for. + * @advance: (inout): Input/output advance width/height of the buffer. + * @var_tag: (out): Variation-axis tag used for justification. + * @var_value: (out): Variation-axis value used to reach target justification. + * + * See hb_shape_full() for basic details. If @shaper_list is not `NULL`, the specified + * shapers will be used in the given order, otherwise the default shapers list + * will be used. + * + * In addition, justify the shaping results such that the shaping results reach + * the target advance width/height, depending on the buffer direction. + * + * Return value: false if all shapers failed, true otherwise + * + * XSince: REPLACEME + **/ hb_bool_t hb_shape_justify (hb_font_t *font, hb_buffer_t *buffer, const hb_feature_t *features, unsigned int num_features, const char * const *shaper_list, - float min_target_width, - float max_target_width, - float *width, /* IN/OUT */ + float min_target_advance, + float max_target_advance, + float *advance, /* IN/OUT */ hb_tag_t *var_tag, /* OUT */ float *var_value /* OUT */) { // TODO Negative font scales? - if (min_target_width <= *width && *width <= max_target_width) + if (min_target_advance <= *advance && *advance <= max_target_advance) return hb_shape_full (font, buffer, features, num_features, shaper_list); @@ -266,7 +292,7 @@ hb_shape_justify (hb_font_t *font, features, num_features, shaper_list)) { - *width = buffer_width (buffer); + *advance = buffer_advance (buffer); return true; } else @@ -280,23 +306,23 @@ hb_shape_justify (hb_font_t *font, hb_memcpy (text_info, buffer->info, text_len * sizeof (buffer->info[0])); auto text = hb_array (text_info, text_len); - if (!*width) + if (!*advance) { hb_font_set_variation (font, tag, axis_info.default_value); if (!hb_shape_full (font, buffer, features, num_features, shaper_list)) return false; - *width = buffer_width (buffer); + *advance = buffer_advance (buffer); } - if (min_target_width <= *width && *width <= max_target_width) + if (min_target_advance <= *advance && *advance <= max_target_advance) return true; double a, b, ya, yb; - if (*width < min_target_width) + if (*advance < min_target_advance) { - ya = *width; + ya = *advance; a = axis_info.default_value; b = axis_info.max_value; @@ -306,16 +332,16 @@ hb_shape_justify (hb_font_t *font, features, num_features, shaper_list)) return false; - yb = buffer_width (buffer); + yb = buffer_advance (buffer); if (yb <= 0) { - *width = (float) yb; + *advance = (float) yb; return true; } } else { - yb = *width; + yb = *advance; a = axis_info.min_value; b = axis_info.default_value; @@ -325,10 +351,10 @@ hb_shape_justify (hb_font_t *font, features, num_features, shaper_list)) return false; - ya = buffer_width (buffer); + ya = buffer_advance (buffer); if (ya >= 0) { - *width = (float) ya; + *advance = (float) ya; return true; } } @@ -345,13 +371,13 @@ hb_shape_justify (hb_font_t *font, shaper_list))) { failed = true; - return (double) min_target_width; + return (double) min_target_advance; } - double w = buffer_width (buffer); - DEBUG_MSG (JUSTIFY, nullptr, "Trying '%c%c%c%c' axis parameter %f. Width %g. Target: min %g max %g", + double w = buffer_advance (buffer); + DEBUG_MSG (JUSTIFY, nullptr, "Trying '%c%c%c%c' axis parameter %f. Advance %g. Target: min %g max %g", HB_UNTAG (tag), x, w, - (double) min_target_width, (double) max_target_width); + (double) min_target_advance, (double) max_target_advance); return w; }; @@ -359,7 +385,7 @@ hb_shape_justify (hb_font_t *font, double itp = solve_itp (f, a, b, epsilon, - min_target_width, max_target_width, + min_target_advance, max_target_advance, ya, yb, y); hb_free (text_info); @@ -368,7 +394,7 @@ hb_shape_justify (hb_font_t *font, return false; *var_value = (float) itp; - *width = (float) y; + *advance = (float) y; return true; } diff --git a/src/hb-shape.h b/src/hb-shape.h index 8c2062c6a..d4d4fdfd2 100644 --- a/src/hb-shape.h +++ b/src/hb-shape.h @@ -59,9 +59,9 @@ hb_shape_justify (hb_font_t *font, const hb_feature_t *features, unsigned int num_features, const char * const *shaper_list, - float min_target_width, - float max_target_width, - float *width, /* IN/OUT */ + float min_target_advance, + float max_target_advance, + float *advance, /* IN/OUT */ hb_tag_t *var_tag, /* OUT */ float *var_value /* OUT */); From 25c66d633d58dcdd1e59095abf673a9ef08a612c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 1 Mar 2023 14:16:08 -0700 Subject: [PATCH 8/9] [justify] Wrap in HB_EXPERIMENTAL_API --- src/gen-def.py | 3 ++- src/hb-shape.cc | 8 +++++++- util/shape-options.hh | 4 ++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/gen-def.py b/src/gen-def.py index 9b4efd4f3..6011817bc 100755 --- a/src/gen-def.py +++ b/src/gen-def.py @@ -19,7 +19,8 @@ symbols = sorted (re.findall (r"^hb_\w+(?= \()", "\n".join (headers_content), re if '--experimental-api' not in sys.argv: # Move these to harfbuzz-sections.txt when got stable experimental_symbols = \ -"""hb_subset_repack_or_fail +"""hb_shape_justify +hb_subset_repack_or_fail hb_subset_input_override_name_table """.splitlines () symbols = [x for x in symbols if x not in experimental_symbols] diff --git a/src/hb-shape.cc b/src/hb-shape.cc index b17699684..13bda36f3 100644 --- a/src/hb-shape.cc +++ b/src/hb-shape.cc @@ -198,6 +198,8 @@ hb_shape (hb_font_t *font, } +#ifdef HB_EXPERIMENTAL_API + static float buffer_advance (hb_buffer_t *buffer) { @@ -246,9 +248,11 @@ reset_buffer (hb_buffer_t *buffer, * In addition, justify the shaping results such that the shaping results reach * the target advance width/height, depending on the buffer direction. * + * This API is currently experimental and will probably change in the future. + * * Return value: false if all shapers failed, true otherwise * - * XSince: REPLACEME + * XSince: EXPERIMENTAL **/ hb_bool_t hb_shape_justify (hb_font_t *font, @@ -399,5 +403,7 @@ hb_shape_justify (hb_font_t *font, return true; } +#endif + #endif diff --git a/util/shape-options.hh b/util/shape-options.hh index 8e38244f0..6096b3696 100644 --- a/util/shape-options.hh +++ b/util/shape-options.hh @@ -189,6 +189,7 @@ struct shape_options_t exit (0); } } +#ifdef HB_EXPERIMENTAL_API else { float unit = (1 << SUBPIXEL_BITS); @@ -205,6 +206,7 @@ struct shape_options_t goto fail; } } +#endif } if (normalize_glyphs) @@ -367,8 +369,10 @@ shape_options_t::add_options (option_parser_t *parser) {"script", 0, 0, G_OPTION_ARG_STRING, &this->script, "Set text script (default: auto)", "ISO-15924 tag"}, {"bot", 0, 0, G_OPTION_ARG_NONE, &this->bot, "Treat text as beginning-of-paragraph", nullptr}, {"eot", 0, 0, G_OPTION_ARG_NONE, &this->eot, "Treat text as end-of-paragraph", nullptr}, +#ifdef HB_EXPERIMENTAL_API {"width", 'w',0, G_OPTION_ARG_INT, &this->width, "Target width to justify to", "WIDTH, or -1"}, +#endif {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->preserve_default_ignorables, "Preserve Default-Ignorable characters", nullptr}, {"remove-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->remove_default_ignorables, "Remove Default-Ignorable characters", nullptr}, {"invisible-glyph", 0, 0, G_OPTION_ARG_INT, &this->invisible_glyph, "Glyph value to replace Default-Ignorables with", nullptr}, From 6de9d2b89fb10ce69ebf501b3e77bd95da5b1792 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 1 Mar 2023 14:32:06 -0700 Subject: [PATCH 9/9] [justify] Rename hb-view --width to hb-view --justify-to --- util/shape-options.hh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/util/shape-options.hh b/util/shape-options.hh index 6096b3696..b22b95883 100644 --- a/util/shape-options.hh +++ b/util/shape-options.hh @@ -161,7 +161,7 @@ struct shape_options_t } else { - if (width <= 0) + if (advance <= 0) { if (!hb_shape_full (font, buffer, features, num_features, shapers)) { @@ -170,11 +170,11 @@ struct shape_options_t goto fail; } - if (width < 0) + if (advance < 0) { float unit = (1 << SUBPIXEL_BITS); - /* Calculate buffer width */ + /* Calculate buffer advance */ float w = 0; unsigned count = 0; hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &count); @@ -185,7 +185,7 @@ struct shape_options_t for (unsigned i = 0; i < count; i++) w += pos[i].y_advance; - printf ("Default width: %u\n", (unsigned) roundf (w / unit)); + printf ("Default size: %u\n", (unsigned) roundf (w / unit)); exit (0); } } @@ -193,12 +193,12 @@ struct shape_options_t else { float unit = (1 << SUBPIXEL_BITS); - float target_width = width * unit; + float target_advance = advance * unit; float w = 0; hb_tag_t var_tag; float var_value; if (!hb_shape_justify (font, buffer, features, num_features, shapers, - target_width - unit * 0.5f, target_width + unit * 0.5f, + target_advance - unit * 0.5f, target_advance + unit * 0.5f, &w, &var_tag, &var_value)) { if (error) @@ -242,7 +242,7 @@ struct shape_options_t hb_feature_t *features = nullptr; unsigned int num_features = 0; char **shapers = nullptr; - signed width = 0; + signed advance = 0; hb_bool_t utf8_clusters = false; hb_codepoint_t invisible_glyph = 0; hb_codepoint_t not_found_glyph = 0; @@ -370,8 +370,8 @@ shape_options_t::add_options (option_parser_t *parser) {"bot", 0, 0, G_OPTION_ARG_NONE, &this->bot, "Treat text as beginning-of-paragraph", nullptr}, {"eot", 0, 0, G_OPTION_ARG_NONE, &this->eot, "Treat text as end-of-paragraph", nullptr}, #ifdef HB_EXPERIMENTAL_API - {"width", 'w',0, - G_OPTION_ARG_INT, &this->width, "Target width to justify to", "WIDTH, or -1"}, + {"justify-to", 0, 0, + G_OPTION_ARG_INT, &this->advance, "Target size to justify to", "SIZE, or -1"}, #endif {"preserve-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->preserve_default_ignorables, "Preserve Default-Ignorable characters", nullptr}, {"remove-default-ignorables",0, 0, G_OPTION_ARG_NONE, &this->remove_default_ignorables, "Remove Default-Ignorable characters", nullptr},