PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/thirdparty/harfbuzz/src/hb-ot-shape-complex-myanmar.cc

https://gitlab.com/godotengine/godot
C++ | 318 lines | 225 code | 43 blank | 50 comment | 38 complexity | 0503c11ad9ed879575050902e336e368 MD5 | raw file
  1. /*
  2. * Copyright © 2011,2012,2013 Google, Inc.
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. *
  24. * Google Author(s): Behdad Esfahbod
  25. */
  26. #include "hb.hh"
  27. #ifndef HB_NO_OT_SHAPE
  28. #include "hb-ot-shape-complex-myanmar.hh"
  29. #include "hb-ot-shape-complex-myanmar-machine.hh"
  30. /*
  31. * Myanmar shaper.
  32. */
  33. static const hb_tag_t
  34. myanmar_basic_features[] =
  35. {
  36. /*
  37. * Basic features.
  38. * These features are applied in order, one at a time, after reordering,
  39. * constrained to the syllable.
  40. */
  41. HB_TAG('r','p','h','f'),
  42. HB_TAG('p','r','e','f'),
  43. HB_TAG('b','l','w','f'),
  44. HB_TAG('p','s','t','f'),
  45. };
  46. static const hb_tag_t
  47. myanmar_other_features[] =
  48. {
  49. /*
  50. * Other features.
  51. * These features are applied all at once, after clearing syllables.
  52. */
  53. HB_TAG('p','r','e','s'),
  54. HB_TAG('a','b','v','s'),
  55. HB_TAG('b','l','w','s'),
  56. HB_TAG('p','s','t','s'),
  57. };
  58. static void
  59. setup_syllables_myanmar (const hb_ot_shape_plan_t *plan,
  60. hb_font_t *font,
  61. hb_buffer_t *buffer);
  62. static void
  63. reorder_myanmar (const hb_ot_shape_plan_t *plan,
  64. hb_font_t *font,
  65. hb_buffer_t *buffer);
  66. static void
  67. collect_features_myanmar (hb_ot_shape_planner_t *plan)
  68. {
  69. hb_ot_map_builder_t *map = &plan->map;
  70. /* Do this before any lookups have been applied. */
  71. map->add_gsub_pause (setup_syllables_myanmar);
  72. map->enable_feature (HB_TAG('l','o','c','l'), F_PER_SYLLABLE);
  73. /* The Indic specs do not require ccmp, but we apply it here since if
  74. * there is a use of it, it's typically at the beginning. */
  75. map->enable_feature (HB_TAG('c','c','m','p'), F_PER_SYLLABLE);
  76. map->add_gsub_pause (reorder_myanmar);
  77. for (unsigned int i = 0; i < ARRAY_LENGTH (myanmar_basic_features); i++)
  78. {
  79. map->enable_feature (myanmar_basic_features[i], F_MANUAL_ZWJ | F_PER_SYLLABLE);
  80. map->add_gsub_pause (nullptr);
  81. }
  82. for (unsigned int i = 0; i < ARRAY_LENGTH (myanmar_other_features); i++)
  83. map->enable_feature (myanmar_other_features[i], F_MANUAL_ZWJ);
  84. }
  85. static void
  86. setup_masks_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
  87. hb_buffer_t *buffer,
  88. hb_font_t *font HB_UNUSED)
  89. {
  90. HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_category);
  91. HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_position);
  92. /* No masks, we just save information about characters. */
  93. unsigned int count = buffer->len;
  94. hb_glyph_info_t *info = buffer->info;
  95. for (unsigned int i = 0; i < count; i++)
  96. set_myanmar_properties (info[i]);
  97. }
  98. static void
  99. setup_syllables_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
  100. hb_font_t *font HB_UNUSED,
  101. hb_buffer_t *buffer)
  102. {
  103. find_syllables_myanmar (buffer);
  104. foreach_syllable (buffer, start, end)
  105. buffer->unsafe_to_break (start, end);
  106. }
  107. static int
  108. compare_myanmar_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
  109. {
  110. int a = pa->myanmar_position();
  111. int b = pb->myanmar_position();
  112. return a < b ? -1 : a == b ? 0 : +1;
  113. }
  114. /* Rules from:
  115. * https://docs.microsoft.com/en-us/typography/script-development/myanmar */
  116. static void
  117. initial_reordering_consonant_syllable (hb_buffer_t *buffer,
  118. unsigned int start, unsigned int end)
  119. {
  120. hb_glyph_info_t *info = buffer->info;
  121. unsigned int base = end;
  122. bool has_reph = false;
  123. {
  124. unsigned int limit = start;
  125. if (start + 3 <= end &&
  126. info[start ].myanmar_category() == OT_Ra &&
  127. info[start+1].myanmar_category() == OT_As &&
  128. info[start+2].myanmar_category() == OT_H)
  129. {
  130. limit += 3;
  131. base = start;
  132. has_reph = true;
  133. }
  134. {
  135. if (!has_reph)
  136. base = limit;
  137. for (unsigned int i = limit; i < end; i++)
  138. if (is_consonant (info[i]))
  139. {
  140. base = i;
  141. break;
  142. }
  143. }
  144. }
  145. /* Reorder! */
  146. {
  147. unsigned int i = start;
  148. for (; i < start + (has_reph ? 3 : 0); i++)
  149. info[i].myanmar_position() = POS_AFTER_MAIN;
  150. for (; i < base; i++)
  151. info[i].myanmar_position() = POS_PRE_C;
  152. if (i < end)
  153. {
  154. info[i].myanmar_position() = POS_BASE_C;
  155. i++;
  156. }
  157. myanmar_position_t pos = POS_AFTER_MAIN;
  158. /* The following loop may be ugly, but it implements all of
  159. * Myanmar reordering! */
  160. for (; i < end; i++)
  161. {
  162. if (info[i].myanmar_category() == OT_MR) /* Pre-base reordering */
  163. {
  164. info[i].myanmar_position() = POS_PRE_C;
  165. continue;
  166. }
  167. if (info[i].myanmar_position() < POS_BASE_C) /* Left matra */
  168. {
  169. continue;
  170. }
  171. if (info[i].myanmar_category() == OT_VS)
  172. {
  173. info[i].myanmar_position() = info[i - 1].myanmar_position();
  174. continue;
  175. }
  176. if (pos == POS_AFTER_MAIN && info[i].myanmar_category() == OT_VBlw)
  177. {
  178. pos = POS_BELOW_C;
  179. info[i].myanmar_position() = pos;
  180. continue;
  181. }
  182. if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_A)
  183. {
  184. info[i].myanmar_position() = POS_BEFORE_SUB;
  185. continue;
  186. }
  187. if (pos == POS_BELOW_C && info[i].myanmar_category() == OT_VBlw)
  188. {
  189. info[i].myanmar_position() = pos;
  190. continue;
  191. }
  192. if (pos == POS_BELOW_C && info[i].myanmar_category() != OT_A)
  193. {
  194. pos = POS_AFTER_SUB;
  195. info[i].myanmar_position() = pos;
  196. continue;
  197. }
  198. info[i].myanmar_position() = pos;
  199. }
  200. }
  201. /* Sit tight, rock 'n roll! */
  202. buffer->sort (start, end, compare_myanmar_order);
  203. }
  204. static void
  205. reorder_syllable_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
  206. hb_face_t *face HB_UNUSED,
  207. hb_buffer_t *buffer,
  208. unsigned int start, unsigned int end)
  209. {
  210. myanmar_syllable_type_t syllable_type = (myanmar_syllable_type_t) (buffer->info[start].syllable() & 0x0F);
  211. switch (syllable_type) {
  212. case myanmar_broken_cluster: /* We already inserted dotted-circles, so just call the consonant_syllable. */
  213. case myanmar_consonant_syllable:
  214. initial_reordering_consonant_syllable (buffer, start, end);
  215. break;
  216. case myanmar_punctuation_cluster:
  217. case myanmar_non_myanmar_cluster:
  218. break;
  219. }
  220. }
  221. static void
  222. reorder_myanmar (const hb_ot_shape_plan_t *plan,
  223. hb_font_t *font,
  224. hb_buffer_t *buffer)
  225. {
  226. if (buffer->message (font, "start reordering myanmar"))
  227. {
  228. hb_syllabic_insert_dotted_circles (font, buffer,
  229. myanmar_broken_cluster,
  230. OT_GB);
  231. foreach_syllable (buffer, start, end)
  232. reorder_syllable_myanmar (plan, font->face, buffer, start, end);
  233. (void) buffer->message (font, "end reordering myanmar");
  234. }
  235. HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_category);
  236. HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_position);
  237. }
  238. const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar =
  239. {
  240. collect_features_myanmar,
  241. nullptr, /* override_features */
  242. nullptr, /* data_create */
  243. nullptr, /* data_destroy */
  244. nullptr, /* preprocess_text */
  245. nullptr, /* postprocess_glyphs */
  246. HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
  247. nullptr, /* decompose */
  248. nullptr, /* compose */
  249. setup_masks_myanmar,
  250. HB_TAG_NONE, /* gpos_tag */
  251. nullptr, /* reorder_marks */
  252. HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY,
  253. false, /* fallback_position */
  254. };
  255. /* Ugly Zawgyi encoding.
  256. * Disable all auto processing.
  257. * https://github.com/harfbuzz/harfbuzz/issues/1162 */
  258. const hb_ot_complex_shaper_t _hb_ot_complex_shaper_myanmar_zawgyi =
  259. {
  260. nullptr, /* collect_features */
  261. nullptr, /* override_features */
  262. nullptr, /* data_create */
  263. nullptr, /* data_destroy */
  264. nullptr, /* preprocess_text */
  265. nullptr, /* postprocess_glyphs */
  266. HB_OT_SHAPE_NORMALIZATION_MODE_NONE,
  267. nullptr, /* decompose */
  268. nullptr, /* compose */
  269. nullptr, /* setup_masks */
  270. HB_TAG_NONE, /* gpos_tag */
  271. nullptr, /* reorder_marks */
  272. HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
  273. false, /* fallback_position */
  274. };
  275. #endif