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: michael@0: #ifndef GRAPHITE2_NSEGCACHE michael@0: michael@0: #include michael@0: #include "inc/CachedFace.h" michael@0: #include "inc/SegCacheStore.h" michael@0: michael@0: michael@0: using namespace graphite2; michael@0: michael@0: CachedFace::CachedFace(const void* appFaceHandle/*non-NULL*/, const gr_face_ops & ops) michael@0: : Face(appFaceHandle, ops), m_cacheStore(0) michael@0: { michael@0: } michael@0: michael@0: CachedFace::~CachedFace() michael@0: { michael@0: delete m_cacheStore; michael@0: } michael@0: michael@0: bool CachedFace::setupCache(unsigned int cacheSize) michael@0: { michael@0: m_cacheStore = new SegCacheStore(*this, m_numSilf, cacheSize); michael@0: return bool(m_cacheStore); michael@0: } michael@0: michael@0: michael@0: bool CachedFace::runGraphite(Segment *seg, const Silf *pSilf) const michael@0: { michael@0: assert(pSilf); michael@0: pSilf->runGraphite(seg, 0, pSilf->substitutionPass()); michael@0: michael@0: unsigned int silfIndex = 0; michael@0: for (; silfIndex < m_numSilf && &(m_silfs[silfIndex]) != pSilf; ++silfIndex); michael@0: if (silfIndex == m_numSilf) return false; michael@0: SegCache * const segCache = m_cacheStore->getOrCreate(silfIndex, seg->getFeatures(0)); michael@0: if (!segCache) michael@0: return false; michael@0: michael@0: assert(m_cacheStore); michael@0: // find where the segment can be broken michael@0: Slot * subSegStartSlot = seg->first(); michael@0: Slot * subSegEndSlot = subSegStartSlot; michael@0: uint16 cmapGlyphs[eMaxSpliceSize]; michael@0: int subSegStart = 0; michael@0: for (unsigned int i = 0; i < seg->charInfoCount(); ++i) michael@0: { michael@0: const unsigned int length = i - subSegStart + 1; michael@0: if (length < eMaxSpliceSize) michael@0: cmapGlyphs[length-1] = subSegEndSlot->gid(); michael@0: else return false; michael@0: const bool spaceOnly = m_cacheStore->isSpaceGlyph(subSegEndSlot->gid()); michael@0: // at this stage the character to slot mapping is still 1 to 1 michael@0: const int breakWeight = seg->charinfo(i)->breakWeight(), michael@0: nextBreakWeight = (i + 1 < seg->charInfoCount())? michael@0: seg->charinfo(i+1)->breakWeight() : 0; michael@0: const uint8 f = seg->charinfo(i)->flags(); michael@0: if (((spaceOnly michael@0: || (breakWeight > 0 && breakWeight <= gr_breakWord) michael@0: || i + 1 == seg->charInfoCount() michael@0: || ((nextBreakWeight < 0 && nextBreakWeight >= gr_breakBeforeWord) michael@0: || (subSegEndSlot->next() && m_cacheStore->isSpaceGlyph(subSegEndSlot->next()->gid())))) michael@0: && f != 1) michael@0: || f == 2) michael@0: { michael@0: // record the next slot before any splicing michael@0: Slot * nextSlot = subSegEndSlot->next(); michael@0: // spaces should be left untouched by graphite rules in any sane font michael@0: if (!spaceOnly) michael@0: { michael@0: // found a break position, check for a cache of the sub sequence michael@0: const SegCacheEntry * entry = segCache->find(cmapGlyphs, length); michael@0: // TODO disable cache for words at start/end of line with contextuals michael@0: if (!entry) michael@0: { michael@0: SegmentScopeState scopeState = seg->setScope(subSegStartSlot, subSegEndSlot, length); michael@0: pSilf->runGraphite(seg, pSilf->substitutionPass(), pSilf->numPasses()); michael@0: if (length < eMaxSpliceSize) michael@0: { michael@0: seg->associateChars(subSegStart, length); michael@0: segCache->cache(m_cacheStore, cmapGlyphs, length, seg, subSegStart); michael@0: } michael@0: seg->removeScope(scopeState); michael@0: } michael@0: else michael@0: seg->splice(subSegStart, length, subSegStartSlot, subSegEndSlot, michael@0: entry->first(), entry->glyphLength()); michael@0: } michael@0: subSegStartSlot = subSegEndSlot = nextSlot; michael@0: subSegStart = i + 1; michael@0: } michael@0: else michael@0: { michael@0: subSegEndSlot = subSegEndSlot->next(); michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: #endif michael@0: