PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/qt/qtbase/src/3rdparty/harfbuzz-ng/src/hb-ot-layout-gsub-table.hh

https://code.google.com/
C++ Header | 1372 lines | 1118 code | 190 blank | 64 comment | 178 complexity | 81071196f58f02fc0048a8fccfdefca0 MD5 | raw file
Possible License(s): LGPL-3.0, CC-BY-SA-4.0, MIT, AGPL-3.0, BSD-3-Clause, LGPL-2.1, CC0-1.0, GPL-2.0, LGPL-2.0, GPL-3.0
  1. /*
  2. * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
  3. * Copyright © 2010,2012,2013 Google, Inc.
  4. *
  5. * This is part of HarfBuzz, a text shaping library.
  6. *
  7. * Permission is hereby granted, without written agreement and without
  8. * license or royalty fees, to use, copy, modify, and distribute this
  9. * software and its documentation for any purpose, provided that the
  10. * above copyright notice and the following two paragraphs appear in
  11. * all copies of this software.
  12. *
  13. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  14. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  15. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  16. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  17. * DAMAGE.
  18. *
  19. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  20. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  21. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  22. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  23. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  24. *
  25. * Red Hat Author(s): Behdad Esfahbod
  26. * Google Author(s): Behdad Esfahbod
  27. */
  28. #ifndef HB_OT_LAYOUT_GSUB_TABLE_HH
  29. #define HB_OT_LAYOUT_GSUB_TABLE_HH
  30. #include "hb-ot-layout-gsubgpos-private.hh"
  31. namespace OT {
  32. struct SingleSubstFormat1
  33. {
  34. inline void closure (hb_closure_context_t *c) const
  35. {
  36. TRACE_CLOSURE (this);
  37. Coverage::Iter iter;
  38. for (iter.init (this+coverage); iter.more (); iter.next ()) {
  39. hb_codepoint_t glyph_id = iter.get_glyph ();
  40. if (c->glyphs->has (glyph_id))
  41. c->glyphs->add ((glyph_id + deltaGlyphID) & 0xFFFF);
  42. }
  43. }
  44. inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  45. {
  46. TRACE_COLLECT_GLYPHS (this);
  47. Coverage::Iter iter;
  48. for (iter.init (this+coverage); iter.more (); iter.next ()) {
  49. hb_codepoint_t glyph_id = iter.get_glyph ();
  50. c->input->add (glyph_id);
  51. c->output->add ((glyph_id + deltaGlyphID) & 0xFFFF);
  52. }
  53. }
  54. inline const Coverage &get_coverage (void) const
  55. {
  56. return this+coverage;
  57. }
  58. inline bool would_apply (hb_would_apply_context_t *c) const
  59. {
  60. TRACE_WOULD_APPLY (this);
  61. return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
  62. }
  63. inline bool apply (hb_apply_context_t *c) const
  64. {
  65. TRACE_APPLY (this);
  66. hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
  67. unsigned int index = (this+coverage).get_coverage (glyph_id);
  68. if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
  69. /* According to the Adobe Annotated OpenType Suite, result is always
  70. * limited to 16bit. */
  71. glyph_id = (glyph_id + deltaGlyphID) & 0xFFFF;
  72. c->replace_glyph (glyph_id);
  73. return TRACE_RETURN (true);
  74. }
  75. inline bool serialize (hb_serialize_context_t *c,
  76. Supplier<GlyphID> &glyphs,
  77. unsigned int num_glyphs,
  78. int delta)
  79. {
  80. TRACE_SERIALIZE (this);
  81. if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
  82. if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false);
  83. deltaGlyphID.set (delta); /* TODO(serilaize) overflow? */
  84. return TRACE_RETURN (true);
  85. }
  86. inline bool sanitize (hb_sanitize_context_t *c) {
  87. TRACE_SANITIZE (this);
  88. return TRACE_RETURN (coverage.sanitize (c, this) && deltaGlyphID.sanitize (c));
  89. }
  90. protected:
  91. USHORT format; /* Format identifier--format = 1 */
  92. OffsetTo<Coverage>
  93. coverage; /* Offset to Coverage table--from
  94. * beginning of Substitution table */
  95. SHORT deltaGlyphID; /* Add to original GlyphID to get
  96. * substitute GlyphID */
  97. public:
  98. DEFINE_SIZE_STATIC (6);
  99. };
  100. struct SingleSubstFormat2
  101. {
  102. inline void closure (hb_closure_context_t *c) const
  103. {
  104. TRACE_CLOSURE (this);
  105. Coverage::Iter iter;
  106. for (iter.init (this+coverage); iter.more (); iter.next ()) {
  107. if (c->glyphs->has (iter.get_glyph ()))
  108. c->glyphs->add (substitute[iter.get_coverage ()]);
  109. }
  110. }
  111. inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  112. {
  113. TRACE_COLLECT_GLYPHS (this);
  114. Coverage::Iter iter;
  115. for (iter.init (this+coverage); iter.more (); iter.next ()) {
  116. c->input->add (iter.get_glyph ());
  117. c->output->add (substitute[iter.get_coverage ()]);
  118. }
  119. }
  120. inline const Coverage &get_coverage (void) const
  121. {
  122. return this+coverage;
  123. }
  124. inline bool would_apply (hb_would_apply_context_t *c) const
  125. {
  126. TRACE_WOULD_APPLY (this);
  127. return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
  128. }
  129. inline bool apply (hb_apply_context_t *c) const
  130. {
  131. TRACE_APPLY (this);
  132. hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
  133. unsigned int index = (this+coverage).get_coverage (glyph_id);
  134. if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
  135. if (unlikely (index >= substitute.len)) return TRACE_RETURN (false);
  136. glyph_id = substitute[index];
  137. c->replace_glyph (glyph_id);
  138. return TRACE_RETURN (true);
  139. }
  140. inline bool serialize (hb_serialize_context_t *c,
  141. Supplier<GlyphID> &glyphs,
  142. Supplier<GlyphID> &substitutes,
  143. unsigned int num_glyphs)
  144. {
  145. TRACE_SERIALIZE (this);
  146. if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
  147. if (unlikely (!substitute.serialize (c, substitutes, num_glyphs))) return TRACE_RETURN (false);
  148. if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false);
  149. return TRACE_RETURN (true);
  150. }
  151. inline bool sanitize (hb_sanitize_context_t *c) {
  152. TRACE_SANITIZE (this);
  153. return TRACE_RETURN (coverage.sanitize (c, this) && substitute.sanitize (c));
  154. }
  155. protected:
  156. USHORT format; /* Format identifier--format = 2 */
  157. OffsetTo<Coverage>
  158. coverage; /* Offset to Coverage table--from
  159. * beginning of Substitution table */
  160. ArrayOf<GlyphID>
  161. substitute; /* Array of substitute
  162. * GlyphIDs--ordered by Coverage Index */
  163. public:
  164. DEFINE_SIZE_ARRAY (6, substitute);
  165. };
  166. struct SingleSubst
  167. {
  168. inline bool serialize (hb_serialize_context_t *c,
  169. Supplier<GlyphID> &glyphs,
  170. Supplier<GlyphID> &substitutes,
  171. unsigned int num_glyphs)
  172. {
  173. TRACE_SERIALIZE (this);
  174. if (unlikely (!c->extend_min (u.format))) return TRACE_RETURN (false);
  175. unsigned int format = 2;
  176. int delta;
  177. if (num_glyphs) {
  178. format = 1;
  179. /* TODO(serialize) check for wrap-around */
  180. delta = substitutes[0] - glyphs[0];
  181. for (unsigned int i = 1; i < num_glyphs; i++)
  182. if (delta != substitutes[i] - glyphs[i]) {
  183. format = 2;
  184. break;
  185. }
  186. }
  187. u.format.set (format);
  188. switch (u.format) {
  189. case 1: return TRACE_RETURN (u.format1.serialize (c, glyphs, num_glyphs, delta));
  190. case 2: return TRACE_RETURN (u.format2.serialize (c, glyphs, substitutes, num_glyphs));
  191. default:return TRACE_RETURN (false);
  192. }
  193. }
  194. template <typename context_t>
  195. inline typename context_t::return_t dispatch (context_t *c) const
  196. {
  197. TRACE_DISPATCH (this);
  198. switch (u.format) {
  199. case 1: return TRACE_RETURN (c->dispatch (u.format1));
  200. case 2: return TRACE_RETURN (c->dispatch (u.format2));
  201. default:return TRACE_RETURN (c->default_return_value ());
  202. }
  203. }
  204. inline bool sanitize (hb_sanitize_context_t *c) {
  205. TRACE_SANITIZE (this);
  206. if (!u.format.sanitize (c)) return TRACE_RETURN (false);
  207. switch (u.format) {
  208. case 1: return TRACE_RETURN (u.format1.sanitize (c));
  209. case 2: return TRACE_RETURN (u.format2.sanitize (c));
  210. default:return TRACE_RETURN (true);
  211. }
  212. }
  213. protected:
  214. union {
  215. USHORT format; /* Format identifier */
  216. SingleSubstFormat1 format1;
  217. SingleSubstFormat2 format2;
  218. } u;
  219. };
  220. struct Sequence
  221. {
  222. inline void closure (hb_closure_context_t *c) const
  223. {
  224. TRACE_CLOSURE (this);
  225. unsigned int count = substitute.len;
  226. for (unsigned int i = 0; i < count; i++)
  227. c->glyphs->add (substitute[i]);
  228. }
  229. inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  230. {
  231. TRACE_COLLECT_GLYPHS (this);
  232. unsigned int count = substitute.len;
  233. for (unsigned int i = 0; i < count; i++)
  234. c->output->add (substitute[i]);
  235. }
  236. inline bool apply (hb_apply_context_t *c) const
  237. {
  238. TRACE_APPLY (this);
  239. if (unlikely (!substitute.len)) return TRACE_RETURN (false);
  240. unsigned int klass = _hb_glyph_info_is_ligature (&c->buffer->cur()) ?
  241. HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH : 0;
  242. unsigned int count = substitute.len;
  243. if (count == 1) /* Special-case to make it in-place. */
  244. {
  245. c->replace_glyph (substitute.array[0]);
  246. }
  247. else
  248. {
  249. for (unsigned int i = 0; i < count; i++) {
  250. _hb_glyph_info_set_lig_props_for_component (&c->buffer->cur(), i);
  251. c->output_glyph (substitute.array[i], klass);
  252. }
  253. c->buffer->skip_glyph ();
  254. }
  255. return TRACE_RETURN (true);
  256. }
  257. inline bool serialize (hb_serialize_context_t *c,
  258. Supplier<GlyphID> &glyphs,
  259. unsigned int num_glyphs)
  260. {
  261. TRACE_SERIALIZE (this);
  262. if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
  263. if (unlikely (!substitute.serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false);
  264. return TRACE_RETURN (true);
  265. }
  266. inline bool sanitize (hb_sanitize_context_t *c) {
  267. TRACE_SANITIZE (this);
  268. return TRACE_RETURN (substitute.sanitize (c));
  269. }
  270. protected:
  271. ArrayOf<GlyphID>
  272. substitute; /* String of GlyphIDs to substitute */
  273. public:
  274. DEFINE_SIZE_ARRAY (2, substitute);
  275. };
  276. struct MultipleSubstFormat1
  277. {
  278. inline void closure (hb_closure_context_t *c) const
  279. {
  280. TRACE_CLOSURE (this);
  281. Coverage::Iter iter;
  282. for (iter.init (this+coverage); iter.more (); iter.next ()) {
  283. if (c->glyphs->has (iter.get_glyph ()))
  284. (this+sequence[iter.get_coverage ()]).closure (c);
  285. }
  286. }
  287. inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  288. {
  289. TRACE_COLLECT_GLYPHS (this);
  290. (this+coverage).add_coverage (c->input);
  291. unsigned int count = sequence.len;
  292. for (unsigned int i = 0; i < count; i++)
  293. (this+sequence[i]).collect_glyphs (c);
  294. }
  295. inline const Coverage &get_coverage (void) const
  296. {
  297. return this+coverage;
  298. }
  299. inline bool would_apply (hb_would_apply_context_t *c) const
  300. {
  301. TRACE_WOULD_APPLY (this);
  302. return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
  303. }
  304. inline bool apply (hb_apply_context_t *c) const
  305. {
  306. TRACE_APPLY (this);
  307. unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
  308. if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
  309. return TRACE_RETURN ((this+sequence[index]).apply (c));
  310. }
  311. inline bool serialize (hb_serialize_context_t *c,
  312. Supplier<GlyphID> &glyphs,
  313. Supplier<unsigned int> &substitute_len_list,
  314. unsigned int num_glyphs,
  315. Supplier<GlyphID> &substitute_glyphs_list)
  316. {
  317. TRACE_SERIALIZE (this);
  318. if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
  319. if (unlikely (!sequence.serialize (c, num_glyphs))) return TRACE_RETURN (false);
  320. for (unsigned int i = 0; i < num_glyphs; i++)
  321. if (unlikely (!sequence[i].serialize (c, this).serialize (c,
  322. substitute_glyphs_list,
  323. substitute_len_list[i]))) return TRACE_RETURN (false);
  324. substitute_len_list.advance (num_glyphs);
  325. if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false);
  326. return TRACE_RETURN (true);
  327. }
  328. inline bool sanitize (hb_sanitize_context_t *c) {
  329. TRACE_SANITIZE (this);
  330. return TRACE_RETURN (coverage.sanitize (c, this) && sequence.sanitize (c, this));
  331. }
  332. protected:
  333. USHORT format; /* Format identifier--format = 1 */
  334. OffsetTo<Coverage>
  335. coverage; /* Offset to Coverage table--from
  336. * beginning of Substitution table */
  337. OffsetArrayOf<Sequence>
  338. sequence; /* Array of Sequence tables
  339. * ordered by Coverage Index */
  340. public:
  341. DEFINE_SIZE_ARRAY (6, sequence);
  342. };
  343. struct MultipleSubst
  344. {
  345. inline bool serialize (hb_serialize_context_t *c,
  346. Supplier<GlyphID> &glyphs,
  347. Supplier<unsigned int> &substitute_len_list,
  348. unsigned int num_glyphs,
  349. Supplier<GlyphID> &substitute_glyphs_list)
  350. {
  351. TRACE_SERIALIZE (this);
  352. if (unlikely (!c->extend_min (u.format))) return TRACE_RETURN (false);
  353. unsigned int format = 1;
  354. u.format.set (format);
  355. switch (u.format) {
  356. case 1: return TRACE_RETURN (u.format1.serialize (c, glyphs, substitute_len_list, num_glyphs, substitute_glyphs_list));
  357. default:return TRACE_RETURN (false);
  358. }
  359. }
  360. template <typename context_t>
  361. inline typename context_t::return_t dispatch (context_t *c) const
  362. {
  363. TRACE_DISPATCH (this);
  364. switch (u.format) {
  365. case 1: return TRACE_RETURN (c->dispatch (u.format1));
  366. default:return TRACE_RETURN (c->default_return_value ());
  367. }
  368. }
  369. inline bool sanitize (hb_sanitize_context_t *c) {
  370. TRACE_SANITIZE (this);
  371. if (!u.format.sanitize (c)) return TRACE_RETURN (false);
  372. switch (u.format) {
  373. case 1: return TRACE_RETURN (u.format1.sanitize (c));
  374. default:return TRACE_RETURN (true);
  375. }
  376. }
  377. protected:
  378. union {
  379. USHORT format; /* Format identifier */
  380. MultipleSubstFormat1 format1;
  381. } u;
  382. };
  383. typedef ArrayOf<GlyphID> AlternateSet; /* Array of alternate GlyphIDs--in
  384. * arbitrary order */
  385. struct AlternateSubstFormat1
  386. {
  387. inline void closure (hb_closure_context_t *c) const
  388. {
  389. TRACE_CLOSURE (this);
  390. Coverage::Iter iter;
  391. for (iter.init (this+coverage); iter.more (); iter.next ()) {
  392. if (c->glyphs->has (iter.get_glyph ())) {
  393. const AlternateSet &alt_set = this+alternateSet[iter.get_coverage ()];
  394. unsigned int count = alt_set.len;
  395. for (unsigned int i = 0; i < count; i++)
  396. c->glyphs->add (alt_set[i]);
  397. }
  398. }
  399. }
  400. inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  401. {
  402. TRACE_COLLECT_GLYPHS (this);
  403. Coverage::Iter iter;
  404. for (iter.init (this+coverage); iter.more (); iter.next ()) {
  405. c->input->add (iter.get_glyph ());
  406. const AlternateSet &alt_set = this+alternateSet[iter.get_coverage ()];
  407. unsigned int count = alt_set.len;
  408. for (unsigned int i = 0; i < count; i++)
  409. c->output->add (alt_set[i]);
  410. }
  411. }
  412. inline const Coverage &get_coverage (void) const
  413. {
  414. return this+coverage;
  415. }
  416. inline bool would_apply (hb_would_apply_context_t *c) const
  417. {
  418. TRACE_WOULD_APPLY (this);
  419. return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
  420. }
  421. inline bool apply (hb_apply_context_t *c) const
  422. {
  423. TRACE_APPLY (this);
  424. hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
  425. unsigned int index = (this+coverage).get_coverage (glyph_id);
  426. if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
  427. const AlternateSet &alt_set = this+alternateSet[index];
  428. if (unlikely (!alt_set.len)) return TRACE_RETURN (false);
  429. hb_mask_t glyph_mask = c->buffer->cur().mask;
  430. hb_mask_t lookup_mask = c->lookup_mask;
  431. /* Note: This breaks badly if two features enabled this lookup together. */
  432. unsigned int shift = _hb_ctz (lookup_mask);
  433. unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift);
  434. if (unlikely (alt_index > alt_set.len || alt_index == 0)) return TRACE_RETURN (false);
  435. glyph_id = alt_set[alt_index - 1];
  436. c->replace_glyph (glyph_id);
  437. return TRACE_RETURN (true);
  438. }
  439. inline bool serialize (hb_serialize_context_t *c,
  440. Supplier<GlyphID> &glyphs,
  441. Supplier<unsigned int> &alternate_len_list,
  442. unsigned int num_glyphs,
  443. Supplier<GlyphID> &alternate_glyphs_list)
  444. {
  445. TRACE_SERIALIZE (this);
  446. if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
  447. if (unlikely (!alternateSet.serialize (c, num_glyphs))) return TRACE_RETURN (false);
  448. for (unsigned int i = 0; i < num_glyphs; i++)
  449. if (unlikely (!alternateSet[i].serialize (c, this).serialize (c,
  450. alternate_glyphs_list,
  451. alternate_len_list[i]))) return TRACE_RETURN (false);
  452. alternate_len_list.advance (num_glyphs);
  453. if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return TRACE_RETURN (false);
  454. return TRACE_RETURN (true);
  455. }
  456. inline bool sanitize (hb_sanitize_context_t *c) {
  457. TRACE_SANITIZE (this);
  458. return TRACE_RETURN (coverage.sanitize (c, this) && alternateSet.sanitize (c, this));
  459. }
  460. protected:
  461. USHORT format; /* Format identifier--format = 1 */
  462. OffsetTo<Coverage>
  463. coverage; /* Offset to Coverage table--from
  464. * beginning of Substitution table */
  465. OffsetArrayOf<AlternateSet>
  466. alternateSet; /* Array of AlternateSet tables
  467. * ordered by Coverage Index */
  468. public:
  469. DEFINE_SIZE_ARRAY (6, alternateSet);
  470. };
  471. struct AlternateSubst
  472. {
  473. inline bool serialize (hb_serialize_context_t *c,
  474. Supplier<GlyphID> &glyphs,
  475. Supplier<unsigned int> &alternate_len_list,
  476. unsigned int num_glyphs,
  477. Supplier<GlyphID> &alternate_glyphs_list)
  478. {
  479. TRACE_SERIALIZE (this);
  480. if (unlikely (!c->extend_min (u.format))) return TRACE_RETURN (false);
  481. unsigned int format = 1;
  482. u.format.set (format);
  483. switch (u.format) {
  484. case 1: return TRACE_RETURN (u.format1.serialize (c, glyphs, alternate_len_list, num_glyphs, alternate_glyphs_list));
  485. default:return TRACE_RETURN (false);
  486. }
  487. }
  488. template <typename context_t>
  489. inline typename context_t::return_t dispatch (context_t *c) const
  490. {
  491. TRACE_DISPATCH (this);
  492. switch (u.format) {
  493. case 1: return TRACE_RETURN (c->dispatch (u.format1));
  494. default:return TRACE_RETURN (c->default_return_value ());
  495. }
  496. }
  497. inline bool sanitize (hb_sanitize_context_t *c) {
  498. TRACE_SANITIZE (this);
  499. if (!u.format.sanitize (c)) return TRACE_RETURN (false);
  500. switch (u.format) {
  501. case 1: return TRACE_RETURN (u.format1.sanitize (c));
  502. default:return TRACE_RETURN (true);
  503. }
  504. }
  505. protected:
  506. union {
  507. USHORT format; /* Format identifier */
  508. AlternateSubstFormat1 format1;
  509. } u;
  510. };
  511. struct Ligature
  512. {
  513. inline void closure (hb_closure_context_t *c) const
  514. {
  515. TRACE_CLOSURE (this);
  516. unsigned int count = component.len;
  517. for (unsigned int i = 1; i < count; i++)
  518. if (!c->glyphs->has (component[i]))
  519. return;
  520. c->glyphs->add (ligGlyph);
  521. }
  522. inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  523. {
  524. TRACE_COLLECT_GLYPHS (this);
  525. unsigned int count = component.len;
  526. for (unsigned int i = 1; i < count; i++)
  527. c->input->add (component[i]);
  528. c->output->add (ligGlyph);
  529. }
  530. inline bool would_apply (hb_would_apply_context_t *c) const
  531. {
  532. TRACE_WOULD_APPLY (this);
  533. if (c->len != component.len)
  534. return TRACE_RETURN (false);
  535. for (unsigned int i = 1; i < c->len; i++)
  536. if (likely (c->glyphs[i] != component[i]))
  537. return TRACE_RETURN (false);
  538. return TRACE_RETURN (true);
  539. }
  540. inline bool apply (hb_apply_context_t *c) const
  541. {
  542. TRACE_APPLY (this);
  543. unsigned int count = component.len;
  544. if (unlikely (count < 1)) return TRACE_RETURN (false);
  545. bool is_mark_ligature = false;
  546. unsigned int total_component_count = 0;
  547. unsigned int match_length = 0;
  548. unsigned int match_positions[MAX_CONTEXT_LENGTH];
  549. if (likely (!match_input (c, count,
  550. &component[1],
  551. match_glyph,
  552. NULL,
  553. &match_length,
  554. match_positions,
  555. &is_mark_ligature,
  556. &total_component_count)))
  557. return TRACE_RETURN (false);
  558. ligate_input (c,
  559. count,
  560. match_positions,
  561. match_length,
  562. ligGlyph,
  563. is_mark_ligature,
  564. total_component_count);
  565. return TRACE_RETURN (true);
  566. }
  567. inline bool serialize (hb_serialize_context_t *c,
  568. GlyphID ligature,
  569. Supplier<GlyphID> &components, /* Starting from second */
  570. unsigned int num_components /* Including first component */)
  571. {
  572. TRACE_SERIALIZE (this);
  573. if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
  574. ligGlyph = ligature;
  575. if (unlikely (!component.serialize (c, components, num_components))) return TRACE_RETURN (false);
  576. return TRACE_RETURN (true);
  577. }
  578. public:
  579. inline bool sanitize (hb_sanitize_context_t *c) {
  580. TRACE_SANITIZE (this);
  581. return TRACE_RETURN (ligGlyph.sanitize (c) && component.sanitize (c));
  582. }
  583. protected:
  584. GlyphID ligGlyph; /* GlyphID of ligature to substitute */
  585. HeadlessArrayOf<GlyphID>
  586. component; /* Array of component GlyphIDs--start
  587. * with the second component--ordered
  588. * in writing direction */
  589. public:
  590. DEFINE_SIZE_ARRAY (4, component);
  591. };
  592. struct LigatureSet
  593. {
  594. inline void closure (hb_closure_context_t *c) const
  595. {
  596. TRACE_CLOSURE (this);
  597. unsigned int num_ligs = ligature.len;
  598. for (unsigned int i = 0; i < num_ligs; i++)
  599. (this+ligature[i]).closure (c);
  600. }
  601. inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  602. {
  603. TRACE_COLLECT_GLYPHS (this);
  604. unsigned int num_ligs = ligature.len;
  605. for (unsigned int i = 0; i < num_ligs; i++)
  606. (this+ligature[i]).collect_glyphs (c);
  607. }
  608. inline bool would_apply (hb_would_apply_context_t *c) const
  609. {
  610. TRACE_WOULD_APPLY (this);
  611. unsigned int num_ligs = ligature.len;
  612. for (unsigned int i = 0; i < num_ligs; i++)
  613. {
  614. const Ligature &lig = this+ligature[i];
  615. if (lig.would_apply (c))
  616. return TRACE_RETURN (true);
  617. }
  618. return TRACE_RETURN (false);
  619. }
  620. inline bool apply (hb_apply_context_t *c) const
  621. {
  622. TRACE_APPLY (this);
  623. unsigned int num_ligs = ligature.len;
  624. for (unsigned int i = 0; i < num_ligs; i++)
  625. {
  626. const Ligature &lig = this+ligature[i];
  627. if (lig.apply (c)) return TRACE_RETURN (true);
  628. }
  629. return TRACE_RETURN (false);
  630. }
  631. inline bool serialize (hb_serialize_context_t *c,
  632. Supplier<GlyphID> &ligatures,
  633. Supplier<unsigned int> &component_count_list,
  634. unsigned int num_ligatures,
  635. Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
  636. {
  637. TRACE_SERIALIZE (this);
  638. if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
  639. if (unlikely (!ligature.serialize (c, num_ligatures))) return TRACE_RETURN (false);
  640. for (unsigned int i = 0; i < num_ligatures; i++)
  641. if (unlikely (!ligature[i].serialize (c, this).serialize (c,
  642. ligatures[i],
  643. component_list,
  644. component_count_list[i]))) return TRACE_RETURN (false);
  645. ligatures.advance (num_ligatures);
  646. component_count_list.advance (num_ligatures);
  647. return TRACE_RETURN (true);
  648. }
  649. inline bool sanitize (hb_sanitize_context_t *c) {
  650. TRACE_SANITIZE (this);
  651. return TRACE_RETURN (ligature.sanitize (c, this));
  652. }
  653. protected:
  654. OffsetArrayOf<Ligature>
  655. ligature; /* Array LigatureSet tables
  656. * ordered by preference */
  657. public:
  658. DEFINE_SIZE_ARRAY (2, ligature);
  659. };
  660. struct LigatureSubstFormat1
  661. {
  662. inline void closure (hb_closure_context_t *c) const
  663. {
  664. TRACE_CLOSURE (this);
  665. Coverage::Iter iter;
  666. for (iter.init (this+coverage); iter.more (); iter.next ()) {
  667. if (c->glyphs->has (iter.get_glyph ()))
  668. (this+ligatureSet[iter.get_coverage ()]).closure (c);
  669. }
  670. }
  671. inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  672. {
  673. TRACE_COLLECT_GLYPHS (this);
  674. Coverage::Iter iter;
  675. for (iter.init (this+coverage); iter.more (); iter.next ()) {
  676. c->input->add (iter.get_glyph ());
  677. (this+ligatureSet[iter.get_coverage ()]).collect_glyphs (c);
  678. }
  679. }
  680. inline const Coverage &get_coverage (void) const
  681. {
  682. return this+coverage;
  683. }
  684. inline bool would_apply (hb_would_apply_context_t *c) const
  685. {
  686. TRACE_WOULD_APPLY (this);
  687. unsigned int index = (this+coverage).get_coverage (c->glyphs[0]);
  688. if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
  689. const LigatureSet &lig_set = this+ligatureSet[index];
  690. return TRACE_RETURN (lig_set.would_apply (c));
  691. }
  692. inline bool apply (hb_apply_context_t *c) const
  693. {
  694. TRACE_APPLY (this);
  695. hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
  696. unsigned int index = (this+coverage).get_coverage (glyph_id);
  697. if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
  698. const LigatureSet &lig_set = this+ligatureSet[index];
  699. return TRACE_RETURN (lig_set.apply (c));
  700. }
  701. inline bool serialize (hb_serialize_context_t *c,
  702. Supplier<GlyphID> &first_glyphs,
  703. Supplier<unsigned int> &ligature_per_first_glyph_count_list,
  704. unsigned int num_first_glyphs,
  705. Supplier<GlyphID> &ligatures_list,
  706. Supplier<unsigned int> &component_count_list,
  707. Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
  708. {
  709. TRACE_SERIALIZE (this);
  710. if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
  711. if (unlikely (!ligatureSet.serialize (c, num_first_glyphs))) return TRACE_RETURN (false);
  712. for (unsigned int i = 0; i < num_first_glyphs; i++)
  713. if (unlikely (!ligatureSet[i].serialize (c, this).serialize (c,
  714. ligatures_list,
  715. component_count_list,
  716. ligature_per_first_glyph_count_list[i],
  717. component_list))) return TRACE_RETURN (false);
  718. ligature_per_first_glyph_count_list.advance (num_first_glyphs);
  719. if (unlikely (!coverage.serialize (c, this).serialize (c, first_glyphs, num_first_glyphs))) return TRACE_RETURN (false);
  720. return TRACE_RETURN (true);
  721. }
  722. inline bool sanitize (hb_sanitize_context_t *c) {
  723. TRACE_SANITIZE (this);
  724. return TRACE_RETURN (coverage.sanitize (c, this) && ligatureSet.sanitize (c, this));
  725. }
  726. protected:
  727. USHORT format; /* Format identifier--format = 1 */
  728. OffsetTo<Coverage>
  729. coverage; /* Offset to Coverage table--from
  730. * beginning of Substitution table */
  731. OffsetArrayOf<LigatureSet>
  732. ligatureSet; /* Array LigatureSet tables
  733. * ordered by Coverage Index */
  734. public:
  735. DEFINE_SIZE_ARRAY (6, ligatureSet);
  736. };
  737. struct LigatureSubst
  738. {
  739. inline bool serialize (hb_serialize_context_t *c,
  740. Supplier<GlyphID> &first_glyphs,
  741. Supplier<unsigned int> &ligature_per_first_glyph_count_list,
  742. unsigned int num_first_glyphs,
  743. Supplier<GlyphID> &ligatures_list,
  744. Supplier<unsigned int> &component_count_list,
  745. Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
  746. {
  747. TRACE_SERIALIZE (this);
  748. if (unlikely (!c->extend_min (u.format))) return TRACE_RETURN (false);
  749. unsigned int format = 1;
  750. u.format.set (format);
  751. switch (u.format) {
  752. case 1: return TRACE_RETURN (u.format1.serialize (c, first_glyphs, ligature_per_first_glyph_count_list, num_first_glyphs,
  753. ligatures_list, component_count_list, component_list));
  754. default:return TRACE_RETURN (false);
  755. }
  756. }
  757. template <typename context_t>
  758. inline typename context_t::return_t dispatch (context_t *c) const
  759. {
  760. TRACE_DISPATCH (this);
  761. switch (u.format) {
  762. case 1: return TRACE_RETURN (c->dispatch (u.format1));
  763. default:return TRACE_RETURN (c->default_return_value ());
  764. }
  765. }
  766. inline bool sanitize (hb_sanitize_context_t *c) {
  767. TRACE_SANITIZE (this);
  768. if (!u.format.sanitize (c)) return TRACE_RETURN (false);
  769. switch (u.format) {
  770. case 1: return TRACE_RETURN (u.format1.sanitize (c));
  771. default:return TRACE_RETURN (true);
  772. }
  773. }
  774. protected:
  775. union {
  776. USHORT format; /* Format identifier */
  777. LigatureSubstFormat1 format1;
  778. } u;
  779. };
  780. struct ContextSubst : Context {};
  781. struct ChainContextSubst : ChainContext {};
  782. struct ExtensionSubst : Extension<ExtensionSubst>
  783. {
  784. typedef struct SubstLookupSubTable LookupSubTable;
  785. inline bool is_reverse (void) const;
  786. };
  787. struct ReverseChainSingleSubstFormat1
  788. {
  789. inline void closure (hb_closure_context_t *c) const
  790. {
  791. TRACE_CLOSURE (this);
  792. const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
  793. unsigned int count;
  794. count = backtrack.len;
  795. for (unsigned int i = 0; i < count; i++)
  796. if (!(this+backtrack[i]).intersects (c->glyphs))
  797. return;
  798. count = lookahead.len;
  799. for (unsigned int i = 0; i < count; i++)
  800. if (!(this+lookahead[i]).intersects (c->glyphs))
  801. return;
  802. const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
  803. Coverage::Iter iter;
  804. for (iter.init (this+coverage); iter.more (); iter.next ()) {
  805. if (c->glyphs->has (iter.get_glyph ()))
  806. c->glyphs->add (substitute[iter.get_coverage ()]);
  807. }
  808. }
  809. inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  810. {
  811. TRACE_COLLECT_GLYPHS (this);
  812. const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
  813. unsigned int count;
  814. (this+coverage).add_coverage (c->input);
  815. count = backtrack.len;
  816. for (unsigned int i = 0; i < count; i++)
  817. (this+backtrack[i]).add_coverage (c->before);
  818. count = lookahead.len;
  819. for (unsigned int i = 0; i < count; i++)
  820. (this+lookahead[i]).add_coverage (c->after);
  821. const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
  822. count = substitute.len;
  823. for (unsigned int i = 0; i < count; i++)
  824. c->output->add (substitute[i]);
  825. }
  826. inline const Coverage &get_coverage (void) const
  827. {
  828. return this+coverage;
  829. }
  830. inline bool would_apply (hb_would_apply_context_t *c) const
  831. {
  832. TRACE_WOULD_APPLY (this);
  833. return TRACE_RETURN (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
  834. }
  835. inline bool apply (hb_apply_context_t *c) const
  836. {
  837. TRACE_APPLY (this);
  838. if (unlikely (c->nesting_level_left != MAX_NESTING_LEVEL))
  839. return TRACE_RETURN (false); /* No chaining to this type */
  840. unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
  841. if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
  842. const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
  843. const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
  844. if (match_backtrack (c,
  845. backtrack.len, (USHORT *) backtrack.array,
  846. match_coverage, this) &&
  847. match_lookahead (c,
  848. lookahead.len, (USHORT *) lookahead.array,
  849. match_coverage, this,
  850. 1))
  851. {
  852. c->replace_glyph_inplace (substitute[index]);
  853. /* Note: We DON'T decrease buffer->idx. The main loop does it
  854. * for us. This is useful for preventing surprises if someone
  855. * calls us through a Context lookup. */
  856. return TRACE_RETURN (true);
  857. }
  858. return TRACE_RETURN (false);
  859. }
  860. inline bool sanitize (hb_sanitize_context_t *c) {
  861. TRACE_SANITIZE (this);
  862. if (!(coverage.sanitize (c, this) && backtrack.sanitize (c, this)))
  863. return TRACE_RETURN (false);
  864. OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
  865. if (!lookahead.sanitize (c, this))
  866. return TRACE_RETURN (false);
  867. ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
  868. return TRACE_RETURN (substitute.sanitize (c));
  869. }
  870. protected:
  871. USHORT format; /* Format identifier--format = 1 */
  872. OffsetTo<Coverage>
  873. coverage; /* Offset to Coverage table--from
  874. * beginning of table */
  875. OffsetArrayOf<Coverage>
  876. backtrack; /* Array of coverage tables
  877. * in backtracking sequence, in glyph
  878. * sequence order */
  879. OffsetArrayOf<Coverage>
  880. lookaheadX; /* Array of coverage tables
  881. * in lookahead sequence, in glyph
  882. * sequence order */
  883. ArrayOf<GlyphID>
  884. substituteX; /* Array of substitute
  885. * GlyphIDs--ordered by Coverage Index */
  886. public:
  887. DEFINE_SIZE_MIN (10);
  888. };
  889. struct ReverseChainSingleSubst
  890. {
  891. template <typename context_t>
  892. inline typename context_t::return_t dispatch (context_t *c) const
  893. {
  894. TRACE_DISPATCH (this);
  895. switch (u.format) {
  896. case 1: return TRACE_RETURN (c->dispatch (u.format1));
  897. default:return TRACE_RETURN (c->default_return_value ());
  898. }
  899. }
  900. inline bool sanitize (hb_sanitize_context_t *c) {
  901. TRACE_SANITIZE (this);
  902. if (!u.format.sanitize (c)) return TRACE_RETURN (false);
  903. switch (u.format) {
  904. case 1: return TRACE_RETURN (u.format1.sanitize (c));
  905. default:return TRACE_RETURN (true);
  906. }
  907. }
  908. protected:
  909. union {
  910. USHORT format; /* Format identifier */
  911. ReverseChainSingleSubstFormat1 format1;
  912. } u;
  913. };
  914. /*
  915. * SubstLookup
  916. */
  917. struct SubstLookupSubTable
  918. {
  919. friend struct SubstLookup;
  920. enum Type {
  921. Single = 1,
  922. Multiple = 2,
  923. Alternate = 3,
  924. Ligature = 4,
  925. Context = 5,
  926. ChainContext = 6,
  927. Extension = 7,
  928. ReverseChainSingle = 8
  929. };
  930. template <typename context_t>
  931. inline typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type) const
  932. {
  933. TRACE_DISPATCH (this);
  934. switch (lookup_type) {
  935. case Single: return TRACE_RETURN (u.single.dispatch (c));
  936. case Multiple: return TRACE_RETURN (u.multiple.dispatch (c));
  937. case Alternate: return TRACE_RETURN (u.alternate.dispatch (c));
  938. case Ligature: return TRACE_RETURN (u.ligature.dispatch (c));
  939. case Context: return TRACE_RETURN (u.context.dispatch (c));
  940. case ChainContext: return TRACE_RETURN (u.chainContext.dispatch (c));
  941. case Extension: return TRACE_RETURN (u.extension.dispatch (c));
  942. case ReverseChainSingle: return TRACE_RETURN (u.reverseChainContextSingle.dispatch (c));
  943. default: return TRACE_RETURN (c->default_return_value ());
  944. }
  945. }
  946. inline bool sanitize (hb_sanitize_context_t *c, unsigned int lookup_type) {
  947. TRACE_SANITIZE (this);
  948. if (!u.header.sub_format.sanitize (c))
  949. return TRACE_RETURN (false);
  950. switch (lookup_type) {
  951. case Single: return TRACE_RETURN (u.single.sanitize (c));
  952. case Multiple: return TRACE_RETURN (u.multiple.sanitize (c));
  953. case Alternate: return TRACE_RETURN (u.alternate.sanitize (c));
  954. case Ligature: return TRACE_RETURN (u.ligature.sanitize (c));
  955. case Context: return TRACE_RETURN (u.context.sanitize (c));
  956. case ChainContext: return TRACE_RETURN (u.chainContext.sanitize (c));
  957. case Extension: return TRACE_RETURN (u.extension.sanitize (c));
  958. case ReverseChainSingle: return TRACE_RETURN (u.reverseChainContextSingle.sanitize (c));
  959. default: return TRACE_RETURN (true);
  960. }
  961. }
  962. protected:
  963. union {
  964. struct {
  965. USHORT sub_format;
  966. } header;
  967. SingleSubst single;
  968. MultipleSubst multiple;
  969. AlternateSubst alternate;
  970. LigatureSubst ligature;
  971. ContextSubst context;
  972. ChainContextSubst chainContext;
  973. ExtensionSubst extension;
  974. ReverseChainSingleSubst reverseChainContextSingle;
  975. } u;
  976. public:
  977. DEFINE_SIZE_UNION (2, header.sub_format);
  978. };
  979. struct SubstLookup : Lookup
  980. {
  981. inline const SubstLookupSubTable& get_subtable (unsigned int i) const
  982. { return this+CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable)[i]; }
  983. inline static bool lookup_type_is_reverse (unsigned int lookup_type)
  984. { return lookup_type == SubstLookupSubTable::ReverseChainSingle; }
  985. inline bool is_reverse (void) const
  986. {
  987. unsigned int type = get_type ();
  988. if (unlikely (type == SubstLookupSubTable::Extension))
  989. return CastR<ExtensionSubst> (get_subtable(0)).is_reverse ();
  990. return lookup_type_is_reverse (type);
  991. }
  992. inline hb_closure_context_t::return_t closure (hb_closure_context_t *c) const
  993. {
  994. TRACE_CLOSURE (this);
  995. c->set_recurse_func (dispatch_recurse_func<hb_closure_context_t>);
  996. return TRACE_RETURN (dispatch (c));
  997. }
  998. inline hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
  999. {
  1000. TRACE_COLLECT_GLYPHS (this);
  1001. c->set_recurse_func (dispatch_recurse_func<hb_collect_glyphs_context_t>);
  1002. return TRACE_RETURN (dispatch (c));
  1003. }
  1004. template <typename set_t>
  1005. inline void add_coverage (set_t *glyphs) const
  1006. {
  1007. hb_get_coverage_context_t c;
  1008. const Coverage *last = NULL;
  1009. unsigned int count = get_subtable_count ();
  1010. for (unsigned int i = 0; i < count; i++) {
  1011. const Coverage *coverage = &get_subtable (i).dispatch (&c, get_type ());
  1012. if (coverage != last) {
  1013. coverage->add_coverage (glyphs);
  1014. last = coverage;
  1015. }
  1016. }
  1017. }
  1018. inline bool would_apply (hb_would_apply_context_t *c, const hb_set_digest_t *digest) const
  1019. {
  1020. TRACE_WOULD_APPLY (this);
  1021. if (unlikely (!c->len)) return TRACE_RETURN (false);
  1022. if (!digest->may_have (c->glyphs[0])) return TRACE_RETURN (false);
  1023. return TRACE_RETURN (dispatch (c));
  1024. }
  1025. inline bool apply_once (hb_apply_context_t *c) const
  1026. {
  1027. TRACE_APPLY (this);
  1028. if (!c->check_glyph_property (&c->buffer->cur(), c->lookup_props))
  1029. return TRACE_RETURN (false);
  1030. return TRACE_RETURN (dispatch (c));
  1031. }
  1032. static bool apply_recurse_func (hb_apply_context_t *c, unsigned int lookup_index);
  1033. inline SubstLookupSubTable& serialize_subtable (hb_serialize_context_t *c,
  1034. unsigned int i)
  1035. { return CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable)[i].serialize (c, this); }
  1036. inline bool serialize_single (hb_serialize_context_t *c,
  1037. uint32_t lookup_props,
  1038. Supplier<GlyphID> &glyphs,
  1039. Supplier<GlyphID> &substitutes,
  1040. unsigned int num_glyphs)
  1041. {
  1042. TRACE_SERIALIZE (this);
  1043. if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Single, lookup_props, 1))) return TRACE_RETURN (false);
  1044. return TRACE_RETURN (serialize_subtable (c, 0).u.single.serialize (c, glyphs, substitutes, num_glyphs));
  1045. }
  1046. inline bool serialize_multiple (hb_serialize_context_t *c,
  1047. uint32_t lookup_props,
  1048. Supplier<GlyphID> &glyphs,
  1049. Supplier<unsigned int> &substitute_len_list,
  1050. unsigned int num_glyphs,
  1051. Supplier<GlyphID> &substitute_glyphs_list)
  1052. {
  1053. TRACE_SERIALIZE (this);
  1054. if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Multiple, lookup_props, 1))) return TRACE_RETURN (false);
  1055. return TRACE_RETURN (serialize_subtable (c, 0).u.multiple.serialize (c, glyphs, substitute_len_list, num_glyphs,
  1056. substitute_glyphs_list));
  1057. }
  1058. inline bool serialize_alternate (hb_serialize_context_t *c,
  1059. uint32_t lookup_props,
  1060. Supplier<GlyphID> &glyphs,
  1061. Supplier<unsigned int> &alternate_len_list,
  1062. unsigned int num_glyphs,
  1063. Supplier<GlyphID> &alternate_glyphs_list)
  1064. {
  1065. TRACE_SERIALIZE (this);
  1066. if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Alternate, lookup_props, 1))) return TRACE_RETURN (false);
  1067. return TRACE_RETURN (serialize_subtable (c, 0).u.alternate.serialize (c, glyphs, alternate_len_list, num_glyphs,
  1068. alternate_glyphs_list));
  1069. }
  1070. inline bool serialize_ligature (hb_serialize_context_t *c,
  1071. uint32_t lookup_props,
  1072. Supplier<GlyphID> &first_glyphs,
  1073. Supplier<unsigned int> &ligature_per_first_glyph_count_list,
  1074. unsigned int num_first_glyphs,
  1075. Supplier<GlyphID> &ligatures_list,
  1076. Supplier<unsigned int> &component_count_list,
  1077. Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
  1078. {
  1079. TRACE_SERIALIZE (this);
  1080. if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Ligature, lookup_props, 1))) return TRACE_RETURN (false);
  1081. return TRACE_RETURN (serialize_subtable (c, 0).u.ligature.serialize (c, first_glyphs, ligature_per_first_glyph_count_list, num_first_glyphs,
  1082. ligatures_list, component_count_list, component_list));
  1083. }
  1084. template <typename context_t>
  1085. static inline typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);
  1086. template <typename context_t>
  1087. inline typename context_t::return_t dispatch (context_t *c) const
  1088. {
  1089. TRACE_DISPATCH (this);
  1090. unsigned int lookup_type = get_type ();
  1091. unsigned int count = get_subtable_count ();
  1092. for (unsigned int i = 0; i < count; i++) {
  1093. typename context_t::return_t r = get_subtable (i).dispatch (c, lookup_type);
  1094. if (c->stop_sublookup_iteration (r))
  1095. return TRACE_RETURN (r);
  1096. }
  1097. return TRACE_RETURN (c->default_return_value ());
  1098. }
  1099. inline bool sanitize (hb_sanitize_context_t *c)
  1100. {
  1101. TRACE_SANITIZE (this);
  1102. if (unlikely (!Lookup::sanitize (c))) return TRACE_RETURN (false);
  1103. OffsetArrayOf<SubstLookupSubTable> &list = CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable);
  1104. if (unlikely (!list.sanitize (c, this, get_type ()))) return TRACE_RETURN (false);
  1105. if (unlikely (get_type () == SubstLookupSubTable::Extension))
  1106. {
  1107. /* The spec says all subtables of an Extension lookup should
  1108. * have the same type. This is specially important if one has
  1109. * a reverse type! */
  1110. unsigned int type = get_subtable (0).u.extension.get_type ();
  1111. unsigned int count = get_subtable_count ();
  1112. for (unsigned int i = 1; i < count; i++)
  1113. if (get_subtable (i).u.extension.get_type () != type)
  1114. return TRACE_RETURN (false);
  1115. }
  1116. return TRACE_RETURN (true);
  1117. }
  1118. };
  1119. typedef OffsetListOf<SubstLookup> SubstLookupList;
  1120. /*
  1121. * GSUB -- The Glyph Substitution Table
  1122. */
  1123. struct GSUB : GSUBGPOS
  1124. {
  1125. static const hb_tag_t tableTag = HB_OT_TAG_GSUB;
  1126. inline const SubstLookup& get_lookup (unsigned int i) const
  1127. { return CastR<SubstLookup> (GSUBGPOS::get_lookup (i)); }
  1128. static inline void substitute_start (hb_font_t *font, hb_buffer_t *buffer);
  1129. static inline void substitute_finish (hb_font_t *font, hb_buffer_t *buffer);
  1130. inline bool sanitize (hb_sanitize_context_t *c) {
  1131. TRACE_SANITIZE (this);
  1132. if (unlikely (!GSUBGPOS::sanitize (c))) return TRACE_RETURN (false);
  1133. OffsetTo<SubstLookupList> &list = CastR<OffsetTo<SubstLookupList> > (lookupList);
  1134. return TRACE_RETURN (list.sanitize (c, this));
  1135. }
  1136. public:
  1137. DEFINE_SIZE_STATIC (10);
  1138. };
  1139. void
  1140. GSUB::substitute_start (hb_font_t *font, hb_buffer_t *buffer)
  1141. {
  1142. _hb_buffer_allocate_gsubgpos_vars (buffer);
  1143. const GDEF &gdef = *hb_ot_layout_from_face (font->face)->gdef;
  1144. unsigned int count = buffer->len;
  1145. for (unsigned int i = 0; i < count; i++)
  1146. {
  1147. _hb_glyph_info_set_glyph_props (&buffer->info[i], gdef.get_glyph_props (buffer->info[i].codepoint));
  1148. _hb_glyph_info_clear_lig_props (&buffer->info[i]);
  1149. buffer->info[i].syllable() = 0;
  1150. }
  1151. }
  1152. void
  1153. GSUB::substitute_finish (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer HB_UNUSED)
  1154. {
  1155. }
  1156. /* Out-of-class implementation for methods recursing */
  1157. inline bool ExtensionSubst::is_reverse (void) const
  1158. {
  1159. unsigned int type = get_type ();
  1160. if (unlikely (type == SubstLookupSubTable::Extension))
  1161. return CastR<ExtensionSubst> (get_subtable<SubstLookupSubTable>()).is_reverse ();
  1162. return SubstLookup::lookup_type_is_reverse (type);
  1163. }
  1164. template <typename context_t>
  1165. inline typename context_t::return_t SubstLookup::dispatch_recurse_func (context_t *c, unsigned int lookup_index)
  1166. {
  1167. const GSUB &gsub = *(hb_ot_layout_from_face (c->face)->gsub);
  1168. const SubstLookup &l = gsub.get_lookup (lookup_index);
  1169. return l.dispatch (c);
  1170. }
  1171. inline bool SubstLookup::apply_recurse_func (hb_apply_context_t *c, unsigned int lookup_index)
  1172. {
  1173. const GSUB &gsub = *(hb_ot_layout_from_face (c->face)->gsub);
  1174. const SubstLookup &l = gsub.get_lookup (lookup_index);
  1175. unsigned int saved_lookup_props = c->lookup_props;
  1176. c->set_lookup (l);
  1177. bool ret = l.apply_once (c);
  1178. c->lookup_props = saved_lookup_props;
  1179. return ret;
  1180. }
  1181. } /* namespace OT */
  1182. #endif /* HB_OT_LAYOUT_GSUB_TABLE_HH */