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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
|
/* Pango
* basic.c:
*
* Copyright (C) 1999 Red Hat Software
*
* 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.
*/
#include <glib.h>
#include <string.h>
#include "pangox.h"
#include "pango-engine.h"
#include "pango-utils.h"
#include "basic-common.h"
typedef struct _CharRange CharRange;
typedef struct _Charset Charset;
typedef struct _CharsetOrdering CharsetOrdering;
typedef struct _CharCache CharCache;
typedef struct _CharCachePointer CharCachePointer;
typedef struct _MaskTable MaskTable;
typedef PangoGlyph (*ConvFunc) (CharCache *cache,
GIConv cd,
const gchar *input);
#define MAX_CHARSETS 32
#define SCRIPT_ENGINE_NAME "BasicScriptEngineX"
struct _Charset
{
int index;
char *id;
char *x_charset;
ConvFunc conv_func;
};
struct _CharsetOrdering
{
const char *langs;
char charsets[MAX_CHARSETS];
};
struct _CharRange
{
guint16 start;
guint16 end;
guint16 charsets;
};
struct _MaskTable
{
int n_subfonts;
PangoXSubfont *subfonts;
Charset **charsets;
};
struct _CharCache
{
guint ref_count;
CharsetOrdering *ordering;
MaskTable *mask_tables[256];
GIConv converters[MAX_CHARSETS];
};
struct _CharCachePointer
{
PangoLanguage *lang;
CharCache *cache;
};
static PangoGlyph conv_8bit (CharCache *cache,
GIConv cd,
const char *input);
static PangoGlyph conv_eucjp (CharCache *cache,
GIConv cd,
const char *input);
static PangoGlyph conv_16bit (CharCache *cache,
GIConv cd,
const char *input);
static PangoGlyph conv_ucs4 (CharCache *cache,
GIConv cd,
const char *input);
static PangoGlyph conv_16bit_MSB_on (CharCache *cache,
GIConv cd,
const char *input);
static PangoGlyph conv_gb18030_1 (CharCache *cache,
GIConv cd,
const char *input);
static PangoGlyph conv_euctw (CharCache *cache,
GIConv cd,
const char *input);
#include "tables-big.i"
static PangoEngineRange basic_ranges[] = {
/* Language characters */
{ 0x0000, 0x02af, "*" },
{ 0x02b0, 0x02ff, "" },
{ 0x0380, 0x058f, "*" },
{ 0x0591, 0x05f4, "" }, /* Hebrew */
{ 0x060c, 0x06f9, "" }, /* Arabic */
{ 0x0e01, 0x0e5b, "" }, /* Thai */
{ 0x10a0, 0x10ff, "*" }, /* Georgian */
{ 0x1200, 0x16ff, "*" }, /* Ethiopic,Cherokee,Canadian,Ogham,Runic */
{ 0x1e00, 0x1fff, "*" },
{ 0x2000, 0x33ff, "*" },
{ 0x3400, 0x9fa5, "*" }, /* Unihan */
{ 0xa000, 0xa4c6, "*" }, /* Yi */
{ 0xac00, 0xd7a3, "" },
{ 0xe000, 0xf7ee, "*" }, /* HKSCS-1999 */
{ 0xf900, 0xfa2d, "*" }, /* CJK Compatibility Ideographs */
{ 0xfe30, 0xfe6b, "*" }, /* CJK Compatibility Forms and Small Form Variants */
{ 0xff00, 0xffee, "*" } /* Halfwidth and Fullwidth Forms */
};
static PangoEngineInfo script_engines[] = {
{
SCRIPT_ENGINE_NAME,
PANGO_ENGINE_TYPE_SHAPE,
PANGO_RENDER_TYPE_X,
basic_ranges, G_N_ELEMENTS(basic_ranges)
}
};
static gint n_script_engines = G_N_ELEMENTS (script_engines);
/*
* X window system script engine portion
*/
/* Structure of our cache:
*
* PangoFont => CharCachePointer ===\
* | \
* CharCachePointer ======> CharCache => CharsetOrdering
* | |======> MaskTable[0] => {subfonts,charset}[n_subfonts],
* | |======> MaskTable[1] => {subfonts,charset}[n_subfonts],
* | \======> MaskTable[...] => {subfonts,charset}[n_subfonts]
* |
* CharCachePointer ======> CharCache => CharsetOrdering
* |======> MaskTable[0] => {subfonts,charset}[n_subfonts],
* |======> MaskTable[1] => {subfonts,charset}[n_subfonts],
* \======> MaskTable[...] => {subfonts,charset}[n_subfonts]
*
* A CharCache structure caches the lookup of what subfonts can be used for what characters for a pair of a Font
* and CharsetOrdering. Multiple language tags can share the same CharsetOrdering - the list of CharCachePointer
* structures that is attached to the font as object data provides lookups from language tag to charcache.
*/
static CharCache *
char_cache_new (CharsetOrdering *ordering)
{
CharCache *result;
int i;
result = g_new0 (CharCache, 1);
result->ref_count = 1;
result->ordering = ordering;
for (i=0; i<MAX_CHARSETS; i++)
result->converters[i] = (GIConv)-1;
return result;
}
static void
char_cache_free (CharCache *cache)
{
int i;
for (i=0; i<256; i++)
if (cache->mask_tables[i])
{
g_free (cache->mask_tables[i]->subfonts);
g_free (cache->mask_tables[i]->charsets);
g_free (cache->mask_tables[i]);
}
for (i=0; i<MAX_CHARSETS; i++)
if (cache->converters[i] != (GIConv)-1)
g_iconv_close (cache->converters[i]);
g_free (cache);
}
static PangoGlyph
find_char (CharCache *cache, PangoFont *font, gunichar wc, const char *input)
{
int mask_index;
MaskTable *mask_table;
int i;
switch (wc)
{
case '\n':
case '\r':
case 0x2028: /* Line separator */
case 0x2029: /* Paragraph separator */
return pango_x_font_get_unknown_glyph (font, wc);
break;
}
if (wc >= G_N_ELEMENTS (char_masks))
mask_index = 0;
else
mask_index = char_masks[wc];
if (cache->mask_tables[mask_index])
mask_table = cache->mask_tables[mask_index];
else
{
char *charset_names[G_N_ELEMENTS(charsets)];
Charset *charsets_map[G_N_ELEMENTS(charsets)];
guint mask;
int n_charsets = 0;
int *subfont_charsets;
mask_table = g_new (MaskTable, 1);
mask = char_mask_map[mask_index] | ENC_ISO_10646;
/* Find the character sets that are included in this mask
*/
for (i=0; i<G_N_ELEMENTS(charsets); i++)
{
int charset_index = cache->ordering->charsets[i];
if (mask & (1 << charset_index))
{
charset_names[n_charsets] = charsets[charset_index].x_charset;
charsets_map[n_charsets] = &charsets[charset_index];
n_charsets++;
}
}
mask_table->n_subfonts = pango_x_list_subfonts (font, charset_names, n_charsets, &mask_table->subfonts, &subfont_charsets);
mask_table->charsets = g_new (Charset *, mask_table->n_subfonts);
for (i=0; i<mask_table->n_subfonts; i++)
mask_table->charsets[i] = charsets_map[subfont_charsets[i]];
g_free (subfont_charsets);
cache->mask_tables[mask_index] = mask_table;
}
for (i=0; i < mask_table->n_subfonts; i++)
{
PangoGlyph index;
PangoGlyph glyph;
Charset *charset;
charset = mask_table->charsets[i];
if (charset)
{
GIConv cd = cache->converters[charset->index];
if (charset->id && cd == (GIConv)-1)
{
cd = g_iconv_open (charset->id, "UTF-8");
if (cd == (GIConv)-1)
{
g_warning ("Could not load converter from %s to UTF-8", charset->id);
mask_table->charsets[i] = NULL;
continue;
}
cache->converters[charset->index] = cd;
}
index = (*charset->conv_func) (cache, cd, input);
glyph = PANGO_X_MAKE_GLYPH (mask_table->subfonts[i], index);
if (pango_x_has_glyph (font, glyph))
return glyph;
}
}
return 0;
}
static void
set_glyph (PangoFont *font, PangoGlyphString *glyphs, int i, int offset, PangoGlyph glyph)
{
PangoRectangle logical_rect;
glyphs->glyphs[i].glyph = glyph;
glyphs->glyphs[i].geometry.x_offset = 0;
glyphs->glyphs[i].geometry.y_offset = 0;
glyphs->log_clusters[i] = offset;
pango_font_get_glyph_extents (font, glyphs->glyphs[i].glyph, NULL, &logical_rect);
glyphs->glyphs[i].geometry.width = logical_rect.width;
}
static PangoGlyph
conv_8bit (CharCache *cache,
GIConv cd,
const char *input)
{
char outbuf;
const char *inptr = input;
size_t inbytesleft;
char *outptr = &outbuf;
size_t outbytesleft = 1;
inbytesleft = g_utf8_next_char (input) - input;
g_iconv (cd, (char **)&inptr, &inbytesleft, &outptr, &outbytesleft);
return (guchar)outbuf;
}
static PangoGlyph
conv_eucjp (CharCache *cache,
GIConv cd,
const char *input)
{
char outbuf[4];
const char *inptr = input;
size_t inbytesleft;
char *outptr = outbuf;
size_t outbytesleft = 4;
inbytesleft = g_utf8_next_char (input) - input;
g_iconv (cd, (char **)&inptr, &inbytesleft, &outptr, &outbytesleft);
if ((guchar)outbuf[0] < 128)
return outbuf[0];
else if ((guchar)outbuf[0] == 0x8e && outbytesleft == 2)
return ((guchar)outbuf[1]);
else if ((guchar)outbuf[0] == 0x8f && outbytesleft == 1)
return ((guchar)outbuf[1] & 0x7f) * 256 + ((guchar)outbuf[2] & 0x7f);
else
return ((guchar)outbuf[0] & 0x7f) * 256 + ((guchar)outbuf[1] & 0x7f);
}
static PangoGlyph
conv_16bit (CharCache *cache,
GIConv cd,
const char *input)
{
char outbuf[2];
const char *inptr = input;
size_t inbytesleft;
char *outptr = outbuf;
size_t outbytesleft = 2;
inbytesleft = g_utf8_next_char (input) - input;
g_iconv (cd, (char **)&inptr, &inbytesleft, &outptr, &outbytesleft);
if ((guchar)outbuf[0] < 128)
return outbuf[0];
else
return ((guchar)outbuf[0] & 0x7f) * 256 + ((guchar)outbuf[1] & 0x7f);
}
static PangoGlyph
conv_16bit_MSB_on (CharCache *cache,
GIConv cd,
const char *input)
{
char outbuf[2];
const char *inptr = input;
size_t inbytesleft;
char *outptr = outbuf;
size_t outbytesleft = 2;
inbytesleft = g_utf8_next_char (input) - input;
g_iconv (cd, (char **)&inptr, &inbytesleft, &outptr, &outbytesleft);
if ((guchar)outbuf[0] < 128)
return outbuf[0];
else
return (guchar)outbuf[0] * 256 + (guchar)outbuf[1];
}
static PangoGlyph
conv_gb18030_1 (CharCache *cache,
GIConv cd,
const char *input)
{
char outbuf[4];
const char *inptr = input;
size_t inbytesleft;
char *outptr = outbuf;
size_t outbytesleft = 4;
inbytesleft = g_utf8_next_char (input) - input;
g_iconv (cd, (char **)&inptr, &inbytesleft, &outptr, &outbytesleft);
if ((guchar)outbuf[0] < 128)
return outbuf[0];
else
return 12600 * ((guchar)outbuf[0] - 0x81) + 1260 * ((guchar)outbuf[1] - 0x30) + 10 * ((guchar)outbuf[2] - 0x81) + ((guchar)outbuf[3] - 0x30);
}
static PangoGlyph
conv_euctw (CharCache *cache,
GIConv cd,
const char *input)
{
char outbuf[4];
const char *inptr = input;
size_t inbytesleft;
char *outptr = outbuf;
size_t outbytesleft = 4;
inbytesleft = g_utf8_next_char (input) - input;
g_iconv (cd, (char **)&inptr, &inbytesleft, &outptr, &outbytesleft);
/* The first two bytes determine which page of CNS to use; we
* get this information from tables-big.i, so ignore them
*/
if ((guchar)outbuf[0] < 128)
return outbuf[0];
else
return ((guchar)outbuf[2] & 0x7f) * 256 + ((guchar)outbuf[3] & 0x7f);
}
static PangoGlyph
conv_ucs4 (CharCache *cache,
GIConv cd,
const char *input)
{
return g_utf8_get_char (input);
}
static void
swap_range (PangoGlyphString *glyphs, int start, int end)
{
int i, j;
for (i = start, j = end - 1; i < j; i++, j--)
{
PangoGlyphInfo glyph_info;
gint log_cluster;
glyph_info = glyphs->glyphs[i];
glyphs->glyphs[i] = glyphs->glyphs[j];
glyphs->glyphs[j] = glyph_info;
log_cluster = glyphs->log_clusters[i];
glyphs->log_clusters[i] = glyphs->log_clusters[j];
glyphs->log_clusters[j] = log_cluster;
}
}
static void
char_caches_free (GSList *caches)
{
GSList *tmp_list = caches;
while (tmp_list)
{
CharCachePointer *pointer = tmp_list->data;
pointer->cache->ref_count--;
if (pointer->cache->ref_count == 0)
char_cache_free (pointer->cache);
g_free (pointer);
tmp_list = tmp_list->next;
}
g_slist_free (caches);
}
static CharsetOrdering *
ordering_for_lang (PangoLanguage *lang)
{
int i;
for (i = 0; i < G_N_ELEMENTS (charset_orderings) - 1; i++)
{
if (pango_language_matches (lang, charset_orderings[i].langs))
return &charset_orderings[i];
}
return &charset_orderings[i];
}
static CharCache *
get_char_cache (PangoFont *font,
PangoLanguage *lang)
{
GQuark cache_id = g_quark_from_string ("basic-char-cache");
CharCache *cache = NULL;
CharCachePointer *pointer;
CharsetOrdering *ordering;
GSList *caches;
GSList *tmp_list;
caches = g_object_get_qdata (G_OBJECT (font), cache_id);
tmp_list = caches;
while (tmp_list)
{
pointer = tmp_list->data;
if (pointer->lang == lang)
return pointer->cache;
tmp_list = tmp_list->next;
}
ordering = ordering_for_lang (lang);
tmp_list = caches;
while (tmp_list)
{
pointer = tmp_list->data;
if (pointer->cache->ordering == ordering)
{
cache = pointer->cache;
break;
}
tmp_list = tmp_list->next;
}
if (!cache)
cache = char_cache_new (ordering);
else
cache->ref_count++;
pointer = g_new (CharCachePointer, 1);
pointer->lang = lang;
pointer->cache = cache;
caches = g_slist_prepend (caches, pointer);
g_object_steal_qdata (G_OBJECT (font), cache_id);
g_object_set_qdata_full (G_OBJECT (font), cache_id,
caches, (GDestroyNotify)char_caches_free);
return cache;
}
static void
basic_engine_shape (PangoFont *font,
const char *text,
gint length,
PangoAnalysis *analysis,
PangoGlyphString *glyphs)
{
int n_chars;
int i;
const char *p;
CharCache *cache;
g_return_if_fail (font != NULL);
g_return_if_fail (text != NULL);
g_return_if_fail (length >= 0);
g_return_if_fail (analysis != NULL);
cache = get_char_cache (font, analysis->language);
n_chars = g_utf8_strlen (text, length);
pango_glyph_string_set_size (glyphs, n_chars);
p = text;
for (i=0; i < n_chars; i++)
{
gunichar wc;
gunichar mirrored_ch;
PangoGlyph index;
char buf[6];
const char *input;
wc = g_utf8_get_char (p);
input = p;
if (analysis->level % 2)
if (pango_get_mirror_char (wc, &mirrored_ch))
{
wc = mirrored_ch;
g_unichar_to_utf8 (wc, buf);
input = buf;
}
if (wc == 0xa0) /* non-break-space */
{
wc = 0x20;
g_unichar_to_utf8 (wc, buf);
input = buf;
}
if (ZERO_WIDTH_CHAR (wc))
{
set_glyph (font, glyphs, i, p - text, 0);
}
else
{
index = find_char (cache, font, wc, input);
if (index)
{
set_glyph (font, glyphs, i, p - text, index);
if (g_unichar_type (wc) == G_UNICODE_NON_SPACING_MARK)
{
if (i > 0)
{
PangoRectangle logical_rect, ink_rect;
glyphs->glyphs[i].geometry.width = MAX (glyphs->glyphs[i-1].geometry.width,
glyphs->glyphs[i].geometry.width);
glyphs->glyphs[i-1].geometry.width = 0;
glyphs->log_clusters[i] = glyphs->log_clusters[i-1];
/* Some heuristics to try to guess how overstrike glyphs are
* done and compensate
*/
pango_font_get_glyph_extents (font, glyphs->glyphs[i].glyph, &ink_rect, &logical_rect);
if (logical_rect.width == 0 && ink_rect.x == 0)
glyphs->glyphs[i].geometry.x_offset = (glyphs->glyphs[i].geometry.width - ink_rect.width) / 2;
}
}
}
else
set_glyph (font, glyphs, i, p - text, pango_x_get_unknown_glyph (font));
}
p = g_utf8_next_char (p);
}
/* Simple bidi support... may have separate modules later */
if (analysis->level % 2)
{
int start, end;
/* Swap all glyphs */
swap_range (glyphs, 0, n_chars);
/* Now reorder glyphs within each cluster back to LTR */
for (start=0; start<n_chars;)
{
end = start;
while (end < n_chars &&
glyphs->log_clusters[end] == glyphs->log_clusters[start])
end++;
swap_range (glyphs, start, end);
start = end;
}
}
}
static PangoCoverage *
basic_engine_get_coverage (PangoFont *font,
PangoLanguage *lang)
{
CharCache *cache = get_char_cache (font, lang);
PangoCoverage *result = pango_coverage_new ();
gunichar wc;
for (wc = 0; wc < 65536; wc++)
{
char buf[6];
g_unichar_to_utf8 (wc, buf);
if (find_char (cache, font, wc, buf))
pango_coverage_set (result, wc, PANGO_COVERAGE_EXACT);
}
return result;
}
static PangoEngine *
basic_engine_x_new ()
{
PangoEngineShape *result;
result = g_new (PangoEngineShape, 1);
result->engine.id = SCRIPT_ENGINE_NAME;
result->engine.type = PANGO_ENGINE_TYPE_SHAPE;
result->engine.length = sizeof (result);
result->script_shape = basic_engine_shape;
result->get_coverage = basic_engine_get_coverage;
return (PangoEngine *)result;
}
void
PANGO_MODULE_ENTRY(list) (PangoEngineInfo **engines,
int *n_engines)
{
*engines = script_engines;
*n_engines = n_script_engines;
}
PangoEngine *
PANGO_MODULE_ENTRY(load) (const char *id)
{
if (!strcmp (id, SCRIPT_ENGINE_NAME))
return basic_engine_x_new ();
else
return NULL;
}
void
PANGO_MODULE_ENTRY(unload) (PangoEngine *engine)
{
}
|