1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/graphite2/src/Silf.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,415 @@ 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 +#include <cstdlib> 1.31 +#include "graphite2/Segment.h" 1.32 +#include "inc/debug.h" 1.33 +#include "inc/Endian.h" 1.34 +#include "inc/Silf.h" 1.35 +#include "inc/Segment.h" 1.36 +#include "inc/Rule.h" 1.37 +#include "inc/Error.h" 1.38 + 1.39 + 1.40 +using namespace graphite2; 1.41 + 1.42 +namespace { static const uint32 ERROROFFSET = 0xFFFFFFFF; } 1.43 + 1.44 +Silf::Silf() throw() 1.45 +: m_passes(0), 1.46 + m_pseudos(0), 1.47 + m_classOffsets(0), 1.48 + m_classData(0), 1.49 + m_justs(0), 1.50 + m_numPasses(0), 1.51 + m_numJusts(0), 1.52 + m_sPass(0), 1.53 + m_pPass(0), 1.54 + m_jPass(0), 1.55 + m_bPass(0), 1.56 + m_flags(0), 1.57 + m_aPseudo(0), 1.58 + m_aBreak(0), 1.59 + m_aUser(0), 1.60 + m_aBidi(0), 1.61 + m_aMirror(0), 1.62 + m_aPassBits(0), 1.63 + m_iMaxComp(0), 1.64 + m_aLig(0), 1.65 + m_numPseudo(0), 1.66 + m_nClass(0), 1.67 + m_nLinear(0), 1.68 + m_gEndLine(0) 1.69 +{ 1.70 + memset(&m_silfinfo, 0, sizeof m_silfinfo); 1.71 +} 1.72 + 1.73 +Silf::~Silf() throw() 1.74 +{ 1.75 + releaseBuffers(); 1.76 +} 1.77 + 1.78 +void Silf::releaseBuffers() throw() 1.79 +{ 1.80 + delete [] m_passes; 1.81 + delete [] m_pseudos; 1.82 + free(m_classOffsets); 1.83 + free(m_classData); 1.84 + free(m_justs); 1.85 + m_passes= 0; 1.86 + m_pseudos = 0; 1.87 + m_classOffsets = 0; 1.88 + m_classData = 0; 1.89 + m_justs = 0; 1.90 +} 1.91 + 1.92 + 1.93 +bool Silf::readGraphite(const byte * const silf_start, size_t lSilf, Face& face, uint32 version) 1.94 +{ 1.95 + const byte * p = silf_start, 1.96 + * const silf_end = p + lSilf; 1.97 + Error e; 1.98 + 1.99 + if (version >= 0x00030000) 1.100 + { 1.101 + if (e.test(lSilf < 28, E_BADSIZE)) { releaseBuffers(); return face.error(e); } 1.102 + be::skip<int32>(p); // ruleVersion 1.103 + be::skip<uint16>(p,2); // passOffset & pseudosOffset 1.104 + } 1.105 + else if (e.test(lSilf < 20, E_BADSIZE)) { releaseBuffers(); return face.error(e); } 1.106 + const uint16 maxGlyph = be::read<uint16>(p); 1.107 + m_silfinfo.extra_ascent = be::read<uint16>(p); 1.108 + m_silfinfo.extra_descent = be::read<uint16>(p); 1.109 + m_numPasses = be::read<uint8>(p); 1.110 + m_sPass = be::read<uint8>(p); 1.111 + m_pPass = be::read<uint8>(p); 1.112 + m_jPass = be::read<uint8>(p); 1.113 + m_bPass = be::read<uint8>(p); 1.114 + m_flags = be::read<uint8>(p); 1.115 + be::skip<uint8>(p,2); // max{Pre,Post}Context. 1.116 + m_aPseudo = be::read<uint8>(p); 1.117 + m_aBreak = be::read<uint8>(p); 1.118 + m_aBidi = be::read<uint8>(p); 1.119 + m_aMirror = be::read<uint8>(p); 1.120 + m_aPassBits = be::read<uint8>(p); 1.121 + 1.122 + // Read Justification levels. 1.123 + m_numJusts = be::read<uint8>(p); 1.124 + if (e.test(maxGlyph >= face.glyphs().numGlyphs(), E_BADMAXGLYPH) 1.125 + || e.test(p + m_numJusts * 8 >= silf_end, E_BADNUMJUSTS)) 1.126 + { 1.127 + releaseBuffers(); return face.error(e); 1.128 + } 1.129 + 1.130 + if (m_numJusts) 1.131 + { 1.132 + m_justs = gralloc<Justinfo>(m_numJusts); 1.133 + if (e.test(!m_justs, E_OUTOFMEM)) return face.error(e); 1.134 + 1.135 + for (uint8 i = 0; i < m_numJusts; i++) 1.136 + { 1.137 + ::new(m_justs + i) Justinfo(p[0], p[1], p[2], p[3]); 1.138 + be::skip<byte>(p,8); 1.139 + } 1.140 + } 1.141 + 1.142 + if (e.test(p + sizeof(uint16) + sizeof(uint8)*8 >= silf_end, E_BADENDJUSTS)) { releaseBuffers(); return face.error(e); } 1.143 + m_aLig = be::read<uint16>(p); 1.144 + m_aUser = be::read<uint8>(p); 1.145 + m_iMaxComp = be::read<uint8>(p); 1.146 + be::skip<byte>(p,5); // direction and 4 reserved bytes 1.147 + be::skip<uint16>(p, be::read<uint8>(p)); // don't need critical features yet 1.148 + be::skip<byte>(p); // reserved 1.149 + if (e.test(p >= silf_end, E_BADCRITFEATURES)) { releaseBuffers(); return face.error(e); } 1.150 + be::skip<uint32>(p, be::read<uint8>(p)); // don't use scriptTag array. 1.151 + if (e.test(p + sizeof(uint16) + sizeof(uint32) >= silf_end, E_BADSCRIPTTAGS)) { releaseBuffers(); return face.error(e); } 1.152 + m_gEndLine = be::read<uint16>(p); // lbGID 1.153 + const byte * o_passes = p, 1.154 + * const passes_start = silf_start + be::read<uint32>(p); 1.155 + 1.156 + const size_t num_attrs = face.glyphs().numAttrs(); 1.157 + if (e.test(m_aPseudo >= num_attrs, E_BADAPSEUDO) 1.158 + || e.test(m_aBreak >= num_attrs, E_BADABREAK) 1.159 + || e.test(m_aBidi >= num_attrs, E_BADABIDI) 1.160 + || e.test(m_aMirror>= num_attrs, E_BADAMIRROR) 1.161 + || e.test(m_numPasses > 128, E_BADNUMPASSES) || e.test(passes_start >= silf_end, E_BADPASSESSTART) 1.162 + || e.test(m_pPass < m_sPass, E_BADPASSBOUND) || e.test(m_pPass > m_numPasses, E_BADPPASS) || e.test(m_sPass > m_numPasses, E_BADSPASS) 1.163 + || e.test(m_jPass < m_pPass, E_BADJPASSBOUND) || e.test(m_jPass > m_numPasses, E_BADJPASS) 1.164 + || e.test((m_bPass != 0xFF && (m_bPass < m_jPass || m_bPass > m_numPasses)), E_BADBPASS) 1.165 + || e.test(m_aLig > 127, E_BADALIG)) 1.166 + { 1.167 + releaseBuffers(); 1.168 + return face.error(e); 1.169 + } 1.170 + be::skip<uint32>(p, m_numPasses); 1.171 + if (e.test(p + sizeof(uint16) >= passes_start, E_BADPASSESSTART)) { releaseBuffers(); return face.error(e); } 1.172 + m_numPseudo = be::read<uint16>(p); 1.173 + be::skip<uint16>(p, 3); // searchPseudo, pseudoSelector, pseudoShift 1.174 + if (e.test(p + m_numPseudo*(sizeof(uint32) + sizeof(uint16)) >= passes_start, E_BADNUMPSEUDO)) 1.175 + { 1.176 + releaseBuffers(); return face.error(e); 1.177 + } 1.178 + m_pseudos = new Pseudo[m_numPseudo]; 1.179 + for (int i = 0; i < m_numPseudo; i++) 1.180 + { 1.181 + m_pseudos[i].uid = be::read<uint32>(p); 1.182 + m_pseudos[i].gid = be::read<uint16>(p); 1.183 + } 1.184 + 1.185 + const size_t clen = readClassMap(p, passes_start - p, version, e); 1.186 + if (e || e.test(p + clen > passes_start, E_BADPASSESSTART)) { releaseBuffers(); return face.error(e); } 1.187 + 1.188 + m_passes = new Pass[m_numPasses]; 1.189 + for (size_t i = 0; i < m_numPasses; ++i) 1.190 + { 1.191 + const byte * const pass_start = silf_start + be::read<uint32>(o_passes), 1.192 + * const pass_end = silf_start + be::peek<uint32>(o_passes); 1.193 + face.error_context((face.error_context() & 0xFF00) + EC_ASILF + (i << 16)); 1.194 + if (e.test(pass_start > pass_end, E_BADPASSSTART) || e.test(pass_end > silf_end, E_BADPASSEND)) { 1.195 + releaseBuffers(); return face.error(e); 1.196 + } 1.197 + 1.198 + m_passes[i].init(this); 1.199 + if (!m_passes[i].readPass(pass_start, pass_end - pass_start, pass_start - silf_start, face, e)) 1.200 + { 1.201 + releaseBuffers(); 1.202 + return false; 1.203 + } 1.204 + } 1.205 + 1.206 + // fill in gr_faceinfo 1.207 + m_silfinfo.upem = face.glyphs().unitsPerEm(); 1.208 + m_silfinfo.has_bidi_pass = (m_bPass != 0xFF); 1.209 + m_silfinfo.justifies = (m_numJusts != 0) || (m_jPass < m_pPass); 1.210 + m_silfinfo.line_ends = (m_flags & 1); 1.211 + m_silfinfo.space_contextuals = gr_faceinfo::gr_space_contextuals((m_flags >> 2) & 0x7); 1.212 + return true; 1.213 +} 1.214 + 1.215 +template<typename T> inline uint32 Silf::readClassOffsets(const byte *&p, size_t data_len, Error &e) 1.216 +{ 1.217 + const T cls_off = 2*sizeof(uint16) + sizeof(T)*(m_nClass+1); 1.218 + const size_t max_off = (be::peek<T>(p + sizeof(T)*m_nClass) - cls_off)/sizeof(uint16); 1.219 + // Check that the last+1 offset is less than or equal to the class map length. 1.220 + if (e.test(be::peek<T>(p) != cls_off, E_MISALIGNEDCLASSES) 1.221 + || e.test(max_off > (data_len - cls_off)/sizeof(uint16), E_HIGHCLASSOFFSET)) 1.222 + return ERROROFFSET; 1.223 + 1.224 + // Read in all the offsets. 1.225 + m_classOffsets = gralloc<uint32>(m_nClass+1); 1.226 + if (e.test(!m_classOffsets, E_OUTOFMEM)) return ERROROFFSET; 1.227 + for (uint32 * o = m_classOffsets, * const o_end = o + m_nClass + 1; o != o_end; ++o) 1.228 + { 1.229 + *o = (be::read<T>(p) - cls_off)/sizeof(uint16); 1.230 + if (e.test(*o > max_off, E_HIGHCLASSOFFSET)) 1.231 + return ERROROFFSET; 1.232 + } 1.233 + return max_off; 1.234 +} 1.235 + 1.236 +size_t Silf::readClassMap(const byte *p, size_t data_len, uint32 version, Error &e) 1.237 +{ 1.238 + if (e.test(data_len < sizeof(uint16)*2, E_BADCLASSSIZE)) return ERROROFFSET; 1.239 + 1.240 + m_nClass = be::read<uint16>(p); 1.241 + m_nLinear = be::read<uint16>(p); 1.242 + 1.243 + // Check that numLinear < numClass, 1.244 + // that there is at least enough data for numClasses offsets. 1.245 + if (e.test(m_nLinear > m_nClass, E_TOOMANYLINEAR) 1.246 + || e.test((m_nClass + 1) * (version >= 0x00040000 ? sizeof(uint32) : sizeof(uint16)) > (data_len - 4), E_CLASSESTOOBIG)) 1.247 + return ERROROFFSET; 1.248 + 1.249 + uint32 max_off; 1.250 + if (version >= 0x00040000) 1.251 + max_off = readClassOffsets<uint32>(p, data_len, e); 1.252 + else 1.253 + max_off = readClassOffsets<uint16>(p, data_len, e); 1.254 + 1.255 + if (max_off == ERROROFFSET) return ERROROFFSET; 1.256 + 1.257 + // Check the linear offsets are sane, these must be monotonically increasing. 1.258 + for (const uint32 *o = m_classOffsets, * const o_end = o + m_nLinear; o != o_end; ++o) 1.259 + if (e.test(o[0] > o[1], E_BADCLASSOFFSET)) 1.260 + return ERROROFFSET; 1.261 + 1.262 + // Fortunately the class data is all uint16s so we can decode these now 1.263 + m_classData = gralloc<uint16>(max_off); 1.264 + if (e.test(!m_classData, E_OUTOFMEM)) return ERROROFFSET; 1.265 + for (uint16 *d = m_classData, * const d_end = d + max_off; d != d_end; ++d) 1.266 + *d = be::read<uint16>(p); 1.267 + 1.268 + // Check the lookup class invariants for each non-linear class 1.269 + for (const uint32 *o = m_classOffsets + m_nLinear, * const o_end = m_classOffsets + m_nClass; o != o_end; ++o) 1.270 + { 1.271 + const uint16 * lookup = m_classData + *o; 1.272 + if (e.test(*o > max_off - 4, E_HIGHCLASSOFFSET) // LookupClass doesn't stretch over max_off 1.273 + || e.test(lookup[0] == 0 // A LookupClass with no looks is a suspicious thing ... 1.274 + || lookup[0] > (max_off - *o - 4)/2 // numIDs lookup pairs fits within (start of LookupClass' lookups array, max_off] 1.275 + || lookup[3] != lookup[0] - lookup[1], E_BADCLASSLOOKUPINFO)) // rangeShift: numIDs - searchRange 1.276 + return ERROROFFSET; 1.277 + } 1.278 + 1.279 + return max_off; 1.280 +} 1.281 + 1.282 +uint16 Silf::findPseudo(uint32 uid) const 1.283 +{ 1.284 + for (int i = 0; i < m_numPseudo; i++) 1.285 + if (m_pseudos[i].uid == uid) return m_pseudos[i].gid; 1.286 + return 0; 1.287 +} 1.288 + 1.289 +uint16 Silf::findClassIndex(uint16 cid, uint16 gid) const 1.290 +{ 1.291 + if (cid > m_nClass) return -1; 1.292 + 1.293 + const uint16 * cls = m_classData + m_classOffsets[cid]; 1.294 + if (cid < m_nLinear) // output class being used for input, shouldn't happen 1.295 + { 1.296 + for (unsigned int i = 0, n = m_classOffsets[cid + 1]; i < n; ++i, ++cls) 1.297 + if (*cls == gid) return i; 1.298 + return -1; 1.299 + } 1.300 + else 1.301 + { 1.302 + const uint16 * min = cls + 4, // lookups array 1.303 + * max = min + cls[0]*2; // lookups aray is numIDs (cls[0]) uint16 pairs long 1.304 + do 1.305 + { 1.306 + const uint16 * p = min + (-2 & ((max-min)/2)); 1.307 + if (p[0] > gid) max = p; 1.308 + else min = p; 1.309 + } 1.310 + while (max - min > 2); 1.311 + return min[0] == gid ? min[1] : -1; 1.312 + } 1.313 +} 1.314 + 1.315 +uint16 Silf::getClassGlyph(uint16 cid, unsigned int index) const 1.316 +{ 1.317 + if (cid > m_nClass) return 0; 1.318 + 1.319 + uint32 loc = m_classOffsets[cid]; 1.320 + if (cid < m_nLinear) 1.321 + { 1.322 + if (index < m_classOffsets[cid + 1] - loc) 1.323 + return m_classData[index + loc]; 1.324 + } 1.325 + else // input class being used for output. Shouldn't happen 1.326 + { 1.327 + for (unsigned int i = loc + 4; i < m_classOffsets[cid + 1]; i += 2) 1.328 + if (m_classData[i + 1] == index) return m_classData[i]; 1.329 + } 1.330 + return 0; 1.331 +} 1.332 + 1.333 + 1.334 +bool Silf::runGraphite(Segment *seg, uint8 firstPass, uint8 lastPass, int dobidi) const 1.335 +{ 1.336 + assert(seg != 0); 1.337 + SlotMap map(*seg); 1.338 + FiniteStateMachine fsm(map, seg->getFace()->logger()); 1.339 + vm::Machine m(map); 1.340 + unsigned int initSize = seg->slotCount(); 1.341 + uint8 lbidi = m_bPass; 1.342 +#if !defined GRAPHITE2_NTRACING 1.343 + json * const dbgout = seg->getFace()->logger(); 1.344 +#endif 1.345 + 1.346 + if (lastPass == 0) 1.347 + { 1.348 + if (firstPass == lastPass && lbidi == 0xFF) 1.349 + return true; 1.350 + lastPass = m_numPasses; 1.351 + } 1.352 + if (firstPass <= lbidi && lastPass >= lbidi && dobidi) 1.353 + lastPass++; 1.354 + else 1.355 + lbidi = 0xFF; 1.356 + 1.357 + for (size_t i = firstPass; i < lastPass; ++i) 1.358 + { 1.359 + // bidi and mirroring 1.360 + if (i == lbidi) 1.361 + { 1.362 +#if !defined GRAPHITE2_NTRACING 1.363 + if (dbgout) 1.364 + { 1.365 + *dbgout << json::item << json::object 1.366 + << "id" << -1 1.367 + << "slots" << json::array; 1.368 + seg->positionSlots(0); 1.369 + for(Slot * s = seg->first(); s; s = s->next()) 1.370 + *dbgout << dslot(seg, s); 1.371 + *dbgout << json::close 1.372 + << "rules" << json::array << json::close 1.373 + << json::close; 1.374 + } 1.375 +#endif 1.376 + 1.377 + if (!(seg->dir() & 2)) 1.378 + seg->bidiPass(m_aBidi, seg->dir() & 1, m_aMirror); 1.379 + else if (m_aMirror) 1.380 + { 1.381 + Slot * s; 1.382 + for (s = seg->first(); s; s = s->next()) 1.383 + { 1.384 + unsigned short g = seg->glyphAttr(s->gid(), m_aMirror); 1.385 + if (g && (!(seg->dir() & 4) || !seg->glyphAttr(s->gid(), m_aMirror + 1))) 1.386 + s->setGlyph(seg, g); 1.387 + } 1.388 + } 1.389 + --i; 1.390 + --lastPass; 1.391 + lbidi = 0xFF; 1.392 + continue; 1.393 + } 1.394 + 1.395 +#if !defined GRAPHITE2_NTRACING 1.396 + if (dbgout) 1.397 + { 1.398 + *dbgout << json::item << json::object 1.399 + << "id" << i+1 1.400 + << "slots" << json::array; 1.401 + seg->positionSlots(0); 1.402 + for(Slot * s = seg->first(); s; s = s->next()) 1.403 + *dbgout << dslot(seg, s); 1.404 + *dbgout << json::close; 1.405 + } 1.406 +#endif 1.407 + 1.408 + // test whether to reorder, prepare for positioning 1.409 + if (i >= 32 || (seg->passBits() & (1 << i)) == 0) 1.410 + m_passes[i].runGraphite(m, fsm); 1.411 + // only subsitution passes can change segment length, cached subsegments are short for their text 1.412 + if (m.status() != vm::Machine::finished 1.413 + || (i < m_pPass && (seg->slotCount() > initSize * MAX_SEG_GROWTH_FACTOR 1.414 + || (seg->slotCount() && seg->slotCount() * MAX_SEG_GROWTH_FACTOR < initSize)))) 1.415 + return false; 1.416 + } 1.417 + return true; 1.418 +}