charset.c 32.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

19 20 21
/*
  Current problems with Unicode rendering: 
  - B and BN bidi type characters (ignored)
22 23 24
  - Mc type characters with combining class 0 (poorly combined)
  - Mn type characters with combining class 0 (poorly combined)
  - Me type characters with combining class 0 (poorly combined)
25 26 27 28
  - Cf type characters (ignored)
  - Cc type characters (ignored)
  - Line-breaking rules (e.g. Zs type characters)
  - Indic languages
29
  - non-Semitic shaping (rarely used)
30
  - Zl and Zp characters
31 32
  - Combining characters of types 7, 8, 9, 21, 35, 36, 84, 91, 103, 107,
  118, 122, 129, 130, 132, 218, 224, 226, 233, 234
33
  - Private use characters (not really a problem)
34 35
  - Variations (no font support)
  - Vertical text
36
  - Ligatures
37 38 39 40 41
  Font information ignored:
  - Kerning
  - Justification data
  - Glyph posititioning
  - Baseline data
42
  Most underline diacritics aren't displayed in gfxterm
43 44
 */

45
#include <grub/charset.h>
46 47
#include <grub/mm.h>
#include <grub/misc.h>
48 49 50
#include <grub/unicode.h>
#include <grub/term.h>
#include <grub/normal.h>
51

52
#if HAVE_FONT_SOURCE
53 54 55
#include "widthspec.h"
#endif

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
/* Returns -2 if not enough space, -1 on invalid character.  */
grub_ssize_t
grub_encode_utf8_character (grub_uint8_t *dest, grub_uint8_t *destend,
			    grub_uint32_t code)
{
  if (dest >= destend)
    return -2;
  if (code <= 0x007F)
    {
      *dest++ = code;
      return 1;
    }
  if (code <= 0x07FF)
    {
      if (dest + 1 >= destend)
	return -2;
      *dest++ = (code >> 6) | 0xC0;
      *dest++ = (code & 0x3F) | 0x80;
      return 2;
    }
  if ((code >= 0xDC00 && code <= 0xDFFF)
      || (code >= 0xD800 && code <= 0xDBFF))
    {
      /* No surrogates in UCS-4... */
      return -1;
    }
  if (code < 0x10000)
    {
      if (dest + 2 >= destend)
	return -2;
      *dest++ = (code >> 12) | 0xE0;
      *dest++ = ((code >> 6) & 0x3F) | 0x80;
      *dest++ = (code & 0x3F) | 0x80;
      return 3;
    }
  {
    if (dest + 3 >= destend)
      return -2;
    *dest++ = (code >> 18) | 0xF0;
    *dest++ = ((code >> 12) & 0x3F) | 0x80;
    *dest++ = ((code >> 6) & 0x3F) | 0x80;
    *dest++ = (code & 0x3F) | 0x80;
    return 4;
  }

}

103
/* Convert UCS-4 to UTF-8.  */
104
grub_size_t
105
grub_ucs4_to_utf8 (const grub_uint32_t *src, grub_size_t size,
106 107 108 109
		   grub_uint8_t *dest, grub_size_t destsize)
{
  /* Keep last char for \0.  */
  grub_uint8_t *destend = dest + destsize - 1;
110
  grub_uint8_t *dest0 = dest;
111 112 113 114

  while (size-- && dest < destend)
    {
      grub_uint32_t code = *src++;
115
      grub_ssize_t s;
116
      s = grub_encode_utf8_character (dest, destend, code);
117 118 119
      if (s == -2)
	break;
      if (s == -1)
120 121
	{
	  *dest++ = '?';
122
	  continue;
123
	}
124
      dest += s;
125 126
    }
  *dest = 0;
127
  return dest - dest0;
128 129
}

130 131 132 133
/* Returns the number of bytes the string src would occupy is converted
   to UTF-8, excluding trailing \0.  */
grub_size_t
grub_get_num_of_utf8_bytes (const grub_uint32_t *src, grub_size_t size)
134 135
{
  grub_size_t remaining;
136
  const grub_uint32_t *ptr;
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  grub_size_t cnt = 0;

  remaining = size;
  ptr = src;
  while (remaining--)
    {
      grub_uint32_t code = *ptr++;
      
      if (code <= 0x007F)
	cnt++;
      else if (code <= 0x07FF)
	cnt += 2;
      else if ((code >= 0xDC00 && code <= 0xDFFF)
	       || (code >= 0xD800 && code <= 0xDBFF))
	/* No surrogates in UCS-4... */
	cnt++;
153
      else if (code < 0x10000)
154
	cnt += 3;
155 156
      else
	cnt += 4;
157
    }
158 159 160 161 162 163 164 165 166
  return cnt;
}

/* Convert UCS-4 to UTF-8.  */
char *
grub_ucs4_to_utf8_alloc (const grub_uint32_t *src, grub_size_t size)
{
  grub_uint8_t *ret;
  grub_size_t cnt = grub_get_num_of_utf8_bytes (src, size) + 1;
167 168 169 170 171

  ret = grub_malloc (cnt);
  if (!ret)
    return 0;

172
  grub_ucs4_to_utf8 (src, size, ret, cnt);
173

174 175 176 177 178 179 180
  return (char *) ret;
}

int
grub_is_valid_utf8 (const grub_uint8_t *src, grub_size_t srcsize)
{
  int count = 0;
181
  grub_uint32_t code = 0;
182 183 184 185 186

  while (srcsize)
    {
      if (srcsize != (grub_size_t)-1)
	srcsize--;
187 188 189 190 191 192 193 194
      if (!grub_utf8_process (*src++, &code, &count))
	return 0;
      if (count != 0)
	continue;
      if (code == 0)
	return 1;
      if (code > GRUB_UNICODE_LAST_VALID)
	return 0;
195 196 197 198 199
    }

  return 1;
}

