src/java.desktop/share/native/libfontmanager/harfbuzz/hb-machinery.hh
author prr
Fri, 01 Mar 2019 16:59:19 -0800
changeset 54232 7c11a7cc7c1d
permissions -rw-r--r--
8210782: Upgrade HarfBuzz to the latest 2.3.1 Reviewed-by: serb, ihse, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54232
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
     1
/*
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
     2
 * Copyright © 2007,2008,2009,2010  Red Hat, Inc.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
     3
 * Copyright © 2012,2018  Google, Inc.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
     4
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
     5
 *  This is part of HarfBuzz, a text shaping library.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
     6
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
     7
 * Permission is hereby granted, without written agreement and without
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
     8
 * license or royalty fees, to use, copy, modify, and distribute this
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
     9
 * software and its documentation for any purpose, provided that the
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    10
 * above copyright notice and the following two paragraphs appear in
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    11
 * all copies of this software.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    12
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    13
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    14
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    15
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    16
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    17
 * DAMAGE.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    18
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    19
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    20
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    21
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    22
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    23
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    24
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    25
 * Red Hat Author(s): Behdad Esfahbod
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    26
 * Google Author(s): Behdad Esfahbod
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    27
 */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    28
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    29
#ifndef HB_MACHINERY_HH
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    30
#define HB_MACHINERY_HH
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    31
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    32
#include "hb.hh"
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    33
#include "hb-blob.hh"
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    34
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    35
#include "hb-array.hh"
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    36
#include "hb-vector.hh"
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    37
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    38
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    39
/*
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    40
 * Casts
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    41
 */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    42
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    43
/* Cast to struct T, reference to reference */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    44
template<typename Type, typename TObject>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    45
static inline const Type& CastR(const TObject &X)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    46
{ return reinterpret_cast<const Type&> (X); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    47
template<typename Type, typename TObject>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    48
static inline Type& CastR(TObject &X)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    49
{ return reinterpret_cast<Type&> (X); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    50
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    51
/* Cast to struct T, pointer to pointer */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    52
template<typename Type, typename TObject>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    53
static inline const Type* CastP(const TObject *X)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    54
{ return reinterpret_cast<const Type*> (X); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    55
template<typename Type, typename TObject>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    56
static inline Type* CastP(TObject *X)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    57
{ return reinterpret_cast<Type*> (X); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    58
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    59
/* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    60
 * location pointed to by P plus Ofs bytes. */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    61
template<typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    62
static inline const Type& StructAtOffset(const void *P, unsigned int offset)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    63
{ return * reinterpret_cast<const Type*> ((const char *) P + offset); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    64
template<typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    65
static inline Type& StructAtOffset(void *P, unsigned int offset)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    66
{ return * reinterpret_cast<Type*> ((char *) P + offset); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    67
template<typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    68
static inline const Type& StructAtOffsetUnaligned(const void *P, unsigned int offset)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    69
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    70
#pragma GCC diagnostic push
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    71
#pragma GCC diagnostic ignored "-Wcast-align"
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    72
  return * reinterpret_cast<Type*> ((char *) P + offset);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    73
#pragma GCC diagnostic pop
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    74
}
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    75
template<typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    76
static inline Type& StructAtOffsetUnaligned(void *P, unsigned int offset)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    77
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    78
#pragma GCC diagnostic push
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    79
#pragma GCC diagnostic ignored "-Wcast-align"
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    80
  return * reinterpret_cast<Type*> ((char *) P + offset);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    81
#pragma GCC diagnostic pop
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    82
}
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    83
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    84
/* StructAfter<T>(X) returns the struct T& that is placed after X.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    85
 * Works with X of variable size also.  X must implement get_size() */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    86
template<typename Type, typename TObject>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    87
static inline const Type& StructAfter(const TObject &X)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    88
{ return StructAtOffset<Type>(&X, X.get_size()); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    89
template<typename Type, typename TObject>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    90
static inline Type& StructAfter(TObject &X)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    91
{ return StructAtOffset<Type>(&X, X.get_size()); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    92
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    93
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    94
/*
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    95
 * Size checking
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    96
 */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    97
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    98
/* Check _assertion in a method environment */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
    99
#define _DEFINE_INSTANCE_ASSERTION1(_line, _assertion) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   100
  void _instance_assertion_on_line_##_line () const \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   101
  { static_assert ((_assertion), ""); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   102
# define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   103
# define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   104
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   105
/* Check that _code compiles in a method environment */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   106
#define _DEFINE_COMPILES_ASSERTION1(_line, _code) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   107
  void _compiles_assertion_on_line_##_line () const \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   108
  { _code; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   109
# define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   110
# define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   111
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   112
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   113
#define DEFINE_SIZE_STATIC(size) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   114
  DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   115
  unsigned int get_size () const { return (size); } \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   116
  static constexpr unsigned null_size = (size); \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   117
  static constexpr unsigned min_size = (size); \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   118
  static constexpr unsigned static_size = (size)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   119
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   120
#define DEFINE_SIZE_UNION(size, _member) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   121
  DEFINE_COMPILES_ASSERTION ((void) this->u._member.static_size) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   122
  DEFINE_INSTANCE_ASSERTION (sizeof(this->u._member) == (size)) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   123
  static constexpr unsigned null_size = (size); \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   124
  static constexpr unsigned min_size = (size)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   125
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   126
#define DEFINE_SIZE_MIN(size) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   127
  DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   128
  static constexpr unsigned null_size = (size); \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   129
  static constexpr unsigned min_size = (size)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   130
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   131
#define DEFINE_SIZE_UNBOUNDED(size) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   132
  DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   133
  static constexpr unsigned min_size = (size)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   134
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   135
#define DEFINE_SIZE_ARRAY(size, array) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   136
  DEFINE_COMPILES_ASSERTION ((void) (array)[0].static_size) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   137
  DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + VAR * sizeof ((array)[0])) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   138
  static constexpr unsigned null_size = (size); \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   139
  static constexpr unsigned min_size = (size)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   140
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   141
#define DEFINE_SIZE_ARRAY_SIZED(size, array) \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   142
  unsigned int get_size () const { return (size - (array).min_size + (array).get_size ()); } \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   143
  DEFINE_SIZE_ARRAY(size, array)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   144
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   145
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   146
/*
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   147
 * Dispatch
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   148
 */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   149
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   150
template <typename Context, typename Return, unsigned int MaxDebugDepth>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   151
struct hb_dispatch_context_t
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   152
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   153
  static constexpr unsigned max_debug_depth = MaxDebugDepth;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   154
  typedef Return return_t;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   155
  template <typename T, typename F>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   156
  bool may_dispatch (const T *obj HB_UNUSED, const F *format HB_UNUSED) { return true; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   157
  static return_t no_dispatch_return_value () { return Context::default_return_value (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   158
  static bool stop_sublookup_iteration (const return_t r HB_UNUSED) { return false; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   159
};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   160
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   161
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   162
/*
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   163
 * Sanitize
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   164
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   165
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   166
 * === Introduction ===
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   167
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   168
 * The sanitize machinery is at the core of our zero-cost font loading.  We
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   169
 * mmap() font file into memory and create a blob out of it.  Font subtables
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   170
 * are returned as a readonly sub-blob of the main font blob.  These table
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   171
 * blobs are then sanitized before use, to ensure invalid memory access does
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   172
 * not happen.  The toplevel sanitize API use is like, eg. to load the 'head'
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   173
 * table:
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   174
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   175
 *   hb_blob_t *head_blob = hb_sanitize_context_t ().reference_table<OT::head> (face);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   176
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   177
 * The blob then can be converted to a head table struct with:
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   178
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   179
 *   const head *head_table = head_blob->as<head> ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   180
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   181
 * What the reference_table does is, to call hb_face_reference_table() to load
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   182
 * the table blob, sanitize it and return either the sanitized blob, or empty
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   183
 * blob if sanitization failed.  The blob->as() function returns the null
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   184
 * object of its template type argument if the blob is empty.  Otherwise, it
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   185
 * just casts the blob contents to the desired type.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   186
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   187
 * Sanitizing a blob of data with a type T works as follows (with minor
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   188
 * simplification):
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   189
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   190
 *   - Cast blob content to T*, call sanitize() method of it,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   191
 *   - If sanitize succeeded, return blob.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   192
 *   - Otherwise, if blob is not writable, try making it writable,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   193
 *     or copy if cannot be made writable in-place,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   194
 *   - Call sanitize() again.  Return blob if sanitize succeeded.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   195
 *   - Return empty blob otherwise.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   196
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   197
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   198
 * === The sanitize() contract ===
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   199
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   200
 * The sanitize() method of each object type shall return true if it's safe to
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   201
 * call other methods of the object, and false otherwise.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   202
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   203
 * Note that what sanitize() checks for might align with what the specification
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   204
 * describes as valid table data, but does not have to be.  In particular, we
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   205
 * do NOT want to be pedantic and concern ourselves with validity checks that
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   206
 * are irrelevant to our use of the table.  On the contrary, we want to be
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   207
 * lenient with error handling and accept invalid data to the extent that it
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   208
 * does not impose extra burden on us.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   209
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   210
 * Based on the sanitize contract, one can see that what we check for depends
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   211
 * on how we use the data in other table methods.  Ie. if other table methods
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   212
 * assume that offsets do NOT point out of the table data block, then that's
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   213
 * something sanitize() must check for (GSUB/GPOS/GDEF/etc work this way).  On
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   214
 * the other hand, if other methods do such checks themselves, then sanitize()
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   215
 * does not have to bother with them (glyf/local work this way).  The choice
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   216
 * depends on the table structure and sanitize() performance.  For example, to
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   217
 * check glyf/loca offsets in sanitize() would cost O(num-glyphs).  We try hard
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   218
 * to avoid such costs during font loading.  By postponing such checks to the
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   219
 * actual glyph loading, we reduce the sanitize cost to O(1) and total runtime
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   220
 * cost to O(used-glyphs).  As such, this is preferred.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   221
 *
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   222
 * The same argument can be made re GSUB/GPOS/GDEF, but there, the table
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   223
 * structure is so complicated that by checking all offsets at sanitize() time,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   224
 * we make the code much simpler in other methods, as offsets and referenced
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   225
 * objects do not need to be validated at each use site.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   226
 */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   227
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   228
/* This limits sanitizing time on really broken fonts. */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   229
#ifndef HB_SANITIZE_MAX_EDITS
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   230
#define HB_SANITIZE_MAX_EDITS 32
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   231
#endif
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   232
#ifndef HB_SANITIZE_MAX_OPS_FACTOR
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   233
#define HB_SANITIZE_MAX_OPS_FACTOR 8
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   234
#endif
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   235
#ifndef HB_SANITIZE_MAX_OPS_MIN
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   236
#define HB_SANITIZE_MAX_OPS_MIN 16384
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   237
#endif
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   238
#ifndef HB_SANITIZE_MAX_OPS_MAX
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   239
#define HB_SANITIZE_MAX_OPS_MAX 0x3FFFFFFF
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   240
#endif
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   241
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   242
struct hb_sanitize_context_t :
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   243
       hb_dispatch_context_t<hb_sanitize_context_t, bool, HB_DEBUG_SANITIZE>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   244
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   245
  hb_sanitize_context_t () :
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   246
        debug_depth (0),
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   247
        start (nullptr), end (nullptr),
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   248
        max_ops (0),
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   249
        writable (false), edit_count (0),
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   250
        blob (nullptr),
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   251
        num_glyphs (65536),
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   252
        num_glyphs_set (false) {}
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   253
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   254
  const char *get_name () { return "SANITIZE"; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   255
  template <typename T, typename F>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   256
  bool may_dispatch (const T *obj HB_UNUSED, const F *format)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   257
  { return format->sanitize (this); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   258
  template <typename T>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   259
  return_t dispatch (const T &obj) { return obj.sanitize (this); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   260
  static return_t default_return_value () { return true; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   261
  static return_t no_dispatch_return_value () { return false; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   262
  bool stop_sublookup_iteration (const return_t r) const { return !r; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   263
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   264
  void init (hb_blob_t *b)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   265
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   266
    this->blob = hb_blob_reference (b);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   267
    this->writable = false;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   268
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   269
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   270
  void set_num_glyphs (unsigned int num_glyphs_)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   271
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   272
    num_glyphs = num_glyphs_;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   273
    num_glyphs_set = true;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   274
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   275
  unsigned int get_num_glyphs () { return num_glyphs; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   276
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   277
  void set_max_ops (int max_ops_) { max_ops = max_ops_; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   278
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   279
  template <typename T>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   280
  void set_object (const T *obj)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   281
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   282
    reset_object ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   283
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   284
    if (!obj) return;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   285
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   286
    const char *obj_start = (const char *) obj;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   287
    if (unlikely (obj_start < this->start || this->end <= obj_start))
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   288
      this->start = this->end = nullptr;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   289
    else
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   290
    {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   291
      this->start = obj_start;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   292
      this->end   = obj_start + MIN<uintptr_t> (this->end - obj_start, obj->get_size ());
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   293
    }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   294
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   295
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   296
  void reset_object ()
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   297
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   298
    this->start = this->blob->data;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   299
    this->end = this->start + this->blob->length;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   300
    assert (this->start <= this->end); /* Must not overflow. */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   301
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   302
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   303
  void start_processing ()
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   304
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   305
    reset_object ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   306
    this->max_ops = MAX ((unsigned int) (this->end - this->start) * HB_SANITIZE_MAX_OPS_FACTOR,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   307
                         (unsigned) HB_SANITIZE_MAX_OPS_MIN);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   308
    this->edit_count = 0;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   309
    this->debug_depth = 0;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   310
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   311
    DEBUG_MSG_LEVEL (SANITIZE, start, 0, +1,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   312
                     "start [%p..%p] (%lu bytes)",
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   313
                     this->start, this->end,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   314
                     (unsigned long) (this->end - this->start));
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   315
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   316
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   317
  void end_processing ()
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   318
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   319
    DEBUG_MSG_LEVEL (SANITIZE, this->start, 0, -1,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   320
                     "end [%p..%p] %u edit requests",
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   321
                     this->start, this->end, this->edit_count);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   322
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   323
    hb_blob_destroy (this->blob);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   324
    this->blob = nullptr;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   325
    this->start = this->end = nullptr;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   326
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   327
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   328
  bool check_range (const void *base,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   329
                           unsigned int len) const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   330
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   331
    const char *p = (const char *) base;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   332
    bool ok = this->start <= p &&
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   333
              p <= this->end &&
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   334
              (unsigned int) (this->end - p) >= len &&
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   335
              this->max_ops-- > 0;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   336
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   337
    DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   338
       "check_range [%p..%p] (%d bytes) in [%p..%p] -> %s",
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   339
       p, p + len, len,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   340
       this->start, this->end,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   341
       ok ? "OK" : "OUT-OF-RANGE");
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   342
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   343
    return likely (ok);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   344
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   345
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   346
  template <typename T>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   347
  bool check_range (const T *base,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   348
                           unsigned int a,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   349
                           unsigned int b) const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   350
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   351
    return !hb_unsigned_mul_overflows (a, b) &&
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   352
           this->check_range (base, a * b);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   353
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   354
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   355
  template <typename T>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   356
  bool check_range (const T *base,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   357
                           unsigned int a,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   358
                           unsigned int b,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   359
                           unsigned int c) const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   360
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   361
    return !hb_unsigned_mul_overflows (a, b) &&
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   362
           this->check_range (base, a * b, c);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   363
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   364
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   365
  template <typename T>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   366
  bool check_array (const T *base, unsigned int len) const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   367
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   368
    return this->check_range (base, len, hb_static_size (T));
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   369
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   370
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   371
  template <typename T>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   372
  bool check_array (const T *base,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   373
                    unsigned int a,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   374
                    unsigned int b) const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   375
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   376
    return this->check_range (base, a, b, hb_static_size (T));
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   377
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   378
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   379
  template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   380
  bool check_struct (const Type *obj) const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   381
  { return likely (this->check_range (obj, obj->min_size)); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   382
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   383
  bool may_edit (const void *base, unsigned int len)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   384
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   385
    if (this->edit_count >= HB_SANITIZE_MAX_EDITS)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   386
      return false;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   387
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   388
    const char *p = (const char *) base;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   389
    this->edit_count++;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   390
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   391
    DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   392
       "may_edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s",
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   393
       this->edit_count,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   394
       p, p + len, len,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   395
       this->start, this->end,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   396
       this->writable ? "GRANTED" : "DENIED");
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   397
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   398
    return this->writable;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   399
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   400
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   401
  template <typename Type, typename ValueType>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   402
  bool try_set (const Type *obj, const ValueType &v)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   403
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   404
    if (this->may_edit (obj, hb_static_size (Type)))
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   405
    {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   406
      hb_assign (* const_cast<Type *> (obj), v);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   407
      return true;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   408
    }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   409
    return false;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   410
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   411
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   412
  template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   413
  hb_blob_t *sanitize_blob (hb_blob_t *blob)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   414
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   415
    bool sane;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   416
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   417
    init (blob);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   418
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   419
  retry:
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   420
    DEBUG_MSG_FUNC (SANITIZE, start, "start");
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   421
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   422
    start_processing ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   423
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   424
    if (unlikely (!start))
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   425
    {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   426
      end_processing ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   427
      return blob;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   428
    }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   429
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   430
    Type *t = CastP<Type> (const_cast<char *> (start));
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   431
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   432
    sane = t->sanitize (this);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   433
    if (sane)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   434
    {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   435
      if (edit_count)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   436
      {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   437
        DEBUG_MSG_FUNC (SANITIZE, start, "passed first round with %d edits; going for second round", edit_count);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   438
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   439
        /* sanitize again to ensure no toe-stepping */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   440
        edit_count = 0;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   441
        sane = t->sanitize (this);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   442
        if (edit_count) {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   443
          DEBUG_MSG_FUNC (SANITIZE, start, "requested %d edits in second round; FAILLING", edit_count);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   444
          sane = false;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   445
        }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   446
      }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   447
    }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   448
    else
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   449
    {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   450
      if (edit_count && !writable) {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   451
        start = hb_blob_get_data_writable (blob, nullptr);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   452
        end = start + blob->length;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   453
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   454
        if (start)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   455
        {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   456
          writable = true;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   457
          /* ok, we made it writable by relocating.  try again */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   458
          DEBUG_MSG_FUNC (SANITIZE, start, "retry");
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   459
          goto retry;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   460
        }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   461
      }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   462
    }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   463
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   464
    end_processing ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   465
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   466
    DEBUG_MSG_FUNC (SANITIZE, start, sane ? "PASSED" : "FAILED");
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   467
    if (sane)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   468
    {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   469
      hb_blob_make_immutable (blob);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   470
      return blob;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   471
    }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   472
    else
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   473
    {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   474
      hb_blob_destroy (blob);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   475
      return hb_blob_get_empty ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   476
    }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   477
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   478
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   479
  template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   480
  hb_blob_t *reference_table (const hb_face_t *face, hb_tag_t tableTag = Type::tableTag)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   481
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   482
    if (!num_glyphs_set)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   483
      set_num_glyphs (hb_face_get_glyph_count (face));
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   484
    return sanitize_blob<Type> (hb_face_reference_table (face, tableTag));
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   485
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   486
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   487
  mutable unsigned int debug_depth;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   488
  const char *start, *end;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   489
  mutable int max_ops;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   490
  private:
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   491
  bool writable;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   492
  unsigned int edit_count;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   493
  hb_blob_t *blob;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   494
  unsigned int num_glyphs;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   495
  bool  num_glyphs_set;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   496
};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   497
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   498
struct hb_sanitize_with_object_t
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   499
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   500
  template <typename T>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   501
  hb_sanitize_with_object_t (hb_sanitize_context_t *c,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   502
                                    const T& obj) : c (c)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   503
  { c->set_object (obj); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   504
  ~hb_sanitize_with_object_t ()
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   505
  { c->reset_object (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   506
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   507
  private:
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   508
  hb_sanitize_context_t *c;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   509
};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   510
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   511
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   512
/*
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   513
 * Serialize
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   514
 */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   515
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   516
struct hb_serialize_context_t
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   517
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   518
  hb_serialize_context_t (void *start_, unsigned int size)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   519
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   520
    this->start = (char *) start_;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   521
    this->end = this->start + size;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   522
    reset ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   523
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   524
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   525
  bool in_error () const { return !this->successful; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   526
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   527
  void reset ()
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   528
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   529
    this->successful = true;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   530
    this->head = this->start;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   531
    this->debug_depth = 0;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   532
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   533
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   534
  bool propagate_error (bool e)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   535
  { return this->successful = this->successful && e; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   536
  template <typename T> bool propagate_error (const T &obj)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   537
  { return this->successful = this->successful && !obj.in_error (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   538
  template <typename T> bool propagate_error (const T *obj)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   539
  { return this->successful = this->successful && !obj->in_error (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   540
  template <typename T1, typename T2> bool propagate_error (T1 &o1, T2 &o2)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   541
  { return propagate_error (o1) && propagate_error (o2); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   542
  template <typename T1, typename T2> bool propagate_error (T1 *o1, T2 *o2)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   543
  { return propagate_error (o1) && propagate_error (o2); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   544
  template <typename T1, typename T2, typename T3>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   545
  bool propagate_error (T1 &o1, T2 &o2, T3 &o3)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   546
  { return propagate_error (o1) && propagate_error (o2, o3); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   547
  template <typename T1, typename T2, typename T3>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   548
  bool propagate_error (T1 *o1, T2 *o2, T3 *o3)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   549
  { return propagate_error (o1) && propagate_error (o2, o3); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   550
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   551
  /* To be called around main operation. */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   552
  template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   553
  Type *start_serialize ()
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   554
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   555
    DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, +1,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   556
                     "start [%p..%p] (%lu bytes)",
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   557
                     this->start, this->end,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   558
                     (unsigned long) (this->end - this->start));
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   559
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   560
    return start_embed<Type> ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   561
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   562
  void end_serialize ()
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   563
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   564
    DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, -1,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   565
                     "end [%p..%p] serialized %d bytes; %s",
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   566
                     this->start, this->end,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   567
                     (int) (this->head - this->start),
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   568
                     this->successful ? "successful" : "UNSUCCESSFUL");
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   569
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   570
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   571
  unsigned int length () const { return this->head - this->start; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   572
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   573
  void align (unsigned int alignment)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   574
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   575
    unsigned int l = length () % alignment;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   576
    if (l)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   577
      allocate_size<void> (alignment - l);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   578
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   579
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   580
  template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   581
  Type *start_embed (const Type *_ HB_UNUSED = nullptr) const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   582
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   583
    Type *ret = reinterpret_cast<Type *> (this->head);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   584
    return ret;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   585
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   586
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   587
  template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   588
  Type *allocate_size (unsigned int size)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   589
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   590
    if (unlikely (!this->successful || this->end - this->head < ptrdiff_t (size))) {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   591
      this->successful = false;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   592
      return nullptr;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   593
    }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   594
    memset (this->head, 0, size);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   595
    char *ret = this->head;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   596
    this->head += size;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   597
    return reinterpret_cast<Type *> (ret);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   598
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   599
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   600
  template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   601
  Type *allocate_min ()
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   602
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   603
    return this->allocate_size<Type> (Type::min_size);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   604
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   605
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   606
  template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   607
  Type *embed (const Type &obj)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   608
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   609
    unsigned int size = obj.get_size ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   610
    Type *ret = this->allocate_size<Type> (size);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   611
    if (unlikely (!ret)) return nullptr;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   612
    memcpy (ret, &obj, size);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   613
    return ret;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   614
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   615
  template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   616
  hb_serialize_context_t &operator << (const Type &obj) { embed (obj); return *this; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   617
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   618
  template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   619
  Type *extend_size (Type &obj, unsigned int size)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   620
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   621
    assert (this->start <= (char *) &obj);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   622
    assert ((char *) &obj <= this->head);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   623
    assert ((char *) &obj + size >= this->head);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   624
    if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return nullptr;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   625
    return reinterpret_cast<Type *> (&obj);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   626
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   627
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   628
  template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   629
  Type *extend_min (Type &obj) { return extend_size (obj, obj.min_size); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   630
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   631
  template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   632
  Type *extend (Type &obj) { return extend_size (obj, obj.get_size ()); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   633
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   634
  /* Output routines. */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   635
  template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   636
  Type *copy () const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   637
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   638
    assert (this->successful);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   639
    unsigned int len = this->head - this->start;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   640
    void *p = malloc (len);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   641
    if (p)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   642
      memcpy (p, this->start, len);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   643
    return reinterpret_cast<Type *> (p);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   644
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   645
  hb_bytes_t copy_bytes () const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   646
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   647
    assert (this->successful);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   648
    unsigned int len = this->head - this->start;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   649
    void *p = malloc (len);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   650
    if (p)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   651
      memcpy (p, this->start, len);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   652
    else
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   653
      return hb_bytes_t ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   654
    return hb_bytes_t ((char *) p, len);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   655
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   656
  hb_blob_t *copy_blob () const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   657
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   658
    assert (this->successful);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   659
    return hb_blob_create (this->start,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   660
                           this->head - this->start,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   661
                           HB_MEMORY_MODE_DUPLICATE,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   662
                           nullptr, nullptr);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   663
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   664
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   665
  public:
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   666
  unsigned int debug_depth;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   667
  char *start, *end, *head;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   668
  bool successful;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   669
};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   670
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   671
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   672
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   673
/*
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   674
 * Big-endian integers.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   675
 */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   676
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   677
template <typename Type, int Bytes> struct BEInt;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   678
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   679
template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   680
struct BEInt<Type, 1>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   681
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   682
  public:
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   683
  void set (Type V)      { v = V; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   684
  operator Type () const { return v; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   685
  private: uint8_t v;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   686
};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   687
template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   688
struct BEInt<Type, 2>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   689
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   690
  public:
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   691
  void set (Type V)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   692
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   693
    v[0] = (V >>  8) & 0xFF;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   694
    v[1] = (V      ) & 0xFF;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   695
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   696
  operator Type () const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   697
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   698
#if ((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__)) && \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   699
    defined(__BYTE_ORDER) && \
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   700
    (__BYTE_ORDER == __LITTLE_ENDIAN || __BYTE_ORDER == __BIG_ENDIAN)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   701
    /* Spoon-feed the compiler a big-endian integer with alignment 1.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   702
     * https://github.com/harfbuzz/harfbuzz/pull/1398 */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   703
    struct __attribute__((packed)) packed_uint16_t { uint16_t v; };
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   704
#if __BYTE_ORDER == __LITTLE_ENDIAN
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   705
    return __builtin_bswap16 (((packed_uint16_t *) this)->v);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   706
#else /* __BYTE_ORDER == __BIG_ENDIAN */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   707
    return ((packed_uint16_t *) this)->v;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   708
#endif
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   709
#endif
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   710
    return (v[0] <<  8)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   711
         + (v[1]      );
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   712
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   713
  private: uint8_t v[2];
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   714
};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   715
template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   716
struct BEInt<Type, 3>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   717
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   718
  public:
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   719
  void set (Type V)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   720
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   721
    v[0] = (V >> 16) & 0xFF;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   722
    v[1] = (V >>  8) & 0xFF;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   723
    v[2] = (V      ) & 0xFF;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   724
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   725
  operator Type () const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   726
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   727
    return (v[0] << 16)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   728
         + (v[1] <<  8)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   729
         + (v[2]      );
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   730
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   731
  private: uint8_t v[3];
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   732
};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   733
template <typename Type>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   734
struct BEInt<Type, 4>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   735
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   736
  public:
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   737
  typedef Type type;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   738
  void set (Type V)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   739
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   740
    v[0] = (V >> 24) & 0xFF;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   741
    v[1] = (V >> 16) & 0xFF;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   742
    v[2] = (V >>  8) & 0xFF;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   743
    v[3] = (V      ) & 0xFF;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   744
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   745
  operator Type () const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   746
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   747
    return (v[0] << 24)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   748
         + (v[1] << 16)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   749
         + (v[2] <<  8)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   750
         + (v[3]      );
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   751
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   752
  private: uint8_t v[4];
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   753
};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   754
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   755
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   756
/*
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   757
 * Lazy loaders.
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   758
 */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   759
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   760
template <typename Data, unsigned int WheresData>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   761
struct hb_data_wrapper_t
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   762
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   763
  static_assert (WheresData > 0, "");
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   764
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   765
  Data * get_data () const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   766
  { return *(((Data **) (void *) this) - WheresData); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   767
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   768
  bool is_inert () const { return !get_data (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   769
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   770
  template <typename Stored, typename Subclass>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   771
  Stored * call_create () const { return Subclass::create (get_data ()); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   772
};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   773
template <>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   774
struct hb_data_wrapper_t<void, 0>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   775
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   776
  bool is_inert () const { return false; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   777
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   778
  template <typename Stored, typename Funcs>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   779
  Stored * call_create () const { return Funcs::create (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   780
};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   781
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   782
template <typename T1, typename T2> struct hb_non_void_t { typedef T1 value; };
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   783
template <typename T2> struct hb_non_void_t<void, T2> { typedef T2 value; };
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   784
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   785
template <typename Returned,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   786
          typename Subclass = void,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   787
          typename Data = void,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   788
          unsigned int WheresData = 0,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   789
          typename Stored = Returned>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   790
struct hb_lazy_loader_t : hb_data_wrapper_t<Data, WheresData>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   791
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   792
  typedef typename hb_non_void_t<Subclass,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   793
                                 hb_lazy_loader_t<Returned,Subclass,Data,WheresData,Stored>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   794
                                >::value Funcs;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   795
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   796
  void init0 () {} /* Init, when memory is already set to 0. No-op for us. */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   797
  void init ()  { instance.set_relaxed (nullptr); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   798
  void fini ()  { do_destroy (instance.get ()); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   799
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   800
  void free_instance ()
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   801
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   802
  retry:
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   803
    Stored *p = instance.get ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   804
    if (unlikely (p && !cmpexch (p, nullptr)))
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   805
      goto retry;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   806
    do_destroy (p);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   807
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   808
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   809
  static void do_destroy (Stored *p)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   810
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   811
    if (p && p != const_cast<Stored *> (Funcs::get_null ()))
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   812
      Funcs::destroy (p);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   813
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   814
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   815
  const Returned * operator -> () const { return get (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   816
  const Returned & operator * () const  { return *get (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   817
  explicit_operator bool () const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   818
  { return get_stored () != Funcs::get_null (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   819
  template <typename C> operator const C * () const { return get (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   820
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   821
  Stored * get_stored () const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   822
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   823
  retry:
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   824
    Stored *p = this->instance.get ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   825
    if (unlikely (!p))
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   826
    {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   827
      if (unlikely (this->is_inert ()))
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   828
        return const_cast<Stored *> (Funcs::get_null ());
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   829
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   830
      p = this->template call_create<Stored, Funcs> ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   831
      if (unlikely (!p))
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   832
        p = const_cast<Stored *> (Funcs::get_null ());
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   833
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   834
      if (unlikely (!cmpexch (nullptr, p)))
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   835
      {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   836
        do_destroy (p);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   837
        goto retry;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   838
      }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   839
    }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   840
    return p;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   841
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   842
  Stored * get_stored_relaxed () const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   843
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   844
    return this->instance.get_relaxed ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   845
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   846
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   847
  bool cmpexch (Stored *current, Stored *value) const
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   848
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   849
    /* This *must* be called when there are no other threads accessing. */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   850
    return this->instance.cmpexch (current, value);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   851
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   852
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   853
  const Returned * get () const { return Funcs::convert (get_stored ()); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   854
  const Returned * get_relaxed () const { return Funcs::convert (get_stored_relaxed ()); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   855
  Returned * get_unconst () const { return const_cast<Returned *> (Funcs::convert (get_stored ())); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   856
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   857
  /* To be possibly overloaded by subclasses. */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   858
  static Returned* convert (Stored *p) { return p; }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   859
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   860
  /* By default null/init/fini the object. */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   861
  static const Stored* get_null () { return &Null(Stored); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   862
  static Stored *create (Data *data)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   863
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   864
    Stored *p = (Stored *) calloc (1, sizeof (Stored));
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   865
    if (likely (p))
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   866
      p->init (data);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   867
    return p;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   868
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   869
  static Stored *create ()
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   870
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   871
    Stored *p = (Stored *) calloc (1, sizeof (Stored));
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   872
    if (likely (p))
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   873
      p->init ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   874
    return p;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   875
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   876
  static void destroy (Stored *p)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   877
  {
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   878
    p->fini ();
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   879
    free (p);
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   880
  }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   881
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   882
//  private:
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   883
  /* Must only have one pointer. */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   884
  hb_atomic_ptr_t<Stored *> instance;
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   885
};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   886
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   887
/* Specializations. */
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   888
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   889
template <typename T, unsigned int WheresFace>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   890
struct hb_face_lazy_loader_t : hb_lazy_loader_t<T,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   891
                                                hb_face_lazy_loader_t<T, WheresFace>,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   892
                                                hb_face_t, WheresFace> {};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   893
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   894
template <typename T, unsigned int WheresFace>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   895
struct hb_table_lazy_loader_t : hb_lazy_loader_t<T,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   896
                                                 hb_table_lazy_loader_t<T, WheresFace>,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   897
                                                 hb_face_t, WheresFace,
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   898
                                                 hb_blob_t>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   899
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   900
  static hb_blob_t *create (hb_face_t *face)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   901
  { return hb_sanitize_context_t ().reference_table<T> (face); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   902
  static void destroy (hb_blob_t *p) { hb_blob_destroy (p); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   903
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   904
  static const hb_blob_t *get_null ()
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   905
  { return hb_blob_get_empty (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   906
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   907
  static const T* convert (const hb_blob_t *blob)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   908
  { return blob->as<T> (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   909
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   910
  hb_blob_t* get_blob () const { return this->get_stored (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   911
};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   912
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   913
template <typename Subclass>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   914
struct hb_font_funcs_lazy_loader_t : hb_lazy_loader_t<hb_font_funcs_t, Subclass>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   915
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   916
  static void destroy (hb_font_funcs_t *p)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   917
  { hb_font_funcs_destroy (p); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   918
  static const hb_font_funcs_t *get_null ()
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   919
  { return hb_font_funcs_get_empty (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   920
};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   921
template <typename Subclass>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   922
struct hb_unicode_funcs_lazy_loader_t : hb_lazy_loader_t<hb_unicode_funcs_t, Subclass>
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   923
{
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   924
  static void destroy (hb_unicode_funcs_t *p)
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   925
  { hb_unicode_funcs_destroy (p); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   926
  static const hb_unicode_funcs_t *get_null ()
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   927
  { return hb_unicode_funcs_get_empty (); }
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   928
};
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   929
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   930
7c11a7cc7c1d 8210782: Upgrade HarfBuzz to the latest 2.3.1
prr
parents:
diff changeset
   931
#endif /* HB_MACHINERY_HH */