hb-common.cc 5.6 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2 3
 * Copyright © 2009,2010  Red Hat, Inc.
 * Copyright © 2011  Google, Inc.
B
Behdad Esfahbod 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Red Hat Author(s): Behdad Esfahbod
B
Behdad Esfahbod 已提交
26
 * Google Author(s): Behdad Esfahbod
B
Behdad Esfahbod 已提交
27 28
 */

29
#include "hb-private.hh"
B
Behdad Esfahbod 已提交
30

B
Behdad Esfahbod 已提交
31 32 33
HB_BEGIN_DECLS


34 35
/* hb_tag_t */

B
Behdad Esfahbod 已提交
36 37 38 39 40 41
hb_tag_t
hb_tag_from_string (const char *s)
{
  char tag[4];
  unsigned int i;

42
  if (!s || !*s)
43 44
    return HB_TAG_NONE;

B
Behdad Esfahbod 已提交
45 46 47 48 49
  for (i = 0; i < 4 && s[i]; i++)
    tag[i] = s[i];
  for (; i < 4; i++)
    tag[i] = ' ';

50
  return HB_TAG_CHAR4 (tag);
B
Behdad Esfahbod 已提交
51
}
B
Behdad Esfahbod 已提交
52 53


54 55
/* hb_language_t */

B
Behdad Esfahbod 已提交
56 57 58 59
struct _hb_language_t {
  const char s[1];
};

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
static const char canon_map[256] = {
   0,   0,   0,   0,   0,   0,   0,   0,    0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,    0,   0,   0,   0,   0,   0,   0,   0,
   0,   0,   0,   0,   0,   0,   0,   0,    0,   0,   0,   0,   0,  '-',  0,   0,
  '0', '1', '2', '3', '4', '5', '6', '7',  '8', '9',  0,   0,   0,   0,   0,   0,
  '-', 'a', 'b', 'c', 'd', 'e', 'f', 'g',  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',  'x', 'y', 'z',  0,   0,   0,   0,  '-',
   0,  'a', 'b', 'c', 'd', 'e', 'f', 'g',  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  'p', 'q', 'r', 's', 't', 'u', 'v', 'w',  'x', 'y', 'z',  0,   0,   0,   0,   0
};

static hb_bool_t
lang_equal (const void *v1,
	    const void *v2)
{
75 76
  const unsigned char *p1 = (const unsigned char *) v1;
  const unsigned char *p2 = (const unsigned char *) v2;
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 103 104 105 106 107

  while (canon_map[*p1] && canon_map[*p1] == canon_map[*p2])
    {
      p1++, p2++;
    }

  return (canon_map[*p1] == canon_map[*p2]);
}

#if 0
static unsigned int
lang_hash (const void *key)
{
  const unsigned char *p = key;
  unsigned int h = 0;
  while (canon_map[*p])
    {
      h = (h << 5) - h + canon_map[*p];
      p++;
    }

  return h;
}
#endif


hb_language_t
hb_language_from_string (const char *str)
{
  static unsigned int num_langs;
  static unsigned int num_alloced;
B
Behdad Esfahbod 已提交
108
  static hb_language_t *langs;
109 110 111 112 113 114 115 116 117
  unsigned int i;
  unsigned char *p;

  /* TODO Use a hash table or something */

  if (!str || !*str)
    return NULL;

  for (i = 0; i < num_langs; i++)
B
Behdad Esfahbod 已提交
118
    if (lang_equal (str, langs[i]->s))
119 120 121 122
      return langs[i];

  if (unlikely (num_langs == num_alloced)) {
    unsigned int new_alloced = 2 * (8 + num_alloced);
123
    hb_language_t *new_langs = (hb_language_t *) realloc (langs, new_alloced * sizeof (langs[0]));
124 125 126 127 128 129
    if (!new_langs)
      return NULL;
    num_alloced = new_alloced;
    langs = new_langs;
  }

B
Behdad Esfahbod 已提交
130 131
  langs[i] = (hb_language_t) strdup (str);
  for (p = (unsigned char *) langs[i]->s; *p; p++)
132 133 134 135
    *p = canon_map[*p];

  num_langs++;

B
Behdad Esfahbod 已提交
136
  return langs[i];
137 138 139 140 141
}

