/** * SECTION:hb-shape * @title: hb-shape * @short_description: Conversion of text strings into positioned glyphs * @include: hb.h * * Shaping is the central operation of HarfBuzz. Shaping operates on buffers, * which are sequences of Unicode characters that use the same font and have * the same text direction, script, and language. After shaping the buffer * contains the output glyphs and their positions.
**/
/** * hb_shape_list_shapers: * * Retrieves the list of shapers supported by HarfBuzz. * * Return value: (transfer none) (array zero-terminated=1): an array of * constant strings * * Since: 0.9.2
**/ constchar **
hb_shape_list_shapers ()
{ return static_shaper_list.get_unconst ();
}
/** * hb_shape_full: * @font: an #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` * * See hb_shape() for 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. * * Return value: false if all shapers failed, true otherwise * * Since: 0.9.2
**/
hb_bool_t
hb_shape_full (hb_font_t *font,
hb_buffer_t *buffer, const hb_feature_t *features, unsignedint num_features, constchar * const *shaper_list)
{ if (unlikely (!buffer->len)) returntrue;
hb_bool_t res = hb_shape_plan_execute (shape_plan, font, buffer, features, num_features);
if (buffer->max_ops <= 0)
buffer->shaping_failed = true;
hb_shape_plan_destroy (shape_plan);
if (text_buffer)
{ if (res && buffer->successful && !buffer->shaping_failed
&& text_buffer->successful
&& !buffer->verify (text_buffer,
font,
features,
num_features,
shaper_list))
res = false;
hb_buffer_destroy (text_buffer);
}
buffer->leave ();
return res;
}
/** * hb_shape: * @font: an #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 * * Shapes @buffer using @font turning its Unicode characters content to * positioned glyphs. If @features is not `NULL`, it will be used to control the * features applied during shaping. If two @features have the same tag but * overlapping ranges the value of the feature with the higher index takes * precedence. * * Since: 0.9.2
**/ void
hb_shape (hb_font_t *font,
hb_buffer_t *buffer, const hb_feature_t *features, unsignedint num_features)
{
hb_shape_full (font, buffer, features, num_features, nullptr);
}
#ifdef HB_EXPERIMENTAL_API
staticfloat
buffer_advance (hb_buffer_t *buffer)
{ 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++)
a += pos[i].x_advance; else for (unsigned i = 0; i < count; i++)
a += pos[i].y_advance; return a;
}
/** * 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. * * If the advance of the buffer shaped with hb_shape_full() is already known, * put that in *advance. Otherwise set *advance to zero. * * This API is currently experimental and will probably change in the future. * * Return value: false if all shapers failed, true otherwise * * XSince: EXPERIMENTAL
**/
hb_bool_t
hb_shape_justify (hb_font_t *font,
hb_buffer_t *buffer, const hb_feature_t *features, unsignedint num_features, constchar * const *shaper_list, 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 default advance already matches target, nothing to do. Shape and return. */ if (min_target_advance <= *advance && *advance <= max_target_advance)
{
*var_tag = HB_TAG_NONE;
*var_value = 0.0f; return hb_shape_full (font, buffer,
features, num_features,
shaper_list);
}
hb_face_t *face = font->face;
/* Choose variation tag to use for justification. */
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 no suitable variation axis found, can't justify. Just shape and return. */ if (!tag)
{
*var_tag = HB_TAG_NONE;
*var_value = 0.0f; if (hb_shape_full (font, buffer,
features, num_features,
shaper_list))
{
*advance = buffer_advance (buffer); returntrue;
} else returnfalse;
}
/* Copy buffer text as we need it so we can shape multiple times. */ 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)) returnfalse;
hb_memcpy (text_info, buffer->info, text_len * sizeof (buffer->info[0])); auto text = hb_array<const hb_glyph_info_t> (text_info, text_len);
/* If default advance was not provided to us, calculate it. */ if (!*advance)
{
hb_font_set_variation (font, tag, axis_info.default_value); if (!hb_shape_full (font, buffer,
features, num_features,
shaper_list)) returnfalse;
*advance = buffer_advance (buffer);
}
/* If default advance already matches target, nothing to do. Shape and return. * Do this again, in case advance was just calculated.
*/ if (min_target_advance <= *advance && *advance <= max_target_advance)
{
*var_tag = HB_TAG_NONE;
*var_value = 0.0f; returntrue;
}
/* Prepare for running the solver. */ double a, b, ya, yb; if (*advance < min_target_advance)
{ /* Need to expand. */
ya = (double) *advance;
a = (double) axis_info.default_value;
b = (double) axis_info.max_value;
/* Shape buffer for maximum expansion to use as other
* starting point for the solver. */
hb_font_set_variation (font, tag, (float) b);
reset_buffer (buffer, text); if (!hb_shape_full (font, buffer,
features, num_features,
shaper_list)) returnfalse;
yb = (double) buffer_advance (buffer); /* If the maximum expansion is less than max target,
* there's nothing to solve for. Just return it. */ if (yb <= (double) max_target_advance)
{
*var_value = (float) b;
*advance = (float) yb; returntrue;
}
} else
{ /* Need to shrink. */
yb = (double) *advance;
a = (double) axis_info.min_value;
b = (double) axis_info.default_value;
/* Shape buffer for maximum shrinkate to use as other
* starting point for the solver. */
hb_font_set_variation (font, tag, (float) a);
reset_buffer (buffer, text); if (!hb_shape_full (font, buffer,
features, num_features,
shaper_list)) returnfalse;
ya = (double) buffer_advance (buffer); /* If the maximum shrinkate is more than min target,
* there's nothing to solve for. Just return it. */ if (ya >= (double) min_target_advance)
{
*var_value = (float) a;
*advance = (float) ya; returntrue;
}
}
/* Run the solver to find a var axis value that hits
* the desired width. */
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.