200
grub_ssize_t
201
grub_utf8_to_ucs4_alloc (const char *msg, grub_uint32_t **unicode_msg,
202
			 grub_uint32_t **last_position)
203 204 205
{
  grub_size_t msg_len = grub_strlen (msg);

206
  *unicode_msg = grub_malloc (msg_len * sizeof (grub_uint32_t));
207 208
 
  if (!*unicode_msg)
209
    return -1;
210 211 212 213

  msg_len = grub_utf8_to_ucs4 (*unicode_msg, msg_len,
  			      (grub_uint8_t *) msg, -1, 0);

214 215
  if (last_position)
    *last_position = *unicode_msg + msg_len;
216 217

  return msg_len;
218
}
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

/* Convert a (possibly null-terminated) UTF-8 string of at most SRCSIZE
   bytes (if SRCSIZE is -1, it is ignored) in length to a UCS-4 string.
   Return the number of characters converted. DEST must be able to hold
   at least DESTSIZE characters.
   If SRCEND is not NULL, then *SRCEND is set to the next byte after the
   last byte used in SRC.  */
grub_size_t
grub_utf8_to_ucs4 (grub_uint32_t *dest, grub_size_t destsize,
		   const grub_uint8_t *src, grub_size_t srcsize,
		   const grub_uint8_t **srcend)
{
  grub_uint32_t *p = dest;
  int count = 0;
  grub_uint32_t code = 0;

  if (srcend)
    *srcend = src;

  while (srcsize && destsize)
    {
240
      int was_count = count;
241 242
      if (srcsize != (grub_size_t)-1)
	srcsize--;
243
      if (!grub_utf8_process (*src++, &code, &count))
244
	{
245 246 247 248 249
	  code = '?';
	  count = 0;
	  /* Character c may be valid, don't eat it.  */
	  if (was_count)
	    src--;
250
	}
251 252 253 254 255 256
      if (count != 0)
	continue;
      if (code == 0)
	break;
      *p++ = code;
      destsize--;
257 258 259 260 261 262
    }

  if (srcend)
    *srcend = src;
  return p - dest;
}
263

264 265 266 267 268 269 270 271 272 273 274 275 276 277
static grub_uint8_t *join_types = NULL;

static void
unpack_join (void)
{
  unsigned i;
  struct grub_unicode_compact_range *cur;

  join_types = grub_zalloc (GRUB_UNICODE_MAX_CACHED_CHAR);
  if (!join_types)
    {
      grub_errno = GRUB_ERR_NONE;
      return;
    }
278 279 280
  for (cur = grub_unicode_compact; cur->len; cur++)
    for (i = cur->start; i < cur->start + (unsigned) cur->len
	   && i < GRUB_UNICODE_MAX_CACHED_CHAR; i++)
281 282 283
      join_types[i] = cur->join_type;
}

284 285 286 287 288 289 290 291 292 293 294 295 296 297
static grub_uint8_t *bidi_types = NULL;

static void
unpack_bidi (void)
{
  unsigned i;
  struct grub_unicode_compact_range *cur;

  bidi_types = grub_zalloc (GRUB_UNICODE_MAX_CACHED_CHAR);
  if (!bidi_types)
    {
      grub_errno = GRUB_ERR_NONE;
      return;
    }
298 299 300
  for (cur = grub_unicode_compact; cur->len; cur++)
    for (i = cur->start; i < cur->start + (unsigned) cur->len
	   && i < GRUB_UNICODE_MAX_CACHED_CHAR; i++)
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
      if (cur->bidi_mirror)
	bidi_types[i] = cur->bidi_type | 0x80;
      else
	bidi_types[i] = cur->bidi_type | 0x00;
}

static inline enum grub_bidi_type
get_bidi_type (grub_uint32_t c)
{
  struct grub_unicode_compact_range *cur;

  if (!bidi_types)
    unpack_bidi ();

  if (bidi_types && c < GRUB_UNICODE_MAX_CACHED_CHAR)
    return bidi_types[c] & 0x7f;

318 319
  for (cur = grub_unicode_compact; cur->len; cur++)
    if (cur->start <= c && c < cur->start + (unsigned) cur->len)
320 321 322 323 324
      return cur->bidi_type;

  return GRUB_BIDI_TYPE_L;
}

325 326 327 328 329 330 331 332 333 334 335
static inline enum grub_join_type
get_join_type (grub_uint32_t c)
{
  struct grub_unicode_compact_range *cur;

  if (!join_types)
    unpack_join ();

  if (join_types && c < GRUB_UNICODE_MAX_CACHED_CHAR)
    return join_types[c];

336 337
  for (cur = grub_unicode_compact; cur->len; cur++)
    if (cur->start <= c && c < cur->start + (unsigned) cur->len)
338 339 340 341 342
      return cur->join_type;

  return GRUB_JOIN_TYPE_NONJOINING;
}

343 344 345 346 347 348 349 350 351 352 353
static inline int
is_mirrored (grub_uint32_t c)
{
  struct grub_unicode_compact_range *cur;

  if (!bidi_types)
    unpack_bidi ();

  if (bidi_types && c < GRUB_UNICODE_MAX_CACHED_CHAR)
    return !!(bidi_types[c] & 0x80);

354 355
  for (cur = grub_unicode_compact; cur->len; cur++)
    if (cur->start <= c && c < cur->start + (unsigned) cur->len)
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
      return cur->bidi_mirror;

  return 0;
}

