| 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 | GlyphID 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 | bool _subset (const hb_subset_plan_t *plan HB_UNUSED, |
| 73 | const VORG *vorg_table, |
| 74 | const hb_vector_t<VertOriginMetric> &subset_metrics, |
| 75 | unsigned int dest_sz, |
| 76 | void *dest) const |
| 77 | { |
| 78 | hb_serialize_context_t c (dest, dest_sz); |
| 79 | |
| 80 | VORG *subset_table = c.start_serialize<VORG> (); |
| 81 | if (unlikely (!c.extend_min (*subset_table))) |
| 82 | return false; |
| 83 | |
| 84 | subset_table->version.major.set (1); |
| 85 | subset_table->version.minor.set (0); |
| 86 | |
| 87 | subset_table->defaultVertOriginY.set (vorg_table->defaultVertOriginY); |
| 88 | subset_table->vertYOrigins.len.set (subset_metrics.length); |
| 89 | |
| 90 | bool success = true; |
| 91 | if (subset_metrics.length > 0) |
| 92 | { |
| 93 | unsigned int size = VertOriginMetric::static_size * subset_metrics.length; |
| 94 | VertOriginMetric *metrics = c.allocate_size<VertOriginMetric> (size); |
| 95 | if (likely (metrics != nullptr)) |
| 96 | memcpy (metrics, &subset_metrics[0], size); |
| 97 | else |
| 98 | success = false; |
| 99 | } |
| 100 | c.end_serialize (); |
| 101 | |
| 102 | return success; |
| 103 | } |
| 104 | |
| 105 | bool subset (hb_subset_plan_t *plan) const |
| 106 | { |
| 107 | hb_blob_t *vorg_blob = hb_sanitize_context_t().reference_table<VORG> (plan->source); |
| 108 | const VORG *vorg_table = vorg_blob->as<VORG> (); |
| 109 | |
| 110 | /* count the number of glyphs to be included in the subset table */ |
| 111 | hb_vector_t<VertOriginMetric> subset_metrics; |
| 112 | subset_metrics.init (); |
| 113 | unsigned int glyph = 0; |
| 114 | unsigned int i = 0; |
| 115 | while ((glyph < plan->glyphs.length) && (i < vertYOrigins.len)) |
| 116 | { |
| 117 | if (plan->glyphs[glyph] > vertYOrigins[i].glyph) |
| 118 | i++; |
| 119 | else if (plan->glyphs[glyph] < vertYOrigins[i].glyph) |
| 120 | glyph++; |
| 121 | else |
| 122 | { |
| 123 | VertOriginMetric *metrics = subset_metrics.push (); |
| 124 | metrics->glyph.set (glyph); |
| 125 | metrics->vertOriginY.set (vertYOrigins[i].vertOriginY); |
| 126 | glyph++; |
| 127 | i++; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* alloc the new table */ |
| 132 | unsigned int dest_sz = VORG::min_size + VertOriginMetric::static_size * subset_metrics.length; |
| 133 | void *dest = (void *) malloc (dest_sz); |
| 134 | if (unlikely (!dest)) |
| 135 | { |
| 136 | subset_metrics.fini (); |
| 137 | hb_blob_destroy (vorg_blob); |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | /* serialize the new table */ |
| 142 | if (!_subset (plan, vorg_table, subset_metrics, dest_sz, dest)) |
| 143 | { |
| 144 | subset_metrics.fini (); |
| 145 | free (dest); |
| 146 | hb_blob_destroy (vorg_blob); |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | hb_blob_t *result = hb_blob_create ((const char *)dest, |
| 151 | dest_sz, |
| 152 | HB_MEMORY_MODE_READONLY, |
| 153 | dest, |
| 154 | free); |
| 155 | bool success = plan->add_table (HB_OT_TAG_VORG, result); |
| 156 | hb_blob_destroy (result); |
| 157 | subset_metrics.fini (); |
| 158 | hb_blob_destroy (vorg_blob); |
| 159 | return success; |
| 160 | } |
| 161 | |
| 162 | bool sanitize (hb_sanitize_context_t *c) const |
| 163 | { |
| 164 | TRACE_SANITIZE (this); |
| 165 | return_trace (c->check_struct (this) && |
| 166 | version.major == 1 && |
| 167 | vertYOrigins.sanitize (c)); |
| 168 | } |
| 169 | |
| 170 | protected: |
| 171 | FixedVersion<> version; /* Version of VORG table. Set to 0x00010000u. */ |
| 172 | FWORD defaultVertOriginY; /* The default vertical origin. */ |
| 173 | SortedArrayOf<VertOriginMetric> |
| 174 | vertYOrigins; /* The array of vertical origins. */ |
| 175 | |
| 176 | public: |
| 177 | DEFINE_SIZE_ARRAY(8, vertYOrigins); |
| 178 | }; |
| 179 | } /* namespace OT */ |
| 180 | |
| 181 | #endif /* HB_OT_VORG_TABLE_HH */ |
| 182 | |