| 1 | /* |
| 2 | * Copyright © 2018 Adobe 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 | * Adobe Author(s): Michiharu Ariza |
| 25 | */ |
| 26 | |
| 27 | #ifndef HB_OT_VORG_TABLE_HH |
| 28 | #define HB_OT_VORG_TABLE_HH |
| 29 | |
| 30 | #include "hb-open-type.hh" |
| 31 | |
| 32 | /* |
| 33 | * VORG -- Vertical Origin Table |
| 34 | * https://docs.microsoft.com/en-us/typography/opentype/spec/vorg |
| 35 | */ |
| 36 | #define HB_OT_TAG_VORG HB_TAG('V','O','R','G') |
| 37 | |
| 38 | namespace OT { |
| 39 | |
| 40 | struct VertOriginMetric |
| 41 | { |
| 42 | int cmp (hb_codepoint_t g) const { return glyph.cmp (g); } |
| 43 | |
| 44 | bool sanitize (hb_sanitize_context_t *c) const |
| 45 | { |
| 46 | TRACE_SANITIZE (this); |
| 47 | return_trace (c->check_struct (this)); |
| 48 | } |
| 49 | |
| 50 | public: |
| 51 | HBGlyphID16 glyph; |
| 52 | FWORD vertOriginY; |
| 53 | |
| 54 | public: |
| 55 | DEFINE_SIZE_STATIC (4); |
| 56 | }; |
| 57 | |
| 58 | struct VORG |
| 59 | { |
| 60 | static constexpr hb_tag_t tableTag = HB_OT_TAG_VORG; |
| 61 | |
| 62 | bool has_data () const { return version.to_int (); } |
| 63 | |
| 64 | int get_y_origin (hb_codepoint_t glyph) const |
| 65 | { |
| 66 | unsigned int i; |
| 67 | if (!vertYOrigins.bfind (glyph, &i)) |
| 68 | return defaultVertOriginY; |
| 69 | return vertYOrigins[i].vertOriginY; |
| 70 | } |
| 71 | |
| 72 | template <typename Iterator, |
| 73 | hb_requires (hb_is_iterator (Iterator))> |
| 74 | void serialize (hb_serialize_context_t *c, |
| 75 | Iterator it, |
| 76 | FWORD defaultVertOriginY) |
| 77 | { |
| 78 | |
| 79 | if (unlikely (!c->extend_min ((*this)))) return; |
| 80 | |
| 81 | this->version.major = 1; |
| 82 | this->version.minor = 0; |
| 83 | |
| 84 | this->defaultVertOriginY = defaultVertOriginY; |
| 85 | this->vertYOrigins.len = it.len (); |
| 86 | |
| 87 | c->copy_all (it); |
| 88 | } |
| 89 | |
| 90 | bool subset (hb_subset_context_t *c) const |
| 91 | { |
| 92 | TRACE_SUBSET (this); |
| 93 | auto *vorg_prime = c->serializer->start_embed<VORG> (); |
| 94 | if (unlikely (!c->serializer->check_success (vorg_prime))) return_trace (false); |
| 95 | |
| 96 | auto it = |
| 97 | + vertYOrigins.as_array () |
| 98 | | hb_filter (c->plan->glyphset (), &VertOriginMetric::glyph) |
| 99 | | hb_map ([&] (const VertOriginMetric& _) |
| 100 | { |
| 101 | hb_codepoint_t new_glyph = HB_SET_VALUE_INVALID; |
| 102 | c->plan->new_gid_for_old_gid (_.glyph, &new_glyph); |
| 103 | |
| 104 | VertOriginMetric metric; |
| 105 | metric.glyph = new_glyph; |
| 106 | metric.vertOriginY = _.vertOriginY; |
| 107 | return metric; |
| 108 | }) |
| 109 | ; |
| 110 | |
| 111 | /* serialize the new table */ |
| 112 | vorg_prime->serialize (c->serializer, it, defaultVertOriginY); |
| 113 | return_trace (true); |
| 114 | } |
| 115 | |
| 116 | bool sanitize (hb_sanitize_context_t *c) const |
| 117 | { |
| 118 | TRACE_SANITIZE (this); |
| 119 | return_trace (c->check_struct (this) && |
| 120 | version.major == 1 && |
| 121 | vertYOrigins.sanitize (c)); |
| 122 | } |
| 123 | |
| 124 | protected: |
| 125 | FixedVersion<>version; /* Version of VORG table. Set to 0x00010000u. */ |
| 126 | FWORD defaultVertOriginY; |
| 127 | /* The default vertical origin. */ |
| 128 | SortedArray16Of<VertOriginMetric> |
| 129 | vertYOrigins; /* The array of vertical origins. */ |
| 130 | |
| 131 | public: |
| 132 | DEFINE_SIZE_ARRAY(8, vertYOrigins); |
| 133 | }; |
| 134 | } /* namespace OT */ |
| 135 | |
| 136 | #endif /* HB_OT_VORG_TABLE_HH */ |
| 137 | |