Path: blob/master/thirdparty/harfbuzz/src/hb-aat-map.cc
9903 views
/*1* Copyright © 2009,2010 Red Hat, Inc.2* Copyright © 2010,2011,2013 Google, Inc.3*4* This is part of HarfBuzz, a text shaping library.5*6* Permission is hereby granted, without written agreement and without7* license or royalty fees, to use, copy, modify, and distribute this8* software and its documentation for any purpose, provided that the9* above copyright notice and the following two paragraphs appear in10* all copies of this software.11*12* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR13* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES14* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN15* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH16* DAMAGE.17*18* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,19* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND20* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS21* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO22* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.23*24* Red Hat Author(s): Behdad Esfahbod25* Google Author(s): Behdad Esfahbod26*/2728#include "hb.hh"2930#ifndef HB_NO_AAT_SHAPE3132#include "hb-aat-map.hh"3334#include "hb-aat-layout.hh"35#include "hb-aat-layout-feat-table.hh"363738void hb_aat_map_builder_t::add_feature (const hb_feature_t &feature)39{40if (!face->table.feat->has_data ()) return;4142if (feature.tag == HB_TAG ('a','a','l','t'))43{44if (!face->table.feat->exposes_feature (HB_AAT_LAYOUT_FEATURE_TYPE_CHARACTER_ALTERNATIVES))45return;46feature_range_t *range = features.push();47range->start = feature.start;48range->end = feature.end;49range->info.type = HB_AAT_LAYOUT_FEATURE_TYPE_CHARACTER_ALTERNATIVES;50range->info.setting = (hb_aat_layout_feature_selector_t) feature.value;51range->info.seq = features.length;52range->info.is_exclusive = true;53return;54}5556const hb_aat_feature_mapping_t *mapping = hb_aat_layout_find_feature_mapping (feature.tag);57if (!mapping) return;5859const AAT::FeatureName* feature_name = &face->table.feat->get_feature (mapping->aatFeatureType);60if (!feature_name->has_data ())61{62/* Special case: Chain::compile_flags will fall back to the deprecated version of63* small-caps if necessary, so we need to check for that possibility.64* https://github.com/harfbuzz/harfbuzz/issues/2307 */65if (mapping->aatFeatureType == HB_AAT_LAYOUT_FEATURE_TYPE_LOWER_CASE &&66mapping->selectorToEnable == HB_AAT_LAYOUT_FEATURE_SELECTOR_LOWER_CASE_SMALL_CAPS)67{68feature_name = &face->table.feat->get_feature (HB_AAT_LAYOUT_FEATURE_TYPE_LETTER_CASE);69if (!feature_name->has_data ()) return;70}71else return;72}7374feature_range_t *range = features.push();75range->start = feature.start;76range->end = feature.end;77range->info.type = mapping->aatFeatureType;78range->info.setting = feature.value ? mapping->selectorToEnable : mapping->selectorToDisable;79range->info.seq = features.length;80range->info.is_exclusive = feature_name->is_exclusive ();81}8283void84hb_aat_map_builder_t::compile (hb_aat_map_t &m)85{86/* Compute active features per range, and compile each. */87if (!features.length)88{89hb_aat_layout_compile_map (this, &m);90return;91}9293/* Sort features by start/end events. */94hb_vector_t<feature_event_t> feature_events;95feature_events.alloc_exact (features.length * 2 + 1);96for (unsigned int i = 0; i < features.length; i++)97{98auto &feature = features.arrayZ[i];99100if (feature.start == feature.end)101continue;102103feature_event_t *event;104105event = feature_events.push ();106event->index = feature.start;107event->start = true;108event->feature = feature.info;109110event = feature_events.push ();111event->index = feature.end;112event->start = false;113event->feature = feature.info;114}115feature_events.qsort ();116/* Add a strategic final event. */117{118feature_info_t feature;119feature.seq = features.length + 1;120121feature_event_t *event = feature_events.push ();122event->index = -1; /* This value does magic. */123event->start = false;124event->feature = feature;125}126127/* Scan events and save features for each range. */128hb_sorted_vector_t<feature_info_t> active_features;129unsigned int last_index = 0;130for (unsigned int i = 0; i < feature_events.length; i++)131{132feature_event_t *event = &feature_events[i];133134if (event->index != last_index)135{136/* Save a snapshot of active features and the range. */137138/* Sort features and merge duplicates */139current_features = active_features;140range_first = last_index;141range_last = event->index - 1;142if (current_features.length)143{144current_features.qsort ();145unsigned int j = 0;146for (unsigned int i = 1; i < current_features.length; i++)147if (current_features.arrayZ[i].type != current_features.arrayZ[j].type ||148/* Nonexclusive feature selectors come in even/odd pairs to turn a setting on/off149* respectively, so we mask out the low-order bit when checking for "duplicates"150* (selectors referring to the same feature setting) here. */151(!current_features.arrayZ[i].is_exclusive && ((current_features.arrayZ[i].setting & ~1) != (current_features.arrayZ[j].setting & ~1))))152current_features.arrayZ[++j] = current_features.arrayZ[i];153current_features.shrink (j + 1);154}155156hb_aat_layout_compile_map (this, &m);157158last_index = event->index;159}160161if (event->start)162{163active_features.push (event->feature);164} else {165feature_info_t *feature = active_features.lsearch (event->feature);166if (feature)167active_features.remove_ordered (feature - active_features.arrayZ);168}169}170171for (auto &chain_flags : m.chain_flags)172// With our above setup this value is one less than desired; adjust it.173chain_flags.tail().cluster_last = HB_FEATURE_GLOBAL_END;174}175176177#endif178179180