ushape.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. *
  6. * Copyright (C) 2000-2016, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. * file name: ushape.cpp
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2000jun29
  16. * created by: Markus W. Scherer
  17. *
  18. * Arabic letter shaping implemented by Ayman Roshdy
  19. */
  20. #include "unicode/utypes.h"
  21. #include "unicode/uchar.h"
  22. #include "unicode/ustring.h"
  23. #include "unicode/ushape.h"
  24. #include "cmemory.h"
  25. #include "putilimp.h"
  26. #include "ustr_imp.h"
  27. #include "ubidi_props.h"
  28. #include "uassert.h"
  29. /*
  30. * This implementation is designed for 16-bit Unicode strings.
  31. * The main assumption is that the Arabic characters and their
  32. * presentation forms each fit into a single char16_t.
  33. * With UTF-8, they occupy 2 or 3 bytes, and more than the ASCII
  34. * characters.
  35. */
  36. /*
  37. * ### TODO in general for letter shaping:
  38. * - the letter shaping code is UTF-16-unaware; needs update
  39. * + especially invertBuffer()?!
  40. * - needs to handle the "Arabic Tail" that is used in some legacy codepages
  41. * as a glyph fragment of wide-glyph letters
  42. * + IBM Unicode conversion tables map it to U+200B (ZWSP)
  43. * + IBM Egypt has proposed to encode the tail in Unicode among Arabic Presentation Forms
  44. * + Unicode 3.2 added U+FE73 ARABIC TAIL FRAGMENT
  45. */
  46. /* definitions for Arabic letter shaping ------------------------------------ */
  47. #define IRRELEVANT 4
  48. #define LAMTYPE 16
  49. #define ALEFTYPE 32
  50. #define LINKR 1
  51. #define LINKL 2
  52. #define APRESENT 8
  53. #define SHADDA 64
  54. #define CSHADDA 128
  55. #define COMBINE (SHADDA+CSHADDA)
  56. #define HAMZAFE_CHAR 0xfe80
  57. #define HAMZA06_CHAR 0x0621
  58. #define YEH_HAMZA_CHAR 0x0626
  59. #define YEH_HAMZAFE_CHAR 0xFE89
  60. #define LAMALEF_SPACE_SUB 0xFFFF
  61. #define TASHKEEL_SPACE_SUB 0xFFFE
  62. #define NEW_TAIL_CHAR 0xFE73
  63. #define OLD_TAIL_CHAR 0x200B
  64. #define LAM_CHAR 0x0644
  65. #define SPACE_CHAR 0x0020
  66. #define SHADDA_CHAR 0xFE7C
  67. #define TATWEEL_CHAR 0x0640
  68. #define SHADDA_TATWEEL_CHAR 0xFE7D
  69. #define SHADDA06_CHAR 0x0651
  70. #define SHAPE_MODE 0
  71. #define DESHAPE_MODE 1
  72. struct uShapeVariables {
  73. char16_t tailChar;
  74. uint32_t uShapeLamalefBegin;
  75. uint32_t uShapeLamalefEnd;
  76. uint32_t uShapeTashkeelBegin;
  77. uint32_t uShapeTashkeelEnd;
  78. int spacesRelativeToTextBeginEnd;
  79. };
  80. static const uint8_t tailFamilyIsolatedFinal[] = {
  81. /* FEB1 */ 1,
  82. /* FEB2 */ 1,
  83. /* FEB3 */ 0,
  84. /* FEB4 */ 0,
  85. /* FEB5 */ 1,
  86. /* FEB6 */ 1,
  87. /* FEB7 */ 0,
  88. /* FEB8 */ 0,
  89. /* FEB9 */ 1,
  90. /* FEBA */ 1,
  91. /* FEBB */ 0,
  92. /* FEBC */ 0,
  93. /* FEBD */ 1,
  94. /* FEBE */ 1
  95. };
  96. static const uint8_t tashkeelMedial[] = {
  97. /* FE70 */ 0,
  98. /* FE71 */ 1,
  99. /* FE72 */ 0,
  100. /* FE73 */ 0,
  101. /* FE74 */ 0,
  102. /* FE75 */ 0,
  103. /* FE76 */ 0,
  104. /* FE77 */ 1,
  105. /* FE78 */ 0,
  106. /* FE79 */ 1,
  107. /* FE7A */ 0,
  108. /* FE7B */ 1,
  109. /* FE7C */ 0,
  110. /* FE7D */ 1,
  111. /* FE7E */ 0,
  112. /* FE7F */ 1
  113. };
  114. static const char16_t yehHamzaToYeh[] =
  115. {
  116. /* isolated*/ 0xFEEF,
  117. /* final */ 0xFEF0
  118. };
  119. static const uint8_t IrrelevantPos[] = {
  120. 0x0, 0x2, 0x4, 0x6,
  121. 0x8, 0xA, 0xC, 0xE
  122. };
  123. static const char16_t convertLamAlef[] =
  124. {
  125. /*FEF5*/ 0x0622,
  126. /*FEF6*/ 0x0622,
  127. /*FEF7*/ 0x0623,
  128. /*FEF8*/ 0x0623,
  129. /*FEF9*/ 0x0625,
  130. /*FEFA*/ 0x0625,
  131. /*FEFB*/ 0x0627,
  132. /*FEFC*/ 0x0627
  133. };
  134. static const char16_t araLink[178]=
  135. {
  136. 1 + 32 + 256 * 0x11,/*0x0622*/
  137. 1 + 32 + 256 * 0x13,/*0x0623*/
  138. 1 + 256 * 0x15,/*0x0624*/
  139. 1 + 32 + 256 * 0x17,/*0x0625*/
  140. 1 + 2 + 256 * 0x19,/*0x0626*/
  141. 1 + 32 + 256 * 0x1D,/*0x0627*/
  142. 1 + 2 + 256 * 0x1F,/*0x0628*/
  143. 1 + 256 * 0x23,/*0x0629*/
  144. 1 + 2 + 256 * 0x25,/*0x062A*/
  145. 1 + 2 + 256 * 0x29,/*0x062B*/
  146. 1 + 2 + 256 * 0x2D,/*0x062C*/
  147. 1 + 2 + 256 * 0x31,/*0x062D*/
  148. 1 + 2 + 256 * 0x35,/*0x062E*/
  149. 1 + 256 * 0x39,/*0x062F*/
  150. 1 + 256 * 0x3B,/*0x0630*/
  151. 1 + 256 * 0x3D,/*0x0631*/
  152. 1 + 256 * 0x3F,/*0x0632*/
  153. 1 + 2 + 256 * 0x41,/*0x0633*/
  154. 1 + 2 + 256 * 0x45,/*0x0634*/
  155. 1 + 2 + 256 * 0x49,/*0x0635*/
  156. 1 + 2 + 256 * 0x4D,/*0x0636*/
  157. 1 + 2 + 256 * 0x51,/*0x0637*/
  158. 1 + 2 + 256 * 0x55,/*0x0638*/
  159. 1 + 2 + 256 * 0x59,/*0x0639*/
  160. 1 + 2 + 256 * 0x5D,/*0x063A*/
  161. 0, 0, 0, 0, 0, /*0x063B-0x063F*/
  162. 1 + 2, /*0x0640*/
  163. 1 + 2 + 256 * 0x61,/*0x0641*/
  164. 1 + 2 + 256 * 0x65,/*0x0642*/
  165. 1 + 2 + 256 * 0x69,/*0x0643*/
  166. 1 + 2 + 16 + 256 * 0x6D,/*0x0644*/
  167. 1 + 2 + 256 * 0x71,/*0x0645*/
  168. 1 + 2 + 256 * 0x75,/*0x0646*/
  169. 1 + 2 + 256 * 0x79,/*0x0647*/
  170. 1 + 256 * 0x7D,/*0x0648*/
  171. 1 + 256 * 0x7F,/*0x0649*/
  172. 1 + 2 + 256 * 0x81,/*0x064A*/
  173. 4 + 256 * 1, /*0x064B*/
  174. 4 + 128 + 256 * 1, /*0x064C*/
  175. 4 + 128 + 256 * 1, /*0x064D*/
  176. 4 + 128 + 256 * 1, /*0x064E*/
  177. 4 + 128 + 256 * 1, /*0x064F*/
  178. 4 + 128 + 256 * 1, /*0x0650*/
  179. 4 + 64 + 256 * 3, /*0x0651*/
  180. 4 + 256 * 1, /*0x0652*/
  181. 4 + 256 * 7, /*0x0653*/
  182. 4 + 256 * 8, /*0x0654*/
  183. 4 + 256 * 8, /*0x0655*/
  184. 4 + 256 * 1, /*0x0656*/
  185. 0, 0, 0, 0, 0, /*0x0657-0x065B*/
  186. 1 + 256 * 0x85,/*0x065C*/
  187. 1 + 256 * 0x87,/*0x065D*/
  188. 1 + 256 * 0x89,/*0x065E*/
  189. 1 + 256 * 0x8B,/*0x065F*/
  190. 0, 0, 0, 0, 0, /*0x0660-0x0664*/
  191. 0, 0, 0, 0, 0, /*0x0665-0x0669*/
  192. 0, 0, 0, 0, 0, 0, /*0x066A-0x066F*/
  193. 4 + 256 * 6, /*0x0670*/
  194. 1 + 8 + 256 * 0x00,/*0x0671*/
  195. 1 + 32, /*0x0672*/
  196. 1 + 32, /*0x0673*/
  197. 0, /*0x0674*/
  198. 1 + 32, /*0x0675*/
  199. 1, 1, /*0x0676-0x0677*/
  200. 1 + 2, /*0x0678*/
  201. 1 + 2 + 8 + 256 * 0x16,/*0x0679*/
  202. 1 + 2 + 8 + 256 * 0x0E,/*0x067A*/
  203. 1 + 2 + 8 + 256 * 0x02,/*0x067B*/
  204. 1+2, 1+2, /*0x67C-0x067D*/
  205. 1+2+8+256 * 0x06, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x067E-0x0683*/
  206. 1+2, 1+2, 1+2+8+256 * 0x2A, 1+2, /*0x0684-0x0687*/
  207. 1 + 8 + 256 * 0x38,/*0x0688*/
  208. 1, 1, 1, /*0x0689-0x068B*/
  209. 1 + 8 + 256 * 0x34,/*0x068C*/
  210. 1 + 8 + 256 * 0x32,/*0x068D*/
  211. 1 + 8 + 256 * 0x36,/*0x068E*/
  212. 1, 1, /*0x068F-0x0690*/
  213. 1 + 8 + 256 * 0x3C,/*0x0691*/
  214. 1, 1, 1, 1, 1, 1, 1+8+256 * 0x3A, 1, /*0x0692-0x0699*/
  215. 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/
  216. 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/
  217. 1+2, 1+2, 1+2, 1+2, 1+2, 1+2+8+256 * 0x3E, /*0x06A4-0x06AD*/
  218. 1+2, 1+2, 1+2, 1+2, /*0x06A4-0x06AD*/
  219. 1+2, 1+2+8+256 * 0x42, 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/
  220. 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/
  221. 1+2, 1+2, /*0x06B8-0x06B9*/
  222. 1 + 8 + 256 * 0x4E,/*0x06BA*/
  223. 1 + 2 + 8 + 256 * 0x50,/*0x06BB*/
  224. 1+2, 1+2, /*0x06BC-0x06BD*/
  225. 1 + 2 + 8 + 256 * 0x5A,/*0x06BE*/
  226. 1+2, /*0x06BF*/
  227. 1 + 8 + 256 * 0x54,/*0x06C0*/
  228. 1 + 2 + 8 + 256 * 0x56,/*0x06C1*/
  229. 1, 1, 1, /*0x06C2-0x06C4*/
  230. 1 + 8 + 256 * 0x90,/*0x06C5*/
  231. 1 + 8 + 256 * 0x89,/*0x06C6*/
  232. 1 + 8 + 256 * 0x87,/*0x06C7*/
  233. 1 + 8 + 256 * 0x8B,/*0x06C8*/
  234. 1 + 8 + 256 * 0x92,/*0x06C9*/
  235. 1, /*0x06CA*/
  236. 1 + 8 + 256 * 0x8E,/*0x06CB*/
  237. 1 + 2 + 8 + 256 * 0xAC,/*0x06CC*/
  238. 1, /*0x06CD*/
  239. 1+2, 1+2, /*0x06CE-0x06CF*/
  240. 1 + 2 + 8 + 256 * 0x94,/*0x06D0*/
  241. 1+2, /*0x06D1*/
  242. 1 + 8 + 256 * 0x5E,/*0x06D2*/
  243. 1 + 8 + 256 * 0x60 /*0x06D3*/
  244. };
  245. static const uint8_t presALink[] = {
  246. /***********0*****1*****2*****3*****4*****5*****6*****7*****8*****9*****A*****B*****C*****D*****E*****F*/
  247. /*FB5*/ 0, 1, 0, 0, 0, 0, 0, 1, 2,1 + 2, 0, 0, 0, 0, 0, 0,
  248. /*FB6*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  249. /*FB7*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,1 + 2, 0, 0,
  250. /*FB8*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1,
  251. /*FB9*/ 2,1 + 2, 0, 1, 2,1 + 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  252. /*FBA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  253. /*FBB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  254. /*FBC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  255. /*FBD*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  256. /*FBE*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  257. /*FBF*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,1 + 2,
  258. /*FC0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  259. /*FC1*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  260. /*FC2*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  261. /*FC3*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  262. /*FC4*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  263. /*FC5*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4,
  264. /*FC6*/ 4, 4, 4
  265. };
  266. static const uint8_t presBLink[]=
  267. {
  268. /***********0*****1*****2*****3*****4*****5*****6*****7*****8*****9*****A*****B*****C*****D*****E*****F*/
  269. /*FE7*/1 + 2,1 + 2,1 + 2, 0,1 + 2, 0,1 + 2,1 + 2,1 + 2,1 + 2,1 + 2,1 + 2,1 + 2,1 + 2,1 + 2,1 + 2,
  270. /*FE8*/ 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2,1 + 2, 0, 1, 0,
  271. /*FE9*/ 1, 2,1 + 2, 0, 1, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
  272. /*FEA*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 0, 1, 0, 1, 0,
  273. /*FEB*/ 1, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
  274. /*FEC*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
  275. /*FED*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
  276. /*FEE*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 0,
  277. /*FEF*/ 1, 0, 1, 2,1 + 2, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0
  278. };
  279. static const char16_t convertFBto06[] =
  280. {
  281. /***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/
  282. /*FB5*/ 0x671, 0x671, 0x67B, 0x67B, 0x67B, 0x67B, 0x67E, 0x67E, 0x67E, 0x67E, 0, 0, 0, 0, 0x67A, 0x67A,
  283. /*FB6*/ 0x67A, 0x67A, 0, 0, 0, 0, 0x679, 0x679, 0x679, 0x679, 0, 0, 0, 0, 0, 0,
  284. /*FB7*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x686, 0x686, 0x686, 0x686, 0, 0,
  285. /*FB8*/ 0, 0, 0x68D, 0x68D, 0x68C, 0x68C, 0x68E, 0x68E, 0x688, 0x688, 0x698, 0x698, 0x691, 0x691, 0x6A9, 0x6A9,
  286. /*FB9*/ 0x6A9, 0x6A9, 0x6AF, 0x6AF, 0x6AF, 0x6AF, 0, 0, 0, 0, 0, 0, 0, 0, 0x6BA, 0x6BA,
  287. /*FBA*/ 0x6BB, 0x6BB, 0x6BB, 0x6BB, 0x6C0, 0x6C0, 0x6C1, 0x6C1, 0x6C1, 0x6C1, 0x6BE, 0x6BE, 0x6BE, 0x6BE, 0x6d2, 0x6D2,
  288. /*FBB*/ 0x6D3, 0x6D3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  289. /*FBC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  290. /*FBD*/ 0, 0, 0, 0, 0, 0, 0, 0x6C7, 0x6C7, 0x6C6, 0x6C6, 0x6C8, 0x6C8, 0, 0x6CB, 0x6CB,
  291. /*FBE*/ 0x6C5, 0x6C5, 0x6C9, 0x6C9, 0x6D0, 0x6D0, 0x6D0, 0x6D0, 0, 0, 0, 0, 0, 0, 0, 0,
  292. /*FBF*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x6CC, 0x6CC, 0x6CC, 0x6CC
  293. };
  294. static const char16_t convertFEto06[] =
  295. {
  296. /***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/
  297. /*FE7*/ 0x64B, 0x64B, 0x64C, 0x64C, 0x64D, 0x64D, 0x64E, 0x64E, 0x64F, 0x64F, 0x650, 0x650, 0x651, 0x651, 0x652, 0x652,
  298. /*FE8*/ 0x621, 0x622, 0x622, 0x623, 0x623, 0x624, 0x624, 0x625, 0x625, 0x626, 0x626, 0x626, 0x626, 0x627, 0x627, 0x628,
  299. /*FE9*/ 0x628, 0x628, 0x628, 0x629, 0x629, 0x62A, 0x62A, 0x62A, 0x62A, 0x62B, 0x62B, 0x62B, 0x62B, 0x62C, 0x62C, 0x62C,
  300. /*FEA*/ 0x62C, 0x62D, 0x62D, 0x62D, 0x62D, 0x62E, 0x62E, 0x62E, 0x62E, 0x62F, 0x62F, 0x630, 0x630, 0x631, 0x631, 0x632,
  301. /*FEB*/ 0x632, 0x633, 0x633, 0x633, 0x633, 0x634, 0x634, 0x634, 0x634, 0x635, 0x635, 0x635, 0x635, 0x636, 0x636, 0x636,
  302. /*FEC*/ 0x636, 0x637, 0x637, 0x637, 0x637, 0x638, 0x638, 0x638, 0x638, 0x639, 0x639, 0x639, 0x639, 0x63A, 0x63A, 0x63A,
  303. /*FED*/ 0x63A, 0x641, 0x641, 0x641, 0x641, 0x642, 0x642, 0x642, 0x642, 0x643, 0x643, 0x643, 0x643, 0x644, 0x644, 0x644,
  304. /*FEE*/ 0x644, 0x645, 0x645, 0x645, 0x645, 0x646, 0x646, 0x646, 0x646, 0x647, 0x647, 0x647, 0x647, 0x648, 0x648, 0x649,
  305. /*FEF*/ 0x649, 0x64A, 0x64A, 0x64A, 0x64A, 0x65C, 0x65C, 0x65D, 0x65D, 0x65E, 0x65E, 0x65F, 0x65F
  306. };
  307. static const uint8_t shapeTable[4][4][4]=
  308. {
  309. { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,1} },
  310. { {0,0,2,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} },
  311. { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,3} },
  312. { {0,0,1,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} }
  313. };
  314. /*
  315. * This function shapes European digits to Arabic-Indic digits
  316. * in-place, writing over the input characters.
  317. * Since we know that we are only looking for BMP code points,
  318. * we can safely just work with code units (again, at least UTF-16).
  319. */
  320. static void
  321. _shapeToArabicDigitsWithContext(char16_t *s, int32_t length,
  322. char16_t digitBase,
  323. UBool isLogical, UBool lastStrongWasAL) {
  324. int32_t i;
  325. char16_t c;
  326. digitBase-=0x30;
  327. /* the iteration direction depends on the type of input */
  328. if(isLogical) {
  329. for(i=0; i<length; ++i) {
  330. c=s[i];
  331. switch(ubidi_getClass(c)) {
  332. case U_LEFT_TO_RIGHT: /* L */
  333. case U_RIGHT_TO_LEFT: /* R */
  334. lastStrongWasAL=false;
  335. break;
  336. case U_RIGHT_TO_LEFT_ARABIC: /* AL */
  337. lastStrongWasAL=true;
  338. break;
  339. case U_EUROPEAN_NUMBER: /* EN */
  340. if (lastStrongWasAL && static_cast<uint32_t>(c - 0x30) < 10) {
  341. s[i] = static_cast<char16_t>(digitBase + c); /* digitBase+(c-0x30) - digitBase was modified above */
  342. }
  343. break;
  344. default :
  345. break;
  346. }
  347. }
  348. } else {
  349. for(i=length; i>0; /* pre-decrement in the body */) {
  350. c=s[--i];
  351. switch(ubidi_getClass(c)) {
  352. case U_LEFT_TO_RIGHT: /* L */
  353. case U_RIGHT_TO_LEFT: /* R */
  354. lastStrongWasAL=false;
  355. break;
  356. case U_RIGHT_TO_LEFT_ARABIC: /* AL */
  357. lastStrongWasAL=true;
  358. break;
  359. case U_EUROPEAN_NUMBER: /* EN */
  360. if (lastStrongWasAL && static_cast<uint32_t>(c - 0x30) < 10) {
  361. s[i] = static_cast<char16_t>(digitBase + c); /* digitBase+(c-0x30) - digitBase was modified above */
  362. }
  363. break;
  364. default :
  365. break;
  366. }
  367. }
  368. }
  369. }
  370. /*
  371. *Name : invertBuffer
  372. *Function : This function inverts the buffer, it's used
  373. * in case the user specifies the buffer to be
  374. * U_SHAPE_TEXT_DIRECTION_LOGICAL
  375. */
  376. static void
  377. invertBuffer(char16_t *buffer, int32_t size, uint32_t /*options*/, int32_t lowlimit, int32_t highlimit) {
  378. char16_t temp;
  379. int32_t i=0,j=0;
  380. for(i=lowlimit,j=size-highlimit-1;i<j;i++,j--) {
  381. temp = buffer[i];
  382. buffer[i] = buffer[j];
  383. buffer[j] = temp;
  384. }
  385. }
  386. /*
  387. *Name : changeLamAlef
  388. *Function : Converts the Alef characters into an equivalent
  389. * LamAlef location in the 0x06xx Range, this is an
  390. * intermediate stage in the operation of the program
  391. * later it'll be converted into the 0xFExx LamAlefs
  392. * in the shaping function.
  393. */
  394. static inline char16_t
  395. changeLamAlef(char16_t ch) {
  396. switch(ch) {
  397. case 0x0622 :
  398. return 0x065C;
  399. case 0x0623 :
  400. return 0x065D;
  401. case 0x0625 :
  402. return 0x065E;
  403. case 0x0627 :
  404. return 0x065F;
  405. }
  406. return 0;
  407. }
  408. /*
  409. *Name : getLink
  410. *Function : Resolves the link between the characters as
  411. * Arabic characters have four forms :
  412. * Isolated, Initial, Middle and Final Form
  413. */
  414. static char16_t
  415. getLink(char16_t ch) {
  416. if(ch >= 0x0622 && ch <= 0x06D3) {
  417. return(araLink[ch-0x0622]);
  418. } else if(ch == 0x200D) {
  419. return(3);
  420. } else if(ch >= 0x206D && ch <= 0x206F) {
  421. return(4);
  422. }else if(ch >= 0xFB50 && ch <= 0xFC62) {
  423. return(presALink[ch-0xFB50]);
  424. } else if(ch >= 0xFE70 && ch <= 0xFEFC) {
  425. return(presBLink[ch-0xFE70]);
  426. }else {
  427. return(0);
  428. }
  429. }
  430. /*
  431. *Name : countSpaces
  432. *Function : Counts the number of spaces
  433. * at each end of the logical buffer
  434. */
  435. static void
  436. countSpaces(char16_t *dest, int32_t size, uint32_t /*options*/, int32_t *spacesCountl, int32_t *spacesCountr) {
  437. int32_t i = 0;
  438. int32_t countl = 0,countr = 0;
  439. while((dest[i] == SPACE_CHAR) && (countl < size)) {
  440. countl++;
  441. i++;
  442. }
  443. if (countl < size) { /* the entire buffer is not all space */
  444. while(dest[size-1] == SPACE_CHAR) {
  445. countr++;
  446. size--;
  447. }
  448. }
  449. *spacesCountl = countl;
  450. *spacesCountr = countr;
  451. }
  452. /*
  453. *Name : isTashkeelChar
  454. *Function : Returns 1 for Tashkeel characters in 06 range else return 0
  455. */
  456. static inline int32_t
  457. isTashkeelChar(char16_t ch) {
  458. return static_cast<int32_t>(ch >= 0x064B && ch <= 0x0652);
  459. }
  460. /*
  461. *Name : isTashkeelCharFE
  462. *Function : Returns 1 for Tashkeel characters in FE range else return 0
  463. */
  464. static inline int32_t
  465. isTashkeelCharFE(char16_t ch) {
  466. return static_cast<int32_t>(ch >= 0xFE70 && ch <= 0xFE7F);
  467. }
  468. /*
  469. *Name : isAlefChar
  470. *Function : Returns 1 for Alef characters else return 0
  471. */
  472. static inline int32_t
  473. isAlefChar(char16_t ch) {
  474. return static_cast<int32_t>(ch == 0x0622 || ch == 0x0623 || ch == 0x0625 || ch == 0x0627);
  475. }
  476. /*
  477. *Name : isLamAlefChar
  478. *Function : Returns 1 for LamAlef characters else return 0
  479. */
  480. static inline int32_t
  481. isLamAlefChar(char16_t ch) {
  482. return static_cast<int32_t>(ch >= 0xFEF5 && ch <= 0xFEFC);
  483. }
  484. /*BIDI
  485. *Name : isTailChar
  486. *Function : returns 1 if the character matches one of the tail characters (0xfe73 or 0x200b) otherwise returns 0
  487. */
  488. static inline int32_t
  489. isTailChar(char16_t ch) {
  490. if(ch == OLD_TAIL_CHAR || ch == NEW_TAIL_CHAR){
  491. return 1;
  492. }else{
  493. return 0;
  494. }
  495. }
  496. /*BIDI
  497. *Name : isSeenTailFamilyChar
  498. *Function : returns 1 if the character is a seen family isolated character
  499. * in the FE range otherwise returns 0
  500. */
  501. static inline int32_t
  502. isSeenTailFamilyChar(char16_t ch) {
  503. if(ch >= 0xfeb1 && ch < 0xfebf){
  504. return tailFamilyIsolatedFinal [ch - 0xFEB1];
  505. }else{
  506. return 0;
  507. }
  508. }
  509. /* Name : isSeenFamilyChar
  510. * Function : returns 1 if the character is a seen family character in the Unicode
  511. * 06 range otherwise returns 0
  512. */
  513. static inline int32_t
  514. isSeenFamilyChar(char16_t ch){
  515. if(ch >= 0x633 && ch <= 0x636){
  516. return 1;
  517. }else {
  518. return 0;
  519. }
  520. }
  521. /*Start of BIDI*/
  522. /*
  523. *Name : isAlefMaksouraChar
  524. *Function : returns 1 if the character is a Alef Maksoura Final or isolated
  525. * otherwise returns 0
  526. */
  527. static inline int32_t
  528. isAlefMaksouraChar(char16_t ch) {
  529. return static_cast<int32_t>(ch == 0xFEEF || ch == 0xFEF0 || ch == 0x0649);
  530. }
  531. /*
  532. * Name : isYehHamzaChar
  533. * Function : returns 1 if the character is a yehHamza isolated or yehhamza
  534. * final is found otherwise returns 0
  535. */
  536. static inline int32_t
  537. isYehHamzaChar(char16_t ch) {
  538. if((ch==0xFE89)||(ch==0xFE8A)){
  539. return 1;
  540. }else{
  541. return 0;
  542. }
  543. }
  544. /*
  545. * Name: isTashkeelOnTatweelChar
  546. * Function: Checks if the Tashkeel Character is on Tatweel or not,if the
  547. * Tashkeel on tatweel (FE range), it returns 1 else if the
  548. * Tashkeel with shadda on tatweel (FC range)return 2 otherwise
  549. * returns 0
  550. */
  551. static inline int32_t
  552. isTashkeelOnTatweelChar(char16_t ch){
  553. if(ch >= 0xfe70 && ch <= 0xfe7f && ch != NEW_TAIL_CHAR && ch != 0xFE75 && ch != SHADDA_TATWEEL_CHAR)
  554. {
  555. return tashkeelMedial [ch - 0xFE70];
  556. }else if( (ch >= 0xfcf2 && ch <= 0xfcf4) || (ch == SHADDA_TATWEEL_CHAR)) {
  557. return 2;
  558. }else{
  559. return 0;
  560. }
  561. }
  562. /*
  563. * Name: isIsolatedTashkeelChar
  564. * Function: Checks if the Tashkeel Character is in the isolated form
  565. * (i.e. Unicode FE range) returns 1 else if the Tashkeel
  566. * with shadda is in the isolated form (i.e. Unicode FC range)
  567. * returns 2 otherwise returns 0
  568. */
  569. static inline int32_t
  570. isIsolatedTashkeelChar(char16_t ch){
  571. if(ch >= 0xfe70 && ch <= 0xfe7f && ch != NEW_TAIL_CHAR && ch != 0xFE75){
  572. return (1 - tashkeelMedial [ch - 0xFE70]);
  573. }else if(ch >= 0xfc5e && ch <= 0xfc63){
  574. return 1;
  575. }else{
  576. return 0;
  577. }
  578. }
  579. /*
  580. *Name : calculateSize
  581. *Function : This function calculates the destSize to be used in preflighting
  582. * when the destSize is equal to 0
  583. * It is used also to calculate the new destsize in case the
  584. * destination buffer will be resized.
  585. */
  586. static int32_t
  587. calculateSize(const char16_t *source, int32_t sourceLength,
  588. int32_t destSize,uint32_t options) {
  589. int32_t i = 0;
  590. int lamAlefOption = 0;
  591. int tashkeelOption = 0;
  592. destSize = sourceLength;
  593. if (((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_SHAPE ||
  594. ((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED )) &&
  595. ((options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE )){
  596. lamAlefOption = 1;
  597. }
  598. if((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_SHAPE &&
  599. ((options&U_SHAPE_TASHKEEL_MASK) == U_SHAPE_TASHKEEL_RESIZE ) ){
  600. tashkeelOption = 1;
  601. }
  602. if(lamAlefOption || tashkeelOption){
  603. if((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_VISUAL_LTR) {
  604. for(i=0;i<sourceLength;i++) {
  605. if( ((isAlefChar(source[i]))&& (i<(sourceLength-1)) &&(source[i+1] == LAM_CHAR)) || (isTashkeelCharFE(source[i])) ) {
  606. destSize--;
  607. }
  608. }
  609. }else if((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL) {
  610. for(i=0;i<sourceLength;i++) {
  611. if( ( (source[i] == LAM_CHAR) && (i<(sourceLength-1)) && (isAlefChar(source[i+1]))) || (isTashkeelCharFE(source[i])) ) {
  612. destSize--;
  613. }
  614. }
  615. }
  616. }
  617. if ((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_UNSHAPE){
  618. if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE){
  619. for(i=0;i<sourceLength;i++) {
  620. if(isLamAlefChar(source[i]))
  621. destSize++;
  622. }
  623. }
  624. }
  625. return destSize;
  626. }
  627. /*
  628. *Name : handleTashkeelWithTatweel
  629. *Function : Replaces Tashkeel as following:
  630. * Case 1 :if the Tashkeel on tatweel, replace it with Tatweel.
  631. * Case 2 :if the Tashkeel aggregated with Shadda on Tatweel, replace
  632. * it with Shadda on Tatweel.
  633. * Case 3: if the Tashkeel is isolated replace it with Space.
  634. *
  635. */
  636. static int32_t
  637. handleTashkeelWithTatweel(char16_t *dest, int32_t sourceLength,
  638. int32_t /*destSize*/, uint32_t /*options*/,
  639. UErrorCode * /*pErrorCode*/) {
  640. int i;
  641. for(i = 0; i < sourceLength; i++){
  642. if((isTashkeelOnTatweelChar(dest[i]) == 1)){
  643. dest[i] = TATWEEL_CHAR;
  644. }else if((isTashkeelOnTatweelChar(dest[i]) == 2)){
  645. dest[i] = SHADDA_TATWEEL_CHAR;
  646. }else if(isIsolatedTashkeelChar(dest[i]) && dest[i] != SHADDA_CHAR){
  647. dest[i] = SPACE_CHAR;
  648. }
  649. }
  650. return sourceLength;
  651. }
  652. /*
  653. *Name : handleGeneratedSpaces
  654. *Function : The shapeUnicode function converts Lam + Alef into LamAlef + space,
  655. * and Tashkeel to space.
  656. * handleGeneratedSpaces function puts these generated spaces
  657. * according to the options the user specifies. LamAlef and Tashkeel
  658. * spaces can be replaced at begin, at end, at near or decrease the
  659. * buffer size.
  660. *
  661. * There is also Auto option for LamAlef and tashkeel, which will put
  662. * the spaces at end of the buffer (or end of text if the user used
  663. * the option U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END).
  664. *
  665. * If the text type was visual_LTR and the option
  666. * U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END was selected the END
  667. * option will place the space at the beginning of the buffer and
  668. * BEGIN will place the space at the end of the buffer.
  669. */
  670. static int32_t
  671. handleGeneratedSpaces(char16_t *dest, int32_t sourceLength,
  672. int32_t destSize,
  673. uint32_t options,
  674. UErrorCode *pErrorCode,struct uShapeVariables shapeVars ) {
  675. int32_t i = 0, j = 0;
  676. int32_t count = 0;
  677. char16_t *tempbuffer=nullptr;
  678. int lamAlefOption = 0;
  679. int tashkeelOption = 0;
  680. int shapingMode = SHAPE_MODE;
  681. if (shapingMode == 0){
  682. if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE ){
  683. lamAlefOption = 1;
  684. }
  685. if ( (options&U_SHAPE_TASHKEEL_MASK) == U_SHAPE_TASHKEEL_RESIZE ){
  686. tashkeelOption = 1;
  687. }
  688. }
  689. tempbuffer = static_cast<char16_t*>(uprv_malloc((sourceLength + 1) * U_SIZEOF_UCHAR));
  690. /* Test for nullptr */
  691. if(tempbuffer == nullptr) {
  692. *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
  693. return 0;
  694. }
  695. if (lamAlefOption || tashkeelOption){
  696. uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
  697. i = j = 0; count = 0;
  698. while(i < sourceLength) {
  699. if ( (lamAlefOption && dest[i] == LAMALEF_SPACE_SUB) ||
  700. (tashkeelOption && dest[i] == TASHKEEL_SPACE_SUB) ){
  701. j--;
  702. count++;
  703. } else {
  704. tempbuffer[j] = dest[i];
  705. }
  706. i++;
  707. j++;
  708. }
  709. while(count >= 0) {
  710. tempbuffer[i] = 0x0000;
  711. i--;
  712. count--;
  713. }
  714. u_memcpy(dest, tempbuffer, sourceLength);
  715. destSize = u_strlen(dest);
  716. }
  717. lamAlefOption = 0;
  718. if (shapingMode == 0){
  719. if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_NEAR ){
  720. lamAlefOption = 1;
  721. }
  722. }
  723. if (lamAlefOption){
  724. /* Lam+Alef is already shaped into LamAlef + FFFF */
  725. i = 0;
  726. while(i < sourceLength) {
  727. if(lamAlefOption&&dest[i] == LAMALEF_SPACE_SUB){
  728. dest[i] = SPACE_CHAR;
  729. }
  730. i++;
  731. }
  732. destSize = sourceLength;
  733. }
  734. lamAlefOption = 0;
  735. tashkeelOption = 0;
  736. if (shapingMode == 0) {
  737. if ( ((options&U_SHAPE_LAMALEF_MASK) == shapeVars.uShapeLamalefBegin) ||
  738. (((options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO )
  739. && (shapeVars.spacesRelativeToTextBeginEnd==1)) ) {
  740. lamAlefOption = 1;
  741. }
  742. if ( (options&U_SHAPE_TASHKEEL_MASK) == shapeVars.uShapeTashkeelBegin ) {
  743. tashkeelOption = 1;
  744. }
  745. }
  746. if(lamAlefOption || tashkeelOption){
  747. uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
  748. i = j = sourceLength; count = 0;
  749. while(i >= 0) {
  750. if ( (lamAlefOption && dest[i] == LAMALEF_SPACE_SUB) ||
  751. (tashkeelOption && dest[i] == TASHKEEL_SPACE_SUB) ){
  752. j++;
  753. count++;
  754. }else {
  755. tempbuffer[j] = dest[i];
  756. }
  757. i--;
  758. j--;
  759. }
  760. for(i=0 ;i < count; i++){
  761. tempbuffer[i] = SPACE_CHAR;
  762. }
  763. u_memcpy(dest, tempbuffer, sourceLength);
  764. destSize = sourceLength;
  765. }
  766. lamAlefOption = 0;
  767. tashkeelOption = 0;
  768. if (shapingMode == 0) {
  769. if ( ((options&U_SHAPE_LAMALEF_MASK) == shapeVars.uShapeLamalefEnd) ||
  770. (((options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO )
  771. && (shapeVars.spacesRelativeToTextBeginEnd==0)) ) {
  772. lamAlefOption = 1;
  773. }
  774. if ( (options&U_SHAPE_TASHKEEL_MASK) == shapeVars.uShapeTashkeelEnd ){
  775. tashkeelOption = 1;
  776. }
  777. }
  778. if(lamAlefOption || tashkeelOption){
  779. uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
  780. i = j = 0; count = 0;
  781. while(i < sourceLength) {
  782. if ( (lamAlefOption && dest[i] == LAMALEF_SPACE_SUB) ||
  783. (tashkeelOption && dest[i] == TASHKEEL_SPACE_SUB) ){
  784. j--;
  785. count++;
  786. }else {
  787. tempbuffer[j] = dest[i];
  788. }
  789. i++;
  790. j++;
  791. }
  792. while(count >= 0) {
  793. tempbuffer[i] = SPACE_CHAR;
  794. i--;
  795. count--;
  796. }
  797. u_memcpy(dest, tempbuffer, sourceLength);
  798. destSize = sourceLength;
  799. }
  800. if(tempbuffer){
  801. uprv_free(tempbuffer);
  802. }
  803. return destSize;
  804. }
  805. /*
  806. *Name :expandCompositCharAtBegin
  807. *Function :Expands the LamAlef character to Lam and Alef consuming the required
  808. * space from beginning of the buffer. If the text type was visual_LTR
  809. * and the option U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END was selected
  810. * the spaces will be located at end of buffer.
  811. * If there are no spaces to expand the LamAlef, an error
  812. * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h
  813. */
  814. static int32_t
  815. expandCompositCharAtBegin(char16_t *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode) {
  816. int32_t i = 0,j = 0;
  817. int32_t countl = 0;
  818. char16_t *tempbuffer=nullptr;
  819. tempbuffer = static_cast<char16_t*>(uprv_malloc((sourceLength + 1) * U_SIZEOF_UCHAR));
  820. /* Test for nullptr */
  821. if(tempbuffer == nullptr) {
  822. *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
  823. return 0;
  824. }
  825. uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
  826. i = 0;
  827. while(dest[i] == SPACE_CHAR) {
  828. countl++;
  829. i++;
  830. }
  831. i = j = sourceLength-1;
  832. while(i >= 0 && j >= 0) {
  833. if( countl>0 && isLamAlefChar(dest[i])) {
  834. tempbuffer[j] = LAM_CHAR;
  835. /* to ensure the array index is within the range */
  836. U_ASSERT(dest[i] >= 0xFEF5u
  837. && dest[i]-0xFEF5u < UPRV_LENGTHOF(convertLamAlef));
  838. tempbuffer[j-1] = convertLamAlef[ dest[i] - 0xFEF5 ];
  839. j--;
  840. countl--;
  841. }else {
  842. if( countl == 0 && isLamAlefChar(dest[i]) ) {
  843. *pErrorCode=U_NO_SPACE_AVAILABLE;
  844. }
  845. tempbuffer[j] = dest[i];
  846. }
  847. i--;
  848. j--;
  849. }
  850. u_memcpy(dest, tempbuffer, sourceLength);
  851. uprv_free(tempbuffer);
  852. destSize = sourceLength;
  853. return destSize;
  854. }
  855. /*
  856. *Name : expandCompositCharAtEnd
  857. *Function : Expands the LamAlef character to Lam and Alef consuming the
  858. * required space from end of the buffer. If the text type was
  859. * Visual LTR and the option U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END
  860. * was used, the spaces will be consumed from begin of buffer. If
  861. * there are no spaces to expand the LamAlef, an error
  862. * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h
  863. */
  864. static int32_t
  865. expandCompositCharAtEnd(char16_t *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode) {
  866. int32_t i = 0,j = 0;
  867. int32_t countr = 0;
  868. int32_t inpsize = sourceLength;
  869. char16_t *tempbuffer=nullptr;
  870. tempbuffer = static_cast<char16_t*>(uprv_malloc((sourceLength + 1) * U_SIZEOF_UCHAR));
  871. /* Test for nullptr */
  872. if(tempbuffer == nullptr) {
  873. *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
  874. return 0;
  875. }
  876. uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
  877. while(dest[inpsize-1] == SPACE_CHAR) {
  878. countr++;
  879. inpsize--;
  880. }
  881. i = sourceLength - countr - 1;
  882. j = sourceLength - 1;
  883. while(i >= 0 && j >= 0) {
  884. if( countr>0 && isLamAlefChar(dest[i]) ) {
  885. tempbuffer[j] = LAM_CHAR;
  886. tempbuffer[j-1] = convertLamAlef[ dest[i] - 0xFEF5 ];
  887. j--;
  888. countr--;
  889. }else {
  890. if ((countr == 0) && isLamAlefChar(dest[i]) ) {
  891. *pErrorCode=U_NO_SPACE_AVAILABLE;
  892. }
  893. tempbuffer[j] = dest[i];
  894. }
  895. i--;
  896. j--;
  897. }
  898. if(countr > 0) {
  899. u_memmove(tempbuffer, tempbuffer+countr, sourceLength);
  900. if(u_strlen(tempbuffer) < sourceLength) {
  901. for(i=sourceLength-1;i>=sourceLength-countr;i--) {
  902. tempbuffer[i] = SPACE_CHAR;
  903. }
  904. }
  905. }
  906. u_memcpy(dest, tempbuffer, sourceLength);
  907. uprv_free(tempbuffer);
  908. destSize = sourceLength;
  909. return destSize;
  910. }
  911. /*
  912. *Name : expandCompositCharAtNear
  913. *Function : Expands the LamAlef character into Lam + Alef, YehHamza character
  914. * into Yeh + Hamza, SeenFamily character into SeenFamily character
  915. * + Tail, while consuming the space next to the character.
  916. * If there are no spaces next to the character, an error
  917. * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h
  918. */
  919. static int32_t
  920. expandCompositCharAtNear(char16_t *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode,
  921. int yehHamzaOption, int seenTailOption, int lamAlefOption, struct uShapeVariables shapeVars) {
  922. int32_t i = 0;
  923. char16_t lamalefChar, yehhamzaChar;
  924. for(i = 0 ;i<=sourceLength-1;i++) {
  925. if (seenTailOption && isSeenTailFamilyChar(dest[i])) {
  926. if ((i>0) && (dest[i-1] == SPACE_CHAR) ) {
  927. dest[i-1] = shapeVars.tailChar;
  928. }else {
  929. *pErrorCode=U_NO_SPACE_AVAILABLE;
  930. }
  931. }else if(yehHamzaOption && (isYehHamzaChar(dest[i])) ) {
  932. if ((i>0) && (dest[i-1] == SPACE_CHAR) ) {
  933. yehhamzaChar = dest[i];
  934. dest[i] = yehHamzaToYeh[yehhamzaChar - YEH_HAMZAFE_CHAR];
  935. dest[i-1] = HAMZAFE_CHAR;
  936. }else {
  937. *pErrorCode=U_NO_SPACE_AVAILABLE;
  938. }
  939. }else if(lamAlefOption && isLamAlefChar(dest[i+1])) {
  940. if(dest[i] == SPACE_CHAR){
  941. lamalefChar = dest[i+1];
  942. dest[i+1] = LAM_CHAR;
  943. dest[i] = convertLamAlef[ lamalefChar - 0xFEF5 ];
  944. }else {
  945. *pErrorCode=U_NO_SPACE_AVAILABLE;
  946. }
  947. }
  948. }
  949. destSize = sourceLength;
  950. return destSize;
  951. }
  952. /*
  953. * Name : expandCompositChar
  954. * Function : LamAlef, need special handling, since it expands from one
  955. * character into two characters while shaping or deshaping.
  956. * In order to expand it, near or far spaces according to the
  957. * options user specifies. Also buffer size can be increased.
  958. *
  959. * For SeenFamily characters and YehHamza only the near option is
  960. * supported, while for LamAlef we can take spaces from begin, end,
  961. * near or even increase the buffer size.
  962. * There is also the Auto option for LamAlef only, which will first
  963. * search for a space at end, begin then near, respectively.
  964. * If there are no spaces to expand these characters, an error will be set to
  965. * U_NO_SPACE_AVAILABLE as defined in utypes.h
  966. */
  967. static int32_t
  968. expandCompositChar(char16_t *dest, int32_t sourceLength,
  969. int32_t destSize,uint32_t options,
  970. UErrorCode *pErrorCode, int shapingMode,struct uShapeVariables shapeVars) {
  971. int32_t i = 0,j = 0;
  972. char16_t *tempbuffer=nullptr;
  973. int yehHamzaOption = 0;
  974. int seenTailOption = 0;
  975. int lamAlefOption = 0;
  976. if (shapingMode == 1){
  977. if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO){
  978. if(shapeVars.spacesRelativeToTextBeginEnd == 0) {
  979. destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode);
  980. if(*pErrorCode == U_NO_SPACE_AVAILABLE) {
  981. *pErrorCode = U_ZERO_ERROR;
  982. destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode);
  983. }
  984. }else {
  985. destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode);
  986. if(*pErrorCode == U_NO_SPACE_AVAILABLE) {
  987. *pErrorCode = U_ZERO_ERROR;
  988. destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode);
  989. }
  990. }
  991. if(*pErrorCode == U_NO_SPACE_AVAILABLE) {
  992. *pErrorCode = U_ZERO_ERROR;
  993. destSize = expandCompositCharAtNear(dest, sourceLength, destSize, pErrorCode, yehHamzaOption,
  994. seenTailOption, 1,shapeVars);
  995. }
  996. }
  997. }
  998. if (shapingMode == 1){
  999. if ( (options&U_SHAPE_LAMALEF_MASK) == shapeVars.uShapeLamalefEnd){
  1000. destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode);
  1001. }
  1002. }
  1003. if (shapingMode == 1){
  1004. if ( (options&U_SHAPE_LAMALEF_MASK) == shapeVars.uShapeLamalefBegin){
  1005. destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode);
  1006. }
  1007. }
  1008. if (shapingMode == 0){
  1009. if ((options&U_SHAPE_YEHHAMZA_MASK) == U_SHAPE_YEHHAMZA_TWOCELL_NEAR){
  1010. yehHamzaOption = 1;
  1011. }
  1012. if ((options&U_SHAPE_SEEN_MASK) == U_SHAPE_SEEN_TWOCELL_NEAR){
  1013. seenTailOption = 1;
  1014. }
  1015. }
  1016. if (shapingMode == 1) {
  1017. if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_NEAR) {
  1018. lamAlefOption = 1;
  1019. }
  1020. }
  1021. if (yehHamzaOption || seenTailOption || lamAlefOption){
  1022. destSize = expandCompositCharAtNear(dest, sourceLength, destSize, pErrorCode, yehHamzaOption,
  1023. seenTailOption,lamAlefOption,shapeVars);
  1024. }
  1025. if (shapingMode == 1){
  1026. if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE){
  1027. destSize = calculateSize(dest,sourceLength,destSize,options);
  1028. tempbuffer = static_cast<char16_t*>(uprv_malloc((destSize + 1) * U_SIZEOF_UCHAR));
  1029. /* Test for nullptr */
  1030. if(tempbuffer == nullptr) {
  1031. *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
  1032. return 0;
  1033. }
  1034. uprv_memset(tempbuffer, 0, (destSize+1)*U_SIZEOF_UCHAR);
  1035. i = j = 0;
  1036. while(i < destSize && j < destSize) {
  1037. if(isLamAlefChar(dest[i]) ) {
  1038. tempbuffer[j] = convertLamAlef[ dest[i] - 0xFEF5 ];
  1039. tempbuffer[j+1] = LAM_CHAR;
  1040. j++;
  1041. }else {
  1042. tempbuffer[j] = dest[i];
  1043. }
  1044. i++;
  1045. j++;
  1046. }
  1047. u_memcpy(dest, tempbuffer, destSize);
  1048. }
  1049. }
  1050. if(tempbuffer) {
  1051. uprv_free(tempbuffer);
  1052. }
  1053. return destSize;
  1054. }
  1055. /*
  1056. *Name : shapeUnicode
  1057. *Function : Converts an Arabic Unicode buffer in 06xx Range into a shaped
  1058. * arabic Unicode buffer in FExx Range
  1059. */
  1060. static int32_t
  1061. shapeUnicode(char16_t *dest, int32_t sourceLength,
  1062. int32_t destSize,uint32_t options,
  1063. UErrorCode *pErrorCode,
  1064. int tashkeelFlag, struct uShapeVariables shapeVars) {
  1065. int32_t i, iend;
  1066. int32_t step;
  1067. int32_t lastPos,Nx, Nw;
  1068. unsigned int Shape;
  1069. int32_t lamalef_found = 0;
  1070. int32_t seenfamFound = 0, yehhamzaFound =0, tashkeelFound = 0;
  1071. char16_t prevLink = 0, lastLink = 0, currLink, nextLink = 0;
  1072. char16_t wLamalef;
  1073. /*
  1074. * Converts the input buffer from FExx Range into 06xx Range
  1075. * to make sure that all characters are in the 06xx range
  1076. * even the lamalef is converted to the special region in
  1077. * the 06xx range
  1078. */
  1079. if ((options & U_SHAPE_PRESERVE_PRESENTATION_MASK) == U_SHAPE_PRESERVE_PRESENTATION_NOOP) {
  1080. for (i = 0; i < sourceLength; i++) {
  1081. char16_t inputChar = dest[i];
  1082. if ( (inputChar >= 0xFB50) && (inputChar <= 0xFBFF)) {
  1083. char16_t c = convertFBto06 [ (inputChar - 0xFB50) ];
  1084. if (c != 0)
  1085. dest[i] = c;
  1086. } else if ( (inputChar >= 0xFE70) && (inputChar <= 0xFEFC)) {
  1087. dest[i] = convertFEto06 [ (inputChar - 0xFE70) ] ;
  1088. } else {
  1089. dest[i] = inputChar ;
  1090. }
  1091. }
  1092. }
  1093. /* sets the index to the end of the buffer, together with the step point to -1 */
  1094. i = sourceLength - 1;
  1095. iend = -1;
  1096. step = -1;
  1097. /*
  1098. * This function resolves the link between the characters .
  1099. * Arabic characters have four forms :
  1100. * Isolated Form, Initial Form, Middle Form and Final Form
  1101. */
  1102. currLink = getLink(dest[i]);
  1103. lastPos = i;
  1104. Nx = -2, Nw = 0;
  1105. while (i != iend) {
  1106. /* If high byte of currLink > 0 then more than one shape */
  1107. if ((currLink & 0xFF00) > 0 || (getLink(dest[i]) & IRRELEVANT) != 0) {
  1108. Nw = i + step;
  1109. while (Nx < 0) { /* we need to know about next char */
  1110. if(Nw == iend) {
  1111. nextLink = 0;
  1112. Nx = 3000;
  1113. } else {
  1114. nextLink = getLink(dest[Nw]);
  1115. if((nextLink & IRRELEVANT) == 0) {
  1116. Nx = Nw;
  1117. } else {
  1118. Nw = Nw + step;
  1119. }
  1120. }
  1121. }
  1122. if ( ((currLink & ALEFTYPE) > 0) && ((lastLink & LAMTYPE) > 0) ) {
  1123. lamalef_found = 1;
  1124. wLamalef = changeLamAlef(dest[i]); /*get from 0x065C-0x065f */
  1125. if ( wLamalef != 0) {
  1126. dest[i] = LAMALEF_SPACE_SUB; /* The default case is to drop the Alef and replace */
  1127. dest[lastPos] =wLamalef; /* it by LAMALEF_SPACE_SUB which is the last character in the */
  1128. i=lastPos; /* unicode private use area, this is done to make */
  1129. } /* sure that removeLamAlefSpaces() handles only the */
  1130. lastLink = prevLink; /* spaces generated during lamalef generation. */
  1131. currLink = getLink(wLamalef); /* LAMALEF_SPACE_SUB is added here and is replaced by spaces */
  1132. } /* in removeLamAlefSpaces() */
  1133. if ((i > 0) && (dest[i-1] == SPACE_CHAR)){
  1134. if ( isSeenFamilyChar(dest[i])) {
  1135. seenfamFound = 1;
  1136. } else if (dest[i] == YEH_HAMZA_CHAR) {
  1137. yehhamzaFound = 1;
  1138. }
  1139. }
  1140. else if(i==0){
  1141. if ( isSeenFamilyChar(dest[i])){
  1142. seenfamFound = 1;
  1143. } else if (dest[i] == YEH_HAMZA_CHAR) {
  1144. yehhamzaFound = 1;
  1145. }
  1146. }
  1147. /*
  1148. * get the proper shape according to link ability of neighbors
  1149. * and of character; depends on the order of the shapes
  1150. * (isolated, initial, middle, final) in the compatibility area
  1151. */
  1152. Shape = shapeTable[nextLink & (LINKR + LINKL)]
  1153. [lastLink & (LINKR + LINKL)]
  1154. [currLink & (LINKR + LINKL)];
  1155. if ((currLink & (LINKR+LINKL)) == 1) {
  1156. Shape &= 1;
  1157. } else if(isTashkeelChar(dest[i])) {
  1158. if( (lastLink & LINKL) && (nextLink & LINKR) && (tashkeelFlag == 1) &&
  1159. dest[i] != 0x064C && dest[i] != 0x064D )
  1160. {
  1161. Shape = 1;
  1162. if( (nextLink&ALEFTYPE) == ALEFTYPE && (lastLink&LAMTYPE) == LAMTYPE ) {
  1163. Shape = 0;
  1164. }
  1165. } else if(tashkeelFlag == 2 && dest[i] == SHADDA06_CHAR){
  1166. Shape = 1;
  1167. } else {
  1168. Shape = 0;
  1169. }
  1170. }
  1171. if ((dest[i] ^ 0x0600) < 0x100) {
  1172. if ( isTashkeelChar(dest[i]) ){
  1173. if (tashkeelFlag == 2 && dest[i] != SHADDA06_CHAR){
  1174. dest[i] = TASHKEEL_SPACE_SUB;
  1175. tashkeelFound = 1;
  1176. } else {
  1177. /* to ensure the array index is within the range */
  1178. U_ASSERT(dest[i] >= 0x064Bu
  1179. && dest[i]-0x064Bu < UPRV_LENGTHOF(IrrelevantPos));
  1180. dest[i] = 0xFE70 + IrrelevantPos[(dest[i] - 0x064B)] + static_cast<char16_t>(Shape);
  1181. }
  1182. }else if ((currLink & APRESENT) > 0) {
  1183. dest[i] = static_cast<char16_t>(0xFB50 + (currLink >> 8) + Shape);
  1184. }else if ((currLink >> 8) > 0 && (currLink & IRRELEVANT) == 0) {
  1185. dest[i] = static_cast<char16_t>(0xFE70 + (currLink >> 8) + Shape);
  1186. }
  1187. }
  1188. }
  1189. /* move one notch forward */
  1190. if ((currLink & IRRELEVANT) == 0) {
  1191. prevLink = lastLink;
  1192. lastLink = currLink;
  1193. lastPos = i;
  1194. }
  1195. i = i + step;
  1196. if (i == Nx) {
  1197. currLink = nextLink;
  1198. Nx = -2;
  1199. } else if(i != iend) {
  1200. currLink = getLink(dest[i]);
  1201. }
  1202. }
  1203. destSize = sourceLength;
  1204. if ( (lamalef_found != 0 ) || (tashkeelFound != 0) ){
  1205. destSize = handleGeneratedSpaces(dest,sourceLength,destSize,options,pErrorCode, shapeVars);
  1206. }
  1207. if ( (seenfamFound != 0) || (yehhamzaFound != 0) ) {
  1208. destSize = expandCompositChar(dest, sourceLength,destSize,options,pErrorCode, SHAPE_MODE,shapeVars);
  1209. }
  1210. return destSize;
  1211. }
  1212. /*
  1213. *Name : deShapeUnicode
  1214. *Function : Converts an Arabic Unicode buffer in FExx Range into unshaped
  1215. * arabic Unicode buffer in 06xx Range
  1216. */
  1217. static int32_t
  1218. deShapeUnicode(char16_t *dest, int32_t sourceLength,
  1219. int32_t destSize,uint32_t options,
  1220. UErrorCode *pErrorCode, struct uShapeVariables shapeVars) {
  1221. int32_t i = 0;
  1222. int32_t lamalef_found = 0;
  1223. int32_t yehHamzaComposeEnabled = 0;
  1224. int32_t seenComposeEnabled = 0;
  1225. yehHamzaComposeEnabled = ((options&U_SHAPE_YEHHAMZA_MASK) == U_SHAPE_YEHHAMZA_TWOCELL_NEAR) ? 1 : 0;
  1226. seenComposeEnabled = ((options&U_SHAPE_SEEN_MASK) == U_SHAPE_SEEN_TWOCELL_NEAR)? 1 : 0;
  1227. /*
  1228. *This for loop changes the buffer from the Unicode FE range to
  1229. *the Unicode 06 range
  1230. */
  1231. for(i = 0; i < sourceLength; i++) {
  1232. char16_t inputChar = dest[i];
  1233. if ( (inputChar >= 0xFB50) && (inputChar <= 0xFBFF)) { /* FBxx Arabic range */
  1234. char16_t c = convertFBto06 [ (inputChar - 0xFB50) ];
  1235. if (c != 0)
  1236. dest[i] = c;
  1237. } else if( (yehHamzaComposeEnabled == 1) && ((inputChar == HAMZA06_CHAR) || (inputChar == HAMZAFE_CHAR))
  1238. && (i < (sourceLength - 1)) && isAlefMaksouraChar(dest[i+1] )) {
  1239. dest[i] = SPACE_CHAR;
  1240. dest[i+1] = YEH_HAMZA_CHAR;
  1241. } else if ( (seenComposeEnabled == 1) && (isTailChar(inputChar)) && (i< (sourceLength - 1))
  1242. && (isSeenTailFamilyChar(dest[i+1])) ) {
  1243. dest[i] = SPACE_CHAR;
  1244. } else if (( inputChar >= 0xFE70) && (inputChar <= 0xFEF4 )) { /* FExx Arabic range */
  1245. dest[i] = convertFEto06 [ (inputChar - 0xFE70) ];
  1246. } else {
  1247. dest[i] = inputChar ;
  1248. }
  1249. if( isLamAlefChar(dest[i]) )
  1250. lamalef_found = 1;
  1251. }
  1252. destSize = sourceLength;
  1253. if (lamalef_found != 0){
  1254. destSize = expandCompositChar(dest,sourceLength,destSize,options,pErrorCode,DESHAPE_MODE, shapeVars);
  1255. }
  1256. return destSize;
  1257. }
  1258. /*
  1259. ****************************************
  1260. * u_shapeArabic
  1261. ****************************************
  1262. */
  1263. U_CAPI int32_t U_EXPORT2
  1264. u_shapeArabic(const char16_t *source, int32_t sourceLength,
  1265. char16_t *dest, int32_t destCapacity,
  1266. uint32_t options,
  1267. UErrorCode *pErrorCode) {
  1268. int32_t destLength;
  1269. struct uShapeVariables shapeVars = { OLD_TAIL_CHAR,U_SHAPE_LAMALEF_BEGIN,U_SHAPE_LAMALEF_END,U_SHAPE_TASHKEEL_BEGIN,U_SHAPE_TASHKEEL_END,0};
  1270. /* usual error checking */
  1271. if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
  1272. return 0;
  1273. }
  1274. /* make sure that no reserved options values are used; allow dest==nullptr only for preflighting */
  1275. if( source==nullptr || sourceLength<-1 || (dest==nullptr && destCapacity!=0) || destCapacity<0 ||
  1276. (((options&U_SHAPE_TASHKEEL_MASK) > 0) &&
  1277. ((options&U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) == U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) ) ||
  1278. (((options&U_SHAPE_TASHKEEL_MASK) > 0) &&
  1279. ((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_UNSHAPE)) ||
  1280. (options&U_SHAPE_DIGIT_TYPE_RESERVED)==U_SHAPE_DIGIT_TYPE_RESERVED ||
  1281. (options&U_SHAPE_DIGITS_MASK)==U_SHAPE_DIGITS_RESERVED ||
  1282. ((options&U_SHAPE_LAMALEF_MASK) != U_SHAPE_LAMALEF_RESIZE &&
  1283. (options&U_SHAPE_AGGREGATE_TASHKEEL_MASK) != 0) ||
  1284. ((options&U_SHAPE_AGGREGATE_TASHKEEL_MASK) == U_SHAPE_AGGREGATE_TASHKEEL &&
  1285. (options&U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) != U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED)
  1286. )
  1287. {
  1288. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1289. return 0;
  1290. }
  1291. /* Validate lamalef options */
  1292. if(((options&U_SHAPE_LAMALEF_MASK) > 0)&&
  1293. !(((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_BEGIN) ||
  1294. ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_END ) ||
  1295. ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_RESIZE )||
  1296. ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_AUTO) ||
  1297. ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_NEAR)))
  1298. {
  1299. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1300. return 0;
  1301. }
  1302. /* Validate Tashkeel options */
  1303. if(((options&U_SHAPE_TASHKEEL_MASK) > 0)&&
  1304. !(((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_BEGIN) ||
  1305. ((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_END )
  1306. ||((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_RESIZE )||
  1307. ((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL)))
  1308. {
  1309. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1310. return 0;
  1311. }
  1312. /* determine the source length */
  1313. if(sourceLength==-1) {
  1314. sourceLength=u_strlen(source);
  1315. }
  1316. if(sourceLength<=0) {
  1317. return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
  1318. }
  1319. /* check that source and destination do not overlap */
  1320. if( dest!=nullptr &&
  1321. ((source<=dest && dest<source+sourceLength) ||
  1322. (dest<=source && source<dest+destCapacity))) {
  1323. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1324. return 0;
  1325. }
  1326. /* Does Options contain the new Seen Tail Unicode code point option */
  1327. if ( (options&U_SHAPE_TAIL_TYPE_MASK) == U_SHAPE_TAIL_NEW_UNICODE){
  1328. shapeVars.tailChar = NEW_TAIL_CHAR;
  1329. }else {
  1330. shapeVars.tailChar = OLD_TAIL_CHAR;
  1331. }
  1332. if((options&U_SHAPE_LETTERS_MASK)!=U_SHAPE_LETTERS_NOOP) {
  1333. char16_t buffer[300];
  1334. char16_t *tempbuffer, *tempsource = nullptr;
  1335. int32_t outputSize, spacesCountl=0, spacesCountr=0;
  1336. if((options&U_SHAPE_AGGREGATE_TASHKEEL_MASK)>0) {
  1337. int32_t logical_order = (options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL;
  1338. int32_t aggregate_tashkeel =
  1339. (options&(U_SHAPE_AGGREGATE_TASHKEEL_MASK+U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED)) ==
  1340. (U_SHAPE_AGGREGATE_TASHKEEL+U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED);
  1341. int step=logical_order?1:-1;
  1342. int j=logical_order?-1:2*sourceLength;
  1343. int i=logical_order?-1:sourceLength;
  1344. int end=logical_order?sourceLength:-1;
  1345. int aggregation_possible = 1;
  1346. char16_t prev = 0;
  1347. char16_t prevLink, currLink = 0;
  1348. int newSourceLength = 0;
  1349. tempsource = (char16_t *)uprv_malloc(2*sourceLength*U_SIZEOF_UCHAR);
  1350. if(tempsource == nullptr) {
  1351. *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
  1352. return 0;
  1353. }
  1354. while ((i+=step) != end) {
  1355. prevLink = currLink;
  1356. currLink = getLink(source[i]);
  1357. if (aggregate_tashkeel && ((prevLink|currLink)&COMBINE) == COMBINE && aggregation_possible) {
  1358. aggregation_possible = 0;
  1359. tempsource[j] = (prev<source[i]?prev:source[i])-0x064C+0xFC5E;
  1360. currLink = getLink(tempsource[j]);
  1361. } else {
  1362. aggregation_possible = 1;
  1363. tempsource[j+=step] = source[i];
  1364. prev = source[i];
  1365. newSourceLength++;
  1366. }
  1367. }
  1368. source = tempsource+(logical_order?0:j);
  1369. sourceLength = newSourceLength;
  1370. }
  1371. /* calculate destination size */
  1372. /* TODO: do we ever need to do this pure preflighting? */
  1373. if(((options&U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_RESIZE) ||
  1374. ((options&U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_RESIZE)) {
  1375. outputSize=calculateSize(source,sourceLength,destCapacity,options);
  1376. } else {
  1377. outputSize=sourceLength;
  1378. }
  1379. if(outputSize>destCapacity) {
  1380. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  1381. if (tempsource != nullptr) uprv_free(tempsource);
  1382. return outputSize;
  1383. }
  1384. /*
  1385. * need a temporary buffer of size max(outputSize, sourceLength)
  1386. * because at first we copy source->temp
  1387. */
  1388. if(sourceLength>outputSize) {
  1389. outputSize=sourceLength;
  1390. }
  1391. /* Start of Arabic letter shaping part */
  1392. if(outputSize<=UPRV_LENGTHOF(buffer)) {
  1393. outputSize=UPRV_LENGTHOF(buffer);
  1394. tempbuffer=buffer;
  1395. } else {
  1396. tempbuffer = (char16_t *)uprv_malloc(outputSize*U_SIZEOF_UCHAR);
  1397. /*Test for nullptr*/
  1398. if(tempbuffer == nullptr) {
  1399. *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
  1400. if (tempsource != nullptr) uprv_free(tempsource);
  1401. return 0;
  1402. }
  1403. }
  1404. u_memcpy(tempbuffer, source, sourceLength);
  1405. if (tempsource != nullptr){
  1406. uprv_free(tempsource);
  1407. }
  1408. if(sourceLength<outputSize) {
  1409. uprv_memset(tempbuffer+sourceLength, 0, (outputSize-sourceLength)*U_SIZEOF_UCHAR);
  1410. }
  1411. if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL) {
  1412. countSpaces(tempbuffer,sourceLength,options,&spacesCountl,&spacesCountr);
  1413. invertBuffer(tempbuffer,sourceLength,options,spacesCountl,spacesCountr);
  1414. }
  1415. if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_VISUAL_LTR) {
  1416. if((options&U_SHAPE_SPACES_RELATIVE_TO_TEXT_MASK) == U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END) {
  1417. shapeVars.spacesRelativeToTextBeginEnd = 1;
  1418. shapeVars.uShapeLamalefBegin = U_SHAPE_LAMALEF_END;
  1419. shapeVars.uShapeLamalefEnd = U_SHAPE_LAMALEF_BEGIN;
  1420. shapeVars.uShapeTashkeelBegin = U_SHAPE_TASHKEEL_END;
  1421. shapeVars.uShapeTashkeelEnd = U_SHAPE_TASHKEEL_BEGIN;
  1422. }
  1423. }
  1424. switch(options&U_SHAPE_LETTERS_MASK) {
  1425. case U_SHAPE_LETTERS_SHAPE :
  1426. if( (options&U_SHAPE_TASHKEEL_MASK)> 0
  1427. && ((options&U_SHAPE_TASHKEEL_MASK) !=U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL)) {
  1428. /* Call the shaping function with tashkeel flag == 2 for removal of tashkeel */
  1429. destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,2,shapeVars);
  1430. }else {
  1431. /* default Call the shaping function with tashkeel flag == 1 */
  1432. destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,1,shapeVars);
  1433. /*After shaping text check if user wants to remove tashkeel and replace it with tatweel*/
  1434. if( (options&U_SHAPE_TASHKEEL_MASK) == U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL){
  1435. destLength = handleTashkeelWithTatweel(tempbuffer,destLength,destCapacity,options,pErrorCode);
  1436. }
  1437. }
  1438. break;
  1439. case U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED :
  1440. /* Call the shaping function with tashkeel flag == 0 */
  1441. destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,0,shapeVars);
  1442. break;
  1443. case U_SHAPE_LETTERS_UNSHAPE :
  1444. /* Call the deshaping function */
  1445. destLength = deShapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,shapeVars);
  1446. break;
  1447. default :
  1448. /* will never occur because of validity checks above */
  1449. destLength = 0;
  1450. break;
  1451. }
  1452. /*
  1453. * TODO: (markus 2002aug01)
  1454. * For as long as we always preflight the outputSize above
  1455. * we should U_ASSERT(outputSize==destLength)
  1456. * except for the adjustment above before the tempbuffer allocation
  1457. */
  1458. if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL) {
  1459. countSpaces(tempbuffer,destLength,options,&spacesCountl,&spacesCountr);
  1460. invertBuffer(tempbuffer,destLength,options,spacesCountl,spacesCountr);
  1461. }
  1462. u_memcpy(dest, tempbuffer, uprv_min(destLength, destCapacity));
  1463. if(tempbuffer!=buffer) {
  1464. uprv_free(tempbuffer);
  1465. }
  1466. if(destLength>destCapacity) {
  1467. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  1468. return destLength;
  1469. }
  1470. /* End of Arabic letter shaping part */
  1471. } else {
  1472. /*
  1473. * No letter shaping:
  1474. * just make sure the destination is large enough and copy the string.
  1475. */
  1476. if(destCapacity<sourceLength) {
  1477. /* this catches preflighting, too */
  1478. *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
  1479. return sourceLength;
  1480. }
  1481. u_memcpy(dest, source, sourceLength);
  1482. destLength=sourceLength;
  1483. }
  1484. /*
  1485. * Perform number shaping.
  1486. * With UTF-16 or UTF-32, the length of the string is constant.
  1487. * The easiest way to do this is to operate on the destination and
  1488. * "shape" the digits in-place.
  1489. */
  1490. if((options&U_SHAPE_DIGITS_MASK)!=U_SHAPE_DIGITS_NOOP) {
  1491. char16_t digitBase;
  1492. int32_t i;
  1493. /* select the requested digit group */
  1494. switch(options&U_SHAPE_DIGIT_TYPE_MASK) {
  1495. case U_SHAPE_DIGIT_TYPE_AN:
  1496. digitBase=0x660; /* Unicode: "Arabic-Indic digits" */
  1497. break;
  1498. case U_SHAPE_DIGIT_TYPE_AN_EXTENDED:
  1499. digitBase=0x6f0; /* Unicode: "Eastern Arabic-Indic digits (Persian and Urdu)" */
  1500. break;
  1501. default:
  1502. /* will never occur because of validity checks above */
  1503. digitBase=0;
  1504. break;
  1505. }
  1506. /* perform the requested operation */
  1507. switch(options&U_SHAPE_DIGITS_MASK) {
  1508. case U_SHAPE_DIGITS_EN2AN:
  1509. /* add (digitBase-'0') to each European (ASCII) digit code point */
  1510. digitBase-=0x30;
  1511. for(i=0; i<destLength; ++i) {
  1512. if(((uint32_t)dest[i]-0x30)<10) {
  1513. dest[i]+=digitBase;
  1514. }
  1515. }
  1516. break;
  1517. case U_SHAPE_DIGITS_AN2EN:
  1518. /* subtract (digitBase-'0') from each Arabic digit code point */
  1519. for(i=0; i<destLength; ++i) {
  1520. if(((uint32_t)dest[i]-(uint32_t)digitBase)<10) {
  1521. dest[i]-=digitBase-0x30;
  1522. }
  1523. }
  1524. break;
  1525. case U_SHAPE_DIGITS_ALEN2AN_INIT_LR:
  1526. _shapeToArabicDigitsWithContext(dest, destLength,
  1527. digitBase,
  1528. (options & U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL,
  1529. false);
  1530. break;
  1531. case U_SHAPE_DIGITS_ALEN2AN_INIT_AL:
  1532. _shapeToArabicDigitsWithContext(dest, destLength,
  1533. digitBase,
  1534. (options & U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL,
  1535. true);
  1536. break;
  1537. default:
  1538. /* will never occur because of validity checks above */
  1539. break;
  1540. }
  1541. }
  1542. return u_terminateUChars(dest, destCapacity, destLength, pErrorCode);
  1543. }