enum grub_comb_type
grub_unicode_get_comb_type (grub_uint32_t c)
{
  static grub_uint8_t *comb_types = NULL;
  struct grub_unicode_compact_range *cur;

  if (!comb_types)
    {
      unsigned i;
      comb_types = grub_zalloc (GRUB_UNICODE_MAX_CACHED_CHAR);
      if (comb_types)
372 373
	for (cur = grub_unicode_compact; cur->len; cur++)
	  for (i = cur->start; i < cur->start + (unsigned) cur->len
374 375 376 377 378 379 380 381 382
		 && i < GRUB_UNICODE_MAX_CACHED_CHAR; i++)
	    comb_types[i] = cur->comb_type;
      else
	grub_errno = GRUB_ERR_NONE;
    }

  if (comb_types && c < GRUB_UNICODE_MAX_CACHED_CHAR)
    return comb_types[c];

383 384
  for (cur = grub_unicode_compact; cur->len; cur++)
    if (cur->start <= c && c < cur->start + (unsigned) cur->len)
385 386 387 388 389
      return cur->comb_type;

  return GRUB_UNICODE_COMB_NONE;
}

390
#if HAVE_FONT_SOURCE
391

392
grub_size_t
393 394 395 396 397 398 399 400 401 402 403 404
grub_unicode_estimate_width (const struct grub_unicode_glyph *c)
{
  if (grub_unicode_get_comb_type (c->base))
    return 0;
  if (widthspec[c->base >> 3] & (1 << (c->base & 7)))
    return 2;
  else
    return 1;
}

#endif

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
static inline int
is_type_after (enum grub_comb_type a, enum grub_comb_type b)
{
  /* Shadda is numerically higher than most of Arabic diacritics but has
     to be rendered before them.  */
  if (a == GRUB_UNICODE_COMB_ARABIC_SHADDA 
      && b <= GRUB_UNICODE_COMB_ARABIC_KASRA
      && b >= GRUB_UNICODE_COMB_ARABIC_FATHATAN)
    return 0;
  if (b == GRUB_UNICODE_COMB_ARABIC_SHADDA 
      && a <= GRUB_UNICODE_COMB_ARABIC_KASRA
      && a >= GRUB_UNICODE_COMB_ARABIC_FATHATAN)
    return 1;
  return a > b;
}

421 422 423 424 425 426 427 428 429 430
grub_size_t
grub_unicode_aglomerate_comb (const grub_uint32_t *in, grub_size_t inlen,
			      struct grub_unicode_glyph *out)
{
  int haveout = 0;
  const grub_uint32_t *ptr;
  unsigned last_comb_pointer = 0;

  grub_memset (out, 0, sizeof (*out));

431 432 433 434 435 436 437 438 439 440
  if (inlen && grub_iscntrl (*in))
    {
      out->base = *in;
      out->variant = 0;
      out->attributes = 0;
      out->ncomb = 0;
      out->estimated_width = 1;
      return 1;
    }

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
  for (ptr = in; ptr < in + inlen; ptr++)
    {
      /* Variation selectors >= 17 are outside of BMP and SMP. 
	 Handle variation selectors first to avoid potentially costly lookups.
      */
      if (*ptr >= GRUB_UNICODE_VARIATION_SELECTOR_1
	  && *ptr <= GRUB_UNICODE_VARIATION_SELECTOR_16)
	{
	  if (haveout)
	    out->variant = *ptr - GRUB_UNICODE_VARIATION_SELECTOR_1 + 1;
	  continue;
	}
      if (*ptr >= GRUB_UNICODE_VARIATION_SELECTOR_17
	  && *ptr <= GRUB_UNICODE_VARIATION_SELECTOR_256)
	{
	  if (haveout)
	    out->variant = *ptr - GRUB_UNICODE_VARIATION_SELECTOR_17 + 17;
	  continue;
	}
	
      enum grub_comb_type comb_type;
      comb_type = grub_unicode_get_comb_type (*ptr);
      if (comb_type)
	{
465
	  struct grub_unicode_combining *n;
466 467 468 469 470 471 472 473 474
	  unsigned j;

	  if (!haveout)
	    continue;

	  if (comb_type == GRUB_UNICODE_COMB_MC
	      || comb_type == GRUB_UNICODE_COMB_ME
	      || comb_type == GRUB_UNICODE_COMB_MN)
	    last_comb_pointer = out->ncomb;
475 476 477 478

	  if (out->ncomb + 1 <= (int) ARRAY_SIZE (out->combining_inline))
	    n = out->combining_inline;
	  else if (out->ncomb > (int) ARRAY_SIZE (out->combining_inline))
479
	    {
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
	      n = grub_realloc (out->combining_ptr,
				sizeof (n[0]) * (out->ncomb + 1));
	      if (!n)
		{
		  grub_errno = GRUB_ERR_NONE;
		  continue;
		}
	      out->combining_ptr = n;
	    }
	  else
	    {
	      n = grub_malloc (sizeof (n[0]) * (out->ncomb + 1));
	      if (!n)
		{
		  grub_errno = GRUB_ERR_NONE;
		  continue;
		}
	      grub_memcpy (n, out->combining_inline,
			   sizeof (out->combining_inline));
	      out->combining_ptr = n;
500 501 502
	    }

	  for (j = last_comb_pointer; j < out->ncomb; j++)
503
	    if (is_type_after (n[j].type, comb_type))
504
	      break;
505 506
	  grub_memmove (n + j + 1,
			n + j,
507
			(out->ncomb - j)
508 509 510
			* sizeof (n[0]));
	  n[j].code = *ptr;
	  n[j].type = comb_type;
511 512 513 514 515 516 517 518 519 520
	  out->ncomb++;
	  continue;
	}
      if (haveout)
	return ptr - in;
      haveout = 1;
      out->base = *ptr;
      out->variant = 0;
      out->attributes = 0;
      out->ncomb = 0;
521
      out->estimated_width = 1;
522 523 524 525
    }
  return ptr - in;
}

