summaryrefslogtreecommitdiff
path: root/src/fcscript.c
blob: cb630cb5a19d9ddc6ede3a9020af24e6d0651208 (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
/*
 * fontconfig/src/fcscript.c
 *
 * Copyright © 2002 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, 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 the author(s) not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  The authors make no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include "fcint.h"
#include "fcftint.h"

/* Objects MT-safe for readonly access. */

typedef struct {
    const FcChar8   script[8];
    const FcCharSet charset;
} FcScriptCharSet;

#include "../fc-script/fcscript.h"

struct _FcScriptSet {
    FcChar32 map_size;
    FcChar32 map[NUM_SCRIPT_SET_MAP];
};

static void
FcScriptSetBitSet (FcScriptSet  *ss,
		   unsigned int  id)
{
  unsigned int bucket;

  id = fcScriptCharSetIndices[id];
  bucket = id >> 5;
  if (bucket >= ss->map_size)
    return; /* shouldn't happen really */

  ss->map[bucket] |= ((FcChar32) 1 << (id & 0x1f));
}

static FcBool
FcScriptSetBitGet (const FcScriptSet *ss,
		   unsigned int       id)
{
  unsigned int bucket;

  id = fcScriptCharSetIndices[id];
  bucket = id >> 5;
  if (bucket >= ss->map_size)
    return FcFalse;

  return ((ss->map[bucket] >> (id & 0x1f)) & 1) ? FcTrue : FcFalse;
}

static void
FcScriptSetBitReset (FcScriptSet    *ss,
		     unsigned int    id)
{
  unsigned int bucket;

  id = fcScriptCharSetIndices[id];
  bucket = id >> 5;
  if (bucket >= ss->map_size)
    return; /* shouldn't happen really */

  ss->map[bucket] &= ~((FcChar32) 1 << (id & 0x1f));
}

FcScriptSet *
FcFreeTypeScriptSet (const FcCharSet  *charset)
{
    static const int threshold = 60;
    int		    i, j, avail;
    FcChar32	    missing, count;
    FcScriptSet	    *ss;

    ss = FcScriptSetCreate ();
    if (!ss)
	return 0;
    if (FcDebug() & FC_DBG_SCRIPTSET)
    {
	printf ("font scriptset");
	FcCharSetPrint (charset);
	printf ("\n");
    }
    for (i = 0; i < NUM_SCRIPT_CHAR_SET; i++)
    {
	if (FcDebug() & FC_DBG_SCRIPTSET)
	{
	    printf ("%s charset", fcScriptCharSets[i].script);
	    FcCharSetPrint (&fcScriptCharSets[i].charset);
	    printf ("\n");
	}

#if 0
	/*
	 * Check for Han charsets to make fonts
	 * which advertise support for a single language
	 * not support other Han languages
	 */
	if (exclusiveCharset &&
	    FcFreeTypeIsExclusiveLang (fcLangCharSets[i].lang))
	{
	    if (fcLangCharSets[i].charset.num != exclusiveCharset->num)
		continue;

	    for (j = 0; j < fcLangCharSets[i].charset.num; j++)
		if (FcCharSetLeaf(&fcLangCharSets[i].charset, j) !=
		    FcCharSetLeaf(exclusiveCharset, j))
		    continue;
	}
#endif
	count = FcCharSetCount (&fcScriptCharSets[i].charset);
	missing = FcCharSetSubtractCount (&fcScriptCharSets[i].charset, charset);
	avail = (count - missing) * 100 / count;
        if (FcDebug() & FC_DBG_SCANV)
	{
	    if (avail < threshold && missing < 10)
	    {
		FcCharSet   *missed = FcCharSetSubtract (&fcScriptCharSets[i].charset,
							 charset);
		FcChar32    ucs4;
		FcChar32    map[FC_CHARSET_MAP_SIZE];
		FcChar32    next;

		printf ("\n%s(%u) ", fcScriptCharSets[i].script, missing);
		printf ("{");
		for (ucs4 = FcCharSetFirstPage (missed, map, &next);
		     ucs4 != FC_CHARSET_DONE;
		     ucs4 = FcCharSetNextPage (missed, map, &next))
		{
		    int	    i, j;
		    for (i = 0; i < FC_CHARSET_MAP_SIZE; i++)
			if (map[i])
			{
			    for (j = 0; j < 32; j++)
				if (map[i] & (1 << j))
				    printf (" %04x", ucs4 + i * 32 + j);
			}
		}
		printf (" }\n\t");
		FcCharSetDestroy (missed);
	    }
	    else
		printf ("%s(%u) ", fcScriptCharSets[i].script, missing);
	}
	if (avail >= threshold)
	    FcScriptSetBitSet (ss, i);
    }

    if (FcDebug() & FC_DBG_SCANV)
	printf ("\n");

    printf ("scriptset:\n");
    FcScriptSetPrint (ss);
    printf ("\n");

    return ss;
}

const FcCharSet *
FcScriptGetCharSet (const FcChar8 *script)
{
    int i;

    for (i = 0; i < NUM_SCRIPT_CHAR_SET; i++)
    {
	if (FcStrCmpIgnoreCase (script, fcScriptCharSets[i].script) == 0)
	{
	    volatile FcCharSet *cs = &fcScriptCharSets[i].charset;
	    printf("%p\n", cs);
//	    __asm__ volatile("int $03");
	    return cs;
//	    return &fcScriptCharSets[i].charset;
	}
    }

    return NULL;
}

FcScriptSet *
FcScriptSetCreate (void)
{
    FcScriptSet *ss;

    ss = malloc (sizeof (FcScriptSet));
    if (!ss)
	return NULL;
    memset (ss->map, '\0',  sizeof (ss->map));
    ss->map_size = NUM_SCRIPT_SET_MAP;

    return ss;
}

void
FcScriptSetDestroy (FcScriptSet *ss)
{
    free (ss);
}

FcBool
FcNameUnparseScriptSet (FcStrBuf *buf, const FcScriptSet *ss)
{
    int		i, bit, count;
    FcChar32	bits;
    FcBool	first = FcTrue;

    count = FC_MIN (ss->map_size, NUM_SCRIPT_SET_MAP);
    for (i = 0; i < count; i++)
    {
	if ((bits = ss->map[i]))
	{
	    for (bit = 0; bit <= 31; bit++)
		if (bits & (1 << bit))
		{
		    int id = (i << 5) | bit;
		    if (!first)
			if (!FcStrBufChar (buf, '|'))
			    return FcFalse;
		    if (!FcStrBufString (buf, fcScriptCharSets[fcScriptCharSetIndicesInv[id]].script))
			return FcFalse;
		    first = FcFalse;
		}
	}
    }
    return FcTrue;
}

#define __fcscript__
#include "fcaliastail.h"
#include "fcftaliastail.h"
#undef __fcscript__