charset.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. Current problems with Unicode rendering:
  20. - B and BN bidi type characters (ignored)
  21. - Mc type characters with combining class 0 (poorly combined)
  22. - Mn type characters with combining class 0 (poorly combined)
  23. - Me type characters with combining class 0 (poorly combined)
  24. - Cf type characters (ignored)
  25. - Cc type characters (ignored)
  26. - Line-breaking rules (e.g. Zs type characters)
  27. - Indic languages
  28. - non-Semitic shaping (rarely used)
  29. - Zl and Zp characters
  30. - Combining characters of types 7, 8, 9, 21, 35, 36, 84, 91, 103, 107,
  31. 118, 122, 129, 130, 132, 218, 224, 226, 233, 234
  32. - Private use characters (not really a problem)
  33. - Variations (no font support)
  34. - Vertical text
  35. - Ligatures
  36. Font information ignored:
  37. - Kerning
  38. - Justification data
  39. - Glyph posititioning
  40. - Baseline data
  41. Most underline diacritics aren't displayed in gfxterm
  42. */
  43. #include <grub/charset.h>
  44. #include <grub/mm.h>
  45. #include <grub/misc.h>
  46. #include <grub/unicode.h>
  47. #include <grub/term.h>
  48. #include <grub/normal.h>
  49. #if HAVE_FONT_SOURCE
  50. #include "widthspec.h"
  51. #endif
  52. /* Returns -2 if not enough space, -1 on invalid character. */
  53. grub_ssize_t
  54. grub_encode_utf8_character (grub_uint8_t *dest, grub_uint8_t *destend,
  55. grub_uint32_t code)
  56. {
  57. if (dest >= destend)
  58. return -2;
  59. if (code <= 0x007F)
  60. {
  61. *dest++ = code;
  62. return 1;
  63. }
  64. if (code <= 0x07FF)
  65. {
  66. if (dest + 1 >= destend)
  67. return -2;
  68. *dest++ = (code >> 6) | 0xC0;
  69. *dest++ = (code & 0x3F) | 0x80;
  70. return 2;
  71. }
  72. if ((code >= 0xDC00 && code <= 0xDFFF)
  73. || (code >= 0xD800 && code <= 0xDBFF))
  74. {
  75. /* No surrogates in UCS-4... */
  76. return -1;
  77. }
  78. if (code < 0x10000)
  79. {
  80. if (dest + 2 >= destend)
  81. return -2;
  82. *dest++ = (code >> 12) | 0xE0;
  83. *dest++ = ((code >> 6) & 0x3F) | 0x80;
  84. *dest++ = (code & 0x3F) | 0x80;
  85. return 3;
  86. }
  87. {
  88. if (dest + 3 >= destend)
  89. return -2;
  90. *dest++ = (code >> 18) | 0xF0;
  91. *dest++ = ((code >> 12) & 0x3F) | 0x80;
  92. *dest++ = ((code >> 6) & 0x3F) | 0x80;
  93. *dest++ = (code & 0x3F) | 0x80;
  94. return 4;
  95. }
  96. }
  97. /* Convert UCS-4 to UTF-8. */
  98. grub_size_t
  99. grub_ucs4_to_utf8 (const grub_uint32_t *src, grub_size_t size,
  100. grub_uint8_t *dest, grub_size_t destsize)
  101. {
  102. /* Keep last char for \0. */
  103. grub_uint8_t *destend = dest + destsize - 1;
  104. grub_uint8_t *dest0 = dest;
  105. while (size-- && dest < destend)
  106. {
  107. grub_uint32_t code = *src++;
  108. grub_ssize_t s;
  109. s = grub_encode_utf8_character (dest, destend, code);
  110. if (s == -2)
  111. break;
  112. if (s == -1)
  113. {
  114. *dest++ = '?';
  115. continue;
  116. }
  117. dest += s;
  118. }
  119. *dest = 0;
  120. return dest - dest0;
  121. }
  122. /* Returns the number of bytes the string src would occupy is converted
  123. to UTF-8, excluding trailing \0. */
  124. grub_size_t
  125. grub_get_num_of_utf8_bytes (const grub_uint32_t *src, grub_size_t size)
  126. {
  127. grub_size_t remaining;
  128. const grub_uint32_t *ptr;
  129. grub_size_t cnt = 0;
  130. remaining = size;
  131. ptr = src;
  132. while (remaining--)
  133. {
  134. grub_uint32_t code = *ptr++;
  135. if (code <= 0x007F)
  136. cnt++;
  137. else if (code <= 0x07FF)
  138. cnt += 2;
  139. else if ((code >= 0xDC00 && code <= 0xDFFF)
  140. || (code >= 0xD800 && code <= 0xDBFF))
  141. /* No surrogates in UCS-4... */
  142. cnt++;
  143. else if (code < 0x10000)
  144. cnt += 3;
  145. else
  146. cnt += 4;
  147. }
  148. return cnt;
  149. }
  150. /* Convert UCS-4 to UTF-8. */
  151. char *
  152. grub_ucs4_to_utf8_alloc (const grub_uint32_t *src, grub_size_t size)
  153. {
  154. grub_uint8_t *ret;
  155. grub_size_t cnt = grub_get_num_of_utf8_bytes (src, size) + 1;
  156. ret = grub_malloc (cnt);
  157. if (!ret)
  158. return 0;
  159. grub_ucs4_to_utf8 (src, size, ret, cnt);
  160. return (char *) ret;
  161. }
  162. int
  163. grub_is_valid_utf8 (const grub_uint8_t *src, grub_size_t srcsize)
  164. {
  165. int count = 0;
  166. grub_uint32_t code = 0;
  167. while (srcsize)
  168. {
  169. if (srcsize != (grub_size_t)-1)
  170. srcsize--;
  171. if (!grub_utf8_process (*src++, &code, &count))
  172. return 0;
  173. if (count != 0)
  174. continue;
  175. if (code == 0)
  176. return 1;
  177. if (code > GRUB_UNICODE_LAST_VALID)
  178. return 0;
  179. }
  180. return 1;
  181. }
  182. grub_ssize_t
  183. grub_utf8_to_ucs4_alloc (const char *msg, grub_uint32_t **unicode_msg,
  184. grub_uint32_t **last_position)
  185. {
  186. grub_size_t msg_len = grub_strlen (msg);
  187. *unicode_msg = grub_malloc (msg_len * sizeof (grub_uint32_t));
  188. if (!*unicode_msg)
  189. return -1;
  190. msg_len = grub_utf8_to_ucs4 (*unicode_msg, msg_len,
  191. (grub_uint8_t *) msg, -1, 0);
  192. if (last_position)
  193. *last_position = *unicode_msg + msg_len;
  194. return msg_len;
  195. }
  196. /* Convert a (possibly null-terminated) UTF-8 string of at most SRCSIZE
  197. bytes (if SRCSIZE is -1, it is ignored) in length to a UCS-4 string.
  198. Return the number of characters converted. DEST must be able to hold
  199. at least DESTSIZE characters.
  200. If SRCEND is not NULL, then *SRCEND is set to the next byte after the
  201. last byte used in SRC. */
  202. grub_size_t
  203. grub_utf8_to_ucs4 (grub_uint32_t *dest, grub_size_t destsize,
  204. const grub_uint8_t *src, grub_size_t srcsize,
  205. const grub_uint8_t **srcend)
  206. {
  207. grub_uint32_t *p = dest;
  208. int count = 0;
  209. grub_uint32_t code = 0;
  210. if (srcend)
  211. *srcend = src;
  212. while (srcsize && destsize)
  213. {
  214. int was_count = count;
  215. if (srcsize != (grub_size_t)-1)
  216. srcsize--;
  217. if (!grub_utf8_process (*src++, &code, &count))
  218. {
  219. code = '?';
  220. count = 0;
  221. /* Character c may be valid, don't eat it. */
  222. if (was_count)
  223. src--;
  224. }
  225. if (count != 0)
  226. continue;
  227. if (code == 0)
  228. break;
  229. *p++ = code;
  230. destsize--;
  231. }
  232. if (srcend)
  233. *srcend = src;
  234. return p - dest;
  235. }
  236. static grub_uint8_t *join_types = NULL;
  237. static void
  238. unpack_join (void)
  239. {
  240. unsigned i;
  241. struct grub_unicode_compact_range *cur;
  242. join_types = grub_zalloc (GRUB_UNICODE_MAX_CACHED_CHAR);
  243. if (!join_types)
  244. {
  245. grub_errno = GRUB_ERR_NONE;
  246. return;
  247. }
  248. for (cur = grub_unicode_compact; cur->len; cur++)
  249. for (i = cur->start; i < cur->start + (unsigned) cur->len
  250. && i < GRUB_UNICODE_MAX_CACHED_CHAR; i++)
  251. join_types[i] = cur->join_type;
  252. }
  253. static grub_uint8_t *bidi_types = NULL;
  254. static void
  255. unpack_bidi (void)
  256. {
  257. unsigned i;
  258. struct grub_unicode_compact_range *cur;
  259. bidi_types = grub_zalloc (GRUB_UNICODE_MAX_CACHED_CHAR);
  260. if (!bidi_types)
  261. {
  262. grub_errno = GRUB_ERR_NONE;
  263. return;
  264. }
  265. for (cur = grub_unicode_compact; cur->len; cur++)
  266. for (i = cur->start; i < cur->start + (unsigned) cur->len
  267. && i < GRUB_UNICODE_MAX_CACHED_CHAR; i++)
  268. if (cur->bidi_mirror)
  269. bidi_types[i] = cur->bidi_type | 0x80;
  270. else
  271. bidi_types[i] = cur->bidi_type | 0x00;
  272. }
  273. static inline enum grub_bidi_type
  274. get_bidi_type (grub_uint32_t c)
  275. {
  276. struct grub_unicode_compact_range *cur;
  277. if (!bidi_types)
  278. unpack_bidi ();
  279. if (bidi_types && c < GRUB_UNICODE_MAX_CACHED_CHAR)
  280. return bidi_types[c] & 0x7f;
  281. for (cur = grub_unicode_compact; cur->len; cur++)
  282. if (cur->start <= c && c < cur->start + (unsigned) cur->len)
  283. return cur->bidi_type;
  284. return GRUB_BIDI_TYPE_L;
  285. }
  286. static inline enum grub_join_type
  287. get_join_type (grub_uint32_t c)
  288. {
  289. struct grub_unicode_compact_range *cur;
  290. if (!join_types)
  291. unpack_join ();
  292. if (join_types && c < GRUB_UNICODE_MAX_CACHED_CHAR)
  293. return join_types[c];
  294. for (cur = grub_unicode_compact; cur->len; cur++)
  295. if (cur->start <= c && c < cur->start + (unsigned) cur->len)
  296. return cur->join_type;
  297. return GRUB_JOIN_TYPE_NONJOINING;
  298. }
  299. static inline int
  300. is_mirrored (grub_uint32_t c)
  301. {
  302. struct grub_unicode_compact_range *cur;
  303. if (!bidi_types)
  304. unpack_bidi ();
  305. if (bidi_types && c < GRUB_UNICODE_MAX_CACHED_CHAR)
  306. return !!(bidi_types[c] & 0x80);
  307. for (cur = grub_unicode_compact; cur->len; cur++)
  308. if (cur->start <= c && c < cur->start + (unsigned) cur->len)
  309. return cur->bidi_mirror;
  310. return 0;
  311. }
  312. enum grub_comb_type
  313. grub_unicode_get_comb_type (grub_uint32_t c)
  314. {
  315. static grub_uint8_t *comb_types = NULL;
  316. struct grub_unicode_compact_range *cur;
  317. if (!comb_types)
  318. {
  319. unsigned i;
  320. comb_types = grub_zalloc (GRUB_UNICODE_MAX_CACHED_CHAR);
  321. if (comb_types)
  322. for (cur = grub_unicode_compact; cur->len; cur++)
  323. for (i = cur->start; i < cur->start + (unsigned) cur->len
  324. && i < GRUB_UNICODE_MAX_CACHED_CHAR; i++)
  325. comb_types[i] = cur->comb_type;
  326. else
  327. grub_errno = GRUB_ERR_NONE;
  328. }
  329. if (comb_types && c < GRUB_UNICODE_MAX_CACHED_CHAR)
  330. return comb_types[c];
  331. for (cur = grub_unicode_compact; cur->len; cur++)
  332. if (cur->start <= c && c < cur->start + (unsigned) cur->len)
  333. return cur->comb_type;
  334. return GRUB_UNICODE_COMB_NONE;
  335. }
  336. #if HAVE_FONT_SOURCE
  337. grub_size_t
  338. grub_unicode_estimate_width (const struct grub_unicode_glyph *c)
  339. {
  340. if (grub_unicode_get_comb_type (c->base))
  341. return 0;
  342. if (widthspec[c->base >> 3] & (1 << (c->base & 7)))
  343. return 2;
  344. else
  345. return 1;
  346. }
  347. #endif
  348. static inline int
  349. is_type_after (enum grub_comb_type a, enum grub_comb_type b)
  350. {
  351. /* Shadda is numerically higher than most of Arabic diacritics but has
  352. to be rendered before them. */
  353. if (a == GRUB_UNICODE_COMB_ARABIC_SHADDA
  354. && b <= GRUB_UNICODE_COMB_ARABIC_KASRA
  355. && b >= GRUB_UNICODE_COMB_ARABIC_FATHATAN)
  356. return 0;
  357. if (b == GRUB_UNICODE_COMB_ARABIC_SHADDA
  358. && a <= GRUB_UNICODE_COMB_ARABIC_KASRA
  359. && a >= GRUB_UNICODE_COMB_ARABIC_FATHATAN)
  360. return 1;
  361. return a > b;
  362. }
  363. grub_size_t
  364. grub_unicode_aglomerate_comb (const grub_uint32_t *in, grub_size_t inlen,
  365. struct grub_unicode_glyph *out)
  366. {
  367. int haveout = 0;
  368. const grub_uint32_t *ptr;
  369. unsigned last_comb_pointer = 0;
  370. grub_memset (out, 0, sizeof (*out));
  371. if (inlen && grub_iscntrl (*in))
  372. {
  373. out->base = *in;
  374. out->variant = 0;
  375. out->attributes = 0;
  376. out->ncomb = 0;
  377. out->estimated_width = 1;
  378. return 1;
  379. }
  380. for (ptr = in; ptr < in + inlen; ptr++)
  381. {
  382. /* Variation selectors >= 17 are outside of BMP and SMP.
  383. Handle variation selectors first to avoid potentially costly lookups.
  384. */
  385. if (*ptr >= GRUB_UNICODE_VARIATION_SELECTOR_1
  386. && *ptr <= GRUB_UNICODE_VARIATION_SELECTOR_16)
  387. {
  388. if (haveout)
  389. out->variant = *ptr - GRUB_UNICODE_VARIATION_SELECTOR_1 + 1;
  390. continue;
  391. }
  392. if (*ptr >= GRUB_UNICODE_VARIATION_SELECTOR_17
  393. && *ptr <= GRUB_UNICODE_VARIATION_SELECTOR_256)
  394. {
  395. if (haveout)
  396. out->variant = *ptr - GRUB_UNICODE_VARIATION_SELECTOR_17 + 17;
  397. continue;
  398. }
  399. enum grub_comb_type comb_type;
  400. comb_type = grub_unicode_get_comb_type (*ptr);
  401. if (comb_type)
  402. {
  403. struct grub_unicode_combining *n;
  404. unsigned j;
  405. if (!haveout)
  406. continue;
  407. if (comb_type == GRUB_UNICODE_COMB_MC
  408. || comb_type == GRUB_UNICODE_COMB_ME
  409. || comb_type == GRUB_UNICODE_COMB_MN)
  410. last_comb_pointer = out->ncomb;
  411. if (out->ncomb + 1 <= (int) ARRAY_SIZE (out->combining_inline))
  412. n = out->combining_inline;
  413. else if (out->ncomb > (int) ARRAY_SIZE (out->combining_inline))
  414. {
  415. n = grub_realloc (out->combining_ptr,
  416. sizeof (n[0]) * (out->ncomb + 1));
  417. if (!n)
  418. {
  419. grub_errno = GRUB_ERR_NONE;
  420. continue;
  421. }
  422. out->combining_ptr = n;
  423. }
  424. else
  425. {
  426. n = grub_malloc (sizeof (n[0]) * (out->ncomb + 1));
  427. if (!n)
  428. {
  429. grub_errno = GRUB_ERR_NONE;
  430. continue;
  431. }
  432. grub_memcpy (n, out->combining_inline,
  433. sizeof (out->combining_inline));
  434. out->combining_ptr = n;
  435. }
  436. for (j = last_comb_pointer; j < out->ncomb; j++)
  437. if (is_type_after (n[j].type, comb_type))
  438. break;
  439. grub_memmove (n + j + 1,
  440. n + j,
  441. (out->ncomb - j)
  442. * sizeof (n[0]));
  443. n[j].code = *ptr;
  444. n[j].type = comb_type;
  445. out->ncomb++;
  446. continue;
  447. }
  448. if (haveout)
  449. return ptr - in;
  450. haveout = 1;
  451. out->base = *ptr;
  452. out->variant = 0;
  453. out->attributes = 0;
  454. out->ncomb = 0;
  455. out->estimated_width = 1;
  456. }
  457. return ptr - in;
  458. }
  459. static void
  460. revert (struct grub_unicode_glyph *visual,
  461. struct grub_term_pos *pos,
  462. unsigned start, unsigned end)
  463. {
  464. struct grub_unicode_glyph t;
  465. unsigned i;
  466. int a = 0, b = 0;
  467. if (pos)
  468. {
  469. a = pos[visual[start].orig_pos].x;
  470. b = pos[visual[end].orig_pos].x;
  471. }
  472. for (i = 0; i < (end - start) / 2 + 1; i++)
  473. {
  474. t = visual[start + i];
  475. visual[start + i] = visual[end - i];
  476. visual[end - i] = t;
  477. if (pos)
  478. {
  479. pos[visual[start + i].orig_pos].x = a + b - pos[visual[start + i].orig_pos].x;
  480. pos[visual[end - i].orig_pos].x = a + b - pos[visual[end - i].orig_pos].x;
  481. }
  482. }
  483. }
  484. static grub_ssize_t
  485. bidi_line_wrap (struct grub_unicode_glyph *visual_out,
  486. struct grub_unicode_glyph *visual,
  487. grub_size_t visual_len,
  488. grub_size_t (*getcharwidth) (const struct grub_unicode_glyph *visual, void *getcharwidth_arg),
  489. void *getcharwidth_arg,
  490. grub_size_t maxwidth, grub_size_t startwidth,
  491. grub_uint32_t contchar,
  492. struct grub_term_pos *pos, int primitive_wrap,
  493. grub_size_t log_end)
  494. {
  495. struct grub_unicode_glyph *outptr = visual_out;
  496. unsigned line_start = 0;
  497. grub_ssize_t line_width;
  498. unsigned k;
  499. grub_ssize_t last_space = -1;
  500. grub_ssize_t last_space_width = 0;
  501. int lines = 0;
  502. if (!visual_len)
  503. return 0;
  504. if (startwidth >= maxwidth && (grub_ssize_t) maxwidth > 0)
  505. {
  506. if (contchar)
  507. {
  508. grub_memset (outptr, 0, sizeof (visual[0]));
  509. outptr->base = contchar;
  510. outptr++;
  511. }
  512. grub_memset (outptr, 0, sizeof (visual[0]));
  513. outptr->base = '\n';
  514. outptr++;
  515. startwidth = 0;
  516. }
  517. line_width = startwidth;
  518. for (k = 0; k <= visual_len; k++)
  519. {
  520. grub_ssize_t last_width = 0;
  521. if (pos && k != visual_len)
  522. {
  523. pos[visual[k].orig_pos].x = line_width;
  524. pos[visual[k].orig_pos].y = lines;
  525. pos[visual[k].orig_pos].valid = 1;
  526. }
  527. if (k == visual_len && pos)
  528. {
  529. pos[log_end].x = line_width;
  530. pos[log_end].y = lines;
  531. pos[log_end].valid = 1;
  532. }
  533. if (getcharwidth && k != visual_len)
  534. line_width += last_width = getcharwidth (&visual[k], getcharwidth_arg);
  535. if (k != visual_len && (visual[k].base == ' '
  536. || visual[k].base == '\t')
  537. && !primitive_wrap)
  538. {
  539. last_space = k;
  540. last_space_width = line_width;
  541. }
  542. if (((grub_ssize_t) maxwidth > 0
  543. && line_width > (grub_ssize_t) maxwidth) || k == visual_len)
  544. {
  545. unsigned min_odd_level = 0xffffffff;
  546. unsigned max_level = 0;
  547. unsigned kk = k;
  548. lines++;
  549. if (k != visual_len && last_space > (signed) line_start)
  550. {
  551. kk = last_space;
  552. line_width -= last_space_width;
  553. }
  554. else if (k != visual_len && line_start == 0 && startwidth != 0
  555. && !primitive_wrap && lines == 1
  556. && line_width - startwidth < maxwidth)
  557. {
  558. kk = 0;
  559. line_width -= startwidth;
  560. }
  561. else
  562. line_width = last_width;
  563. {
  564. unsigned i;
  565. for (i = line_start; i < kk; i++)
  566. {
  567. if (visual[i].bidi_level > max_level)
  568. max_level = visual[i].bidi_level;
  569. if (visual[i].bidi_level < min_odd_level && (visual[i].bidi_level & 1))
  570. min_odd_level = visual[i].bidi_level;
  571. }
  572. }
  573. {
  574. unsigned j;
  575. /* FIXME: can be optimized. */
  576. for (j = max_level; j > min_odd_level - 1; j--)
  577. {
  578. unsigned in = line_start;
  579. unsigned i;
  580. for (i = line_start; i < kk; i++)
  581. {
  582. if (i != line_start && visual[i].bidi_level >= j
  583. && visual[i-1].bidi_level < j)
  584. in = i;
  585. if (visual[i].bidi_level >= j && (i + 1 == kk
  586. || visual[i+1].bidi_level < j))
  587. revert (visual, pos, in, i);
  588. }
  589. }
  590. }
  591. {
  592. unsigned i;
  593. for (i = line_start; i < kk; i++)
  594. {
  595. if (is_mirrored (visual[i].base) && visual[i].bidi_level)
  596. visual[i].attributes |= GRUB_UNICODE_GLYPH_ATTRIBUTE_MIRROR;
  597. if ((visual[i].attributes & GRUB_UNICODE_GLYPH_ATTRIBUTES_JOIN)
  598. && visual[i].bidi_level)
  599. {
  600. int left, right;
  601. left = visual[i].attributes
  602. & (GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED
  603. | GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED_EXPLICIT);
  604. right = visual[i].attributes
  605. & (GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED
  606. | GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED_EXPLICIT);
  607. visual[i].attributes &= ~GRUB_UNICODE_GLYPH_ATTRIBUTES_JOIN;
  608. left <<= GRUB_UNICODE_GLYPH_ATTRIBUTES_JOIN_LEFT_TO_RIGHT_SHIFT;
  609. right >>= GRUB_UNICODE_GLYPH_ATTRIBUTES_JOIN_LEFT_TO_RIGHT_SHIFT;
  610. visual[i].attributes |= (left | right);
  611. }
  612. }
  613. }
  614. {
  615. int left_join = 0;
  616. unsigned i;
  617. for (i = line_start; i < kk; i++)
  618. {
  619. enum grub_join_type join_type = get_join_type (visual[i].base);
  620. if (!(visual[i].attributes
  621. & GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED_EXPLICIT)
  622. && (join_type == GRUB_JOIN_TYPE_LEFT
  623. || join_type == GRUB_JOIN_TYPE_DUAL))
  624. {
  625. if (left_join)
  626. visual[i].attributes
  627. |= GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED;
  628. else
  629. visual[i].attributes
  630. &= ~GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED;
  631. }
  632. if (join_type == GRUB_JOIN_TYPE_NONJOINING
  633. || join_type == GRUB_JOIN_TYPE_LEFT)
  634. left_join = 0;
  635. if (join_type == GRUB_JOIN_TYPE_RIGHT
  636. || join_type == GRUB_JOIN_TYPE_DUAL
  637. || join_type == GRUB_JOIN_TYPE_CAUSING)
  638. left_join = 1;
  639. }
  640. }
  641. {
  642. int right_join = 0;
  643. signed i;
  644. for (i = kk - 1; i >= 0 && (unsigned) i + 1 > line_start;
  645. i--)
  646. {
  647. enum grub_join_type join_type = get_join_type (visual[i].base);
  648. if (!(visual[i].attributes
  649. & GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED_EXPLICIT)
  650. && (join_type == GRUB_JOIN_TYPE_RIGHT
  651. || join_type == GRUB_JOIN_TYPE_DUAL))
  652. {
  653. if (right_join)
  654. visual[i].attributes
  655. |= GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED;
  656. else
  657. visual[i].attributes
  658. &= ~GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED;
  659. }
  660. if (join_type == GRUB_JOIN_TYPE_NONJOINING
  661. || join_type == GRUB_JOIN_TYPE_RIGHT)
  662. right_join = 0;
  663. if (join_type == GRUB_JOIN_TYPE_LEFT
  664. || join_type == GRUB_JOIN_TYPE_DUAL
  665. || join_type == GRUB_JOIN_TYPE_CAUSING)
  666. right_join = 1;
  667. }
  668. }
  669. grub_memcpy (outptr, &visual[line_start],
  670. (kk - line_start) * sizeof (visual[0]));
  671. outptr += kk - line_start;
  672. if (kk != visual_len)
  673. {
  674. if (contchar)
  675. {
  676. grub_memset (outptr, 0, sizeof (visual[0]));
  677. outptr->base = contchar;
  678. outptr++;
  679. }
  680. grub_memset (outptr, 0, sizeof (visual[0]));
  681. outptr->base = '\n';
  682. outptr++;
  683. }
  684. if ((signed) kk == last_space)
  685. kk++;
  686. line_start = kk;
  687. if (pos && kk != visual_len)
  688. {
  689. pos[visual[kk].orig_pos].x = 0;
  690. pos[visual[kk].orig_pos].y = lines;
  691. }
  692. }
  693. }
  694. return outptr - visual_out;
  695. }
  696. static grub_ssize_t
  697. grub_bidi_line_logical_to_visual (const grub_uint32_t *logical,
  698. grub_size_t logical_len,
  699. struct grub_unicode_glyph *visual_out,
  700. grub_size_t (*getcharwidth) (const struct grub_unicode_glyph *visual, void *getcharwidth_arg),
  701. void *getcharwidth_arg,
  702. grub_size_t maxwidth, grub_size_t startwidth,
  703. grub_uint32_t contchar,
  704. struct grub_term_pos *pos,
  705. int primitive_wrap,
  706. grub_size_t log_end)
  707. {
  708. enum grub_bidi_type type = GRUB_BIDI_TYPE_L;
  709. enum override_status {OVERRIDE_NEUTRAL = 0, OVERRIDE_R, OVERRIDE_L};
  710. unsigned base_level;
  711. enum override_status cur_override;
  712. unsigned i;
  713. unsigned stack_level[GRUB_BIDI_MAX_EXPLICIT_LEVEL + 3];
  714. enum override_status stack_override[GRUB_BIDI_MAX_EXPLICIT_LEVEL + 3];
  715. unsigned stack_depth = 0;
  716. unsigned invalid_pushes = 0;
  717. unsigned visual_len = 0;
  718. unsigned run_start, run_end;
  719. struct grub_unicode_glyph *visual;
  720. unsigned cur_level;
  721. int bidi_needed = 0;
  722. #define push_stack(new_override, new_level) \
  723. { \
  724. if (new_level > GRUB_BIDI_MAX_EXPLICIT_LEVEL) \
  725. { \
  726. invalid_pushes++; \
  727. } \
  728. else \
  729. { \
  730. stack_level[stack_depth] = cur_level; \
  731. stack_override[stack_depth] = cur_override; \
  732. stack_depth++; \
  733. cur_level = new_level; \
  734. cur_override = new_override; \
  735. } \
  736. }
  737. #define pop_stack() \
  738. { \
  739. if (invalid_pushes) \
  740. { \
  741. invalid_pushes--; \
  742. } \
  743. else if (stack_depth) \
  744. { \
  745. stack_depth--; \
  746. cur_level = stack_level[stack_depth]; \
  747. cur_override = stack_override[stack_depth]; \
  748. } \
  749. }
  750. visual = grub_malloc (sizeof (visual[0]) * logical_len);
  751. if (!visual)
  752. return -1;
  753. for (i = 0; i < logical_len; i++)
  754. {
  755. type = get_bidi_type (logical[i]);
  756. if (type == GRUB_BIDI_TYPE_L || type == GRUB_BIDI_TYPE_AL
  757. || type == GRUB_BIDI_TYPE_R)
  758. break;
  759. }
  760. if (type == GRUB_BIDI_TYPE_R || type == GRUB_BIDI_TYPE_AL)
  761. base_level = 1;
  762. else
  763. base_level = 0;
  764. cur_level = base_level;
  765. cur_override = OVERRIDE_NEUTRAL;
  766. {
  767. const grub_uint32_t *lptr;
  768. enum {JOIN_DEFAULT, NOJOIN, JOIN_FORCE} join_state = JOIN_DEFAULT;
  769. int zwj_propagate_to_previous = 0;
  770. for (lptr = logical; lptr < logical + logical_len;)
  771. {
  772. grub_size_t p;
  773. if (*lptr == GRUB_UNICODE_ZWJ)
  774. {
  775. if (zwj_propagate_to_previous)
  776. {
  777. visual[visual_len - 1].attributes
  778. |= GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED_EXPLICIT
  779. | GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED;
  780. }
  781. zwj_propagate_to_previous = 0;
  782. join_state = JOIN_FORCE;
  783. lptr++;
  784. continue;
  785. }
  786. if (*lptr == GRUB_UNICODE_ZWNJ)
  787. {
  788. if (zwj_propagate_to_previous)
  789. {
  790. visual[visual_len - 1].attributes
  791. |= GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED_EXPLICIT;
  792. visual[visual_len - 1].attributes
  793. &= ~GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED;
  794. }
  795. zwj_propagate_to_previous = 0;
  796. join_state = NOJOIN;
  797. lptr++;
  798. continue;
  799. }
  800. /* The tags: deprecated, never used. */
  801. if (*lptr >= GRUB_UNICODE_TAG_START && *lptr <= GRUB_UNICODE_TAG_END)
  802. continue;
  803. p = grub_unicode_aglomerate_comb (lptr, logical + logical_len - lptr,
  804. &visual[visual_len]);
  805. visual[visual_len].orig_pos = lptr - logical;
  806. type = get_bidi_type (visual[visual_len].base);
  807. switch (type)
  808. {
  809. case GRUB_BIDI_TYPE_RLE:
  810. bidi_needed = 1;
  811. push_stack (cur_override, (cur_level | 1) + 1);
  812. break;
  813. case GRUB_BIDI_TYPE_RLO:
  814. bidi_needed = 1;
  815. push_stack (OVERRIDE_R, (cur_level | 1) + 1);
  816. break;
  817. case GRUB_BIDI_TYPE_LRE:
  818. push_stack (cur_override, (cur_level & ~1) + 2);
  819. break;
  820. case GRUB_BIDI_TYPE_LRO:
  821. push_stack (OVERRIDE_L, (cur_level & ~1) + 2);
  822. break;
  823. case GRUB_BIDI_TYPE_PDF:
  824. pop_stack ();
  825. break;
  826. case GRUB_BIDI_TYPE_BN:
  827. break;
  828. case GRUB_BIDI_TYPE_R:
  829. case GRUB_BIDI_TYPE_AL:
  830. bidi_needed = 1;
  831. /* Fallthrough. */
  832. default:
  833. {
  834. if (join_state == JOIN_FORCE)
  835. {
  836. visual[visual_len].attributes
  837. |= GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED_EXPLICIT
  838. | GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED;
  839. }
  840. if (join_state == NOJOIN)
  841. {
  842. visual[visual_len].attributes
  843. |= GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED_EXPLICIT;
  844. visual[visual_len].attributes
  845. &= ~GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED;
  846. }
  847. join_state = JOIN_DEFAULT;
  848. zwj_propagate_to_previous = 1;
  849. visual[visual_len].bidi_level = cur_level;
  850. if (cur_override != OVERRIDE_NEUTRAL)
  851. visual[visual_len].bidi_type =
  852. (cur_override == OVERRIDE_L) ? GRUB_BIDI_TYPE_L
  853. : GRUB_BIDI_TYPE_R;
  854. else
  855. visual[visual_len].bidi_type = type;
  856. visual_len++;
  857. }
  858. }
  859. lptr += p;
  860. }
  861. }
  862. if (bidi_needed)
  863. {
  864. for (run_start = 0; run_start < visual_len; run_start = run_end)
  865. {
  866. unsigned prev_level, next_level, cur_run_level;
  867. unsigned last_type, last_strong_type;
  868. for (run_end = run_start; run_end < visual_len &&
  869. visual[run_end].bidi_level == visual[run_start].bidi_level; run_end++);
  870. if (run_start == 0)
  871. prev_level = base_level;
  872. else
  873. prev_level = visual[run_start - 1].bidi_level;
  874. if (run_end == visual_len)
  875. next_level = base_level;
  876. else
  877. next_level = visual[run_end].bidi_level;
  878. cur_run_level = visual[run_start].bidi_level;
  879. if (prev_level & 1)
  880. last_type = GRUB_BIDI_TYPE_R;
  881. else
  882. last_type = GRUB_BIDI_TYPE_L;
  883. last_strong_type = last_type;
  884. for (i = run_start; i < run_end; i++)
  885. {
  886. switch (visual[i].bidi_type)
  887. {
  888. case GRUB_BIDI_TYPE_NSM:
  889. visual[i].bidi_type = last_type;
  890. break;
  891. case GRUB_BIDI_TYPE_EN:
  892. if (last_strong_type == GRUB_BIDI_TYPE_AL)
  893. visual[i].bidi_type = GRUB_BIDI_TYPE_AN;
  894. break;
  895. case GRUB_BIDI_TYPE_L:
  896. case GRUB_BIDI_TYPE_R:
  897. last_strong_type = visual[i].bidi_type;
  898. break;
  899. case GRUB_BIDI_TYPE_ES:
  900. if (last_type == GRUB_BIDI_TYPE_EN
  901. && i + 1 < run_end
  902. && visual[i + 1].bidi_type == GRUB_BIDI_TYPE_EN)
  903. visual[i].bidi_type = GRUB_BIDI_TYPE_EN;
  904. else
  905. visual[i].bidi_type = GRUB_BIDI_TYPE_ON;
  906. break;
  907. case GRUB_BIDI_TYPE_ET:
  908. {
  909. unsigned j;
  910. if (last_type == GRUB_BIDI_TYPE_EN)
  911. {
  912. visual[i].bidi_type = GRUB_BIDI_TYPE_EN;
  913. break;
  914. }
  915. for (j = i; j < run_end
  916. && visual[j].bidi_type == GRUB_BIDI_TYPE_ET; j++);
  917. if (j != run_end && visual[j].bidi_type == GRUB_BIDI_TYPE_EN)
  918. {
  919. for (; i < run_end
  920. && visual[i].bidi_type == GRUB_BIDI_TYPE_ET; i++)
  921. visual[i].bidi_type = GRUB_BIDI_TYPE_EN;
  922. i--;
  923. break;
  924. }
  925. for (; i < run_end
  926. && visual[i].bidi_type == GRUB_BIDI_TYPE_ET; i++)
  927. visual[i].bidi_type = GRUB_BIDI_TYPE_ON;
  928. i--;
  929. break;
  930. }
  931. break;
  932. case GRUB_BIDI_TYPE_CS:
  933. if (last_type == GRUB_BIDI_TYPE_EN
  934. && i + 1 < run_end
  935. && visual[i + 1].bidi_type == GRUB_BIDI_TYPE_EN)
  936. {
  937. visual[i].bidi_type = GRUB_BIDI_TYPE_EN;
  938. break;
  939. }
  940. if (last_type == GRUB_BIDI_TYPE_AN
  941. && i + 1 < run_end
  942. && (visual[i + 1].bidi_type == GRUB_BIDI_TYPE_AN
  943. || (visual[i + 1].bidi_type == GRUB_BIDI_TYPE_EN
  944. && last_strong_type == GRUB_BIDI_TYPE_AL)))
  945. {
  946. visual[i].bidi_type = GRUB_BIDI_TYPE_EN;
  947. break;
  948. }
  949. visual[i].bidi_type = GRUB_BIDI_TYPE_ON;
  950. break;
  951. case GRUB_BIDI_TYPE_AL:
  952. last_strong_type = visual[i].bidi_type;
  953. visual[i].bidi_type = GRUB_BIDI_TYPE_R;
  954. break;
  955. default: /* Make GCC happy. */
  956. break;
  957. }
  958. last_type = visual[i].bidi_type;
  959. if (visual[i].bidi_type == GRUB_BIDI_TYPE_EN
  960. && last_strong_type == GRUB_BIDI_TYPE_L)
  961. visual[i].bidi_type = GRUB_BIDI_TYPE_L;
  962. }
  963. if (prev_level & 1)
  964. last_type = GRUB_BIDI_TYPE_R;
  965. else
  966. last_type = GRUB_BIDI_TYPE_L;
  967. for (i = run_start; i < run_end; )
  968. {
  969. unsigned j;
  970. unsigned next_type;
  971. for (j = i; j < run_end &&
  972. (visual[j].bidi_type == GRUB_BIDI_TYPE_B
  973. || visual[j].bidi_type == GRUB_BIDI_TYPE_S
  974. || visual[j].bidi_type == GRUB_BIDI_TYPE_WS
  975. || visual[j].bidi_type == GRUB_BIDI_TYPE_ON); j++);
  976. if (j == i)
  977. {
  978. if (visual[i].bidi_type == GRUB_BIDI_TYPE_L)
  979. last_type = GRUB_BIDI_TYPE_L;
  980. else
  981. last_type = GRUB_BIDI_TYPE_R;
  982. i++;
  983. continue;
  984. }
  985. if (j == run_end)
  986. next_type = (next_level & 1) ? GRUB_BIDI_TYPE_R : GRUB_BIDI_TYPE_L;
  987. else
  988. {
  989. if (visual[j].bidi_type == GRUB_BIDI_TYPE_L)
  990. next_type = GRUB_BIDI_TYPE_L;
  991. else
  992. next_type = GRUB_BIDI_TYPE_R;
  993. }
  994. if (next_type == last_type)
  995. for (; i < j; i++)
  996. visual[i].bidi_type = last_type;
  997. else
  998. for (; i < j; i++)
  999. visual[i].bidi_type = (cur_run_level & 1) ? GRUB_BIDI_TYPE_R
  1000. : GRUB_BIDI_TYPE_L;
  1001. }
  1002. }
  1003. for (i = 0; i < visual_len; i++)
  1004. {
  1005. if (!(visual[i].bidi_level & 1) && visual[i].bidi_type == GRUB_BIDI_TYPE_R)
  1006. {
  1007. visual[i].bidi_level++;
  1008. continue;
  1009. }
  1010. if (!(visual[i].bidi_level & 1) && (visual[i].bidi_type == GRUB_BIDI_TYPE_AN
  1011. || visual[i].bidi_type == GRUB_BIDI_TYPE_EN))
  1012. {
  1013. visual[i].bidi_level += 2;
  1014. continue;
  1015. }
  1016. if ((visual[i].bidi_level & 1) && (visual[i].bidi_type == GRUB_BIDI_TYPE_L
  1017. || visual[i].bidi_type == GRUB_BIDI_TYPE_AN
  1018. || visual[i].bidi_type == GRUB_BIDI_TYPE_EN))
  1019. {
  1020. visual[i].bidi_level++;
  1021. continue;
  1022. }
  1023. }
  1024. }
  1025. else
  1026. {
  1027. for (i = 0; i < visual_len; i++)
  1028. visual[i].bidi_level = 0;
  1029. }
  1030. {
  1031. grub_ssize_t ret;
  1032. ret = bidi_line_wrap (visual_out, visual, visual_len,
  1033. getcharwidth, getcharwidth_arg, maxwidth, startwidth, contchar,
  1034. pos, primitive_wrap, log_end);
  1035. grub_free (visual);
  1036. return ret;
  1037. }
  1038. }
  1039. static int
  1040. is_visible (const struct grub_unicode_glyph *gl)
  1041. {
  1042. if (gl->ncomb)
  1043. return 1;
  1044. if (gl->base == GRUB_UNICODE_LRM || gl->base == GRUB_UNICODE_RLM)
  1045. return 0;
  1046. return 1;
  1047. }
  1048. grub_ssize_t
  1049. grub_bidi_logical_to_visual (const grub_uint32_t *logical,
  1050. grub_size_t logical_len,
  1051. struct grub_unicode_glyph **visual_out,
  1052. grub_size_t (*getcharwidth) (const struct grub_unicode_glyph *visual, void *getcharwidth_arg),
  1053. void *getcharwidth_arg,
  1054. grub_size_t max_length, grub_size_t startwidth,
  1055. grub_uint32_t contchar, struct grub_term_pos *pos, int primitive_wrap)
  1056. {
  1057. const grub_uint32_t *line_start = logical, *ptr;
  1058. struct grub_unicode_glyph *visual_ptr;
  1059. *visual_out = visual_ptr = grub_malloc (3 * sizeof (visual_ptr[0])
  1060. * (logical_len + 2));
  1061. if (!visual_ptr)
  1062. return -1;
  1063. for (ptr = logical; ptr <= logical + logical_len; ptr++)
  1064. {
  1065. if (ptr == logical + logical_len || *ptr == '\n')
  1066. {
  1067. grub_ssize_t ret;
  1068. grub_ssize_t i, j;
  1069. ret = grub_bidi_line_logical_to_visual (line_start,
  1070. ptr - line_start,
  1071. visual_ptr,
  1072. getcharwidth,
  1073. getcharwidth_arg,
  1074. max_length,
  1075. startwidth,
  1076. contchar,
  1077. pos,
  1078. primitive_wrap,
  1079. logical_len);
  1080. startwidth = 0;
  1081. if (ret < 0)
  1082. {
  1083. grub_free (*visual_out);
  1084. return ret;
  1085. }
  1086. for (i = 0, j = 0; i < ret; i++)
  1087. if (is_visible(&visual_ptr[i]))
  1088. visual_ptr[j++] = visual_ptr[i];
  1089. visual_ptr += j;
  1090. line_start = ptr;
  1091. if (ptr != logical + logical_len)
  1092. {
  1093. grub_memset (visual_ptr, 0, sizeof (visual_ptr[0]));
  1094. visual_ptr->base = '\n';
  1095. visual_ptr++;
  1096. line_start++;
  1097. }
  1098. }
  1099. }
  1100. return visual_ptr - *visual_out;
  1101. }
  1102. grub_uint32_t
  1103. grub_unicode_mirror_code (grub_uint32_t in)
  1104. {
  1105. int i;
  1106. for (i = 0; grub_unicode_bidi_pairs[i].key; i++)
  1107. if (grub_unicode_bidi_pairs[i].key == in)
  1108. return grub_unicode_bidi_pairs[i].replace;
  1109. return in;
  1110. }
  1111. grub_uint32_t
  1112. grub_unicode_shape_code (grub_uint32_t in, grub_uint8_t attr)
  1113. {
  1114. int i;
  1115. if (!(in >= GRUB_UNICODE_ARABIC_START
  1116. && in < GRUB_UNICODE_ARABIC_END))
  1117. return in;
  1118. for (i = 0; grub_unicode_arabic_shapes[i].code; i++)
  1119. if (grub_unicode_arabic_shapes[i].code == in)
  1120. {
  1121. grub_uint32_t out = 0;
  1122. switch (attr & (GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED
  1123. | GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED))
  1124. {
  1125. case 0:
  1126. out = grub_unicode_arabic_shapes[i].isolated;
  1127. break;
  1128. case GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED:
  1129. out = grub_unicode_arabic_shapes[i].right_linked;
  1130. break;
  1131. case GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED:
  1132. out = grub_unicode_arabic_shapes[i].left_linked;
  1133. break;
  1134. case GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED
  1135. |GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED:
  1136. out = grub_unicode_arabic_shapes[i].both_linked;
  1137. break;
  1138. }
  1139. if (out)
  1140. return out;
  1141. }
  1142. return in;
  1143. }
  1144. const grub_uint32_t *
  1145. grub_unicode_get_comb_start (const grub_uint32_t *str,
  1146. const grub_uint32_t *cur)
  1147. {
  1148. const grub_uint32_t *ptr;
  1149. for (ptr = cur; ptr >= str; ptr--)
  1150. {
  1151. if (*ptr >= GRUB_UNICODE_VARIATION_SELECTOR_1
  1152. && *ptr <= GRUB_UNICODE_VARIATION_SELECTOR_16)
  1153. continue;
  1154. if (*ptr >= GRUB_UNICODE_VARIATION_SELECTOR_17
  1155. && *ptr <= GRUB_UNICODE_VARIATION_SELECTOR_256)
  1156. continue;
  1157. enum grub_comb_type comb_type;
  1158. comb_type = grub_unicode_get_comb_type (*ptr);
  1159. if (comb_type)
  1160. continue;
  1161. return ptr;
  1162. }
  1163. return str;
  1164. }
  1165. const grub_uint32_t *
  1166. grub_unicode_get_comb_end (const grub_uint32_t *end,
  1167. const grub_uint32_t *cur)
  1168. {
  1169. const grub_uint32_t *ptr;
  1170. for (ptr = cur; ptr < end; ptr++)
  1171. {
  1172. if (*ptr >= GRUB_UNICODE_VARIATION_SELECTOR_1
  1173. && *ptr <= GRUB_UNICODE_VARIATION_SELECTOR_16)
  1174. continue;
  1175. if (*ptr >= GRUB_UNICODE_VARIATION_SELECTOR_17
  1176. && *ptr <= GRUB_UNICODE_VARIATION_SELECTOR_256)
  1177. continue;
  1178. enum grub_comb_type comb_type;
  1179. comb_type = grub_unicode_get_comb_type (*ptr);
  1180. if (comb_type)
  1181. continue;
  1182. return ptr;
  1183. }
  1184. return end;
  1185. }