pango-bidi-type.c 9.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* Pango
 * pango-bidi-type.c: Bidirectional Character Types
 *
 * Copyright (C) 2008 Jürg Billeter <j@bitron.ch>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

22
#include "config.h"
23

24
25
#include <string.h>

26
27
#include <fribidi.h>

28
29
#undef PANGO_DISABLE_DEPRECATED

30
#include "pango-bidi-type.h"
Behdad Esfahbod's avatar
Behdad Esfahbod committed
31
#include "pango-utils.h"
32
33

/**
34
 * pango_bidi_type_for_unichar:
35
36
 * @ch: a Unicode character
 *
Matthias Clasen's avatar
Matthias Clasen committed
37
 * Determines the bidirectional type of a character.
38
 *
Matthias Clasen's avatar
Matthias Clasen committed
39
40
41
 * The bidirectional type is specified in the Unicode Character Database.
 *
 * A simplified version of this function is available as [func@unichar_direction].
42
43
44
45
46
47
48
49
50
 *
 * Return value: the bidirectional character type, as used in the
 * Unicode bidirectional algorithm.
 *
 * Since: 1.22
 */
PangoBidiType
pango_bidi_type_for_unichar (gunichar ch)
{
51
52
53
54
55
  FriBidiCharType fribidi_ch_type;

  G_STATIC_ASSERT (sizeof (FriBidiChar) == sizeof (gunichar));

  fribidi_ch_type = fribidi_get_bidi_type (ch);
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

  switch (fribidi_ch_type)
    {
    case FRIBIDI_TYPE_LTR:  return PANGO_BIDI_TYPE_L;
    case FRIBIDI_TYPE_LRE:  return PANGO_BIDI_TYPE_LRE;
    case FRIBIDI_TYPE_LRO:  return PANGO_BIDI_TYPE_LRO;
    case FRIBIDI_TYPE_RTL:  return PANGO_BIDI_TYPE_R;
    case FRIBIDI_TYPE_AL:   return PANGO_BIDI_TYPE_AL;
    case FRIBIDI_TYPE_RLE:  return PANGO_BIDI_TYPE_RLE;
    case FRIBIDI_TYPE_RLO:  return PANGO_BIDI_TYPE_RLO;
    case FRIBIDI_TYPE_PDF:  return PANGO_BIDI_TYPE_PDF;
    case FRIBIDI_TYPE_EN:   return PANGO_BIDI_TYPE_EN;
    case FRIBIDI_TYPE_ES:   return PANGO_BIDI_TYPE_ES;
    case FRIBIDI_TYPE_ET:   return PANGO_BIDI_TYPE_ET;
    case FRIBIDI_TYPE_AN:   return PANGO_BIDI_TYPE_AN;
    case FRIBIDI_TYPE_CS:   return PANGO_BIDI_TYPE_CS;
    case FRIBIDI_TYPE_NSM:  return PANGO_BIDI_TYPE_NSM;
    case FRIBIDI_TYPE_BN:   return PANGO_BIDI_TYPE_BN;
    case FRIBIDI_TYPE_BS:   return PANGO_BIDI_TYPE_B;
    case FRIBIDI_TYPE_SS:   return PANGO_BIDI_TYPE_S;
    case FRIBIDI_TYPE_WS:   return PANGO_BIDI_TYPE_WS;
    case FRIBIDI_TYPE_ON:   return PANGO_BIDI_TYPE_ON;
Matthias Clasen's avatar
Matthias Clasen committed
78
79
80
81
    case FRIBIDI_TYPE_LRI:  return PANGO_BIDI_TYPE_LRI;
    case FRIBIDI_TYPE_RLI:  return PANGO_BIDI_TYPE_RLI;
    case FRIBIDI_TYPE_FSI:  return PANGO_BIDI_TYPE_FSI;
    case FRIBIDI_TYPE_PDI:  return PANGO_BIDI_TYPE_PDI;
82
    case _FRIBIDI_TYPE_SENTINEL:
83
84
85
86
    default:
      return PANGO_BIDI_TYPE_ON;
    }
}
87
88
89
90
91

/* Some bidi-related functions */

