/src/harfbuzz/src/hb-aat-map.cc
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright © 2009,2010  Red Hat, Inc.  | 
3  |  |  * Copyright © 2010,2011,2013  Google, Inc.  | 
4  |  |  *  | 
5  |  |  *  This is part of HarfBuzz, a text shaping library.  | 
6  |  |  *  | 
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.  | 
12  |  |  *  | 
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  | 
17  |  |  * DAMAGE.  | 
18  |  |  *  | 
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.  | 
24  |  |  *  | 
25  |  |  * Red Hat Author(s): Behdad Esfahbod  | 
26  |  |  * Google Author(s): Behdad Esfahbod  | 
27  |  |  */  | 
28  |  |  | 
29  |  | #include "hb.hh"  | 
30  |  |  | 
31  |  | #ifndef HB_NO_AAT_SHAPE  | 
32  |  |  | 
33  |  | #include "hb-aat-map.hh"  | 
34  |  |  | 
35  |  | #include "hb-aat-layout.hh"  | 
36  |  | #include "hb-aat-layout-feat-table.hh"  | 
37  |  |  | 
38  |  |  | 
39  |  | void hb_aat_map_builder_t::add_feature (const hb_feature_t &feature)  | 
40  | 0  | { | 
41  | 0  |   if (!face->table.feat->has_data ()) return;  | 
42  |  |  | 
43  | 0  |   if (feature.tag == HB_TAG ('a','a','l','t')) | 
44  | 0  |   { | 
45  | 0  |     if (!face->table.feat->exposes_feature (HB_AAT_LAYOUT_FEATURE_TYPE_CHARACTER_ALTERNATIVES))  | 
46  | 0  |       return;  | 
47  | 0  |     feature_range_t *range = features.push();  | 
48  | 0  |     range->start = feature.start;  | 
49  | 0  |     range->end = feature.end;  | 
50  | 0  |     range->info.type = HB_AAT_LAYOUT_FEATURE_TYPE_CHARACTER_ALTERNATIVES;  | 
51  | 0  |     range->info.setting = (hb_aat_layout_feature_selector_t) feature.value;  | 
52  | 0  |     range->info.seq = features.length;  | 
53  | 0  |     range->info.is_exclusive = true;  | 
54  | 0  |     return;  | 
55  | 0  |   }  | 
56  |  |  | 
57  | 0  |   const hb_aat_feature_mapping_t *mapping = hb_aat_layout_find_feature_mapping (feature.tag);  | 
58  | 0  |   if (!mapping) return;  | 
59  |  |  | 
60  | 0  |   const AAT::FeatureName* feature_name = &face->table.feat->get_feature (mapping->aatFeatureType);  | 
61  | 0  |   if (!feature_name->has_data ())  | 
62  | 0  |   { | 
63  |  |     /* Special case: Chain::compile_flags will fall back to the deprecated version of  | 
64  |  |      * small-caps if necessary, so we need to check for that possibility.  | 
65  |  |      * https://github.com/harfbuzz/harfbuzz/issues/2307 */  | 
66  | 0  |     if (mapping->aatFeatureType == HB_AAT_LAYOUT_FEATURE_TYPE_LOWER_CASE &&  | 
67  | 0  |   mapping->selectorToEnable == HB_AAT_LAYOUT_FEATURE_SELECTOR_LOWER_CASE_SMALL_CAPS)  | 
68  | 0  |     { | 
69  | 0  |       feature_name = &face->table.feat->get_feature (HB_AAT_LAYOUT_FEATURE_TYPE_LETTER_CASE);  | 
70  | 0  |       if (!feature_name->has_data ()) return;  | 
71  | 0  |     }  | 
72  | 0  |     else return;  | 
73  | 0  |   }  | 
74  |  |  | 
75  | 0  |   feature_range_t *range = features.push();  | 
76  | 0  |   range->start = feature.start;  | 
77  | 0  |   range->end = feature.end;  | 
78  | 0  |   range->info.type = mapping->aatFeatureType;  | 
79  | 0  |   range->info.setting = feature.value ? mapping->selectorToEnable : mapping->selectorToDisable;  | 
80  | 0  |   range->info.seq = features.length;  | 
81  | 0  |   range->info.is_exclusive = feature_name->is_exclusive ();  | 
82  | 0  | }  | 
83  |  |  | 
84  |  | void  | 
85  |  | hb_aat_map_builder_t::compile (hb_aat_map_t  &m)  | 
86  | 7.41k  | { | 
87  |  |   /* Compute active features per range, and compile each. */  | 
88  |  |  | 
89  |  |   /* Sort features by start/end events. */  | 
90  | 7.41k  |   hb_vector_t<feature_event_t> feature_events;  | 
91  | 7.41k  |   for (unsigned int i = 0; i < features.length; i++)  | 
92  | 0  |   { | 
93  | 0  |     auto &feature = features[i];  | 
94  |  | 
  | 
95  | 0  |     if (features[i].start == features[i].end)  | 
96  | 0  |       continue;  | 
97  |  |  | 
98  | 0  |     feature_event_t *event;  | 
99  |  | 
  | 
100  | 0  |     event = feature_events.push ();  | 
101  | 0  |     event->index = features[i].start;  | 
102  | 0  |     event->start = true;  | 
103  | 0  |     event->feature = feature.info;  | 
104  |  | 
  | 
105  | 0  |     event = feature_events.push ();  | 
106  | 0  |     event->index = features[i].end;  | 
107  | 0  |     event->start = false;  | 
108  | 0  |     event->feature = feature.info;  | 
109  | 0  |   }  | 
110  | 7.41k  |   feature_events.qsort ();  | 
111  |  |   /* Add a strategic final event. */  | 
112  | 7.41k  |   { | 
113  | 7.41k  |     feature_info_t feature;  | 
114  | 7.41k  |     feature.seq = features.length + 1;  | 
115  |  |  | 
116  | 7.41k  |     feature_event_t *event = feature_events.push ();  | 
117  | 7.41k  |     event->index = -1; /* This value does magic. */  | 
118  | 7.41k  |     event->start = false;  | 
119  | 7.41k  |     event->feature = feature;  | 
120  | 7.41k  |   }  | 
121  |  |  | 
122  |  |   /* Scan events and save features for each range. */  | 
123  | 7.41k  |   hb_sorted_vector_t<feature_info_t> active_features;  | 
124  | 7.41k  |   unsigned int last_index = 0;  | 
125  | 14.8k  |   for (unsigned int i = 0; i < feature_events.length; i++)  | 
126  | 7.38k  |   { | 
127  | 7.38k  |     feature_event_t *event = &feature_events[i];  | 
128  |  |  | 
129  | 7.38k  |     if (event->index != last_index)  | 
130  | 7.38k  |     { | 
131  |  |       /* Save a snapshot of active features and the range. */  | 
132  |  |  | 
133  |  |       /* Sort features and merge duplicates */  | 
134  | 7.38k  |       current_features = active_features;  | 
135  | 7.38k  |       range_first = last_index;  | 
136  | 7.38k  |       range_last = event->index - 1;  | 
137  | 7.38k  |       if (current_features.length)  | 
138  | 0  |       { | 
139  | 0  |   current_features.qsort ();  | 
140  | 0  |   unsigned int j = 0;  | 
141  | 0  |   for (unsigned int i = 1; i < current_features.length; i++)  | 
142  | 0  |     if (current_features[i].type != current_features[j].type ||  | 
143  |  |         /* Nonexclusive feature selectors come in even/odd pairs to turn a setting on/off  | 
144  |  |          * respectively, so we mask out the low-order bit when checking for "duplicates"  | 
145  |  |          * (selectors referring to the same feature setting) here. */  | 
146  | 0  |         (!current_features[i].is_exclusive && ((current_features[i].setting & ~1) != (current_features[j].setting & ~1))))  | 
147  | 0  |       current_features[++j] = current_features[i];  | 
148  | 0  |   current_features.shrink (j + 1);  | 
149  | 0  |       }  | 
150  |  |  | 
151  | 7.38k  |       hb_aat_layout_compile_map (this, &m);  | 
152  |  |  | 
153  | 7.38k  |       last_index = event->index;  | 
154  | 7.38k  |     }  | 
155  |  |  | 
156  | 7.38k  |     if (event->start)  | 
157  | 0  |     { | 
158  | 0  |       active_features.push (event->feature);  | 
159  | 7.38k  |     } else { | 
160  | 7.38k  |       feature_info_t *feature = active_features.lsearch (event->feature);  | 
161  | 7.38k  |       if (feature)  | 
162  | 0  |   active_features.remove_ordered (feature - active_features.arrayZ);  | 
163  | 7.38k  |     }  | 
164  | 7.38k  |   }  | 
165  |  |  | 
166  | 7.41k  |   for (auto &chain_flags : m.chain_flags)  | 
167  |  |     // With our above setup this value is one less than desired; adjust it.  | 
168  | 2.18k  |     chain_flags.tail().cluster_last = HB_FEATURE_GLOBAL_END;  | 
169  | 7.41k  | }  | 
170  |  |  | 
171  |  |  | 
172  |  | #endif  |