2 * Copyright © 2011,2012,2013 Google, Inc.
3 * Copyright © 2021 Khaled Hosny
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Google Author(s): Behdad Esfahbod
28 #ifndef HB_MS_FEATURE_RANGES_HH
29 #define HB_MS_FEATURE_RANGES_HH
33 typedef struct hb_ms_feature_t {
38 typedef struct hb_ms_features_t {
39 hb_ms_feature_t *features;
40 uint32_t num_features;
43 struct hb_ms_active_feature_t {
47 HB_INTERNAL static int cmp (const void *pa, const void *pb) {
48 const auto *a = (const hb_ms_active_feature_t *) pa;
49 const auto *b = (const hb_ms_active_feature_t *) pb;
50 return a->fea.tag_le < b->fea.tag_le ? -1 : a->fea.tag_le > b->fea.tag_le ? 1 :
51 a->order < b->order ? -1 : a->order > b->order ? 1 :
52 a->fea.value < b->fea.value ? -1 : a->fea.value > b->fea.value ? 1 :
55 bool operator== (const hb_ms_active_feature_t& f) const
56 { return cmp (this, &f) == 0; }
59 struct hb_ms_feature_event_t {
62 hb_ms_active_feature_t feature;
64 HB_INTERNAL static int cmp (const void *pa, const void *pb)
66 const auto *a = (const hb_ms_feature_event_t *) pa;
67 const auto *b = (const hb_ms_feature_event_t *) pb;
68 return a->index < b->index ? -1 : a->index > b->index ? 1 :
69 a->start < b->start ? -1 : a->start > b->start ? 1 :
70 hb_ms_active_feature_t::cmp (&a->feature, &b->feature);
74 struct hb_ms_range_record_t {
75 hb_ms_features_t features;
76 unsigned int index_first; /* == start */
77 unsigned int index_last; /* == end - 1 */
81 hb_ms_setup_features (const hb_feature_t *features,
82 unsigned int num_features,
83 hb_vector_t<hb_ms_feature_t> &feature_records, /* OUT */
84 hb_vector_t<hb_ms_range_record_t> &range_records /* OUT */)
86 feature_records.shrink(0);
87 range_records.shrink(0);
89 /* Sort features by start/end events. */
90 hb_vector_t<hb_ms_feature_event_t> feature_events;
91 for (unsigned int i = 0; i < num_features; i++)
93 hb_ms_active_feature_t feature;
94 feature.fea.tag_le = hb_uint32_swap (features[i].tag);
95 feature.fea.value = features[i].value;
98 hb_ms_feature_event_t *event;
100 event = feature_events.push ();
101 event->index = features[i].start;
103 event->feature = feature;
105 event = feature_events.push ();
106 event->index = features[i].end;
107 event->start = false;
108 event->feature = feature;
110 feature_events.qsort ();
111 /* Add a strategic final event. */
113 hb_ms_active_feature_t feature;
114 feature.fea.tag_le = 0;
115 feature.fea.value = 0;
116 feature.order = num_features + 1;
118 auto *event = feature_events.push ();
119 event->index = 0; /* This value does magic. */
120 event->start = false;
121 event->feature = feature;
124 /* Scan events and save features for each range. */
125 hb_vector_t<hb_ms_active_feature_t> active_features;
126 unsigned int last_index = 0;
127 for (unsigned int i = 0; i < feature_events.length; i++)
129 auto *event = &feature_events[i];
131 if (event->index != last_index)
133 /* Save a snapshot of active features and the range. */
134 auto *range = range_records.push ();
135 auto offset = feature_records.length;
137 active_features.qsort ();
138 for (unsigned int j = 0; j < active_features.length; j++)
140 if (!j || active_features[j].fea.tag_le != feature_records[feature_records.length - 1].tag_le)
142 feature_records.push (active_features[j].fea);
146 /* Overrides value for existing feature. */
147 feature_records[feature_records.length - 1].value = active_features[j].fea.value;
151 /* Will convert to pointer after all is ready, since feature_records.array
152 * may move as we grow it. */
153 range->features.features = reinterpret_cast<hb_ms_feature_t *> (offset);
154 range->features.num_features = feature_records.length - offset;
155 range->index_first = last_index;
156 range->index_last = event->index - 1;
158 last_index = event->index;
163 active_features.push (event->feature);
167 auto *feature = active_features.lsearch (event->feature);
169 active_features.remove (feature - active_features.arrayZ);
173 if (!range_records.length) /* No active feature found. */
176 /* Fixup the pointers. */
177 for (unsigned int i = 0; i < range_records.length; i++)
179 auto *range = &range_records[i];
180 range->features.features = (hb_ms_feature_t *) feature_records + reinterpret_cast<uintptr_t> (range->features.features);
183 return !!num_features;
187 hb_ms_make_feature_ranges (hb_vector_t<hb_ms_feature_t> &feature_records,
188 hb_vector_t<hb_ms_range_record_t> &range_records,
189 unsigned int chars_offset,
190 unsigned int chars_len,
191 uint16_t *log_clusters,
192 hb_vector_t<hb_ms_features_t*> &range_features, /* OUT */
193 hb_vector_t<uint32_t> &range_counts /* OUT */)
195 range_features.shrink (0);
196 range_counts.shrink (0);
198 auto *last_range = &range_records[0];
199 for (unsigned int i = chars_offset; i < chars_len; i++)
201 auto *range = last_range;
202 while (log_clusters[i] < range->index_first)
204 while (log_clusters[i] > range->index_last)
206 if (!range_features.length ||
207 &range->features != range_features[range_features.length - 1])
209 auto **features = range_features.push ();
210 auto *c = range_counts.push ();
211 if (unlikely (!features || !c))
213 range_features.shrink (0);
214 range_counts.shrink (0);
217 *features = &range->features;
222 range_counts[range_counts.length - 1]++;
229 #endif /* HB_MS_FEATURE_RANGES_HH */