Path: blob/master/thirdparty/harfbuzz/src/hb-buffer.cc
9906 views
/*1* Copyright © 1998-2004 David Turner and Werner Lemberg2* Copyright © 2004,2007,2009,2010 Red Hat, Inc.3* Copyright © 2011,2012 Google, Inc.4*5* This is part of HarfBuzz, a text shaping library.6*7* Permission is hereby granted, without written agreement and without8* license or royalty fees, to use, copy, modify, and distribute this9* software and its documentation for any purpose, provided that the10* above copyright notice and the following two paragraphs appear in11* all copies of this software.12*13* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR14* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES15* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN16* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH17* DAMAGE.18*19* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,20* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND21* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS22* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO23* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.24*25* Red Hat Author(s): Owen Taylor, Behdad Esfahbod26* Google Author(s): Behdad Esfahbod27*/2829#include "hb-buffer.hh"30#include "hb-utf.hh"313233/**34* SECTION: hb-buffer35* @title: hb-buffer36* @short_description: Input and output buffers37* @include: hb.h38*39* Buffers serve a dual role in HarfBuzz; before shaping, they hold40* the input characters that are passed to hb_shape(), and after41* shaping they hold the output glyphs.42*43* The input buffer is a sequence of Unicode codepoints, with44* associated attributes such as direction and script. The output45* buffer is a sequence of glyphs, with associated attributes such46* as position and cluster.47**/484950/**51* hb_segment_properties_equal:52* @a: first #hb_segment_properties_t to compare.53* @b: second #hb_segment_properties_t to compare.54*55* Checks the equality of two #hb_segment_properties_t's.56*57* Return value:58* `true` if all properties of @a equal those of @b, `false` otherwise.59*60* Since: 0.9.761**/62hb_bool_t63hb_segment_properties_equal (const hb_segment_properties_t *a,64const hb_segment_properties_t *b)65{66return a->direction == b->direction &&67a->script == b->script &&68a->language == b->language &&69a->reserved1 == b->reserved1 &&70a->reserved2 == b->reserved2;7172}7374/**75* hb_segment_properties_hash:76* @p: #hb_segment_properties_t to hash.77*78* Creates a hash representing @p.79*80* Return value:81* A hash of @p.82*83* Since: 0.9.784**/85unsigned int86hb_segment_properties_hash (const hb_segment_properties_t *p)87{88return ((unsigned int) p->direction * 31 +89(unsigned int) p->script) * 31 +90(intptr_t) (p->language);91}9293/**94* hb_segment_properties_overlay:95* @p: #hb_segment_properties_t to fill in.96* @src: #hb_segment_properties_t to fill in from.97*98* Fills in missing fields of @p from @src in a considered manner.99*100* First, if @p does not have direction set, direction is copied from @src.101*102* Next, if @p and @src have the same direction (which can be unset), if @p103* does not have script set, script is copied from @src.104*105* Finally, if @p and @src have the same direction and script (which either106* can be unset), if @p does not have language set, language is copied from107* @src.108*109* Since: 3.3.0110**/111void112hb_segment_properties_overlay (hb_segment_properties_t *p,113const hb_segment_properties_t *src)114{115if (unlikely (!p || !src))116return;117118if (!p->direction)119p->direction = src->direction;120121if (p->direction != src->direction)122return;123124if (!p->script)125p->script = src->script;126127if (p->script != src->script)128return;129130if (!p->language)131p->language = src->language;132}133134/* Here is how the buffer works internally:135*136* There are two info pointers: info and out_info. They always have137* the same allocated size, but different lengths.138*139* As an optimization, both info and out_info may point to the140* same piece of memory, which is owned by info. This remains the141* case as long as out_len doesn't exceed i at any time.142* In that case, sync() is mostly no-op and the glyph operations143* operate mostly in-place.144*145* As soon as out_info gets longer than info, out_info is moved over146* to an alternate buffer (which we reuse the pos buffer for), and its147* current contents (out_len entries) are copied to the new place.148*149* This should all remain transparent to the user. sync() then150* switches info over to out_info and does housekeeping.151*/152153154155/* Internal API */156157bool158hb_buffer_t::enlarge (unsigned int size)159{160if (unlikely (!successful))161return false;162if (unlikely (size > max_len))163{164successful = false;165return false;166}167168unsigned int new_allocated = allocated;169hb_glyph_position_t *new_pos = nullptr;170hb_glyph_info_t *new_info = nullptr;171bool separate_out = out_info != info;172173if (unlikely (hb_unsigned_mul_overflows (size, sizeof (info[0]))))174goto done;175176while (size >= new_allocated)177new_allocated += (new_allocated >> 1) + 32;178179unsigned new_bytes;180if (unlikely (hb_unsigned_mul_overflows (new_allocated, sizeof (info[0]), &new_bytes)))181goto done;182183static_assert (sizeof (info[0]) == sizeof (pos[0]), "");184new_pos = (hb_glyph_position_t *) hb_realloc (pos, new_bytes);185new_info = (hb_glyph_info_t *) hb_realloc (info, new_bytes);186187done:188if (unlikely (!new_pos || !new_info))189successful = false;190191if (likely (new_pos))192pos = new_pos;193194if (likely (new_info))195info = new_info;196197out_info = separate_out ? (hb_glyph_info_t *) pos : info;198if (likely (successful))199allocated = new_allocated;200201return likely (successful);202}203204bool205hb_buffer_t::make_room_for (unsigned int num_in,206unsigned int num_out)207{208if (unlikely (!ensure (out_len + num_out))) return false;209210if (out_info == info &&211out_len + num_out > idx + num_in)212{213assert (have_output);214215out_info = (hb_glyph_info_t *) pos;216hb_memcpy (out_info, info, out_len * sizeof (out_info[0]));217}218219return true;220}221222bool223hb_buffer_t::shift_forward (unsigned int count)224{225assert (have_output);226if (unlikely (!ensure (len + count))) return false;227228memmove (info + idx + count, info + idx, (len - idx) * sizeof (info[0]));229if (idx + count > len)230{231/* Under memory failure we might expose this area. At least232* clean it up. Oh well...233*234* Ideally, we should at least set Default_Ignorable bits on235* these, as well as consistent cluster values. But the former236* is layering violation... */237hb_memset (info + len, 0, (idx + count - len) * sizeof (info[0]));238}239len += count;240idx += count;241242return true;243}244245hb_buffer_t::scratch_buffer_t *246hb_buffer_t::get_scratch_buffer (unsigned int *size)247{248have_output = false;249have_positions = false;250251out_len = 0;252out_info = info;253254assert ((uintptr_t) pos % sizeof (scratch_buffer_t) == 0);255*size = allocated * sizeof (pos[0]) / sizeof (scratch_buffer_t);256return (scratch_buffer_t *) (void *) pos;257}258259260261/* HarfBuzz-Internal API */262263void264hb_buffer_t::similar (const hb_buffer_t &src)265{266hb_unicode_funcs_destroy (unicode);267unicode = hb_unicode_funcs_reference (src.unicode);268flags = src.flags;269cluster_level = src.cluster_level;270replacement = src.replacement;271invisible = src.invisible;272not_found = src.not_found;273not_found_variation_selector = src.not_found_variation_selector;274}275276void277hb_buffer_t::reset ()278{279hb_unicode_funcs_destroy (unicode);280unicode = hb_unicode_funcs_reference (hb_unicode_funcs_get_default ());281flags = HB_BUFFER_FLAG_DEFAULT;282cluster_level = HB_BUFFER_CLUSTER_LEVEL_DEFAULT;283replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT;284invisible = 0;285not_found = 0;286not_found_variation_selector = HB_CODEPOINT_INVALID;287288clear ();289}290291void292hb_buffer_t::clear ()293{294content_type = HB_BUFFER_CONTENT_TYPE_INVALID;295hb_segment_properties_t default_props = HB_SEGMENT_PROPERTIES_DEFAULT;296props = default_props;297298successful = true;299shaping_failed = false;300have_output = false;301have_positions = false;302303idx = 0;304len = 0;305out_len = 0;306out_info = info;307308hb_memset (context, 0, sizeof context);309hb_memset (context_len, 0, sizeof context_len);310311deallocate_var_all ();312serial = 0;313random_state = 1;314scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;315}316317void318hb_buffer_t::enter ()319{320deallocate_var_all ();321serial = 0;322shaping_failed = false;323scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;324unsigned mul;325if (likely (!hb_unsigned_mul_overflows (len, HB_BUFFER_MAX_LEN_FACTOR, &mul)))326{327max_len = hb_max (mul, (unsigned) HB_BUFFER_MAX_LEN_MIN);328}329if (likely (!hb_unsigned_mul_overflows (len, HB_BUFFER_MAX_OPS_FACTOR, &mul)))330{331max_ops = hb_max (mul, (unsigned) HB_BUFFER_MAX_OPS_MIN);332}333}334void335hb_buffer_t::leave ()336{337max_len = HB_BUFFER_MAX_LEN_DEFAULT;338max_ops = HB_BUFFER_MAX_OPS_DEFAULT;339deallocate_var_all ();340serial = 0;341// Intentionally not reseting shaping_failed, such that it can be inspected.342}343344345void346hb_buffer_t::add (hb_codepoint_t codepoint,347unsigned int cluster)348{349hb_glyph_info_t *glyph;350351if (unlikely (!ensure (len + 1))) return;352353glyph = &info[len];354355hb_memset (glyph, 0, sizeof (*glyph));356glyph->codepoint = codepoint;357glyph->mask = 0;358glyph->cluster = cluster;359360len++;361}362363void364hb_buffer_t::add_info (const hb_glyph_info_t &glyph_info)365{366if (unlikely (!ensure (len + 1))) return;367368info[len] = glyph_info;369370len++;371}372void373hb_buffer_t::add_info_and_pos (const hb_glyph_info_t &glyph_info,374const hb_glyph_position_t &glyph_pos)375{376if (unlikely (!ensure (len + 1))) return;377378info[len] = glyph_info;379assert (have_positions);380pos[len] = glyph_pos;381382len++;383}384385386void387hb_buffer_t::clear_output ()388{389have_output = true;390have_positions = false;391392idx = 0;393out_len = 0;394out_info = info;395}396397void398hb_buffer_t::clear_positions ()399{400have_output = false;401have_positions = true;402403out_len = 0;404out_info = info;405406hb_memset (pos, 0, sizeof (pos[0]) * len);407}408409bool410hb_buffer_t::sync ()411{412bool ret = false;413414assert (have_output);415416assert (idx <= len);417418if (unlikely (!successful || !next_glyphs (len - idx)))419goto reset;420421if (out_info != info)422{423pos = (hb_glyph_position_t *) info;424info = out_info;425}426len = out_len;427ret = true;428429reset:430have_output = false;431out_len = 0;432out_info = info;433idx = 0;434435return ret;436}437438int439hb_buffer_t::sync_so_far ()440{441bool had_output = have_output;442unsigned out_i = out_len;443unsigned i = idx;444unsigned old_idx = idx;445446if (sync ())447idx = out_i;448else449idx = i;450451if (had_output)452{453have_output = true;454out_len = idx;455}456457assert (idx <= len);458459return idx - old_idx;460}461462bool463hb_buffer_t::move_to (unsigned int i)464{465if (!have_output)466{467assert (i <= len);468idx = i;469return true;470}471if (unlikely (!successful))472return false;473474assert (i <= out_len + (len - idx));475476if (out_len < i)477{478unsigned int count = i - out_len;479if (unlikely (!make_room_for (count, count))) return false;480481memmove (out_info + out_len, info + idx, count * sizeof (out_info[0]));482idx += count;483out_len += count;484}485else if (out_len > i)486{487/* Tricky part: rewinding... */488unsigned int count = out_len - i;489490/* This will blow in our face if memory allocation fails later491* in this same lookup...492*493* We used to shift with extra 32 items.494* But that would leave empty slots in the buffer in case of allocation495* failures. See comments in shift_forward(). This can cause O(N^2)496* behavior more severely than adding 32 empty slots can... */497if (unlikely (idx < count && !shift_forward (count - idx))) return false;498499assert (idx >= count);500501idx -= count;502out_len -= count;503memmove (info + idx, out_info + out_len, count * sizeof (out_info[0]));504}505506return true;507}508509510void511hb_buffer_t::set_masks (hb_mask_t value,512hb_mask_t mask,513unsigned int cluster_start,514unsigned int cluster_end)515{516if (!mask)517return;518519hb_mask_t not_mask = ~mask;520value &= mask;521522unsigned int count = len;523for (unsigned int i = 0; i < count; i++)524if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)525info[i].mask = (info[i].mask & not_mask) | value;526}527528void529hb_buffer_t::merge_clusters_impl (unsigned int start,530unsigned int end)531{532if (!HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (cluster_level))533{534unsafe_to_break (start, end);535return;536}537538unsigned int cluster = info[start].cluster;539540for (unsigned int i = start + 1; i < end; i++)541cluster = hb_min (cluster, info[i].cluster);542543/* Extend end */544if (cluster != info[end - 1].cluster)545while (end < len && info[end - 1].cluster == info[end].cluster)546end++;547548/* Extend start */549if (cluster != info[start].cluster)550while (idx < start && info[start - 1].cluster == info[start].cluster)551start--;552553/* If we hit the start of buffer, continue in out-buffer. */554if (idx == start && info[start].cluster != cluster)555for (unsigned int i = out_len; i && out_info[i - 1].cluster == info[start].cluster; i--)556set_cluster (out_info[i - 1], cluster);557558for (unsigned int i = start; i < end; i++)559set_cluster (info[i], cluster);560}561void562hb_buffer_t::merge_out_clusters (unsigned int start,563unsigned int end)564{565if (!HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (cluster_level))566return;567568if (unlikely (end - start < 2))569return;570571unsigned int cluster = out_info[start].cluster;572573for (unsigned int i = start + 1; i < end; i++)574cluster = hb_min (cluster, out_info[i].cluster);575576/* Extend start */577while (start && out_info[start - 1].cluster == out_info[start].cluster)578start--;579580/* Extend end */581while (end < out_len && out_info[end - 1].cluster == out_info[end].cluster)582end++;583584/* If we hit the end of out-buffer, continue in buffer. */585if (end == out_len)586for (unsigned int i = idx; i < len && info[i].cluster == out_info[end - 1].cluster; i++)587set_cluster (info[i], cluster);588589for (unsigned int i = start; i < end; i++)590set_cluster (out_info[i], cluster);591}592void593hb_buffer_t::delete_glyph ()594{595/* The logic here is duplicated in hb_ot_hide_default_ignorables(). */596597unsigned int cluster = info[idx].cluster;598if ((idx + 1 < len && cluster == info[idx + 1].cluster) ||599(out_len && cluster == out_info[out_len - 1].cluster))600{601/* Cluster survives; do nothing. */602goto done;603}604605if (out_len)606{607/* Merge cluster backward. */608if (cluster < out_info[out_len - 1].cluster)609{610unsigned int mask = info[idx].mask;611unsigned int old_cluster = out_info[out_len - 1].cluster;612for (unsigned i = out_len; i && out_info[i - 1].cluster == old_cluster; i--)613set_cluster (out_info[i - 1], cluster, mask);614}615goto done;616}617618if (idx + 1 < len)619{620/* Merge cluster forward. */621merge_clusters (idx, idx + 2);622goto done;623}624625done:626skip_glyph ();627}628629void630hb_buffer_t::delete_glyphs_inplace (bool (*filter) (const hb_glyph_info_t *info))631{632/* Merge clusters and delete filtered glyphs.633* NOTE! We can't use out-buffer as we have positioning data. */634unsigned int j = 0;635unsigned int count = len;636for (unsigned int i = 0; i < count; i++)637{638if (filter (&info[i]))639{640/* Merge clusters.641* Same logic as delete_glyph(), but for in-place removal. */642643unsigned int cluster = info[i].cluster;644if (i + 1 < count && cluster == info[i + 1].cluster)645continue; /* Cluster survives; do nothing. */646647if (j)648{649/* Merge cluster backward. */650if (cluster < info[j - 1].cluster)651{652unsigned int mask = info[i].mask;653unsigned int old_cluster = info[j - 1].cluster;654for (unsigned k = j; k && info[k - 1].cluster == old_cluster; k--)655set_cluster (info[k - 1], cluster, mask);656}657continue;658}659660if (i + 1 < count)661merge_clusters (i, i + 2); /* Merge cluster forward. */662663continue;664}665666if (j != i)667{668info[j] = info[i];669pos[j] = pos[i];670}671j++;672}673len = j;674}675676void677hb_buffer_t::guess_segment_properties ()678{679assert_unicode ();680681/* If script is set to INVALID, guess from buffer contents */682if (props.script == HB_SCRIPT_INVALID) {683for (unsigned int i = 0; i < len; i++) {684hb_script_t script = unicode->script (info[i].codepoint);685if (likely (script != HB_SCRIPT_COMMON &&686script != HB_SCRIPT_INHERITED &&687script != HB_SCRIPT_UNKNOWN)) {688props.script = script;689break;690}691}692}693694/* If direction is set to INVALID, guess from script */695if (props.direction == HB_DIRECTION_INVALID) {696props.direction = hb_script_get_horizontal_direction (props.script);697if (props.direction == HB_DIRECTION_INVALID)698props.direction = HB_DIRECTION_LTR;699}700701/* If language is not set, use default language from locale */702if (props.language == HB_LANGUAGE_INVALID) {703/* TODO get_default_for_script? using $LANGUAGE */704props.language = hb_language_get_default ();705}706}707708709/* Public API */710711DEFINE_NULL_INSTANCE (hb_buffer_t) =712{713HB_OBJECT_HEADER_STATIC,714715const_cast<hb_unicode_funcs_t *> (&_hb_Null_hb_unicode_funcs_t),716HB_BUFFER_FLAG_DEFAULT,717HB_BUFFER_CLUSTER_LEVEL_DEFAULT,718HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT,7190, /* invisible */7200, /* not_found */721HB_CODEPOINT_INVALID, /* not_found_variation_selector */722723724HB_BUFFER_CONTENT_TYPE_INVALID,725HB_SEGMENT_PROPERTIES_DEFAULT,726727false, /* successful */728true, /* shaping_failed */729false, /* have_output */730true /* have_positions */731732/* Zero is good enough for everything else. */733};734735736/**737* hb_buffer_create:738*739* Creates a new #hb_buffer_t with all properties to defaults.740*741* Return value: (transfer full):742* A newly allocated #hb_buffer_t with a reference count of 1. The initial743* reference count should be released with hb_buffer_destroy() when you are done744* using the #hb_buffer_t. This function never returns `NULL`. If memory cannot745* be allocated, a special #hb_buffer_t object will be returned on which746* hb_buffer_allocation_successful() returns `false`.747*748* Since: 0.9.2749**/750hb_buffer_t *751hb_buffer_create ()752{753hb_buffer_t *buffer;754755if (!(buffer = hb_object_create<hb_buffer_t> ()))756return hb_buffer_get_empty ();757758buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;759buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT;760761buffer->reset ();762763return buffer;764}765766/**767* hb_buffer_create_similar:768* @src: An #hb_buffer_t769*770* Creates a new #hb_buffer_t, similar to hb_buffer_create(). The only771* difference is that the buffer is configured similarly to @src.772*773* Return value: (transfer full):774* A newly allocated #hb_buffer_t, similar to hb_buffer_create().775*776* Since: 3.3.0777**/778hb_buffer_t *779hb_buffer_create_similar (const hb_buffer_t *src)780{781hb_buffer_t *buffer = hb_buffer_create ();782783buffer->similar (*src);784785return buffer;786}787788/**789* hb_buffer_reset:790* @buffer: An #hb_buffer_t791*792* Resets the buffer to its initial status, as if it was just newly created793* with hb_buffer_create().794*795* Since: 0.9.2796**/797void798hb_buffer_reset (hb_buffer_t *buffer)799{800if (unlikely (hb_object_is_immutable (buffer)))801return;802803buffer->reset ();804}805806/**807* hb_buffer_get_empty:808*809* Fetches an empty #hb_buffer_t.810*811* Return value: (transfer full): The empty buffer812*813* Since: 0.9.2814**/815hb_buffer_t *816hb_buffer_get_empty ()817{818return const_cast<hb_buffer_t *> (&Null (hb_buffer_t));819}820821/**822* hb_buffer_reference: (skip)823* @buffer: An #hb_buffer_t824*825* Increases the reference count on @buffer by one. This prevents @buffer from826* being destroyed until a matching call to hb_buffer_destroy() is made.827*828* Return value: (transfer full):829* The referenced #hb_buffer_t.830*831* Since: 0.9.2832**/833hb_buffer_t *834hb_buffer_reference (hb_buffer_t *buffer)835{836return hb_object_reference (buffer);837}838839/**840* hb_buffer_destroy: (skip)841* @buffer: An #hb_buffer_t842*843* Deallocate the @buffer.844* Decreases the reference count on @buffer by one. If the result is zero, then845* @buffer and all associated resources are freed. See hb_buffer_reference().846*847* Since: 0.9.2848**/849void850hb_buffer_destroy (hb_buffer_t *buffer)851{852if (!hb_object_destroy (buffer)) return;853854hb_unicode_funcs_destroy (buffer->unicode);855856hb_free (buffer->info);857hb_free (buffer->pos);858#ifndef HB_NO_BUFFER_MESSAGE859if (buffer->message_destroy)860buffer->message_destroy (buffer->message_data);861#endif862863hb_free (buffer);864}865866/**867* hb_buffer_set_user_data: (skip)868* @buffer: An #hb_buffer_t869* @key: The user-data key870* @data: A pointer to the user data871* @destroy: (nullable): A callback to call when @data is not needed anymore872* @replace: Whether to replace an existing data with the same key873*874* Attaches a user-data key/data pair to the specified buffer.875*876* Return value: `true` if success, `false` otherwise877*878* Since: 0.9.2879**/880hb_bool_t881hb_buffer_set_user_data (hb_buffer_t *buffer,882hb_user_data_key_t *key,883void * data,884hb_destroy_func_t destroy,885hb_bool_t replace)886{887return hb_object_set_user_data (buffer, key, data, destroy, replace);888}889890/**891* hb_buffer_get_user_data: (skip)892* @buffer: An #hb_buffer_t893* @key: The user-data key to query894*895* Fetches the user data associated with the specified key,896* attached to the specified buffer.897*898* Return value: (transfer none): A pointer to the user data899*900* Since: 0.9.2901**/902void *903hb_buffer_get_user_data (const hb_buffer_t *buffer,904hb_user_data_key_t *key)905{906return hb_object_get_user_data (buffer, key);907}908909910/**911* hb_buffer_set_content_type:912* @buffer: An #hb_buffer_t913* @content_type: The type of buffer contents to set914*915* Sets the type of @buffer contents. Buffers are either empty, contain916* characters (before shaping), or contain glyphs (the result of shaping).917*918* You rarely need to call this function, since a number of other919* functions transition the content type for you. Namely:920*921* - A newly created buffer starts with content type922* %HB_BUFFER_CONTENT_TYPE_INVALID. Calling hb_buffer_reset(),923* hb_buffer_clear_contents(), as well as calling hb_buffer_set_length()924* with an argument of zero all set the buffer content type to invalid925* as well.926*927* - Calling hb_buffer_add_utf8(), hb_buffer_add_utf16(),928* hb_buffer_add_utf32(), hb_buffer_add_codepoints() and929* hb_buffer_add_latin1() expect that buffer is either empty and930* have a content type of invalid, or that buffer content type is931* %HB_BUFFER_CONTENT_TYPE_UNICODE, and they also set the content932* type to Unicode if they added anything to an empty buffer.933*934* - Finally hb_shape() and hb_shape_full() expect that the buffer935* is either empty and have content type of invalid, or that buffer936* content type is %HB_BUFFER_CONTENT_TYPE_UNICODE, and upon937* success they set the buffer content type to938* %HB_BUFFER_CONTENT_TYPE_GLYPHS.939*940* The above transitions are designed such that one can use a buffer941* in a loop of "reset : add-text : shape" without needing to ever942* modify the content type manually.943*944* Since: 0.9.5945**/946void947hb_buffer_set_content_type (hb_buffer_t *buffer,948hb_buffer_content_type_t content_type)949{950buffer->content_type = content_type;951}952953/**954* hb_buffer_get_content_type:955* @buffer: An #hb_buffer_t956*957* Fetches the type of @buffer contents. Buffers are either empty, contain958* characters (before shaping), or contain glyphs (the result of shaping).959*960* Return value:961* The type of @buffer contents962*963* Since: 0.9.5964**/965hb_buffer_content_type_t966hb_buffer_get_content_type (const hb_buffer_t *buffer)967{968return buffer->content_type;969}970971972/**973* hb_buffer_set_unicode_funcs:974* @buffer: An #hb_buffer_t975* @unicode_funcs: The Unicode-functions structure976*977* Sets the Unicode-functions structure of a buffer to978* @unicode_funcs.979*980* Since: 0.9.2981**/982void983hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,984hb_unicode_funcs_t *unicode_funcs)985{986if (unlikely (hb_object_is_immutable (buffer)))987return;988989if (!unicode_funcs)990unicode_funcs = hb_unicode_funcs_get_default ();991992hb_unicode_funcs_reference (unicode_funcs);993hb_unicode_funcs_destroy (buffer->unicode);994buffer->unicode = unicode_funcs;995}996997/**998* hb_buffer_get_unicode_funcs:999* @buffer: An #hb_buffer_t1000*1001* Fetches the Unicode-functions structure of a buffer.1002*1003* Return value: The Unicode-functions structure1004*1005* Since: 0.9.21006**/1007hb_unicode_funcs_t *1008hb_buffer_get_unicode_funcs (const hb_buffer_t *buffer)1009{1010return buffer->unicode;1011}10121013/**1014* hb_buffer_set_direction:1015* @buffer: An #hb_buffer_t1016* @direction: the #hb_direction_t of the @buffer1017*1018* Set the text flow direction of the buffer. No shaping can happen without1019* setting @buffer direction, and it controls the visual direction for the1020* output glyphs; for RTL direction the glyphs will be reversed. Many layout1021* features depend on the proper setting of the direction, for example,1022* reversing RTL text before shaping, then shaping with LTR direction is not1023* the same as keeping the text in logical order and shaping with RTL1024* direction.1025*1026* Since: 0.9.21027**/1028void1029hb_buffer_set_direction (hb_buffer_t *buffer,1030hb_direction_t direction)1031{1032if (unlikely (hb_object_is_immutable (buffer)))1033return;10341035buffer->props.direction = direction;1036}10371038/**1039* hb_buffer_get_direction:1040* @buffer: An #hb_buffer_t1041*1042* See hb_buffer_set_direction()1043*1044* Return value:1045* The direction of the @buffer.1046*1047* Since: 0.9.21048**/1049hb_direction_t1050hb_buffer_get_direction (const hb_buffer_t *buffer)1051{1052return buffer->props.direction;1053}10541055/**1056* hb_buffer_set_script:1057* @buffer: An #hb_buffer_t1058* @script: An #hb_script_t to set.1059*1060* Sets the script of @buffer to @script.1061*1062* Script is crucial for choosing the proper shaping behaviour for scripts that1063* require it (e.g. Arabic) and the which OpenType features defined in the font1064* to be applied.1065*1066* You can pass one of the predefined #hb_script_t values, or use1067* hb_script_from_string() or hb_script_from_iso15924_tag() to get the1068* corresponding script from an ISO 15924 script tag.1069*1070* Since: 0.9.21071**/1072void1073hb_buffer_set_script (hb_buffer_t *buffer,1074hb_script_t script)1075{1076if (unlikely (hb_object_is_immutable (buffer)))1077return;10781079buffer->props.script = script;1080}10811082/**1083* hb_buffer_get_script:1084* @buffer: An #hb_buffer_t1085*1086* Fetches the script of @buffer.1087*1088* Return value:1089* The #hb_script_t of the @buffer1090*1091* Since: 0.9.21092**/1093hb_script_t1094hb_buffer_get_script (const hb_buffer_t *buffer)1095{1096return buffer->props.script;1097}10981099/**1100* hb_buffer_set_language:1101* @buffer: An #hb_buffer_t1102* @language: An hb_language_t to set1103*1104* Sets the language of @buffer to @language.1105*1106* Languages are crucial for selecting which OpenType feature to apply to the1107* buffer which can result in applying language-specific behaviour. Languages1108* are orthogonal to the scripts, and though they are related, they are1109* different concepts and should not be confused with each other.1110*1111* Use hb_language_from_string() to convert from BCP 47 language tags to1112* #hb_language_t.1113*1114* Since: 0.9.21115**/1116void1117hb_buffer_set_language (hb_buffer_t *buffer,1118hb_language_t language)1119{1120if (unlikely (hb_object_is_immutable (buffer)))1121return;11221123buffer->props.language = language;1124}11251126/**1127* hb_buffer_get_language:1128* @buffer: An #hb_buffer_t1129*1130* See hb_buffer_set_language().1131*1132* Return value: (transfer none):1133* The #hb_language_t of the buffer. Must not be freed by the caller.1134*1135* Since: 0.9.21136**/1137hb_language_t1138hb_buffer_get_language (const hb_buffer_t *buffer)1139{1140return buffer->props.language;1141}11421143/**1144* hb_buffer_set_segment_properties:1145* @buffer: An #hb_buffer_t1146* @props: An #hb_segment_properties_t to use1147*1148* Sets the segment properties of the buffer, a shortcut for calling1149* hb_buffer_set_direction(), hb_buffer_set_script() and1150* hb_buffer_set_language() individually.1151*1152* Since: 0.9.71153**/1154void1155hb_buffer_set_segment_properties (hb_buffer_t *buffer,1156const hb_segment_properties_t *props)1157{1158if (unlikely (hb_object_is_immutable (buffer)))1159return;11601161buffer->props = *props;1162}11631164/**1165* hb_buffer_get_segment_properties:1166* @buffer: An #hb_buffer_t1167* @props: (out): The output #hb_segment_properties_t1168*1169* Sets @props to the #hb_segment_properties_t of @buffer.1170*1171* Since: 0.9.71172**/1173void1174hb_buffer_get_segment_properties (const hb_buffer_t *buffer,1175hb_segment_properties_t *props)1176{1177*props = buffer->props;1178}117911801181/**1182* hb_buffer_set_flags:1183* @buffer: An #hb_buffer_t1184* @flags: The buffer flags to set1185*1186* Sets @buffer flags to @flags. See #hb_buffer_flags_t.1187*1188* Since: 0.9.71189**/1190void1191hb_buffer_set_flags (hb_buffer_t *buffer,1192hb_buffer_flags_t flags)1193{1194if (unlikely (hb_object_is_immutable (buffer)))1195return;11961197buffer->flags = flags;1198}11991200/**1201* hb_buffer_get_flags:1202* @buffer: An #hb_buffer_t1203*1204* Fetches the #hb_buffer_flags_t of @buffer.1205*1206* Return value:1207* The @buffer flags1208*1209* Since: 0.9.71210**/1211hb_buffer_flags_t1212hb_buffer_get_flags (const hb_buffer_t *buffer)1213{1214return buffer->flags;1215}12161217/**1218* hb_buffer_set_cluster_level:1219* @buffer: An #hb_buffer_t1220* @cluster_level: The cluster level to set on the buffer1221*1222* Sets the cluster level of a buffer. The #hb_buffer_cluster_level_t1223* dictates one aspect of how HarfBuzz will treat non-base characters1224* during shaping.1225*1226* Since: 0.9.421227**/1228void1229hb_buffer_set_cluster_level (hb_buffer_t *buffer,1230hb_buffer_cluster_level_t cluster_level)1231{1232if (unlikely (hb_object_is_immutable (buffer)))1233return;12341235buffer->cluster_level = cluster_level;1236}12371238/**1239* hb_buffer_get_cluster_level:1240* @buffer: An #hb_buffer_t1241*1242* Fetches the cluster level of a buffer. The #hb_buffer_cluster_level_t1243* dictates one aspect of how HarfBuzz will treat non-base characters1244* during shaping.1245*1246* Return value: The cluster level of @buffer1247*1248* Since: 0.9.421249**/1250hb_buffer_cluster_level_t1251hb_buffer_get_cluster_level (const hb_buffer_t *buffer)1252{1253return buffer->cluster_level;1254}125512561257/**1258* hb_buffer_set_replacement_codepoint:1259* @buffer: An #hb_buffer_t1260* @replacement: the replacement #hb_codepoint_t1261*1262* Sets the #hb_codepoint_t that replaces invalid entries for a given encoding1263* when adding text to @buffer.1264*1265* Default is #HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT.1266*1267* Since: 0.9.311268**/1269void1270hb_buffer_set_replacement_codepoint (hb_buffer_t *buffer,1271hb_codepoint_t replacement)1272{1273if (unlikely (hb_object_is_immutable (buffer)))1274return;12751276buffer->replacement = replacement;1277}12781279/**1280* hb_buffer_get_replacement_codepoint:1281* @buffer: An #hb_buffer_t1282*1283* Fetches the #hb_codepoint_t that replaces invalid entries for a given encoding1284* when adding text to @buffer.1285*1286* Return value:1287* The @buffer replacement #hb_codepoint_t1288*1289* Since: 0.9.311290**/1291hb_codepoint_t1292hb_buffer_get_replacement_codepoint (const hb_buffer_t *buffer)1293{1294return buffer->replacement;1295}129612971298/**1299* hb_buffer_set_invisible_glyph:1300* @buffer: An #hb_buffer_t1301* @invisible: the invisible #hb_codepoint_t1302*1303* Sets the #hb_codepoint_t that replaces invisible characters in1304* the shaping result. If set to zero (default), the glyph for the1305* U+0020 SPACE character is used. Otherwise, this value is used1306* verbatim.1307*1308* Since: 2.0.01309**/1310void1311hb_buffer_set_invisible_glyph (hb_buffer_t *buffer,1312hb_codepoint_t invisible)1313{1314if (unlikely (hb_object_is_immutable (buffer)))1315return;13161317buffer->invisible = invisible;1318}13191320/**1321* hb_buffer_get_invisible_glyph:1322* @buffer: An #hb_buffer_t1323*1324* See hb_buffer_set_invisible_glyph().1325*1326* Return value:1327* The @buffer invisible #hb_codepoint_t1328*1329* Since: 2.0.01330**/1331hb_codepoint_t1332hb_buffer_get_invisible_glyph (const hb_buffer_t *buffer)1333{1334return buffer->invisible;1335}13361337/**1338* hb_buffer_set_not_found_glyph:1339* @buffer: An #hb_buffer_t1340* @not_found: the not-found #hb_codepoint_t1341*1342* Sets the #hb_codepoint_t that replaces characters not found in1343* the font during shaping.1344*1345* The not-found glyph defaults to zero, sometimes known as the1346* ".notdef" glyph. This API allows for differentiating the two.1347*1348* Since: 3.1.01349**/1350void1351hb_buffer_set_not_found_glyph (hb_buffer_t *buffer,1352hb_codepoint_t not_found)1353{1354if (unlikely (hb_object_is_immutable (buffer)))1355return;13561357buffer->not_found = not_found;1358}13591360/**1361* hb_buffer_get_not_found_glyph:1362* @buffer: An #hb_buffer_t1363*1364* See hb_buffer_set_not_found_glyph().1365*1366* Return value:1367* The @buffer not-found #hb_codepoint_t1368*1369* Since: 3.1.01370**/1371hb_codepoint_t1372hb_buffer_get_not_found_glyph (const hb_buffer_t *buffer)1373{1374return buffer->not_found;1375}13761377/**1378* hb_buffer_set_not_found_variation_selector_glyph:1379* @buffer: An #hb_buffer_t1380* @not_found_variation_selector: the not-found-variation-selector #hb_codepoint_t1381*1382* Sets the #hb_codepoint_t that replaces variation-selector characters not resolved1383* in the font during shaping.1384*1385* The not-found-variation-selector glyph defaults to #HB_CODEPOINT_INVALID,1386* in which case an unresolved variation-selector will be removed from the glyph1387* string during shaping. This API allows for changing that and retaining a glyph,1388* such that the situation can be detected by the client and handled accordingly1389* (e.g. by using a different font).1390*1391* Since: 10.0.01392**/1393void1394hb_buffer_set_not_found_variation_selector_glyph (hb_buffer_t *buffer,1395hb_codepoint_t not_found_variation_selector)1396{1397buffer->not_found_variation_selector = not_found_variation_selector;1398}13991400/**1401* hb_buffer_get_not_found_variation_selector_glyph:1402* @buffer: An #hb_buffer_t1403*1404* See hb_buffer_set_not_found_variation_selector_glyph().1405*1406* Return value:1407* The @buffer not-found-variation-selector #hb_codepoint_t1408*1409* Since: 10.0.01410**/1411hb_codepoint_t1412hb_buffer_get_not_found_variation_selector_glyph (const hb_buffer_t *buffer)1413{1414return buffer->not_found_variation_selector;1415}14161417/**1418* hb_buffer_set_random_state:1419* @buffer: An #hb_buffer_t1420* @state: the new random state1421*1422* Sets the random state of the buffer. The state changes1423* every time a glyph uses randomness (eg. the `rand`1424* OpenType feature). This function together with1425* hb_buffer_get_random_state() allow for transferring1426* the current random state to a subsequent buffer, to1427* get better randomness distribution.1428*1429* Defaults to 1 and when buffer contents are cleared.1430* A value of 0 disables randomness during shaping.1431*1432* Since: 8.4.01433**/1434void1435hb_buffer_set_random_state (hb_buffer_t *buffer,1436unsigned state)1437{1438if (unlikely (hb_object_is_immutable (buffer)))1439return;14401441buffer->random_state = state;1442}14431444/**1445* hb_buffer_get_random_state:1446* @buffer: An #hb_buffer_t1447*1448* See hb_buffer_set_random_state().1449*1450* Return value:1451* The @buffer random state1452*1453* Since: 8.4.01454**/1455unsigned1456hb_buffer_get_random_state (const hb_buffer_t *buffer)1457{1458return buffer->random_state;1459}14601461/**1462* hb_buffer_clear_contents:1463* @buffer: An #hb_buffer_t1464*1465* Similar to hb_buffer_reset(), but does not clear the Unicode functions and1466* the replacement code point.1467*1468* Since: 0.9.111469**/1470void1471hb_buffer_clear_contents (hb_buffer_t *buffer)1472{1473if (unlikely (hb_object_is_immutable (buffer)))1474return;14751476buffer->clear ();1477}14781479/**1480* hb_buffer_pre_allocate:1481* @buffer: An #hb_buffer_t1482* @size: Number of items to pre allocate.1483*1484* Pre allocates memory for @buffer to fit at least @size number of items.1485*1486* Return value:1487* `true` if @buffer memory allocation succeeded, `false` otherwise1488*1489* Since: 0.9.21490**/1491hb_bool_t1492hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)1493{1494return buffer->ensure (size);1495}14961497/**1498* hb_buffer_allocation_successful:1499* @buffer: An #hb_buffer_t1500*1501* Check if allocating memory for the buffer succeeded.1502*1503* Return value:1504* `true` if @buffer memory allocation succeeded, `false` otherwise.1505*1506* Since: 0.9.21507**/1508hb_bool_t1509hb_buffer_allocation_successful (hb_buffer_t *buffer)1510{1511return buffer->successful;1512}15131514/**1515* hb_buffer_add:1516* @buffer: An #hb_buffer_t1517* @codepoint: A Unicode code point.1518* @cluster: The cluster value of @codepoint.1519*1520* Appends a character with the Unicode value of @codepoint to @buffer, and1521* gives it the initial cluster value of @cluster. Clusters can be any thing1522* the client wants, they are usually used to refer to the index of the1523* character in the input text stream and are output in1524* #hb_glyph_info_t.cluster field.1525*1526* This function does not check the validity of @codepoint, it is up to the1527* caller to ensure it is a valid Unicode code point.1528*1529* Since: 0.9.71530**/1531void1532hb_buffer_add (hb_buffer_t *buffer,1533hb_codepoint_t codepoint,1534unsigned int cluster)1535{1536buffer->add (codepoint, cluster);1537buffer->clear_context (1);1538}15391540/**1541* hb_buffer_set_length:1542* @buffer: An #hb_buffer_t1543* @length: The new length of @buffer1544*1545* Similar to hb_buffer_pre_allocate(), but clears any new items added at the1546* end.1547*1548* Return value:1549* `true` if @buffer memory allocation succeeded, `false` otherwise.1550*1551* Since: 0.9.21552**/1553hb_bool_t1554hb_buffer_set_length (hb_buffer_t *buffer,1555unsigned int length)1556{1557if (unlikely (hb_object_is_immutable (buffer)))1558return length == 0;15591560if (unlikely (!buffer->ensure (length)))1561return false;15621563/* Wipe the new space */1564if (length > buffer->len) {1565hb_memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));1566if (buffer->have_positions)1567hb_memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));1568}15691570buffer->len = length;15711572if (!length)1573{1574buffer->content_type = HB_BUFFER_CONTENT_TYPE_INVALID;1575buffer->clear_context (0);1576}1577buffer->clear_context (1);15781579return true;1580}15811582/**1583* hb_buffer_get_length:1584* @buffer: An #hb_buffer_t1585*1586* Returns the number of items in the buffer.1587*1588* Return value:1589* The @buffer length.1590* The value valid as long as buffer has not been modified.1591*1592* Since: 0.9.21593**/1594unsigned int1595hb_buffer_get_length (const hb_buffer_t *buffer)1596{1597return buffer->len;1598}15991600/**1601* hb_buffer_get_glyph_infos:1602* @buffer: An #hb_buffer_t1603* @length: (out): The output-array length.1604*1605* Returns @buffer glyph information array. Returned pointer1606* is valid as long as @buffer contents are not modified.1607*1608* Return value: (transfer none) (array length=length):1609* The @buffer glyph information array.1610* The value valid as long as buffer has not been modified.1611*1612* Since: 0.9.21613**/1614hb_glyph_info_t *1615hb_buffer_get_glyph_infos (hb_buffer_t *buffer,1616unsigned int *length)1617{1618if (length)1619*length = buffer->len;16201621return (hb_glyph_info_t *) buffer->info;1622}16231624/**1625* hb_buffer_get_glyph_positions:1626* @buffer: An #hb_buffer_t1627* @length: (out): The output length1628*1629* Returns @buffer glyph position array. Returned pointer1630* is valid as long as @buffer contents are not modified.1631*1632* If buffer did not have positions before, the positions will be1633* initialized to zeros, unless this function is called from1634* within a buffer message callback (see hb_buffer_set_message_func()),1635* in which case `NULL` is returned.1636*1637* Return value: (transfer none) (array length=length):1638* The @buffer glyph position array.1639* The value valid as long as buffer has not been modified.1640*1641* Since: 0.9.21642**/1643hb_glyph_position_t *1644hb_buffer_get_glyph_positions (hb_buffer_t *buffer,1645unsigned int *length)1646{1647if (length)1648*length = buffer->len;16491650if (!buffer->have_positions)1651{1652if (unlikely (buffer->message_depth))1653return nullptr;16541655buffer->clear_positions ();1656}16571658return (hb_glyph_position_t *) buffer->pos;1659}16601661/**1662* hb_buffer_has_positions:1663* @buffer: an #hb_buffer_t.1664*1665* Returns whether @buffer has glyph position data.1666* A buffer gains position data when hb_buffer_get_glyph_positions() is called on it,1667* and cleared of position data when hb_buffer_clear_contents() is called.1668*1669* Return value:1670* `true` if the @buffer has position array, `false` otherwise.1671*1672* Since: 2.7.31673**/1674HB_EXTERN hb_bool_t1675hb_buffer_has_positions (hb_buffer_t *buffer)1676{1677return buffer->have_positions;1678}16791680/**1681* hb_glyph_info_get_glyph_flags:1682* @info: a #hb_glyph_info_t1683*1684* Returns glyph flags encoded within a #hb_glyph_info_t.1685*1686* Return value:1687* The #hb_glyph_flags_t encoded within @info1688*1689* Since: 1.5.01690**/1691hb_glyph_flags_t1692(hb_glyph_info_get_glyph_flags) (const hb_glyph_info_t *info)1693{1694return hb_glyph_info_get_glyph_flags (info);1695}16961697/**1698* hb_buffer_reverse:1699* @buffer: An #hb_buffer_t1700*1701* Reverses buffer contents.1702*1703* Since: 0.9.21704**/1705void1706hb_buffer_reverse (hb_buffer_t *buffer)1707{1708buffer->reverse ();1709}17101711/**1712* hb_buffer_reverse_range:1713* @buffer: An #hb_buffer_t1714* @start: start index1715* @end: end index1716*1717* Reverses buffer contents between @start and @end.1718*1719* Since: 0.9.411720**/1721void1722hb_buffer_reverse_range (hb_buffer_t *buffer,1723unsigned int start, unsigned int end)1724{1725buffer->reverse_range (start, end);1726}17271728/**1729* hb_buffer_reverse_clusters:1730* @buffer: An #hb_buffer_t1731*1732* Reverses buffer clusters. That is, the buffer contents are1733* reversed, then each cluster (consecutive items having the1734* same cluster number) are reversed again.1735*1736* Since: 0.9.21737**/1738void1739hb_buffer_reverse_clusters (hb_buffer_t *buffer)1740{1741buffer->reverse_clusters ();1742}17431744/**1745* hb_buffer_guess_segment_properties:1746* @buffer: An #hb_buffer_t1747*1748* Sets unset buffer segment properties based on buffer Unicode1749* contents. If buffer is not empty, it must have content type1750* #HB_BUFFER_CONTENT_TYPE_UNICODE.1751*1752* If buffer script is not set (ie. is #HB_SCRIPT_INVALID), it1753* will be set to the Unicode script of the first character in1754* the buffer that has a script other than #HB_SCRIPT_COMMON,1755* #HB_SCRIPT_INHERITED, and #HB_SCRIPT_UNKNOWN.1756*1757* Next, if buffer direction is not set (ie. is #HB_DIRECTION_INVALID),1758* it will be set to the natural horizontal direction of the1759* buffer script as returned by hb_script_get_horizontal_direction().1760* If hb_script_get_horizontal_direction() returns #HB_DIRECTION_INVALID,1761* then #HB_DIRECTION_LTR is used.1762*1763* Finally, if buffer language is not set (ie. is #HB_LANGUAGE_INVALID),1764* it will be set to the process's default language as returned by1765* hb_language_get_default(). This may change in the future by1766* taking buffer script into consideration when choosing a language.1767* Note that hb_language_get_default() is NOT threadsafe the first time1768* it is called. See documentation for that function for details.1769*1770* Since: 0.9.71771**/1772void1773hb_buffer_guess_segment_properties (hb_buffer_t *buffer)1774{1775buffer->guess_segment_properties ();1776}17771778template <typename utf_t>1779static inline void1780hb_buffer_add_utf (hb_buffer_t *buffer,1781const typename utf_t::codepoint_t *text,1782int text_length,1783unsigned int item_offset,1784int item_length)1785{1786typedef typename utf_t::codepoint_t T;1787const hb_codepoint_t replacement = buffer->replacement;17881789buffer->assert_unicode ();17901791if (unlikely (hb_object_is_immutable (buffer)))1792return;17931794if (text_length == -1)1795text_length = utf_t::strlen (text);17961797if (item_length == -1)1798item_length = text_length - item_offset;17991800if (unlikely (item_length < 0 ||1801item_length > INT_MAX / 8 ||1802!buffer->ensure (buffer->len + item_length * sizeof (T) / 4)))1803return;18041805/* If buffer is empty and pre-context provided, install it.1806* This check is written this way, to make sure people can1807* provide pre-context in one add_utf() call, then provide1808* text in a follow-up call. See:1809*1810* https://bugzilla.mozilla.org/show_bug.cgi?id=801410#c131811*/1812if (!buffer->len && item_offset > 0)1813{1814/* Add pre-context */1815buffer->clear_context (0);1816const T *prev = text + item_offset;1817const T *start = text;1818while (start < prev && buffer->context_len[0] < buffer->CONTEXT_LENGTH)1819{1820hb_codepoint_t u;1821prev = utf_t::prev (prev, start, &u, replacement);1822buffer->context[0][buffer->context_len[0]++] = u;1823}1824}18251826const T *next = text + item_offset;1827const T *end = next + item_length;1828while (next < end)1829{1830hb_codepoint_t u;1831const T *old_next = next;1832next = utf_t::next (next, end, &u, replacement);1833buffer->add (u, old_next - (const T *) text);1834}18351836/* Add post-context */1837buffer->clear_context (1);1838end = text + text_length;1839while (next < end && buffer->context_len[1] < buffer->CONTEXT_LENGTH)1840{1841hb_codepoint_t u;1842next = utf_t::next (next, end, &u, replacement);1843buffer->context[1][buffer->context_len[1]++] = u;1844}18451846buffer->content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;1847}18481849/**1850* hb_buffer_add_utf8:1851* @buffer: An #hb_buffer_t1852* @text: (array length=text_length) (element-type uint8_t): An array of UTF-81853* characters to append.1854* @text_length: The length of the @text, or -1 if it is `NULL` terminated.1855* @item_offset: The offset of the first character to add to the @buffer.1856* @item_length: The number of characters to add to the @buffer, or -1 for the1857* end of @text (assuming it is `NULL` terminated).1858*1859* See hb_buffer_add_codepoints().1860*1861* Replaces invalid UTF-8 characters with the @buffer replacement code point,1862* see hb_buffer_set_replacement_codepoint().1863*1864* Since: 0.9.21865**/1866void1867hb_buffer_add_utf8 (hb_buffer_t *buffer,1868const char *text,1869int text_length,1870unsigned int item_offset,1871int item_length)1872{1873hb_buffer_add_utf<hb_utf8_t> (buffer, (const uint8_t *) text, text_length, item_offset, item_length);1874}18751876/**1877* hb_buffer_add_utf16:1878* @buffer: An #hb_buffer_t1879* @text: (array length=text_length): An array of UTF-16 characters to append1880* @text_length: The length of the @text, or -1 if it is `NULL` terminated1881* @item_offset: The offset of the first character to add to the @buffer1882* @item_length: The number of characters to add to the @buffer, or -1 for the1883* end of @text (assuming it is `NULL` terminated)1884*1885* See hb_buffer_add_codepoints().1886*1887* Replaces invalid UTF-16 characters with the @buffer replacement code point,1888* see hb_buffer_set_replacement_codepoint().1889*1890* Since: 0.9.21891**/1892void1893hb_buffer_add_utf16 (hb_buffer_t *buffer,1894const uint16_t *text,1895int text_length,1896unsigned int item_offset,1897int item_length)1898{1899hb_buffer_add_utf<hb_utf16_t> (buffer, text, text_length, item_offset, item_length);1900}19011902/**1903* hb_buffer_add_utf32:1904* @buffer: An #hb_buffer_t1905* @text: (array length=text_length): An array of UTF-32 characters to append1906* @text_length: The length of the @text, or -1 if it is `NULL` terminated1907* @item_offset: The offset of the first character to add to the @buffer1908* @item_length: The number of characters to add to the @buffer, or -1 for the1909* end of @text (assuming it is `NULL` terminated)1910*1911* See hb_buffer_add_codepoints().1912*1913* Replaces invalid UTF-32 characters with the @buffer replacement code point,1914* see hb_buffer_set_replacement_codepoint().1915*1916* Since: 0.9.21917**/1918void1919hb_buffer_add_utf32 (hb_buffer_t *buffer,1920const uint32_t *text,1921int text_length,1922unsigned int item_offset,1923int item_length)1924{1925hb_buffer_add_utf<hb_utf32_t> (buffer, text, text_length, item_offset, item_length);1926}19271928/**1929* hb_buffer_add_latin1:1930* @buffer: An #hb_buffer_t1931* @text: (array length=text_length) (element-type uint8_t): an array of UTF-81932* characters to append1933* @text_length: the length of the @text, or -1 if it is `NULL` terminated1934* @item_offset: the offset of the first character to add to the @buffer1935* @item_length: the number of characters to add to the @buffer, or -1 for the1936* end of @text (assuming it is `NULL` terminated)1937*1938* Similar to hb_buffer_add_codepoints(), but allows only access to first 2561939* Unicode code points that can fit in 8-bit strings.1940*1941* <note>Has nothing to do with non-Unicode Latin-1 encoding.</note>1942*1943* Since: 0.9.391944**/1945void1946hb_buffer_add_latin1 (hb_buffer_t *buffer,1947const uint8_t *text,1948int text_length,1949unsigned int item_offset,1950int item_length)1951{1952hb_buffer_add_utf<hb_latin1_t> (buffer, text, text_length, item_offset, item_length);1953}19541955/**1956* hb_buffer_add_codepoints:1957* @buffer: a #hb_buffer_t to append characters to.1958* @text: (array length=text_length): an array of Unicode code points to append.1959* @text_length: the length of the @text, or -1 if it is `NULL` terminated.1960* @item_offset: the offset of the first code point to add to the @buffer.1961* @item_length: the number of code points to add to the @buffer, or -1 for the1962* end of @text (assuming it is `NULL` terminated).1963*1964* Appends characters from @text array to @buffer. The @item_offset is the1965* position of the first character from @text that will be appended, and1966* @item_length is the number of character. When shaping part of a larger text1967* (e.g. a run of text from a paragraph), instead of passing just the substring1968* corresponding to the run, it is preferable to pass the whole1969* paragraph and specify the run start and length as @item_offset and1970* @item_length, respectively, to give HarfBuzz the full context to be able,1971* for example, to do cross-run Arabic shaping or properly handle combining1972* marks at stat of run.1973*1974* This function does not check the validity of @text, it is up to the caller1975* to ensure it contains a valid Unicode scalar values. In contrast,1976* hb_buffer_add_utf32() can be used that takes similar input but performs1977* sanity-check on the input.1978*1979* Since: 0.9.311980**/1981void1982hb_buffer_add_codepoints (hb_buffer_t *buffer,1983const hb_codepoint_t *text,1984int text_length,1985unsigned int item_offset,1986int item_length)1987{1988hb_buffer_add_utf<hb_utf32_novalidate_t> (buffer, text, text_length, item_offset, item_length);1989}199019911992/**1993* hb_buffer_append:1994* @buffer: An #hb_buffer_t1995* @source: source #hb_buffer_t1996* @start: start index into source buffer to copy. Use 0 to copy from start of buffer.1997* @end: end index into source buffer to copy. Use @UINT_MAX (or ((unsigned int) -1)) to copy to end of buffer.1998*1999* Append (part of) contents of another buffer to this buffer.2000*2001* Since: 1.5.02002**/2003HB_EXTERN void2004hb_buffer_append (hb_buffer_t *buffer,2005const hb_buffer_t *source,2006unsigned int start,2007unsigned int end)2008{2009assert (!buffer->have_output && !source->have_output);2010assert (buffer->have_positions == source->have_positions ||2011!buffer->len || !source->len);2012assert (buffer->content_type == source->content_type ||2013!buffer->len || !source->len);20142015if (end > source->len)2016end = source->len;2017if (start > end)2018start = end;2019if (start == end)2020return;20212022if (buffer->len + (end - start) < buffer->len) /* Overflows. */2023{2024buffer->successful = false;2025return;2026}20272028unsigned int orig_len = buffer->len;2029hb_buffer_set_length (buffer, buffer->len + (end - start));2030if (unlikely (!buffer->successful))2031return;20322033if (!orig_len)2034buffer->content_type = source->content_type;2035if (!buffer->have_positions && source->have_positions)2036buffer->clear_positions ();20372038hb_segment_properties_overlay (&buffer->props, &source->props);20392040hb_memcpy (buffer->info + orig_len, source->info + start, (end - start) * sizeof (buffer->info[0]));2041if (buffer->have_positions)2042hb_memcpy (buffer->pos + orig_len, source->pos + start, (end - start) * sizeof (buffer->pos[0]));20432044if (source->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE)2045{2046/* See similar logic in add_utf. */20472048/* pre-context */2049if (!orig_len && start + source->context_len[0] > 0)2050{2051buffer->clear_context (0);2052while (start > 0 && buffer->context_len[0] < buffer->CONTEXT_LENGTH)2053buffer->context[0][buffer->context_len[0]++] = source->info[--start].codepoint;2054for (auto i = 0u; i < source->context_len[0] && buffer->context_len[0] < buffer->CONTEXT_LENGTH; i++)2055buffer->context[0][buffer->context_len[0]++] = source->context[0][i];2056}20572058/* post-context */2059buffer->clear_context (1);2060while (end < source->len && buffer->context_len[1] < buffer->CONTEXT_LENGTH)2061buffer->context[1][buffer->context_len[1]++] = source->info[end++].codepoint;2062for (auto i = 0u; i < source->context_len[1] && buffer->context_len[1] < buffer->CONTEXT_LENGTH; i++)2063buffer->context[1][buffer->context_len[1]++] = source->context[1][i];2064}2065}206620672068static int2069compare_info_codepoint (const hb_glyph_info_t *pa,2070const hb_glyph_info_t *pb)2071{2072return (int) pb->codepoint - (int) pa->codepoint;2073}20742075static inline void2076normalize_glyphs_cluster (hb_buffer_t *buffer,2077unsigned int start,2078unsigned int end,2079bool backward)2080{2081hb_glyph_position_t *pos = buffer->pos;20822083/* Total cluster advance */2084hb_position_t total_x_advance = 0, total_y_advance = 0;2085for (unsigned int i = start; i < end; i++)2086{2087total_x_advance += pos[i].x_advance;2088total_y_advance += pos[i].y_advance;2089}20902091hb_position_t x_advance = 0, y_advance = 0;2092for (unsigned int i = start; i < end; i++)2093{2094pos[i].x_offset += x_advance;2095pos[i].y_offset += y_advance;20962097x_advance += pos[i].x_advance;2098y_advance += pos[i].y_advance;20992100pos[i].x_advance = 0;2101pos[i].y_advance = 0;2102}21032104if (backward)2105{2106/* Transfer all cluster advance to the last glyph. */2107pos[end - 1].x_advance = total_x_advance;2108pos[end - 1].y_advance = total_y_advance;21092110hb_stable_sort (buffer->info + start, end - start - 1, compare_info_codepoint, buffer->pos + start);2111} else {2112/* Transfer all cluster advance to the first glyph. */2113pos[start].x_advance += total_x_advance;2114pos[start].y_advance += total_y_advance;2115for (unsigned int i = start + 1; i < end; i++) {2116pos[i].x_offset -= total_x_advance;2117pos[i].y_offset -= total_y_advance;2118}2119hb_stable_sort (buffer->info + start + 1, end - start - 1, compare_info_codepoint, buffer->pos + start + 1);2120}2121}21222123/**2124* hb_buffer_normalize_glyphs:2125* @buffer: An #hb_buffer_t2126*2127* Reorders a glyph buffer to have canonical in-cluster glyph order / position.2128* The resulting clusters should behave identical to pre-reordering clusters.2129*2130* <note>This has nothing to do with Unicode normalization.</note>2131*2132* Since: 0.9.22133**/2134void2135hb_buffer_normalize_glyphs (hb_buffer_t *buffer)2136{2137assert (buffer->have_positions);21382139buffer->assert_glyphs ();21402141bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);21422143foreach_cluster (buffer, start, end)2144normalize_glyphs_cluster (buffer, start, end, backward);2145}21462147void2148hb_buffer_t::sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *))2149{2150assert (!have_positions);2151for (unsigned int i = start + 1; i < end; i++)2152{2153unsigned int j = i;2154while (j > start && compar (&info[j - 1], &info[i]) > 0)2155j--;2156if (i == j)2157continue;2158/* Move item i to occupy place for item j, shift what's in between. */2159merge_clusters (j, i + 1);2160{2161hb_glyph_info_t t = info[i];2162memmove (&info[j + 1], &info[j], (i - j) * sizeof (hb_glyph_info_t));2163info[j] = t;2164}2165}2166}216721682169/*2170* Comparing buffers.2171*/21722173/**2174* hb_buffer_diff:2175* @buffer: a buffer.2176* @reference: other buffer to compare to.2177* @dottedcircle_glyph: glyph id of U+25CC DOTTED CIRCLE, or (hb_codepoint_t) -1.2178* @position_fuzz: allowed absolute difference in position values.2179*2180* If dottedcircle_glyph is (hb_codepoint_t) -1 then #HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT2181* and #HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT are never returned. This should be used by most2182* callers if just comparing two buffers is needed.2183*2184* Since: 1.5.02185**/2186hb_buffer_diff_flags_t2187hb_buffer_diff (hb_buffer_t *buffer,2188hb_buffer_t *reference,2189hb_codepoint_t dottedcircle_glyph,2190unsigned int position_fuzz)2191{2192if (buffer->content_type != reference->content_type && buffer->len && reference->len)2193return HB_BUFFER_DIFF_FLAG_CONTENT_TYPE_MISMATCH;21942195hb_buffer_diff_flags_t result = HB_BUFFER_DIFF_FLAG_EQUAL;2196bool contains = dottedcircle_glyph != (hb_codepoint_t) -1;21972198unsigned int count = reference->len;21992200if (buffer->len != count)2201{2202/*2203* we can't compare glyph-by-glyph, but we do want to know if there2204* are .notdef or dottedcircle glyphs present in the reference buffer2205*/2206const hb_glyph_info_t *info = reference->info;2207unsigned int i;2208for (i = 0; i < count; i++)2209{2210if (contains && info[i].codepoint == dottedcircle_glyph)2211result |= HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT;2212if (contains && info[i].codepoint == 0)2213result |= HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT;2214}2215result |= HB_BUFFER_DIFF_FLAG_LENGTH_MISMATCH;2216return hb_buffer_diff_flags_t (result);2217}22182219if (!count)2220return hb_buffer_diff_flags_t (result);22212222const hb_glyph_info_t *buf_info = buffer->info;2223const hb_glyph_info_t *ref_info = reference->info;2224for (unsigned int i = 0; i < count; i++)2225{2226if (buf_info->codepoint != ref_info->codepoint)2227result |= HB_BUFFER_DIFF_FLAG_CODEPOINT_MISMATCH;2228if (buf_info->cluster != ref_info->cluster)2229result |= HB_BUFFER_DIFF_FLAG_CLUSTER_MISMATCH;2230if ((buf_info->mask ^ ref_info->mask) & HB_GLYPH_FLAG_DEFINED)2231result |= HB_BUFFER_DIFF_FLAG_GLYPH_FLAGS_MISMATCH;2232if (contains && ref_info->codepoint == dottedcircle_glyph)2233result |= HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT;2234if (contains && ref_info->codepoint == 0)2235result |= HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT;2236buf_info++;2237ref_info++;2238}22392240if (buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS)2241{2242assert (buffer->have_positions);2243const hb_glyph_position_t *buf_pos = buffer->pos;2244const hb_glyph_position_t *ref_pos = reference->pos;2245for (unsigned int i = 0; i < count; i++)2246{2247if ((unsigned int) abs (buf_pos->x_advance - ref_pos->x_advance) > position_fuzz ||2248(unsigned int) abs (buf_pos->y_advance - ref_pos->y_advance) > position_fuzz ||2249(unsigned int) abs (buf_pos->x_offset - ref_pos->x_offset) > position_fuzz ||2250(unsigned int) abs (buf_pos->y_offset - ref_pos->y_offset) > position_fuzz)2251{2252result |= HB_BUFFER_DIFF_FLAG_POSITION_MISMATCH;2253break;2254}2255buf_pos++;2256ref_pos++;2257}2258}22592260return result;2261}226222632264/*2265* Debugging.2266*/22672268#ifndef HB_NO_BUFFER_MESSAGE2269/**2270* hb_buffer_set_message_func:2271* @buffer: An #hb_buffer_t2272* @func: (closure user_data) (destroy destroy) (scope notified): Callback function2273* @user_data: (nullable): Data to pass to @func2274* @destroy: (nullable): The function to call when @user_data is not needed anymore2275*2276* Sets the implementation function for #hb_buffer_message_func_t.2277*2278* Since: 1.1.32279**/2280void2281hb_buffer_set_message_func (hb_buffer_t *buffer,2282hb_buffer_message_func_t func,2283void *user_data, hb_destroy_func_t destroy)2284{2285if (unlikely (hb_object_is_immutable (buffer)))2286{2287if (destroy)2288destroy (user_data);2289return;2290}22912292if (buffer->message_destroy)2293buffer->message_destroy (buffer->message_data);22942295if (func) {2296buffer->message_func = func;2297buffer->message_data = user_data;2298buffer->message_destroy = destroy;2299} else {2300buffer->message_func = nullptr;2301buffer->message_data = nullptr;2302buffer->message_destroy = nullptr;2303}2304}2305bool2306hb_buffer_t::message_impl (hb_font_t *font, const char *fmt, va_list ap)2307{2308assert (!have_output || (out_info == info && out_len == idx));23092310message_depth++;23112312char buf[100];2313vsnprintf (buf, sizeof (buf), fmt, ap);2314bool ret = (bool) this->message_func (this, font, buf, this->message_data);23152316message_depth--;23172318return ret;2319}2320#endif232123222323