michael@0: /* GRAPHITE2 LICENSING michael@0: michael@0: Copyright 2010, SIL International michael@0: All rights reserved. michael@0: michael@0: This library is free software; you can redistribute it and/or modify michael@0: it under the terms of the GNU Lesser General Public License as published michael@0: by the Free Software Foundation; either version 2.1 of License, or michael@0: (at your option) any later version. michael@0: michael@0: This program is distributed in the hope that it will be useful, michael@0: but WITHOUT ANY WARRANTY; without even the implied warranty of michael@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU michael@0: Lesser General Public License for more details. michael@0: michael@0: You should also have received a copy of the GNU Lesser General Public michael@0: License along with this library in the file named "LICENSE". michael@0: If not, write to the Free Software Foundation, 51 Franklin Street, michael@0: Suite 500, Boston, MA 02110-1335, USA or visit their web page on the michael@0: internet at http://www.fsf.org/licenses/lgpl.html. michael@0: michael@0: Alternatively, the contents of this file may be used under the terms of the michael@0: Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public michael@0: License, as published by the Free Software Foundation, either version 2 michael@0: of the License or (at your option) any later version. michael@0: */ michael@0: #include michael@0: michael@0: #include "inc/Main.h" michael@0: #include "inc/bits.h" michael@0: #include "inc/Endian.h" michael@0: #include "inc/FeatureMap.h" michael@0: #include "inc/FeatureVal.h" michael@0: #include "graphite2/Font.h" michael@0: #include "inc/TtfUtil.h" michael@0: #include michael@0: #include "inc/Face.h" michael@0: michael@0: michael@0: using namespace graphite2; michael@0: michael@0: namespace michael@0: { michael@0: static int cmpNameAndFeatures(const void *ap, const void *bp) michael@0: { michael@0: const NameAndFeatureRef & a = *static_cast(ap), michael@0: & b = *static_cast(bp); michael@0: return (a < b ? -1 : (b < a ? 1 : 0)); michael@0: } michael@0: michael@0: const size_t FEAT_HEADER = sizeof(uint32) + 2*sizeof(uint16) + sizeof(uint32), michael@0: FEATURE_SIZE = sizeof(uint32) michael@0: + 2*sizeof(uint16) michael@0: + sizeof(uint32) michael@0: + 2*sizeof(uint16), michael@0: FEATURE_SETTING_SIZE = sizeof(int16) + sizeof(uint16); michael@0: michael@0: uint16 readFeatureSettings(const byte * p, FeatureSetting * s, size_t num_settings) michael@0: { michael@0: uint16 max_val = 0; michael@0: for (FeatureSetting * const end = s + num_settings; s != end; ++s) michael@0: { michael@0: const int16 value = be::read(p); michael@0: ::new (s) FeatureSetting(value, be::read(p)); michael@0: if (uint16(value) > max_val) max_val = value; michael@0: } michael@0: michael@0: return max_val; michael@0: } michael@0: } michael@0: michael@0: FeatureRef::FeatureRef(const Face & face, michael@0: unsigned short & bits_offset, uint32 max_val, michael@0: uint32 name, uint16 uiName, uint16 flags, michael@0: FeatureSetting *settings, uint16 num_set) throw() michael@0: : m_pFace(&face), michael@0: m_nameValues(settings), michael@0: m_mask(mask_over_val(max_val)), michael@0: m_max(max_val), michael@0: m_id(name), michael@0: m_nameid(uiName), michael@0: m_flags(flags), michael@0: m_numSet(num_set) michael@0: { michael@0: const uint8 need_bits = bit_set_count(m_mask); michael@0: m_index = (bits_offset + need_bits) / SIZEOF_CHUNK; michael@0: if (m_index > bits_offset / SIZEOF_CHUNK) michael@0: bits_offset = m_index*SIZEOF_CHUNK; michael@0: m_bits = bits_offset % SIZEOF_CHUNK; michael@0: bits_offset += need_bits; michael@0: m_mask <<= m_bits; michael@0: } michael@0: michael@0: FeatureRef::~FeatureRef() throw() michael@0: { michael@0: free(m_nameValues); michael@0: } michael@0: michael@0: bool FeatureMap::readFeats(const Face & face) michael@0: { michael@0: const Face::Table feat(face, TtfUtil::Tag::Feat); michael@0: const byte * p = feat; michael@0: if (!p) return true; michael@0: if (feat.size() < FEAT_HEADER) return false; michael@0: michael@0: const byte *const feat_start = p, michael@0: *const feat_end = p + feat.size(); michael@0: michael@0: const uint32 version = be::read(p); michael@0: m_numFeats = be::read(p); michael@0: be::skip(p); michael@0: be::skip(p); michael@0: michael@0: // Sanity checks michael@0: if (m_numFeats == 0) return true; michael@0: if (version < 0x00010000 || michael@0: p + m_numFeats*FEATURE_SIZE > feat_end) michael@0: { //defensive michael@0: m_numFeats = 0; michael@0: return false; michael@0: } michael@0: michael@0: m_feats = new FeatureRef [m_numFeats]; michael@0: uint16 * const defVals = gralloc(m_numFeats); michael@0: if (!defVals || !m_feats) return false; michael@0: unsigned short bits = 0; //to cause overflow on first Feature michael@0: michael@0: for (int i = 0, ie = m_numFeats; i != ie; i++) michael@0: { michael@0: const uint32 label = version < 0x00020000 ? be::read(p) : be::read(p); michael@0: const uint16 num_settings = be::read(p); michael@0: if (version >= 0x00020000) michael@0: be::skip(p); michael@0: const byte * const feat_setts = feat_start + be::read(p); michael@0: const uint16 flags = be::read(p), michael@0: uiName = be::read(p); michael@0: michael@0: if (feat_setts + num_settings * FEATURE_SETTING_SIZE > feat_end) michael@0: { michael@0: free(defVals); michael@0: return false; michael@0: } michael@0: michael@0: FeatureSetting *uiSet; michael@0: uint32 maxVal; michael@0: if (num_settings != 0) michael@0: { michael@0: uiSet = gralloc(num_settings); michael@0: if (!uiSet) michael@0: { michael@0: free(defVals); michael@0: return false; michael@0: } michael@0: maxVal = readFeatureSettings(feat_setts, uiSet, num_settings); michael@0: defVals[i] = uiSet[0].value(); michael@0: } michael@0: else michael@0: { michael@0: uiSet = 0; michael@0: maxVal = 0xffffffff; michael@0: defVals[i] = 0; michael@0: } michael@0: michael@0: ::new (m_feats + i) FeatureRef (face, bits, maxVal, michael@0: label, uiName, flags, michael@0: uiSet, num_settings); michael@0: } michael@0: m_defaultFeatures = new Features(bits/(sizeof(uint32)*8) + 1, *this); michael@0: m_pNamedFeats = new NameAndFeatureRef[m_numFeats]; michael@0: if (!m_defaultFeatures || !m_pNamedFeats) michael@0: { michael@0: free(defVals); michael@0: return false; michael@0: } michael@0: for (int i = 0; i < m_numFeats; ++i) michael@0: { michael@0: m_feats[i].applyValToFeature(defVals[i], *m_defaultFeatures); michael@0: m_pNamedFeats[i] = m_feats+i; michael@0: } michael@0: michael@0: free(defVals); michael@0: michael@0: qsort(m_pNamedFeats, m_numFeats, sizeof(NameAndFeatureRef), &cmpNameAndFeatures); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool SillMap::readFace(const Face & face) michael@0: { michael@0: if (!m_FeatureMap.readFeats(face)) return false; michael@0: if (!readSill(face)) return false; michael@0: return true; michael@0: } michael@0: michael@0: michael@0: bool SillMap::readSill(const Face & face) michael@0: { michael@0: const Face::Table sill(face, TtfUtil::Tag::Sill); michael@0: const byte *p = sill; michael@0: michael@0: if (!p) return true; michael@0: if (sill.size() < 12) return false; michael@0: if (be::read(p) != 0x00010000UL) return false; michael@0: m_numLanguages = be::read(p); michael@0: m_langFeats = new LangFeaturePair[m_numLanguages]; michael@0: if (!m_langFeats || !m_FeatureMap.m_numFeats) { m_numLanguages = 0; return true; } //defensive michael@0: michael@0: p += 6; // skip the fast search michael@0: if (sill.size() < m_numLanguages * 8U + 12) return false; michael@0: michael@0: for (int i = 0; i < m_numLanguages; i++) michael@0: { michael@0: uint32 langid = be::read(p); michael@0: uint16 numSettings = be::read(p); michael@0: uint16 offset = be::read(p); michael@0: if (offset + 8U * numSettings > sill.size() && numSettings > 0) return false; michael@0: Features* feats = new Features(*m_FeatureMap.m_defaultFeatures); michael@0: if (!feats) return false; michael@0: const byte *pLSet = sill + offset; michael@0: michael@0: // Apply langauge specific settings michael@0: for (int j = 0; j < numSettings; j++) michael@0: { michael@0: uint32 name = be::read(pLSet); michael@0: uint16 val = be::read(pLSet); michael@0: pLSet += 2; michael@0: const FeatureRef* pRef = m_FeatureMap.findFeatureRef(name); michael@0: if (pRef) pRef->applyValToFeature(val, *feats); michael@0: } michael@0: // Add the language id feature which is always feature id 1 michael@0: const FeatureRef* pRef = m_FeatureMap.findFeatureRef(1); michael@0: if (pRef) pRef->applyValToFeature(langid, *feats); michael@0: michael@0: m_langFeats[i].m_lang = langid; michael@0: m_langFeats[i].m_pFeatures = feats; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: michael@0: Features* SillMap::cloneFeatures(uint32 langname/*0 means default*/) const michael@0: { michael@0: if (langname) michael@0: { michael@0: // the number of languages in a font is usually small e.g. 8 in Doulos michael@0: // so this loop is not very expensive michael@0: for (uint16 i = 0; i < m_numLanguages; i++) michael@0: { michael@0: if (m_langFeats[i].m_lang == langname) michael@0: return new Features(*m_langFeats[i].m_pFeatures); michael@0: } michael@0: } michael@0: return new Features (*m_FeatureMap.m_defaultFeatures); michael@0: } michael@0: michael@0: michael@0: michael@0: const FeatureRef *FeatureMap::findFeatureRef(uint32 name) const michael@0: { michael@0: NameAndFeatureRef *it; michael@0: michael@0: for (it = m_pNamedFeats; it < m_pNamedFeats + m_numFeats; ++it) michael@0: if (it->m_name == name) michael@0: return it->m_pFRef; michael@0: return NULL; michael@0: } michael@0: michael@0: bool FeatureRef::applyValToFeature(uint32 val, Features & pDest) const michael@0: { michael@0: if (val>maxVal() || !m_pFace) michael@0: return false; michael@0: if (pDest.m_pMap==NULL) michael@0: pDest.m_pMap = &m_pFace->theSill().theFeatureMap(); michael@0: else michael@0: if (pDest.m_pMap!=&m_pFace->theSill().theFeatureMap()) michael@0: return false; //incompatible michael@0: pDest.reserve(m_index); michael@0: pDest[m_index] &= ~m_mask; michael@0: pDest[m_index] |= (uint32(val) << m_bits); michael@0: return true; michael@0: } michael@0: michael@0: uint32 FeatureRef::getFeatureVal(const Features& feats) const michael@0: { michael@0: if (m_index < feats.size() && &m_pFace->theSill().theFeatureMap()==feats.m_pMap) michael@0: return (feats[m_index] & m_mask) >> m_bits; michael@0: else michael@0: return 0; michael@0: } michael@0: michael@0: