|
1 /* GRAPHITE2 LICENSING |
|
2 |
|
3 Copyright 2010, SIL International |
|
4 All rights reserved. |
|
5 |
|
6 This library is free software; you can redistribute it and/or modify |
|
7 it under the terms of the GNU Lesser General Public License as published |
|
8 by the Free Software Foundation; either version 2.1 of License, or |
|
9 (at your option) any later version. |
|
10 |
|
11 This program is distributed in the hope that it will be useful, |
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 Lesser General Public License for more details. |
|
15 |
|
16 You should also have received a copy of the GNU Lesser General Public |
|
17 License along with this library in the file named "LICENSE". |
|
18 If not, write to the Free Software Foundation, 51 Franklin Street, |
|
19 Suite 500, Boston, MA 02110-1335, USA or visit their web page on the |
|
20 internet at http://www.fsf.org/licenses/lgpl.html. |
|
21 |
|
22 Alternatively, the contents of this file may be used under the terms of the |
|
23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public |
|
24 License, as published by the Free Software Foundation, either version 2 |
|
25 of the License or (at your option) any later version. |
|
26 */ |
|
27 |
|
28 #ifndef GRAPHITE2_NSEGCACHE |
|
29 |
|
30 #include <graphite2/Segment.h> |
|
31 #include "inc/CachedFace.h" |
|
32 #include "inc/SegCacheStore.h" |
|
33 |
|
34 |
|
35 using namespace graphite2; |
|
36 |
|
37 CachedFace::CachedFace(const void* appFaceHandle/*non-NULL*/, const gr_face_ops & ops) |
|
38 : Face(appFaceHandle, ops), m_cacheStore(0) |
|
39 { |
|
40 } |
|
41 |
|
42 CachedFace::~CachedFace() |
|
43 { |
|
44 delete m_cacheStore; |
|
45 } |
|
46 |
|
47 bool CachedFace::setupCache(unsigned int cacheSize) |
|
48 { |
|
49 m_cacheStore = new SegCacheStore(*this, m_numSilf, cacheSize); |
|
50 return bool(m_cacheStore); |
|
51 } |
|
52 |
|
53 |
|
54 bool CachedFace::runGraphite(Segment *seg, const Silf *pSilf) const |
|
55 { |
|
56 assert(pSilf); |
|
57 pSilf->runGraphite(seg, 0, pSilf->substitutionPass()); |
|
58 |
|
59 unsigned int silfIndex = 0; |
|
60 for (; silfIndex < m_numSilf && &(m_silfs[silfIndex]) != pSilf; ++silfIndex); |
|
61 if (silfIndex == m_numSilf) return false; |
|
62 SegCache * const segCache = m_cacheStore->getOrCreate(silfIndex, seg->getFeatures(0)); |
|
63 if (!segCache) |
|
64 return false; |
|
65 |
|
66 assert(m_cacheStore); |
|
67 // find where the segment can be broken |
|
68 Slot * subSegStartSlot = seg->first(); |
|
69 Slot * subSegEndSlot = subSegStartSlot; |
|
70 uint16 cmapGlyphs[eMaxSpliceSize]; |
|
71 int subSegStart = 0; |
|
72 for (unsigned int i = 0; i < seg->charInfoCount(); ++i) |
|
73 { |
|
74 const unsigned int length = i - subSegStart + 1; |
|
75 if (length < eMaxSpliceSize) |
|
76 cmapGlyphs[length-1] = subSegEndSlot->gid(); |
|
77 else return false; |
|
78 const bool spaceOnly = m_cacheStore->isSpaceGlyph(subSegEndSlot->gid()); |
|
79 // at this stage the character to slot mapping is still 1 to 1 |
|
80 const int breakWeight = seg->charinfo(i)->breakWeight(), |
|
81 nextBreakWeight = (i + 1 < seg->charInfoCount())? |
|
82 seg->charinfo(i+1)->breakWeight() : 0; |
|
83 const uint8 f = seg->charinfo(i)->flags(); |
|
84 if (((spaceOnly |
|
85 || (breakWeight > 0 && breakWeight <= gr_breakWord) |
|
86 || i + 1 == seg->charInfoCount() |
|
87 || ((nextBreakWeight < 0 && nextBreakWeight >= gr_breakBeforeWord) |
|
88 || (subSegEndSlot->next() && m_cacheStore->isSpaceGlyph(subSegEndSlot->next()->gid())))) |
|
89 && f != 1) |
|
90 || f == 2) |
|
91 { |
|
92 // record the next slot before any splicing |
|
93 Slot * nextSlot = subSegEndSlot->next(); |
|
94 // spaces should be left untouched by graphite rules in any sane font |
|
95 if (!spaceOnly) |
|
96 { |
|
97 // found a break position, check for a cache of the sub sequence |
|
98 const SegCacheEntry * entry = segCache->find(cmapGlyphs, length); |
|
99 // TODO disable cache for words at start/end of line with contextuals |
|
100 if (!entry) |
|
101 { |
|
102 SegmentScopeState scopeState = seg->setScope(subSegStartSlot, subSegEndSlot, length); |
|
103 pSilf->runGraphite(seg, pSilf->substitutionPass(), pSilf->numPasses()); |
|
104 if (length < eMaxSpliceSize) |
|
105 { |
|
106 seg->associateChars(subSegStart, length); |
|
107 segCache->cache(m_cacheStore, cmapGlyphs, length, seg, subSegStart); |
|
108 } |
|
109 seg->removeScope(scopeState); |
|
110 } |
|
111 else |
|
112 seg->splice(subSegStart, length, subSegStartSlot, subSegEndSlot, |
|
113 entry->first(), entry->glyphLength()); |
|
114 } |
|
115 subSegStartSlot = subSegEndSlot = nextSlot; |
|
116 subSegStart = i + 1; |
|
117 } |
|
118 else |
|
119 { |
|
120 subSegEndSlot = subSegEndSlot->next(); |
|
121 } |
|
122 } |
|
123 return true; |
|
124 } |
|
125 |
|
126 #endif |
|
127 |