/**
 * pango_log2vis_get_embedding_levels:
92
93
94
 * @text: the text to itemize.
 * @length: the number of bytes (not characters) to process, or -1
 *   if @text is nul-terminated and the length should be calculated.
95
96
 * @pbase_dir: input base direction, and output resolved direction.
 *
Matthias Clasen's avatar
Matthias Clasen committed
97
98
99
100
 * Return the bidirectional embedding levels of the input paragraph.
 *
 * The bidirectional embedding levels are defined by the Unicode Bidirectional
 * Algorithm available at:
101
102
103
104
105
106
107
 *
 *   http://www.unicode.org/reports/tr9/
 *
 * If the input base direction is a weak direction, the direction of the
 * characters in the text will determine the final resolved direction.
 *
 * Return value: a newly allocated array of embedding levels, one item per
Matthias Clasen's avatar
Matthias Clasen committed
108
 *   character (not byte), that should be freed using g_free().
109
110
111
112
113
114
115
116
 *
 * Since: 1.4
 */
guint8 *
pango_log2vis_get_embedding_levels (const gchar    *text,
				    int             length,
				    PangoDirection *pbase_dir)
{
117
  glong n_chars, i;
118
  guint8 *embedding_levels_list;
119
120
121
  const gchar *p;
  FriBidiParType fribidi_base_dir;
  FriBidiCharType *bidi_types;
122
  FriBidiBracketType *bracket_types;
123
  FriBidiLevel max_level;
124
125
  FriBidiCharType ored_types = 0;
  FriBidiCharType anded_strongs = FRIBIDI_TYPE_RLE;
126

127
128
129
  G_STATIC_ASSERT (sizeof (FriBidiLevel) == sizeof (guint8));
  G_STATIC_ASSERT (sizeof (FriBidiChar) == sizeof (gunichar));

130
131
132
133
  switch (*pbase_dir)
    {
    case PANGO_DIRECTION_LTR:
    case PANGO_DIRECTION_TTB_RTL:
134
      fribidi_base_dir = FRIBIDI_PAR_LTR;
135
136
137
      break;
    case PANGO_DIRECTION_RTL:
    case PANGO_DIRECTION_TTB_LTR:
138
      fribidi_base_dir = FRIBIDI_PAR_RTL;
139
140
      break;
    case PANGO_DIRECTION_WEAK_RTL:
141
      fribidi_base_dir = FRIBIDI_PAR_WRTL;
142
143
144
145
      break;
    case PANGO_DIRECTION_WEAK_LTR:
    case PANGO_DIRECTION_NEUTRAL:
    default:
146
      fribidi_base_dir = FRIBIDI_PAR_WLTR;
147
148
149
      break;
    }

150
151
152
153
154
155
  if (length < 0)
    length = strlen (text);

  n_chars = g_utf8_strlen (text, length);

  bidi_types = g_new (FriBidiCharType, n_chars);
156
  bracket_types = g_new (FriBidiBracketType, n_chars);
157
158
159
160
161
  embedding_levels_list = g_new (guint8, n_chars);

  for (i = 0, p = text; p < text + length; p = g_utf8_next_char(p), i++)
    {
      gunichar ch = g_utf8_get_char (p);
162
163
164
165
166
      FriBidiCharType char_type = fribidi_get_bidi_type (ch);

      if (i == n_chars)
        break;

167
168
169
170
      bidi_types[i] = char_type;
      ored_types |= char_type;
      if (FRIBIDI_IS_STRONG (char_type))
        anded_strongs &= char_type;
171
172
173
174
      if (G_UNLIKELY(bidi_types[i] == FRIBIDI_TYPE_ON))
        bracket_types[i] = fribidi_get_bracket (ch);
      else
        bracket_types[i] = FRIBIDI_NO_BRACKET;
175
176
    }

177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
219
220
221
222
223
224
225
    /* Short-circuit (malloc-expensive) FriBidi call for unidirectional
     * text.
     *
     * For details see:
     * https://bugzilla.gnome.org/show_bug.cgi?id=590183
     */

    /* The case that all resolved levels will be ltr.
     * No isolates, all strongs be LTR, there should be no Arabic numbers
     * (or letters for that matter), and one of the following:
     *
     * o base_dir doesn't have an RTL taste.
     * o there are letters, and base_dir is weak.
     */
    if (!FRIBIDI_IS_ISOLATE (ored_types) &&
	!FRIBIDI_IS_RTL (ored_types) &&
	!FRIBIDI_IS_ARABIC (ored_types) &&
	(!FRIBIDI_IS_RTL (fribidi_base_dir) ||
	  (FRIBIDI_IS_WEAK (fribidi_base_dir) &&
	   FRIBIDI_IS_LETTER (ored_types))
	))
      {
        /* all LTR */
	fribidi_base_dir = FRIBIDI_PAR_LTR;
	memset (embedding_levels_list, 0, n_chars);
	goto resolved;
      }
    /* The case that all resolved levels will be RTL is much more complex.
     * No isolates, no numbers, all strongs are RTL, and one of
     * the following:
     *
     * o base_dir has an RTL taste (may be weak).
     * o there are letters, and base_dir is weak.
     */
    else if (!FRIBIDI_IS_ISOLATE (ored_types) &&
	     !FRIBIDI_IS_NUMBER (ored_types) &&
	     FRIBIDI_IS_RTL (anded_strongs) &&
	     (FRIBIDI_IS_RTL (fribidi_base_dir) ||
	       (FRIBIDI_IS_WEAK (fribidi_base_dir) &&
		FRIBIDI_IS_LETTER (ored_types))
	     ))
      {
        /* all RTL */
	fribidi_base_dir = FRIBIDI_PAR_RTL;
	memset (embedding_levels_list, 1, n_chars);
	goto resolved;
      }


226
227
228
  max_level = fribidi_get_par_embedding_levels_ex (bidi_types, bracket_types, n_chars,
						   &fribidi_base_dir,
						   (FriBidiLevel*)embedding_levels_list);
229
230
231

  if (G_UNLIKELY(max_level == 0))
    {
232
      /* fribidi_get_par_embedding_levels() failed. */
233
234
      memset (embedding_levels_list, 0, length);
    }
235

236
237
resolved:
  g_free (bidi_types);
238
239
  g_free (bracket_types);

240
  *pbase_dir = (fribidi_base_dir == FRIBIDI_PAR_LTR) ?  PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
241
242
243
244
245
246
247
248

  return embedding_levels_list;
}

