PageRenderTime 71ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/azizhuss/arabicreshaper/ArabicShaping.java

https://github.com/zhaozw/DroidPersianCalendar
Java | 709 lines | 536 code | 108 blank | 65 comment | 92 complexity | c65ec260f0c6bf103cd8ef9c3b85c4ee MD5 | raw file
Possible License(s): GPL-3.0
  1. package com.azizhuss.arabicreshaper;
  2. // borrowed from code.google.com/p/arabicreshaper/, Apache License
  3. /**
  4. * This code is for Arabic Reshaping. Writtien by Abdulaziz Alhussien.
  5. * azizanroid@gmail.com
  6. * <p/>
  7. * This code is used in Mirsal, Ibrahim Keyboard, Arabic Contact, Arabic notepad
  8. * applications
  9. *
  10. * @author azizanroid@gmail.com
  11. * @author ebraminio (renaming and cleaning-up)
  12. */
  13. public class ArabicShaping {
  14. private static final char RIGHT_LEFT_CHAR = 0x0001;
  15. private static final char RIGHT_NOLEFT_CHAR_ALEF = 0x0006;
  16. private static final char RIGHT_NOLEFT_CHAR = 0x0004;
  17. private static final char RIGHT_LEFT_CHAR_LAM = 0x0003;
  18. private static final char TANWEEN = 0x000C;
  19. private static final char TASHKEEL = 0x000A;
  20. private static final char TATWEEL_CHAR = 0x0008;
  21. private static final char NORIGHT_NOLEFT_CHAR = 0x0007;
  22. private static final char NOTUSED_CHAR = 0x000F;
  23. private static final char NOTARABIC_CHAR = 0x0000;
  24. static final char RIGHT_LEFT_CHAR_MASK = 0x0880;
  25. private static final char RIGHT_NOLEFT_CHAR_MASK = 0x0800;
  26. private static final char LEFT_CHAR_MASK = 0x0080;
  27. private static final char allchar[][] = {
  28. { 0x0621, 0x0007, 0xFE80, 0xFE80, 0xFE80, 0xFE80 },
  29. { 0x0622, 0x0806, 0xFE81, 0xFE82, 0xFEF5, 0xFEF6 },
  30. { 0x0623, 0x0806, 0xFE83, 0xFE84, 0xFEF7, 0xFEF8 },
  31. { 0x0624, 0x0804, 0xFE85, 0xFE86, 0xFE86, 0xFE86 },
  32. { 0x0625, 0x0806, 0xFE87, 0xFE88, 0xFEF9, 0xFEFA },
  33. { 0x0626, 0x0881, 0xFE89, 0xFE8A, 0xFE8B, 0xFE8C },
  34. { 0x0627, 0x0806, 0xFE8D, 0xFE8E, 0xFEFB, 0xFEFC },
  35. { 0x0628, 0x0881, 0xFE8F, 0xFE90, 0xFE91, 0xFE92 },
  36. { 0x0629, 0x0804, 0xFE93, 0xFE94, 0xFE94, 0xFE94 },
  37. { 0x062A, 0x0881, 0xFE95, 0xFE96, 0xFE97, 0xFE98 },
  38. { 0x062B, 0x0881, 0xFE99, 0xFE9A, 0xFE9B, 0xFE9C },
  39. { 0x062C, 0x0881, 0xFE9D, 0xFE9E, 0xFE9F, 0xFEA0 },
  40. { 0x062D, 0x0881, 0xFEA1, 0xFEA2, 0xFEA3, 0xFEA4 },
  41. { 0x062E, 0x0881, 0xFEA5, 0xFEA6, 0xFEA7, 0xFEA8 },
  42. { 0x062F, 0x0804, 0xFEA9, 0xFEAA, 0xFEAA, 0xFEAA },
  43. { 0x0630, 0x0804, 0xFEAB, 0xFEAC, 0xFEAC, 0xFEAC },
  44. { 0x0631, 0x0804, 0xFEAD, 0xFEAE, 0xFEAE, 0xFEAE },
  45. { 0x0632, 0x0804, 0xFEAF, 0xFEB0, 0xFEB0, 0xFEB0 },
  46. { 0x0633, 0x0881, 0xFEB1, 0xFEB2, 0xFEB3, 0xFEB4 },
  47. { 0x0634, 0x0881, 0xFEB5, 0xFEB6, 0xFEB7, 0xFEB8 },
  48. { 0x0635, 0x0881, 0xFEB9, 0xFEBA, 0xFEBB, 0xFEBC },
  49. { 0x0636, 0x0881, 0xFEBD, 0xFEBE, 0xFEBF, 0xFEC0 },
  50. { 0x0637, 0x0881, 0xFEC1, 0xFEC2, 0xFEC3, 0xFEC4 },
  51. { 0x0638, 0x0881, 0xFEC5, 0xFEC6, 0xFEC7, 0xFEC8 },
  52. { 0x0639, 0x0881, 0xFEC9, 0xFECA, 0xFECB, 0xFECC },
  53. { 0x063A, 0x0881, 0xFECD, 0xFECE, 0xFECF, 0xFED0 },
  54. { 0x063B, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  55. { 0x063C, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  56. { 0x063D, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  57. { 0x063E, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  58. { 0x063F, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  59. { 0x0640, 0x0888, 0x0640, 0x0640, 0x0640, 0x0640 },
  60. { 0x0641, 0x0881, 0xFED1, 0xFED2, 0xFED3, 0xFED4 },
  61. { 0x0642, 0x0881, 0xFED5, 0xFED6, 0xFED7, 0xFED8 },
  62. { 0x0643, 0x0881, 0xFED9, 0xFEDA, 0xFEDB, 0xFEDC },
  63. { 0x0644, 0x0883, 0xFEDD, 0xFEDE, 0xFEDF, 0xFEE0 },
  64. { 0x0645, 0x0881, 0xFEE1, 0xFEE2, 0xFEE3, 0xFEE4 },
  65. { 0x0646, 0x0881, 0xFEE5, 0xFEE6, 0xFEE7, 0xFEE8 },
  66. { 0x0647, 0x0881, 0xFEE9, 0xFEEA, 0xFEEB, 0xFEEC },
  67. { 0x0648, 0x0804, 0xFEED, 0xFEEE, 0xFEEE, 0xFEEE },
  68. { 0x0649, 0x0804, 0xFEEF, 0xFEF0, 0xFEF0, 0xFEF0 },
  69. { 0x064A, 0x0881, 0xFEF1, 0xFEF2, 0xFEF3, 0xFEF4 },
  70. { 0x064B, 0x000C, 0x064B, 0xFE70, 0xFE71, 0xFE70 },
  71. { 0x064C, 0x000C, 0x064C, 0xFE72, 0xFE72, 0xFE72 },
  72. { 0x064D, 0x000C, 0x064D, 0xFE74, 0xFE74, 0xFE74 },
  73. { 0x064E, 0x000A, 0x064E, 0xFE76, 0xFE77, 0xFE76 },
  74. { 0x064F, 0x000A, 0x064F, 0xFE78, 0xFE79, 0xFE78 },
  75. { 0x0650, 0x000A, 0x0650, 0xFE7A, 0xFE7B, 0xFE7A },
  76. { 0x0651, 0x000A, 0x0651, 0xFE7C, 0xFE7D, 0xFE7C },
  77. { 0x0652, 0x000A, 0x0652, 0xFE7E, 0xFE7F, 0xFE7E },
  78. { 0x0653, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  79. { 0x0654, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  80. { 0x0655, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  81. { 0x0656, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  82. { 0x0657, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  83. { 0x0658, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  84. { 0x0659, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  85. { 0x065A, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  86. { 0x065B, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  87. { 0x065C, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  88. { 0x065D, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  89. { 0x065E, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  90. { 0x065F, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  91. { 0x0660, 0x000B, 0x0660, 0x0660, 0x0660, 0x0660 },
  92. { 0x0661, 0x000B, 0x0661, 0x0661, 0x0661, 0x0661 },
  93. { 0x0662, 0x000B, 0x0662, 0x0662, 0x0662, 0x0662 },
  94. { 0x0663, 0x000B, 0x0663, 0x0663, 0x0663, 0x0663 },
  95. { 0x0664, 0x000B, 0x0665, 0x0664, 0x0664, 0x0664 },
  96. { 0x0665, 0x000B, 0x0665, 0x0665, 0x0665, 0x0665 },
  97. { 0x0666, 0x000B, 0x0666, 0x0666, 0x0666, 0x0666 },
  98. { 0x0667, 0x000B, 0x0667, 0x0667, 0x0667, 0x0667 },
  99. { 0x0668, 0x000B, 0x0668, 0x0668, 0x0668, 0x0668 },
  100. { 0x0669, 0x000B, 0x0669, 0x0669, 0x0669, 0x0669 },
  101. { 0x066A, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  102. { 0x066B, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  103. { 0x066C, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  104. { 0x066D, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  105. { 0x066E, 0x000E, 0x065E, 0x065E, 0x065E, 0x065E },
  106. { 0x066F, 0x000E, 0x065F, 0x065F, 0x065F, 0x065F },
  107. { 0x0670, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  108. { 0x0671, 0x0804, 0xFB50, 0xFB51, 0xFB51, 0xFB51 },
  109. { 0x0672, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  110. { 0x0673, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  111. { 0x0674, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  112. { 0x0675, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  113. { 0x0676, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  114. { 0x0677, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  115. { 0x0678, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  116. { 0x0679, 0x0881, 0xFB66, 0xFB67, 0xFB68, 0xFB69 },
  117. { 0x067A, 0x0881, 0xFB5E, 0xFB5F, 0xFB60, 0xFB61 },
  118. { 0x067B, 0x0881, 0xFB52, 0xFB53, 0xFB54, 0xFB55 },
  119. { 0x067C, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  120. { 0x067D, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  121. { 0x067E, 0x0881, 0xFB56, 0xFB57, 0xFB58, 0xFB59 },
  122. { 0x067F, 0x0881, 0xFB62, 0xFB63, 0xFB64, 0xFB65 },
  123. { 0x0680, 0x0881, 0xFB5A, 0xFB5B, 0xFB5C, 0xFB5D },
  124. { 0x0681, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  125. { 0x0682, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  126. { 0x0683, 0x0881, 0xFB76, 0xFB77, 0xFB78, 0xFB79 },
  127. { 0x0684, 0x0881, 0xFB72, 0xFB73, 0xFB74, 0xFB75 },
  128. { 0x0685, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  129. { 0x0686, 0x0881, 0xFB7A, 0xFB7B, 0xFB7C, 0xFB7D },
  130. { 0x0687, 0x0881, 0xFB7E, 0xFB7F, 0xFB80, 0xFB81 },
  131. { 0x0688, 0x0804, 0xFB88, 0xFB89, 0xFB89, 0xFB89 },
  132. { 0x0689, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  133. { 0x068A, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  134. { 0x068B, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  135. { 0x068C, 0x0804, 0xFB84, 0xFB85, 0xFB85, 0xFB85 },
  136. { 0x068D, 0x0804, 0xFB82, 0xFB83, 0xFB83, 0xFB83 },
  137. { 0x068E, 0x0804, 0xFB86, 0xFB87, 0xFB83, 0xFB83 },
  138. { 0x068F, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  139. { 0x0690, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  140. { 0x0691, 0x0804, 0xFB8C, 0xFB8D, 0xFB8D, 0xFB8D },
  141. { 0x0692, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  142. { 0x0693, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  143. { 0x0694, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  144. { 0x0695, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  145. { 0x0696, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  146. { 0x0697, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  147. { 0x0698, 0x0804, 0xFB8A, 0xFB8B, 0xFB8B, 0xFB8B },
  148. { 0x0699, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  149. { 0x069A, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  150. { 0x069B, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  151. { 0x069C, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  152. { 0x069D, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  153. { 0x069E, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  154. { 0x069F, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  155. { 0x06A0, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  156. { 0x06A1, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  157. { 0x06A2, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  158. { 0x06A3, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  159. { 0x06A4, 0x0881, 0xFB6A, 0xFB6B, 0xFB6C, 0xFB6D },
  160. { 0x06A5, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  161. { 0x06A6, 0x0881, 0xFB6E, 0xFB6F, 0xFB70, 0xFB71 },
  162. { 0x06A7, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  163. { 0x06A8, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  164. { 0x06A9, 0x0881, 0xFB8E, 0xFB8F, 0xFB90, 0xFB91 },
  165. { 0x06AA, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  166. { 0x06AB, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  167. { 0x06AC, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  168. { 0x06AD, 0x0881, 0xFBD3, 0xFBD4, 0xFBD5, 0xFBD6 },
  169. { 0x06AE, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  170. { 0x06AF, 0x0881, 0xFB92, 0xFB93, 0xFB94, 0xFB95 },
  171. { 0x06B0, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  172. { 0x06B1, 0x0881, 0xFB9A, 0xFB9B, 0xFB9C, 0xFB9D },
  173. { 0x06B2, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  174. { 0x06B3, 0x0881, 0xFB96, 0xFB97, 0xFB98, 0xFB99 },
  175. { 0x06B4, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  176. { 0x06B5, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  177. { 0x06B6, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  178. { 0x06B7, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  179. { 0x06B8, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  180. { 0x06B9, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  181. { 0x06BA, 0x0804, 0xFB9E, 0xFB9F, 0xFB9F, 0xFB9F },
  182. { 0x06BB, 0x0881, 0xFBA0, 0xFBA1, 0xFBA2, 0xFBA3 },
  183. { 0x06BC, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  184. { 0x06BD, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  185. { 0x06BE, 0x0881, 0xFBAA, 0xFBAB, 0xFBAC, 0xFBAD },
  186. { 0x06BF, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  187. { 0x06C0, 0x0804, 0xFBA4, 0xFBA5, 0xFBA5, 0xFBA5 },
  188. { 0x06C1, 0x0881, 0xFBA6, 0xFBA7, 0xFBA8, 0xFBA9 },
  189. { 0x06C2, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  190. { 0x06C3, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  191. { 0x06C4, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  192. { 0x06C5, 0x0804, 0xFBE0, 0xFBE1, 0xFBE1, 0xFBE1 },
  193. { 0x06C6, 0x0804, 0xFBD9, 0xFBDA, 0xFBDA, 0xFBDA },
  194. { 0x06C7, 0x0804, 0xFBD7, 0xFBD8, 0xFBD8, 0xFBD8 },
  195. { 0x06C8, 0x0804, 0xFBDB, 0xFBDC, 0xFBDC, 0xFBDC },
  196. { 0x06C9, 0x0804, 0xFBE2, 0xFBE3, 0xFBE3, 0xFBE3 },
  197. { 0x06CA, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  198. { 0x06CB, 0x0804, 0xFBDE, 0xFBDF, 0xFBDF, 0xFBDF },
  199. { 0x06CC, 0x0881, 0xFBFC, 0xFBFD, 0xFBFE, 0xFBFF },
  200. { 0x06CD, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  201. { 0x06CE, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  202. { 0x06CF, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  203. { 0x06D0, 0x0881, 0xFBE4, 0xFBE5, 0xFBE6, 0xFBE7 },
  204. { 0x06D1, 0x000F, 0x0, 0x0, 0x0, 0x0 },
  205. { 0x06D2, 0x0804, 0xFBAE, 0xFBAF, 0xFBAF, 0xFBAF },
  206. { 0x06D3, 0x0804, 0xFBB0, 0xFBB1, 0xFBB1, 0xFBB1 } };
  207. public static String shape(String str) {
  208. String Temp = " " + str + " ";
  209. char pre, at, post;
  210. StringBuilder reshapedString = new StringBuilder();
  211. int i = 0;
  212. int len = str.length();
  213. char post_post;
  214. char pre_pre = ' ';
  215. while (i < len) {
  216. pre = Temp.charAt(i);
  217. at = Temp.charAt(i + 1);
  218. post = Temp.charAt(i + 2);
  219. int which_case = getCase(at);
  220. int what_case_post = getCase(post);
  221. int what_case_pre = getCase(pre);
  222. int what_case_post_post;
  223. // int what_case_pre_pre;
  224. // which_case=0x000F&
  225. // Log.v("what case"," :" +which_case);
  226. int pre_step = 0;
  227. if (what_case_pre == TASHKEEL) {
  228. pre = pre_pre;
  229. what_case_pre = getCase(pre);
  230. }
  231. if ((what_case_pre & LEFT_CHAR_MASK) == LEFT_CHAR_MASK) {
  232. pre_step = 1;
  233. }
  234. switch (which_case & 0x000F) {
  235. case NOTUSED_CHAR:
  236. case NOTARABIC_CHAR:
  237. reshapedString.append(at);
  238. i++;
  239. continue;
  240. case NORIGHT_NOLEFT_CHAR:
  241. case TATWEEL_CHAR:
  242. reshapedString.append(getShape(at, 0));
  243. i++;
  244. continue;
  245. case RIGHT_LEFT_CHAR_LAM:
  246. if ((what_case_post & 0x000F) == RIGHT_NOLEFT_CHAR_ALEF) {
  247. reshapedString.append(getShape(post, pre_step + 2));
  248. i = i + 2;
  249. continue;
  250. } else if ((what_case_post & RIGHT_NOLEFT_CHAR_MASK) == RIGHT_NOLEFT_CHAR_MASK) {
  251. reshapedString.append(getShape(at, 2 + pre_step));
  252. i = i + 1;
  253. continue;
  254. } else if (what_case_post == TANWEEN) {
  255. reshapedString.append(getShape(at, pre_step));
  256. i = i + 1;
  257. continue;
  258. } else if (what_case_post == TASHKEEL) {
  259. post_post = Temp.charAt(i + 3);
  260. what_case_post_post = getCase(post_post);
  261. if ((what_case_post_post & RIGHT_NOLEFT_CHAR_MASK) == RIGHT_NOLEFT_CHAR_MASK) {
  262. reshapedString.append(getShape(at, 2 + pre_step));
  263. i = i + 1;
  264. continue;
  265. } else {
  266. reshapedString.append(getShape(at, pre_step));
  267. i = i + 1;
  268. continue;
  269. }
  270. } else {
  271. reshapedString.append(getShape(at, pre_step));
  272. i = i + 1;
  273. continue;
  274. }
  275. case RIGHT_LEFT_CHAR:
  276. if ((what_case_post & RIGHT_NOLEFT_CHAR_MASK) == RIGHT_NOLEFT_CHAR_MASK) {
  277. reshapedString.append(getShape(at, 2 + pre_step));
  278. i = i + 1;
  279. continue;
  280. } else if (what_case_post == TANWEEN) {
  281. reshapedString.append(getShape(at, pre_step));
  282. i = i + 1;
  283. continue;
  284. } else if (what_case_post == TASHKEEL) {
  285. post_post = Temp.charAt(i + 3);
  286. what_case_post_post = getCase(post_post);
  287. if ((what_case_post_post & RIGHT_NOLEFT_CHAR_MASK) == RIGHT_NOLEFT_CHAR_MASK) {
  288. reshapedString.append(getShape(at, 2 + pre_step));
  289. i = i + 1;
  290. continue;
  291. } else {
  292. reshapedString.append(getShape(at, pre_step));
  293. i = i + 1;
  294. continue;
  295. }
  296. } else {
  297. reshapedString.append(getShape(at, pre_step));
  298. i = i + 1;
  299. continue;
  300. }
  301. case RIGHT_NOLEFT_CHAR_ALEF:
  302. case RIGHT_NOLEFT_CHAR:
  303. reshapedString.append(getShape(at, pre_step));
  304. i = i + 1;
  305. continue;
  306. case TASHKEEL:
  307. reshapedString.append(getShape(at, 0));
  308. i++;
  309. pre_pre = pre;
  310. continue;
  311. case TANWEEN:
  312. reshapedString.append(getShape(at, 0));
  313. i++;
  314. pre_pre = pre;
  315. continue;
  316. default:
  317. reshapedString.append(getShape(at, 0));
  318. i++;
  319. }
  320. }
  321. return reshapedString.toString();
  322. }
  323. public static String reshape_reverse(String Str) {
  324. String Temp = " " + Str + " ";
  325. char pre, at, post;
  326. StringBuilder reshapedString = new StringBuilder();
  327. int i = 0;
  328. int len = Str.length();
  329. char post_post;
  330. // char pre_pre = ' ';
  331. while (i < len) {
  332. pre = Temp.charAt(i + 2);
  333. at = Temp.charAt(i + 1);
  334. post = Temp.charAt(i);
  335. int which_case = getCase(at);
  336. int what_case_post = getCase(post);
  337. int what_case_pre = getCase(pre);
  338. int what_case_post_post;
  339. // int what_case_pre_pre;
  340. // which_case=0x000F&
  341. // Log.v("what case"," :" +which_case);
  342. int pre_step = 0;
  343. if (what_case_pre == TASHKEEL) {
  344. pre = Temp.charAt(i + 3);
  345. what_case_pre = getCase(pre);
  346. }
  347. if ((what_case_pre & LEFT_CHAR_MASK) == LEFT_CHAR_MASK) {
  348. pre_step = 1;
  349. }
  350. // System.out.println("##letter "+ pre);
  351. switch (which_case & 0x000F) {
  352. case NOTUSED_CHAR:
  353. case NOTARABIC_CHAR:
  354. reshapedString.append(at);
  355. i++;
  356. continue;
  357. case NORIGHT_NOLEFT_CHAR:
  358. case TATWEEL_CHAR:
  359. reshapedString.append(getShape(at, 0));
  360. i++;
  361. continue;
  362. case RIGHT_NOLEFT_CHAR_ALEF:
  363. // System.out.println("--letter "+ pre);
  364. if ((what_case_pre & 0x000F) == RIGHT_LEFT_CHAR_LAM) {
  365. pre = Temp.charAt(i + 3);
  366. // System.out.println("++letter "+ pre);
  367. what_case_pre = getCase(pre);
  368. pre_step = 0;
  369. if ((what_case_pre & LEFT_CHAR_MASK) == LEFT_CHAR_MASK) {
  370. pre_step = 1;
  371. }
  372. reshapedString.append(getShape(at, pre_step + 2));
  373. i = i + 2;
  374. continue;
  375. } /*
  376. * else if
  377. * ((what_case_post&RIGHT_NOLEFT_CHAR_MASK)==RIGHT_NOLEFT_CHAR_MASK
  378. * ){ reshapedString.append(getShape(at,2+pre_step)); i=i+1;
  379. *
  380. * continue;
  381. *
  382. *
  383. * } else if (what_case_post==TANWEEN){
  384. * reshapedString.append(getShape(at,pre_step)); i=i+1;
  385. * continue;
  386. *
  387. *
  388. * } else if (what_case_post==TASHKEEL){
  389. * post_post=Temp.charAt(i+3);
  390. * what_case_post_post=getCase(post_post); if
  391. * ((what_case_post_post
  392. * &RIGHT_NOLEFT_CHAR_MASK)==RIGHT_NOLEFT_CHAR_MASK){
  393. * reshapedString.append(getShape(at,2+pre_step)); i=i+1;
  394. *
  395. * continue;
  396. *
  397. * } else { reshapedString.append(getShape(at,pre_step)); i=i+1;
  398. * continue;
  399. *
  400. * }
  401. *
  402. *
  403. *
  404. *
  405. *
  406. * }
  407. */else {
  408. reshapedString.append(getShape(at, pre_step));
  409. i = i + 1;
  410. continue;
  411. }
  412. case RIGHT_LEFT_CHAR_LAM:
  413. case RIGHT_LEFT_CHAR:
  414. if ((what_case_post & RIGHT_NOLEFT_CHAR_MASK) == RIGHT_NOLEFT_CHAR_MASK) {
  415. reshapedString.append(getShape(at, 2 + pre_step));
  416. i = i + 1;
  417. continue;
  418. } else if (what_case_post == TANWEEN) {
  419. reshapedString.append(getShape(at, pre_step));
  420. i = i + 1;
  421. continue;
  422. } else if (what_case_post == TASHKEEL) {
  423. post_post = Temp.charAt(i + 3);
  424. what_case_post_post = getCase(post_post);
  425. if ((what_case_post_post & RIGHT_NOLEFT_CHAR_MASK) == RIGHT_NOLEFT_CHAR_MASK) {
  426. reshapedString.append(getShape(at, 2 + pre_step));
  427. i = i + 1;
  428. continue;
  429. } else {
  430. reshapedString.append(getShape(at, pre_step));
  431. i = i + 1;
  432. continue;
  433. }
  434. } else {
  435. reshapedString.append(getShape(at, pre_step));
  436. i = i + 1;
  437. continue;
  438. }
  439. case RIGHT_NOLEFT_CHAR:
  440. reshapedString.append(getShape(at, pre_step));
  441. i = i + 1;
  442. continue;
  443. case TASHKEEL:
  444. reshapedString.append(getShape(at, 0));
  445. i++;
  446. // pre_pre = pre;
  447. continue;
  448. case TANWEEN:
  449. reshapedString.append(getShape(at, 0));
  450. i++;
  451. // pre_pre = pre;
  452. continue;
  453. default:
  454. reshapedString.append(getShape(at, 0));
  455. i++;
  456. }
  457. }
  458. return reshapedString.toString();
  459. }
  460. public static String reshape_browser(String Str) {
  461. String Temp = " " + Str + " ";
  462. char pre, at, post;
  463. StringBuilder reshapedString = new StringBuilder();
  464. int i = 0;
  465. int len = Str.length();
  466. // boolean pre_can_connect = false;
  467. char post_post;
  468. char pre_pre = ' ';
  469. while (i < len) {
  470. pre = Temp.charAt(i);
  471. at = Temp.charAt(i + 1);
  472. post = Temp.charAt(i + 2);
  473. int which_case = getCase(at);
  474. int what_case_post = getCase(post);
  475. int what_case_pre = getCase(pre);
  476. int what_case_post_post;
  477. // int what_case_pre_pre;
  478. // which_case=0x000F&
  479. // Log.v("what case"," :" +which_case);
  480. if (at == '\u060c') {
  481. reshapedString.append(',');
  482. i++;
  483. continue;
  484. }
  485. // if (at==)
  486. int pre_step = 0;
  487. if (what_case_pre == TASHKEEL) {
  488. pre = pre_pre;
  489. what_case_pre = getCase(pre);
  490. }
  491. if ((what_case_pre & LEFT_CHAR_MASK) == LEFT_CHAR_MASK) {
  492. pre_step = 1;
  493. }
  494. switch (which_case & 0x000F) {
  495. case NOTUSED_CHAR:
  496. case NOTARABIC_CHAR:
  497. reshapedString.append(at);
  498. // pre_can_connect = false;
  499. i++;
  500. continue;
  501. case NORIGHT_NOLEFT_CHAR:
  502. reshapedString.append(getShape(at, 0));
  503. i++;
  504. continue;
  505. case TATWEEL_CHAR:
  506. reshapedString.append(getShape(at, 0));
  507. // pre_can_connect = false;
  508. i++;
  509. continue;
  510. case RIGHT_LEFT_CHAR_LAM:
  511. if ((what_case_post & 0x000F) == RIGHT_NOLEFT_CHAR_ALEF) {
  512. reshapedString.append(getShape(post, pre_step + 2));
  513. i = i + 2;
  514. // pre_can_connect = false;
  515. continue;
  516. } else if ((what_case_post & RIGHT_NOLEFT_CHAR_MASK) == RIGHT_NOLEFT_CHAR_MASK) {
  517. reshapedString.append(getShape(at, 2 + pre_step));
  518. i = i + 1;
  519. // pre_can_connect = true;
  520. continue;
  521. } else if (what_case_post == TANWEEN) {
  522. reshapedString.append(getShape(at, pre_step));
  523. i = i + 1;
  524. continue;
  525. } else if (what_case_post == TASHKEEL) {
  526. post_post = Temp.charAt(i + 2);
  527. what_case_post_post = getCase(post_post);
  528. if ((what_case_post_post & RIGHT_NOLEFT_CHAR_MASK) == RIGHT_NOLEFT_CHAR_MASK) {
  529. reshapedString.append(getShape(at, 2 + pre_step));
  530. i = i + 1;
  531. // pre_can_connect = true;
  532. continue;
  533. } else {
  534. reshapedString.append(getShape(at, pre_step));
  535. i = i + 1;
  536. continue;
  537. }
  538. } else {
  539. reshapedString.append(getShape(at, pre_step));
  540. i = i + 1;
  541. continue;
  542. }
  543. case RIGHT_LEFT_CHAR:
  544. if ((what_case_post & RIGHT_NOLEFT_CHAR_MASK) == RIGHT_NOLEFT_CHAR_MASK) {
  545. reshapedString.append(getShape(at, 2 + pre_step));
  546. i = i + 1;
  547. continue;
  548. } else if (what_case_post == TANWEEN) {
  549. reshapedString.append(getShape(at, pre_step));
  550. i = i + 1;
  551. continue;
  552. } else if (what_case_post == TASHKEEL) {
  553. post_post = Temp.charAt(i + 3);
  554. what_case_post_post = getCase(post_post);
  555. if ((what_case_post_post & RIGHT_NOLEFT_CHAR_MASK) == RIGHT_NOLEFT_CHAR_MASK) {
  556. reshapedString.append(getShape(at, 2 + pre_step));
  557. i = i + 1;
  558. // pre_can_connect = true;
  559. continue;
  560. } else {
  561. reshapedString.append(getShape(at, pre_step));
  562. i = i + 1;
  563. continue;
  564. }
  565. } else {
  566. reshapedString.append(getShape(at, pre_step));
  567. i = i + 1;
  568. continue;
  569. }
  570. case RIGHT_NOLEFT_CHAR_ALEF:
  571. case RIGHT_NOLEFT_CHAR:
  572. reshapedString.append(getShape(at, pre_step));
  573. i = i + 1;
  574. continue;
  575. case TASHKEEL:
  576. reshapedString.append(getShape(at, 0));
  577. i++;
  578. pre_pre = pre;
  579. continue;
  580. case TANWEEN:
  581. reshapedString.append(getShape(at, 0));
  582. i++;
  583. pre_pre = pre;
  584. continue;
  585. default:
  586. reshapedString.append(getShape(at, 0));
  587. i++;
  588. }
  589. }
  590. return reshapedString.toString();
  591. }
  592. private static int getCase(char ch) {
  593. if (ch < 0x0621 || ch > 0x06d2) {
  594. return 0;
  595. }
  596. return allchar[(int) ch - 0x0621][1];
  597. }
  598. private static char getShape(char ch, int which_shape) {
  599. return allchar[(int) ch - 0x0621][2 + which_shape];
  600. }
  601. }