526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
static void
revert (struct grub_unicode_glyph *visual,
	struct grub_term_pos *pos,
	unsigned start, unsigned end)
{
  struct grub_unicode_glyph t;
  unsigned i;
  int a = 0, b = 0;
  if (pos)
    {
      a = pos[visual[start].orig_pos].x;
      b = pos[visual[end].orig_pos].x;
    }
  for (i = 0; i < (end - start) / 2 + 1; i++)
    {
      t = visual[start + i];
      visual[start + i] = visual[end - i];
      visual[end - i] = t;

      if (pos)
	{
	  pos[visual[start + i].orig_pos].x = a + b - pos[visual[start + i].orig_pos].x;
	  pos[visual[end - i].orig_pos].x = a + b - pos[visual[end - i].orig_pos].x;
	}
    }
}


554
static grub_ssize_t
555 556
bidi_line_wrap (struct grub_unicode_glyph *visual_out,
		struct grub_unicode_glyph *visual,
557
		grub_size_t visual_len,
558
		grub_size_t (*getcharwidth) (const struct grub_unicode_glyph *visual, void *getcharwidth_arg),
559
		void *getcharwidth_arg,
560 561 562 563
		grub_size_t maxwidth, grub_size_t startwidth,
		grub_uint32_t contchar,
		struct grub_term_pos *pos, int primitive_wrap,
		grub_size_t log_end)
564 565 566
{
  struct grub_unicode_glyph *outptr = visual_out;
  unsigned line_start = 0;
567
  grub_ssize_t line_width;
568
  unsigned k;
569 570
  grub_ssize_t last_space = -1;
  grub_ssize_t last_space_width = 0;
571
  int lines = 0;
572 573 574 575

  if (!visual_len)
    return 0;

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
  if (startwidth >= maxwidth && (grub_ssize_t) maxwidth > 0)
    {
      if (contchar)
	{
	  grub_memset (outptr, 0, sizeof (visual[0]));
	  outptr->base = contchar;
	  outptr++;
	}
      grub_memset (outptr, 0, sizeof (visual[0]));
      outptr->base = '\n';
      outptr++;
      startwidth = 0;
    }

  line_width = startwidth;

592 593 594
  for (k = 0; k <= visual_len; k++)
    {
      grub_ssize_t last_width = 0;
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
 
      if (pos && k != visual_len)
	{
	  pos[visual[k].orig_pos].x = line_width;
	  pos[visual[k].orig_pos].y = lines;
	  pos[visual[k].orig_pos].valid = 1;
	}

      if (k == visual_len && pos)
	{
	  pos[log_end].x = line_width;
	  pos[log_end].y = lines;
	  pos[log_end].valid = 1;
	}

610
      if (getcharwidth && k != visual_len)
611
	line_width += last_width = getcharwidth (&visual[k], getcharwidth_arg);
612

613
      if (k != visual_len && (visual[k].base == ' '
614 615
			      || visual[k].base == '\t')
	  && !primitive_wrap)
616 617 618 619 620
	{
	  last_space = k;
	  last_space_width = line_width;
	}

621 622
      if (((grub_ssize_t) maxwidth > 0 
	   && line_width > (grub_ssize_t) maxwidth) || k == visual_len)
623
	{	  
624 625
	  unsigned min_odd_level = 0xffffffff;
	  unsigned max_level = 0;
626
	  unsigned kk = k;
627

628 629
	  lines++;

630
	  if (k != visual_len && last_space > (signed) line_start)
631 632 633 634
	    {
	      kk = last_space;
	      line_width -= last_space_width;
	    }
635
	  else if (k != visual_len && line_start == 0 && startwidth != 0
636 637
		   && !primitive_wrap && lines == 1
		   && line_width - startwidth < maxwidth)
638
	    {
639 640
	      kk = 0;
	      line_width -= startwidth;
641
	    }
642
	  else
643
	    line_width = last_width;
644

645 646
	  {
	    unsigned i;
647
	    for (i = line_start; i < kk; i++)
648
	      {
649 650 651 652
		if (visual[i].bidi_level > max_level)
		  max_level = visual[i].bidi_level;
		if (visual[i].bidi_level < min_odd_level && (visual[i].bidi_level & 1))
		  min_odd_level = visual[i].bidi_level;
653 654 655 656 657 658
	      }
	  }

	  {
	    unsigned j;	  
	    /* FIXME: can be optimized.  */
659
	    for (j = max_level; j > min_odd_level - 1; j--)
660
	      {
661
		unsigned in = line_start;
662
		unsigned i;
663
		for (i = line_start; i < kk; i++)
664
		  {
665 666
		    if (i != line_start && visual[i].bidi_level >= j
			&& visual[i-1].bidi_level < j)
667
		      in = i;
668
		    if (visual[i].bidi_level >= j && (i + 1 == kk
669
						 || visual[i+1].bidi_level < j))
670
		      revert (visual, pos, in, i);
671 672 673
		  }
	      }
	  }
674
	  
675 676
	  {
	    unsigned i;
677
	    for (i = line_start; i < kk; i++)
678
	      {
679
		if (is_mirrored (visual[i].base) && visual[i].bidi_level)
680 681
		  visual[i].attributes |= GRUB_UNICODE_GLYPH_ATTRIBUTE_MIRROR;
		if ((visual[i].attributes & GRUB_UNICODE_GLYPH_ATTRIBUTES_JOIN)
682
		    && visual[i].bidi_level)
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
		  {
		    int left, right;
		    left = visual[i].attributes
		      & (GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED 
			 | GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED_EXPLICIT);
		    right = visual[i].attributes
		      & (GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED 
			 | GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED_EXPLICIT);
		    visual[i].attributes &= ~GRUB_UNICODE_GLYPH_ATTRIBUTES_JOIN;
		    left <<= GRUB_UNICODE_GLYPH_ATTRIBUTES_JOIN_LEFT_TO_RIGHT_SHIFT;
		    right >>= GRUB_UNICODE_GLYPH_ATTRIBUTES_JOIN_LEFT_TO_RIGHT_SHIFT;
		    visual[i].attributes |= (left | right);
		  }
	      }
	  }

	  {
	    int left_join = 0;
	    unsigned i;
702
	    for (i = line_start; i < kk; i++)
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
	      {
		enum grub_join_type join_type = get_join_type (visual[i].base);
		if (!(visual[i].attributes
		      & GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED_EXPLICIT)
		    && (join_type == GRUB_JOIN_TYPE_LEFT
			|| join_type == GRUB_JOIN_TYPE_DUAL))
		  {
		    if (left_join)
		      visual[i].attributes
			|= GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED;
		    else
		      visual[i].attributes
			&= ~GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED;
		  }
		if (join_type == GRUB_JOIN_TYPE_NONJOINING
		    || join_type == GRUB_JOIN_TYPE_LEFT)
		  left_join = 0;
		if (join_type == GRUB_JOIN_TYPE_RIGHT
		    || join_type == GRUB_JOIN_TYPE_DUAL
		    || join_type == GRUB_JOIN_TYPE_CAUSING)
		  left_join = 1;
	      }
	  }

	  {
	    int right_join = 0;
	    signed i;
730
	    for (i = kk - 1; i >= 0 && (unsigned) i + 1 > line_start;
731
		 i--)
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
	      {
		enum grub_join_type join_type = get_join_type (visual[i].base);
		if (!(visual[i].attributes
		      & GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED_EXPLICIT)
		    && (join_type == GRUB_JOIN_TYPE_RIGHT
			|| join_type == GRUB_JOIN_TYPE_DUAL))
		  {
		    if (right_join)
		      visual[i].attributes
			|= GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED;
		    else
		      visual[i].attributes
			&= ~GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED;
		  }
		if (join_type == GRUB_JOIN_TYPE_NONJOINING
		    || join_type == GRUB_JOIN_TYPE_RIGHT)
		  right_join = 0;
		if (join_type == GRUB_JOIN_TYPE_LEFT
		    || join_type == GRUB_JOIN_TYPE_DUAL
		    || join_type == GRUB_JOIN_TYPE_CAUSING)
		  right_join = 1;
	      }
	  }		
755

756
	  grub_memcpy (outptr, &visual[line_start],
757 758 759
		       (kk - line_start) * sizeof (visual[0]));
	  outptr += kk - line_start;
	  if (kk != visual_len)
760
	    {
761 762 763 764 765 766
	      if (contchar)
		{
		  grub_memset (outptr, 0, sizeof (visual[0]));
		  outptr->base = contchar;
		  outptr++;
		}
767 768 769 770
	      grub_memset (outptr, 0, sizeof (visual[0]));
	      outptr->base = '\n';
	      outptr++;
	    }
771

772 773
	  if ((signed) kk == last_space)
	    kk++;
774

775 776
	  line_start = kk;
	  if (pos && kk != visual_len)
777
	    {
778 779
	      pos[visual[kk].orig_pos].x = 0;
	      pos[visual[kk].orig_pos].y = lines;
780
	    }
781 782 783 784 785 786 787
	}
    }

  return outptr - visual_out;
}


