tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

hb-ot-var-cvar-table.hh (8658B)


      1 /*
      2 * Copyright © 2023  Google, Inc.
      3 *
      4 *  This is part of HarfBuzz, a text shaping library.
      5 *
      6 * Permission is hereby granted, without written agreement and without
      7 * license or royalty fees, to use, copy, modify, and distribute this
      8 * software and its documentation for any purpose, provided that the
      9 * above copyright notice and the following two paragraphs appear in
     10 * all copies of this software.
     11 *
     12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
     13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
     14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
     15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
     16 * DAMAGE.
     17 *
     18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
     19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
     20 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
     21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
     22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
     23 *
     24 */
     25 
     26 #ifndef HB_OT_VAR_CVAR_TABLE_HH
     27 #define HB_OT_VAR_CVAR_TABLE_HH
     28 
     29 #include "hb-ot-var-common.hh"
     30 #include "hb-ot-var-fvar-table.hh"
     31 
     32 
     33 namespace OT {
     34 /*
     35 * cvar -- control value table (CVT) Variations
     36 * https://docs.microsoft.com/en-us/typography/opentype/spec/cvar
     37 */
     38 #define HB_OT_TAG_cvar HB_TAG('c','v','a','r')
     39 
     40 struct cvar
     41 {
     42  static constexpr hb_tag_t tableTag = HB_OT_TAG_cvar;
     43 
     44  bool sanitize (hb_sanitize_context_t *c) const
     45  {
     46    TRACE_SANITIZE (this);
     47    return_trace (c->check_struct (this) &&
     48 	  hb_barrier () &&
     49 	  likely (version.major == 1) &&
     50 	  tupleVariationData.sanitize (c));
     51  }
     52 
     53  const TupleVariationData<>* get_tuple_var_data (void) const
     54  { return &tupleVariationData; }
     55 
     56  bool decompile_tuple_variations (unsigned axis_count,
     57                                   unsigned point_count,
     58                                   hb_blob_t *blob,
     59                                   bool is_gvar,
     60                                   const hb_map_t *axes_old_index_tag_map,
     61                                   TupleVariationData<>::tuple_variations_t& tuple_variations /* OUT */) const
     62  {
     63    hb_vector_t<unsigned> shared_indices;
     64    TupleVariationData<>::tuple_iterator_t iterator;
     65    hb_bytes_t var_data_bytes = blob->as_bytes ().sub_array (4);
     66    if (!TupleVariationData<>::get_tuple_iterator (var_data_bytes, axis_count, this,
     67                                                 shared_indices, &iterator))
     68      return false;
     69 
     70    return tupleVariationData.decompile_tuple_variations (point_count, is_gvar, iterator,
     71                                                          axes_old_index_tag_map,
     72                                                          shared_indices,
     73                                                          hb_array<const F2DOT14> (),
     74                                                          tuple_variations);
     75  }
     76 
     77  static bool calculate_cvt_deltas (unsigned axis_count,
     78                                    hb_array_t<int> coords,
     79                                    unsigned num_cvt_item,
     80                                    const TupleVariationData<> *tuple_var_data,
     81                                    const void *base,
     82                                    hb_vector_t<float>& cvt_deltas /* OUT */)
     83  {
     84    if (!coords) return true;
     85    hb_vector_t<unsigned> shared_indices;
     86    TupleVariationData<>::tuple_iterator_t iterator;
     87    unsigned var_data_length = tuple_var_data->get_size (axis_count * 2);
     88    hb_bytes_t var_data_bytes = hb_bytes_t (reinterpret_cast<const char*> (tuple_var_data), var_data_length);
     89    if (!TupleVariationData<>::get_tuple_iterator (var_data_bytes, axis_count, base,
     90                                                 shared_indices, &iterator))
     91      return true; /* isn't applied at all */
     92 
     93    hb_array_t<const F2DOT14> shared_tuples = hb_array<F2DOT14> ();
     94    hb_vector_t<unsigned> private_indices;
     95    hb_vector_t<int> unpacked_deltas;
     96 
     97    do
     98    {
     99      float scalar = iterator.current_tuple->calculate_scalar (coords, axis_count, shared_tuples);
    100      if (scalar == 0.f) continue;
    101      const HBUINT8 *p = iterator.get_serialized_data ();
    102      unsigned int length = iterator.current_tuple->get_data_size ();
    103      if (unlikely (!iterator.var_data_bytes.check_range (p, length)))
    104        return false;
    105 
    106      const HBUINT8 *end = p + length;
    107 
    108      bool has_private_points = iterator.current_tuple->has_private_points ();
    109      if (has_private_points &&
    110          !TupleVariationData<>::decompile_points (p, private_indices, end))
    111        return false;
    112      const hb_vector_t<unsigned int> &indices = has_private_points ? private_indices : shared_indices;
    113 
    114      bool apply_to_all = (indices.length == 0);
    115      unsigned num_deltas = apply_to_all ? num_cvt_item : indices.length;
    116      if (unlikely (!unpacked_deltas.resize_dirty  (num_deltas))) return false;
    117      if (unlikely (!TupleVariationData<>::decompile_deltas (p, unpacked_deltas, end))) return false;
    118 
    119      for (unsigned int i = 0; i < num_deltas; i++)
    120      {
    121        unsigned int idx = apply_to_all ? i : indices[i];
    122        if (unlikely (idx >= num_cvt_item)) continue;
    123        if (scalar != 1.0f) cvt_deltas[idx] += unpacked_deltas[i] * scalar ;
    124        else cvt_deltas[idx] += unpacked_deltas[i];
    125      }
    126    } while (iterator.move_to_next ());
    127 
    128    return true;
    129  }
    130  
    131  bool serialize (hb_serialize_context_t *c,
    132                  TupleVariationData<>::tuple_variations_t& tuple_variations) const
    133  {
    134    TRACE_SERIALIZE (this);
    135    if (!tuple_variations) return_trace (false);
    136    if (unlikely (!c->embed (version))) return_trace (false);
    137 
    138    return_trace (tupleVariationData.serialize (c, false, tuple_variations));
    139  }
    140 
    141  bool subset (hb_subset_context_t *c) const
    142  {
    143    TRACE_SUBSET (this);
    144    if (c->plan->all_axes_pinned)
    145      return_trace (false);
    146 
    147    OT::TupleVariationData<>::tuple_variations_t tuple_variations;
    148    unsigned axis_count = c->plan->axes_old_index_tag_map.get_population ();
    149 
    150    const hb_tag_t cvt = HB_TAG('c','v','t',' ');
    151    hb_blob_t *cvt_blob = hb_face_reference_table (c->plan->source, cvt);
    152    unsigned point_count = hb_blob_get_length (cvt_blob) / FWORD::static_size;
    153    hb_blob_destroy (cvt_blob);
    154 
    155    if (!decompile_tuple_variations (axis_count, point_count,
    156                                     c->source_blob, false,
    157                                     &(c->plan->axes_old_index_tag_map),
    158                                     tuple_variations))
    159      return_trace (false);
    160 
    161    optimize_scratch_t scratch;
    162    if (!tuple_variations.instantiate (c->plan->axes_location, c->plan->axes_triple_distances, scratch))
    163      return_trace (false);
    164 
    165    if (!tuple_variations.compile_bytes (c->plan->axes_index_map, c->plan->axes_old_index_tag_map,
    166                                         false /* do not use shared points */))
    167      return_trace (false);
    168 
    169    return_trace (serialize (c->serializer, tuple_variations));
    170  }
    171 
    172  static bool add_cvt_and_apply_deltas (hb_subset_plan_t *plan,
    173                                        const TupleVariationData<> *tuple_var_data,
    174                                        const void *base)
    175  {
    176    const hb_tag_t cvt = HB_TAG('c','v','t',' ');
    177    hb_blob_t *cvt_blob = hb_face_reference_table (plan->source, cvt);
    178    hb_blob_t *cvt_prime_blob = hb_blob_copy_writable_or_fail (cvt_blob);
    179    hb_blob_destroy (cvt_blob);
    180  
    181    if (unlikely (!cvt_prime_blob))
    182      return false;
    183 
    184    unsigned cvt_blob_length = hb_blob_get_length (cvt_prime_blob);
    185    unsigned num_cvt_item = cvt_blob_length / FWORD::static_size;
    186 
    187    hb_vector_t<float> cvt_deltas;
    188    if (unlikely (!cvt_deltas.resize (num_cvt_item)))
    189    {
    190      hb_blob_destroy (cvt_prime_blob);
    191      return false;
    192    }
    193 
    194    if (!calculate_cvt_deltas (plan->normalized_coords.length, plan->normalized_coords.as_array (),
    195                               num_cvt_item, tuple_var_data, base, cvt_deltas))
    196    {
    197      hb_blob_destroy (cvt_prime_blob);
    198      return false;
    199    }
    200 
    201    FWORD *cvt_prime = (FWORD *) hb_blob_get_data_writable (cvt_prime_blob, nullptr);
    202    for (unsigned i = 0; i < num_cvt_item; i++)
    203      cvt_prime[i] += (int) roundf (cvt_deltas[i]);
    204    
    205    bool success = plan->add_table (cvt, cvt_prime_blob);
    206    hb_blob_destroy (cvt_prime_blob);
    207    return success;
    208  }
    209 
    210  protected:
    211  FixedVersion<>version;		/* Version of the CVT variation table
    212 				 * initially set to 0x00010000u */
    213  TupleVariationData<> tupleVariationData; /* TupleVariationDate for cvar table */
    214  public:
    215  DEFINE_SIZE_MIN (8);
    216 };
    217 
    218 } /* namespace OT */
    219 
    220 
    221 #endif /* HB_OT_VAR_CVAR_TABLE_HH */