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} };