788 789 790 791
static grub_ssize_t
grub_bidi_line_logical_to_visual (const grub_uint32_t *logical,
				  grub_size_t logical_len,
				  struct grub_unicode_glyph *visual_out,
792
				  grub_size_t (*getcharwidth) (const struct grub_unicode_glyph *visual, void *getcharwidth_arg),
793
				  void *getcharwidth_arg,
794 795 796 797 798
				  grub_size_t maxwidth, grub_size_t startwidth,
				  grub_uint32_t contchar,
				  struct grub_term_pos *pos,
				  int primitive_wrap,
				  grub_size_t log_end)
799 800 801 802 803 804 805 806 807 808 809 810 811 812
{
  enum grub_bidi_type type = GRUB_BIDI_TYPE_L;
  enum override_status {OVERRIDE_NEUTRAL = 0, OVERRIDE_R, OVERRIDE_L};
  unsigned base_level;
  enum override_status cur_override;
  unsigned i;
  unsigned stack_level[GRUB_BIDI_MAX_EXPLICIT_LEVEL + 3];
  enum override_status stack_override[GRUB_BIDI_MAX_EXPLICIT_LEVEL + 3];
  unsigned stack_depth = 0;
  unsigned invalid_pushes = 0;
  unsigned visual_len = 0;
  unsigned run_start, run_end;
  struct grub_unicode_glyph *visual;
  unsigned cur_level;
813
  int bidi_needed = 0;
814

815 816 817 818 819 820 821 822 823 824 825 826 827 828
#define push_stack(new_override, new_level)		\
  {							\
    if (new_level > GRUB_BIDI_MAX_EXPLICIT_LEVEL)	\
      {							\
      invalid_pushes++;					\
      }							\
    else						\
      {							\
      stack_level[stack_depth] = cur_level;		\
      stack_override[stack_depth] = cur_override;	\
      stack_depth++;					\
      cur_level = new_level;				\
      cur_override = new_override;			\
      }							\
829 830
  }

831 832 833 834 835 836 837 838 839 840 841 842
#define pop_stack()				\
  {						\
    if (invalid_pushes)				\
      {						\
      invalid_pushes--;				\
      }						\
    else if (stack_depth)			\
      {						\
      stack_depth--;				\
      cur_level = stack_level[stack_depth];	\
      cur_override = stack_override[stack_depth];	\
      }							\
843 844 845 846
  }

  visual = grub_malloc (sizeof (visual[0]) * logical_len);
  if (!visual)
847
    return -1;
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864

  for (i = 0; i < logical_len; i++)
    {
      type = get_bidi_type (logical[i]);
      if (type == GRUB_BIDI_TYPE_L || type == GRUB_BIDI_TYPE_AL
	  || type == GRUB_BIDI_TYPE_R)
	break;
    }
  if (type == GRUB_BIDI_TYPE_R || type == GRUB_BIDI_TYPE_AL)
    base_level = 1;
  else
    base_level = 0;
  
  cur_level = base_level;
  cur_override = OVERRIDE_NEUTRAL;
  {
    const grub_uint32_t *lptr;
865 866
    enum {JOIN_DEFAULT, NOJOIN, JOIN_FORCE} join_state = JOIN_DEFAULT;
    int zwj_propagate_to_previous = 0;
867 868
    for (lptr = logical; lptr < logical + logical_len;)
      {
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
	grub_size_t p;

	if (*lptr == GRUB_UNICODE_ZWJ)
	  {
	    if (zwj_propagate_to_previous)
	      {
		visual[visual_len - 1].attributes
		  |= GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED_EXPLICIT
		  | GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED;
	      }
	    zwj_propagate_to_previous = 0;
	    join_state = JOIN_FORCE;
	    lptr++;
	    continue;
	  }

	if (*lptr == GRUB_UNICODE_ZWNJ)
	  {
	    if (zwj_propagate_to_previous)
	      {
		visual[visual_len - 1].attributes
		  |= GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED_EXPLICIT;
		visual[visual_len - 1].attributes 
		  &= ~GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED;
	      }
	    zwj_propagate_to_previous = 0;
	    join_state = NOJOIN;
	    lptr++;
	    continue;
	  }

900 901 902 903
	/* The tags: deprecated, never used.  */
	if (*lptr >= GRUB_UNICODE_TAG_START && *lptr <= GRUB_UNICODE_TAG_END)
	  continue;

904 905
	p = grub_unicode_aglomerate_comb (lptr, logical + logical_len - lptr, 
					  &visual[visual_len]);
906
	visual[visual_len].orig_pos = lptr - logical;
907 908 909 910
	type = get_bidi_type (visual[visual_len].base);
	switch (type)
	  {
	  case GRUB_BIDI_TYPE_RLE:
911
	    bidi_needed = 1;
912 913 914
	    push_stack (cur_override, (cur_level | 1) + 1);
	    break;
	  case GRUB_BIDI_TYPE_RLO:
915
	    bidi_needed = 1;
916 917 918 919 920 921 922 923 924 925 926 927 928
	    push_stack (OVERRIDE_R, (cur_level | 1) + 1);
	    break;
	  case GRUB_BIDI_TYPE_LRE:
	    push_stack (cur_override, (cur_level & ~1) + 2);
	    break;
	  case GRUB_BIDI_TYPE_LRO:
	    push_stack (OVERRIDE_L, (cur_level & ~1) + 2);
	    break;
	  case GRUB_BIDI_TYPE_PDF:
	    pop_stack ();
	    break;
	  case GRUB_BIDI_TYPE_BN:
	    break;
929 930 931
	  case GRUB_BIDI_TYPE_R:
	  case GRUB_BIDI_TYPE_AL:
	    bidi_needed = 1;
932
	    /* Fallthrough.  */
933 934
	  default:
	    {
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
	      if (join_state == JOIN_FORCE)
		{
		  visual[visual_len].attributes
		    |= GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED_EXPLICIT
		    | GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED;
		}
	      
	      if (join_state == NOJOIN)
		{
		  visual[visual_len].attributes
		    |= GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED_EXPLICIT;
		  visual[visual_len].attributes
		    &= ~GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED;
		}

	      join_state = JOIN_DEFAULT;
	      zwj_propagate_to_previous = 1;

953
	      visual[visual_len].bidi_level = cur_level;
954
	      if (cur_override != OVERRIDE_NEUTRAL)
955
		visual[visual_len].bidi_type = 
956 957 958
		  (cur_override == OVERRIDE_L) ? GRUB_BIDI_TYPE_L
		  : GRUB_BIDI_TYPE_R;
	      else
959
		visual[visual_len].bidi_type = type;
960 961 962 963 964 965 966
	      visual_len++;
	    }
	  }
	lptr += p;
      }
  }

967
  if (bidi_needed)
968
    {
969
      for (run_start = 0; run_start < visual_len; run_start = run_end)
970
	{
971 972 973
	  unsigned prev_level, next_level, cur_run_level;
	  unsigned last_type, last_strong_type;
	  for (run_end = run_start; run_end < visual_len &&
974
		 visual[run_end].bidi_level == visual[run_start].bidi_level; run_end++);
975 976 977
	  if (run_start == 0)
	    prev_level = base_level;
	  else
978
	    prev_level = visual[run_start - 1].bidi_level;
979 980 981
	  if (run_end == visual_len)
	    next_level = base_level;
	  else
982 983
	    next_level = visual[run_end].bidi_level;
	  cur_run_level = visual[run_start].bidi_level;
984 985 986 987 988 989
	  if (prev_level & 1)
	    last_type = GRUB_BIDI_TYPE_R;
	  else
	    last_type = GRUB_BIDI_TYPE_L;
	  last_strong_type = last_type;
	  for (i = run_start; i < run_end; i++)
990
	    {
991
	      switch (visual[i].bidi_type)
992 993
		{
		case GRUB_BIDI_TYPE_NSM:
994
		  visual[i].bidi_type = last_type;
995 996 997
		  break;
		case GRUB_BIDI_TYPE_EN:
		  if (last_strong_type == GRUB_BIDI_TYPE_AL)
998
		    visual[i].bidi_type = GRUB_BIDI_TYPE_AN;
999 1000 1001
		  break;
		case GRUB_BIDI_TYPE_L:
		case GRUB_BIDI_TYPE_R:
1002
		  last_strong_type = visual[i].bidi_type;
1003 1004 1005 1006
		  break;
		case GRUB_BIDI_TYPE_ES:
		  if (last_type == GRUB_BIDI_TYPE_EN
		      && i + 1 < run_end 
1007 1008
		      && visual[i + 1].bidi_type == GRUB_BIDI_TYPE_EN)
		    visual[i].bidi_type = GRUB_BIDI_TYPE_EN;
1009
		  else
1010
		    visual[i].bidi_type = GRUB_BIDI_TYPE_ON;
1011 1012
		  break;
		case GRUB_BIDI_TYPE_ET:
1013
		  {
1014 1015 1016
		    unsigned j;
		    if (last_type == GRUB_BIDI_TYPE_EN)
		      {
1017
			visual[i].bidi_type = GRUB_BIDI_TYPE_EN;
1018 1019 1020
			break;
		      }
		    for (j = i; j < run_end
1021 1022
			   && visual[j].bidi_type == GRUB_BIDI_TYPE_ET; j++);
		    if (j != run_end && visual[j].bidi_type == GRUB_BIDI_TYPE_EN)
1023 1024
		      {
			for (; i < run_end
1025 1026
			       && visual[i].bidi_type == GRUB_BIDI_TYPE_ET; i++)
			  visual[i].bidi_type = GRUB_BIDI_TYPE_EN;
1027 1028 1029
			i--;
			break;
		      }
1030
		    for (; i < run_end
1031 1032
			   && visual[i].bidi_type == GRUB_BIDI_TYPE_ET; i++)
		      visual[i].bidi_type = GRUB_BIDI_TYPE_ON;
1033
		    i--;
1034
		    break;		
1035
		  }
1036 1037 1038 1039
		  break;
		case GRUB_BIDI_TYPE_CS:
		  if (last_type == GRUB_BIDI_TYPE_EN
		      && i + 1 < run_end 
1040
		      && visual[i + 1].bidi_type == GRUB_BIDI_TYPE_EN)
1041
		    {
1042
		      visual[i].bidi_type = GRUB_BIDI_TYPE_EN;
1043 1044 1045 1046
		      break;
		    }
		  if (last_type == GRUB_BIDI_TYPE_AN
		      && i + 1 < run_end 
1047 1048
		      && (visual[i + 1].bidi_type == GRUB_BIDI_TYPE_AN
			  || (visual[i + 1].bidi_type == GRUB_BIDI_TYPE_EN
1049 1050
			      && last_strong_type == GRUB_BIDI_TYPE_AL)))
		    {
1051
		      visual[i].bidi_type = GRUB_BIDI_TYPE_EN;
1052 1053
		      break;
		    }
1054
		  visual[i].bidi_type = GRUB_BIDI_TYPE_ON;
1055 1056
		  break;
		case GRUB_BIDI_TYPE_AL:
1057 1058
		  last_strong_type = visual[i].bidi_type;
		  visual[i].bidi_type = GRUB_BIDI_TYPE_R;
1059 1060
		  break;
		default: /* Make GCC happy.  */
1061 1062
		  break;
		}
1063 1064
	      last_type = visual[i].bidi_type;
	      if (visual[i].bidi_type == GRUB_BIDI_TYPE_EN
1065
		  && last_strong_type == GRUB_BIDI_TYPE_L)
1066
		visual[i].bidi_type = GRUB_BIDI_TYPE_L;
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	    }
	  if (prev_level & 1)
	    last_type = GRUB_BIDI_TYPE_R;
	  else
	    last_type = GRUB_BIDI_TYPE_L;
	  for (i = run_start; i < run_end; )
	    {
	      unsigned j;
	      unsigned next_type;
	      for (j = i; j < run_end &&
1077 1078 1079 1080
		     (visual[j].bidi_type == GRUB_BIDI_TYPE_B
		      || visual[j].bidi_type == GRUB_BIDI_TYPE_S
		      || visual[j].bidi_type == GRUB_BIDI_TYPE_WS
		      || visual[j].bidi_type == GRUB_BIDI_TYPE_ON); j++);
1081
	      if (j == i)
1082
		{
1083
		  if (visual[i].bidi_type == GRUB_BIDI_TYPE_L)
1084 1085 1086 1087 1088
		    last_type = GRUB_BIDI_TYPE_L;
		  else
		    last_type = GRUB_BIDI_TYPE_R;
		  i++;
		  continue;
1089
		}
1090 1091 1092 1093
	      if (j == run_end)
		next_type = (next_level & 1) ? GRUB_BIDI_TYPE_R : GRUB_BIDI_TYPE_L;
	      else
		{
1094
		  if (visual[j].bidi_type == GRUB_BIDI_TYPE_L)
1095 1096 1097 1098 1099 1100
		    next_type = GRUB_BIDI_TYPE_L;
		  else
		    next_type = GRUB_BIDI_TYPE_R;
		}
	      if (next_type == last_type)
		for (; i < j; i++)
1101
		  visual[i].bidi_type = last_type;
1102 1103
	      else
		for (; i < j; i++)
1104
		  visual[i].bidi_type = (cur_run_level & 1) ? GRUB_BIDI_TYPE_R
1105
		    : GRUB_BIDI_TYPE_L;
1106 1107
	    }
	}
1108 1109

      for (i = 0; i < visual_len; i++)
1110
	{
1111
	  if (!(visual[i].bidi_level & 1) && visual[i].bidi_type == GRUB_BIDI_TYPE_R)
1112
	    {
1113
	      visual[i].bidi_level++;
1114 1115
	      continue;
	    }
1116 1117
	  if (!(visual[i].bidi_level & 1) && (visual[i].bidi_type == GRUB_BIDI_TYPE_AN
				   || visual[i].bidi_type == GRUB_BIDI_TYPE_EN))
1118
	    {
1119
	      visual[i].bidi_level += 2;
1120 1121
	      continue;
	    }
1122 1123 1124
	  if ((visual[i].bidi_level & 1) && (visual[i].bidi_type == GRUB_BIDI_TYPE_L
				  || visual[i].bidi_type == GRUB_BIDI_TYPE_AN
				  || visual[i].bidi_type == GRUB_BIDI_TYPE_EN))
1125
	    {
1126
	      visual[i].bidi_level++;
1127
	      continue;
1128 1129 1130
	    }
	}
    }
1131
  else
1132
    {
1133
      for (i = 0; i < visual_len; i++)
1134
	visual[i].bidi_level = 0;
1135 1136 1137
    }

  {
1138
    grub_ssize_t ret;
1139
    ret = bidi_line_wrap (visual_out, visual, visual_len,
1140
			  getcharwidth, getcharwidth_arg, maxwidth, startwidth, contchar,
1141
			  pos, primitive_wrap, log_end);
1142
    grub_free (visual);
1143
    return ret;
1144 1145 1146 1147 1148 1149 1150
  }
}

