ubidi.cpp 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036
  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) 1999-2015, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. * file name: ubidi.c
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 1999jul27
  16. * created by: Markus W. Scherer, updated by Matitiahu Allouche
  17. *
  18. */
  19. #include "cmemory.h"
  20. #include "unicode/utypes.h"
  21. #include "unicode/ustring.h"
  22. #include "unicode/uchar.h"
  23. #include "unicode/ubidi.h"
  24. #include "unicode/utf16.h"
  25. #include "ubidi_props.h"
  26. #include "ubidiimp.h"
  27. #include "uassert.h"
  28. /*
  29. * General implementation notes:
  30. *
  31. * Throughout the implementation, there are comments like (W2) that refer to
  32. * rules of the BiDi algorithm, in this example to the second rule of the
  33. * resolution of weak types.
  34. *
  35. * For handling surrogate pairs, where two char16_t's form one "abstract" (or UTF-32)
  36. * character according to UTF-16, the second char16_t gets the directional property of
  37. * the entire character assigned, while the first one gets a BN, a boundary
  38. * neutral, type, which is ignored by most of the algorithm according to
  39. * rule (X9) and the implementation suggestions of the BiDi algorithm.
  40. *
  41. * Later, adjustWSLevels() will set the level for each BN to that of the
  42. * following character (char16_t), which results in surrogate pairs getting the
  43. * same level on each of their surrogates.
  44. *
  45. * In a UTF-8 implementation, the same thing could be done: the last byte of
  46. * a multi-byte sequence would get the "real" property, while all previous
  47. * bytes of that sequence would get BN.
  48. *
  49. * It is not possible to assign all those parts of a character the same real
  50. * property because this would fail in the resolution of weak types with rules
  51. * that look at immediately surrounding types.
  52. *
  53. * As a related topic, this implementation does not remove Boundary Neutral
  54. * types from the input, but ignores them wherever this is relevant.
  55. * For example, the loop for the resolution of the weak types reads
  56. * types until it finds a non-BN.
  57. * Also, explicit embedding codes are neither changed into BN nor removed.
  58. * They are only treated the same way real BNs are.
  59. * As stated before, adjustWSLevels() takes care of them at the end.
  60. * For the purpose of conformance, the levels of all these codes
  61. * do not matter.
  62. *
  63. * Note that this implementation modifies the dirProps
  64. * after the initial setup, when applying X5c (replace FSI by LRI or RLI),
  65. * X6, N0 (replace paired brackets by L or R).
  66. *
  67. * In this implementation, the resolution of weak types (W1 to W6),
  68. * neutrals (N1 and N2), and the assignment of the resolved level (In)
  69. * are all done in one single loop, in resolveImplicitLevels().
  70. * Changes of dirProp values are done on the fly, without writing
  71. * them back to the dirProps array.
  72. *
  73. *
  74. * This implementation contains code that allows to bypass steps of the
  75. * algorithm that are not needed on the specific paragraph
  76. * in order to speed up the most common cases considerably,
  77. * like text that is entirely LTR, or RTL text without numbers.
  78. *
  79. * Most of this is done by setting a bit for each directional property
  80. * in a flags variable and later checking for whether there are
  81. * any LTR characters or any RTL characters, or both, whether
  82. * there are any explicit embedding codes, etc.
  83. *
  84. * If the (Xn) steps are performed, then the flags are re-evaluated,
  85. * because they will then not contain the embedding codes any more
  86. * and will be adjusted for override codes, so that subsequently
  87. * more bypassing may be possible than what the initial flags suggested.
  88. *
  89. * If the text is not mixed-directional, then the
  90. * algorithm steps for the weak type resolution are not performed,
  91. * and all levels are set to the paragraph level.
  92. *
  93. * If there are no explicit embedding codes, then the (Xn) steps
  94. * are not performed.
  95. *
  96. * If embedding levels are supplied as a parameter, then all
  97. * explicit embedding codes are ignored, and the (Xn) steps
  98. * are not performed.
  99. *
  100. * White Space types could get the level of the run they belong to,
  101. * and are checked with a test of (flags&MASK_EMBEDDING) to
  102. * consider if the paragraph direction should be considered in
  103. * the flags variable.
  104. *
  105. * If there are no White Space types in the paragraph, then
  106. * (L1) is not necessary in adjustWSLevels().
  107. */
  108. /* to avoid some conditional statements, use tiny constant arrays */
  109. static const Flags flagLR[2]={ DIRPROP_FLAG(L), DIRPROP_FLAG(R) };
  110. static const Flags flagE[2]={ DIRPROP_FLAG(LRE), DIRPROP_FLAG(RLE) };
  111. static const Flags flagO[2]={ DIRPROP_FLAG(LRO), DIRPROP_FLAG(RLO) };
  112. #define DIRPROP_FLAG_LR(level) flagLR[(level)&1]
  113. #define DIRPROP_FLAG_E(level) flagE[(level)&1]
  114. #define DIRPROP_FLAG_O(level) flagO[(level)&1]
  115. #define DIR_FROM_STRONG(strong) ((strong)==L ? L : R)
  116. #define NO_OVERRIDE(level) ((level)&~UBIDI_LEVEL_OVERRIDE)
  117. /* UBiDi object management -------------------------------------------------- */
  118. U_CAPI UBiDi * U_EXPORT2
  119. ubidi_open()
  120. {
  121. UErrorCode errorCode=U_ZERO_ERROR;
  122. return ubidi_openSized(0, 0, &errorCode);
  123. }
  124. U_CAPI UBiDi * U_EXPORT2
  125. ubidi_openSized(int32_t maxLength, int32_t maxRunCount, UErrorCode *pErrorCode) {
  126. UBiDi *pBiDi;
  127. /* check the argument values */
  128. if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
  129. return nullptr;
  130. } else if(maxLength<0 || maxRunCount<0) {
  131. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  132. return nullptr; /* invalid arguments */
  133. }
  134. /* allocate memory for the object */
  135. pBiDi=(UBiDi *)uprv_malloc(sizeof(UBiDi));
  136. if(pBiDi==nullptr) {
  137. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  138. return nullptr;
  139. }
  140. /* reset the object, all pointers nullptr, all flags false, all sizes 0 */
  141. uprv_memset(pBiDi, 0, sizeof(UBiDi));
  142. /* allocate memory for arrays as requested */
  143. if(maxLength>0) {
  144. if( !getInitialDirPropsMemory(pBiDi, maxLength) ||
  145. !getInitialLevelsMemory(pBiDi, maxLength)
  146. ) {
  147. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  148. }
  149. } else {
  150. pBiDi->mayAllocateText=true;
  151. }
  152. if(maxRunCount>0) {
  153. if(maxRunCount==1) {
  154. /* use simpleRuns[] */
  155. pBiDi->runsSize=sizeof(Run);
  156. } else if(!getInitialRunsMemory(pBiDi, maxRunCount)) {
  157. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  158. }
  159. } else {
  160. pBiDi->mayAllocateRuns=true;
  161. }
  162. if(U_SUCCESS(*pErrorCode)) {
  163. return pBiDi;
  164. } else {
  165. ubidi_close(pBiDi);
  166. return nullptr;
  167. }
  168. }
  169. /*
  170. * We are allowed to allocate memory if memory==nullptr or
  171. * mayAllocate==true for each array that we need.
  172. * We also try to grow memory as needed if we
  173. * allocate it.
  174. *
  175. * Assume sizeNeeded>0.
  176. * If *pMemory!=nullptr, then assume *pSize>0.
  177. *
  178. * ### this realloc() may unnecessarily copy the old data,
  179. * which we know we don't need any more;
  180. * is this the best way to do this??
  181. */
  182. U_CFUNC UBool
  183. ubidi_getMemory(BidiMemoryForAllocation *bidiMem, int32_t *pSize, UBool mayAllocate, int32_t sizeNeeded) {
  184. void **pMemory = (void **)bidiMem;
  185. /* check for existing memory */
  186. if(*pMemory==nullptr) {
  187. /* we need to allocate memory */
  188. if(mayAllocate && (*pMemory=uprv_malloc(sizeNeeded))!=nullptr) {
  189. *pSize=sizeNeeded;
  190. return true;
  191. } else {
  192. return false;
  193. }
  194. } else {
  195. if(sizeNeeded<=*pSize) {
  196. /* there is already enough memory */
  197. return true;
  198. }
  199. else if(!mayAllocate) {
  200. /* not enough memory, and we must not allocate */
  201. return false;
  202. } else {
  203. /* we try to grow */
  204. void *memory;
  205. /* in most cases, we do not need the copy-old-data part of
  206. * realloc, but it is needed when adding runs using getRunsMemory()
  207. * in setParaRunsOnly()
  208. */
  209. if((memory=uprv_realloc(*pMemory, sizeNeeded))!=nullptr) {
  210. *pMemory=memory;
  211. *pSize=sizeNeeded;
  212. return true;
  213. } else {
  214. /* we failed to grow */
  215. return false;
  216. }
  217. }
  218. }
  219. }
  220. U_CAPI void U_EXPORT2
  221. ubidi_close(UBiDi *pBiDi) {
  222. if(pBiDi!=nullptr) {
  223. pBiDi->pParaBiDi=nullptr; /* in case one tries to reuse this block */
  224. if(pBiDi->dirPropsMemory!=nullptr) {
  225. uprv_free(pBiDi->dirPropsMemory);
  226. }
  227. if(pBiDi->levelsMemory!=nullptr) {
  228. uprv_free(pBiDi->levelsMemory);
  229. }
  230. if(pBiDi->openingsMemory!=nullptr) {
  231. uprv_free(pBiDi->openingsMemory);
  232. }
  233. if(pBiDi->parasMemory!=nullptr) {
  234. uprv_free(pBiDi->parasMemory);
  235. }
  236. if(pBiDi->runsMemory!=nullptr) {
  237. uprv_free(pBiDi->runsMemory);
  238. }
  239. if(pBiDi->isolatesMemory!=nullptr) {
  240. uprv_free(pBiDi->isolatesMemory);
  241. }
  242. if(pBiDi->insertPoints.points!=nullptr) {
  243. uprv_free(pBiDi->insertPoints.points);
  244. }
  245. uprv_free(pBiDi);
  246. }
  247. }
  248. /* set to approximate "inverse BiDi" ---------------------------------------- */
  249. U_CAPI void U_EXPORT2
  250. ubidi_setInverse(UBiDi *pBiDi, UBool isInverse) {
  251. if(pBiDi!=nullptr) {
  252. pBiDi->isInverse=isInverse;
  253. pBiDi->reorderingMode = isInverse ? UBIDI_REORDER_INVERSE_NUMBERS_AS_L
  254. : UBIDI_REORDER_DEFAULT;
  255. }
  256. }
  257. U_CAPI UBool U_EXPORT2
  258. ubidi_isInverse(UBiDi *pBiDi) {
  259. if(pBiDi!=nullptr) {
  260. return pBiDi->isInverse;
  261. } else {
  262. return false;
  263. }
  264. }
  265. /* FOOD FOR THOUGHT: currently the reordering modes are a mixture of
  266. * algorithm for direct BiDi, algorithm for inverse BiDi and the bizarre
  267. * concept of RUNS_ONLY which is a double operation.
  268. * It could be advantageous to divide this into 3 concepts:
  269. * a) Operation: direct / inverse / RUNS_ONLY
  270. * b) Direct algorithm: default / NUMBERS_SPECIAL / GROUP_NUMBERS_WITH_R
  271. * c) Inverse algorithm: default / INVERSE_LIKE_DIRECT / NUMBERS_SPECIAL
  272. * This would allow combinations not possible today like RUNS_ONLY with
  273. * NUMBERS_SPECIAL.
  274. * Also allow to set INSERT_MARKS for the direct step of RUNS_ONLY and
  275. * REMOVE_CONTROLS for the inverse step.
  276. * Not all combinations would be supported, and probably not all do make sense.
  277. * This would need to document which ones are supported and what are the
  278. * fallbacks for unsupported combinations.
  279. */
  280. U_CAPI void U_EXPORT2
  281. ubidi_setReorderingMode(UBiDi *pBiDi, UBiDiReorderingMode reorderingMode) UPRV_NO_SANITIZE_UNDEFINED {
  282. if ((pBiDi!=nullptr) && (reorderingMode >= UBIDI_REORDER_DEFAULT)
  283. && (reorderingMode < UBIDI_REORDER_COUNT)) {
  284. pBiDi->reorderingMode = reorderingMode;
  285. pBiDi->isInverse = (UBool)(reorderingMode == UBIDI_REORDER_INVERSE_NUMBERS_AS_L);
  286. }
  287. }
  288. U_CAPI UBiDiReorderingMode U_EXPORT2
  289. ubidi_getReorderingMode(UBiDi *pBiDi) {
  290. if (pBiDi!=nullptr) {
  291. return pBiDi->reorderingMode;
  292. } else {
  293. return UBIDI_REORDER_DEFAULT;
  294. }
  295. }
  296. U_CAPI void U_EXPORT2
  297. ubidi_setReorderingOptions(UBiDi *pBiDi, uint32_t reorderingOptions) {
  298. if (reorderingOptions & UBIDI_OPTION_REMOVE_CONTROLS) {
  299. reorderingOptions&=~UBIDI_OPTION_INSERT_MARKS;
  300. }
  301. if (pBiDi!=nullptr) {
  302. pBiDi->reorderingOptions=reorderingOptions;
  303. }
  304. }
  305. U_CAPI uint32_t U_EXPORT2
  306. ubidi_getReorderingOptions(UBiDi *pBiDi) {
  307. if (pBiDi!=nullptr) {
  308. return pBiDi->reorderingOptions;
  309. } else {
  310. return 0;
  311. }
  312. }
  313. U_CAPI UBiDiDirection U_EXPORT2
  314. ubidi_getBaseDirection(const char16_t *text,
  315. int32_t length){
  316. int32_t i;
  317. UChar32 uchar;
  318. UCharDirection dir;
  319. if( text==nullptr || length<-1 ){
  320. return UBIDI_NEUTRAL;
  321. }
  322. if(length==-1) {
  323. length=u_strlen(text);
  324. }
  325. for( i = 0 ; i < length; ) {
  326. /* i is incremented by U16_NEXT */
  327. U16_NEXT(text, i, length, uchar);
  328. dir = u_charDirection(uchar);
  329. if( dir == U_LEFT_TO_RIGHT )
  330. return UBIDI_LTR;
  331. if( dir == U_RIGHT_TO_LEFT || dir ==U_RIGHT_TO_LEFT_ARABIC )
  332. return UBIDI_RTL;
  333. }
  334. return UBIDI_NEUTRAL;
  335. }
  336. /* perform (P2)..(P3) ------------------------------------------------------- */
  337. /**
  338. * Returns the directionality of the first strong character
  339. * after the last B in prologue, if any.
  340. * Requires prologue!=null.
  341. */
  342. static DirProp
  343. firstL_R_AL(UBiDi *pBiDi) {
  344. const char16_t *text=pBiDi->prologue;
  345. int32_t length=pBiDi->proLength;
  346. int32_t i;
  347. UChar32 uchar;
  348. DirProp dirProp, result=ON;
  349. for(i=0; i<length; ) {
  350. /* i is incremented by U16_NEXT */
  351. U16_NEXT(text, i, length, uchar);
  352. dirProp=(DirProp)ubidi_getCustomizedClass(pBiDi, uchar);
  353. if(result==ON) {
  354. if(dirProp==L || dirProp==R || dirProp==AL) {
  355. result=dirProp;
  356. }
  357. } else {
  358. if(dirProp==B) {
  359. result=ON;
  360. }
  361. }
  362. }
  363. return result;
  364. }
  365. /*
  366. * Check that there are enough entries in the array pointed to by pBiDi->paras
  367. */
  368. static UBool
  369. checkParaCount(UBiDi *pBiDi) {
  370. int32_t count=pBiDi->paraCount;
  371. if(pBiDi->paras==pBiDi->simpleParas) {
  372. if(count<=SIMPLE_PARAS_COUNT)
  373. return true;
  374. if(!getInitialParasMemory(pBiDi, SIMPLE_PARAS_COUNT * 2))
  375. return false;
  376. pBiDi->paras=pBiDi->parasMemory;
  377. uprv_memcpy(pBiDi->parasMemory, pBiDi->simpleParas, SIMPLE_PARAS_COUNT * sizeof(Para));
  378. return true;
  379. }
  380. if(!getInitialParasMemory(pBiDi, count * 2))
  381. return false;
  382. pBiDi->paras=pBiDi->parasMemory;
  383. return true;
  384. }
  385. /*
  386. * Get the directional properties for the text, calculate the flags bit-set, and
  387. * determine the paragraph level if necessary (in pBiDi->paras[i].level).
  388. * FSI initiators are also resolved and their dirProp replaced with LRI or RLI.
  389. * When encountering an FSI, it is initially replaced with an LRI, which is the
  390. * default. Only if a strong R or AL is found within its scope will the LRI be
  391. * replaced by an RLI.
  392. */
  393. static UBool
  394. getDirProps(UBiDi *pBiDi) {
  395. const char16_t *text=pBiDi->text;
  396. DirProp *dirProps=pBiDi->dirPropsMemory; /* pBiDi->dirProps is const */
  397. int32_t i=0, originalLength=pBiDi->originalLength;
  398. Flags flags=0; /* collect all directionalities in the text */
  399. UChar32 uchar;
  400. DirProp dirProp=0, defaultParaLevel=0; /* initialize to avoid compiler warnings */
  401. UBool isDefaultLevel=IS_DEFAULT_LEVEL(pBiDi->paraLevel);
  402. /* for inverse BiDi, the default para level is set to RTL if there is a
  403. strong R or AL character at either end of the text */
  404. UBool isDefaultLevelInverse=isDefaultLevel && (UBool)
  405. (pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_LIKE_DIRECT ||
  406. pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL);
  407. int32_t lastArabicPos=-1;
  408. int32_t controlCount=0;
  409. UBool removeBiDiControls = (UBool)(pBiDi->reorderingOptions &
  410. UBIDI_OPTION_REMOVE_CONTROLS);
  411. enum State {
  412. NOT_SEEKING_STRONG, /* 0: not contextual paraLevel, not after FSI */
  413. SEEKING_STRONG_FOR_PARA, /* 1: looking for first strong char in para */
  414. SEEKING_STRONG_FOR_FSI, /* 2: looking for first strong after FSI */
  415. LOOKING_FOR_PDI /* 3: found strong after FSI, looking for PDI */
  416. };
  417. State state;
  418. DirProp lastStrong=ON; /* for default level & inverse BiDi */
  419. /* The following stacks are used to manage isolate sequences. Those
  420. sequences may be nested, but obviously never more deeply than the
  421. maximum explicit embedding level.
  422. lastStack is the index of the last used entry in the stack. A value of -1
  423. means that there is no open isolate sequence.
  424. lastStack is reset to -1 on paragraph boundaries. */
  425. /* The following stack contains the position of the initiator of
  426. each open isolate sequence */
  427. int32_t isolateStartStack[UBIDI_MAX_EXPLICIT_LEVEL+1];
  428. /* The following stack contains the last known state before
  429. encountering the initiator of an isolate sequence */
  430. State previousStateStack[UBIDI_MAX_EXPLICIT_LEVEL+1];
  431. int32_t stackLast=-1;
  432. if(pBiDi->reorderingOptions & UBIDI_OPTION_STREAMING)
  433. pBiDi->length=0;
  434. defaultParaLevel=pBiDi->paraLevel&1;
  435. if(isDefaultLevel) {
  436. pBiDi->paras[0].level=defaultParaLevel;
  437. lastStrong=defaultParaLevel;
  438. if(pBiDi->proLength>0 && /* there is a prologue */
  439. (dirProp=firstL_R_AL(pBiDi))!=ON) { /* with a strong character */
  440. if(dirProp==L)
  441. pBiDi->paras[0].level=0; /* set the default para level */
  442. else
  443. pBiDi->paras[0].level=1; /* set the default para level */
  444. state=NOT_SEEKING_STRONG;
  445. } else {
  446. state=SEEKING_STRONG_FOR_PARA;
  447. }
  448. } else {
  449. pBiDi->paras[0].level=pBiDi->paraLevel;
  450. state=NOT_SEEKING_STRONG;
  451. }
  452. /* count paragraphs and determine the paragraph level (P2..P3) */
  453. /*
  454. * see comment in ubidi.h:
  455. * the UBIDI_DEFAULT_XXX values are designed so that
  456. * their bit 0 alone yields the intended default
  457. */
  458. for( /* i=0 above */ ; i<originalLength; ) {
  459. /* i is incremented by U16_NEXT */
  460. U16_NEXT(text, i, originalLength, uchar);
  461. flags|=DIRPROP_FLAG(dirProp=(DirProp)ubidi_getCustomizedClass(pBiDi, uchar));
  462. dirProps[i-1]=dirProp;
  463. if(uchar>0xffff) { /* set the lead surrogate's property to BN */
  464. flags|=DIRPROP_FLAG(BN);
  465. dirProps[i-2]=BN;
  466. }
  467. if(removeBiDiControls && IS_BIDI_CONTROL_CHAR(uchar))
  468. controlCount++;
  469. if(dirProp==L) {
  470. if(state==SEEKING_STRONG_FOR_PARA) {
  471. pBiDi->paras[pBiDi->paraCount-1].level=0;
  472. state=NOT_SEEKING_STRONG;
  473. }
  474. else if(state==SEEKING_STRONG_FOR_FSI) {
  475. if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL) {
  476. /* no need for next statement, already set by default */
  477. /* dirProps[isolateStartStack[stackLast]]=LRI; */
  478. flags|=DIRPROP_FLAG(LRI);
  479. }
  480. state=LOOKING_FOR_PDI;
  481. }
  482. lastStrong=L;
  483. continue;
  484. }
  485. if(dirProp==R || dirProp==AL) {
  486. if(state==SEEKING_STRONG_FOR_PARA) {
  487. pBiDi->paras[pBiDi->paraCount-1].level=1;
  488. state=NOT_SEEKING_STRONG;
  489. }
  490. else if(state==SEEKING_STRONG_FOR_FSI) {
  491. if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL) {
  492. dirProps[isolateStartStack[stackLast]]=RLI;
  493. flags|=DIRPROP_FLAG(RLI);
  494. }
  495. state=LOOKING_FOR_PDI;
  496. }
  497. lastStrong=R;
  498. if(dirProp==AL)
  499. lastArabicPos=i-1;
  500. continue;
  501. }
  502. if(dirProp>=FSI && dirProp<=RLI) { /* FSI, LRI or RLI */
  503. stackLast++;
  504. if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL) {
  505. isolateStartStack[stackLast]=i-1;
  506. previousStateStack[stackLast]=state;
  507. }
  508. if(dirProp==FSI) {
  509. dirProps[i-1]=LRI; /* default if no strong char */
  510. state=SEEKING_STRONG_FOR_FSI;
  511. }
  512. else
  513. state=LOOKING_FOR_PDI;
  514. continue;
  515. }
  516. if(dirProp==PDI) {
  517. if(state==SEEKING_STRONG_FOR_FSI) {
  518. if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL) {
  519. /* no need for next statement, already set by default */
  520. /* dirProps[isolateStartStack[stackLast]]=LRI; */
  521. flags|=DIRPROP_FLAG(LRI);
  522. }
  523. }
  524. if(stackLast>=0) {
  525. if(stackLast<=UBIDI_MAX_EXPLICIT_LEVEL)
  526. state=previousStateStack[stackLast];
  527. stackLast--;
  528. }
  529. continue;
  530. }
  531. if(dirProp==B) {
  532. if(i<originalLength && uchar==CR && text[i]==LF) /* do nothing on the CR */
  533. continue;
  534. pBiDi->paras[pBiDi->paraCount-1].limit=i;
  535. if(isDefaultLevelInverse && lastStrong==R)
  536. pBiDi->paras[pBiDi->paraCount-1].level=1;
  537. if(pBiDi->reorderingOptions & UBIDI_OPTION_STREAMING) {
  538. /* When streaming, we only process whole paragraphs
  539. thus some updates are only done on paragraph boundaries */
  540. pBiDi->length=i; /* i is index to next character */
  541. pBiDi->controlCount=controlCount;
  542. }
  543. if(i<originalLength) { /* B not last char in text */
  544. pBiDi->paraCount++;
  545. if(checkParaCount(pBiDi)==false) /* not enough memory for a new para entry */
  546. return false;
  547. if(isDefaultLevel) {
  548. pBiDi->paras[pBiDi->paraCount-1].level=defaultParaLevel;
  549. state=SEEKING_STRONG_FOR_PARA;
  550. lastStrong=defaultParaLevel;
  551. } else {
  552. pBiDi->paras[pBiDi->paraCount-1].level=pBiDi->paraLevel;
  553. state=NOT_SEEKING_STRONG;
  554. }
  555. stackLast=-1;
  556. }
  557. continue;
  558. }
  559. }
  560. /* Ignore still open isolate sequences with overflow */
  561. if(stackLast>UBIDI_MAX_EXPLICIT_LEVEL) {
  562. stackLast=UBIDI_MAX_EXPLICIT_LEVEL;
  563. state=SEEKING_STRONG_FOR_FSI; /* to be on the safe side */
  564. }
  565. /* Resolve direction of still unresolved open FSI sequences */
  566. while(stackLast>=0) {
  567. if(state==SEEKING_STRONG_FOR_FSI) {
  568. /* no need for next statement, already set by default */
  569. /* dirProps[isolateStartStack[stackLast]]=LRI; */
  570. flags|=DIRPROP_FLAG(LRI);
  571. break;
  572. }
  573. state=previousStateStack[stackLast];
  574. stackLast--;
  575. }
  576. /* When streaming, ignore text after the last paragraph separator */
  577. if(pBiDi->reorderingOptions & UBIDI_OPTION_STREAMING) {
  578. if(pBiDi->length<originalLength)
  579. pBiDi->paraCount--;
  580. } else {
  581. pBiDi->paras[pBiDi->paraCount-1].limit=originalLength;
  582. pBiDi->controlCount=controlCount;
  583. }
  584. /* For inverse bidi, default para direction is RTL if there is
  585. a strong R or AL at either end of the paragraph */
  586. if(isDefaultLevelInverse && lastStrong==R) {
  587. pBiDi->paras[pBiDi->paraCount-1].level=1;
  588. }
  589. if(isDefaultLevel) {
  590. pBiDi->paraLevel=static_cast<UBiDiLevel>(pBiDi->paras[0].level);
  591. }
  592. /* The following is needed to resolve the text direction for default level
  593. paragraphs containing no strong character */
  594. for(i=0; i<pBiDi->paraCount; i++)
  595. flags|=DIRPROP_FLAG_LR(pBiDi->paras[i].level);
  596. if(pBiDi->orderParagraphsLTR && (flags&DIRPROP_FLAG(B))) {
  597. flags|=DIRPROP_FLAG(L);
  598. }
  599. pBiDi->flags=flags;
  600. pBiDi->lastArabicPos=lastArabicPos;
  601. return true;
  602. }
  603. /* determine the paragraph level at position index */
  604. U_CFUNC UBiDiLevel
  605. ubidi_getParaLevelAtIndex(const UBiDi *pBiDi, int32_t pindex) {
  606. int32_t i;
  607. for(i=0; i<pBiDi->paraCount; i++)
  608. if(pindex<pBiDi->paras[i].limit)
  609. break;
  610. if(i>=pBiDi->paraCount)
  611. i=pBiDi->paraCount-1;
  612. return (UBiDiLevel)(pBiDi->paras[i].level);
  613. }
  614. /* Functions for handling paired brackets ----------------------------------- */
  615. /* In the isoRuns array, the first entry is used for text outside of any
  616. isolate sequence. Higher entries are used for each more deeply nested
  617. isolate sequence. isoRunLast is the index of the last used entry. The
  618. openings array is used to note the data of opening brackets not yet
  619. matched by a closing bracket, or matched but still susceptible to change
  620. level.
  621. Each isoRun entry contains the index of the first and
  622. one-after-last openings entries for pending opening brackets it
  623. contains. The next openings entry to use is the one-after-last of the
  624. most deeply nested isoRun entry.
  625. isoRun entries also contain their current embedding level and the last
  626. encountered strong character, since these will be needed to resolve
  627. the level of paired brackets. */
  628. static void
  629. bracketInit(UBiDi *pBiDi, BracketData *bd) {
  630. bd->pBiDi=pBiDi;
  631. bd->isoRunLast=0;
  632. bd->isoRuns[0].start=0;
  633. bd->isoRuns[0].limit=0;
  634. bd->isoRuns[0].level=GET_PARALEVEL(pBiDi, 0);
  635. UBiDiLevel t = GET_PARALEVEL(pBiDi, 0) & 1;
  636. bd->isoRuns[0].lastStrong = bd->isoRuns[0].lastBase = t;
  637. bd->isoRuns[0].contextDir = (UBiDiDirection)t;
  638. bd->isoRuns[0].contextPos=0;
  639. if(pBiDi->openingsMemory) {
  640. bd->openings=pBiDi->openingsMemory;
  641. bd->openingsCount=pBiDi->openingsSize / sizeof(Opening);
  642. } else {
  643. bd->openings=bd->simpleOpenings;
  644. bd->openingsCount=SIMPLE_OPENINGS_COUNT;
  645. }
  646. bd->isNumbersSpecial=bd->pBiDi->reorderingMode==UBIDI_REORDER_NUMBERS_SPECIAL ||
  647. bd->pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL;
  648. }
  649. /* paragraph boundary */
  650. static void
  651. bracketProcessB(BracketData *bd, UBiDiLevel level) {
  652. bd->isoRunLast=0;
  653. bd->isoRuns[0].limit=0;
  654. bd->isoRuns[0].level=level;
  655. bd->isoRuns[0].lastStrong=bd->isoRuns[0].lastBase=level&1;
  656. bd->isoRuns[0].contextDir=(UBiDiDirection)(level&1);
  657. bd->isoRuns[0].contextPos=0;
  658. }
  659. /* LRE, LRO, RLE, RLO, PDF */
  660. static void
  661. bracketProcessBoundary(BracketData *bd, int32_t lastCcPos,
  662. UBiDiLevel contextLevel, UBiDiLevel embeddingLevel) {
  663. IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast];
  664. DirProp *dirProps=bd->pBiDi->dirProps;
  665. if(DIRPROP_FLAG(dirProps[lastCcPos])&MASK_ISO) /* after an isolate */
  666. return;
  667. if(NO_OVERRIDE(embeddingLevel)>NO_OVERRIDE(contextLevel)) /* not a PDF */
  668. contextLevel=embeddingLevel;
  669. pLastIsoRun->limit=pLastIsoRun->start;
  670. pLastIsoRun->level=embeddingLevel;
  671. pLastIsoRun->lastStrong=pLastIsoRun->lastBase=contextLevel&1;
  672. pLastIsoRun->contextDir=(UBiDiDirection)(contextLevel&1);
  673. pLastIsoRun->contextPos=(UBiDiDirection)lastCcPos;
  674. }
  675. /* LRI or RLI */
  676. static void
  677. bracketProcessLRI_RLI(BracketData *bd, UBiDiLevel level) {
  678. IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast];
  679. int16_t lastLimit;
  680. pLastIsoRun->lastBase=ON;
  681. lastLimit=pLastIsoRun->limit;
  682. bd->isoRunLast++;
  683. pLastIsoRun++;
  684. pLastIsoRun->start=pLastIsoRun->limit=lastLimit;
  685. pLastIsoRun->level=level;
  686. pLastIsoRun->lastStrong=pLastIsoRun->lastBase=level&1;
  687. pLastIsoRun->contextDir=(UBiDiDirection)(level&1);
  688. pLastIsoRun->contextPos=0;
  689. }
  690. /* PDI */
  691. static void
  692. bracketProcessPDI(BracketData *bd) {
  693. IsoRun *pLastIsoRun;
  694. bd->isoRunLast--;
  695. pLastIsoRun=&bd->isoRuns[bd->isoRunLast];
  696. pLastIsoRun->lastBase=ON;
  697. }
  698. /* newly found opening bracket: create an openings entry */
  699. static UBool /* return true if success */
  700. bracketAddOpening(BracketData *bd, char16_t match, int32_t position) {
  701. IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast];
  702. Opening *pOpening;
  703. if(pLastIsoRun->limit>=bd->openingsCount) { /* no available new entry */
  704. UBiDi *pBiDi=bd->pBiDi;
  705. if(!getInitialOpeningsMemory(pBiDi, pLastIsoRun->limit * 2))
  706. return false;
  707. if(bd->openings==bd->simpleOpenings)
  708. uprv_memcpy(pBiDi->openingsMemory, bd->simpleOpenings,
  709. SIMPLE_OPENINGS_COUNT * sizeof(Opening));
  710. bd->openings=pBiDi->openingsMemory; /* may have changed */
  711. bd->openingsCount=pBiDi->openingsSize / sizeof(Opening);
  712. }
  713. pOpening=&bd->openings[pLastIsoRun->limit];
  714. pOpening->position=position;
  715. pOpening->match=match;
  716. pOpening->contextDir=pLastIsoRun->contextDir;
  717. pOpening->contextPos=pLastIsoRun->contextPos;
  718. pOpening->flags=0;
  719. pLastIsoRun->limit++;
  720. return true;
  721. }
  722. /* change N0c1 to N0c2 when a preceding bracket is assigned the embedding level */
  723. static void
  724. fixN0c(BracketData *bd, int32_t openingIndex, int32_t newPropPosition, DirProp newProp) {
  725. /* This function calls itself recursively */
  726. IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast];
  727. Opening *qOpening;
  728. DirProp *dirProps=bd->pBiDi->dirProps;
  729. int32_t k, openingPosition, closingPosition;
  730. for(k=openingIndex+1, qOpening=&bd->openings[k]; k<pLastIsoRun->limit; k++, qOpening++) {
  731. if(qOpening->match>=0) /* not an N0c match */
  732. continue;
  733. if(newPropPosition<qOpening->contextPos)
  734. break;
  735. if(newPropPosition>=qOpening->position)
  736. continue;
  737. if(newProp==qOpening->contextDir)
  738. break;
  739. openingPosition=qOpening->position;
  740. dirProps[openingPosition]=newProp;
  741. closingPosition=-(qOpening->match);
  742. dirProps[closingPosition]=newProp;
  743. qOpening->match=0; /* prevent further changes */
  744. fixN0c(bd, k, openingPosition, newProp);
  745. fixN0c(bd, k, closingPosition, newProp);
  746. }
  747. }
  748. /* process closing bracket */
  749. static DirProp /* return L or R if N0b or N0c, ON if N0d */
  750. bracketProcessClosing(BracketData *bd, int32_t openIdx, int32_t position) {
  751. IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast];
  752. Opening *pOpening, *qOpening;
  753. UBiDiDirection direction;
  754. UBool stable;
  755. DirProp newProp;
  756. pOpening=&bd->openings[openIdx];
  757. direction=(UBiDiDirection)(pLastIsoRun->level&1);
  758. stable=true; /* assume stable until proved otherwise */
  759. /* The stable flag is set when brackets are paired and their
  760. level is resolved and cannot be changed by what will be
  761. found later in the source string.
  762. An unstable match can occur only when applying N0c, where
  763. the resolved level depends on the preceding context, and
  764. this context may be affected by text occurring later.
  765. Example: RTL paragraph containing: abc[(latin) HEBREW]
  766. When the closing parenthesis is encountered, it appears
  767. that N0c1 must be applied since 'abc' sets an opposite
  768. direction context and both parentheses receive level 2.
  769. However, when the closing square bracket is processed,
  770. N0b applies because of 'HEBREW' being included within the
  771. brackets, thus the square brackets are treated like R and
  772. receive level 1. However, this changes the preceding
  773. context of the opening parenthesis, and it now appears
  774. that N0c2 must be applied to the parentheses rather than
  775. N0c1. */
  776. if((direction==0 && pOpening->flags&FOUND_L) ||
  777. (direction==1 && pOpening->flags&FOUND_R)) { /* N0b */
  778. newProp=static_cast<DirProp>(direction);
  779. }
  780. else if(pOpening->flags&(FOUND_L|FOUND_R)) { /* N0c */
  781. /* it is stable if there is no containing pair or in
  782. conditions too complicated and not worth checking */
  783. stable=(openIdx==pLastIsoRun->start);
  784. if(direction!=pOpening->contextDir)
  785. newProp= static_cast<DirProp>(pOpening->contextDir); /* N0c1 */
  786. else
  787. newProp= static_cast<DirProp>(direction); /* N0c2 */
  788. } else {
  789. /* forget this and any brackets nested within this pair */
  790. pLastIsoRun->limit= static_cast<uint16_t>(openIdx);
  791. return ON; /* N0d */
  792. }
  793. bd->pBiDi->dirProps[pOpening->position]=newProp;
  794. bd->pBiDi->dirProps[position]=newProp;
  795. /* Update nested N0c pairs that may be affected */
  796. fixN0c(bd, openIdx, pOpening->position, newProp);
  797. if(stable) {
  798. pLastIsoRun->limit= static_cast<uint16_t>(openIdx); /* forget any brackets nested within this pair */
  799. /* remove lower located synonyms if any */
  800. while(pLastIsoRun->limit>pLastIsoRun->start &&
  801. bd->openings[pLastIsoRun->limit-1].position==pOpening->position)
  802. pLastIsoRun->limit--;
  803. } else {
  804. int32_t k;
  805. pOpening->match=-position;
  806. /* neutralize lower located synonyms if any */
  807. k=openIdx-1;
  808. while(k>=pLastIsoRun->start &&
  809. bd->openings[k].position==pOpening->position)
  810. bd->openings[k--].match=0;
  811. /* neutralize any unmatched opening between the current pair;
  812. this will also neutralize higher located synonyms if any */
  813. for(k=openIdx+1; k<pLastIsoRun->limit; k++) {
  814. qOpening=&bd->openings[k];
  815. if(qOpening->position>=position)
  816. break;
  817. if(qOpening->match>0)
  818. qOpening->match=0;
  819. }
  820. }
  821. return newProp;
  822. }
  823. /* handle strong characters, digits and candidates for closing brackets */
  824. static UBool /* return true if success */
  825. bracketProcessChar(BracketData *bd, int32_t position) {
  826. IsoRun *pLastIsoRun=&bd->isoRuns[bd->isoRunLast];
  827. DirProp *dirProps, dirProp, newProp;
  828. UBiDiLevel level;
  829. dirProps=bd->pBiDi->dirProps;
  830. dirProp=dirProps[position];
  831. if(dirProp==ON) {
  832. char16_t c, match;
  833. int32_t idx;
  834. /* First see if it is a matching closing bracket. Hopefully, this is
  835. more efficient than checking if it is a closing bracket at all */
  836. c=bd->pBiDi->text[position];
  837. for(idx=pLastIsoRun->limit-1; idx>=pLastIsoRun->start; idx--) {
  838. if(bd->openings[idx].match!=c)
  839. continue;
  840. /* We have a match */
  841. newProp=bracketProcessClosing(bd, idx, position);
  842. if(newProp==ON) { /* N0d */
  843. c=0; /* prevent handling as an opening */
  844. break;
  845. }
  846. pLastIsoRun->lastBase=ON;
  847. pLastIsoRun->contextDir=(UBiDiDirection)newProp;
  848. pLastIsoRun->contextPos=position;
  849. level=bd->pBiDi->levels[position];
  850. if(level&UBIDI_LEVEL_OVERRIDE) { /* X4, X5 */
  851. uint16_t flag;
  852. int32_t i;
  853. newProp=level&1;
  854. pLastIsoRun->lastStrong=newProp;
  855. flag=DIRPROP_FLAG(newProp);
  856. for(i=pLastIsoRun->start; i<idx; i++)
  857. bd->openings[i].flags|=flag;
  858. /* matching brackets are not overridden by LRO/RLO */
  859. bd->pBiDi->levels[position]&=~UBIDI_LEVEL_OVERRIDE;
  860. }
  861. /* matching brackets are not overridden by LRO/RLO */
  862. bd->pBiDi->levels[bd->openings[idx].position]&=~UBIDI_LEVEL_OVERRIDE;
  863. return true;
  864. }
  865. /* We get here only if the ON character is not a matching closing
  866. bracket or it is a case of N0d */
  867. /* Now see if it is an opening bracket */
  868. if(c)
  869. match= static_cast<char16_t>(u_getBidiPairedBracket(c)); /* get the matching char */
  870. else
  871. match=0;
  872. if(match!=c && /* has a matching char */
  873. ubidi_getPairedBracketType(c)==U_BPT_OPEN) { /* opening bracket */
  874. /* special case: process synonyms
  875. create an opening entry for each synonym */
  876. if(match==0x232A) { /* RIGHT-POINTING ANGLE BRACKET */
  877. if(!bracketAddOpening(bd, 0x3009, position))
  878. return false;
  879. }
  880. else if(match==0x3009) { /* RIGHT ANGLE BRACKET */
  881. if(!bracketAddOpening(bd, 0x232A, position))
  882. return false;
  883. }
  884. if(!bracketAddOpening(bd, match, position))
  885. return false;
  886. }
  887. }
  888. level=bd->pBiDi->levels[position];
  889. if(level&UBIDI_LEVEL_OVERRIDE) { /* X4, X5 */
  890. newProp=level&1;
  891. if(dirProp!=S && dirProp!=WS && dirProp!=ON)
  892. dirProps[position]=newProp;
  893. pLastIsoRun->lastBase=newProp;
  894. pLastIsoRun->lastStrong=newProp;
  895. pLastIsoRun->contextDir=(UBiDiDirection)newProp;
  896. pLastIsoRun->contextPos=position;
  897. }
  898. else if(dirProp<=R || dirProp==AL) {
  899. newProp= static_cast<DirProp>(DIR_FROM_STRONG(dirProp));
  900. pLastIsoRun->lastBase=dirProp;
  901. pLastIsoRun->lastStrong=dirProp;
  902. pLastIsoRun->contextDir=(UBiDiDirection)newProp;
  903. pLastIsoRun->contextPos=position;
  904. }
  905. else if(dirProp==EN) {
  906. pLastIsoRun->lastBase=EN;
  907. if(pLastIsoRun->lastStrong==L) {
  908. newProp=L; /* W7 */
  909. if(!bd->isNumbersSpecial)
  910. dirProps[position]=ENL;
  911. pLastIsoRun->contextDir=(UBiDiDirection)L;
  912. pLastIsoRun->contextPos=position;
  913. }
  914. else {
  915. newProp=R; /* N0 */
  916. if(pLastIsoRun->lastStrong==AL)
  917. dirProps[position]=AN; /* W2 */
  918. else
  919. dirProps[position]=ENR;
  920. pLastIsoRun->contextDir=(UBiDiDirection)R;
  921. pLastIsoRun->contextPos=position;
  922. }
  923. }
  924. else if(dirProp==AN) {
  925. newProp=R; /* N0 */
  926. pLastIsoRun->lastBase=AN;
  927. pLastIsoRun->contextDir=(UBiDiDirection)R;
  928. pLastIsoRun->contextPos=position;
  929. }
  930. else if(dirProp==NSM) {
  931. /* if the last real char was ON, change NSM to ON so that it
  932. will stay ON even if the last real char is a bracket which
  933. may be changed to L or R */
  934. newProp=pLastIsoRun->lastBase;
  935. if(newProp==ON)
  936. dirProps[position]=newProp;
  937. }
  938. else {
  939. newProp=dirProp;
  940. pLastIsoRun->lastBase=dirProp;
  941. }
  942. if(newProp<=R || newProp==AL) {
  943. int32_t i;
  944. uint16_t flag=DIRPROP_FLAG(DIR_FROM_STRONG(newProp));
  945. for(i=pLastIsoRun->start; i<pLastIsoRun->limit; i++)
  946. if(position>bd->openings[i].position)
  947. bd->openings[i].flags|=flag;
  948. }
  949. return true;
  950. }
  951. /* perform (X1)..(X9) ------------------------------------------------------- */
  952. /* determine if the text is mixed-directional or single-directional */
  953. static UBiDiDirection
  954. directionFromFlags(UBiDi *pBiDi) {
  955. Flags flags=pBiDi->flags;
  956. /* if the text contains AN and neutrals, then some neutrals may become RTL */
  957. if(!(flags&MASK_RTL || ((flags&DIRPROP_FLAG(AN)) && (flags&MASK_POSSIBLE_N)))) {
  958. return UBIDI_LTR;
  959. } else if(!(flags&MASK_LTR)) {
  960. return UBIDI_RTL;
  961. } else {
  962. return UBIDI_MIXED;
  963. }
  964. }
  965. /*
  966. * Resolve the explicit levels as specified by explicit embedding codes.
  967. * Recalculate the flags to have them reflect the real properties
  968. * after taking the explicit embeddings into account.
  969. *
  970. * The BiDi algorithm is designed to result in the same behavior whether embedding
  971. * levels are externally specified (from "styled text", supposedly the preferred
  972. * method) or set by explicit embedding codes (LRx, RLx, PDF, FSI, PDI) in the plain text.
  973. * That is why (X9) instructs to remove all not-isolate explicit codes (and BN).
  974. * However, in a real implementation, the removal of these codes and their index
  975. * positions in the plain text is undesirable since it would result in
  976. * reallocated, reindexed text.
  977. * Instead, this implementation leaves the codes in there and just ignores them
  978. * in the subsequent processing.
  979. * In order to get the same reordering behavior, positions with a BN or a not-isolate
  980. * explicit embedding code just get the same level assigned as the last "real"
  981. * character.
  982. *
  983. * Some implementations, not this one, then overwrite some of these
  984. * directionality properties at "real" same-level-run boundaries by
  985. * L or R codes so that the resolution of weak types can be performed on the
  986. * entire paragraph at once instead of having to parse it once more and
  987. * perform that resolution on same-level-runs.
  988. * This limits the scope of the implicit rules in effectively
  989. * the same way as the run limits.
  990. *
  991. * Instead, this implementation does not modify these codes, except for
  992. * paired brackets whose properties (ON) may be replaced by L or R.
  993. * On one hand, the paragraph has to be scanned for same-level-runs, but
  994. * on the other hand, this saves another loop to reset these codes,
  995. * or saves making and modifying a copy of dirProps[].
  996. *
  997. *
  998. * Note that (Pn) and (Xn) changed significantly from version 4 of the BiDi algorithm.
  999. *
  1000. *
  1001. * Handling the stack of explicit levels (Xn):
  1002. *
  1003. * With the BiDi stack of explicit levels, as pushed with each
  1004. * LRE, RLE, LRO, RLO, LRI, RLI and FSI and popped with each PDF and PDI,
  1005. * the explicit level must never exceed UBIDI_MAX_EXPLICIT_LEVEL.
  1006. *
  1007. * In order to have a correct push-pop semantics even in the case of overflows,
  1008. * overflow counters and a valid isolate counter are used as described in UAX#9
  1009. * section 3.3.2 "Explicit Levels and Directions".
  1010. *
  1011. * This implementation assumes that UBIDI_MAX_EXPLICIT_LEVEL is odd.
  1012. *
  1013. * Returns normally the direction; -1 if there was a memory shortage
  1014. *
  1015. */
  1016. static UBiDiDirection
  1017. resolveExplicitLevels(UBiDi *pBiDi, UErrorCode *pErrorCode) {
  1018. DirProp *dirProps=pBiDi->dirProps;
  1019. UBiDiLevel *levels=pBiDi->levels;
  1020. const char16_t *text=pBiDi->text;
  1021. int32_t i=0, length=pBiDi->length;
  1022. Flags flags=pBiDi->flags; /* collect all directionalities in the text */
  1023. DirProp dirProp;
  1024. UBiDiLevel level=GET_PARALEVEL(pBiDi, 0);
  1025. UBiDiDirection direction;
  1026. pBiDi->isolateCount=0;
  1027. if(U_FAILURE(*pErrorCode)) { return UBIDI_LTR; }
  1028. /* determine if the text is mixed-directional or single-directional */
  1029. direction=directionFromFlags(pBiDi);
  1030. /* we may not need to resolve any explicit levels */
  1031. if((direction!=UBIDI_MIXED)) {
  1032. /* not mixed directionality: levels don't matter - trailingWSStart will be 0 */
  1033. return direction;
  1034. }
  1035. if(pBiDi->reorderingMode > UBIDI_REORDER_LAST_LOGICAL_TO_VISUAL) {
  1036. /* inverse BiDi: mixed, but all characters are at the same embedding level */
  1037. /* set all levels to the paragraph level */
  1038. int32_t paraIndex, start, limit;
  1039. for(paraIndex=0; paraIndex<pBiDi->paraCount; paraIndex++) {
  1040. if(paraIndex==0)
  1041. start=0;
  1042. else
  1043. start=pBiDi->paras[paraIndex-1].limit;
  1044. limit=pBiDi->paras[paraIndex].limit;
  1045. level= static_cast<UBiDiLevel>(pBiDi->paras[paraIndex].level);
  1046. for(i=start; i<limit; i++)
  1047. levels[i]=level;
  1048. }
  1049. return direction; /* no bracket matching for inverse BiDi */
  1050. }
  1051. if(!(flags&(MASK_EXPLICIT|MASK_ISO))) {
  1052. /* no embeddings, set all levels to the paragraph level */
  1053. /* we still have to perform bracket matching */
  1054. int32_t paraIndex, start, limit;
  1055. BracketData bracketData;
  1056. bracketInit(pBiDi, &bracketData);
  1057. for(paraIndex=0; paraIndex<pBiDi->paraCount; paraIndex++) {
  1058. if(paraIndex==0)
  1059. start=0;
  1060. else
  1061. start=pBiDi->paras[paraIndex-1].limit;
  1062. limit=pBiDi->paras[paraIndex].limit;
  1063. level= static_cast<UBiDiLevel>(pBiDi->paras[paraIndex].level);
  1064. for(i=start; i<limit; i++) {
  1065. levels[i]=level;
  1066. dirProp=dirProps[i];
  1067. if(dirProp==BN)
  1068. continue;
  1069. if(dirProp==B) {
  1070. if((i+1)<length) {
  1071. if(text[i]==CR && text[i+1]==LF)
  1072. continue; /* skip CR when followed by LF */
  1073. bracketProcessB(&bracketData, level);
  1074. }
  1075. continue;
  1076. }
  1077. if(!bracketProcessChar(&bracketData, i)) {
  1078. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  1079. return UBIDI_LTR;
  1080. }
  1081. }
  1082. }
  1083. return direction;
  1084. }
  1085. {
  1086. /* continue to perform (Xn) */
  1087. /* (X1) level is set for all codes, embeddingLevel keeps track of the push/pop operations */
  1088. /* both variables may carry the UBIDI_LEVEL_OVERRIDE flag to indicate the override status */
  1089. UBiDiLevel embeddingLevel=level, newLevel;
  1090. UBiDiLevel previousLevel=level; /* previous level for regular (not CC) characters */
  1091. int32_t lastCcPos=0; /* index of last effective LRx,RLx, PDx */
  1092. /* The following stack remembers the embedding level and the ISOLATE flag of level runs.
  1093. stackLast points to its current entry. */
  1094. uint16_t stack[UBIDI_MAX_EXPLICIT_LEVEL+2]; /* we never push anything >=UBIDI_MAX_EXPLICIT_LEVEL
  1095. but we need one more entry as base */
  1096. uint32_t stackLast=0;
  1097. int32_t overflowIsolateCount=0;
  1098. int32_t overflowEmbeddingCount=0;
  1099. int32_t validIsolateCount=0;
  1100. BracketData bracketData;
  1101. bracketInit(pBiDi, &bracketData);
  1102. stack[0]=level; /* initialize base entry to para level, no override, no isolate */
  1103. /* recalculate the flags */
  1104. flags=0;
  1105. for(i=0; i<length; ++i) {
  1106. dirProp=dirProps[i];
  1107. switch(dirProp) {
  1108. case LRE:
  1109. case RLE:
  1110. case LRO:
  1111. case RLO:
  1112. /* (X2, X3, X4, X5) */
  1113. flags|=DIRPROP_FLAG(BN);
  1114. levels[i]=previousLevel;
  1115. if (dirProp==LRE || dirProp==LRO)
  1116. /* least greater even level */
  1117. newLevel=(UBiDiLevel)((embeddingLevel+2)&~(UBIDI_LEVEL_OVERRIDE|1));
  1118. else
  1119. /* least greater odd level */
  1120. newLevel=(UBiDiLevel)((NO_OVERRIDE(embeddingLevel)+1)|1);
  1121. if(newLevel<=UBIDI_MAX_EXPLICIT_LEVEL && overflowIsolateCount==0 &&
  1122. overflowEmbeddingCount==0) {
  1123. lastCcPos=i;
  1124. embeddingLevel=newLevel;
  1125. if(dirProp==LRO || dirProp==RLO)
  1126. embeddingLevel|=UBIDI_LEVEL_OVERRIDE;
  1127. stackLast++;
  1128. stack[stackLast]=embeddingLevel;
  1129. /* we don't need to set UBIDI_LEVEL_OVERRIDE off for LRE and RLE
  1130. since this has already been done for newLevel which is
  1131. the source for embeddingLevel.
  1132. */
  1133. } else {
  1134. if(overflowIsolateCount==0)
  1135. overflowEmbeddingCount++;
  1136. }
  1137. break;
  1138. case PDF:
  1139. /* (X7) */
  1140. flags|=DIRPROP_FLAG(BN);
  1141. levels[i]=previousLevel;
  1142. /* handle all the overflow cases first */
  1143. if(overflowIsolateCount) {
  1144. break;
  1145. }
  1146. if(overflowEmbeddingCount) {
  1147. overflowEmbeddingCount--;
  1148. break;
  1149. }
  1150. if(stackLast>0 && stack[stackLast]<ISOLATE) { /* not an isolate entry */
  1151. lastCcPos=i;
  1152. stackLast--;
  1153. embeddingLevel=(UBiDiLevel)stack[stackLast];
  1154. }
  1155. break;
  1156. case LRI:
  1157. case RLI:
  1158. flags|=(DIRPROP_FLAG(ON)|DIRPROP_FLAG_LR(embeddingLevel));
  1159. levels[i]=NO_OVERRIDE(embeddingLevel);
  1160. if(NO_OVERRIDE(embeddingLevel)!=NO_OVERRIDE(previousLevel)) {
  1161. bracketProcessBoundary(&bracketData, lastCcPos,
  1162. previousLevel, embeddingLevel);
  1163. flags|=DIRPROP_FLAG_MULTI_RUNS;
  1164. }
  1165. previousLevel=embeddingLevel;
  1166. /* (X5a, X5b) */
  1167. if(dirProp==LRI)
  1168. /* least greater even level */
  1169. newLevel=(UBiDiLevel)((embeddingLevel+2)&~(UBIDI_LEVEL_OVERRIDE|1));
  1170. else
  1171. /* least greater odd level */
  1172. newLevel=(UBiDiLevel)((NO_OVERRIDE(embeddingLevel)+1)|1);
  1173. if(newLevel<=UBIDI_MAX_EXPLICIT_LEVEL && overflowIsolateCount==0 &&
  1174. overflowEmbeddingCount==0) {
  1175. flags|=DIRPROP_FLAG(dirProp);
  1176. lastCcPos=i;
  1177. validIsolateCount++;
  1178. if(validIsolateCount>pBiDi->isolateCount)
  1179. pBiDi->isolateCount=validIsolateCount;
  1180. embeddingLevel=newLevel;
  1181. /* we can increment stackLast without checking because newLevel
  1182. will exceed UBIDI_MAX_EXPLICIT_LEVEL before stackLast overflows */
  1183. stackLast++;
  1184. stack[stackLast]=embeddingLevel+ISOLATE;
  1185. bracketProcessLRI_RLI(&bracketData, embeddingLevel);
  1186. } else {
  1187. /* make it WS so that it is handled by adjustWSLevels() */
  1188. dirProps[i]=WS;
  1189. overflowIsolateCount++;
  1190. }
  1191. break;
  1192. case PDI:
  1193. if(NO_OVERRIDE(embeddingLevel)!=NO_OVERRIDE(previousLevel)) {
  1194. bracketProcessBoundary(&bracketData, lastCcPos,
  1195. previousLevel, embeddingLevel);
  1196. flags|=DIRPROP_FLAG_MULTI_RUNS;
  1197. }
  1198. /* (X6a) */
  1199. if(overflowIsolateCount) {
  1200. overflowIsolateCount--;
  1201. /* make it WS so that it is handled by adjustWSLevels() */
  1202. dirProps[i]=WS;
  1203. }
  1204. else if(validIsolateCount) {
  1205. flags|=DIRPROP_FLAG(PDI);
  1206. lastCcPos=i;
  1207. overflowEmbeddingCount=0;
  1208. while(stack[stackLast]<ISOLATE) /* pop embedding entries */
  1209. stackLast--; /* until the last isolate entry */
  1210. stackLast--; /* pop also the last isolate entry */
  1211. validIsolateCount--;
  1212. bracketProcessPDI(&bracketData);
  1213. } else
  1214. /* make it WS so that it is handled by adjustWSLevels() */
  1215. dirProps[i]=WS;
  1216. embeddingLevel=(UBiDiLevel)stack[stackLast]&~ISOLATE;
  1217. flags|=(DIRPROP_FLAG(ON)|DIRPROP_FLAG_LR(embeddingLevel));
  1218. previousLevel=embeddingLevel;
  1219. levels[i]=NO_OVERRIDE(embeddingLevel);
  1220. break;
  1221. case B:
  1222. flags|=DIRPROP_FLAG(B);
  1223. levels[i]=GET_PARALEVEL(pBiDi, i);
  1224. if((i+1)<length) {
  1225. if(text[i]==CR && text[i+1]==LF)
  1226. break; /* skip CR when followed by LF */
  1227. overflowEmbeddingCount=overflowIsolateCount=0;
  1228. validIsolateCount=0;
  1229. stackLast=0;
  1230. previousLevel=embeddingLevel=GET_PARALEVEL(pBiDi, i+1);
  1231. stack[0]=embeddingLevel; /* initialize base entry to para level, no override, no isolate */
  1232. bracketProcessB(&bracketData, embeddingLevel);
  1233. }
  1234. break;
  1235. case BN:
  1236. /* BN, LRE, RLE, and PDF are supposed to be removed (X9) */
  1237. /* they will get their levels set correctly in adjustWSLevels() */
  1238. levels[i]=previousLevel;
  1239. flags|=DIRPROP_FLAG(BN);
  1240. break;
  1241. default:
  1242. /* all other types are normal characters and get the "real" level */
  1243. if(NO_OVERRIDE(embeddingLevel)!=NO_OVERRIDE(previousLevel)) {
  1244. bracketProcessBoundary(&bracketData, lastCcPos,
  1245. previousLevel, embeddingLevel);
  1246. flags|=DIRPROP_FLAG_MULTI_RUNS;
  1247. if(embeddingLevel&UBIDI_LEVEL_OVERRIDE)
  1248. flags|=DIRPROP_FLAG_O(embeddingLevel);
  1249. else
  1250. flags|=DIRPROP_FLAG_E(embeddingLevel);
  1251. }
  1252. previousLevel=embeddingLevel;
  1253. levels[i]=embeddingLevel;
  1254. if(!bracketProcessChar(&bracketData, i))
  1255. return (UBiDiDirection)-1;
  1256. /* the dirProp may have been changed in bracketProcessChar() */
  1257. flags|=DIRPROP_FLAG(dirProps[i]);
  1258. break;
  1259. }
  1260. }
  1261. if(flags&MASK_EMBEDDING)
  1262. flags|=DIRPROP_FLAG_LR(pBiDi->paraLevel);
  1263. if(pBiDi->orderParagraphsLTR && (flags&DIRPROP_FLAG(B)))
  1264. flags|=DIRPROP_FLAG(L);
  1265. /* again, determine if the text is mixed-directional or single-directional */
  1266. pBiDi->flags=flags;
  1267. direction=directionFromFlags(pBiDi);
  1268. }
  1269. return direction;
  1270. }
  1271. /*
  1272. * Use a pre-specified embedding levels array:
  1273. *
  1274. * Adjust the directional properties for overrides (->LEVEL_OVERRIDE),
  1275. * ignore all explicit codes (X9),
  1276. * and check all the preset levels.
  1277. *
  1278. * Recalculate the flags to have them reflect the real properties
  1279. * after taking the explicit embeddings into account.
  1280. */
  1281. static UBiDiDirection
  1282. checkExplicitLevels(UBiDi *pBiDi, UErrorCode *pErrorCode) {
  1283. DirProp *dirProps=pBiDi->dirProps;
  1284. UBiDiLevel *levels=pBiDi->levels;
  1285. int32_t isolateCount=0;
  1286. int32_t length=pBiDi->length;
  1287. Flags flags=0; /* collect all directionalities in the text */
  1288. pBiDi->isolateCount=0;
  1289. int32_t currentParaIndex = 0;
  1290. int32_t currentParaLimit = pBiDi->paras[0].limit;
  1291. int32_t currentParaLevel = pBiDi->paraLevel;
  1292. for(int32_t i=0; i<length; ++i) {
  1293. UBiDiLevel level=levels[i];
  1294. DirProp dirProp=dirProps[i];
  1295. if(dirProp==LRI || dirProp==RLI) {
  1296. isolateCount++;
  1297. if(isolateCount>pBiDi->isolateCount)
  1298. pBiDi->isolateCount=isolateCount;
  1299. }
  1300. else if(dirProp==PDI)
  1301. isolateCount--;
  1302. else if(dirProp==B)
  1303. isolateCount=0;
  1304. // optimized version of int32_t currentParaLevel = GET_PARALEVEL(pBiDi, i);
  1305. if (pBiDi->defaultParaLevel != 0 &&
  1306. i == currentParaLimit && (currentParaIndex + 1) < pBiDi->paraCount) {
  1307. currentParaLevel = pBiDi->paras[++currentParaIndex].level;
  1308. currentParaLimit = pBiDi->paras[currentParaIndex].limit;
  1309. }
  1310. UBiDiLevel overrideFlag = level & UBIDI_LEVEL_OVERRIDE;
  1311. level &= ~UBIDI_LEVEL_OVERRIDE;
  1312. if (level < currentParaLevel || UBIDI_MAX_EXPLICIT_LEVEL < level) {
  1313. if (level == 0) {
  1314. if (dirProp == B) {
  1315. // Paragraph separators are ok with explicit level 0.
  1316. // Prevents reordering of paragraphs.
  1317. } else {
  1318. // Treat explicit level 0 as a wildcard for the paragraph level.
  1319. // Avoid making the caller guess what the paragraph level would be.
  1320. level = (UBiDiLevel)currentParaLevel;
  1321. levels[i] = level | overrideFlag;
  1322. }
  1323. } else {
  1324. // 1 <= level < currentParaLevel or UBIDI_MAX_EXPLICIT_LEVEL < level
  1325. /* level out of bounds */
  1326. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  1327. return UBIDI_LTR;
  1328. }
  1329. }
  1330. if (overrideFlag != 0) {
  1331. /* keep the override flag in levels[i] but adjust the flags */
  1332. flags|=DIRPROP_FLAG_O(level);
  1333. } else {
  1334. /* set the flags */
  1335. flags|=DIRPROP_FLAG_E(level)|DIRPROP_FLAG(dirProp);
  1336. }
  1337. }
  1338. if(flags&MASK_EMBEDDING)
  1339. flags|=DIRPROP_FLAG_LR(pBiDi->paraLevel);
  1340. /* determine if the text is mixed-directional or single-directional */
  1341. pBiDi->flags=flags;
  1342. return directionFromFlags(pBiDi);
  1343. }
  1344. /******************************************************************
  1345. The Properties state machine table
  1346. *******************************************************************
  1347. All table cells are 8 bits:
  1348. bits 0..4: next state
  1349. bits 5..7: action to perform (if > 0)
  1350. Cells may be of format "n" where n represents the next state
  1351. (except for the rightmost column).
  1352. Cells may also be of format "s(x,y)" where x represents an action
  1353. to perform and y represents the next state.
  1354. *******************************************************************
  1355. Definitions and type for properties state table
  1356. *******************************************************************
  1357. */
  1358. #define IMPTABPROPS_COLUMNS 16
  1359. #define IMPTABPROPS_RES (IMPTABPROPS_COLUMNS - 1)
  1360. #define GET_STATEPROPS(cell) ((cell)&0x1f)
  1361. #define GET_ACTIONPROPS(cell) ((cell)>>5)
  1362. #define s(action, newState) ((uint8_t)(newState+(action<<5)))
  1363. static const uint8_t groupProp[] = /* dirProp regrouped */
  1364. {
  1365. /* L R EN ES ET AN CS B S WS ON LRE LRO AL RLE RLO PDF NSM BN FSI LRI RLI PDI ENL ENR */
  1366. 0, 1, 2, 7, 8, 3, 9, 6, 5, 4, 4, 10, 10, 12, 10, 10, 10, 11, 10, 4, 4, 4, 4, 13, 14
  1367. };
  1368. enum { DirProp_L=0, DirProp_R=1, DirProp_EN=2, DirProp_AN=3, DirProp_ON=4, DirProp_S=5, DirProp_B=6 }; /* reduced dirProp */
  1369. /******************************************************************
  1370. PROPERTIES STATE TABLE
  1371. In table impTabProps,
  1372. - the ON column regroups ON and WS, FSI, RLI, LRI and PDI
  1373. - the BN column regroups BN, LRE, RLE, LRO, RLO, PDF
  1374. - the Res column is the reduced property assigned to a run
  1375. Action 1: process current run1, init new run1
  1376. 2: init new run2
  1377. 3: process run1, process run2, init new run1
  1378. 4: process run1, set run1=run2, init new run2
  1379. Notes:
  1380. 1) This table is used in resolveImplicitLevels().
  1381. 2) This table triggers actions when there is a change in the Bidi
  1382. property of incoming characters (action 1).
  1383. 3) Most such property sequences are processed immediately (in
  1384. fact, passed to processPropertySeq().
  1385. 4) However, numbers are assembled as one sequence. This means
  1386. that undefined situations (like CS following digits, until
  1387. it is known if the next char will be a digit) are held until
  1388. following chars define them.
  1389. Example: digits followed by CS, then comes another CS or ON;
  1390. the digits will be processed, then the CS assigned
  1391. as the start of an ON sequence (action 3).
  1392. 5) There are cases where more than one sequence must be
  1393. processed, for instance digits followed by CS followed by L:
  1394. the digits must be processed as one sequence, and the CS
  1395. must be processed as an ON sequence, all this before starting
  1396. assembling chars for the opening L sequence.
  1397. */
  1398. static const uint8_t impTabProps[][IMPTABPROPS_COLUMNS] =
  1399. {
  1400. /* L , R , EN , AN , ON , S , B , ES , ET , CS , BN , NSM , AL , ENL , ENR , Res */
  1401. /* 0 Init */ { 1 , 2 , 4 , 5 , 7 , 15 , 17 , 7 , 9 , 7 , 0 , 7 , 3 , 18 , 21 , DirProp_ON },
  1402. /* 1 L */ { 1 , s(1,2), s(1,4), s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), s(1,9), s(1,7), 1 , 1 , s(1,3),s(1,18),s(1,21), DirProp_L },
  1403. /* 2 R */ { s(1,1), 2 , s(1,4), s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), s(1,9), s(1,7), 2 , 2 , s(1,3),s(1,18),s(1,21), DirProp_R },
  1404. /* 3 AL */ { s(1,1), s(1,2), s(1,6), s(1,6), s(1,8),s(1,16),s(1,17), s(1,8), s(1,8), s(1,8), 3 , 3 , 3 ,s(1,18),s(1,21), DirProp_R },
  1405. /* 4 EN */ { s(1,1), s(1,2), 4 , s(1,5), s(1,7),s(1,15),s(1,17),s(2,10), 11 ,s(2,10), 4 , 4 , s(1,3), 18 , 21 , DirProp_EN },
  1406. /* 5 AN */ { s(1,1), s(1,2), s(1,4), 5 , s(1,7),s(1,15),s(1,17), s(1,7), s(1,9),s(2,12), 5 , 5 , s(1,3),s(1,18),s(1,21), DirProp_AN },
  1407. /* 6 AL:EN/AN */ { s(1,1), s(1,2), 6 , 6 , s(1,8),s(1,16),s(1,17), s(1,8), s(1,8),s(2,13), 6 , 6 , s(1,3), 18 , 21 , DirProp_AN },
  1408. /* 7 ON */ { s(1,1), s(1,2), s(1,4), s(1,5), 7 ,s(1,15),s(1,17), 7 ,s(2,14), 7 , 7 , 7 , s(1,3),s(1,18),s(1,21), DirProp_ON },
  1409. /* 8 AL:ON */ { s(1,1), s(1,2), s(1,6), s(1,6), 8 ,s(1,16),s(1,17), 8 , 8 , 8 , 8 , 8 , s(1,3),s(1,18),s(1,21), DirProp_ON },
  1410. /* 9 ET */ { s(1,1), s(1,2), 4 , s(1,5), 7 ,s(1,15),s(1,17), 7 , 9 , 7 , 9 , 9 , s(1,3), 18 , 21 , DirProp_ON },
  1411. /*10 EN+ES/CS */ { s(3,1), s(3,2), 4 , s(3,5), s(4,7),s(3,15),s(3,17), s(4,7),s(4,14), s(4,7), 10 , s(4,7), s(3,3), 18 , 21 , DirProp_EN },
  1412. /*11 EN+ET */ { s(1,1), s(1,2), 4 , s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), 11 , s(1,7), 11 , 11 , s(1,3), 18 , 21 , DirProp_EN },
  1413. /*12 AN+CS */ { s(3,1), s(3,2), s(3,4), 5 , s(4,7),s(3,15),s(3,17), s(4,7),s(4,14), s(4,7), 12 , s(4,7), s(3,3),s(3,18),s(3,21), DirProp_AN },
  1414. /*13 AL:EN/AN+CS */ { s(3,1), s(3,2), 6 , 6 , s(4,8),s(3,16),s(3,17), s(4,8), s(4,8), s(4,8), 13 , s(4,8), s(3,3), 18 , 21 , DirProp_AN },
  1415. /*14 ON+ET */ { s(1,1), s(1,2), s(4,4), s(1,5), 7 ,s(1,15),s(1,17), 7 , 14 , 7 , 14 , 14 , s(1,3),s(4,18),s(4,21), DirProp_ON },
  1416. /*15 S */ { s(1,1), s(1,2), s(1,4), s(1,5), s(1,7), 15 ,s(1,17), s(1,7), s(1,9), s(1,7), 15 , s(1,7), s(1,3),s(1,18),s(1,21), DirProp_S },
  1417. /*16 AL:S */ { s(1,1), s(1,2), s(1,6), s(1,6), s(1,8), 16 ,s(1,17), s(1,8), s(1,8), s(1,8), 16 , s(1,8), s(1,3),s(1,18),s(1,21), DirProp_S },
  1418. /*17 B */ { s(1,1), s(1,2), s(1,4), s(1,5), s(1,7),s(1,15), 17 , s(1,7), s(1,9), s(1,7), 17 , s(1,7), s(1,3),s(1,18),s(1,21), DirProp_B },
  1419. /*18 ENL */ { s(1,1), s(1,2), 18 , s(1,5), s(1,7),s(1,15),s(1,17),s(2,19), 20 ,s(2,19), 18 , 18 , s(1,3), 18 , 21 , DirProp_L },
  1420. /*19 ENL+ES/CS */ { s(3,1), s(3,2), 18 , s(3,5), s(4,7),s(3,15),s(3,17), s(4,7),s(4,14), s(4,7), 19 , s(4,7), s(3,3), 18 , 21 , DirProp_L },
  1421. /*20 ENL+ET */ { s(1,1), s(1,2), 18 , s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), 20 , s(1,7), 20 , 20 , s(1,3), 18 , 21 , DirProp_L },
  1422. /*21 ENR */ { s(1,1), s(1,2), 21 , s(1,5), s(1,7),s(1,15),s(1,17),s(2,22), 23 ,s(2,22), 21 , 21 , s(1,3), 18 , 21 , DirProp_AN },
  1423. /*22 ENR+ES/CS */ { s(3,1), s(3,2), 21 , s(3,5), s(4,7),s(3,15),s(3,17), s(4,7),s(4,14), s(4,7), 22 , s(4,7), s(3,3), 18 , 21 , DirProp_AN },
  1424. /*23 ENR+ET */ { s(1,1), s(1,2), 21 , s(1,5), s(1,7),s(1,15),s(1,17), s(1,7), 23 , s(1,7), 23 , 23 , s(1,3), 18 , 21 , DirProp_AN }
  1425. };
  1426. /* we must undef macro s because the levels tables have a different
  1427. * structure (4 bits for action and 4 bits for next state.
  1428. */
  1429. #undef s
  1430. /******************************************************************
  1431. The levels state machine tables
  1432. *******************************************************************
  1433. All table cells are 8 bits:
  1434. bits 0..3: next state
  1435. bits 4..7: action to perform (if > 0)
  1436. Cells may be of format "n" where n represents the next state
  1437. (except for the rightmost column).
  1438. Cells may also be of format "s(x,y)" where x represents an action
  1439. to perform and y represents the next state.
  1440. This format limits each table to 16 states each and to 15 actions.
  1441. *******************************************************************
  1442. Definitions and type for levels state tables
  1443. *******************************************************************
  1444. */
  1445. #define IMPTABLEVELS_COLUMNS (DirProp_B + 2)
  1446. #define IMPTABLEVELS_RES (IMPTABLEVELS_COLUMNS - 1)
  1447. #define GET_STATE(cell) ((cell)&0x0f)
  1448. #define GET_ACTION(cell) ((cell)>>4)
  1449. #define s(action, newState) ((uint8_t)(newState+(action<<4)))
  1450. typedef uint8_t ImpTab[][IMPTABLEVELS_COLUMNS];
  1451. typedef uint8_t ImpAct[];
  1452. /* FOOD FOR THOUGHT: each ImpTab should have its associated ImpAct,
  1453. * instead of having a pair of ImpTab and a pair of ImpAct.
  1454. */
  1455. typedef struct ImpTabPair {
  1456. const void * pImpTab[2];
  1457. const void * pImpAct[2];
  1458. } ImpTabPair;
  1459. /******************************************************************
  1460. LEVELS STATE TABLES
  1461. In all levels state tables,
  1462. - state 0 is the initial state
  1463. - the Res column is the increment to add to the text level
  1464. for this property sequence.
  1465. The impAct arrays for each table of a pair map the local action
  1466. numbers of the table to the total list of actions. For instance,
  1467. action 2 in a given table corresponds to the action number which
  1468. appears in entry [2] of the impAct array for that table.
  1469. The first entry of all impAct arrays must be 0.
  1470. Action 1: init conditional sequence
  1471. 2: prepend conditional sequence to current sequence
  1472. 3: set ON sequence to new level - 1
  1473. 4: init EN/AN/ON sequence
  1474. 5: fix EN/AN/ON sequence followed by R
  1475. 6: set previous level sequence to level 2
  1476. Notes:
  1477. 1) These tables are used in processPropertySeq(). The input
  1478. is property sequences as determined by resolveImplicitLevels.
  1479. 2) Most such property sequences are processed immediately
  1480. (levels are assigned).
  1481. 3) However, some sequences cannot be assigned a final level till
  1482. one or more following sequences are received. For instance,
  1483. ON following an R sequence within an even-level paragraph.
  1484. If the following sequence is R, the ON sequence will be
  1485. assigned basic run level+1, and so will the R sequence.
  1486. 4) S is generally handled like ON, since its level will be fixed
  1487. to paragraph level in adjustWSLevels().
  1488. */
  1489. static const ImpTab impTabL_DEFAULT = /* Even paragraph level */
  1490. /* In this table, conditional sequences receive the lower possible level
  1491. until proven otherwise.
  1492. */
  1493. {
  1494. /* L , R , EN , AN , ON , S , B , Res */
  1495. /* 0 : init */ { 0 , 1 , 0 , 2 , 0 , 0 , 0 , 0 },
  1496. /* 1 : R */ { 0 , 1 , 3 , 3 , s(1,4), s(1,4), 0 , 1 },
  1497. /* 2 : AN */ { 0 , 1 , 0 , 2 , s(1,5), s(1,5), 0 , 2 },
  1498. /* 3 : R+EN/AN */ { 0 , 1 , 3 , 3 , s(1,4), s(1,4), 0 , 2 },
  1499. /* 4 : R+ON */ { 0 , s(2,1), s(3,3), s(3,3), 4 , 4 , 0 , 0 },
  1500. /* 5 : AN+ON */ { 0 , s(2,1), 0 , s(3,2), 5 , 5 , 0 , 0 }
  1501. };
  1502. static const ImpTab impTabR_DEFAULT = /* Odd paragraph level */
  1503. /* In this table, conditional sequences receive the lower possible level
  1504. until proven otherwise.
  1505. */
  1506. {
  1507. /* L , R , EN , AN , ON , S , B , Res */
  1508. /* 0 : init */ { 1 , 0 , 2 , 2 , 0 , 0 , 0 , 0 },
  1509. /* 1 : L */ { 1 , 0 , 1 , 3 , s(1,4), s(1,4), 0 , 1 },
  1510. /* 2 : EN/AN */ { 1 , 0 , 2 , 2 , 0 , 0 , 0 , 1 },
  1511. /* 3 : L+AN */ { 1 , 0 , 1 , 3 , 5 , 5 , 0 , 1 },
  1512. /* 4 : L+ON */ { s(2,1), 0 , s(2,1), 3 , 4 , 4 , 0 , 0 },
  1513. /* 5 : L+AN+ON */ { 1 , 0 , 1 , 3 , 5 , 5 , 0 , 0 }
  1514. };
  1515. static const ImpAct impAct0 = {0,1,2,3,4};
  1516. static const ImpTabPair impTab_DEFAULT = {{&impTabL_DEFAULT,
  1517. &impTabR_DEFAULT},
  1518. {&impAct0, &impAct0}};
  1519. static const ImpTab impTabL_NUMBERS_SPECIAL = /* Even paragraph level */
  1520. /* In this table, conditional sequences receive the lower possible level
  1521. until proven otherwise.
  1522. */
  1523. {
  1524. /* L , R , EN , AN , ON , S , B , Res */
  1525. /* 0 : init */ { 0 , 2 , s(1,1), s(1,1), 0 , 0 , 0 , 0 },
  1526. /* 1 : L+EN/AN */ { 0 , s(4,2), 1 , 1 , 0 , 0 , 0 , 0 },
  1527. /* 2 : R */ { 0 , 2 , 4 , 4 , s(1,3), s(1,3), 0 , 1 },
  1528. /* 3 : R+ON */ { 0 , s(2,2), s(3,4), s(3,4), 3 , 3 , 0 , 0 },
  1529. /* 4 : R+EN/AN */ { 0 , 2 , 4 , 4 , s(1,3), s(1,3), 0 , 2 }
  1530. };
  1531. static const ImpTabPair impTab_NUMBERS_SPECIAL = {{&impTabL_NUMBERS_SPECIAL,
  1532. &impTabR_DEFAULT},
  1533. {&impAct0, &impAct0}};
  1534. static const ImpTab impTabL_GROUP_NUMBERS_WITH_R =
  1535. /* In this table, EN/AN+ON sequences receive levels as if associated with R
  1536. until proven that there is L or sor/eor on both sides. AN is handled like EN.
  1537. */
  1538. {
  1539. /* L , R , EN , AN , ON , S , B , Res */
  1540. /* 0 init */ { 0 , 3 , s(1,1), s(1,1), 0 , 0 , 0 , 0 },
  1541. /* 1 EN/AN */ { s(2,0), 3 , 1 , 1 , 2 , s(2,0), s(2,0), 2 },
  1542. /* 2 EN/AN+ON */ { s(2,0), 3 , 1 , 1 , 2 , s(2,0), s(2,0), 1 },
  1543. /* 3 R */ { 0 , 3 , 5 , 5 , s(1,4), 0 , 0 , 1 },
  1544. /* 4 R+ON */ { s(2,0), 3 , 5 , 5 , 4 , s(2,0), s(2,0), 1 },
  1545. /* 5 R+EN/AN */ { 0 , 3 , 5 , 5 , s(1,4), 0 , 0 , 2 }
  1546. };
  1547. static const ImpTab impTabR_GROUP_NUMBERS_WITH_R =
  1548. /* In this table, EN/AN+ON sequences receive levels as if associated with R
  1549. until proven that there is L on both sides. AN is handled like EN.
  1550. */
  1551. {
  1552. /* L , R , EN , AN , ON , S , B , Res */
  1553. /* 0 init */ { 2 , 0 , 1 , 1 , 0 , 0 , 0 , 0 },
  1554. /* 1 EN/AN */ { 2 , 0 , 1 , 1 , 0 , 0 , 0 , 1 },
  1555. /* 2 L */ { 2 , 0 , s(1,4), s(1,4), s(1,3), 0 , 0 , 1 },
  1556. /* 3 L+ON */ { s(2,2), 0 , 4 , 4 , 3 , 0 , 0 , 0 },
  1557. /* 4 L+EN/AN */ { s(2,2), 0 , 4 , 4 , 3 , 0 , 0 , 1 }
  1558. };
  1559. static const ImpTabPair impTab_GROUP_NUMBERS_WITH_R = {
  1560. {&impTabL_GROUP_NUMBERS_WITH_R,
  1561. &impTabR_GROUP_NUMBERS_WITH_R},
  1562. {&impAct0, &impAct0}};
  1563. static const ImpTab impTabL_INVERSE_NUMBERS_AS_L =
  1564. /* This table is identical to the Default LTR table except that EN and AN are
  1565. handled like L.
  1566. */
  1567. {
  1568. /* L , R , EN , AN , ON , S , B , Res */
  1569. /* 0 : init */ { 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 },
  1570. /* 1 : R */ { 0 , 1 , 0 , 0 , s(1,4), s(1,4), 0 , 1 },
  1571. /* 2 : AN */ { 0 , 1 , 0 , 0 , s(1,5), s(1,5), 0 , 2 },
  1572. /* 3 : R+EN/AN */ { 0 , 1 , 0 , 0 , s(1,4), s(1,4), 0 , 2 },
  1573. /* 4 : R+ON */ { s(2,0), 1 , s(2,0), s(2,0), 4 , 4 , s(2,0), 1 },
  1574. /* 5 : AN+ON */ { s(2,0), 1 , s(2,0), s(2,0), 5 , 5 , s(2,0), 1 }
  1575. };
  1576. static const ImpTab impTabR_INVERSE_NUMBERS_AS_L =
  1577. /* This table is identical to the Default RTL table except that EN and AN are
  1578. handled like L.
  1579. */
  1580. {
  1581. /* L , R , EN , AN , ON , S , B , Res */
  1582. /* 0 : init */ { 1 , 0 , 1 , 1 , 0 , 0 , 0 , 0 },
  1583. /* 1 : L */ { 1 , 0 , 1 , 1 , s(1,4), s(1,4), 0 , 1 },
  1584. /* 2 : EN/AN */ { 1 , 0 , 1 , 1 , 0 , 0 , 0 , 1 },
  1585. /* 3 : L+AN */ { 1 , 0 , 1 , 1 , 5 , 5 , 0 , 1 },
  1586. /* 4 : L+ON */ { s(2,1), 0 , s(2,1), s(2,1), 4 , 4 , 0 , 0 },
  1587. /* 5 : L+AN+ON */ { 1 , 0 , 1 , 1 , 5 , 5 , 0 , 0 }
  1588. };
  1589. static const ImpTabPair impTab_INVERSE_NUMBERS_AS_L = {
  1590. {&impTabL_INVERSE_NUMBERS_AS_L,
  1591. &impTabR_INVERSE_NUMBERS_AS_L},
  1592. {&impAct0, &impAct0}};
  1593. static const ImpTab impTabR_INVERSE_LIKE_DIRECT = /* Odd paragraph level */
  1594. /* In this table, conditional sequences receive the lower possible level
  1595. until proven otherwise.
  1596. */
  1597. {
  1598. /* L , R , EN , AN , ON , S , B , Res */
  1599. /* 0 : init */ { 1 , 0 , 2 , 2 , 0 , 0 , 0 , 0 },
  1600. /* 1 : L */ { 1 , 0 , 1 , 2 , s(1,3), s(1,3), 0 , 1 },
  1601. /* 2 : EN/AN */ { 1 , 0 , 2 , 2 , 0 , 0 , 0 , 1 },
  1602. /* 3 : L+ON */ { s(2,1), s(3,0), 6 , 4 , 3 , 3 , s(3,0), 0 },
  1603. /* 4 : L+ON+AN */ { s(2,1), s(3,0), 6 , 4 , 5 , 5 , s(3,0), 3 },
  1604. /* 5 : L+AN+ON */ { s(2,1), s(3,0), 6 , 4 , 5 , 5 , s(3,0), 2 },
  1605. /* 6 : L+ON+EN */ { s(2,1), s(3,0), 6 , 4 , 3 , 3 , s(3,0), 1 }
  1606. };
  1607. static const ImpAct impAct1 = {0,1,13,14};
  1608. /* FOOD FOR THOUGHT: in LTR table below, check case "JKL 123abc"
  1609. */
  1610. static const ImpTabPair impTab_INVERSE_LIKE_DIRECT = {
  1611. {&impTabL_DEFAULT,
  1612. &impTabR_INVERSE_LIKE_DIRECT},
  1613. {&impAct0, &impAct1}};
  1614. static const ImpTab impTabL_INVERSE_LIKE_DIRECT_WITH_MARKS =
  1615. /* The case handled in this table is (visually): R EN L
  1616. */
  1617. {
  1618. /* L , R , EN , AN , ON , S , B , Res */
  1619. /* 0 : init */ { 0 , s(6,3), 0 , 1 , 0 , 0 , 0 , 0 },
  1620. /* 1 : L+AN */ { 0 , s(6,3), 0 , 1 , s(1,2), s(3,0), 0 , 4 },
  1621. /* 2 : L+AN+ON */ { s(2,0), s(6,3), s(2,0), 1 , 2 , s(3,0), s(2,0), 3 },
  1622. /* 3 : R */ { 0 , s(6,3), s(5,5), s(5,6), s(1,4), s(3,0), 0 , 3 },
  1623. /* 4 : R+ON */ { s(3,0), s(4,3), s(5,5), s(5,6), 4 , s(3,0), s(3,0), 3 },
  1624. /* 5 : R+EN */ { s(3,0), s(4,3), 5 , s(5,6), s(1,4), s(3,0), s(3,0), 4 },
  1625. /* 6 : R+AN */ { s(3,0), s(4,3), s(5,5), 6 , s(1,4), s(3,0), s(3,0), 4 }
  1626. };
  1627. static const ImpTab impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS =
  1628. /* The cases handled in this table are (visually): R EN L
  1629. R L AN L
  1630. */
  1631. {
  1632. /* L , R , EN , AN , ON , S , B , Res */
  1633. /* 0 : init */ { s(1,3), 0 , 1 , 1 , 0 , 0 , 0 , 0 },
  1634. /* 1 : R+EN/AN */ { s(2,3), 0 , 1 , 1 , 2 , s(4,0), 0 , 1 },
  1635. /* 2 : R+EN/AN+ON */ { s(2,3), 0 , 1 , 1 , 2 , s(4,0), 0 , 0 },
  1636. /* 3 : L */ { 3 , 0 , 3 , s(3,6), s(1,4), s(4,0), 0 , 1 },
  1637. /* 4 : L+ON */ { s(5,3), s(4,0), 5 , s(3,6), 4 , s(4,0), s(4,0), 0 },
  1638. /* 5 : L+ON+EN */ { s(5,3), s(4,0), 5 , s(3,6), 4 , s(4,0), s(4,0), 1 },
  1639. /* 6 : L+AN */ { s(5,3), s(4,0), 6 , 6 , 4 , s(4,0), s(4,0), 3 }
  1640. };
  1641. static const ImpAct impAct2 = {0,1,2,5,6,7,8};
  1642. static const ImpAct impAct3 = {0,1,9,10,11,12};
  1643. static const ImpTabPair impTab_INVERSE_LIKE_DIRECT_WITH_MARKS = {
  1644. {&impTabL_INVERSE_LIKE_DIRECT_WITH_MARKS,
  1645. &impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS},
  1646. {&impAct2, &impAct3}};
  1647. static const ImpTabPair impTab_INVERSE_FOR_NUMBERS_SPECIAL = {
  1648. {&impTabL_NUMBERS_SPECIAL,
  1649. &impTabR_INVERSE_LIKE_DIRECT},
  1650. {&impAct0, &impAct1}};
  1651. static const ImpTab impTabL_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS =
  1652. /* The case handled in this table is (visually): R EN L
  1653. */
  1654. {
  1655. /* L , R , EN , AN , ON , S , B , Res */
  1656. /* 0 : init */ { 0 , s(6,2), 1 , 1 , 0 , 0 , 0 , 0 },
  1657. /* 1 : L+EN/AN */ { 0 , s(6,2), 1 , 1 , 0 , s(3,0), 0 , 4 },
  1658. /* 2 : R */ { 0 , s(6,2), s(5,4), s(5,4), s(1,3), s(3,0), 0 , 3 },
  1659. /* 3 : R+ON */ { s(3,0), s(4,2), s(5,4), s(5,4), 3 , s(3,0), s(3,0), 3 },
  1660. /* 4 : R+EN/AN */ { s(3,0), s(4,2), 4 , 4 , s(1,3), s(3,0), s(3,0), 4 }
  1661. };
  1662. static const ImpTabPair impTab_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS = {
  1663. {&impTabL_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS,
  1664. &impTabR_INVERSE_LIKE_DIRECT_WITH_MARKS},
  1665. {&impAct2, &impAct3}};
  1666. #undef s
  1667. typedef struct {
  1668. const ImpTab * pImpTab; /* level table pointer */
  1669. const ImpAct * pImpAct; /* action map array */
  1670. int32_t startON; /* start of ON sequence */
  1671. int32_t startL2EN; /* start of level 2 sequence */
  1672. int32_t lastStrongRTL; /* index of last found R or AL */
  1673. int32_t state; /* current state */
  1674. int32_t runStart; /* start position of the run */
  1675. UBiDiLevel runLevel; /* run level before implicit solving */
  1676. } LevState;
  1677. /*------------------------------------------------------------------------*/
  1678. static void
  1679. addPoint(UBiDi *pBiDi, int32_t pos, int32_t flag)
  1680. /* param pos: position where to insert
  1681. param flag: one of LRM_BEFORE, LRM_AFTER, RLM_BEFORE, RLM_AFTER
  1682. */
  1683. {
  1684. #define FIRSTALLOC 10
  1685. Point point;
  1686. InsertPoints * pInsertPoints=&(pBiDi->insertPoints);
  1687. if (pInsertPoints->capacity == 0)
  1688. {
  1689. pInsertPoints->points=static_cast<Point *>(uprv_malloc(sizeof(Point)*FIRSTALLOC));
  1690. if (pInsertPoints->points == nullptr)
  1691. {
  1692. pInsertPoints->errorCode=U_MEMORY_ALLOCATION_ERROR;
  1693. return;
  1694. }
  1695. pInsertPoints->capacity=FIRSTALLOC;
  1696. }
  1697. if (pInsertPoints->size >= pInsertPoints->capacity) /* no room for new point */
  1698. {
  1699. Point * savePoints=pInsertPoints->points;
  1700. pInsertPoints->points=static_cast<Point *>(uprv_realloc(pInsertPoints->points,
  1701. pInsertPoints->capacity*2*sizeof(Point)));
  1702. if (pInsertPoints->points == nullptr)
  1703. {
  1704. pInsertPoints->points=savePoints;
  1705. pInsertPoints->errorCode=U_MEMORY_ALLOCATION_ERROR;
  1706. return;
  1707. }
  1708. else pInsertPoints->capacity*=2;
  1709. }
  1710. point.pos=pos;
  1711. point.flag=flag;
  1712. pInsertPoints->points[pInsertPoints->size]=point;
  1713. pInsertPoints->size++;
  1714. #undef FIRSTALLOC
  1715. }
  1716. static void
  1717. setLevelsOutsideIsolates(UBiDi *pBiDi, int32_t start, int32_t limit, UBiDiLevel level)
  1718. {
  1719. DirProp *dirProps=pBiDi->dirProps, dirProp;
  1720. UBiDiLevel *levels=pBiDi->levels;
  1721. int32_t isolateCount=0, k;
  1722. for(k=start; k<limit; k++) {
  1723. dirProp=dirProps[k];
  1724. if(dirProp==PDI)
  1725. isolateCount--;
  1726. if(isolateCount==0)
  1727. levels[k]=level;
  1728. if(dirProp==LRI || dirProp==RLI)
  1729. isolateCount++;
  1730. }
  1731. }
  1732. /* perform rules (Wn), (Nn), and (In) on a run of the text ------------------ */
  1733. /*
  1734. * This implementation of the (Wn) rules applies all rules in one pass.
  1735. * In order to do so, it needs a look-ahead of typically 1 character
  1736. * (except for W5: sequences of ET) and keeps track of changes
  1737. * in a rule Wp that affect a later Wq (p<q).
  1738. *
  1739. * The (Nn) and (In) rules are also performed in that same single loop,
  1740. * but effectively one iteration behind for white space.
  1741. *
  1742. * Since all implicit rules are performed in one step, it is not necessary
  1743. * to actually store the intermediate directional properties in dirProps[].
  1744. */
  1745. static void
  1746. processPropertySeq(UBiDi *pBiDi, LevState *pLevState, uint8_t _prop,
  1747. int32_t start, int32_t limit) {
  1748. uint8_t cell, oldStateSeq, actionSeq;
  1749. const ImpTab * pImpTab=pLevState->pImpTab;
  1750. const ImpAct * pImpAct=pLevState->pImpAct;
  1751. UBiDiLevel * levels=pBiDi->levels;
  1752. UBiDiLevel level, addLevel;
  1753. InsertPoints * pInsertPoints;
  1754. int32_t start0, k;
  1755. start0=start; /* save original start position */
  1756. oldStateSeq=(uint8_t)pLevState->state;
  1757. cell=(*pImpTab)[oldStateSeq][_prop];
  1758. pLevState->state=GET_STATE(cell); /* isolate the new state */
  1759. actionSeq=(*pImpAct)[GET_ACTION(cell)]; /* isolate the action */
  1760. addLevel=(*pImpTab)[pLevState->state][IMPTABLEVELS_RES];
  1761. if(actionSeq) {
  1762. switch(actionSeq) {
  1763. case 1: /* init ON seq */
  1764. pLevState->startON=start0;
  1765. break;
  1766. case 2: /* prepend ON seq to current seq */
  1767. start=pLevState->startON;
  1768. break;
  1769. case 3: /* EN/AN after R+ON */
  1770. level=pLevState->runLevel+1;
  1771. setLevelsOutsideIsolates(pBiDi, pLevState->startON, start0, level);
  1772. break;
  1773. case 4: /* EN/AN before R for NUMBERS_SPECIAL */
  1774. level=pLevState->runLevel+2;
  1775. setLevelsOutsideIsolates(pBiDi, pLevState->startON, start0, level);
  1776. break;
  1777. case 5: /* L or S after possible relevant EN/AN */
  1778. /* check if we had EN after R/AL */
  1779. if (pLevState->startL2EN >= 0) {
  1780. addPoint(pBiDi, pLevState->startL2EN, LRM_BEFORE);
  1781. }
  1782. pLevState->startL2EN=-1; /* not within previous if since could also be -2 */
  1783. /* check if we had any relevant EN/AN after R/AL */
  1784. pInsertPoints=&(pBiDi->insertPoints);
  1785. if ((pInsertPoints->capacity == 0) ||
  1786. (pInsertPoints->size <= pInsertPoints->confirmed))
  1787. {
  1788. /* nothing, just clean up */
  1789. pLevState->lastStrongRTL=-1;
  1790. /* check if we have a pending conditional segment */
  1791. level=(*pImpTab)[oldStateSeq][IMPTABLEVELS_RES];
  1792. if ((level & 1) && (pLevState->startON > 0)) { /* after ON */
  1793. start=pLevState->startON; /* reset to basic run level */
  1794. }
  1795. if (_prop == DirProp_S) /* add LRM before S */
  1796. {
  1797. addPoint(pBiDi, start0, LRM_BEFORE);
  1798. pInsertPoints->confirmed=pInsertPoints->size;
  1799. }
  1800. break;
  1801. }
  1802. /* reset previous RTL cont to level for LTR text */
  1803. for (k=pLevState->lastStrongRTL+1; k<start0; k++)
  1804. {
  1805. /* reset odd level, leave runLevel+2 as is */
  1806. levels[k]=(levels[k] - 2) & ~1;
  1807. }
  1808. /* mark insert points as confirmed */
  1809. pInsertPoints->confirmed=pInsertPoints->size;
  1810. pLevState->lastStrongRTL=-1;
  1811. if (_prop == DirProp_S) /* add LRM before S */
  1812. {
  1813. addPoint(pBiDi, start0, LRM_BEFORE);
  1814. pInsertPoints->confirmed=pInsertPoints->size;
  1815. }
  1816. break;
  1817. case 6: /* R/AL after possible relevant EN/AN */
  1818. /* just clean up */
  1819. pInsertPoints=&(pBiDi->insertPoints);
  1820. if (pInsertPoints->capacity > 0)
  1821. /* remove all non confirmed insert points */
  1822. pInsertPoints->size=pInsertPoints->confirmed;
  1823. pLevState->startON=-1;
  1824. pLevState->startL2EN=-1;
  1825. pLevState->lastStrongRTL=limit - 1;
  1826. break;
  1827. case 7: /* EN/AN after R/AL + possible cont */
  1828. /* check for real AN */
  1829. if ((_prop == DirProp_AN) && (pBiDi->dirProps[start0] == AN) &&
  1830. (pBiDi->reorderingMode!=UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL))
  1831. {
  1832. /* real AN */
  1833. if (pLevState->startL2EN == -1) /* if no relevant EN already found */
  1834. {
  1835. /* just note the righmost digit as a strong RTL */
  1836. pLevState->lastStrongRTL=limit - 1;
  1837. break;
  1838. }
  1839. if (pLevState->startL2EN >= 0) /* after EN, no AN */
  1840. {
  1841. addPoint(pBiDi, pLevState->startL2EN, LRM_BEFORE);
  1842. pLevState->startL2EN=-2;
  1843. }
  1844. /* note AN */
  1845. addPoint(pBiDi, start0, LRM_BEFORE);
  1846. break;
  1847. }
  1848. /* if first EN/AN after R/AL */
  1849. if (pLevState->startL2EN == -1) {
  1850. pLevState->startL2EN=start0;
  1851. }
  1852. break;
  1853. case 8: /* note location of latest R/AL */
  1854. pLevState->lastStrongRTL=limit - 1;
  1855. pLevState->startON=-1;
  1856. break;
  1857. case 9: /* L after R+ON/EN/AN */
  1858. /* include possible adjacent number on the left */
  1859. for (k=start0-1; k>=0 && !(levels[k]&1); k--);
  1860. if(k>=0) {
  1861. addPoint(pBiDi, k, RLM_BEFORE); /* add RLM before */
  1862. pInsertPoints=&(pBiDi->insertPoints);
  1863. pInsertPoints->confirmed=pInsertPoints->size; /* confirm it */
  1864. }
  1865. pLevState->startON=start0;
  1866. break;
  1867. case 10: /* AN after L */
  1868. /* AN numbers between L text on both sides may be trouble. */
  1869. /* tentatively bracket with LRMs; will be confirmed if followed by L */
  1870. addPoint(pBiDi, start0, LRM_BEFORE); /* add LRM before */
  1871. addPoint(pBiDi, start0, LRM_AFTER); /* add LRM after */
  1872. break;
  1873. case 11: /* R after L+ON/EN/AN */
  1874. /* false alert, infirm LRMs around previous AN */
  1875. pInsertPoints=&(pBiDi->insertPoints);
  1876. pInsertPoints->size=pInsertPoints->confirmed;
  1877. if (_prop == DirProp_S) /* add RLM before S */
  1878. {
  1879. addPoint(pBiDi, start0, RLM_BEFORE);
  1880. pInsertPoints->confirmed=pInsertPoints->size;
  1881. }
  1882. break;
  1883. case 12: /* L after L+ON/AN */
  1884. level=pLevState->runLevel + addLevel;
  1885. for(k=pLevState->startON; k<start0; k++) {
  1886. if (levels[k]<level)
  1887. levels[k]=level;
  1888. }
  1889. pInsertPoints=&(pBiDi->insertPoints);
  1890. pInsertPoints->confirmed=pInsertPoints->size; /* confirm inserts */
  1891. pLevState->startON=start0;
  1892. break;
  1893. case 13: /* L after L+ON+EN/AN/ON */
  1894. level=pLevState->runLevel;
  1895. for(k=start0-1; k>=pLevState->startON; k--) {
  1896. if(levels[k]==level+3) {
  1897. while(levels[k]==level+3) {
  1898. levels[k--]-=2;
  1899. }
  1900. while(levels[k]==level) {
  1901. k--;
  1902. }
  1903. }
  1904. if(levels[k]==level+2) {
  1905. levels[k]=level;
  1906. continue;
  1907. }
  1908. levels[k]=level+1;
  1909. }
  1910. break;
  1911. case 14: /* R after L+ON+EN/AN/ON */
  1912. level=pLevState->runLevel+1;
  1913. for(k=start0-1; k>=pLevState->startON; k--) {
  1914. if(levels[k]>level) {
  1915. levels[k]-=2;
  1916. }
  1917. }
  1918. break;
  1919. default: /* we should never get here */
  1920. UPRV_UNREACHABLE_EXIT;
  1921. }
  1922. }
  1923. if((addLevel) || (start < start0)) {
  1924. level=pLevState->runLevel + addLevel;
  1925. if(start>=pLevState->runStart) {
  1926. for(k=start; k<limit; k++) {
  1927. levels[k]=level;
  1928. }
  1929. } else {
  1930. setLevelsOutsideIsolates(pBiDi, start, limit, level);
  1931. }
  1932. }
  1933. }
  1934. /**
  1935. * Returns the directionality of the last strong character at the end of the prologue, if any.
  1936. * Requires prologue!=null.
  1937. */
  1938. static DirProp
  1939. lastL_R_AL(UBiDi *pBiDi) {
  1940. const char16_t *text=pBiDi->prologue;
  1941. int32_t length=pBiDi->proLength;
  1942. int32_t i;
  1943. UChar32 uchar;
  1944. DirProp dirProp;
  1945. for(i=length; i>0; ) {
  1946. /* i is decremented by U16_PREV */
  1947. U16_PREV(text, 0, i, uchar);
  1948. dirProp=(DirProp)ubidi_getCustomizedClass(pBiDi, uchar);
  1949. if(dirProp==L) {
  1950. return DirProp_L;
  1951. }
  1952. if(dirProp==R || dirProp==AL) {
  1953. return DirProp_R;
  1954. }
  1955. if(dirProp==B) {
  1956. return DirProp_ON;
  1957. }
  1958. }
  1959. return DirProp_ON;
  1960. }
  1961. /**
  1962. * Returns the directionality of the first strong character, or digit, in the epilogue, if any.
  1963. * Requires epilogue!=null.
  1964. */
  1965. static DirProp
  1966. firstL_R_AL_EN_AN(UBiDi *pBiDi) {
  1967. const char16_t *text=pBiDi->epilogue;
  1968. int32_t length=pBiDi->epiLength;
  1969. int32_t i;
  1970. UChar32 uchar;
  1971. DirProp dirProp;
  1972. for(i=0; i<length; ) {
  1973. /* i is incremented by U16_NEXT */
  1974. U16_NEXT(text, i, length, uchar);
  1975. dirProp=(DirProp)ubidi_getCustomizedClass(pBiDi, uchar);
  1976. if(dirProp==L) {
  1977. return DirProp_L;
  1978. }
  1979. if(dirProp==R || dirProp==AL) {
  1980. return DirProp_R;
  1981. }
  1982. if(dirProp==EN) {
  1983. return DirProp_EN;
  1984. }
  1985. if(dirProp==AN) {
  1986. return DirProp_AN;
  1987. }
  1988. }
  1989. return DirProp_ON;
  1990. }
  1991. static void
  1992. resolveImplicitLevels(UBiDi *pBiDi,
  1993. int32_t start, int32_t limit,
  1994. DirProp sor, DirProp eor) {
  1995. const DirProp *dirProps=pBiDi->dirProps;
  1996. DirProp dirProp;
  1997. LevState levState;
  1998. int32_t i, start1, start2;
  1999. uint16_t oldStateImp, stateImp, actionImp;
  2000. uint8_t gprop, resProp, cell;
  2001. UBool inverseRTL;
  2002. DirProp nextStrongProp=R;
  2003. int32_t nextStrongPos=-1;
  2004. /* check for RTL inverse BiDi mode */
  2005. /* FOOD FOR THOUGHT: in case of RTL inverse BiDi, it would make sense to
  2006. * loop on the text characters from end to start.
  2007. * This would need a different properties state table (at least different
  2008. * actions) and different levels state tables (maybe very similar to the
  2009. * LTR corresponding ones.
  2010. */
  2011. inverseRTL=(UBool)
  2012. ((start<pBiDi->lastArabicPos) && (GET_PARALEVEL(pBiDi, start) & 1) &&
  2013. (pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_LIKE_DIRECT ||
  2014. pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL));
  2015. /* initialize for property and levels state tables */
  2016. levState.startL2EN=-1; /* used for INVERSE_LIKE_DIRECT_WITH_MARKS */
  2017. levState.lastStrongRTL=-1; /* used for INVERSE_LIKE_DIRECT_WITH_MARKS */
  2018. levState.runStart=start;
  2019. levState.runLevel=pBiDi->levels[start];
  2020. levState.pImpTab=(const ImpTab*)((pBiDi->pImpTabPair)->pImpTab)[levState.runLevel&1];
  2021. levState.pImpAct=(const ImpAct*)((pBiDi->pImpTabPair)->pImpAct)[levState.runLevel&1];
  2022. if(start==0 && pBiDi->proLength>0) {
  2023. DirProp lastStrong=lastL_R_AL(pBiDi);
  2024. if(lastStrong!=DirProp_ON) {
  2025. sor=lastStrong;
  2026. }
  2027. }
  2028. /* The isolates[] entries contain enough information to
  2029. resume the bidi algorithm in the same state as it was
  2030. when it was interrupted by an isolate sequence. */
  2031. if(dirProps[start]==PDI && pBiDi->isolateCount >= 0) {
  2032. levState.startON=pBiDi->isolates[pBiDi->isolateCount].startON;
  2033. start1=pBiDi->isolates[pBiDi->isolateCount].start1;
  2034. stateImp=pBiDi->isolates[pBiDi->isolateCount].stateImp;
  2035. levState.state=pBiDi->isolates[pBiDi->isolateCount].state;
  2036. pBiDi->isolateCount--;
  2037. } else {
  2038. levState.startON=-1;
  2039. start1=start;
  2040. if(dirProps[start]==NSM)
  2041. stateImp = 1 + sor;
  2042. else
  2043. stateImp=0;
  2044. levState.state=0;
  2045. processPropertySeq(pBiDi, &levState, sor, start, start);
  2046. }
  2047. start2=start; /* to make Java compiler happy */
  2048. for(i=start; i<=limit; i++) {
  2049. if(i>=limit) {
  2050. int32_t k;
  2051. for(k=limit-1; k>start&&(DIRPROP_FLAG(dirProps[k])&MASK_BN_EXPLICIT); k--);
  2052. dirProp=dirProps[k];
  2053. if(dirProp==LRI || dirProp==RLI)
  2054. break; /* no forced closing for sequence ending with LRI/RLI */
  2055. gprop=eor;
  2056. } else {
  2057. DirProp prop, prop1;
  2058. prop=dirProps[i];
  2059. if(prop==B) {
  2060. pBiDi->isolateCount=-1; /* current isolates stack entry == none */
  2061. }
  2062. if(inverseRTL) {
  2063. if(prop==AL) {
  2064. /* AL before EN does not make it AN */
  2065. prop=R;
  2066. } else if(prop==EN) {
  2067. if(nextStrongPos<=i) {
  2068. /* look for next strong char (L/R/AL) */
  2069. int32_t j;
  2070. nextStrongProp=R; /* set default */
  2071. nextStrongPos=limit;
  2072. for(j=i+1; j<limit; j++) {
  2073. prop1=dirProps[j];
  2074. if(prop1==L || prop1==R || prop1==AL) {
  2075. nextStrongProp=prop1;
  2076. nextStrongPos=j;
  2077. break;
  2078. }
  2079. }
  2080. }
  2081. if(nextStrongProp==AL) {
  2082. prop=AN;
  2083. }
  2084. }
  2085. }
  2086. gprop=groupProp[prop];
  2087. }
  2088. oldStateImp=stateImp;
  2089. cell=impTabProps[oldStateImp][gprop];
  2090. stateImp=GET_STATEPROPS(cell); /* isolate the new state */
  2091. actionImp=GET_ACTIONPROPS(cell); /* isolate the action */
  2092. if((i==limit) && (actionImp==0)) {
  2093. /* there is an unprocessed sequence if its property == eor */
  2094. actionImp=1; /* process the last sequence */
  2095. }
  2096. if(actionImp) {
  2097. resProp=impTabProps[oldStateImp][IMPTABPROPS_RES];
  2098. switch(actionImp) {
  2099. case 1: /* process current seq1, init new seq1 */
  2100. processPropertySeq(pBiDi, &levState, resProp, start1, i);
  2101. start1=i;
  2102. break;
  2103. case 2: /* init new seq2 */
  2104. start2=i;
  2105. break;
  2106. case 3: /* process seq1, process seq2, init new seq1 */
  2107. processPropertySeq(pBiDi, &levState, resProp, start1, start2);
  2108. processPropertySeq(pBiDi, &levState, DirProp_ON, start2, i);
  2109. start1=i;
  2110. break;
  2111. case 4: /* process seq1, set seq1=seq2, init new seq2 */
  2112. processPropertySeq(pBiDi, &levState, resProp, start1, start2);
  2113. start1=start2;
  2114. start2=i;
  2115. break;
  2116. default: /* we should never get here */
  2117. UPRV_UNREACHABLE_EXIT;
  2118. }
  2119. }
  2120. }
  2121. /* flush possible pending sequence, e.g. ON */
  2122. if(limit==pBiDi->length && pBiDi->epiLength>0) {
  2123. DirProp firstStrong=firstL_R_AL_EN_AN(pBiDi);
  2124. if(firstStrong!=DirProp_ON) {
  2125. eor=firstStrong;
  2126. }
  2127. }
  2128. /* look for the last char not a BN or LRE/RLE/LRO/RLO/PDF */
  2129. for(i=limit-1; i>start&&(DIRPROP_FLAG(dirProps[i])&MASK_BN_EXPLICIT); i--);
  2130. dirProp=dirProps[i];
  2131. if((dirProp==LRI || dirProp==RLI) && limit<pBiDi->length) {
  2132. pBiDi->isolateCount++;
  2133. pBiDi->isolates[pBiDi->isolateCount].stateImp=stateImp;
  2134. pBiDi->isolates[pBiDi->isolateCount].state=levState.state;
  2135. pBiDi->isolates[pBiDi->isolateCount].start1=start1;
  2136. pBiDi->isolates[pBiDi->isolateCount].startON=levState.startON;
  2137. }
  2138. else
  2139. processPropertySeq(pBiDi, &levState, eor, limit, limit);
  2140. }
  2141. /* perform (L1) and (X9) ---------------------------------------------------- */
  2142. /*
  2143. * Reset the embedding levels for some non-graphic characters (L1).
  2144. * This function also sets appropriate levels for BN, and
  2145. * explicit embedding types that are supposed to have been removed
  2146. * from the paragraph in (X9).
  2147. */
  2148. static void
  2149. adjustWSLevels(UBiDi *pBiDi) {
  2150. const DirProp *dirProps=pBiDi->dirProps;
  2151. UBiDiLevel *levels=pBiDi->levels;
  2152. int32_t i;
  2153. if(pBiDi->flags&MASK_WS) {
  2154. UBool orderParagraphsLTR=pBiDi->orderParagraphsLTR;
  2155. Flags flag;
  2156. i=pBiDi->trailingWSStart;
  2157. while(i>0) {
  2158. /* reset a sequence of WS/BN before eop and B/S to the paragraph paraLevel */
  2159. while(i>0 && (flag=DIRPROP_FLAG(dirProps[--i]))&MASK_WS) {
  2160. if(orderParagraphsLTR&&(flag&DIRPROP_FLAG(B))) {
  2161. levels[i]=0;
  2162. } else {
  2163. levels[i]=GET_PARALEVEL(pBiDi, i);
  2164. }
  2165. }
  2166. /* reset BN to the next character's paraLevel until B/S, which restarts above loop */
  2167. /* here, i+1 is guaranteed to be <length */
  2168. while(i>0) {
  2169. flag=DIRPROP_FLAG(dirProps[--i]);
  2170. if(flag&MASK_BN_EXPLICIT) {
  2171. levels[i]=levels[i+1];
  2172. } else if(orderParagraphsLTR&&(flag&DIRPROP_FLAG(B))) {
  2173. levels[i]=0;
  2174. break;
  2175. } else if(flag&MASK_B_S) {
  2176. levels[i]=GET_PARALEVEL(pBiDi, i);
  2177. break;
  2178. }
  2179. }
  2180. }
  2181. }
  2182. }
  2183. U_CAPI void U_EXPORT2
  2184. ubidi_setContext(UBiDi *pBiDi,
  2185. const char16_t *prologue, int32_t proLength,
  2186. const char16_t *epilogue, int32_t epiLength,
  2187. UErrorCode *pErrorCode) {
  2188. /* check the argument values */
  2189. RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode);
  2190. if(pBiDi==nullptr || proLength<-1 || epiLength<-1 ||
  2191. (prologue==nullptr && proLength!=0) || (epilogue==nullptr && epiLength!=0)) {
  2192. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  2193. return;
  2194. }
  2195. if(proLength==-1) {
  2196. pBiDi->proLength=u_strlen(prologue);
  2197. } else {
  2198. pBiDi->proLength=proLength;
  2199. }
  2200. if(epiLength==-1) {
  2201. pBiDi->epiLength=u_strlen(epilogue);
  2202. } else {
  2203. pBiDi->epiLength=epiLength;
  2204. }
  2205. pBiDi->prologue=prologue;
  2206. pBiDi->epilogue=epilogue;
  2207. }
  2208. static void
  2209. setParaSuccess(UBiDi *pBiDi) {
  2210. pBiDi->proLength=0; /* forget the last context */
  2211. pBiDi->epiLength=0;
  2212. pBiDi->pParaBiDi=pBiDi; /* mark successful setPara */
  2213. }
  2214. #define BIDI_MIN(x, y) ((x)<(y) ? (x) : (y))
  2215. #define BIDI_ABS(x) ((x)>=0 ? (x) : (-(x)))
  2216. static void
  2217. setParaRunsOnly(UBiDi *pBiDi, const char16_t *text, int32_t length,
  2218. UBiDiLevel paraLevel, UErrorCode *pErrorCode) {
  2219. int32_t *runsOnlyMemory = nullptr;
  2220. int32_t *visualMap;
  2221. char16_t *visualText;
  2222. int32_t saveLength, saveTrailingWSStart;
  2223. const UBiDiLevel *levels;
  2224. UBiDiLevel *saveLevels;
  2225. UBiDiDirection saveDirection;
  2226. UBool saveMayAllocateText;
  2227. Run *runs;
  2228. int32_t visualLength, i, j, visualStart, logicalStart,
  2229. runCount, runLength, addedRuns, insertRemove,
  2230. start, limit, step, indexOddBit, logicalPos,
  2231. index0, index1;
  2232. uint32_t saveOptions;
  2233. pBiDi->reorderingMode=UBIDI_REORDER_DEFAULT;
  2234. if(length==0) {
  2235. ubidi_setPara(pBiDi, text, length, paraLevel, nullptr, pErrorCode);
  2236. goto cleanup3;
  2237. }
  2238. /* obtain memory for mapping table and visual text */
  2239. runsOnlyMemory=static_cast<int32_t *>(uprv_malloc(length*(sizeof(int32_t)+sizeof(char16_t)+sizeof(UBiDiLevel))));
  2240. if(runsOnlyMemory==nullptr) {
  2241. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  2242. goto cleanup3;
  2243. }
  2244. visualMap=runsOnlyMemory;
  2245. visualText=(char16_t *)&visualMap[length];
  2246. saveLevels=(UBiDiLevel *)&visualText[length];
  2247. saveOptions=pBiDi->reorderingOptions;
  2248. if(saveOptions & UBIDI_OPTION_INSERT_MARKS) {
  2249. pBiDi->reorderingOptions&=~UBIDI_OPTION_INSERT_MARKS;
  2250. pBiDi->reorderingOptions|=UBIDI_OPTION_REMOVE_CONTROLS;
  2251. }
  2252. paraLevel&=1; /* accept only 0 or 1 */
  2253. ubidi_setPara(pBiDi, text, length, paraLevel, nullptr, pErrorCode);
  2254. if(U_FAILURE(*pErrorCode)) {
  2255. goto cleanup3;
  2256. }
  2257. /* we cannot access directly pBiDi->levels since it is not yet set if
  2258. * direction is not MIXED
  2259. */
  2260. levels=ubidi_getLevels(pBiDi, pErrorCode);
  2261. uprv_memcpy(saveLevels, levels, (size_t)pBiDi->length*sizeof(UBiDiLevel));
  2262. saveTrailingWSStart=pBiDi->trailingWSStart;
  2263. saveLength=pBiDi->length;
  2264. saveDirection=pBiDi->direction;
  2265. /* FOOD FOR THOUGHT: instead of writing the visual text, we could use
  2266. * the visual map and the dirProps array to drive the second call
  2267. * to ubidi_setPara (but must make provision for possible removal of
  2268. * BiDi controls. Alternatively, only use the dirProps array via
  2269. * customized classifier callback.
  2270. */
  2271. visualLength=ubidi_writeReordered(pBiDi, visualText, length,
  2272. UBIDI_DO_MIRRORING, pErrorCode);
  2273. ubidi_getVisualMap(pBiDi, visualMap, pErrorCode);
  2274. if(U_FAILURE(*pErrorCode)) {
  2275. goto cleanup2;
  2276. }
  2277. pBiDi->reorderingOptions=saveOptions;
  2278. pBiDi->reorderingMode=UBIDI_REORDER_INVERSE_LIKE_DIRECT;
  2279. paraLevel^=1;
  2280. /* Because what we did with reorderingOptions, visualText may be shorter
  2281. * than the original text. But we don't want the levels memory to be
  2282. * reallocated shorter than the original length, since we need to restore
  2283. * the levels as after the first call to ubidi_setpara() before returning.
  2284. * We will force mayAllocateText to false before the second call to
  2285. * ubidi_setpara(), and will restore it afterwards.
  2286. */
  2287. saveMayAllocateText=pBiDi->mayAllocateText;
  2288. pBiDi->mayAllocateText=false;
  2289. ubidi_setPara(pBiDi, visualText, visualLength, paraLevel, nullptr, pErrorCode);
  2290. pBiDi->mayAllocateText=saveMayAllocateText;
  2291. ubidi_getRuns(pBiDi, pErrorCode);
  2292. if(U_FAILURE(*pErrorCode)) {
  2293. goto cleanup1;
  2294. }
  2295. /* check if some runs must be split, count how many splits */
  2296. addedRuns=0;
  2297. runCount=pBiDi->runCount;
  2298. runs=pBiDi->runs;
  2299. visualStart=0;
  2300. for(i=0; i<runCount; i++, visualStart+=runLength) {
  2301. runLength=runs[i].visualLimit-visualStart;
  2302. if(runLength<2) {
  2303. continue;
  2304. }
  2305. logicalStart=GET_INDEX(runs[i].logicalStart);
  2306. for(j=logicalStart+1; j<logicalStart+runLength; j++) {
  2307. index0=visualMap[j];
  2308. index1=visualMap[j-1];
  2309. if((BIDI_ABS(index0-index1)!=1) || (saveLevels[index0]!=saveLevels[index1])) {
  2310. addedRuns++;
  2311. }
  2312. }
  2313. }
  2314. if(addedRuns) {
  2315. if(getRunsMemory(pBiDi, runCount+addedRuns)) {
  2316. if(runCount==1) {
  2317. /* because we switch from UBiDi.simpleRuns to UBiDi.runs */
  2318. pBiDi->runsMemory[0]=runs[0];
  2319. }
  2320. runs=pBiDi->runs=pBiDi->runsMemory;
  2321. pBiDi->runCount+=addedRuns;
  2322. } else {
  2323. goto cleanup1;
  2324. }
  2325. }
  2326. /* split runs which are not consecutive in source text */
  2327. for(i=runCount-1; i>=0; i--) {
  2328. runLength= i==0 ? runs[0].visualLimit :
  2329. runs[i].visualLimit-runs[i-1].visualLimit;
  2330. logicalStart=runs[i].logicalStart;
  2331. indexOddBit=GET_ODD_BIT(logicalStart);
  2332. logicalStart=GET_INDEX(logicalStart);
  2333. if(runLength<2) {
  2334. if(addedRuns) {
  2335. runs[i+addedRuns]=runs[i];
  2336. }
  2337. logicalPos=visualMap[logicalStart];
  2338. runs[i+addedRuns].logicalStart=MAKE_INDEX_ODD_PAIR(logicalPos,
  2339. saveLevels[logicalPos]^indexOddBit);
  2340. continue;
  2341. }
  2342. if(indexOddBit) {
  2343. start=logicalStart;
  2344. limit=logicalStart+runLength-1;
  2345. step=1;
  2346. } else {
  2347. start=logicalStart+runLength-1;
  2348. limit=logicalStart;
  2349. step=-1;
  2350. }
  2351. for(j=start; j!=limit; j+=step) {
  2352. index0=visualMap[j];
  2353. index1=visualMap[j+step];
  2354. if((BIDI_ABS(index0-index1)!=1) || (saveLevels[index0]!=saveLevels[index1])) {
  2355. logicalPos=BIDI_MIN(visualMap[start], index0);
  2356. runs[i+addedRuns].logicalStart=MAKE_INDEX_ODD_PAIR(logicalPos,
  2357. saveLevels[logicalPos]^indexOddBit);
  2358. runs[i+addedRuns].visualLimit=runs[i].visualLimit;
  2359. runs[i].visualLimit-=BIDI_ABS(j-start)+1;
  2360. insertRemove=runs[i].insertRemove&(LRM_AFTER|RLM_AFTER);
  2361. runs[i+addedRuns].insertRemove=insertRemove;
  2362. runs[i].insertRemove&=~insertRemove;
  2363. start=j+step;
  2364. addedRuns--;
  2365. }
  2366. }
  2367. if(addedRuns) {
  2368. runs[i+addedRuns]=runs[i];
  2369. }
  2370. logicalPos=BIDI_MIN(visualMap[start], visualMap[limit]);
  2371. runs[i+addedRuns].logicalStart=MAKE_INDEX_ODD_PAIR(logicalPos,
  2372. saveLevels[logicalPos]^indexOddBit);
  2373. }
  2374. cleanup1:
  2375. /* restore initial paraLevel */
  2376. pBiDi->paraLevel^=1;
  2377. cleanup2:
  2378. /* restore real text */
  2379. pBiDi->text=text;
  2380. pBiDi->length=saveLength;
  2381. pBiDi->originalLength=length;
  2382. pBiDi->direction=saveDirection;
  2383. /* the saved levels should never excess levelsSize, but we check anyway */
  2384. if(saveLength>pBiDi->levelsSize) {
  2385. saveLength=pBiDi->levelsSize;
  2386. }
  2387. uprv_memcpy(pBiDi->levels, saveLevels, (size_t)saveLength*sizeof(UBiDiLevel));
  2388. pBiDi->trailingWSStart=saveTrailingWSStart;
  2389. if(pBiDi->runCount>1) {
  2390. pBiDi->direction=UBIDI_MIXED;
  2391. }
  2392. cleanup3:
  2393. /* free memory for mapping table and visual text */
  2394. uprv_free(runsOnlyMemory);
  2395. pBiDi->reorderingMode=UBIDI_REORDER_RUNS_ONLY;
  2396. }
  2397. /* ubidi_setPara ------------------------------------------------------------ */
  2398. U_CAPI void U_EXPORT2
  2399. ubidi_setPara(UBiDi *pBiDi, const char16_t *text, int32_t length,
  2400. UBiDiLevel paraLevel, UBiDiLevel *embeddingLevels,
  2401. UErrorCode *pErrorCode) {
  2402. UBiDiDirection direction;
  2403. DirProp *dirProps;
  2404. /* check the argument values */
  2405. RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode);
  2406. if(pBiDi==nullptr || text==nullptr || length<-1 ||
  2407. (paraLevel>UBIDI_MAX_EXPLICIT_LEVEL && paraLevel<UBIDI_DEFAULT_LTR)) {
  2408. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  2409. return;
  2410. }
  2411. if(length==-1) {
  2412. length=u_strlen(text);
  2413. }
  2414. /* special treatment for RUNS_ONLY mode */
  2415. if(pBiDi->reorderingMode==UBIDI_REORDER_RUNS_ONLY) {
  2416. setParaRunsOnly(pBiDi, text, length, paraLevel, pErrorCode);
  2417. return;
  2418. }
  2419. /* initialize the UBiDi structure */
  2420. pBiDi->pParaBiDi=nullptr; /* mark unfinished setPara */
  2421. pBiDi->text=text;
  2422. pBiDi->length=pBiDi->originalLength=pBiDi->resultLength=length;
  2423. pBiDi->paraLevel=paraLevel;
  2424. pBiDi->direction=(UBiDiDirection)(paraLevel&1);
  2425. pBiDi->paraCount=1;
  2426. pBiDi->dirProps=nullptr;
  2427. pBiDi->levels=nullptr;
  2428. pBiDi->runs=nullptr;
  2429. pBiDi->insertPoints.size=0; /* clean up from last call */
  2430. pBiDi->insertPoints.confirmed=0; /* clean up from last call */
  2431. /*
  2432. * Save the original paraLevel if contextual; otherwise, set to 0.
  2433. */
  2434. pBiDi->defaultParaLevel=IS_DEFAULT_LEVEL(paraLevel);
  2435. if(length==0) {
  2436. /*
  2437. * For an empty paragraph, create a UBiDi object with the paraLevel and
  2438. * the flags and the direction set but without allocating zero-length arrays.
  2439. * There is nothing more to do.
  2440. */
  2441. if(IS_DEFAULT_LEVEL(paraLevel)) {
  2442. pBiDi->paraLevel&=1;
  2443. pBiDi->defaultParaLevel=0;
  2444. }
  2445. pBiDi->flags=DIRPROP_FLAG_LR(paraLevel);
  2446. pBiDi->runCount=0;
  2447. pBiDi->paraCount=0;
  2448. setParaSuccess(pBiDi); /* mark successful setPara */
  2449. return;
  2450. }
  2451. pBiDi->runCount=-1;
  2452. /* allocate paras memory */
  2453. if(pBiDi->parasMemory)
  2454. pBiDi->paras=pBiDi->parasMemory;
  2455. else
  2456. pBiDi->paras=pBiDi->simpleParas;
  2457. /*
  2458. * Get the directional properties,
  2459. * the flags bit-set, and
  2460. * determine the paragraph level if necessary.
  2461. */
  2462. if(getDirPropsMemory(pBiDi, length)) {
  2463. pBiDi->dirProps=pBiDi->dirPropsMemory;
  2464. if(!getDirProps(pBiDi)) {
  2465. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  2466. return;
  2467. }
  2468. } else {
  2469. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  2470. return;
  2471. }
  2472. dirProps=pBiDi->dirProps;
  2473. /* the processed length may have changed if UBIDI_OPTION_STREAMING */
  2474. length= pBiDi->length;
  2475. pBiDi->trailingWSStart=length; /* the levels[] will reflect the WS run */
  2476. /* are explicit levels specified? */
  2477. if(embeddingLevels==nullptr) {
  2478. /* no: determine explicit levels according to the (Xn) rules */\
  2479. if(getLevelsMemory(pBiDi, length)) {
  2480. pBiDi->levels=pBiDi->levelsMemory;
  2481. direction=resolveExplicitLevels(pBiDi, pErrorCode);
  2482. if(U_FAILURE(*pErrorCode)) {
  2483. return;
  2484. }
  2485. } else {
  2486. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  2487. return;
  2488. }
  2489. } else {
  2490. /* set BN for all explicit codes, check that all levels are 0 or paraLevel..UBIDI_MAX_EXPLICIT_LEVEL */
  2491. pBiDi->levels=embeddingLevels;
  2492. direction=checkExplicitLevels(pBiDi, pErrorCode);
  2493. if(U_FAILURE(*pErrorCode)) {
  2494. return;
  2495. }
  2496. }
  2497. /* allocate isolate memory */
  2498. if(pBiDi->isolateCount<=SIMPLE_ISOLATES_COUNT)
  2499. pBiDi->isolates=pBiDi->simpleIsolates;
  2500. else
  2501. if((int32_t)(pBiDi->isolateCount*sizeof(Isolate))<=pBiDi->isolatesSize)
  2502. pBiDi->isolates=pBiDi->isolatesMemory;
  2503. else {
  2504. if(getInitialIsolatesMemory(pBiDi, pBiDi->isolateCount)) {
  2505. pBiDi->isolates=pBiDi->isolatesMemory;
  2506. } else {
  2507. *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  2508. return;
  2509. }
  2510. }
  2511. pBiDi->isolateCount=-1; /* current isolates stack entry == none */
  2512. /*
  2513. * The steps after (X9) in the UBiDi algorithm are performed only if
  2514. * the paragraph text has mixed directionality!
  2515. */
  2516. pBiDi->direction=direction;
  2517. switch(direction) {
  2518. case UBIDI_LTR:
  2519. /* all levels are implicitly at paraLevel (important for ubidi_getLevels()) */
  2520. pBiDi->trailingWSStart=0;
  2521. break;
  2522. case UBIDI_RTL:
  2523. /* all levels are implicitly at paraLevel (important for ubidi_getLevels()) */
  2524. pBiDi->trailingWSStart=0;
  2525. break;
  2526. default:
  2527. /*
  2528. * Choose the right implicit state table
  2529. */
  2530. switch(pBiDi->reorderingMode) {
  2531. case UBIDI_REORDER_DEFAULT:
  2532. pBiDi->pImpTabPair=&impTab_DEFAULT;
  2533. break;
  2534. case UBIDI_REORDER_NUMBERS_SPECIAL:
  2535. pBiDi->pImpTabPair=&impTab_NUMBERS_SPECIAL;
  2536. break;
  2537. case UBIDI_REORDER_GROUP_NUMBERS_WITH_R:
  2538. pBiDi->pImpTabPair=&impTab_GROUP_NUMBERS_WITH_R;
  2539. break;
  2540. case UBIDI_REORDER_INVERSE_NUMBERS_AS_L:
  2541. pBiDi->pImpTabPair=&impTab_INVERSE_NUMBERS_AS_L;
  2542. break;
  2543. case UBIDI_REORDER_INVERSE_LIKE_DIRECT:
  2544. if (pBiDi->reorderingOptions & UBIDI_OPTION_INSERT_MARKS) {
  2545. pBiDi->pImpTabPair=&impTab_INVERSE_LIKE_DIRECT_WITH_MARKS;
  2546. } else {
  2547. pBiDi->pImpTabPair=&impTab_INVERSE_LIKE_DIRECT;
  2548. }
  2549. break;
  2550. case UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL:
  2551. if (pBiDi->reorderingOptions & UBIDI_OPTION_INSERT_MARKS) {
  2552. pBiDi->pImpTabPair=&impTab_INVERSE_FOR_NUMBERS_SPECIAL_WITH_MARKS;
  2553. } else {
  2554. pBiDi->pImpTabPair=&impTab_INVERSE_FOR_NUMBERS_SPECIAL;
  2555. }
  2556. break;
  2557. default:
  2558. /* we should never get here */
  2559. UPRV_UNREACHABLE_EXIT;
  2560. }
  2561. /*
  2562. * If there are no external levels specified and there
  2563. * are no significant explicit level codes in the text,
  2564. * then we can treat the entire paragraph as one run.
  2565. * Otherwise, we need to perform the following rules on runs of
  2566. * the text with the same embedding levels. (X10)
  2567. * "Significant" explicit level codes are ones that actually
  2568. * affect non-BN characters.
  2569. * Examples for "insignificant" ones are empty embeddings
  2570. * LRE-PDF, LRE-RLE-PDF-PDF, etc.
  2571. */
  2572. if(embeddingLevels==nullptr && pBiDi->paraCount<=1 &&
  2573. !(pBiDi->flags&DIRPROP_FLAG_MULTI_RUNS)) {
  2574. resolveImplicitLevels(pBiDi, 0, length,
  2575. GET_LR_FROM_LEVEL(GET_PARALEVEL(pBiDi, 0)),
  2576. GET_LR_FROM_LEVEL(GET_PARALEVEL(pBiDi, length-1)));
  2577. } else {
  2578. /* sor, eor: start and end types of same-level-run */
  2579. UBiDiLevel *levels=pBiDi->levels;
  2580. int32_t start, limit=0;
  2581. UBiDiLevel level, nextLevel;
  2582. DirProp sor, eor;
  2583. /* determine the first sor and set eor to it because of the loop body (sor=eor there) */
  2584. level=GET_PARALEVEL(pBiDi, 0);
  2585. nextLevel=levels[0];
  2586. if(level<nextLevel) {
  2587. eor=GET_LR_FROM_LEVEL(nextLevel);
  2588. } else {
  2589. eor=GET_LR_FROM_LEVEL(level);
  2590. }
  2591. do {
  2592. /* determine start and limit of the run (end points just behind the run) */
  2593. /* the values for this run's start are the same as for the previous run's end */
  2594. start=limit;
  2595. level=nextLevel;
  2596. if((start>0) && (dirProps[start-1]==B)) {
  2597. /* except if this is a new paragraph, then set sor = para level */
  2598. sor=GET_LR_FROM_LEVEL(GET_PARALEVEL(pBiDi, start));
  2599. } else {
  2600. sor=eor;
  2601. }
  2602. /* search for the limit of this run */
  2603. while((++limit<length) &&
  2604. ((levels[limit]==level) ||
  2605. (DIRPROP_FLAG(dirProps[limit])&MASK_BN_EXPLICIT))) {}
  2606. /* get the correct level of the next run */
  2607. if(limit<length) {
  2608. nextLevel=levels[limit];
  2609. } else {
  2610. nextLevel=GET_PARALEVEL(pBiDi, length-1);
  2611. }
  2612. /* determine eor from max(level, nextLevel); sor is last run's eor */
  2613. if(NO_OVERRIDE(level)<NO_OVERRIDE(nextLevel)) {
  2614. eor=GET_LR_FROM_LEVEL(nextLevel);
  2615. } else {
  2616. eor=GET_LR_FROM_LEVEL(level);
  2617. }
  2618. /* if the run consists of overridden directional types, then there
  2619. are no implicit types to be resolved */
  2620. if(!(level&UBIDI_LEVEL_OVERRIDE)) {
  2621. resolveImplicitLevels(pBiDi, start, limit, sor, eor);
  2622. } else {
  2623. /* remove the UBIDI_LEVEL_OVERRIDE flags */
  2624. do {
  2625. levels[start++]&=~UBIDI_LEVEL_OVERRIDE;
  2626. } while(start<limit);
  2627. }
  2628. } while(limit<length);
  2629. }
  2630. /* check if we got any memory shortage while adding insert points */
  2631. if (U_FAILURE(pBiDi->insertPoints.errorCode))
  2632. {
  2633. *pErrorCode=pBiDi->insertPoints.errorCode;
  2634. return;
  2635. }
  2636. /* reset the embedding levels for some non-graphic characters (L1), (X9) */
  2637. adjustWSLevels(pBiDi);
  2638. break;
  2639. }
  2640. /* add RLM for inverse Bidi with contextual orientation resolving
  2641. * to RTL which would not round-trip otherwise
  2642. */
  2643. if((pBiDi->defaultParaLevel>0) &&
  2644. (pBiDi->reorderingOptions & UBIDI_OPTION_INSERT_MARKS) &&
  2645. ((pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_LIKE_DIRECT) ||
  2646. (pBiDi->reorderingMode==UBIDI_REORDER_INVERSE_FOR_NUMBERS_SPECIAL))) {
  2647. int32_t i, j, start, last;
  2648. UBiDiLevel level;
  2649. DirProp dirProp;
  2650. for(i=0; i<pBiDi->paraCount; i++) {
  2651. last=(pBiDi->paras[i].limit)-1;
  2652. level= static_cast<UBiDiLevel>(pBiDi->paras[i].level);
  2653. if(level==0)
  2654. continue; /* LTR paragraph */
  2655. start= i==0 ? 0 : pBiDi->paras[i-1].limit;
  2656. for(j=last; j>=start; j--) {
  2657. dirProp=dirProps[j];
  2658. if(dirProp==L) {
  2659. if(j<last) {
  2660. while(dirProps[last]==B) {
  2661. last--;
  2662. }
  2663. }
  2664. addPoint(pBiDi, last, RLM_BEFORE);
  2665. break;
  2666. }
  2667. if(DIRPROP_FLAG(dirProp) & MASK_R_AL) {
  2668. break;
  2669. }
  2670. }
  2671. }
  2672. }
  2673. if(pBiDi->reorderingOptions & UBIDI_OPTION_REMOVE_CONTROLS) {
  2674. pBiDi->resultLength -= pBiDi->controlCount;
  2675. } else {
  2676. pBiDi->resultLength += pBiDi->insertPoints.size;
  2677. }
  2678. setParaSuccess(pBiDi); /* mark successful setPara */
  2679. }
  2680. U_CAPI void U_EXPORT2
  2681. ubidi_orderParagraphsLTR(UBiDi *pBiDi, UBool orderParagraphsLTR) {
  2682. if(pBiDi!=nullptr) {
  2683. pBiDi->orderParagraphsLTR=orderParagraphsLTR;
  2684. }
  2685. }
  2686. U_CAPI UBool U_EXPORT2
  2687. ubidi_isOrderParagraphsLTR(UBiDi *pBiDi) {
  2688. if(pBiDi!=nullptr) {
  2689. return pBiDi->orderParagraphsLTR;
  2690. } else {
  2691. return false;
  2692. }
  2693. }
  2694. U_CAPI UBiDiDirection U_EXPORT2
  2695. ubidi_getDirection(const UBiDi *pBiDi) {
  2696. if(IS_VALID_PARA_OR_LINE(pBiDi)) {
  2697. return pBiDi->direction;
  2698. } else {
  2699. return UBIDI_LTR;
  2700. }
  2701. }
  2702. U_CAPI const char16_t * U_EXPORT2
  2703. ubidi_getText(const UBiDi *pBiDi) {
  2704. if(IS_VALID_PARA_OR_LINE(pBiDi)) {
  2705. return pBiDi->text;
  2706. } else {
  2707. return nullptr;
  2708. }
  2709. }
  2710. U_CAPI int32_t U_EXPORT2
  2711. ubidi_getLength(const UBiDi *pBiDi) {
  2712. if(IS_VALID_PARA_OR_LINE(pBiDi)) {
  2713. return pBiDi->originalLength;
  2714. } else {
  2715. return 0;
  2716. }
  2717. }
  2718. U_CAPI int32_t U_EXPORT2
  2719. ubidi_getProcessedLength(const UBiDi *pBiDi) {
  2720. if(IS_VALID_PARA_OR_LINE(pBiDi)) {
  2721. return pBiDi->length;
  2722. } else {
  2723. return 0;
  2724. }
  2725. }
  2726. U_CAPI int32_t U_EXPORT2
  2727. ubidi_getResultLength(const UBiDi *pBiDi) {
  2728. if(IS_VALID_PARA_OR_LINE(pBiDi)) {
  2729. return pBiDi->resultLength;
  2730. } else {
  2731. return 0;
  2732. }
  2733. }
  2734. /* paragraphs API functions ------------------------------------------------- */
  2735. U_CAPI UBiDiLevel U_EXPORT2
  2736. ubidi_getParaLevel(const UBiDi *pBiDi) {
  2737. if(IS_VALID_PARA_OR_LINE(pBiDi)) {
  2738. return pBiDi->paraLevel;
  2739. } else {
  2740. return 0;
  2741. }
  2742. }
  2743. U_CAPI int32_t U_EXPORT2
  2744. ubidi_countParagraphs(UBiDi *pBiDi) {
  2745. if(!IS_VALID_PARA_OR_LINE(pBiDi)) {
  2746. return 0;
  2747. } else {
  2748. return pBiDi->paraCount;
  2749. }
  2750. }
  2751. U_CAPI void U_EXPORT2
  2752. ubidi_getParagraphByIndex(const UBiDi *pBiDi, int32_t paraIndex,
  2753. int32_t *pParaStart, int32_t *pParaLimit,
  2754. UBiDiLevel *pParaLevel, UErrorCode *pErrorCode) {
  2755. int32_t paraStart;
  2756. /* check the argument values */
  2757. RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode);
  2758. RETURN_VOID_IF_NOT_VALID_PARA_OR_LINE(pBiDi, *pErrorCode);
  2759. RETURN_VOID_IF_BAD_RANGE(paraIndex, 0, pBiDi->paraCount, *pErrorCode);
  2760. pBiDi=pBiDi->pParaBiDi; /* get Para object if Line object */
  2761. if(paraIndex) {
  2762. paraStart=pBiDi->paras[paraIndex-1].limit;
  2763. } else {
  2764. paraStart=0;
  2765. }
  2766. if(pParaStart!=nullptr) {
  2767. *pParaStart=paraStart;
  2768. }
  2769. if(pParaLimit!=nullptr) {
  2770. *pParaLimit=pBiDi->paras[paraIndex].limit;
  2771. }
  2772. if(pParaLevel!=nullptr) {
  2773. *pParaLevel=GET_PARALEVEL(pBiDi, paraStart);
  2774. }
  2775. }
  2776. U_CAPI int32_t U_EXPORT2
  2777. ubidi_getParagraph(const UBiDi *pBiDi, int32_t charIndex,
  2778. int32_t *pParaStart, int32_t *pParaLimit,
  2779. UBiDiLevel *pParaLevel, UErrorCode *pErrorCode) {
  2780. int32_t paraIndex;
  2781. /* check the argument values */
  2782. /* pErrorCode will be checked by the call to ubidi_getParagraphByIndex */
  2783. RETURN_IF_NULL_OR_FAILING_ERRCODE(pErrorCode, -1);
  2784. RETURN_IF_NOT_VALID_PARA_OR_LINE(pBiDi, *pErrorCode, -1);
  2785. pBiDi=pBiDi->pParaBiDi; /* get Para object if Line object */
  2786. RETURN_IF_BAD_RANGE(charIndex, 0, pBiDi->length, *pErrorCode, -1);
  2787. for(paraIndex=0; charIndex>=pBiDi->paras[paraIndex].limit; paraIndex++);
  2788. ubidi_getParagraphByIndex(pBiDi, paraIndex, pParaStart, pParaLimit, pParaLevel, pErrorCode);
  2789. return paraIndex;
  2790. }
  2791. U_CAPI void U_EXPORT2
  2792. ubidi_setClassCallback(UBiDi *pBiDi, UBiDiClassCallback *newFn,
  2793. const void *newContext, UBiDiClassCallback **oldFn,
  2794. const void **oldContext, UErrorCode *pErrorCode)
  2795. {
  2796. RETURN_VOID_IF_NULL_OR_FAILING_ERRCODE(pErrorCode);
  2797. if(pBiDi==nullptr) {
  2798. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  2799. return;
  2800. }
  2801. if( oldFn )
  2802. {
  2803. *oldFn = pBiDi->fnClassCallback;
  2804. }
  2805. if( oldContext )
  2806. {
  2807. *oldContext = pBiDi->coClassCallback;
  2808. }
  2809. pBiDi->fnClassCallback = newFn;
  2810. pBiDi->coClassCallback = newContext;
  2811. }
  2812. U_CAPI void U_EXPORT2
  2813. ubidi_getClassCallback(UBiDi *pBiDi, UBiDiClassCallback **fn, const void **context)
  2814. {
  2815. if(pBiDi==nullptr) {
  2816. return;
  2817. }
  2818. if( fn )
  2819. {
  2820. *fn = pBiDi->fnClassCallback;
  2821. }
  2822. if( context )
  2823. {
  2824. *context = pBiDi->coClassCallback;
  2825. }
  2826. }
  2827. U_CAPI UCharDirection U_EXPORT2
  2828. ubidi_getCustomizedClass(UBiDi *pBiDi, UChar32 c)
  2829. {
  2830. UCharDirection dir;
  2831. if( pBiDi->fnClassCallback == nullptr ||
  2832. (dir = (*pBiDi->fnClassCallback)(pBiDi->coClassCallback, c)) == U_BIDI_CLASS_DEFAULT )
  2833. {
  2834. dir = ubidi_getClass(c);
  2835. }
  2836. if(dir >= U_CHAR_DIRECTION_COUNT) {
  2837. dir = (UCharDirection)ON;
  2838. }
  2839. return dir;
  2840. }