summaryrefslogtreecommitdiff
path: root/modules/arabic/arabic-x.c
blob: 05df83f92d143fb5b86639729f0fd8f65703a75d (plain)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* Pango - Arabic module 
 * arabic module
 *
 * (C) 2000 Karl Koehler<koehler@or.uni-bonn.de>
 *          Owen Taylor <otayler@redhat.com> 
 * 
 */

#include <stdio.h>
#include <glib.h>
#include "pango.h"
#include "pangox.h"
#include "utils.h"
#include <unicode.h>

#include "arconv.h" 
#include "mulefont.h"
#include "langboxfont.h"

/*  #define DEBUG    */
#ifdef DEBUG
#include <stdio.h>
#endif


static PangoEngineRange arabic_range[] = {
    { 0x060B, 0x06D3, "*" }  /* 0x060B, 0x06D3 */
};

static PangoEngineInfo script_engines[] = {
    {
        "ArabicScriptEngineLang",
        PANGO_ENGINE_TYPE_LANG,
        PANGO_RENDER_TYPE_NONE,
        arabic_range, G_N_ELEMENTS(arabic_range)
    },
    {
        "ArabicScriptEngineX",
        PANGO_ENGINE_TYPE_SHAPE,
        PANGO_RENDER_TYPE_X,
        arabic_range, G_N_ELEMENTS(arabic_range)
    }
};

static gint n_script_engines = G_N_ELEMENTS (script_engines);




/*
 * Language script engine
 */

static void 
arabic_engine_break (const char    *text,
                     int            len,
                     PangoAnalysis *analysis,
                     PangoLogAttr  *attrs)
{
    /* Most of the code comes from tamil_engine_break
     * only difference is char stop based on modifiers
     */

    const char *cur = text;
    const char *next;
    gint        i  = 0;
    GUChar4     wc;

    while (*cur)
        {
            if (!_pango_utf8_iterate (cur, &next, &wc))
                return;
            if (cur == next)
                break;
            if ((next - text) > len)
                break;
            cur = next;

            attrs[i].is_white = (wc == ' ' || wc == '\t' || wc == 'n') ? 1 : 0;
            attrs[i].is_break = (i > 0 && attrs[i-1].is_white) || attrs[i].is_white;
            attrs[i].is_char_stop = 1;
            attrs[i].is_word_stop = (i == 0) || attrs[i-1].is_white;
            /* actually, is_word_stop in not correct, but simple and good enough. */
            i++;
        }
}

static PangoEngine *
arabic_engine_lang_new ()
{
    PangoEngineLang *result;
  
    result = g_new (PangoEngineLang, 1);

    result->engine.id = "ArabicScriptEngine";
    result->engine.type = PANGO_ENGINE_TYPE_LANG;
    result->engine.length = sizeof (result);
    result->script_break = arabic_engine_break;

    return (PangoEngine *)result;
}

/*
 * X window system script engine portion
 */

static int
find_unic_font (PangoFont *font,char* charsets[],PangoXSubfont* rfonts)
{
    PangoXSubfont *subfonts;
    int           *subfont_charsets;
    int            n_subfonts;
    int            i;
    int            result = 0;

    n_subfonts = pango_x_list_subfonts (font, charsets, 3, 
                                        &subfonts, &subfont_charsets);
    
    for (i=0; i < n_subfonts; i++)
        {
            if  ( (strcmp (charsets[subfont_charsets[i]], "mulearabic-2") == 0)
                  && arabic_muleinit(font,rfonts) )
                {
                    result = 1; /* we know we have a mulearabic-font ... */
                    break;
                }
            else if ( (strcmp (charsets[subfont_charsets[i]], "iso8859-6.8x") == 0)
                      && arabic_lboxinit(font,rfonts) )
                {
                    result = 2;
                    break;
                } 
            else
                { /* test if the font has Alif-Madda; if so assume it is ok */
                    if ( pango_x_has_glyph /* Alif-Madda */
                         (font,PANGO_X_MAKE_GLYPH(subfonts[i],0xFE81)))
                        {
                            rfonts[0] = subfonts[i];
                            result   = 12;
                            break;
                        }
                    if ( pango_x_has_glyph /* Shadda+Kasra */
                         (font,PANGO_X_MAKE_GLYPH(subfonts[i],0xFC62)))
                        {
                            result = 13; /* extra vowels in font, hopefully */
                            break;
                        }
                    if ( pango_x_has_glyph /* Lam-Min alone */
                         (font,PANGO_X_MAKE_GLYPH(subfonts[i],0xFC42)))
                        {
                            result = 14; /* extra ligatures in font, hopefully */
                            break;
                        }
                }
        }

    g_free (subfonts);
    g_free (subfont_charsets);

    return result;
}

static char *default_charset[] = {
    "iso10646-1",  
    "iso8859-6.8x",
    /*          "iso8859-6.8x" */
    "mulearabic-2"    
};
  


static void
set_glyph (PangoGlyphString *glyphs, 
           PangoFont *font, PangoXSubfont subfont,
           int i, int cluster_start, int glyph, int is_vowel)
{
    PangoRectangle logical_rect;
#ifdef DEBUG
    if ( i < 0){
        fprintf(stderr,"[ar] setglyph: setting glyph %x at index %i, cluster %i\n",
                glyph,i,cluster_start);
        raise(6);
    } else {
        fprintf(stderr,"[ar] setglyph: setting glyph %x at index %i, "
                "cluster %i  ( subfont %x )\n",
                glyph,i,cluster_start,subfont);
    }
#endif

    glyphs->glyphs[i].glyph = PANGO_X_MAKE_GLYPH (subfont, glyph);
  
    glyphs->glyphs[i].geometry.x_offset = 0;
    glyphs->glyphs[i].geometry.y_offset = 0;

    pango_font_get_glyph_extents (font, glyphs->glyphs[i].glyph, NULL, &logical_rect);
    glyphs->log_clusters[i] = cluster_start;
    if (is_vowel)
        {
            glyphs->glyphs[i].geometry.width = 0;
        }
    else
        {
            glyphs->glyphs[i].geometry.width = logical_rect.width;
        }
}


