gfx/graphite2/src/CachedFace.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/graphite2/src/CachedFace.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/*  GRAPHITE2 LICENSING
     1.5 +
     1.6 +    Copyright 2010, SIL International
     1.7 +    All rights reserved.
     1.8 +
     1.9 +    This library is free software; you can redistribute it and/or modify
    1.10 +    it under the terms of the GNU Lesser General Public License as published
    1.11 +    by the Free Software Foundation; either version 2.1 of License, or
    1.12 +    (at your option) any later version.
    1.13 +
    1.14 +    This program is distributed in the hope that it will be useful,
    1.15 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.17 +    Lesser General Public License for more details.
    1.18 +
    1.19 +    You should also have received a copy of the GNU Lesser General Public
    1.20 +    License along with this library in the file named "LICENSE".
    1.21 +    If not, write to the Free Software Foundation, 51 Franklin Street,
    1.22 +    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
    1.23 +    internet at http://www.fsf.org/licenses/lgpl.html.
    1.24 +
    1.25 +Alternatively, the contents of this file may be used under the terms of the
    1.26 +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
    1.27 +License, as published by the Free Software Foundation, either version 2
    1.28 +of the License or (at your option) any later version.
    1.29 +*/
    1.30 +
    1.31 +#ifndef GRAPHITE2_NSEGCACHE
    1.32 +
    1.33 +#include <graphite2/Segment.h>
    1.34 +#include "inc/CachedFace.h"
    1.35 +#include "inc/SegCacheStore.h"
    1.36 +
    1.37 +
    1.38 +using namespace graphite2;
    1.39 +
    1.40 +CachedFace::CachedFace(const void* appFaceHandle/*non-NULL*/, const gr_face_ops & ops)
    1.41 +: Face(appFaceHandle, ops), m_cacheStore(0)
    1.42 +{
    1.43 +}
    1.44 +
    1.45 +CachedFace::~CachedFace()
    1.46 +{
    1.47 +    delete m_cacheStore;
    1.48 +}
    1.49 +
    1.50 +bool CachedFace::setupCache(unsigned int cacheSize)
    1.51 +{
    1.52 +    m_cacheStore = new SegCacheStore(*this, m_numSilf, cacheSize);
    1.53 +    return bool(m_cacheStore);
    1.54 +}
    1.55 +
    1.56 +
    1.57 +bool CachedFace::runGraphite(Segment *seg, const Silf *pSilf) const
    1.58 +{
    1.59 +    assert(pSilf);
    1.60 +    pSilf->runGraphite(seg, 0, pSilf->substitutionPass());
    1.61 +
    1.62 +    unsigned int silfIndex = 0;
    1.63 +    for (; silfIndex < m_numSilf && &(m_silfs[silfIndex]) != pSilf; ++silfIndex);
    1.64 +    if (silfIndex == m_numSilf)  return false;
    1.65 +    SegCache * const segCache = m_cacheStore->getOrCreate(silfIndex, seg->getFeatures(0));
    1.66 +    if (!segCache)
    1.67 +        return false;
    1.68 +
    1.69 +    assert(m_cacheStore);
    1.70 +    // find where the segment can be broken
    1.71 +    Slot * subSegStartSlot = seg->first();
    1.72 +    Slot * subSegEndSlot = subSegStartSlot;
    1.73 +    uint16 cmapGlyphs[eMaxSpliceSize];
    1.74 +    int subSegStart = 0;
    1.75 +    for (unsigned int i = 0; i < seg->charInfoCount(); ++i)
    1.76 +    {
    1.77 +        const unsigned int length = i - subSegStart + 1;
    1.78 +        if (length < eMaxSpliceSize)
    1.79 +            cmapGlyphs[length-1] = subSegEndSlot->gid();
    1.80 +        else return false;
    1.81 +        const bool spaceOnly = m_cacheStore->isSpaceGlyph(subSegEndSlot->gid());
    1.82 +        // at this stage the character to slot mapping is still 1 to 1
    1.83 +        const int   breakWeight = seg->charinfo(i)->breakWeight(),
    1.84 +                    nextBreakWeight = (i + 1 < seg->charInfoCount())?
    1.85 +                            seg->charinfo(i+1)->breakWeight() : 0;
    1.86 +        const uint8 f = seg->charinfo(i)->flags();
    1.87 +        if (((spaceOnly
    1.88 +                || (breakWeight > 0 && breakWeight <= gr_breakWord)
    1.89 +                || i + 1 == seg->charInfoCount()
    1.90 +                || ((nextBreakWeight < 0 && nextBreakWeight >= gr_breakBeforeWord)
    1.91 +                    || (subSegEndSlot->next() && m_cacheStore->isSpaceGlyph(subSegEndSlot->next()->gid()))))
    1.92 +                && f != 1)
    1.93 +            || f == 2)
    1.94 +        {
    1.95 +            // record the next slot before any splicing
    1.96 +            Slot * nextSlot = subSegEndSlot->next();
    1.97 +            // spaces should be left untouched by graphite rules in any sane font
    1.98 +            if (!spaceOnly)
    1.99 +            {
   1.100 +                // found a break position, check for a cache of the sub sequence
   1.101 +                const SegCacheEntry * entry = segCache->find(cmapGlyphs, length);
   1.102 +                // TODO disable cache for words at start/end of line with contextuals
   1.103 +                if (!entry)
   1.104 +                {
   1.105 +                    SegmentScopeState scopeState = seg->setScope(subSegStartSlot, subSegEndSlot, length);
   1.106 +                    pSilf->runGraphite(seg, pSilf->substitutionPass(), pSilf->numPasses());
   1.107 +                    if (length < eMaxSpliceSize)
   1.108 +                    {
   1.109 +                        seg->associateChars(subSegStart, length);
   1.110 +                        segCache->cache(m_cacheStore, cmapGlyphs, length, seg, subSegStart);
   1.111 +                    }
   1.112 +                    seg->removeScope(scopeState);
   1.113 +                }
   1.114 +                else
   1.115 +                    seg->splice(subSegStart, length, subSegStartSlot, subSegEndSlot,
   1.116 +                        entry->first(), entry->glyphLength());
   1.117 +            }
   1.118 +            subSegStartSlot = subSegEndSlot = nextSlot;
   1.119 +            subSegStart = i + 1;
   1.120 +        }
   1.121 +        else
   1.122 +        {
   1.123 +            subSegEndSlot = subSegEndSlot->next();
   1.124 +        }
   1.125 +    }
   1.126 +    return true;
   1.127 +}
   1.128 +
   1.129 +#endif
   1.130 +

mercurial