PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/pango/mini-fribidi/fribidi.c

https://gitlab.com/ImageMagick/pango
C | 967 lines | 636 code | 110 blank | 221 comment | 148 complexity | 7692e24044a388ac2055ae6539413e53 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /* FriBidi - Library of BiDi algorithm
  2. * Copyright (C) 1999,2000 Dov Grobgeld, and
  3. * Copyright (C) 2001,2002 Behdad Esfahbod.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this library, in a file named COPYING; if not, write to the
  17. * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  18. * Boston, MA 02111-1307, USA
  19. *
  20. * For licensing issues, contact <dov@imagic.weizmann.ac.il> and
  21. * <fwpg@sharif.edu>.
  22. */
  23. #include "config.h"
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "fribidi.h"
  27. /* Redefine FRIBIDI_CHUNK_SIZE in config.h to override this. */
  28. #ifndef FRIBIDI_CHUNK_SIZE
  29. #ifdef MEM_OPTIMIZED
  30. #define FRIBIDI_CHUNK_SIZE 16
  31. #else
  32. #define FRIBIDI_CHUNK_SIZE 128
  33. #endif
  34. #endif
  35. #define DBG(s)
  36. #define DBG2(s, t)
  37. /*======================================================================
  38. * Typedef for the run-length list.
  39. *----------------------------------------------------------------------*/
  40. typedef struct _TypeLink TypeLink;
  41. struct _TypeLink
  42. {
  43. TypeLink *prev;
  44. TypeLink *next;
  45. FriBidiStrIndex pos, len;
  46. FriBidiCharType type;
  47. FriBidiLevel level;
  48. };
  49. #define FRIBIDI_LEVEL_START -1
  50. #define FRIBIDI_LEVEL_END -1
  51. #define FRIBIDI_LEVEL_REMOVED -2
  52. typedef struct
  53. {
  54. FriBidiCharType override; /* only L, R and N are valid */
  55. FriBidiLevel level;
  56. }
  57. LevelInfo;
  58. static TypeLink *
  59. new_type_link (void)
  60. {
  61. TypeLink *link;
  62. link = g_slice_new0 (TypeLink);
  63. return link;
  64. }
  65. static void
  66. free_type_link (TypeLink *link)
  67. {
  68. g_slice_free (TypeLink, link);
  69. }
  70. #define FRIBIDI_ADD_TYPE_LINK(p,q) \
  71. do { \
  72. (p)->len = (q)->pos - (p)->pos; \
  73. (p)->next = (q); \
  74. (q)->prev = (p); \
  75. (p) = (q); \
  76. } while (0)
  77. static TypeLink *
  78. run_length_encode_types_utf8 (const char *s,
  79. int bytelen,
  80. FriBidiStrIndex *len,
  81. FriBidiCharType *pored_types,
  82. FriBidiCharType *panded_strongs)
  83. {
  84. TypeLink *list, *last, *link;
  85. FriBidiCharType char_type;
  86. FriBidiCharType ored_types = 0;
  87. FriBidiCharType anded_strongs = FRIBIDI_TYPE_RLE;
  88. FriBidiStrIndex i;
  89. const char *p;
  90. /* Add the starting link */
  91. list = new_type_link ();
  92. list->type = FRIBIDI_TYPE_SOT;
  93. list->level = FRIBIDI_LEVEL_START;
  94. last = list;
  95. /* Sweep over the string s */
  96. i = 0;
  97. for (p = s; p < s + bytelen; p = g_utf8_next_char(p)) {
  98. char_type = fribidi_get_type (g_utf8_get_char (p));
  99. ored_types |= char_type;
  100. if (FRIBIDI_IS_STRONG (char_type))
  101. anded_strongs &= char_type;
  102. if (char_type != last->type)
  103. {
  104. link = new_type_link ();
  105. link->type = char_type;
  106. link->pos = i;
  107. FRIBIDI_ADD_TYPE_LINK (last, link);
  108. }
  109. i++;
  110. }
  111. /* Add the ending link */
  112. link = new_type_link ();
  113. link->type = FRIBIDI_TYPE_EOT;
  114. link->level = FRIBIDI_LEVEL_END;
  115. link->pos = i;
  116. FRIBIDI_ADD_TYPE_LINK (last, link);
  117. if (len)
  118. *len = i;
  119. if (pored_types)
  120. *pored_types = ored_types;
  121. if (panded_strongs)
  122. *panded_strongs = anded_strongs;
  123. return list;
  124. }
  125. /* explicits_list is a list like type_rl_list, that holds the explicit
  126. codes that are removed from rl_list, to reinsert them later by calling
  127. the override_list.
  128. */
  129. static void
  130. init_list (TypeLink **start,
  131. TypeLink **end)
  132. {
  133. TypeLink *list;
  134. TypeLink *link;
  135. /* Add the starting link */
  136. list = new_type_link ();
  137. list->type = FRIBIDI_TYPE_SOT;
  138. list->level = FRIBIDI_LEVEL_START;
  139. list->len = 0;
  140. list->pos = 0;
  141. /* Add the ending link */
  142. link = new_type_link ();
  143. link->type = FRIBIDI_TYPE_EOT;
  144. link->level = FRIBIDI_LEVEL_END;
  145. link->len = 0;
  146. link->pos = 0;
  147. list->next = link;
  148. link->prev = list;
  149. *start = list;
  150. *end = link;
  151. }
  152. /* move an element before another element in a list, the list must have a
  153. previous element, used to update explicits_list.
  154. assuming that p have both prev and next or none of them, also update
  155. the list that p is currently in, if any.
  156. */
  157. static void
  158. move_element_before (TypeLink *p,
  159. TypeLink *list)
  160. {
  161. if (p->prev)
  162. {
  163. p->prev->next = p->next;
  164. p->next->prev = p->prev;
  165. }
  166. p->prev = list->prev;
  167. list->prev->next = p;
  168. p->next = list;
  169. list->prev = p;
  170. }
  171. /* override the rl_list 'base', with the elements in the list 'over', to
  172. reinsert the previously-removed explicit codes (at X9) from
  173. 'explicits_list' back into 'type_rl_list'. This is used at the end of I2
  174. to restore the explicit marks, and also to reset the character types of
  175. characters at L1.
  176. it is assumed that the 'pos' of the first element in 'base' list is not
  177. more than the 'pos' of the first element of the 'over' list, and the
  178. 'pos' of the last element of the 'base' list is not less than the 'pos'
  179. of the last element of the 'over' list. these two conditions are always
  180. satisfied for the two usages mentioned above.
  181. TBD: use some explanatory names instead of p, q, ...
  182. */
  183. static void
  184. override_list (TypeLink *base,
  185. TypeLink *over)
  186. {
  187. TypeLink *p = base, *q, *r, *s, *t;
  188. FriBidiStrIndex pos = 0, pos2;
  189. if (!over)
  190. return;
  191. q = over;
  192. while (q)
  193. {
  194. if (!q->len || q->pos < pos)
  195. {
  196. t = q;
  197. q = q->next;
  198. free_type_link (t);
  199. continue;
  200. }
  201. pos = q->pos;
  202. while (p->next && p->next->pos <= pos)
  203. p = p->next;
  204. /* now p is the element that q must be inserted 'in'. */
  205. pos2 = pos + q->len;
  206. r = p;
  207. while (r->next && r->next->pos < pos2)
  208. r = r->next;
  209. /* now r is the last element that q affects. */
  210. if (p == r)
  211. {
  212. /* split p into at most 3 interval, and insert q in the place of
  213. the second interval, set r to be the third part. */
  214. /* third part needed? */
  215. if (p->next && p->next->pos == pos2)
  216. r = r->next;
  217. else
  218. {
  219. r = new_type_link ();
  220. *r = *p;
  221. if (r->next)
  222. {
  223. r->next->prev = r;
  224. r->len = r->next->pos - pos2;
  225. }
  226. else
  227. r->len -= pos - p->pos;
  228. r->pos = pos2;
  229. }
  230. /* first part needed? */
  231. if (p->prev && p->pos == pos)
  232. {
  233. t = p;
  234. p = p->prev;
  235. free_type_link (t);
  236. }
  237. else
  238. p->len = pos - p->pos;
  239. }
  240. else
  241. {
  242. /* cut the end of p. */
  243. p->len = pos - p->pos;
  244. /* if all of p is cut, remove it. */
  245. if (!p->len && p->prev)
  246. p = p->prev;
  247. /* cut the begining of r. */
  248. r->pos = pos2;
  249. if (r->next)
  250. r->len = r->next->pos - pos2;
  251. /* if all of r is cut, remove it. */
  252. if (!r->len && r->next)
  253. r = r->next;
  254. /* remove the elements between p and r. */
  255. for (s = p->next; s != r;)
  256. {
  257. t = s;
  258. s = s->next;
  259. free_type_link (t);
  260. }
  261. }
  262. /* before updating the next and prev links to point to the inserted q,
  263. we must remember the next element of q in the 'over' list.
  264. */
  265. t = q;
  266. q = q->next;
  267. p->next = t;
  268. t->prev = p;
  269. t->next = r;
  270. r->prev = t;
  271. }
  272. }
  273. /* Some convenience macros */
  274. #define RL_TYPE(list) ((list)->type)
  275. #define RL_LEN(list) ((list)->len)
  276. #define RL_POS(list) ((list)->pos)
  277. #define RL_LEVEL(list) ((list)->level)
  278. static TypeLink *
  279. merge_with_prev (TypeLink *second)
  280. {
  281. TypeLink *first = second->prev;
  282. first->next = second->next;
  283. first->next->prev = first;
  284. RL_LEN (first) += RL_LEN (second);
  285. free_type_link (second);
  286. return first;
  287. }
  288. static void
  289. compact_list (TypeLink *list)
  290. {
  291. if (list->next)
  292. for (list = list->next; list; list = list->next)
  293. if (RL_TYPE (list->prev) == RL_TYPE (list)
  294. && RL_LEVEL (list->prev) == RL_LEVEL (list))
  295. list = merge_with_prev (list);
  296. }
  297. static void
  298. compact_neutrals (TypeLink *list)
  299. {
  300. if (list->next)
  301. {
  302. for (list = list->next; list; list = list->next)
  303. {
  304. if (RL_LEVEL (list->prev) == RL_LEVEL (list)
  305. &&
  306. ((RL_TYPE
  307. (list->prev) == RL_TYPE (list)
  308. || (FRIBIDI_IS_NEUTRAL (RL_TYPE (list->prev))
  309. && FRIBIDI_IS_NEUTRAL (RL_TYPE (list))))))
  310. list = merge_with_prev (list);
  311. }
  312. }
  313. }
  314. /*======================================================================
  315. * Frees up the rl_list, must be called after each call to
  316. * fribidi_analyse_string(), after the list is not needed anymore.
  317. *----------------------------------------------------------------------*/
  318. static void
  319. free_rl_list (TypeLink *type_rl_list)
  320. {
  321. DBG ("Entering free_rl_list()\n");
  322. if (!type_rl_list)
  323. {
  324. DBG ("Leaving free_rl_list()\n");
  325. return;
  326. }
  327. g_slice_free_chain (TypeLink, type_rl_list, next);
  328. DBG ("Leaving free_rl_list()\n");
  329. return;
  330. }
  331. /*=========================================================================
  332. * define macros for push and pop the status in to / out of the stack
  333. *-------------------------------------------------------------------------*/
  334. /* There's some little points in pushing and poping into the status stack:
  335. 1. when the embedding level is not valid (more than UNI_MAX_BIDI_LEVEL=61),
  336. you must reject it, and not to push into the stack, but when you see a
  337. PDF, you must find the matching code, and if it was pushed in the stack,
  338. pop it, it means you must pop if and only if you have pushed the
  339. matching code, the over_pushed var counts the number of rejected codes yet.
  340. 2. there's a more confusing point too, when the embedding level is exactly
  341. UNI_MAX_BIDI_LEVEL-1=60, an LRO or LRE must be rejected because the new
  342. level would be UNI_MAX_BIDI_LEVEL+1=62, that is invalid, but an RLO or RLE
  343. must be accepted because the new level is UNI_MAX_BIDI_LEVEL=61, that is
  344. valid, so the rejected codes may be not continuous in the logical order,
  345. in fact there is at most two continuous intervals of codes, with a RLO or
  346. RLE between them. To support this case, the first_interval var counts the
  347. number of rejected codes in the first interval, when it is 0, means that
  348. there is only one interval yet.
  349. */
  350. /* a. If this new level would be valid, then this embedding code is valid.
  351. Remember (push) the current embedding level and override status.
  352. Reset current level to this new level, and reset the override status to
  353. new_override.
  354. b. If the new level would not be valid, then this code is invalid. Don't
  355. change the current level or override status.
  356. */
  357. #define PUSH_STATUS \
  358. do { \
  359. if (new_level <= UNI_MAX_BIDI_LEVEL) \
  360. { \
  361. if (level == UNI_MAX_BIDI_LEVEL - 1) \
  362. first_interval = over_pushed; \
  363. status_stack[stack_size].level = level; \
  364. status_stack[stack_size].override = override; \
  365. stack_size++; \
  366. level = new_level; \
  367. override = new_override; \
  368. } else \
  369. over_pushed++; \
  370. } while (0)
  371. /* If there was a valid matching code, restore (pop) the last remembered
  372. (pushed) embedding level and directional override.
  373. */
  374. #define POP_STATUS \
  375. do { \
  376. if (over_pushed || stack_size) \
  377. { \
  378. if (over_pushed > first_interval) \
  379. over_pushed--; \
  380. else \
  381. { \
  382. if (over_pushed == first_interval) \
  383. first_interval = 0; \
  384. stack_size--; \
  385. level = status_stack[stack_size].level; \
  386. override = status_stack[stack_size].override; \
  387. } \
  388. } \
  389. } while (0)
  390. /*==========================================================================
  391. * There was no support for sor and eor in the absence of Explicit Embedding
  392. * Levels, so define macros, to support them, with as less change as needed.
  393. *--------------------------------------------------------------------------*/
  394. /* Return the type of previous char or the sor, if already at the start of
  395. a run level. */
  396. #define PREV_TYPE_OR_SOR(pp) \
  397. ( \
  398. RL_LEVEL(pp->prev) == RL_LEVEL(pp) ? \
  399. RL_TYPE(pp->prev) : \
  400. FRIBIDI_LEVEL_TO_DIR(MAX(RL_LEVEL(pp->prev), RL_LEVEL(pp))) \
  401. )
  402. /* Return the type of next char or the eor, if already at the end of
  403. a run level. */
  404. #define NEXT_TYPE_OR_EOR(pp) \
  405. ( \
  406. !pp->next ? \
  407. FRIBIDI_LEVEL_TO_DIR(RL_LEVEL(pp)) : \
  408. (RL_LEVEL(pp->next) == RL_LEVEL(pp) ? \
  409. RL_TYPE(pp->next) : \
  410. FRIBIDI_LEVEL_TO_DIR(MAX(RL_LEVEL(pp->next), RL_LEVEL(pp))) \
  411. ) \
  412. )
  413. /* Return the embedding direction of a link. */
  414. #define FRIBIDI_EMBEDDING_DIRECTION(list) \
  415. FRIBIDI_LEVEL_TO_DIR(RL_LEVEL(list))
  416. /*======================================================================
  417. * This function should follow the Unicode specification closely!
  418. *----------------------------------------------------------------------*/
  419. static fribidi_boolean
  420. fribidi_analyse_string_utf8 ( /* input */
  421. const char *str,
  422. int bytelen,
  423. FriBidiCharType *pbase_dir,
  424. /* output */
  425. FriBidiStrIndex *len,
  426. TypeLink **ptype_rl_list,
  427. FriBidiLevel *pmax_level)
  428. {
  429. FriBidiLevel base_level, max_level;
  430. FriBidiCharType base_dir;
  431. TypeLink *type_rl_list, *explicits_list, *explicits_list_end, *pp;
  432. DBG ("Entering fribidi_analyse_string()\n");
  433. /* Determinate character types */
  434. DBG (" Determine character types\n");
  435. {
  436. FriBidiCharType ored_types;
  437. FriBidiCharType anded_strongs;
  438. /* Run length encode the character types */
  439. type_rl_list = run_length_encode_types_utf8 (str, bytelen, len,
  440. &ored_types, &anded_strongs);
  441. /* The case that all resolved levels will be ltr.
  442. * First, all strongs should be ltr, there should be no Arabic numbers
  443. * (or letters for that matter), and one of the following:
  444. *
  445. * o *pbase_dir doesn't have an rtl taste.
  446. * o there are letters, and *pbase_dir is weak.
  447. *
  448. * For details see:
  449. * https://bugzilla.gnome.org/show_bug.cgi?id=590183
  450. */
  451. if (!FRIBIDI_IS_RTL (ored_types) && !FRIBIDI_IS_ARABIC (ored_types) &&
  452. (!FRIBIDI_IS_RTL (*pbase_dir) ||
  453. (FRIBIDI_IS_WEAK (*pbase_dir) && FRIBIDI_IS_LETTER (ored_types))
  454. ))
  455. {
  456. /* all ltr */
  457. free_rl_list (type_rl_list);
  458. *ptype_rl_list = NULL;
  459. *pmax_level = 0;
  460. *pbase_dir = FRIBIDI_TYPE_LTR;
  461. return 0;
  462. }
  463. /* The case that all resolved levels will be rtl is much more complex.
  464. * First, there should be no numbers, all strongs be rtl, and one of
  465. * the following:
  466. *
  467. * o *pbase_dir has an rtl taste (may be weak).
  468. * o there are letters, and *pbase_dir is weak.
  469. */
  470. else if (!FRIBIDI_IS_NUMBER (ored_types) && FRIBIDI_IS_RTL (anded_strongs) &&
  471. (FRIBIDI_IS_RTL (*pbase_dir) ||
  472. (FRIBIDI_IS_WEAK (*pbase_dir) && FRIBIDI_IS_LETTER (ored_types))
  473. ))
  474. {
  475. free_rl_list (type_rl_list);
  476. *ptype_rl_list = NULL;
  477. *pmax_level = 1;
  478. *pbase_dir = FRIBIDI_TYPE_RTL;
  479. return 0;
  480. }
  481. }
  482. DBG (" Determine character types, Done\n");
  483. init_list (&explicits_list, &explicits_list_end);
  484. /* Find base level */
  485. DBG (" Finding the base level\n");
  486. if (FRIBIDI_IS_STRONG (*pbase_dir))
  487. base_level = FRIBIDI_DIR_TO_LEVEL (*pbase_dir);
  488. /* P2. P3. Search for first strong character and use its direction as
  489. base direction */
  490. else
  491. {
  492. /* If no strong base_dir was found, resort to the weak direction
  493. that was passed on input. */
  494. base_level = FRIBIDI_DIR_TO_LEVEL (*pbase_dir);
  495. base_dir = FRIBIDI_TYPE_ON;
  496. for (pp = type_rl_list; pp; pp = pp->next)
  497. if (FRIBIDI_IS_LETTER (RL_TYPE (pp)))
  498. {
  499. base_level = FRIBIDI_DIR_TO_LEVEL (RL_TYPE (pp));
  500. base_dir = FRIBIDI_LEVEL_TO_DIR (base_level);
  501. break;
  502. }
  503. }
  504. base_dir = FRIBIDI_LEVEL_TO_DIR (base_level);
  505. DBG2 (" Base level : %c\n", fribidi_char_from_level (base_level));
  506. DBG2 (" Base dir : %c\n", fribidi_char_from_type (base_dir));
  507. DBG (" Finding the base level, Done\n");
  508. /* Explicit Levels and Directions */
  509. DBG ("Explicit Levels and Directions\n");
  510. {
  511. /* X1. Begin by setting the current embedding level to the paragraph
  512. embedding level. Set the directional override status to neutral.
  513. Process each character iteratively, applying rules X2 through X9.
  514. Only embedding levels from 0 to 61 are valid in this phase. */
  515. FriBidiLevel level, new_level;
  516. FriBidiCharType override, new_override;
  517. FriBidiStrIndex i;
  518. int stack_size, over_pushed, first_interval;
  519. LevelInfo *status_stack;
  520. TypeLink temp_link;
  521. level = base_level;
  522. override = FRIBIDI_TYPE_ON;
  523. /* stack */
  524. stack_size = 0;
  525. over_pushed = 0;
  526. first_interval = 0;
  527. status_stack =
  528. (LevelInfo *) malloc (sizeof (LevelInfo) * (UNI_MAX_BIDI_LEVEL + 2));
  529. for (pp = type_rl_list->next; pp->next; pp = pp->next)
  530. {
  531. FriBidiCharType this_type = RL_TYPE (pp);
  532. if (FRIBIDI_IS_EXPLICIT_OR_BN (this_type))
  533. {
  534. if (FRIBIDI_IS_STRONG (this_type))
  535. { /* LRE, RLE, LRO, RLO */
  536. /* 1. Explicit Embeddings */
  537. /* X2. With each RLE, compute the least greater odd embedding level. */
  538. /* X3. With each LRE, compute the least greater even embedding level. */
  539. /* 2. Explicit Overrides */
  540. /* X4. With each RLO, compute the least greater odd embedding level. */
  541. /* X5. With each LRO, compute the least greater even embedding level. */
  542. new_override = FRIBIDI_EXPLICIT_TO_OVERRIDE_DIR (this_type);
  543. for (i = 0; i < RL_LEN (pp); i++)
  544. {
  545. new_level =
  546. ((level + FRIBIDI_DIR_TO_LEVEL (this_type) + 2) & ~1) -
  547. FRIBIDI_DIR_TO_LEVEL (this_type);
  548. PUSH_STATUS;
  549. }
  550. }
  551. else if (this_type == FRIBIDI_TYPE_PDF)
  552. {
  553. /* 3. Terminating Embeddings and overrides */
  554. /* X7. With each PDF, determine the matching embedding or
  555. override code. */
  556. for (i = 0; i < RL_LEN (pp); i++)
  557. POP_STATUS;
  558. }
  559. /* X9. Remove all RLE, LRE, RLO, LRO, PDF, and BN codes. */
  560. /* Remove element and add it to explicits_list */
  561. temp_link.next = pp->next;
  562. pp->level = FRIBIDI_LEVEL_REMOVED;
  563. move_element_before (pp, explicits_list_end);
  564. pp = &temp_link;
  565. }
  566. else if (this_type == FRIBIDI_TYPE_BS)
  567. {
  568. /* X8. All explicit directional embeddings and overrides are
  569. completely terminated at the end of each paragraph. Paragraph
  570. separators are not included in the embedding. */
  571. break;
  572. }
  573. else
  574. {
  575. /* X6. For all typed besides RLE, LRE, RLO, LRO, and PDF:
  576. a. Set the level of the current character to the current
  577. embedding level.
  578. b. Whenever the directional override status is not neutral,
  579. reset the current character type to the directional override
  580. status. */
  581. RL_LEVEL (pp) = level;
  582. if (!FRIBIDI_IS_NEUTRAL (override))
  583. RL_TYPE (pp) = override;
  584. }
  585. }
  586. /* Implementing X8. It has no effect on a single paragraph! */
  587. level = base_level;
  588. override = FRIBIDI_TYPE_ON;
  589. stack_size = 0;
  590. over_pushed = 0;
  591. free (status_stack);
  592. }
  593. /* X10. The remaining rules are applied to each run of characters at the
  594. same level. For each run, determine the start-of-level-run (sor) and
  595. end-of-level-run (eor) type, either L or R. This depends on the
  596. higher of the two levels on either side of the boundary (at the start
  597. or end of the paragraph, the level of the 'other' run is the base
  598. embedding level). If the higher level is odd, the type is R, otherwise
  599. it is L. */
  600. /* Resolving Implicit Levels can be done out of X10 loop, so only change
  601. of Resolving Weak Types and Resolving Neutral Types is needed. */
  602. compact_list (type_rl_list);
  603. /* 4. Resolving weak types */
  604. DBG ("Resolving weak types\n");
  605. {
  606. FriBidiCharType last_strong, prev_type_org;
  607. fribidi_boolean w4;
  608. last_strong = base_dir;
  609. for (pp = type_rl_list->next; pp->next; pp = pp->next)
  610. {
  611. FriBidiCharType prev_type, this_type, next_type;
  612. prev_type = PREV_TYPE_OR_SOR (pp);
  613. this_type = RL_TYPE (pp);
  614. next_type = NEXT_TYPE_OR_EOR (pp);
  615. if (FRIBIDI_IS_STRONG (prev_type))
  616. last_strong = prev_type;
  617. /* W1. NSM
  618. Examine each non-spacing mark (NSM) in the level run, and change the
  619. type of the NSM to the type of the previous character. If the NSM
  620. is at the start of the level run, it will get the type of sor. */
  621. /* Implementation note: it is important that if the previous character
  622. is not sor, then we should merge this run with the previous,
  623. because of rules like W5, that we assume all of a sequence of
  624. adjacent ETs are in one TypeLink. */
  625. if (this_type == FRIBIDI_TYPE_NSM)
  626. {
  627. if (RL_LEVEL (pp->prev) == RL_LEVEL (pp))
  628. pp = merge_with_prev (pp);
  629. else
  630. RL_TYPE (pp) = prev_type;
  631. if (prev_type == next_type && RL_LEVEL (pp) == RL_LEVEL (pp->next))
  632. {
  633. pp = merge_with_prev (pp->next);
  634. }
  635. continue; /* As we know the next condition cannot be true. */
  636. }
  637. /* W2: European numbers. */
  638. if (this_type == FRIBIDI_TYPE_EN && last_strong == FRIBIDI_TYPE_AL)
  639. {
  640. RL_TYPE (pp) = FRIBIDI_TYPE_AN;
  641. /* Resolving dependency of loops for rules W1 and W2, so we
  642. can merge them in one loop. */
  643. if (next_type == FRIBIDI_TYPE_NSM)
  644. RL_TYPE (pp->next) = FRIBIDI_TYPE_AN;
  645. }
  646. }
  647. last_strong = base_dir;
  648. /* Resolving dependency of loops for rules W4 and W5, W5 may
  649. want to prevent W4 to take effect in the next turn, do this
  650. through "w4". */
  651. w4 = FRIBIDI_TRUE;
  652. /* Resolving dependency of loops for rules W4 and W5 with W7,
  653. W7 may change an EN to L but it sets the prev_type_org if needed,
  654. so W4 and W5 in next turn can still do their works. */
  655. prev_type_org = FRIBIDI_TYPE_ON;
  656. for (pp = type_rl_list->next; pp->next; pp = pp->next)
  657. {
  658. FriBidiCharType prev_type, this_type, next_type;
  659. prev_type = PREV_TYPE_OR_SOR (pp);
  660. this_type = RL_TYPE (pp);
  661. next_type = NEXT_TYPE_OR_EOR (pp);
  662. if (FRIBIDI_IS_STRONG (prev_type))
  663. last_strong = prev_type;
  664. /* W3: Change ALs to R. */
  665. if (this_type == FRIBIDI_TYPE_AL)
  666. {
  667. RL_TYPE (pp) = FRIBIDI_TYPE_RTL;
  668. w4 = FRIBIDI_TRUE;
  669. prev_type_org = FRIBIDI_TYPE_ON;
  670. continue;
  671. }
  672. /* W4. A single european separator changes to a european number.
  673. A single common separator between two numbers of the same type
  674. changes to that type. */
  675. if (w4
  676. && RL_LEN (pp) == 1 && FRIBIDI_IS_ES_OR_CS (this_type)
  677. && FRIBIDI_IS_NUMBER (prev_type_org) && prev_type_org == next_type
  678. && (prev_type_org == FRIBIDI_TYPE_EN
  679. || this_type == FRIBIDI_TYPE_CS))
  680. {
  681. RL_TYPE (pp) = prev_type;
  682. this_type = RL_TYPE (pp);
  683. }
  684. w4 = FRIBIDI_TRUE;
  685. /* W5. A sequence of European terminators adjacent to European
  686. numbers changes to All European numbers. */
  687. if (this_type == FRIBIDI_TYPE_ET
  688. && (prev_type_org == FRIBIDI_TYPE_EN
  689. || next_type == FRIBIDI_TYPE_EN))
  690. {
  691. RL_TYPE (pp) = FRIBIDI_TYPE_EN;
  692. w4 = FRIBIDI_FALSE;
  693. this_type = RL_TYPE (pp);
  694. }
  695. /* W6. Otherwise change separators and terminators to other neutral. */
  696. if (FRIBIDI_IS_NUMBER_SEPARATOR_OR_TERMINATOR (this_type))
  697. RL_TYPE (pp) = FRIBIDI_TYPE_ON;
  698. /* W7. Change european numbers to L. */
  699. if (this_type == FRIBIDI_TYPE_EN && last_strong == FRIBIDI_TYPE_LTR)
  700. {
  701. RL_TYPE (pp) = FRIBIDI_TYPE_LTR;
  702. prev_type_org = (RL_LEVEL (pp) == RL_LEVEL (pp->next) ?
  703. FRIBIDI_TYPE_EN : FRIBIDI_TYPE_ON);
  704. }
  705. else
  706. prev_type_org = PREV_TYPE_OR_SOR (pp->next);
  707. }
  708. }
  709. compact_neutrals (type_rl_list);
  710. /* 5. Resolving Neutral Types */
  711. DBG ("Resolving neutral types\n");
  712. {
  713. /* N1. and N2.
  714. For each neutral, resolve it. */
  715. for (pp = type_rl_list->next; pp->next; pp = pp->next)
  716. {
  717. FriBidiCharType prev_type, this_type, next_type;
  718. /* "European and arabic numbers are treated as though they were R"
  719. FRIBIDI_CHANGE_NUMBER_TO_RTL does this. */
  720. this_type = FRIBIDI_CHANGE_NUMBER_TO_RTL (RL_TYPE (pp));
  721. prev_type = FRIBIDI_CHANGE_NUMBER_TO_RTL (PREV_TYPE_OR_SOR (pp));
  722. next_type = FRIBIDI_CHANGE_NUMBER_TO_RTL (NEXT_TYPE_OR_EOR (pp));
  723. if (FRIBIDI_IS_NEUTRAL (this_type))
  724. RL_TYPE (pp) = (prev_type == next_type) ?
  725. /* N1. */ prev_type :
  726. /* N2. */ FRIBIDI_EMBEDDING_DIRECTION (pp);
  727. }
  728. }
  729. compact_list (type_rl_list);
  730. /* 6. Resolving implicit levels */
  731. DBG ("Resolving implicit levels\n");
  732. {
  733. max_level = base_level;
  734. for (pp = type_rl_list->next; pp->next; pp = pp->next)
  735. {
  736. FriBidiCharType this_type;
  737. int level;
  738. this_type = RL_TYPE (pp);
  739. level = RL_LEVEL (pp);
  740. /* I1. Even */
  741. /* I2. Odd */
  742. if (FRIBIDI_IS_NUMBER (this_type))
  743. RL_LEVEL (pp) = (level + 2) & ~1;
  744. else
  745. RL_LEVEL (pp) = (level ^ FRIBIDI_DIR_TO_LEVEL (this_type)) +
  746. (level & 1);
  747. if (RL_LEVEL (pp) > max_level)
  748. max_level = RL_LEVEL (pp);
  749. }
  750. }
  751. compact_list (type_rl_list);
  752. /* Reinsert the explicit codes & bn's that already removed, from the
  753. explicits_list to type_rl_list. */
  754. DBG ("Reinserting explicit codes\n");
  755. {
  756. TypeLink *p;
  757. override_list (type_rl_list, explicits_list);
  758. p = type_rl_list->next;
  759. if (p->level < 0)
  760. p->level = base_level;
  761. for (; p->next; p = p->next)
  762. if (p->level < 0)
  763. p->level = p->prev->level;
  764. }
  765. DBG ("Reset the embedding levels\n");
  766. {
  767. int j, k, state, pos;
  768. TypeLink *p, *q, *list, *list_end;
  769. const char *strp = str + bytelen;
  770. /* L1. Reset the embedding levels of some chars. */
  771. init_list (&list, &list_end);
  772. q = list_end;
  773. state = 1;
  774. pos = *len - 1;
  775. for (j = *len - 1; j >= -1; j--)
  776. {
  777. /* if state is on at the very first of string, do this too. */
  778. if (j >= 0)
  779. k = fribidi_get_type (g_utf8_get_char (strp = g_utf8_prev_char (strp)));
  780. else
  781. k = FRIBIDI_TYPE_ON;
  782. if (!state && FRIBIDI_IS_SEPARATOR (k))
  783. {
  784. state = 1;
  785. pos = j;
  786. }
  787. else if (state && !FRIBIDI_IS_EXPLICIT_OR_SEPARATOR_OR_BN_OR_WS (k))
  788. {
  789. state = 0;
  790. p = new_type_link ();
  791. p->prev = p->next = NULL;
  792. p->pos = j + 1;
  793. p->len = pos - j;
  794. p->type = base_dir;
  795. p->level = base_level;
  796. move_element_before (p, q);
  797. q = p;
  798. }
  799. }
  800. override_list (type_rl_list, list);
  801. }
  802. *ptype_rl_list = type_rl_list;
  803. *pmax_level = max_level;
  804. *pbase_dir = base_dir;
  805. DBG ("Leaving fribidi_analyse_string()\n");
  806. return 1;
  807. }
  808. /*======================================================================
  809. * fribidi_log2vis_get_embedding_levels() is used in order to just get
  810. * the embedding levels.
  811. *----------------------------------------------------------------------*/
  812. FRIBIDI_API FriBidiLevel *
  813. fribidi_log2vis_get_embedding_levels_new_utf8 ( /* input */
  814. const char *str,
  815. int bytelen,
  816. FriBidiCharType *pbase_dir)
  817. {
  818. TypeLink *type_rl_list, *pp;
  819. FriBidiLevel max_level, *embedding_level_list;
  820. FriBidiStrIndex len;
  821. DBG ("Entering fribidi_log2vis_get_embedding_levels()\n");
  822. if (bytelen == 0)
  823. {
  824. DBG ("Leaving fribidi_log2vis_get_embedding_levels()\n");
  825. return NULL;
  826. }
  827. if (!fribidi_analyse_string_utf8 (str, bytelen, pbase_dir,
  828. /* output */
  829. &len, &type_rl_list, &max_level))
  830. {
  831. /* unidirectional. return all-zero or all-one embedding levels */
  832. if (max_level)
  833. {
  834. embedding_level_list = g_new (FriBidiLevel, len);
  835. /* assumes sizeof(FriBidiLevel) == 1, which is true! */
  836. memset (embedding_level_list, max_level, len);
  837. return embedding_level_list;
  838. }
  839. else
  840. {
  841. return g_new0 (FriBidiLevel, len);
  842. }
  843. }
  844. embedding_level_list = g_new (FriBidiLevel, len);
  845. for (pp = type_rl_list->next; pp->next; pp = pp->next)
  846. {
  847. FriBidiStrIndex i, pos = RL_POS (pp), len = RL_LEN (pp);
  848. FriBidiLevel level = RL_LEVEL (pp);
  849. for (i = 0; i < len; i++)
  850. embedding_level_list[pos + i] = level;
  851. }
  852. free_rl_list (type_rl_list);
  853. DBG ("Leaving fribidi_log2vis_get_embedding_levels()\n");
  854. return embedding_level_list;
  855. }