Face.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later
  2. // Copyright 2010, SIL International, All rights reserved.
  3. #include <cstring>
  4. #include "graphite2/Segment.h"
  5. #include "inc/CmapCache.h"
  6. #include "inc/debug.h"
  7. #include "inc/Decompressor.h"
  8. #include "inc/Endian.h"
  9. #include "inc/Face.h"
  10. #include "inc/FileFace.h"
  11. #include "inc/GlyphFace.h"
  12. #include "inc/json.h"
  13. #include "inc/Segment.h"
  14. #include "inc/NameTable.h"
  15. #include "inc/Error.h"
  16. using namespace graphite2;
  17. namespace
  18. {
  19. enum compression
  20. {
  21. NONE,
  22. LZ4
  23. };
  24. }
  25. Face::Face(const void* appFaceHandle/*non-NULL*/, const gr_face_ops & ops)
  26. : m_appFaceHandle(appFaceHandle),
  27. m_pFileFace(NULL),
  28. m_pGlyphFaceCache(NULL),
  29. m_cmap(NULL),
  30. m_pNames(NULL),
  31. m_logger(NULL),
  32. m_error(0), m_errcntxt(0),
  33. m_silfs(NULL),
  34. m_numSilf(0),
  35. m_ascent(0),
  36. m_descent(0)
  37. {
  38. memset(&m_ops, 0, sizeof m_ops);
  39. memcpy(&m_ops, &ops, min(sizeof m_ops, ops.size));
  40. }
  41. Face::~Face()
  42. {
  43. setLogger(0);
  44. delete m_pGlyphFaceCache;
  45. delete m_cmap;
  46. delete[] m_silfs;
  47. #ifndef GRAPHITE2_NFILEFACE
  48. delete m_pFileFace;
  49. #endif
  50. delete m_pNames;
  51. }
  52. float Face::default_glyph_advance(const void* font_ptr, gr_uint16 glyphid)
  53. {
  54. const Font & font = *reinterpret_cast<const Font *>(font_ptr);
  55. return font.face().glyphs().glyph(glyphid)->theAdvance().x * font.scale();
  56. }
  57. bool Face::readGlyphs(uint32 faceOptions)
  58. {
  59. Error e;
  60. #ifdef GRAPHITE2_TELEMETRY
  61. telemetry::category _glyph_cat(tele.glyph);
  62. #endif
  63. error_context(EC_READGLYPHS);
  64. m_pGlyphFaceCache = new GlyphCache(*this, faceOptions);
  65. if (e.test(!m_pGlyphFaceCache, E_OUTOFMEM)
  66. || e.test(m_pGlyphFaceCache->numGlyphs() == 0, E_NOGLYPHS)
  67. || e.test(m_pGlyphFaceCache->unitsPerEm() == 0, E_BADUPEM))
  68. {
  69. return error(e);
  70. }
  71. if (faceOptions & gr_face_cacheCmap)
  72. m_cmap = new CachedCmap(*this);
  73. else
  74. m_cmap = new DirectCmap(*this);
  75. if (e.test(!m_cmap, E_OUTOFMEM) || e.test(!*m_cmap, E_BADCMAP))
  76. return error(e);
  77. if (faceOptions & gr_face_preloadGlyphs)
  78. nameTable(); // preload the name table along with the glyphs.
  79. return true;
  80. }
  81. bool Face::readGraphite(const Table & silf)
  82. {
  83. #ifdef GRAPHITE2_TELEMETRY
  84. telemetry::category _silf_cat(tele.silf);
  85. #endif
  86. Error e;
  87. error_context(EC_READSILF);
  88. const byte * p = silf;
  89. if (e.test(!p, E_NOSILF) || e.test(silf.size() < 20, E_BADSIZE)) return error(e);
  90. const uint32 version = be::read<uint32>(p);
  91. if (e.test(version < 0x00020000, E_TOOOLD)) return error(e);
  92. if (version >= 0x00030000)
  93. be::skip<uint32>(p); // compilerVersion
  94. m_numSilf = be::read<uint16>(p);
  95. be::skip<uint16>(p); // reserved
  96. bool havePasses = false;
  97. m_silfs = new Silf[m_numSilf];
  98. if (e.test(!m_silfs, E_OUTOFMEM)) return error(e);
  99. for (int i = 0; i < m_numSilf; i++)
  100. {
  101. error_context(EC_ASILF + (i << 8));
  102. const uint32 offset = be::read<uint32>(p),
  103. next = i == m_numSilf - 1 ? uint32(silf.size()) : be::peek<uint32>(p);
  104. if (e.test(next > silf.size() || offset >= next, E_BADSIZE))
  105. return error(e);
  106. if (!m_silfs[i].readGraphite(silf + offset, next - offset, *this, version))
  107. return false;
  108. if (m_silfs[i].numPasses())
  109. havePasses = true;
  110. }
  111. return havePasses;
  112. }
  113. bool Face::readFeatures()
  114. {
  115. return m_Sill.readFace(*this);
  116. }
  117. bool Face::runGraphite(Segment *seg, const Silf *aSilf) const
  118. {
  119. #if !defined GRAPHITE2_NTRACING
  120. json * dbgout = logger();
  121. if (dbgout)
  122. {
  123. *dbgout << json::object
  124. << "id" << objectid(seg)
  125. << "passes" << json::array;
  126. }
  127. #endif
  128. // if ((seg->dir() & 1) != aSilf->dir())
  129. // seg->reverseSlots();
  130. if ((seg->dir() & 3) == 3 && aSilf->bidiPass() == 0xFF)
  131. seg->doMirror(aSilf->aMirror());
  132. bool res = aSilf->runGraphite(seg, 0, aSilf->positionPass(), true);
  133. if (res)
  134. {
  135. seg->associateChars(0, seg->charInfoCount());
  136. if (aSilf->flags() & 0x20)
  137. res &= seg->initCollisions();
  138. if (res)
  139. res &= aSilf->runGraphite(seg, aSilf->positionPass(), aSilf->numPasses(), false);
  140. }
  141. #if !defined GRAPHITE2_NTRACING
  142. if (dbgout)
  143. {
  144. seg->positionSlots(0, 0, 0, seg->currdir());
  145. *dbgout << json::item
  146. << json::close // Close up the passes array
  147. << "outputdir" << (seg->currdir() ? "rtl" : "ltr")
  148. << "output" << json::array;
  149. for(Slot * s = seg->first(); s; s = s->next())
  150. *dbgout << dslot(seg, s);
  151. *dbgout << json::close
  152. << "advance" << seg->advance()
  153. << "chars" << json::array;
  154. for(size_t i = 0, n = seg->charInfoCount(); i != n; ++i)
  155. *dbgout << json::flat << *seg->charinfo(int(i));
  156. *dbgout << json::close // Close up the chars array
  157. << json::close; // Close up the segment object
  158. }
  159. #endif
  160. return res;
  161. }
  162. void Face::setLogger(FILE * log_file GR_MAYBE_UNUSED)
  163. {
  164. #if !defined GRAPHITE2_NTRACING
  165. delete m_logger;
  166. m_logger = log_file ? new json(log_file) : 0;
  167. #endif
  168. }
  169. const Silf *Face::chooseSilf(uint32 script) const
  170. {
  171. if (m_numSilf == 0)
  172. return NULL;
  173. else if (m_numSilf == 1 || script == 0)
  174. return m_silfs;
  175. else // do more work here
  176. return m_silfs;
  177. }
  178. uint16 Face::findPseudo(uint32 uid) const
  179. {
  180. return (m_numSilf) ? m_silfs[0].findPseudo(uid) : 0;
  181. }
  182. int32 Face::getGlyphMetric(uint16 gid, uint8 metric) const
  183. {
  184. switch (metrics(metric))
  185. {
  186. case kgmetAscent : return m_ascent;
  187. case kgmetDescent : return m_descent;
  188. default:
  189. if (gid >= glyphs().numGlyphs()) return 0;
  190. return glyphs().glyph(gid)->getMetric(metric);
  191. }
  192. }
  193. void Face::takeFileFace(FileFace* pFileFace GR_MAYBE_UNUSED/*takes ownership*/)
  194. {
  195. #ifndef GRAPHITE2_NFILEFACE
  196. if (m_pFileFace==pFileFace)
  197. return;
  198. delete m_pFileFace;
  199. m_pFileFace = pFileFace;
  200. #endif
  201. }
  202. NameTable * Face::nameTable() const
  203. {
  204. if (m_pNames) return m_pNames;
  205. const Table name(*this, Tag::name);
  206. if (name)
  207. m_pNames = new NameTable(name, name.size());
  208. return m_pNames;
  209. }
  210. uint16 Face::languageForLocale(const char * locale) const
  211. {
  212. nameTable();
  213. if (m_pNames)
  214. return m_pNames->getLanguageId(locale);
  215. return 0;
  216. }
  217. Face::Table::Table(const Face & face, const Tag n, uint32 version) throw()
  218. : _f(&face), _sz(0), _compressed(false)
  219. {
  220. _p = static_cast<const byte *>((*_f->m_ops.get_table)(_f->m_appFaceHandle, n, &_sz));
  221. if (!TtfUtil::CheckTable(n, _p, _sz))
  222. {
  223. release(); // Make sure we release the table buffer even if the table failed its checks
  224. return;
  225. }
  226. if (be::peek<uint32>(_p) >= version)
  227. decompress();
  228. }
  229. void Face::Table::release()
  230. {
  231. if (_compressed)
  232. free(const_cast<byte *>(_p));
  233. else if (_p && _f->m_ops.release_table)
  234. (*_f->m_ops.release_table)(_f->m_appFaceHandle, _p);
  235. _p = 0; _sz = 0;
  236. }
  237. Face::Table & Face::Table::operator = (const Table && rhs) throw()
  238. {
  239. if (this == &rhs) return *this;
  240. release();
  241. new (this) Table(std::move(rhs));
  242. return *this;
  243. }
  244. Error Face::Table::decompress()
  245. {
  246. Error e;
  247. if (e.test(_sz < 5 * sizeof(uint32), E_BADSIZE))
  248. return e;
  249. byte * uncompressed_table = 0;
  250. size_t uncompressed_size = 0;
  251. const byte * p = _p;
  252. const uint32 version = be::read<uint32>(p); // Table version number.
  253. // The scheme is in the top 5 bits of the 1st uint32.
  254. const uint32 hdr = be::read<uint32>(p);
  255. switch(compression(hdr >> 27))
  256. {
  257. case NONE: return e;
  258. case LZ4:
  259. {
  260. uncompressed_size = hdr & 0x07ffffff;
  261. uncompressed_table = gralloc<byte>(uncompressed_size);
  262. if (!e.test(!uncompressed_table || uncompressed_size < 4, E_OUTOFMEM))
  263. {
  264. memset(uncompressed_table, 0, 4); // make sure version number is initialised
  265. // coverity[forward_null : FALSE] - uncompressed_table has been checked so can't be null
  266. // coverity[checked_return : FALSE] - we test e later
  267. e.test(lz4::decompress(p, _sz - 2*sizeof(uint32), uncompressed_table, uncompressed_size) != signed(uncompressed_size), E_SHRINKERFAILED);
  268. }
  269. break;
  270. }
  271. default:
  272. e.error(E_BADSCHEME);
  273. };
  274. // Check the uncompressed version number against the original.
  275. if (!e)
  276. // coverity[forward_null : FALSE] - uncompressed_table has already been tested so can't be null
  277. // coverity[checked_return : FALSE] - we test e later
  278. e.test(be::peek<uint32>(uncompressed_table) != version, E_SHRINKERFAILED);
  279. // Tell the provider to release the compressed form since were replacing
  280. // it anyway.
  281. release();
  282. if (e)
  283. {
  284. free(uncompressed_table);
  285. uncompressed_table = 0;
  286. uncompressed_size = 0;
  287. }
  288. _p = uncompressed_table;
  289. _sz = uncompressed_size;
  290. _compressed = true;
  291. return e;
  292. }