File: fontbank.c

package info (click to toggle)
kinput2 3.0-19
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,688 kB
  • ctags: 4,649
  • sloc: ansic: 45,508; makefile: 100; sh: 35
file content (323 lines) | stat: -rw-r--r-- 7,135 bytes parent folder | download | duplicates (10)
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
#ifndef lint
static char *rcsid = "$Id: fontbank.c,v 1.4 1994/05/17 10:52:15 ishisone Rel $";
#endif
/*
 * Copyright (c) 1991, 1994  Software Research Associates, Inc.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Software Research Associates not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  Software Research
 * Associates makes no representations about the suitability of this software
 * for any purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * Author:  Makoto Ishisone, Software Research Associates, Inc., Japan
 */

#include <X11/Xos.h>
#include <X11/Intrinsic.h>
#include "CachedFont.h"
#include "KIFontSet.h"
#include "FontBank.h"

#define DEBUG_VAR debug_fontbank
#include "DebugPrint.h"

#define FC_HASH_SIZE	8

typedef struct _font_cache_ {
    int sum;
    char *name;
    XFontStruct **fonts;
    int num_fonts;
    struct _font_cache_ *next;
} FontCache;

typedef struct _fb_info_ {
    char *language;
    KICharSet *charsets;
    int num_charsets;
} FBInfo;

typedef struct _fb_rec_ {
    Display *dpy;
    struct _fb_rec_ *next;
    FBInfo *info;
    int reference_count;
    FontCache *hash[FC_HASH_SIZE];
} FBRec;
    
static FontBank fontBankList = NULL;

/*
 * Language information:
 * The FBInfo structure holds information about the character sets
 * required for a specific language.  Currently, this infomation
 * is provided only for Japanese.
 */

static KICharSetSpec asciiCharSets[] = {
    { "iso8859-1", NULL },		/* preferable */
    { "jisx0201.1976-0", NULL },	/* alternative */
    { "iso8859-*", NULL },		/* alternative */
};
static KICharSetSpec kanaCharSets[] = {
    { "jisx0201.1976-0", NULL },
};
static KICharSetSpec kanjiCharSets[] = {
    { "jisx0208.1983-0", NULL },
    { "jisx0208.1983-1", NULL },
    { "jisx0208.1990-0", NULL },
    { "jisx0208.1990-1", NULL },
    { "jisx0208.1976-0", NULL },
    { "jisx0208.1976-1", NULL },
};
static KICharSet jpCharSets[3] = {
    { asciiCharSets, XtNumber(asciiCharSets) },
    { kanaCharSets, XtNumber(kanaCharSets) },
    { kanjiCharSets, XtNumber(kanjiCharSets) },
};

static FBInfo fontBankInfo[] = {
    { "ja_JP", jpCharSets, XtNumber(jpCharSets) },
    { NULL }
};

static int
getsum(s)
char *s;
{
    unsigned char *p = (unsigned char *)s;
    int sum = 0;

    while (*p != '\0') sum += *p++;
    return sum;
}

static FBInfo *
getInfo(lang)
char *lang;
{
    FBInfo *fip;

    for (fip = fontBankInfo; fip->language != NULL; fip++) {
	if (!strcmp(fip->language, lang)) return fip;
    }
    DPRINT(("fonbank: language %s not supported\n", lang));
    return NULL;
}

static XFontStruct **
lookupCacheFonts(bank, fontset, num_fontsp)
FontBank bank;
char *fontset;
int *num_fontsp;
{
    FontCache *fc;
    int i;
    int sum;

    sum = getsum(fontset);
    fc = bank->hash[sum % FC_HASH_SIZE];

    /* lookup cache */
    while (fc != NULL) {
	if (fc->sum == sum && !strcmp(fc->name, fontset)) {
	    /* found */
	    *num_fontsp = fc->num_fonts;
	    for (i = 0; i < *num_fontsp; i++) {
		(void)CachedLoadFontByFontStruct(bank->dpy, fc->fonts[i]);
	    }
	    return fc->fonts;
	}
	fc = fc->next;
    }
    *num_fontsp = 0;
    return NULL;
}

static void
cacheFonts(bank, fontset, fonts, num_fonts)
FontBank bank;
char *fontset;
XFontStruct **fonts;
int num_fonts;
{
    FontCache *fc;
    int sum;

    fc = XtNew(FontCache);
    fontset = XtNewString(fontset);
    sum = getsum(fontset);

    fc->sum = sum;
    fc->name = fontset;
    fc->fonts = fonts;
    fc->num_fonts = num_fonts;
    fc->next = bank->hash[sum % FC_HASH_SIZE];
    bank->hash[sum % FC_HASH_SIZE] = fc;
}

