File: unicode_join.c

package info (click to toggle)
qemacs 0.3.1-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,040 kB
  • ctags: 3,226
  • sloc: ansic: 29,371; sh: 353; makefile: 338
file content (334 lines) | stat: -rw-r--r-- 8,917 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Unicode joining algorithms for QEmacs.  
 *
 * Copyright (c) 2000 Fabrice Bellard.
 *
 * This library 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 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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
 */
#include "qfribidi.h"
#include "qe.h"

#ifdef CONFIG_UNICODE_JOIN

/* ligature tables */
static unsigned short *subst1;
static unsigned short *ligature2;
static unsigned short *ligature_long;

static int subst1_count = 0;
static int ligature2_count = 0;

/* XXX: eof ? */
static int uni_get_be16(FILE *f)
{
    int v;
    v = fgetc(f) << 8;
    v |= fgetc(f);
    return v;
}

static unsigned short *read_array_be16(FILE *f, int n)
{
    unsigned short *tab;
    int i;

    tab = malloc(n * sizeof(unsigned short));
    if (!tab) 
        return NULL;
    for(i=0;i<n;i++) {
        tab[i] = uni_get_be16(f);
    }
    return tab;
}

void load_ligatures(void)
{
    FILE *f;
    unsigned char buf[1024];
    int long_count;

    if (find_resource_file(buf, sizeof(buf), "ligatures") < 0)
        return;

    f = fopen(buf, "r");
    if (!f)
        return;
    if (fread(buf, 1, 4, f) != 4 ||
        memcmp(buf, "liga", 4) != 0) 
        goto fail;

    subst1_count = uni_get_be16(f);
    ligature2_count = uni_get_be16(f);
    long_count = uni_get_be16(f);

    subst1 = read_array_be16(f, subst1_count * 2);
    if (!subst1) 
        goto fail;
    ligature2 = read_array_be16(f, ligature2_count * 3);
    if (!ligature2) 
        goto fail;
    ligature_long = read_array_be16(f, long_count);
    if (!ligature_long)
        goto fail;
    fclose(f);
    return;
 fail:
    free(subst1);
    free(ligature2);
    free(ligature_long);
    subst1_count = 0;
    ligature2_count = 0;
    fclose(f);
}

static int find_ligature(int l1, int l2)
{
    int a, b, m, v1, v2;

    a = 0;
    b = ligature2_count - 1;
    while (a <= b) {
        m = (a + b) >> 1;
        v1 = ligature2[3 * m];
        v2 = ligature2[3 * m + 1];
        if (v1 == l1 && v2 == l2)
            return ligature2[3 * m + 2];
        else if (v1 > l1 || (v1 == l1 && v2 > l2)) {
            b = m - 1;
        } else {
            a = m + 1;
        }
    }
    return -1;
}

/* apply all the ligature rules in logical order. Always return a
   smaller buffer */
static int unicode_ligature(unsigned int *buf_out, 
                            unsigned int *pos_L_to_V,
                            int len)
{
    int l, l1, l2, len1, len2, i, j;
    unsigned int *q;
    const unsigned short *lig;
    unsigned int buf[len];
    
    memcpy(buf, buf_out, len * sizeof(int));

    q = buf_out;
    for(i=0;i<len;) {
        l1 = buf[i];
        /* eliminate invisible chars */
        if (l1 >= 0x202a && l1 <= 0x202e) {
            /* LRE, RLE, PDF, RLO, LRO */
            pos_L_to_V[i] = q - buf_out;
            i++;
            goto found;
        }
        /* fast test to eliminate common cases */
        if (i == (len - 1))
            goto nolig;
        l2 = buf[i + 1];
        if (l1 <= 0x7f && l2 <= 0x7f)
            goto nolig;
        l = find_ligature(l1, l2);
        if (l < 0)
            goto nolig;
        if (l > 0) {
            /* ligature of length 2 found */
            pos_L_to_V[i] = q - buf_out;
            pos_L_to_V[i+1] = q - buf_out;
            *q++ = l;
            i += 2;
        } else {
            /* generic case : use ligature_long[] table */
            lig = ligature_long;
            for(;;) {
                len1 = *lig++;
                if (len1 == 0)
                    break;
                len2 = *lig++;
                if (i + len1 <= len) {
                    for(j=0;j<len1;j++) {
                        if (buf[i+j] != lig[j])
                            goto notfound;
                    }
                    for(j=0;j<len1;j++)
                        pos_L_to_V[i + j] = q - buf_out;
                    for(j=0;j<len2;j++) {
                        *q++ = lig[len1 + j];
                    }
                    i += len1;
                    goto found;
                }
            notfound:
                lig += len1 + len2;
            }
            /* nothing found */
        nolig:
            pos_L_to_V[i] = q - buf_out;
            *q++ = l1;
            i++;
        found:
	    continue;
        }
    }
    return q - buf_out;
}

