]>
git.wh0rd.org - fontconfig.git/blob - fc-glyphname/fc-glyphname.c
4 * Copyright © 2003 Keith Packard
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
27 /* stub definitions for declarations from fcint.h.. */
28 int * _fcBankId
= 0, * _fcBankIdx
= 0;
29 FcValueList
** _fcValueLists
= 0;
30 FcPatternElt
** _fcPatternElts
= 0;
34 FcCacheBankToIndexMTF (int bank
)
38 /* end stub definitions */
41 rawindex (const FcGlyphName
*gn
);
44 scan (FILE *f
, char *filename
);
53 FcHashGlyphName (const FcChar8
*name
);
56 insert (FcGlyphName
*gn
, FcGlyphName
**table
, FcChar32 h
);
59 dump (FcGlyphName
* const *table
, const char *name
);
62 FcAllocGlyphName (FcChar32 ucs
, FcChar8
*name
)
66 gn
= malloc (sizeof (FcGlyphName
) + strlen ((char *) name
));
70 strcpy ((char *) gn
->name
, (char *) name
);
75 fatal (const char *file
, int lineno
, const char *msg
)
78 fprintf (stderr
, "%s:%d: %s\n", file
, lineno
, msg
);
80 fprintf (stderr
, "%s: %s\n", file
, msg
);
85 #define MAX_GLYPHFILE 256
86 #define MAX_GLYPHNAME 10240
87 #define MAX_NAMELEN 1024
89 static FcGlyphName
*raw
[MAX_GLYPHNAME
];
91 static int max_name_len
;
92 static FcGlyphName
*name_to_ucs
[MAX_GLYPHNAME
*2];
93 static FcGlyphName
*ucs_to_name
[MAX_GLYPHNAME
*2];
94 static unsigned int hash
, rehash
;
97 rawindex (const FcGlyphName
*gn
)
101 for (i
= 0; i
< nraw
; i
++)
108 scan (FILE *f
, char *filename
)
110 char buf
[MAX_NAMELEN
];
111 char name
[MAX_NAMELEN
];
117 while (fgets (buf
, sizeof (buf
), f
))
120 if (sscanf (buf
, "%[^;];%lx\n", name
, &ucs
) != 2)
122 gn
= FcAllocGlyphName ((FcChar32
) ucs
, (FcChar8
*) name
);
124 fatal (filename
, lineno
, "out of memory");
126 if (len
> max_name_len
)
132 static int compare_string (const void *a
, const void *b
)
134 const char *const *as
= a
, *const *bs
= b
;
135 return strcmp (*as
, *bs
);
138 static int compare_glyphname (const void *a
, const void *b
)
140 const FcGlyphName
*const *ag
= a
, *const *bg
= b
;
142 return strcmp ((char *) (*ag
)->name
, (char *) (*bg
)->name
);
177 for (t
= 3; t
<= l
; t
+= 2)
184 * Find a prime pair that leaves at least 25% of the hash table empty
195 while (!isprime(h
-2) || !isprime(h
))
202 FcHashGlyphName (const FcChar8
*name
)
207 while ((c
= *name
++))
209 h
= ((h
<< 1) | (h
>> 31)) ^ c
;
215 insert (FcGlyphName
*gn
, FcGlyphName
**table
, FcChar32 h
)
219 i
= (int) (h
% hash
);
222 if (!r
) r
= (int) (h
% rehash
);
231 dump (FcGlyphName
* const *table
, const char *name
)
235 printf ("static const FcGlyphName *%s[%d] = {\n", name
, hash
);
237 for (i
= 0; i
< hash
; i
++)
239 printf ("(FcGlyphName *) &glyph%d,\n", rawindex(table
[i
]));
247 main (int argc
, char **argv
)
249 char *files
[MAX_GLYPHFILE
];
257 if (i
== MAX_GLYPHFILE
)
258 fatal (*argv
, 0, "Too many glyphname files");
259 files
[i
] = argv
[i
+1];
263 qsort (files
, i
, sizeof (char *), compare_string
);
264 for (i
= 0; files
[i
]; i
++)
266 f
= fopen (files
[i
], "r");
268 fatal (files
[i
], 0, strerror (errno
));
272 qsort (raw
, nraw
, sizeof (FcGlyphName
*), compare_glyphname
);
276 for (i
= 0; i
< nraw
; i
++)
278 insert (raw
[i
], name_to_ucs
, FcHashGlyphName (raw
[i
]->name
));
279 insert (raw
[i
], ucs_to_name
, raw
[i
]->ucs
);
283 * Scan the input until the marker is found
286 while (fgets (line
, sizeof (line
), stdin
))
288 if (!strncmp (line
, "@@@", 3))
290 fputs (line
, stdout
);
293 printf ("/* %d glyphnames in %d entries, %d%% occupancy */\n\n",
294 nraw
, hash
, nraw
* 100 / hash
);
296 printf ("#define FC_GLYPHNAME_HASH %u\n", hash
);
297 printf ("#define FC_GLYPHNAME_REHASH %u\n", rehash
);
298 printf ("#define FC_GLYPHNAME_MAXLEN %d\n\n", max_name_len
);
304 for (i
= 0; i
< nraw
; i
++)
305 printf ("static const struct { const FcChar32 ucs; const FcChar8 name[%d]; }"
306 " glyph%d = { 0x%lx, \"%s\" };\n",
307 (int) strlen ((char *) raw
[i
]->name
) + 1,
308 i
, (unsigned long) raw
[i
]->ucs
, raw
[i
]->name
);
311 * Dump out name_to_ucs table
314 dump (name_to_ucs
, "name_to_ucs");
317 * Dump out ucs_to_name table
319 dump (ucs_to_name
, "ucs_to_name");
321 while (fgets (line
, sizeof (line
), stdin
))
322 fputs (line
, stdout
);
325 exit (ferror (stdout
));