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 : #include <cstring>
28 :
29 : #include "inc/Main.h"
30 : #include "inc/Endian.h"
31 : #include "inc/FeatureMap.h"
32 : #include "inc/FeatureVal.h"
33 : #include "graphite2/Font.h"
34 : #include "inc/TtfUtil.h"
35 : #include <cstdlib>
36 : #include "inc/Face.h"
37 :
38 :
39 : using namespace graphite2;
40 :
41 : namespace
42 : {
43 0 : static int cmpNameAndFeatures(const void *ap, const void *bp)
44 : {
45 0 : const NameAndFeatureRef & a = *static_cast<const NameAndFeatureRef *>(ap),
46 0 : & b = *static_cast<const NameAndFeatureRef *>(bp);
47 0 : return (a < b ? -1 : (b < a ? 1 : 0));
48 : }
49 : }
50 :
51 :
52 0 : bool FeatureMap::readFeats(const Face & face)
53 : {
54 : size_t lFeat;
55 0 : const byte *pFeat = face.getTable(TtfUtil::Tag::Feat, &lFeat);
56 0 : const byte *pOrig = pFeat;
57 0 : uint16 *defVals=0;
58 : uint32 version;
59 0 : if (!pFeat) return true;
60 0 : if (lFeat < 12) return false;
61 :
62 0 : version = be::read<uint32>(pFeat);
63 0 : if (version < 0x00010000) return false;
64 0 : m_numFeats = be::read<uint16>(pFeat);
65 0 : be::skip<uint16>(pFeat);
66 0 : be::skip<uint32>(pFeat);
67 0 : if (m_numFeats * 16U + 12 > lFeat) { m_numFeats = 0; return false; } //defensive
68 0 : if (!m_numFeats) return true;
69 0 : m_feats = new FeatureRef[m_numFeats];
70 0 : defVals = gralloc<uint16>(m_numFeats);
71 0 : byte currIndex = 0;
72 0 : byte currBits = 0; //to cause overflow on first Feature
73 :
74 0 : for (int i = 0, ie = m_numFeats; i != ie; i++)
75 : {
76 : uint32 name;
77 0 : if (version < 0x00020000)
78 0 : name = be::read<uint16>(pFeat);
79 : else
80 0 : name = be::read<uint32>(pFeat);
81 0 : uint16 numSet = be::read<uint16>(pFeat);
82 :
83 : uint32 offset;
84 0 : if (version < 0x00020000)
85 0 : offset = be::read<uint32>(pFeat);
86 : else
87 : {
88 0 : be::skip<uint16>(pFeat);
89 0 : offset = be::read<uint32>(pFeat);
90 : }
91 0 : if (offset > lFeat)
92 : {
93 0 : free(defVals);
94 0 : return false;
95 : }
96 0 : uint16 flags = be::read<uint16>(pFeat);
97 0 : uint16 uiName = be::read<uint16>(pFeat);
98 0 : const byte *pSet = pOrig + offset;
99 0 : uint16 maxVal = 0;
100 :
101 0 : if (numSet == 0)
102 : {
103 0 : --m_numFeats;
104 0 : continue;
105 : }
106 :
107 0 : if (offset + numSet * 4 > lFeat) return false;
108 0 : FeatureSetting *uiSet = gralloc<FeatureSetting>(numSet);
109 0 : for (int j = 0; j < numSet; j++)
110 : {
111 0 : int16 val = be::read<int16>(pSet);
112 0 : if (val > maxVal) maxVal = val;
113 0 : if (j == 0) defVals[i] = val;
114 0 : uint16 label = be::read<uint16>(pSet);
115 0 : new (uiSet + j) FeatureSetting(label, val);
116 : }
117 0 : uint32 mask = 1;
118 0 : byte bits = 0;
119 0 : for (bits = 0; bits < 32; bits++, mask <<= 1)
120 : {
121 0 : if (mask > maxVal)
122 : {
123 0 : if (bits + currBits > 32)
124 : {
125 0 : currIndex++;
126 0 : currBits = 0;
127 0 : mask = 2;
128 : }
129 0 : ::new (m_feats + i) FeatureRef (currBits, currIndex,
130 : (mask - 1) << currBits, flags,
131 0 : name, uiName, numSet, uiSet, &face);
132 0 : currBits += bits;
133 0 : break;
134 : }
135 : }
136 : }
137 0 : m_defaultFeatures = new Features(currIndex + 1, *this);
138 0 : m_pNamedFeats = new NameAndFeatureRef[m_numFeats];
139 0 : for (int i = 0; i < m_numFeats; i++)
140 : {
141 0 : m_feats[i].applyValToFeature(defVals[i], *m_defaultFeatures);
142 0 : m_pNamedFeats[i] = m_feats+i;
143 : }
144 :
145 0 : free(defVals);
146 :
147 0 : qsort(m_pNamedFeats, m_numFeats, sizeof(NameAndFeatureRef), &cmpNameAndFeatures);
148 :
149 0 : return true;
150 : }
151 :
152 0 : bool SillMap::readFace(const Face & face)
153 : {
154 0 : if (!m_FeatureMap.readFeats(face)) return false;
155 0 : if (!readSill(face)) return false;
156 0 : return true;
157 : }
158 :
159 :
160 0 : bool SillMap::readSill(const Face & face)
161 : {
162 : size_t lSill;
163 0 : const byte *pSill = face.getTable(TtfUtil::Tag::Sill, &lSill);
164 0 : const byte *pBase = pSill;
165 :
166 0 : if (!pSill) return true;
167 0 : if (lSill < 12) return false;
168 0 : if (be::read<uint32>(pSill) != 0x00010000UL) return false;
169 0 : m_numLanguages = be::read<uint16>(pSill);
170 0 : m_langFeats = new LangFeaturePair[m_numLanguages];
171 0 : if (!m_langFeats || !m_FeatureMap.m_numFeats) { m_numLanguages = 0; return true; } //defensive
172 :
173 0 : pSill += 6; // skip the fast search
174 0 : if (lSill < m_numLanguages * 8U + 12) return false;
175 :
176 0 : for (int i = 0; i < m_numLanguages; i++)
177 : {
178 0 : uint32 langid = be::read<uint32>(pSill);
179 0 : uint16 numSettings = be::read<uint16>(pSill);
180 0 : uint16 offset = be::read<uint16>(pSill);
181 0 : if (offset + 8U * numSettings > lSill && numSettings > 0) return false;
182 0 : Features* feats = new Features(*m_FeatureMap.m_defaultFeatures);
183 0 : const byte *pLSet = pBase + offset;
184 :
185 0 : for (int j = 0; j < numSettings; j++)
186 : {
187 0 : uint32 name = be::read<uint32>(pLSet);
188 0 : uint16 val = be::read<uint16>(pLSet);
189 0 : pLSet += 2;
190 0 : const FeatureRef* pRef = m_FeatureMap.findFeatureRef(name);
191 0 : if (pRef)
192 0 : pRef->applyValToFeature(val, *feats);
193 : }
194 : //std::pair<uint32, Features *>kvalue = std::pair<uint32, Features *>(langid, feats);
195 : //m_langMap.insert(kvalue);
196 0 : m_langFeats[i].m_lang = langid;
197 0 : m_langFeats[i].m_pFeatures = feats;
198 : }
199 0 : return true;
200 : }
201 :
202 :
203 0 : Features* SillMap::cloneFeatures(uint32 langname/*0 means default*/) const
204 : {
205 0 : if (langname)
206 : {
207 : // the number of languages in a font is usually small e.g. 8 in Doulos
208 : // so this loop is not very expensive
209 0 : for (uint16 i = 0; i < m_numLanguages; i++)
210 : {
211 0 : if (m_langFeats[i].m_lang == langname)
212 0 : return new Features(*m_langFeats[i].m_pFeatures);
213 : }
214 : }
215 0 : return new Features (*m_FeatureMap.m_defaultFeatures);
216 : }
217 :
218 :
219 :
220 0 : const FeatureRef *FeatureMap::findFeatureRef(uint32 name) const
221 : {
222 : NameAndFeatureRef *it;
223 :
224 0 : for (it = m_pNamedFeats; it < m_pNamedFeats + m_numFeats; ++it)
225 0 : if (it->m_name == name)
226 0 : return it->m_pFRef;
227 0 : return NULL;
228 : }
229 :
230 0 : bool FeatureRef::applyValToFeature(uint16 val, Features & pDest) const
231 : {
232 0 : if (val>m_max || !m_pFace)
233 0 : return false;
234 0 : if (pDest.m_pMap==NULL)
235 0 : pDest.m_pMap = &m_pFace->theSill().theFeatureMap();
236 : else
237 0 : if (pDest.m_pMap!=&m_pFace->theSill().theFeatureMap())
238 0 : return false; //incompatible
239 0 : pDest.reserve(m_index);
240 0 : pDest[m_index] &= ~m_mask;
241 0 : pDest[m_index] |= (uint32(val) << m_bits);
242 0 : return true;
243 : }
244 :
245 0 : uint16 FeatureRef::getFeatureVal(const Features& feats) const
246 : {
247 0 : if (m_index < feats.size() && &m_pFace->theSill().theFeatureMap()==feats.m_pMap)
248 0 : return (feats[m_index] & m_mask) >> m_bits;
249 : else
250 0 : return 0;
251 : }
252 :
253 :
|