~ubuntu-branches/ubuntu/vivid/fribidi/vivid

« back to all changes in this revision

Viewing changes to fribidi_char_sets_utf8.c

  • Committer: Bazaar Package Importer
  • Author(s): Lior Kaplan
  • Date: 2006-09-16 10:37:10 UTC
  • mfrom: (3.1.3 edgy)
  • Revision ID: james.westby@ubuntu.com-20060916103710-1ktngzyx9azkujhl
Tags: 0.10.7-4
* Fix manual page name section (Closes: #368632)
* debian/control: Upgrade standards version to 3.7.2 (no changes needed).
* debian/copyright: Update Dov Grobgeld's contact address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
24
#include "fribidi_config.h"
 
25
#ifndef FRIBIDI_NO_CHARSETS
 
26
 
 
27
#include "fribidi.h"
 
28
 
 
29
/* the following added by Raphael Finkel <raphael@cs.uky.edu> 12/1999 */
 
30
 
 
31
int
 
32
fribidi_utf8_to_unicode (char *s, int len, FriBidiChar *us)
 
33
/* warning: the length of input string may exceed the length of the output */
 
34
{
 
35
  int length;
 
36
  char *t = s;
 
37
 
 
38
  length = 0;
 
39
  while (s - t < len)
 
40
    {
 
41
      if (*(unsigned char *) s <= 0x7f) /* one byte */
 
42
        {
 
43
          *us++ = *s++;         /* expand with 0s */
 
44
        }
 
45
      else if (*(unsigned char *) s <= 0xdf)    /* 2 byte */
 
46
        {
 
47
          *us++ =
 
48
            ((*(unsigned char *) s & 0x1f) << 6) +
 
49
            ((*(unsigned char *) (s + 1)) & 0x3f);
 
50
          s += 2;
 
51
        }
 
52
      else                      /* 3 byte */
 
53
        {
 
54
          *us++ =
 
55
            ((int) (*(unsigned char *) s & 0x0f) << 12) +
 
56
            ((*(unsigned char *) (s + 1) & 0x3f) << 6) +
 
57
            (*(unsigned char *) (s + 2) & 0x3f);
 
58
          s += 3;
 
59
        }
 
60
      length++;
 
61
    }
 
62
  *us = 0;
 
63
  return (length);
 
64
}
 
65
 
 
66
int
 
67
fribidi_unicode_to_utf8 (FriBidiChar *us, int length, char *s)
 
68
/* warning: the length of output string may exceed the length of the input */
 
69
{
 
70
  int i;
 
71
  char *t;
 
72
 
 
73
  t = s;
 
74
  for (i = 0; i < length; i++)
 
75
    {
 
76
      FriBidiChar mychar = us[i];
 
77
      if (mychar <= 0x7F)
 
78
        {                       /* 7 sig bits */
 
79
          *t++ = mychar;
 
80
        }
 
81
      else if (mychar <= 0x7FF)
 
82
        {                       /* 11 sig bits */
 
83
          *t++ = 0xC0 | (unsigned char) (mychar >> 6);  /* upper 5 bits */
 
84
          *t++ = 0x80 | (unsigned char) (mychar & 0x3F);        /* lower 6 bits */
 
85
        }
 
86
      else if (mychar <= 0xFFFF)
 
87
        {                       /* 16 sig bits */
 
88
          *t++ = 0xE0 | (unsigned char) (mychar >> 12); /* upper 4 bits */
 
89
          *t++ = 0x80 | (unsigned char) ((mychar >> 6) & 0x3F); /* next 6 bits */
 
90
          *t++ = 0x80 | (unsigned char) (mychar & 0x3F);        /* lowest 6 bits */
 
91
        }
 
92
      else if (mychar < FRIBIDI_UNICODE_CHARS)
 
93
        {                       /* 21 sig bits */
 
94
          *t++ = 0xF0 | (unsigned char) ((mychar >> 18) & 0x07);        /* upper 3 bits */
 
95
          *t++ = 0x80 | (unsigned char) ((mychar >> 12) & 0x3F);        /* next 6 bits */
 
96
          *t++ = 0x80 | (unsigned char) ((mychar >> 6) & 0x3F); /* next 6 bits */
 
97
          *t++ = 0x80 | (unsigned char) (mychar & 0x3F);        /* lowest 6 bits */
 
98
        }
 
99
    }
 
100
  *t = 0;
 
101
 
 
102
  return (t - s);
 
103
}
 
104
 
 
105
#endif