2009-08-13 23:13:25 +02:00
|
|
|
/*
|
2011-04-21 23:14:28 +02:00
|
|
|
* Copyright © 2009 Red Hat, Inc.
|
2012-09-07 04:09:06 +02:00
|
|
|
* Copyright © 2012 Google, Inc.
|
2009-08-13 23:13:25 +02:00
|
|
|
*
|
2010-04-22 06:11:43 +02:00
|
|
|
* This is part of HarfBuzz, a text shaping library.
|
2009-08-13 23:13:25 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, without written agreement and without
|
|
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose, provided that the
|
|
|
|
* above copyright notice and the following two paragraphs appear in
|
|
|
|
* all copies of this software.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
|
|
|
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*
|
|
|
|
* Red Hat Author(s): Behdad Esfahbod
|
2012-09-07 04:09:06 +02:00
|
|
|
* Google Author(s): Behdad Esfahbod
|
2009-08-13 23:13:25 +02:00
|
|
|
*/
|
|
|
|
|
2023-03-01 18:59:04 +01:00
|
|
|
#define HB_DEBUG_JUSTIFY 1
|
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#include "hb.hh"
|
2009-08-13 23:13:25 +02:00
|
|
|
|
2018-08-26 07:36:36 +02:00
|
|
|
#include "hb-shaper.hh"
|
|
|
|
#include "hb-shape-plan.hh"
|
|
|
|
#include "hb-buffer.hh"
|
|
|
|
#include "hb-font.hh"
|
|
|
|
#include "hb-machinery.hh"
|
2009-11-05 03:07:03 +01:00
|
|
|
|
2018-10-27 13:07:33 +02:00
|
|
|
|
2022-12-04 23:20:51 +01:00
|
|
|
#ifndef HB_NO_SHAPER
|
|
|
|
|
2014-01-08 01:28:55 +01:00
|
|
|
/**
|
|
|
|
* SECTION:hb-shape
|
2018-10-27 13:50:38 +02:00
|
|
|
* @title: hb-shape
|
2014-01-08 01:28:55 +01:00
|
|
|
* @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
|
2018-10-27 13:07:33 +02:00
|
|
|
* the same text direction, script, and language. After shaping the buffer
|
2014-01-08 01:28:55 +01:00
|
|
|
* contains the output glyphs and their positions.
|
|
|
|
**/
|
2011-08-05 04:31:05 +02:00
|
|
|
|
2018-10-27 13:07:33 +02:00
|
|
|
|
2021-09-14 13:09:54 +02:00
|
|
|
static inline void free_static_shaper_list ();
|
2018-08-14 22:50:24 +02:00
|
|
|
|
2022-06-21 00:51:35 +02:00
|
|
|
static const char * const nil_shaper_list[] = {nullptr};
|
2018-08-13 02:14:32 +02:00
|
|
|
|
|
|
|
static struct hb_shaper_list_lazy_loader_t : hb_lazy_loader_t<const char *,
|
2018-08-13 02:19:55 +02:00
|
|
|
hb_shaper_list_lazy_loader_t>
|
2018-08-13 02:14:32 +02:00
|
|
|
{
|
2018-12-17 19:01:01 +01:00
|
|
|
static const char ** create ()
|
2018-08-13 02:14:32 +02:00
|
|
|
{
|
2021-07-08 18:58:50 +02:00
|
|
|
const char **shaper_list = (const char **) hb_calloc (1 + HB_SHAPERS_COUNT, sizeof (const char *));
|
2018-08-13 02:14:32 +02:00
|
|
|
if (unlikely (!shaper_list))
|
|
|
|
return nullptr;
|
|
|
|
|
2018-11-15 03:08:54 +01:00
|
|
|
const hb_shaper_entry_t *shapers = _hb_shapers_get ();
|
2018-08-13 02:14:32 +02:00
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < HB_SHAPERS_COUNT; i++)
|
|
|
|
shaper_list[i] = shapers[i].name;
|
|
|
|
shaper_list[i] = nullptr;
|
|
|
|
|
2021-09-14 13:09:54 +02:00
|
|
|
hb_atexit (free_static_shaper_list);
|
2018-08-13 02:14:32 +02:00
|
|
|
|
|
|
|
return shaper_list;
|
|
|
|
}
|
2018-12-16 20:08:10 +01:00
|
|
|
static void destroy (const char **l)
|
2021-07-08 18:58:50 +02:00
|
|
|
{ hb_free (l); }
|
2022-06-21 02:01:25 +02:00
|
|
|
static const char * const * get_null ()
|
|
|
|
{ return nil_shaper_list; }
|
2018-08-13 02:14:32 +02:00
|
|
|
} static_shaper_list;
|
2012-06-06 01:23:29 +02:00
|
|
|
|
2021-09-14 13:09:54 +02:00
|
|
|
static inline
|
2018-12-17 19:01:01 +01:00
|
|
|
void free_static_shaper_list ()
|
2012-06-06 01:23:29 +02:00
|
|
|
{
|
2018-08-13 02:19:55 +02:00
|
|
|
static_shaper_list.free_instance ();
|
2012-06-06 01:23:29 +02:00
|
|
|
}
|
2011-08-06 01:48:49 +02:00
|
|
|
|
2018-08-13 02:14:32 +02:00
|
|
|
|
2013-09-06 21:40:22 +02:00
|
|
|
/**
|
|
|
|
* hb_shape_list_shapers:
|
|
|
|
*
|
2014-01-08 01:28:55 +01:00
|
|
|
* Retrieves the list of shapers supported by HarfBuzz.
|
2013-09-06 21:40:22 +02:00
|
|
|
*
|
2014-01-08 01:28:55 +01:00
|
|
|
* Return value: (transfer none) (array zero-terminated=1): an array of
|
|
|
|
* constant strings
|
2013-09-06 21:40:22 +02:00
|
|
|
*
|
2015-06-01 13:22:01 +02:00
|
|
|
* Since: 0.9.2
|
2013-09-06 21:40:22 +02:00
|
|
|
**/
|
2011-08-06 01:48:49 +02:00
|
|
|
const char **
|
2018-12-17 19:01:01 +01:00
|
|
|
hb_shape_list_shapers ()
|
2011-08-06 01:48:49 +02:00
|
|
|
{
|
2018-08-13 02:14:32 +02:00
|
|
|
return static_shaper_list.get_unconst ();
|
2011-08-06 01:48:49 +02:00
|
|
|
}
|
2011-08-05 04:31:05 +02:00
|
|
|
|
2012-06-06 01:23:29 +02:00
|
|
|
|
2013-09-06 21:40:22 +02:00
|
|
|
/**
|
|
|
|
* hb_shape_full:
|
2014-01-08 01:28:55 +01:00
|
|
|
* @font: an #hb_font_t to use for shaping
|
|
|
|
* @buffer: an #hb_buffer_t to shape
|
2020-12-30 23:28:27 +01:00
|
|
|
* @features: (array length=num_features) (nullable): an array of user
|
2022-06-30 08:43:57 +02:00
|
|
|
* specified #hb_feature_t or `NULL`
|
2014-01-08 01:28:55 +01:00
|
|
|
* @num_features: the length of @features array
|
2022-06-30 08:43:57 +02:00
|
|
|
* @shaper_list: (array zero-terminated=1) (nullable): a `NULL`-terminated
|
|
|
|
* array of shapers to use or `NULL`
|
2013-09-06 21:40:22 +02:00
|
|
|
*
|
2022-06-30 08:43:57 +02:00
|
|
|
* See hb_shape() for details. If @shaper_list is not `NULL`, the specified
|
2014-01-08 01:28:55 +01:00
|
|
|
* shapers will be used in the given order, otherwise the default shapers list
|
|
|
|
* will be used.
|
2013-09-06 21:40:22 +02:00
|
|
|
*
|
2017-01-23 01:55:40 +01:00
|
|
|
* Return value: false if all shapers failed, true otherwise
|
2013-09-06 21:40:22 +02:00
|
|
|
*
|
2015-06-01 13:22:01 +02:00
|
|
|
* Since: 0.9.2
|
2013-09-06 21:40:22 +02:00
|
|
|
**/
|
2011-08-05 04:31:05 +02:00
|
|
|
hb_bool_t
|
2011-08-10 16:25:56 +02:00
|
|
|
hb_shape_full (hb_font_t *font,
|
|
|
|
hb_buffer_t *buffer,
|
|
|
|
const hb_feature_t *features,
|
|
|
|
unsigned int num_features,
|
|
|
|
const char * const *shaper_list)
|
2011-03-16 18:53:32 +01:00
|
|
|
{
|
2022-06-01 12:43:10 +02:00
|
|
|
if (unlikely (!buffer->len))
|
|
|
|
return true;
|
|
|
|
|
2022-06-01 12:54:18 +02:00
|
|
|
buffer->enter ();
|
|
|
|
|
2022-01-28 21:45:25 +01:00
|
|
|
hb_buffer_t *text_buffer = nullptr;
|
|
|
|
if (buffer->flags & HB_BUFFER_FLAG_VERIFY)
|
|
|
|
{
|
|
|
|
text_buffer = hb_buffer_create ();
|
|
|
|
hb_buffer_append (text_buffer, buffer, 0, -1);
|
|
|
|
}
|
|
|
|
|
2016-09-10 12:57:24 +02:00
|
|
|
hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached2 (font->face, &buffer->props,
|
|
|
|
features, num_features,
|
|
|
|
font->coords, font->num_coords,
|
|
|
|
shaper_list);
|
2022-06-01 12:54:18 +02:00
|
|
|
|
2012-07-27 07:13:53 +02:00
|
|
|
hb_bool_t res = hb_shape_plan_execute (shape_plan, font, buffer, features, num_features);
|
2022-06-01 12:54:18 +02:00
|
|
|
|
|
|
|
if (buffer->max_ops <= 0)
|
|
|
|
buffer->shaping_failed = true;
|
|
|
|
|
2012-07-27 07:13:53 +02:00
|
|
|
hb_shape_plan_destroy (shape_plan);
|
2012-09-07 04:26:16 +02:00
|
|
|
|
2022-01-28 21:45:25 +01:00
|
|
|
if (text_buffer)
|
|
|
|
{
|
2022-05-31 15:52:04 +02:00
|
|
|
if (res && buffer->successful && !buffer->shaping_failed
|
|
|
|
&& text_buffer->successful
|
|
|
|
&& !buffer->verify (text_buffer,
|
2022-01-28 21:45:25 +01:00
|
|
|
font,
|
|
|
|
features,
|
|
|
|
num_features,
|
|
|
|
shaper_list))
|
|
|
|
res = false;
|
|
|
|
hb_buffer_destroy (text_buffer);
|
|
|
|
}
|
|
|
|
|
2022-06-01 12:54:18 +02:00
|
|
|
buffer->leave ();
|
|
|
|
|
2012-07-27 07:13:53 +02:00
|
|
|
return res;
|
2011-03-16 18:53:32 +01:00
|
|
|
}
|
2011-08-05 23:22:19 +02:00
|
|
|
|
2013-09-06 21:40:22 +02:00
|
|
|
/**
|
|
|
|
* hb_shape:
|
2014-01-08 01:28:55 +01:00
|
|
|
* @font: an #hb_font_t to use for shaping
|
|
|
|
* @buffer: an #hb_buffer_t to shape
|
2020-12-30 23:28:27 +01:00
|
|
|
* @features: (array length=num_features) (nullable): an array of user
|
2022-06-30 08:43:57 +02:00
|
|
|
* specified #hb_feature_t or `NULL`
|
2014-01-08 01:28:55 +01:00
|
|
|
* @num_features: the length of @features array
|
|
|
|
*
|
|
|
|
* Shapes @buffer using @font turning its Unicode characters content to
|
2022-06-30 08:43:57 +02:00
|
|
|
* positioned glyphs. If @features is not `NULL`, it will be used to control the
|
2019-10-10 21:30:48 +02:00
|
|
|
* 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.
|
2013-09-06 21:40:22 +02:00
|
|
|
*
|
2015-09-03 13:23:22 +02:00
|
|
|
* Since: 0.9.2
|
2013-09-06 21:40:22 +02:00
|
|
|
**/
|
2011-08-05 23:22:19 +02:00
|
|
|
void
|
|
|
|
hb_shape (hb_font_t *font,
|
|
|
|
hb_buffer_t *buffer,
|
|
|
|
const hb_feature_t *features,
|
|
|
|
unsigned int num_features)
|
|
|
|
{
|
2017-10-15 12:11:08 +02:00
|
|
|
hb_shape_full (font, buffer, features, num_features, nullptr);
|
2011-08-05 23:22:19 +02:00
|
|
|
}
|
2022-12-04 23:20:51 +01:00
|
|
|
|
|
|
|
|
2023-02-28 20:25:32 +01:00
|
|
|
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<const hb_glyph_info_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,
|
2023-03-01 18:44:57 +01:00
|
|
|
float min_target_width,
|
|
|
|
float max_target_width,
|
2023-02-28 20:25:32 +01:00
|
|
|
float *width, /* IN/OUT */
|
|
|
|
hb_tag_t *var_tag, /* OUT */
|
|
|
|
float *var_value /* OUT */)
|
|
|
|
{
|
|
|
|
// TODO Negative font scales?
|
|
|
|
|
2023-03-01 18:44:57 +01:00
|
|
|
if (min_target_width <= *width && *width <= max_target_width)
|
2023-02-28 20:25:32 +01:00
|
|
|
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<const hb_glyph_info_t> (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);
|
|
|
|
}
|
|
|
|
|
2023-03-01 18:44:57 +01:00
|
|
|
if (min_target_width <= *width && *width <= max_target_width)
|
2023-02-28 20:25:32 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
double a, b, ya, yb;
|
2023-03-01 18:44:57 +01:00
|
|
|
if (*width < min_target_width)
|
2023-02-28 20:25:32 +01:00
|
|
|
{
|
2023-03-01 18:44:57 +01:00
|
|
|
ya = *width;
|
2023-02-28 20:25:32 +01:00
|
|
|
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;
|
2023-03-01 18:44:57 +01:00
|
|
|
yb = buffer_width (buffer);
|
2023-02-28 20:25:32 +01:00
|
|
|
if (yb <= 0)
|
|
|
|
{
|
2023-03-01 18:44:57 +01:00
|
|
|
*width = (float) yb;
|
2023-02-28 20:25:32 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-01 18:44:57 +01:00
|
|
|
yb = *width;
|
2023-02-28 20:25:32 +01:00
|
|
|
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;
|
2023-03-01 18:44:57 +01:00
|
|
|
ya = buffer_width (buffer);
|
2023-02-28 20:25:32 +01:00
|
|
|
if (ya >= 0)
|
|
|
|
{
|
2023-03-01 18:44:57 +01:00
|
|
|
*width = (float) ya;
|
2023-02-28 20:25:32 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double epsilon = (b - a) / (1<<14);
|
|
|
|
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;
|
2023-03-01 18:59:04 +01:00
|
|
|
return (double) min_target_width;
|
2023-02-28 20:25:32 +01:00
|
|
|
}
|
|
|
|
|
2023-03-01 18:59:04 +01:00
|
|
|
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;
|
2023-02-28 20:25:32 +01:00
|
|
|
};
|
|
|
|
|
2023-03-01 18:44:57 +01:00
|
|
|
double y = 0;
|
2023-02-28 20:25:32 +01:00
|
|
|
double itp = solve_itp (f,
|
|
|
|
a, b,
|
|
|
|
epsilon,
|
2023-03-01 18:44:57 +01:00
|
|
|
min_target_width, max_target_width,
|
|
|
|
ya, yb, y);
|
2023-02-28 20:25:32 +01:00
|
|
|
|
|
|
|
hb_free (text_info);
|
|
|
|
|
|
|
|
if (failed)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*var_value = (float) itp;
|
2023-03-01 18:44:57 +01:00
|
|
|
*width = (float) y;
|
2023-02-28 20:25:32 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-04 23:20:51 +01:00
|
|
|
#endif
|