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

/vendor/mpdf/mpdf/classes/myanmar.php

https://gitlab.com/0sajib0/NECI
PHP | 484 lines | 385 code | 61 blank | 38 comment | 92 complexity | 9d6ec6f00847157a58996fe17a0d6ce4 MD5 | raw file
  1. <?php
  2. class MYANMAR
  3. {
  4. /* FROM hb-ot-shape-complex-indic-private.hh */
  5. // indic_category
  6. const OT_X = 0;
  7. const OT_C = 1;
  8. const OT_V = 2;
  9. const OT_N = 3;
  10. const OT_H = 4;
  11. const OT_ZWNJ = 5;
  12. const OT_ZWJ = 6;
  13. const OT_M = 7; /* Matra or Dependent Vowel */
  14. const OT_SM = 8;
  15. const OT_VD = 9;
  16. const OT_A = 10;
  17. const OT_NBSP = 11;
  18. const OT_DOTTEDCIRCLE = 12; /* Not in the spec, but special in Uniscribe. /Very very/ special! */
  19. const OT_RS = 13; /* Register Shifter, used in Khmer OT spec */
  20. const OT_Coeng = 14;
  21. const OT_Repha = 15;
  22. const OT_Ra = 16; /* Not explicitly listed in the OT spec, but used in the grammar. */
  23. const OT_CM = 17;
  24. /* FROM hb-ot-shape-complex-myanmar.hh */
  25. // myanmar_category
  26. const OT_DB = 3; // same as INDIC::OT_N; /* Dot below */
  27. const OT_GB = 12; // same as INDIC::OT_DOTTEDCIRCLE;
  28. const OT_As = 18; /* Asat */
  29. const OT_D = 19; /* Digits except zero */
  30. const OT_D0 = 20; /* Digit zero */
  31. const OT_MH = 21; /* Various consonant medial types */
  32. const OT_MR = 22; /* Various consonant medial types */
  33. const OT_MW = 23; /* Various consonant medial types */
  34. const OT_MY = 24; /* Various consonant medial types */
  35. const OT_PT = 25; /* Pwo and other tones */
  36. const OT_VAbv = 26;
  37. const OT_VBlw = 27;
  38. const OT_VPre = 28;
  39. const OT_VPst = 29;
  40. const OT_VS = 30; /* Variation selectors */
  41. // Based on myanmar_category used to make string to find syllables
  42. // OT_ to string character (using e.g. OT_C from MYANMAR) hb-ot-shape-complex-myanmar-private.hh
  43. public static $myanmar_category_char = array(
  44. 'x',
  45. 'C',
  46. 'V',
  47. 'N',
  48. 'H',
  49. 'Z',
  50. 'J',
  51. 'x',
  52. 'S',
  53. 'x',
  54. 'A',
  55. 'x',
  56. 'D',
  57. 'x',
  58. 'x',
  59. 'x',
  60. 'R',
  61. 'x',
  62. 'a', /* As Asat */
  63. 'd', /* Digits except zero */
  64. 'o', /* Digit zero */
  65. 'k', /* Medial types */
  66. 'l', /* Medial types */
  67. 'm', /* Medial types */
  68. 'n', /* Medial types */
  69. 'p', /* Pwo and other tones */
  70. 'v', /* Vowel aboVe */
  71. 'b', /* Vowel Below */
  72. 'e', /* Vowel prE */
  73. 't', /* Vowel posT */
  74. 's', /* variation Selector */
  75. );
  76. /* Visual positions in a syllable from left to right. */
  77. /* FROM hb-ot-shape-complex-myanmar-private.hh */
  78. // myanmar_position
  79. const POS_START = 0;
  80. const POS_RA_TO_BECOME_REPH = 1;
  81. const POS_PRE_M = 2;
  82. const POS_PRE_C = 3;
  83. const POS_BASE_C = 4;
  84. const POS_AFTER_MAIN = 5;
  85. const POS_ABOVE_C = 6;
  86. const POS_BEFORE_SUB = 7;
  87. const POS_BELOW_C = 8;
  88. const POS_AFTER_SUB = 9;
  89. const POS_BEFORE_POST = 10;
  90. const POS_POST_C = 11;
  91. const POS_AFTER_POST = 12;
  92. const POS_FINAL_C = 13;
  93. const POS_SMVD = 14;
  94. const POS_END = 15;
  95. public static function set_myanmar_properties(&$info)
  96. {
  97. $u = $info['uni'];
  98. $type = self::myanmar_get_categories($u);
  99. $cat = ($type & 0x7F);
  100. $pos = ($type >> 8);
  101. /*
  102. * Re-assign category
  103. * http://www.microsoft.com/typography/OpenTypeDev/myanmar/intro.htm#analyze
  104. */
  105. if (self::in_range($u, 0xFE00, 0xFE0F))
  106. $cat = self::OT_VS;
  107. else if ($u == 0x200C)
  108. $cat = self::OT_ZWNJ;
  109. else if ($u == 0x200D)
  110. $cat = self::OT_ZWJ;
  111. switch ($u) {
  112. case 0x002D: case 0x00A0: case 0x00D7: case 0x2012:
  113. case 0x2013: case 0x2014: case 0x2015: case 0x2022:
  114. case 0x25CC: case 0x25FB: case 0x25FC: case 0x25FD:
  115. case 0x25FE:
  116. $cat = self::OT_GB;
  117. break;
  118. case 0x1004: case 0x101B: case 0x105A:
  119. $cat = self::OT_Ra;
  120. break;
  121. case 0x1032: case 0x1036:
  122. $cat = self::OT_A;
  123. break;
  124. case 0x103A:
  125. $cat = self::OT_As;
  126. break;
  127. case 0x1041: case 0x1042: case 0x1043: case 0x1044:
  128. case 0x1045: case 0x1046: case 0x1047: case 0x1048:
  129. case 0x1049: case 0x1090: case 0x1091: case 0x1092:
  130. case 0x1093: case 0x1094: case 0x1095: case 0x1096:
  131. case 0x1097: case 0x1098: case 0x1099:
  132. $cat = self::OT_D;
  133. break;
  134. case 0x1040:
  135. $cat = self::OT_D; /* XXX The spec says D0, but Uniscribe doesn't seem to do. */
  136. break;
  137. case 0x103E: case 0x1060:
  138. $cat = self::OT_MH;
  139. break;
  140. case 0x103C:
  141. $cat = self::OT_MR;
  142. break;
  143. case 0x103D: case 0x1082:
  144. $cat = self::OT_MW;
  145. break;
  146. case 0x103B: case 0x105E: case 0x105F:
  147. $cat = self::OT_MY;
  148. break;
  149. case 0x1063: case 0x1064: case 0x1069: case 0x106A:
  150. case 0x106B: case 0x106C: case 0x106D: case 0xAA7B:
  151. $cat = self::OT_PT;
  152. break;
  153. case 0x1038: case 0x1087: case 0x1088: case 0x1089:
  154. case 0x108A: case 0x108B: case 0x108C: case 0x108D:
  155. case 0x108F: case 0x109A: case 0x109B: case 0x109C:
  156. $cat = self::OT_SM;
  157. break;
  158. }
  159. if ($cat == self::OT_M) {
  160. switch ($pos) {
  161. case self::POS_PRE_C:
  162. $cat = self::OT_VPre;
  163. $pos = self::POS_PRE_M;
  164. break;
  165. case self::POS_ABOVE_C: $cat = self::OT_VAbv;
  166. break;
  167. case self::POS_BELOW_C: $cat = self::OT_VBlw;
  168. break;
  169. case self::POS_POST_C: $cat = self::OT_VPst;
  170. break;
  171. }
  172. }
  173. $info['myanmar_category'] = $cat;
  174. $info['myanmar_position'] = $pos;
  175. }
  176. // syllable_type
  177. const CONSONANT_SYLLABLE = 0;
  178. const BROKEN_CLUSTER = 3;
  179. const NON_MYANMAR_CLUSTER = 4;
  180. public static function set_syllables(&$o, $s, &$broken_syllables)
  181. {
  182. $ptr = 0;
  183. $syllable_serial = 1;
  184. $broken_syllables = false;
  185. while ($ptr < strlen($s)) {
  186. $match = '';
  187. $syllable_length = 1;
  188. $syllable_type = self::NON_MYANMAR_CLUSTER;
  189. // CONSONANT_SYLLABLE Consonant syllable
  190. // From OT spec:
  191. if (preg_match('/^(RaH)?([C|R]|V|d|D)[s]?(H([C|R|V])[s]?)*(H|[a]*[n]?[l]?((m[k]?|k)[a]?)?[e]*[v]*[b]*[A]*(N[a]?)?(t[k]?[a]*[v]*[A]*(N[a]?)?)*(p[A]*(N[a]?)?)*S*[J|Z]?)/', substr($s, $ptr), $ma)) {
  192. $syllable_length = strlen($ma[0]);
  193. $syllable_type = self::CONSONANT_SYLLABLE;
  194. }
  195. // BROKEN_CLUSTER syllable
  196. else if (preg_match('/^(RaH)?s?(H|[a]*[n]?[l]?((m[k]?|k)[a]?)?[e]*[v]*[b]*[A]*(N[a]?)?(t[k]?[a]*[v]*[A]*(N[a]?)?)*(p[A]*(N[a]?)?)*S*[J|Z]?)/', substr($s, $ptr), $ma)) {
  197. if (strlen($ma[0])) { // May match blank
  198. $syllable_length = strlen($ma[0]);
  199. $syllable_type = self::BROKEN_CLUSTER;
  200. $broken_syllables = true;
  201. }
  202. }
  203. for ($i = $ptr; $i < $ptr + $syllable_length; $i++) {
  204. $o[$i]['syllable'] = ($syllable_serial << 4) | $syllable_type;
  205. }
  206. $ptr += $syllable_length;
  207. $syllable_serial++;
  208. if ($syllable_serial == 16)
  209. $syllable_serial = 1;
  210. }
  211. }
  212. public static function reordering(&$info, $GSUBdata, $broken_syllables, $dottedcircle)
  213. {
  214. if ($broken_syllables && $dottedcircle) {
  215. self::insert_dotted_circles($info, $dottedcircle);
  216. }
  217. $count = count($info);
  218. if (!$count)
  219. return;
  220. $last = 0;
  221. $last_syllable = $info[0]['syllable'];
  222. for ($i = 1; $i < $count; $i++) {
  223. if ($last_syllable != $info[$i]['syllable']) {
  224. self::reordering_syllable($info, $GSUBdata, $last, $i);
  225. $last = $i;
  226. $last_syllable = $info[$last]['syllable'];
  227. }
  228. }
  229. self::reordering_syllable($info, $GSUBdata, $last, $count);
  230. }
  231. public static function insert_dotted_circles(&$info, $dottedcircle)
  232. {
  233. $idx = 0;
  234. $last_syllable = 0;
  235. while ($idx < count($info)) {
  236. $syllable = $info[$idx]['syllable'];
  237. $syllable_type = ($syllable & 0x0F);
  238. if ($last_syllable != $syllable && $syllable_type == self::BROKEN_CLUSTER) {
  239. $last_syllable = $syllable;
  240. $dottedcircle[0]['syllable'] = $info[$idx]['syllable'];
  241. array_splice($info, $idx, 0, $dottedcircle);
  242. } else
  243. $idx++;
  244. }
  245. // In case of final bloken cluster...
  246. $syllable = $info[$idx]['syllable'];
  247. $syllable_type = ($syllable & 0x0F);
  248. if ($last_syllable != $syllable && $syllable_type == self::BROKEN_CLUSTER) {
  249. $dottedcircle[0]['syllable'] = $info[$idx]['syllable'];
  250. array_splice($info, $idx, 0, $dottedcircle);
  251. }
  252. }
  253. /* Rules from:
  254. * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
  255. public static function reordering_syllable(&$info, $GSUBdata, $start, $end)
  256. {
  257. /* vowel_syllable: We made the vowels look like consonants. So uses the consonant logic! */
  258. /* broken_cluster: We already inserted dotted-circles, so just call the standalone_cluster. */
  259. $syllable_type = ($info[$start]['syllable'] & 0x0F);
  260. if ($syllable_type == self::NON_MYANMAR_CLUSTER) {
  261. return;
  262. }
  263. if ($syllable_type == self::BROKEN_CLUSTER) {
  264. //if ($uniscribe_bug_compatible) {
  265. /* For dotted-circle, this is what Uniscribe does:
  266. * If dotted-circle is the last glyph, it just does nothing.
  267. * i.e. It doesn't form Reph. */
  268. if ($info[$end - 1]['myanmar_category'] == self::OT_DOTTEDCIRCLE) {
  269. return;
  270. }
  271. }
  272. $base = $end;
  273. $has_reph = false;
  274. $limit = $start;
  275. if (($start + 3 <= $end) &&
  276. $info[$start]['myanmar_category'] == self::OT_Ra &&
  277. $info[$start + 1]['myanmar_category'] == self::OT_As &&
  278. $info[$start + 2]['myanmar_category'] == self::OT_H) {
  279. $limit += 3;
  280. $base = $start;
  281. $has_reph = true;
  282. }
  283. if (!$has_reph)
  284. $base = $limit;
  285. for ($i = $limit; $i < $end; $i++) {
  286. if (self::is_consonant($info[$i])) {
  287. $base = $i;
  288. break;
  289. }
  290. }
  291. /* Reorder! */
  292. $i = $start;
  293. for (; $i < $start + ($has_reph ? 3 : 0); $i++)
  294. $info[$i]['myanmar_position'] = self::POS_AFTER_MAIN;
  295. for (; $i < $base; $i++)
  296. $info[$i]['myanmar_position'] = self::POS_PRE_C;
  297. if ($i < $end) {
  298. $info[$i]['myanmar_position'] = self::POS_BASE_C;
  299. $i++;
  300. }
  301. $pos = self::POS_AFTER_MAIN;
  302. /* The following loop may be ugly, but it implements all of
  303. * Myanmar reordering! */
  304. for (; $i < $end; $i++) {
  305. if ($info[$i]['myanmar_category'] == self::OT_MR) /* Pre-base reordering */ {
  306. $info[$i]['myanmar_position'] = self::POS_PRE_C;
  307. continue;
  308. }
  309. if ($info[$i]['myanmar_position'] < self::POS_BASE_C) /* Left matra */ {
  310. continue;
  311. }
  312. if ($pos == self::POS_AFTER_MAIN && $info[$i]['myanmar_category'] == self::OT_VBlw) {
  313. $pos = self::POS_BELOW_C;
  314. $info[$i]['myanmar_position'] = $pos;
  315. continue;
  316. }
  317. if ($pos == self::POS_BELOW_C && $info[$i]['myanmar_category'] == self::OT_A) {
  318. $info[$i]['myanmar_position'] = self::POS_BEFORE_SUB;
  319. continue;
  320. }
  321. if ($pos == self::POS_BELOW_C && $info[$i]['myanmar_category'] == self::OT_VBlw) {
  322. $info[$i]['myanmar_position'] = $pos;
  323. continue;
  324. }
  325. if ($pos == self::POS_BELOW_C && $info[$i]['myanmar_category'] != self::OT_A) {
  326. $pos = self::POS_AFTER_SUB;
  327. $info[$i]['myanmar_position'] = $pos;
  328. continue;
  329. }
  330. $info[$i]['myanmar_position'] = $pos;
  331. }
  332. /* Sit tight, rock 'n roll! */
  333. self::bubble_sort($info, $start, $end - $start);
  334. }
  335. public static function is_one_of($info, $flags)
  336. {
  337. if (isset($info['is_ligature']) && $info['is_ligature'])
  338. return false; /* If it ligated, all bets are off. */
  339. return !!(self::FLAG($info['myanmar_category']) & $flags);
  340. }
  341. /* Vowels and placeholders treated as if they were consonants. */
  342. public static function is_consonant($info)
  343. {
  344. return self::is_one_of($info, (self::FLAG(self::OT_C) | self::FLAG(self::OT_CM) | self::FLAG(self::OT_Ra) | self::FLAG(self::OT_V) | self::FLAG(self::OT_NBSP) | self::FLAG(self::OT_GB)));
  345. }
  346. // From hb-private.hh
  347. public static function in_range($u, $lo, $hi)
  348. {
  349. if ((($lo ^ $hi) & $lo) == 0 && (($lo ^ $hi) & $hi) == ($lo ^ $hi) && (($lo ^ $hi) & (($lo ^ $hi) + 1)) == 0)
  350. return ($u & ~($lo ^ $hi)) == $lo;
  351. else
  352. return $lo <= $u && $u <= $hi;
  353. }
  354. // From hb-private.hh
  355. public static function FLAG($x)
  356. {
  357. return (1 << ($x));
  358. }
  359. public static function FLAG_RANGE($x, $y)
  360. {
  361. self::FLAG(y + 1) - self::FLAG(x);
  362. }
  363. // BELOW from hb-ot-shape-complex-indic.cc
  364. // see INDIC for details
  365. public static $myanmar_table = array(
  366. /* Myanmar (1000..109F) */
  367. /* 1000 */ 3841, 3841, 3841, 3841, 3841, 3841, 3841, 3841,
  368. /* 1008 */ 3841, 3841, 3841, 3841, 3841, 3841, 3841, 3841,
  369. /* 1010 */ 3841, 3841, 3841, 3841, 3841, 3841, 3841, 3841,
  370. /* 1018 */ 3841, 3841, 3841, 3841, 3841, 3841, 3841, 3841,
  371. /* 1020 */ 3841, 3842, 3842, 3842, 3842, 3842, 3842, 3842,
  372. /* 1028 */ 3842, 3842, 3842, 2823, 2823, 1543, 1543, 2055,
  373. /* 1030 */ 2055, 775, 1543, 1543, 1543, 1543, 3848, 3843,
  374. /* 1038 */ 3848, 3844, 1540, 3857, 3857, 3857, 3857, 3841,
  375. /* 1040 */ 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840,
  376. /* 1048 */ 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840,
  377. /* 1050 */ 3841, 3841, 3842, 3842, 3842, 3842, 2823, 2823,
  378. /* 1058 */ 2055, 2055, 3841, 3841, 3841, 3841, 3857, 3857,
  379. /* 1060 */ 3857, 3841, 2823, 3843, 3843, 3841, 3841, 2823,
  380. /* 1068 */ 2823, 3843, 3843, 3843, 3843, 3843, 3841, 3841,
  381. /* 1070 */ 3841, 1543, 1543, 1543, 1543, 3841, 3841, 3841,
  382. /* 1078 */ 3841, 3841, 3841, 3841, 3841, 3841, 3841, 3841,
  383. /* 1080 */ 3841, 3841, 3857, 2823, 775, 1543, 1543, 3843,
  384. /* 1088 */ 3843, 3843, 3843, 3843, 3843, 3843, 3841, 3843,
  385. /* 1090 */ 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840,
  386. /* 1098 */ 3840, 3840, 3843, 3843, 2823, 1543, 3840, 3840,
  387. /* Myanmar Extended-A (AA60..AA7F) */
  388. /* AA60 */ 3841, 3841, 3841, 3841, 3841, 3841, 3841, 3841,
  389. /* AA68 */ 3841, 3841, 3841, 3841, 3841, 3841, 3841, 3841,
  390. /* AA70 */ 3840, 3841, 3841, 3841, 3840, 3840, 3840, 3840,
  391. /* AA78 */ 3840, 3840, 3841, 3843, 3840, 3840, 3840, 3840,
  392. );
  393. // from "hb-ot-shape-complex-indic-table.cc"
  394. public static function myanmar_get_categories($u)
  395. {
  396. if (0x1000 <= $u && $u <= 0x109F)
  397. return self::$myanmar_table[$u - 0x1000 + 0]; // offset 0 for Most "myanmar"
  398. if (0xAA60 <= $u && $u <= 0xAA7F)
  399. return self::$myanmar_table[$u - 0xAA60 + 160]; // offset for extensions
  400. if ($u == 0x00A0)
  401. return 3851; // (ISC_CP | (IMC_x << 8))
  402. if ($u == 0x25CC)
  403. return 3851; // (ISC_CP | (IMC_x << 8))
  404. return 3840; // (ISC_x | (IMC_x << 8))
  405. }
  406. public static function bubble_sort(&$arr, $start, $len)
  407. {
  408. if ($len < 2) {
  409. return;
  410. }
  411. $k = $start + $len - 2;
  412. while ($k >= $start) {
  413. for ($j = $start; $j <= $k; $j++) {
  414. if ($arr[$j]['myanmar_position'] > $arr[$j + 1]['myanmar_position']) {
  415. $t = $arr[$j];
  416. $arr[$j] = $arr[$j + 1];
  417. $arr[$j + 1] = $t;
  418. }
  419. }
  420. $k--;
  421. }
  422. }
  423. }