grub_ssize_t
grub_bidi_logical_to_visual (const grub_uint32_t *logical,
			     grub_size_t logical_len,
			     struct grub_unicode_glyph **visual_out,
1151
			     grub_size_t (*getcharwidth) (const struct grub_unicode_glyph *visual, void *getcharwidth_arg),
1152
			     void *getcharwidth_arg,
1153 1154
			     grub_size_t max_length, grub_size_t startwidth,
			     grub_uint32_t contchar, struct grub_term_pos *pos, int primitive_wrap)
1155 1156 1157
{
  const grub_uint32_t *line_start = logical, *ptr;
  struct grub_unicode_glyph *visual_ptr;
1158
  *visual_out = visual_ptr = grub_malloc (3 * sizeof (visual_ptr[0])
1159
					  * (logical_len + 2));
1160 1161
  if (!visual_ptr)
    return -1;
1162 1163 1164 1165 1166 1167 1168 1169 1170
  for (ptr = logical; ptr <= logical + logical_len; ptr++)
    {
      if (ptr == logical + logical_len || *ptr == '\n')
	{
	  grub_ssize_t ret;
	  ret = grub_bidi_line_logical_to_visual (line_start,
						  ptr - line_start,
						  visual_ptr,
						  getcharwidth,
1171
						  getcharwidth_arg,
1172
						  max_length,
1173 1174 1175 1176 1177
						  startwidth,
						  contchar,
						  pos,
						  primitive_wrap,
						  logical_len);
1178 1179
	  startwidth = 0;

1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
	  if (ret < 0)
	    {
	      grub_free (*visual_out);
	      return ret;
	    }
	  visual_ptr += ret; 
	  line_start = ptr;
	  if (ptr != logical + logical_len)
	    {
	      grub_memset (visual_ptr, 0, sizeof (visual_ptr[0]));
	      visual_ptr->base = '\n';
	      visual_ptr++;
1192
	      line_start++;
1193 1194 1195 1196 1197 1198
	    }
	}
    }
  return visual_ptr - *visual_out;
}