/* The following thing is actually critical ... */

static void 
arabic_engine_shape (PangoFont        *font, 
                     const char       *text,
                     int               length,
                     PangoAnalysis    *analysis, 
                     PangoGlyphString *glyphs)
{
    PangoXSubfont subfont;
    PangoXSubfont arfonts[3];


    int         n_chars, n_glyph;
    int         i;
    const char *p;
    const char *pold;
    const char *next;
    GUChar4    *wc;
    int         lvl      = 1;

    g_return_if_fail (font != NULL);
    g_return_if_fail (text != NULL);
    g_return_if_fail (length >= 0);
    g_return_if_fail (analysis != NULL);

    /* We assume we have an unicode-font like 10x20 which containes
    **   the needed chars -- or tree mulearabic-coded fonts ...
    */
  
    n_chars = n_glyph = unicode_strlen (text, length);

    if (!(lvl = find_unic_font (font, default_charset,arfonts)))
        {

            PangoGlyph unknown_glyph = pango_x_get_unknown_glyph (font);
            
            pango_glyph_string_set_size (glyphs, n_chars);

            p = text;
            for (i=0; i<n_chars; i++)
                {
                    set_glyph (glyphs, font, 
                               PANGO_X_GLYPH_SUBFONT (unknown_glyph), i, 
                               p - text, PANGO_X_GLYPH_INDEX (unknown_glyph),0);
                    p = unicode_next_utf8 (p);
                }
            return;
        }
    subfont = arfonts[0];

    wc = (GUChar4 *)g_malloc(sizeof(GUChar4)*n_chars);  

    p = text;
    for (i=0; i < n_chars; i++)
        {
            _pango_utf8_iterate (p, &next, &wc[n_chars - i - 1]);
            p = next;
        }
  
  
    if (analysis->level % 2 == 0)
        {
            /* We were called on a LTR directional run (e.g. some numbers); 
               fallback as simple as possible */
            pango_glyph_string_set_size (glyphs, n_chars);

        }
    else 
        {
            arabic_reshape(&n_glyph,wc,lvl);
            pango_glyph_string_set_size (glyphs, n_glyph);
        };


    p    = text;
    pold = p;
    i    = n_chars-1;
#ifdef DEBUG
    fprintf(stderr,"[ar]: after shaping : %i glyphs ",n_glyph);
#endif
    while(i >= 0)
        {
            if (wc[i] == 0)
                {
                    p = unicode_next_utf8 (p);
                    i--;
                }
            else 
                {   
                    int cluster_start ;
                    int is_vowel      = arabic_isvowel(wc[i]);
                    cluster_start     = is_vowel ? pold - text : p - text;

                    if ( lvl ==  1 )
                        {
#ifdef DEBUG
                            fprintf(stderr,"[ar] mule-recoding char %x",
                                    wc[i]);
#endif
                            arabic_mule_recode(&subfont,&(wc[i]),arfonts);
                        }
                    else if ( lvl == 2 )
                        {
                            if (( i > 0 )&&(wc[i-1] == 0))
                                {
                                    arabic_lbox_recode(&subfont,&(wc[i]),
                                                       &(wc[i-1]), arfonts);
                                }
                            else 
                                arabic_lbox_recode(&subfont,&(wc[i]),NULL,
                                                   arfonts);
                        }
                    
                    set_glyph(glyphs, font, subfont, n_glyph - 1,
                              cluster_start, wc[i], is_vowel);
      
                    pold = p;
                    p    = unicode_next_utf8 (p);
                    n_glyph--;
                    i--;
                }
        }

    g_free(wc);
}


static PangoCoverage *
arabic_engine_get_coverage (PangoFont  *font,
                            const char *lang)
{
    GUChar4 i;
    PangoCoverage *result = pango_coverage_new ();

    for (i = 0x60B; i <= 0x66D; i++)
        pango_coverage_set (result, i, PANGO_COVERAGE_EXACT);
    for (i = 0x670; i <= 0x6D3; i++)
        pango_coverage_set (result, i, PANGO_COVERAGE_EXACT);

    return result;
}

static PangoEngine *
arabic_engine_x_new ()
{
    PangoEngineShape *result;
  
    result = g_new (PangoEngineShape, 1);

    result->engine.id = "ArabicScriptEngine";
    result->engine.type = PANGO_ENGINE_TYPE_LANG;
    result->engine.length = sizeof (result);
    result->script_shape = arabic_engine_shape;
    result->get_coverage = arabic_engine_get_coverage;

    return (PangoEngine *)result;
}





/* The following three functions provide the public module API for
 * Pango
 */
#ifdef MODULE_PREFIX
#define MODULE_ENTRY(func) _pango_arabic_##func
#else
#define MODULE_ENTRY(func) func
#endif

void 
MODULE_ENTRY(script_engine_list) (PangoEngineInfo **engines, int *n_engines)
{
    *engines   = script_engines;
    *n_engines = n_script_engines;
}

PangoEngine *
MODULE_ENTRY(script_engine_load) (const char *id)
{
    if (!strcmp (id, "ArabicScriptEngineLang"))
        return arabic_engine_lang_new ();
    else if (!strcmp (id, "ArabicScriptEngineX"))
        return arabic_engine_x_new ();
    else
        return NULL;
}

void 
MODULE_ENTRY(script_engine_unload) (PangoEngine *engine)
{
}