[cff] Replace byte_str_t with hb_bytes_t use

This commit is contained in:
Behdad Esfahbod 2022-05-09 16:09:56 -06:00
parent fe1d85a55a
commit 8aa54aaca2
11 changed files with 41 additions and 46 deletions

View File

@ -277,15 +277,10 @@ struct UnsizedByteStr : UnsizedArrayOf <HBUINT8>
/* Defining null_size allows a Null object may be created. Should be safe because: /* Defining null_size allows a Null object may be created. Should be safe because:
* A descendent struct Dict uses a Null pointer to indicate a missing table, * A descendent struct Dict uses a Null pointer to indicate a missing table,
* checked before access. * checked before access.
* byte_str_t, a wrapper struct pairing a byte pointer along with its length, always
* checks the length before access. A Null pointer is used as the initial pointer
* along with zero length by the default ctor.
*/ */
DEFINE_SIZE_MIN(0); DEFINE_SIZE_MIN(0);
}; };
using byte_str_t = hb_ubytes_t;
/* A byte string associated with the current offset and an error condition */ /* A byte string associated with the current offset and an error condition */
struct byte_str_ref_t struct byte_str_ref_t
{ {
@ -293,17 +288,17 @@ struct byte_str_ref_t
void init () void init ()
{ {
str = byte_str_t (); str = hb_ubytes_t ();
offset = 0; offset = 0;
error = false; error = false;
} }
void fini () {} void fini () {}
byte_str_ref_t (const byte_str_t &str_, unsigned int offset_ = 0) byte_str_ref_t (const hb_ubytes_t &str_, unsigned int offset_ = 0)
: str (str_), offset (offset_), error (false) {} : str (str_), offset (offset_), error (false) {}
void reset (const byte_str_t &str_, unsigned int offset_ = 0) void reset (const hb_ubytes_t &str_, unsigned int offset_ = 0)
{ {
str = str_; str = str_;
offset = offset_; offset = offset_;
@ -319,10 +314,10 @@ struct byte_str_ref_t
return str[offset + i]; return str[offset + i];
} }
/* Conversion to byte_str_t */ /* Conversion to hb_ubytes_t */
operator byte_str_t () const { return str.sub_array (offset, str.length - offset); } operator hb_ubytes_t () const { return str.sub_array (offset, str.length - offset); }
byte_str_t sub_array (unsigned int offset_, unsigned int len_) const hb_ubytes_t sub_array (unsigned int offset_, unsigned int len_) const
{ return str.sub_array (offset_, len_); } { return str.sub_array (offset_, len_); }
bool avail (unsigned int count=1) const bool avail (unsigned int count=1) const
@ -343,14 +338,14 @@ struct byte_str_ref_t
void set_error () { error = true; } void set_error () { error = true; }
bool in_error () const { return error; } bool in_error () const { return error; }
byte_str_t str; hb_ubytes_t str;
unsigned int offset; /* beginning of the sub-string within str */ unsigned int offset; /* beginning of the sub-string within str */
protected: protected:
bool error; bool error;
}; };
typedef hb_vector_t<byte_str_t> byte_str_array_t; typedef hb_vector_t<hb_ubytes_t> byte_str_array_t;
/* stack */ /* stack */
template <typename ELEM, int LIMIT> template <typename ELEM, int LIMIT>
@ -504,7 +499,7 @@ struct arg_stack_t : cff_stack_t<ARG, 513>
struct op_str_t struct op_str_t
{ {
op_code_t op; op_code_t op;
byte_str_t str; hb_ubytes_t str;
}; };
/* base of OP_SERIALIZER */ /* base of OP_SERIALIZER */
@ -566,7 +561,7 @@ struct parsed_values_t
template <typename ARG=number_t> template <typename ARG=number_t>
struct interp_env_t struct interp_env_t
{ {
void init (const byte_str_t &str_) void init (const hb_ubytes_t &str_)
{ {
str_ref.reset (str_); str_ref.reset (str_);
argStack.init (); argStack.init ();

View File

@ -79,10 +79,10 @@ struct biased_subrs_t
unsigned int get_count () const { return subrs ? subrs->count : 0; } unsigned int get_count () const { return subrs ? subrs->count : 0; }
unsigned int get_bias () const { return bias; } unsigned int get_bias () const { return bias; }
byte_str_t operator [] (unsigned int index) const hb_ubytes_t operator [] (unsigned int index) const
{ {
if (unlikely (!subrs || index >= subrs->count)) if (unlikely (!subrs || index >= subrs->count))
return Null (byte_str_t); return Null (hb_ubytes_t);
else else
return (*subrs)[index]; return (*subrs)[index];
} }
@ -112,7 +112,7 @@ struct point_t
template <typename ARG, typename SUBRS> template <typename ARG, typename SUBRS>
struct cs_interp_env_t : interp_env_t<ARG> struct cs_interp_env_t : interp_env_t<ARG>
{ {
void init (const byte_str_t &str, const SUBRS *globalSubrs_, const SUBRS *localSubrs_) void init (const hb_ubytes_t &str, const SUBRS *globalSubrs_, const SUBRS *localSubrs_)
{ {
interp_env_t<ARG>::init (str); interp_env_t<ARG>::init (str);

View File

@ -38,7 +38,7 @@ typedef biased_subrs_t<CFF1Subrs> cff1_biased_subrs_t;
struct cff1_cs_interp_env_t : cs_interp_env_t<number_t, CFF1Subrs> struct cff1_cs_interp_env_t : cs_interp_env_t<number_t, CFF1Subrs>
{ {
template <typename ACC> template <typename ACC>
void init (const byte_str_t &str, ACC &acc, unsigned int fd) void init (const hb_ubytes_t &str, ACC &acc, unsigned int fd)
{ {
SUPER::init (str, acc.globalSubrs, acc.privateDicts[fd].localSubrs); SUPER::init (str, acc.globalSubrs, acc.privateDicts[fd].localSubrs);
processed_width = false; processed_width = false;

View File

@ -67,7 +67,7 @@ typedef biased_subrs_t<CFF2Subrs> cff2_biased_subrs_t;
struct cff2_cs_interp_env_t : cs_interp_env_t<blend_arg_t, CFF2Subrs> struct cff2_cs_interp_env_t : cs_interp_env_t<blend_arg_t, CFF2Subrs>
{ {
template <typename ACC> template <typename ACC>
void init (const byte_str_t &str, ACC &acc, unsigned int fd, void init (const hb_ubytes_t &str, ACC &acc, unsigned int fd,
const int *coords_=nullptr, unsigned int num_coords_=0) const int *coords_=nullptr, unsigned int num_coords_=0)
{ {
SUPER::init (str, acc.globalSubrs, acc.privateDicts[fd].localSubrs); SUPER::init (str, acc.globalSubrs, acc.privateDicts[fd].localSubrs);

View File

@ -115,7 +115,7 @@ struct CFFIndex
/* serialize data */ /* serialize data */
for (unsigned int i = 0; i < byteArray.length; i++) for (unsigned int i = 0; i < byteArray.length; i++)
{ {
const byte_str_t &bs = byteArray[i]; const hb_ubytes_t &bs = byteArray[i];
unsigned char *dest = c->allocate_size<unsigned char> (bs.length); unsigned char *dest = c->allocate_size<unsigned char> (bs.length);
if (unlikely (!dest)) return_trace (false); if (unlikely (!dest)) return_trace (false);
memcpy (dest, &bs[0], bs.length); memcpy (dest, &bs[0], bs.length);
@ -132,7 +132,7 @@ struct CFFIndex
byteArray.init (); byteArray.init ();
byteArray.resize (buffArray.length); byteArray.resize (buffArray.length);
for (unsigned int i = 0; i < byteArray.length; i++) for (unsigned int i = 0; i < byteArray.length; i++)
byteArray[i] = byte_str_t (buffArray[i].arrayZ, buffArray[i].length); byteArray[i] = hb_ubytes_t (buffArray[i].arrayZ, buffArray[i].length);
bool result = this->serialize (c, offSize_, byteArray); bool result = this->serialize (c, offSize_, byteArray);
byteArray.fini (); byteArray.fini ();
return result; return result;
@ -144,7 +144,7 @@ struct CFFIndex
Iterator it) Iterator it)
{ {
TRACE_SERIALIZE (this); TRACE_SERIALIZE (this);
serialize_header(c, + it | hb_map ([] (const byte_str_t &_) { return _.length; })); serialize_header(c, + it | hb_map ([] (const hb_ubytes_t &_) { return _.length; }));
for (const auto &_ : +it) for (const auto &_ : +it)
_.copy (c); _.copy (c);
return_trace (true); return_trace (true);
@ -159,7 +159,7 @@ struct CFFIndex
{ {
auto it = auto it =
+ hb_iter (buffArray) + hb_iter (buffArray)
| hb_map ([] (const str_buff_t &_) { return byte_str_t (_.arrayZ, _.length); }) | hb_map ([] (const str_buff_t &_) { return hb_ubytes_t (_.arrayZ, _.length); })
; ;
return serialize (c, it); return serialize (c, it);
} }
@ -233,10 +233,10 @@ struct CFFIndex
{ return (const unsigned char *) this + min_size + offSize.static_size + offset_array_size (); } { return (const unsigned char *) this + min_size + offSize.static_size + offset_array_size (); }
public: public:
byte_str_t operator [] (unsigned int index) const hb_ubytes_t operator [] (unsigned int index) const
{ {
if (unlikely (index >= count)) return byte_str_t (); if (unlikely (index >= count)) return hb_ubytes_t ();
return byte_str_t (data_base () + offset_at (index) - 1, length_at (index)); return hb_ubytes_t (data_base () + offset_at (index) - 1, length_at (index));
} }
unsigned int get_size () const unsigned int get_size () const

View File

@ -396,7 +396,7 @@ bool _get_bounds (const OT::cff1::accelerator_t *cff, hb_codepoint_t glyph, boun
unsigned int fd = cff->fdSelect->get_fd (glyph); unsigned int fd = cff->fdSelect->get_fd (glyph);
cff1_cs_interpreter_t<cff1_cs_opset_extents_t, cff1_extents_param_t> interp; cff1_cs_interpreter_t<cff1_cs_opset_extents_t, cff1_extents_param_t> interp;
const byte_str_t str = (*cff->charStrings)[glyph]; const hb_ubytes_t str = (*cff->charStrings)[glyph];
interp.env.init (str, *cff, fd); interp.env.init (str, *cff, fd);
interp.env.set_in_seac (in_seac); interp.env.set_in_seac (in_seac);
cff1_extents_param_t param; cff1_extents_param_t param;
@ -542,7 +542,7 @@ bool _get_path (const OT::cff1::accelerator_t *cff, hb_font_t *font, hb_codepoin
unsigned int fd = cff->fdSelect->get_fd (glyph); unsigned int fd = cff->fdSelect->get_fd (glyph);
cff1_cs_interpreter_t<cff1_cs_opset_path_t, cff1_path_param_t> interp; cff1_cs_interpreter_t<cff1_cs_opset_path_t, cff1_path_param_t> interp;
const byte_str_t str = (*cff->charStrings)[glyph]; const hb_ubytes_t str = (*cff->charStrings)[glyph];
interp.env.init (str, *cff, fd); interp.env.init (str, *cff, fd);
interp.env.set_in_seac (in_seac); interp.env.set_in_seac (in_seac);
cff1_path_param_t param (cff, font, draw_session, delta); cff1_path_param_t param (cff, font, draw_session, delta);
@ -599,7 +599,7 @@ bool OT::cff1::accelerator_t::get_seac_components (hb_codepoint_t glyph, hb_code
unsigned int fd = fdSelect->get_fd (glyph); unsigned int fd = fdSelect->get_fd (glyph);
cff1_cs_interpreter_t<cff1_cs_opset_seac_t, get_seac_param_t> interp; cff1_cs_interpreter_t<cff1_cs_opset_seac_t, get_seac_param_t> interp;
const byte_str_t str = (*charStrings)[glyph]; const hb_ubytes_t str = (*charStrings)[glyph];
interp.env.init (str, *this, fd); interp.env.init (str, *this, fd);
get_seac_param_t param; get_seac_param_t param;
param.init (this); param.init (this);

View File

@ -1024,7 +1024,7 @@ struct cff1
{ fini (); return; } { fini (); return; }
{ /* parse top dict */ { /* parse top dict */
const byte_str_t topDictStr = (*topDictIndex)[0]; const hb_ubytes_t topDictStr = (*topDictIndex)[0];
if (unlikely (!topDictStr.sanitize (&sc))) { fini (); return; } if (unlikely (!topDictStr.sanitize (&sc))) { fini (); return; }
cff1_top_dict_interpreter_t top_interp; cff1_top_dict_interpreter_t top_interp;
top_interp.env.init (topDictStr); top_interp.env.init (topDictStr);
@ -1098,7 +1098,7 @@ struct cff1
{ {
for (unsigned int i = 0; i < fdCount; i++) for (unsigned int i = 0; i < fdCount; i++)
{ {
byte_str_t fontDictStr = (*fdArray)[i]; hb_ubytes_t fontDictStr = (*fdArray)[i];
if (unlikely (!fontDictStr.sanitize (&sc))) { fini (); return; } if (unlikely (!fontDictStr.sanitize (&sc))) { fini (); return; }
cff1_font_dict_values_t *font; cff1_font_dict_values_t *font;
cff1_font_dict_interpreter_t font_interp; cff1_font_dict_interpreter_t font_interp;
@ -1108,7 +1108,7 @@ struct cff1
font->init (); font->init ();
if (unlikely (!font_interp.interpret (*font))) { fini (); return; } if (unlikely (!font_interp.interpret (*font))) { fini (); return; }
PRIVDICTVAL *priv = &privateDicts[i]; PRIVDICTVAL *priv = &privateDicts[i];
const byte_str_t privDictStr = StructAtOffset<UnsizedByteStr> (cff, font->privateDictInfo.offset).as_ubytes (font->privateDictInfo.size); const hb_ubytes_t privDictStr = StructAtOffset<UnsizedByteStr> (cff, font->privateDictInfo.offset).as_ubytes (font->privateDictInfo.size);
if (unlikely (!privDictStr.sanitize (&sc))) { fini (); return; } if (unlikely (!privDictStr.sanitize (&sc))) { fini (); return; }
dict_interpreter_t<PRIVOPSET, PRIVDICTVAL> priv_interp; dict_interpreter_t<PRIVOPSET, PRIVDICTVAL> priv_interp;
priv_interp.env.init (privDictStr); priv_interp.env.init (privDictStr);
@ -1126,7 +1126,7 @@ struct cff1
cff1_top_dict_values_t *font = &topDict; cff1_top_dict_values_t *font = &topDict;
PRIVDICTVAL *priv = &privateDicts[0]; PRIVDICTVAL *priv = &privateDicts[0];
const byte_str_t privDictStr = StructAtOffset<UnsizedByteStr> (cff, font->privateDictInfo.offset).as_ubytes (font->privateDictInfo.size); const hb_ubytes_t privDictStr = StructAtOffset<UnsizedByteStr> (cff, font->privateDictInfo.offset).as_ubytes (font->privateDictInfo.size);
if (unlikely (!privDictStr.sanitize (&sc))) { fini (); return; } if (unlikely (!privDictStr.sanitize (&sc))) { fini (); return; }
dict_interpreter_t<PRIVOPSET, PRIVDICTVAL> priv_interp; dict_interpreter_t<PRIVOPSET, PRIVDICTVAL> priv_interp;
priv_interp.env.init (privDictStr); priv_interp.env.init (privDictStr);
@ -1287,7 +1287,7 @@ struct cff1
gname.name = cff1_std_strings (sid); gname.name = cff1_std_strings (sid);
else else
{ {
byte_str_t ustr = (*stringIndex)[sid - cff1_std_strings_length]; hb_ubytes_t ustr = (*stringIndex)[sid - cff1_std_strings_length];
gname.name = hb_bytes_t ((const char*)ustr.arrayZ, ustr.length); gname.name = hb_bytes_t ((const char*)ustr.arrayZ, ustr.length);
} }
if (unlikely (!gname.name.arrayZ)) { fini (); return; } if (unlikely (!gname.name.arrayZ)) { fini (); return; }
@ -1319,7 +1319,7 @@ struct cff1
} }
else else
{ {
byte_str_t ubyte_str = (*stringIndex)[sid - cff1_std_strings_length]; hb_ubytes_t ubyte_str = (*stringIndex)[sid - cff1_std_strings_length];
str = (const char *)ubyte_str.arrayZ; str = (const char *)ubyte_str.arrayZ;
str_len = ubyte_str.length; str_len = ubyte_str.length;
} }

View File

@ -113,7 +113,7 @@ bool OT::cff2::accelerator_t::get_extents (hb_font_t *font,
unsigned int fd = fdSelect->get_fd (glyph); unsigned int fd = fdSelect->get_fd (glyph);
cff2_cs_interpreter_t<cff2_cs_opset_extents_t, cff2_extents_param_t> interp; cff2_cs_interpreter_t<cff2_cs_opset_extents_t, cff2_extents_param_t> interp;
const byte_str_t str = (*charStrings)[glyph]; const hb_ubytes_t str = (*charStrings)[glyph];
interp.env.init (str, *this, fd, font->coords, font->num_coords); interp.env.init (str, *this, fd, font->coords, font->num_coords);
cff2_extents_param_t param; cff2_extents_param_t param;
param.init (); param.init ();
@ -203,7 +203,7 @@ bool OT::cff2::accelerator_t::get_path (hb_font_t *font, hb_codepoint_t glyph, h
unsigned int fd = fdSelect->get_fd (glyph); unsigned int fd = fdSelect->get_fd (glyph);
cff2_cs_interpreter_t<cff2_cs_opset_path_t, cff2_path_param_t> interp; cff2_cs_interpreter_t<cff2_cs_opset_path_t, cff2_path_param_t> interp;
const byte_str_t str = (*charStrings)[glyph]; const hb_ubytes_t str = (*charStrings)[glyph];
interp.env.init (str, *this, fd, font->coords, font->num_coords); interp.env.init (str, *this, fd, font->coords, font->num_coords);
cff2_path_param_t param (font, draw_session); cff2_path_param_t param (font, draw_session);
if (unlikely (!interp.interpret (param))) return false; if (unlikely (!interp.interpret (param))) return false;

View File

@ -247,7 +247,7 @@ typedef cff2_private_dict_values_base_t<num_dict_val_t> cff2_private_dict_values
struct cff2_priv_dict_interp_env_t : num_interp_env_t struct cff2_priv_dict_interp_env_t : num_interp_env_t
{ {
void init (const byte_str_t &str) void init (const hb_ubytes_t &str)
{ {
num_interp_env_t::init (str); num_interp_env_t::init (str);
ivs = 0; ivs = 0;
@ -415,7 +415,7 @@ struct cff2
goto fail; goto fail;
{ /* parse top dict */ { /* parse top dict */
byte_str_t topDictStr = (cff2 + cff2->topDict).as_ubytes (cff2->topDictSize); hb_ubytes_t topDictStr = (cff2 + cff2->topDict).as_ubytes (cff2->topDictSize);
if (unlikely (!topDictStr.sanitize (&sc))) goto fail; if (unlikely (!topDictStr.sanitize (&sc))) goto fail;
cff2_top_dict_interpreter_t top_interp; cff2_top_dict_interpreter_t top_interp;
top_interp.env.init (topDictStr); top_interp.env.init (topDictStr);
@ -447,7 +447,7 @@ struct cff2
/* parse font dicts and gather private dicts */ /* parse font dicts and gather private dicts */
for (unsigned int i = 0; i < fdCount; i++) for (unsigned int i = 0; i < fdCount; i++)
{ {
const byte_str_t fontDictStr = (*fdArray)[i]; const hb_ubytes_t fontDictStr = (*fdArray)[i];
if (unlikely (!fontDictStr.sanitize (&sc))) goto fail; if (unlikely (!fontDictStr.sanitize (&sc))) goto fail;
cff2_font_dict_values_t *font; cff2_font_dict_values_t *font;
cff2_font_dict_interpreter_t font_interp; cff2_font_dict_interpreter_t font_interp;
@ -457,7 +457,7 @@ struct cff2
font->init (); font->init ();
if (unlikely (!font_interp.interpret (*font))) goto fail; if (unlikely (!font_interp.interpret (*font))) goto fail;
const byte_str_t privDictStr = StructAtOffsetOrNull<UnsizedByteStr> (cff2, font->privateDictInfo.offset).as_ubytes (font->privateDictInfo.size); const hb_ubytes_t privDictStr = StructAtOffsetOrNull<UnsizedByteStr> (cff2, font->privateDictInfo.offset).as_ubytes (font->privateDictInfo.size);
if (unlikely (!privDictStr.sanitize (&sc))) goto fail; if (unlikely (!privDictStr.sanitize (&sc))) goto fail;
dict_interpreter_t<PRIVOPSET, PRIVDICTVAL, cff2_priv_dict_interp_env_t> priv_interp; dict_interpreter_t<PRIVOPSET, PRIVDICTVAL, cff2_priv_dict_interp_env_t> priv_interp;
priv_interp.env.init(privDictStr); priv_interp.env.init(privDictStr);

View File

@ -107,7 +107,7 @@ struct str_encoder_t
encode_byte (op); encode_byte (op);
} }
void copy_str (const byte_str_t &str) void copy_str (const hb_ubytes_t &str)
{ {
unsigned int offset = buff.length; unsigned int offset = buff.length;
if (unlikely (!buff.resize (offset + str.length))) if (unlikely (!buff.resize (offset + str.length)))
@ -253,7 +253,7 @@ struct subr_flattener_t
if (endchar_op != OpCode_Invalid) flat_charstrings[i].push (endchar_op); if (endchar_op != OpCode_Invalid) flat_charstrings[i].push (endchar_op);
continue; continue;
} }
const byte_str_t str = (*acc.charStrings)[glyph]; const hb_ubytes_t str = (*acc.charStrings)[glyph];
unsigned int fd = acc.fdSelect->get_fd (glyph); unsigned int fd = acc.fdSelect->get_fd (glyph);
if (unlikely (fd >= acc.fdCount)) if (unlikely (fd >= acc.fdCount))
return false; return false;
@ -561,7 +561,7 @@ struct subr_subsetter_t
hb_codepoint_t glyph; hb_codepoint_t glyph;
if (!plan->old_gid_for_new_gid (i, &glyph)) if (!plan->old_gid_for_new_gid (i, &glyph))
continue; continue;
const byte_str_t str = (*acc.charStrings)[glyph]; const hb_ubytes_t str = (*acc.charStrings)[glyph];
unsigned int fd = acc.fdSelect->get_fd (glyph); unsigned int fd = acc.fdSelect->get_fd (glyph);
if (unlikely (fd >= acc.fdCount)) if (unlikely (fd >= acc.fdCount))
return false; return false;

View File

@ -169,7 +169,7 @@ struct cff1_top_dict_op_serializer_t : cff_top_dict_op_serializer_t<cff1_top_dic
supp_op.op = op; supp_op.op = op;
if ( unlikely (!(opstr.str.length >= opstr.last_arg_offset + 3))) if ( unlikely (!(opstr.str.length >= opstr.last_arg_offset + 3)))
return_trace (false); return_trace (false);
supp_op.str = byte_str_t (&opstr.str + opstr.last_arg_offset, opstr.str.length - opstr.last_arg_offset); supp_op.str = hb_ubytes_t (&opstr.str + opstr.last_arg_offset, opstr.str.length - opstr.last_arg_offset);
return_trace (UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[name_dict_values_t::registry]) && return_trace (UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[name_dict_values_t::registry]) &&
UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[name_dict_values_t::ordering]) && UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[name_dict_values_t::ordering]) &&
copy_opstr (c, supp_op)); copy_opstr (c, supp_op));