1199 1200
grub_uint32_t
grub_unicode_mirror_code (grub_uint32_t in)
1201 1202 1203 1204 1205 1206 1207
{
  int i;
  for (i = 0; grub_unicode_bidi_pairs[i].key; i++)
    if (grub_unicode_bidi_pairs[i].key == in)
      return grub_unicode_bidi_pairs[i].replace;
  return in;
}
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219

grub_uint32_t
grub_unicode_shape_code (grub_uint32_t in, grub_uint8_t attr)
{
  int i;
  if (!(in >= GRUB_UNICODE_ARABIC_START
	&& in < GRUB_UNICODE_ARABIC_END))
    return in;

  for (i = 0; grub_unicode_arabic_shapes[i].code; i++)
    if (grub_unicode_arabic_shapes[i].code == in)
      {
1220
	grub_uint32_t out = 0;
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
	switch (attr & (GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED
			| GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED))
	  {
	  case 0:
	    out = grub_unicode_arabic_shapes[i].isolated;
	    break;
	  case GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED:
	    out = grub_unicode_arabic_shapes[i].right_linked;
	    break;
	  case GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED:
	    out = grub_unicode_arabic_shapes[i].left_linked;
	    break;
	  case GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED
	    |GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED:
	    out = grub_unicode_arabic_shapes[i].both_linked;
	    break;
	  }
	if (out)
	  return out;
      }

  return in;
}
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268