/* fast classification of unicode chars to optimize the algorithms */
#define UNICODE_ARABIC   0x00000001
#define UNICODE_INDIC    0x00000002
#define UNICODE_NONASCII 0x00000004

static int unicode_classify(unsigned int *buf, int len)
{
    int i, mask, c;

    mask = 0;
    for(i=0;i<len;i++) {
        c = buf[i];
        if (c <= 0x7f) /* latin1 fast handling */
            continue;
        mask |= UNICODE_NONASCII;
        if (c >= 0x2000) /* fast test for non handled scripts */
            continue; 
        if (c >= 0x600 && c <= 0x6ff) 
            mask |= UNICODE_ARABIC;
        else if (c >= 0x900 && c <= 0x97f)
            mask |= UNICODE_INDIC;
    }
    return mask;
}

static void compose_char_to_glyph(unsigned int *ctog, int len, unsigned *ctog1)
{
    int i;
    for(i=0;i<len;i++)
        ctog[i] = ctog1[ctog[i]];
}

static void bidi_reverse_buf(unsigned int *str, int len)
{
    int i, len2 = len / 2;
    
    for (i = 0; i < len2; i++) {
	unsigned int tmp = str[i];
	str[i] = fribidi_get_mirror_char(str[len - 1 - i]);
	str[len - 1 - i] = fribidi_get_mirror_char(tmp);
    }
    /* do not forget central char ! */
    if (len & 1) {
        str[len2] = fribidi_get_mirror_char(str[len2]);
    }
}

/* Convert a string of unicode characters to a string of glyphs. We
   suppose that the font implements a minimum number of standard
   ligatures chars. The string is reversed if 'reversed' is set to
   deal with the bidir case. 'char_to_glyph_pos' gives the index of
   the first glyph associated to a given character of the source
   buffer. */
int unicode_to_glyphs(unsigned int *dst, unsigned int *char_to_glyph_pos,
                      int dst_size, unsigned int *src, int src_size, int reverse)
{
    int len, i;
    unsigned int ctog[src_size];
    unsigned int ctog1[src_size];
    unsigned int buf[src_size];
    int unicode_class;

    unicode_class = unicode_classify(src, src_size);
    if (unicode_class == 0 && !reverse) {
        /* fast case : no special treatment */
        len = src_size;
        if (len > dst_size)
            len = dst_size;
        memcpy(dst, src, len * sizeof(unsigned int));
        if (char_to_glyph_pos) {
            for(i=0;i<len;i++)
                char_to_glyph_pos[i] = i;
        }
        return len;
    } else {
        /* generic case */

        /* init current buffer */
        len = src_size;
        for(i=0;i<len;i++)
            ctog[i] = i;
        memcpy(buf, src, len * sizeof(int));
        
        /* apply each filter */
    
        if (unicode_class & UNICODE_ARABIC) {
            len = arab_join(buf, ctog1, len);
            /* not needed for arabjoin */
            //compose_char_to_glyph(ctog, src_size, ctog1);
        }

        if (unicode_class & UNICODE_INDIC) {
            len = devanagari_log2vis(buf, ctog1, len);
            compose_char_to_glyph(ctog, src_size, ctog1);
        }
        
        len = unicode_ligature(buf, ctog1, len);
        compose_char_to_glyph(ctog, src_size, ctog1);

        if (reverse) {
            bidi_reverse_buf(buf, len);
            for(i=0;i<src_size;i++) {
                ctog[i] = len - 1 - ctog[i];
            }
        }

        if (len > dst_size)
            len = dst_size;
        memcpy(dst, buf, len * sizeof(unsigned int));
        
        if (char_to_glyph_pos) {
            memcpy(char_to_glyph_pos, ctog, src_size * sizeof(unsigned int));
        }
    }
    return len;
}

#else /* CONFIG_UNICODE_JOIN */


/* fallback unicode functions */

void load_ligatures(void)
{
}

int unicode_to_glyphs(unsigned int *dst, unsigned int *char_to_glyph_pos,
                      int dst_size, unsigned int *src, int src_size, int reverse)
{
    int len, i;

    len = src_size;
    if (len > dst_size)
        len = dst_size;
    memcpy(dst, src, len * sizeof(unsigned int));
    if (char_to_glyph_pos) {
        for(i=0;i<len;i++)
            char_to_glyph_pos[i] = i;
    }
    return len;
}


#endif /* CONFIG_UNICODE_JOIN */