const char *
hb_language_to_string (hb_language_t language)
{
B
Behdad Esfahbod 已提交
142
  return language->s;
143 144 145
}


B
Behdad Esfahbod 已提交
146 147 148 149 150
/* hb_script_t */

hb_script_t
hb_script_from_iso15924_tag (hb_tag_t tag)
{
151 152 153
  if (unlikely (tag == HB_TAG_NONE))
    return HB_SCRIPT_INVALID;

B
Behdad Esfahbod 已提交
154 155 156
  /* Be lenient, adjust case (one capital letter followed by three small letters) */
  tag = (tag & 0xDFDFDFDF) | 0x00202020;

157 158 159 160 161 162 163 164 165 166
  switch (tag) {
    case HB_TAG('C','y','r','s'): return HB_SCRIPT_CYRILLIC;
    case HB_TAG('G','e','o','a'): return HB_SCRIPT_GEORGIAN;
    case HB_TAG('G','e','o','n'): return HB_SCRIPT_GEORGIAN;
    case HB_TAG('L','a','t','f'): return HB_SCRIPT_LATIN;
    case HB_TAG('L','a','t','g'): return HB_SCRIPT_LATIN;
    case HB_TAG('S','y','r','e'): return HB_SCRIPT_SYRIAC;
    case HB_TAG('S','y','r','j'): return HB_SCRIPT_SYRIAC;
    case HB_TAG('S','y','r','n'): return HB_SCRIPT_SYRIAC;
  }
B
Behdad Esfahbod 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

  /* If it looks right, just use the tag as a script */
  if (((uint32_t) tag & 0xE0E0E0E0) == 0x40606060)
    return (hb_script_t) tag;

  /* Otherwise, return unknown */
  return HB_SCRIPT_UNKNOWN;
}

hb_script_t
hb_script_from_string (const char *s)
{
  return hb_script_from_iso15924_tag (hb_tag_from_string (s));
}

hb_tag_t
hb_script_to_iso15924_tag (hb_script_t script)
{
185
  return (hb_tag_t) script;
B
Behdad Esfahbod 已提交
186 187 188 189 190
}

hb_direction_t
hb_script_get_horizontal_direction (hb_script_t script)
{
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  switch ((hb_tag_t) script)
  {
    case HB_SCRIPT_ARABIC:
    case HB_SCRIPT_HEBREW:
    case HB_SCRIPT_SYRIAC:
    case HB_SCRIPT_THAANA:

    /* Unicode-4.0 additions */
    case HB_SCRIPT_CYPRIOT:

    /* Unicode-5.0 additions */
    case HB_SCRIPT_PHOENICIAN:
    case HB_SCRIPT_NKO:

    /* Unicode-5.2 additions */
    case HB_SCRIPT_AVESTAN:
    case HB_SCRIPT_IMPERIAL_ARAMAIC:
    case HB_SCRIPT_INSCRIPTIONAL_PAHLAVI:
    case HB_SCRIPT_INSCRIPTIONAL_PARTHIAN:
    case HB_SCRIPT_OLD_SOUTH_ARABIAN:
    case HB_SCRIPT_OLD_TURKIC:
    case HB_SCRIPT_SAMARITAN:

    /* Unicode-6.0 additions */
    case HB_SCRIPT_MANDAIC:

      return HB_DIRECTION_RTL;
  }
B
Behdad Esfahbod 已提交
219

220
  return HB_DIRECTION_LTR;
B
Behdad Esfahbod 已提交
221 222 223
}


B
Behdad Esfahbod 已提交
224
HB_END_DECLS