const grub_uint32_t *
grub_unicode_get_comb_start (const grub_uint32_t *str, 
			     const grub_uint32_t *cur)
{
  const grub_uint32_t *ptr;
  for (ptr = cur; ptr >= str; ptr--)
    {
      if (*ptr >= GRUB_UNICODE_VARIATION_SELECTOR_1
	  && *ptr <= GRUB_UNICODE_VARIATION_SELECTOR_16)
	continue;

      if (*ptr >= GRUB_UNICODE_VARIATION_SELECTOR_17
	  && *ptr <= GRUB_UNICODE_VARIATION_SELECTOR_256)
	continue;
	
      enum grub_comb_type comb_type;
      comb_type = grub_unicode_get_comb_type (*ptr);
      if (comb_type)
	continue;
      return ptr;
    }
  return str;
}

1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
const grub_uint32_t *
grub_unicode_get_comb_end (const grub_uint32_t *end, 
			   const grub_uint32_t *cur)
{
  const grub_uint32_t *ptr;
  for (ptr = cur; ptr < end; ptr++)
    {
      if (*ptr >= GRUB_UNICODE_VARIATION_SELECTOR_1
	  && *ptr <= GRUB_UNICODE_VARIATION_SELECTOR_16)
	continue;

      if (*ptr >= GRUB_UNICODE_VARIATION_SELECTOR_17
	  && *ptr <= GRUB_UNICODE_VARIATION_SELECTOR_256)
	continue;
	
      enum grub_comb_type comb_type;
      comb_type = grub_unicode_get_comb_type (*ptr);
      if (comb_type)
	continue;
      return ptr;
    }
  return end;
}