File: fribidi-arabic.c

package info (click to toggle)
fribidi 1.0.5-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 19,752 kB
  • sloc: ansic: 8,701; sh: 4,573; makefile: 350; python: 27
file content (191 lines) | stat: -rw-r--r-- 4,873 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* fribidi-arabic.c - Arabic shaping
 *
 * Copyright (C) 2005  Behdad Esfahbod
 *
 * This file is part of GNU FriBidi.
 * 
 * GNU FriBidi is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 * 
 * GNU FriBidi 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GNU FriBidi; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * For licensing issues, contact <fribidi.license@gmail.com> or write to
 * Sharif FarsiWeb, Inc., PO Box 13445-389, Tehran, Iran.
 *
 * Author(s):
 *   Behdad Esfahbod, 2005
 */

#include "common.h"

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif


#include <fribidi-arabic.h>
#include <fribidi-unicode.h>


typedef struct _PairMap {
  FriBidiChar pair[2], to;
} PairMap;


#define FRIBIDI_ACCESS_SHAPE_TABLE(table,min,max,x,shape) (table), (min), (max)
# define FRIBIDI_ACCESS_SHAPE_TABLE_REAL(table,min,max,x,shape) \
	(((x)<(min)||(x)>(max))?(x):(table)[(x)-(min)][(shape)])

#include "arabic-shaping.tab.i"
#include "arabic-misc.tab.i"


static void
fribidi_shape_arabic_joining (
  /* input */
  const FriBidiChar table[][4],
  FriBidiChar min,
  FriBidiChar max,
  const FriBidiStrIndex len,
  const FriBidiArabicProp *ar_props,
  /* input and output */
  FriBidiChar *str
)
{
  register FriBidiStrIndex i;

  for (i = 0; i < len; i++)
    if (FRIBIDI_ARAB_SHAPES(ar_props[i]))
      str[i] = FRIBIDI_ACCESS_SHAPE_TABLE_REAL (table, min, max, str[i], FRIBIDI_JOIN_SHAPE (ar_props[i]));
}



static int
comp_PairMap (const void *pa, const void *pb)
{
  PairMap *a = (PairMap *)pa;
  PairMap *b = (PairMap *)pb;

  if (a->pair[0] != b->pair[0])
    return a->pair[0] < b->pair[0] ? -1 : +1;
  else
    return a->pair[1] < b->pair[1] ? -1 :
           a->pair[1] > b->pair[1] ? +1 :
	   0;
}


static FriBidiChar
find_pair_match (const PairMap *table, int size, FriBidiChar first, FriBidiChar second)
{
  PairMap *match;
  PairMap x;
  x.pair[0] = first;
  x.pair[1] = second;
  x.to = 0;
  match = bsearch (&x, table, size, sizeof (table[0]), comp_PairMap);
  return match ? match->to : 0;
}

#define PAIR_MATCH(table,len,first,second) \
	((first)<(table[0].pair[0])||(first)>(table[len-1].pair[0])?0: \
	 find_pair_match(table, len, first, second))

static void
fribidi_shape_arabic_ligature (
  /* input */
  const PairMap *table,
  int size,
  const FriBidiLevel *embedding_levels,
  const FriBidiStrIndex len,
  /* input and output */
  FriBidiArabicProp *ar_props,
  FriBidiChar *str
)
{
  /* TODO: This doesn't form ligatures for even-level Arabic text.
   * no big problem though. */
  register FriBidiStrIndex i;

  for (i = 0; i < len - 1; i++) {
    register FriBidiChar c;
    if (FRIBIDI_LEVEL_IS_RTL(embedding_levels[i]) &&
	embedding_levels[i] == embedding_levels[i+1] &&
	(c = PAIR_MATCH(table, size, str[i], str[i+1])))
      {
	str[i] = FRIBIDI_CHAR_FILL;
	FRIBIDI_SET_BITS(ar_props[i], FRIBIDI_MASK_LIGATURED);
	str[i+1] = c;
      }
  }
}

#define DO_LIGATURING(table, levels, len, ar_props, str) \
	fribidi_shape_arabic_ligature ((table), sizeof(table)/sizeof((table)[0]), levels, len, ar_props, str)

#define DO_SHAPING(tablemacro, len, ar_props, str) \
	fribidi_shape_arabic_joining (tablemacro(,), len, ar_props, str);
	



FRIBIDI_ENTRY void
fribidi_shape_arabic (
  /* input */
  FriBidiFlags flags,
  const FriBidiLevel *embedding_levels,
  const FriBidiStrIndex len,
  /* input and output */
  FriBidiArabicProp *ar_props,
  FriBidiChar *str
)
{
  DBG ("in fribidi_shape_arabic");

  if UNLIKELY
    (len == 0 || !str) return;

  DBG ("in fribidi_shape");

  fribidi_assert (ar_props);

  if (FRIBIDI_TEST_BITS (flags, FRIBIDI_FLAG_SHAPE_ARAB_PRES))
    {
      DO_SHAPING (FRIBIDI_GET_ARABIC_SHAPE_PRES, len, ar_props, str);
    }

  if (FRIBIDI_TEST_BITS (flags, FRIBIDI_FLAG_SHAPE_ARAB_LIGA))
    {
      DO_LIGATURING (mandatory_liga_table, embedding_levels, len, ar_props, str);
    }

  if (FRIBIDI_TEST_BITS (flags, FRIBIDI_FLAG_SHAPE_ARAB_CONSOLE))
    {
      DO_LIGATURING (console_liga_table, embedding_levels, len, ar_props, str);
      DO_SHAPING (FRIBIDI_GET_ARABIC_SHAPE_NSM, len, ar_props, str);
    }
}

/* Editor directions:
 * Local Variables:
 *   mode: c
 *   c-basic-offset: 2
 *   indent-tabs-mode: t
 *   tab-width: 8
 * End:
 * vim: textwidth=78: autoindent: cindent: shiftwidth=2: tabstop=8:
 */