/**
 * pango_unichar_direction:
 * @ch: a Unicode character
 *
Matthias Clasen's avatar
Matthias Clasen committed
249
250
251
252
 * Determines the inherent direction of a character.
 *
 * The inherent direction is either %PANGO_DIRECTION_LTR, %PANGO_DIRECTION_RTL,
 * or %PANGO_DIRECTION_NEUTRAL.
253
254
 *
 * This function is useful to categorize characters into left-to-right
Matthias Clasen's avatar
Matthias Clasen committed
255
 * letters, right-to-left letters, and everything else. If full Unicode
256
 * bidirectional type of a character is needed,
Matthias Clasen's avatar
Matthias Clasen committed
257
 * [func@Pango.BidiType.for_unichar] can be used instead.
258
259
260
261
262
263
 *
 * Return value: the direction of the character.
 */
PangoDirection
pango_unichar_direction (gunichar ch)
{
264
265
266
267
268
  FriBidiCharType fribidi_ch_type;

  G_STATIC_ASSERT (sizeof (FriBidiChar) == sizeof (gunichar));

  fribidi_ch_type = fribidi_get_bidi_type (ch);
269
270
271
272
273
274
275
276
277

  if (!FRIBIDI_IS_STRONG (fribidi_ch_type))
    return PANGO_DIRECTION_NEUTRAL;
  else if (FRIBIDI_IS_RTL (fribidi_ch_type))
    return PANGO_DIRECTION_RTL;
  else
    return PANGO_DIRECTION_LTR;
}

278

279
280
281
282
283
/**
 * pango_get_mirror_char:
 * @ch: a Unicode character
 * @mirrored_ch: location to store the mirrored character
 *
Matthias Clasen's avatar
Matthias Clasen committed
284
285
286
 * Returns the mirrored character of a Unicode character.
 *
 * Mirror characters are determined by the Unicode mirrored property.
287
288
289
 *
 * Return value: %TRUE if @ch has a mirrored character and @mirrored_ch is
 * filled in, %FALSE otherwise
290
 *
Matthias Clasen's avatar
Matthias Clasen committed
291
 * Deprecated: 1.30: Use g_unichar_get_mirror_char() instead; the docs for that function
292
293
 * provide full details.
 */
294
gboolean
Matthias Clasen's avatar
Matthias Clasen committed
295
296
pango_get_mirror_char (gunichar  ch,
		       gunichar *mirrored_ch)
297
298
299
300
{
  return g_unichar_get_mirror_char (ch, mirrored_ch);
}