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: #include michael@0: #include "graphite2/Segment.h" michael@0: #include "inc/debug.h" michael@0: #include "inc/Endian.h" michael@0: #include "inc/Silf.h" michael@0: #include "inc/Segment.h" michael@0: #include "inc/Rule.h" michael@0: #include "inc/Error.h" michael@0: michael@0: michael@0: using namespace graphite2; michael@0: michael@0: namespace { static const uint32 ERROROFFSET = 0xFFFFFFFF; } michael@0: michael@0: Silf::Silf() throw() michael@0: : m_passes(0), michael@0: m_pseudos(0), michael@0: m_classOffsets(0), michael@0: m_classData(0), michael@0: m_justs(0), michael@0: m_numPasses(0), michael@0: m_numJusts(0), michael@0: m_sPass(0), michael@0: m_pPass(0), michael@0: m_jPass(0), michael@0: m_bPass(0), michael@0: m_flags(0), michael@0: m_aPseudo(0), michael@0: m_aBreak(0), michael@0: m_aUser(0), michael@0: m_aBidi(0), michael@0: m_aMirror(0), michael@0: m_aPassBits(0), michael@0: m_iMaxComp(0), michael@0: m_aLig(0), michael@0: m_numPseudo(0), michael@0: m_nClass(0), michael@0: m_nLinear(0), michael@0: m_gEndLine(0) michael@0: { michael@0: memset(&m_silfinfo, 0, sizeof m_silfinfo); michael@0: } michael@0: michael@0: Silf::~Silf() throw() michael@0: { michael@0: releaseBuffers(); michael@0: } michael@0: michael@0: void Silf::releaseBuffers() throw() michael@0: { michael@0: delete [] m_passes; michael@0: delete [] m_pseudos; michael@0: free(m_classOffsets); michael@0: free(m_classData); michael@0: free(m_justs); michael@0: m_passes= 0; michael@0: m_pseudos = 0; michael@0: m_classOffsets = 0; michael@0: m_classData = 0; michael@0: m_justs = 0; michael@0: } michael@0: michael@0: michael@0: bool Silf::readGraphite(const byte * const silf_start, size_t lSilf, Face& face, uint32 version) michael@0: { michael@0: const byte * p = silf_start, michael@0: * const silf_end = p + lSilf; michael@0: Error e; michael@0: michael@0: if (version >= 0x00030000) michael@0: { michael@0: if (e.test(lSilf < 28, E_BADSIZE)) { releaseBuffers(); return face.error(e); } michael@0: be::skip(p); // ruleVersion michael@0: be::skip(p,2); // passOffset & pseudosOffset michael@0: } michael@0: else if (e.test(lSilf < 20, E_BADSIZE)) { releaseBuffers(); return face.error(e); } michael@0: const uint16 maxGlyph = be::read(p); michael@0: m_silfinfo.extra_ascent = be::read(p); michael@0: m_silfinfo.extra_descent = be::read(p); michael@0: m_numPasses = be::read(p); michael@0: m_sPass = be::read(p); michael@0: m_pPass = be::read(p); michael@0: m_jPass = be::read(p); michael@0: m_bPass = be::read(p); michael@0: m_flags = be::read(p); michael@0: be::skip(p,2); // max{Pre,Post}Context. michael@0: m_aPseudo = be::read(p); michael@0: m_aBreak = be::read(p); michael@0: m_aBidi = be::read(p); michael@0: m_aMirror = be::read(p); michael@0: m_aPassBits = be::read(p); michael@0: michael@0: // Read Justification levels. michael@0: m_numJusts = be::read(p); michael@0: if (e.test(maxGlyph >= face.glyphs().numGlyphs(), E_BADMAXGLYPH) michael@0: || e.test(p + m_numJusts * 8 >= silf_end, E_BADNUMJUSTS)) michael@0: { michael@0: releaseBuffers(); return face.error(e); michael@0: } michael@0: michael@0: if (m_numJusts) michael@0: { michael@0: m_justs = gralloc(m_numJusts); michael@0: if (e.test(!m_justs, E_OUTOFMEM)) return face.error(e); michael@0: michael@0: for (uint8 i = 0; i < m_numJusts; i++) michael@0: { michael@0: ::new(m_justs + i) Justinfo(p[0], p[1], p[2], p[3]); michael@0: be::skip(p,8); michael@0: } michael@0: } michael@0: michael@0: if (e.test(p + sizeof(uint16) + sizeof(uint8)*8 >= silf_end, E_BADENDJUSTS)) { releaseBuffers(); return face.error(e); } michael@0: m_aLig = be::read(p); michael@0: m_aUser = be::read(p); michael@0: m_iMaxComp = be::read(p); michael@0: be::skip(p,5); // direction and 4 reserved bytes michael@0: be::skip(p, be::read(p)); // don't need critical features yet michael@0: be::skip(p); // reserved michael@0: if (e.test(p >= silf_end, E_BADCRITFEATURES)) { releaseBuffers(); return face.error(e); } michael@0: be::skip(p, be::read(p)); // don't use scriptTag array. michael@0: if (e.test(p + sizeof(uint16) + sizeof(uint32) >= silf_end, E_BADSCRIPTTAGS)) { releaseBuffers(); return face.error(e); } michael@0: m_gEndLine = be::read(p); // lbGID michael@0: const byte * o_passes = p, michael@0: * const passes_start = silf_start + be::read(p); michael@0: michael@0: const size_t num_attrs = face.glyphs().numAttrs(); michael@0: if (e.test(m_aPseudo >= num_attrs, E_BADAPSEUDO) michael@0: || e.test(m_aBreak >= num_attrs, E_BADABREAK) michael@0: || e.test(m_aBidi >= num_attrs, E_BADABIDI) michael@0: || e.test(m_aMirror>= num_attrs, E_BADAMIRROR) michael@0: || e.test(m_numPasses > 128, E_BADNUMPASSES) || e.test(passes_start >= silf_end, E_BADPASSESSTART) michael@0: || e.test(m_pPass < m_sPass, E_BADPASSBOUND) || e.test(m_pPass > m_numPasses, E_BADPPASS) || e.test(m_sPass > m_numPasses, E_BADSPASS) michael@0: || e.test(m_jPass < m_pPass, E_BADJPASSBOUND) || e.test(m_jPass > m_numPasses, E_BADJPASS) michael@0: || e.test((m_bPass != 0xFF && (m_bPass < m_jPass || m_bPass > m_numPasses)), E_BADBPASS) michael@0: || e.test(m_aLig > 127, E_BADALIG)) michael@0: { michael@0: releaseBuffers(); michael@0: return face.error(e); michael@0: } michael@0: be::skip(p, m_numPasses); michael@0: if (e.test(p + sizeof(uint16) >= passes_start, E_BADPASSESSTART)) { releaseBuffers(); return face.error(e); } michael@0: m_numPseudo = be::read(p); michael@0: be::skip(p, 3); // searchPseudo, pseudoSelector, pseudoShift michael@0: if (e.test(p + m_numPseudo*(sizeof(uint32) + sizeof(uint16)) >= passes_start, E_BADNUMPSEUDO)) michael@0: { michael@0: releaseBuffers(); return face.error(e); michael@0: } michael@0: m_pseudos = new Pseudo[m_numPseudo]; michael@0: for (int i = 0; i < m_numPseudo; i++) michael@0: { michael@0: m_pseudos[i].uid = be::read(p); michael@0: m_pseudos[i].gid = be::read(p); michael@0: } michael@0: michael@0: const size_t clen = readClassMap(p, passes_start - p, version, e); michael@0: if (e || e.test(p + clen > passes_start, E_BADPASSESSTART)) { releaseBuffers(); return face.error(e); } michael@0: michael@0: m_passes = new Pass[m_numPasses]; michael@0: for (size_t i = 0; i < m_numPasses; ++i) michael@0: { michael@0: const byte * const pass_start = silf_start + be::read(o_passes), michael@0: * const pass_end = silf_start + be::peek(o_passes); michael@0: face.error_context((face.error_context() & 0xFF00) + EC_ASILF + (i << 16)); michael@0: if (e.test(pass_start > pass_end, E_BADPASSSTART) || e.test(pass_end > silf_end, E_BADPASSEND)) { michael@0: releaseBuffers(); return face.error(e); michael@0: } michael@0: michael@0: m_passes[i].init(this); michael@0: if (!m_passes[i].readPass(pass_start, pass_end - pass_start, pass_start - silf_start, face, e)) michael@0: { michael@0: releaseBuffers(); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: // fill in gr_faceinfo michael@0: m_silfinfo.upem = face.glyphs().unitsPerEm(); michael@0: m_silfinfo.has_bidi_pass = (m_bPass != 0xFF); michael@0: m_silfinfo.justifies = (m_numJusts != 0) || (m_jPass < m_pPass); michael@0: m_silfinfo.line_ends = (m_flags & 1); michael@0: m_silfinfo.space_contextuals = gr_faceinfo::gr_space_contextuals((m_flags >> 2) & 0x7); michael@0: return true; michael@0: } michael@0: michael@0: template inline uint32 Silf::readClassOffsets(const byte *&p, size_t data_len, Error &e) michael@0: { michael@0: const T cls_off = 2*sizeof(uint16) + sizeof(T)*(m_nClass+1); michael@0: const size_t max_off = (be::peek(p + sizeof(T)*m_nClass) - cls_off)/sizeof(uint16); michael@0: // Check that the last+1 offset is less than or equal to the class map length. michael@0: if (e.test(be::peek(p) != cls_off, E_MISALIGNEDCLASSES) michael@0: || e.test(max_off > (data_len - cls_off)/sizeof(uint16), E_HIGHCLASSOFFSET)) michael@0: return ERROROFFSET; michael@0: michael@0: // Read in all the offsets. michael@0: m_classOffsets = gralloc(m_nClass+1); michael@0: if (e.test(!m_classOffsets, E_OUTOFMEM)) return ERROROFFSET; michael@0: for (uint32 * o = m_classOffsets, * const o_end = o + m_nClass + 1; o != o_end; ++o) michael@0: { michael@0: *o = (be::read(p) - cls_off)/sizeof(uint16); michael@0: if (e.test(*o > max_off, E_HIGHCLASSOFFSET)) michael@0: return ERROROFFSET; michael@0: } michael@0: return max_off; michael@0: } michael@0: michael@0: size_t Silf::readClassMap(const byte *p, size_t data_len, uint32 version, Error &e) michael@0: { michael@0: if (e.test(data_len < sizeof(uint16)*2, E_BADCLASSSIZE)) return ERROROFFSET; michael@0: michael@0: m_nClass = be::read(p); michael@0: m_nLinear = be::read(p); michael@0: michael@0: // Check that numLinear < numClass, michael@0: // that there is at least enough data for numClasses offsets. michael@0: if (e.test(m_nLinear > m_nClass, E_TOOMANYLINEAR) michael@0: || e.test((m_nClass + 1) * (version >= 0x00040000 ? sizeof(uint32) : sizeof(uint16)) > (data_len - 4), E_CLASSESTOOBIG)) michael@0: return ERROROFFSET; michael@0: michael@0: uint32 max_off; michael@0: if (version >= 0x00040000) michael@0: max_off = readClassOffsets(p, data_len, e); michael@0: else michael@0: max_off = readClassOffsets(p, data_len, e); michael@0: michael@0: if (max_off == ERROROFFSET) return ERROROFFSET; michael@0: michael@0: // Check the linear offsets are sane, these must be monotonically increasing. michael@0: for (const uint32 *o = m_classOffsets, * const o_end = o + m_nLinear; o != o_end; ++o) michael@0: if (e.test(o[0] > o[1], E_BADCLASSOFFSET)) michael@0: return ERROROFFSET; michael@0: michael@0: // Fortunately the class data is all uint16s so we can decode these now michael@0: m_classData = gralloc(max_off); michael@0: if (e.test(!m_classData, E_OUTOFMEM)) return ERROROFFSET; michael@0: for (uint16 *d = m_classData, * const d_end = d + max_off; d != d_end; ++d) michael@0: *d = be::read(p); michael@0: michael@0: // Check the lookup class invariants for each non-linear class michael@0: for (const uint32 *o = m_classOffsets + m_nLinear, * const o_end = m_classOffsets + m_nClass; o != o_end; ++o) michael@0: { michael@0: const uint16 * lookup = m_classData + *o; michael@0: if (e.test(*o > max_off - 4, E_HIGHCLASSOFFSET) // LookupClass doesn't stretch over max_off michael@0: || e.test(lookup[0] == 0 // A LookupClass with no looks is a suspicious thing ... michael@0: || lookup[0] > (max_off - *o - 4)/2 // numIDs lookup pairs fits within (start of LookupClass' lookups array, max_off] michael@0: || lookup[3] != lookup[0] - lookup[1], E_BADCLASSLOOKUPINFO)) // rangeShift: numIDs - searchRange michael@0: return ERROROFFSET; michael@0: } michael@0: michael@0: return max_off; michael@0: } michael@0: michael@0: uint16 Silf::findPseudo(uint32 uid) const michael@0: { michael@0: for (int i = 0; i < m_numPseudo; i++) michael@0: if (m_pseudos[i].uid == uid) return m_pseudos[i].gid; michael@0: return 0; michael@0: } michael@0: michael@0: uint16 Silf::findClassIndex(uint16 cid, uint16 gid) const michael@0: { michael@0: if (cid > m_nClass) return -1; michael@0: michael@0: const uint16 * cls = m_classData + m_classOffsets[cid]; michael@0: if (cid < m_nLinear) // output class being used for input, shouldn't happen michael@0: { michael@0: for (unsigned int i = 0, n = m_classOffsets[cid + 1]; i < n; ++i, ++cls) michael@0: if (*cls == gid) return i; michael@0: return -1; michael@0: } michael@0: else michael@0: { michael@0: const uint16 * min = cls + 4, // lookups array michael@0: * max = min + cls[0]*2; // lookups aray is numIDs (cls[0]) uint16 pairs long michael@0: do michael@0: { michael@0: const uint16 * p = min + (-2 & ((max-min)/2)); michael@0: if (p[0] > gid) max = p; michael@0: else min = p; michael@0: } michael@0: while (max - min > 2); michael@0: return min[0] == gid ? min[1] : -1; michael@0: } michael@0: } michael@0: michael@0: uint16 Silf::getClassGlyph(uint16 cid, unsigned int index) const michael@0: { michael@0: if (cid > m_nClass) return 0; michael@0: michael@0: uint32 loc = m_classOffsets[cid]; michael@0: if (cid < m_nLinear) michael@0: { michael@0: if (index < m_classOffsets[cid + 1] - loc) michael@0: return m_classData[index + loc]; michael@0: } michael@0: else // input class being used for output. Shouldn't happen michael@0: { michael@0: for (unsigned int i = loc + 4; i < m_classOffsets[cid + 1]; i += 2) michael@0: if (m_classData[i + 1] == index) return m_classData[i]; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: bool Silf::runGraphite(Segment *seg, uint8 firstPass, uint8 lastPass, int dobidi) const michael@0: { michael@0: assert(seg != 0); michael@0: SlotMap map(*seg); michael@0: FiniteStateMachine fsm(map, seg->getFace()->logger()); michael@0: vm::Machine m(map); michael@0: unsigned int initSize = seg->slotCount(); michael@0: uint8 lbidi = m_bPass; michael@0: #if !defined GRAPHITE2_NTRACING michael@0: json * const dbgout = seg->getFace()->logger(); michael@0: #endif michael@0: michael@0: if (lastPass == 0) michael@0: { michael@0: if (firstPass == lastPass && lbidi == 0xFF) michael@0: return true; michael@0: lastPass = m_numPasses; michael@0: } michael@0: if (firstPass <= lbidi && lastPass >= lbidi && dobidi) michael@0: lastPass++; michael@0: else michael@0: lbidi = 0xFF; michael@0: michael@0: for (size_t i = firstPass; i < lastPass; ++i) michael@0: { michael@0: // bidi and mirroring michael@0: if (i == lbidi) michael@0: { michael@0: #if !defined GRAPHITE2_NTRACING michael@0: if (dbgout) michael@0: { michael@0: *dbgout << json::item << json::object michael@0: << "id" << -1 michael@0: << "slots" << json::array; michael@0: seg->positionSlots(0); michael@0: for(Slot * s = seg->first(); s; s = s->next()) michael@0: *dbgout << dslot(seg, s); michael@0: *dbgout << json::close michael@0: << "rules" << json::array << json::close michael@0: << json::close; michael@0: } michael@0: #endif michael@0: michael@0: if (!(seg->dir() & 2)) michael@0: seg->bidiPass(m_aBidi, seg->dir() & 1, m_aMirror); michael@0: else if (m_aMirror) michael@0: { michael@0: Slot * s; michael@0: for (s = seg->first(); s; s = s->next()) michael@0: { michael@0: unsigned short g = seg->glyphAttr(s->gid(), m_aMirror); michael@0: if (g && (!(seg->dir() & 4) || !seg->glyphAttr(s->gid(), m_aMirror + 1))) michael@0: s->setGlyph(seg, g); michael@0: } michael@0: } michael@0: --i; michael@0: --lastPass; michael@0: lbidi = 0xFF; michael@0: continue; michael@0: } michael@0: michael@0: #if !defined GRAPHITE2_NTRACING michael@0: if (dbgout) michael@0: { michael@0: *dbgout << json::item << json::object michael@0: << "id" << i+1 michael@0: << "slots" << json::array; michael@0: seg->positionSlots(0); michael@0: for(Slot * s = seg->first(); s; s = s->next()) michael@0: *dbgout << dslot(seg, s); michael@0: *dbgout << json::close; michael@0: } michael@0: #endif michael@0: michael@0: // test whether to reorder, prepare for positioning michael@0: if (i >= 32 || (seg->passBits() & (1 << i)) == 0) michael@0: m_passes[i].runGraphite(m, fsm); michael@0: // only subsitution passes can change segment length, cached subsegments are short for their text michael@0: if (m.status() != vm::Machine::finished michael@0: || (i < m_pPass && (seg->slotCount() > initSize * MAX_SEG_GROWTH_FACTOR michael@0: || (seg->slotCount() && seg->slotCount() * MAX_SEG_GROWTH_FACTOR < initSize)))) michael@0: return false; michael@0: } michael@0: return true; michael@0: }