static XFontStruct **
extractFonts(dpy, fontset, charsets, ncharsets, nfontsp)
Display *dpy;
char *fontset;
KICharSet *charsets;
int ncharsets;
int *nfontsp;
{
    KICharSetFont *kifonts;
    KICharSetFont buf[10];
    XFontStruct **fonts, **fp;

    if (ncharsets > 10) {
	kifonts = (KICharSetFont *)XtMalloc(ncharsets * sizeof(KICharSetFont));
    } else {
	kifonts = buf;
    }
    *nfontsp = ExtractFontsFromFontSet(dpy, fontset, charsets,
				       kifonts, ncharsets);
    fonts = NULL;
    if (*nfontsp > 0) {
	int i;

	fonts = (XFontStruct **)XtMalloc(*nfontsp * sizeof(XFontStruct *));
	for (i = 0, fp = fonts; i < ncharsets; i++) {
	    if (kifonts[i].font != NULL) *fp++ = kifonts[i].font;
	}
    }
    if (kifonts != buf) XtFree((char *)kifonts);
    return fonts;
}

static void
freeCache(bank)
FontBank bank;
{
    FontCache *fc;
    int i;

    for (i = 0; i < FC_HASH_SIZE; i++) {
	fc = bank->hash[i];
	while (fc != NULL) {
	    FontCache *next = fc->next;

	    XtFree(fc->name);
	    XtFree((char *)fc->fonts);
	    XtFree((char *)fc);
	    fc = next;
	}
    }
}


/*
 * Public functions
 */

FontBank
FontBankCreate(dpy, language)
Display *dpy;
char *language;
{
    FontBank fb;
    FBInfo *info;
    int i;

    TRACE(("FontBankCreate(language:%s)\n", language));

    if ((info = getInfo(language)) == NULL) return NULL;

    for (fb = fontBankList; fb != NULL; fb = fb->next) {
	if (fb->dpy == dpy && fb->info == info) {
	    TRACE(("\tfontbank for %s already exists\n", language));
	    fb->reference_count++;
	    return fb;
	}
    }

    TRACE(("\tcreate fontbank for %s...\n", language));
    fb = XtNew(FBRec);
    fb->dpy = dpy;
    fb->info = info;
    fb->reference_count = 1;
    for (i = 0; i < FC_HASH_SIZE; i++) {
	fb->hash[i] = NULL;
    }
    fb->next = fontBankList;
    fontBankList = fb;
    return fb;
}

void
FontBankDestroy(bank)
FontBank bank;
{
    TRACE(("FontBankDestroy()\n"));

    if (--(bank->reference_count) <= 0) {
	FontBank fb, fb0;

	TRACE(("\tfreeing fontbank...\n"));
	fb = fontBankList;
	fb0 = NULL;
	while (fb != NULL) {
	    if (fb == bank) {
		if (fb0 == NULL) {
		    fontBankList = fb->next;
		} else {
		    fb0->next = fb->next;
		}
		freeCache(fb);
		XtFree((char *)fb);
		return;
	    }
	    fb0 = fb;
	    fb = fb->next;
	}
    }
}

XFontStruct **
FontBankGet(bank, fontset, num_fontsp)
FontBank bank;
char *fontset;
int *num_fontsp;
{
    XFontStruct **fpp;

    TRACE(("FontBankGet(fontset:%s)\n", fontset));

    if ((fpp = lookupCacheFonts(bank, fontset, num_fontsp)) != NULL) {
	TRACE(("\tfontset found in fontbank (numfonts=%d)\n",
	       *num_fontsp));
	return fpp;
    }

    fpp = extractFonts(bank->dpy, fontset, bank->info->charsets,
		       bank->info->num_charsets, num_fontsp);

    /* enter cache */
    TRACE(("\tcaching fontset (numfonts=%d)\n", *num_fontsp));
    cacheFonts(bank, fontset, fpp, *num_fontsp);
    return fpp;
}

void
FontBankFreeFonts(bank, fonts, num_fonts)
FontBank bank;
XFontStruct **fonts;
int num_fonts;
{
    int i;

    TRACE(("FontBankFreeFonts()\n"));

    for (i = 0; i < num_fonts; i++) {
	CachedFreeFont(bank->dpy, fonts[i]);
    }
}