]>
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;
31 FcCacheBankToIndexMTF (int bank
)
35 /* end stub definitions */
38 rawindex (const FcGlyphName
*gn
);
41 scan (FILE *f
, char *filename
);
50 FcHashGlyphName (const FcChar8
*name
);
53 insert (FcGlyphName
*gn
, FcGlyphName
**table
, FcChar32 h
);
56 dump (FcGlyphName
* const *table
, const char *name
);
59 FcAllocGlyphName (FcChar32 ucs
, FcChar8
*name
)
63 gn
= malloc (sizeof (FcGlyphName
) + strlen ((char *) name
));
67 strcpy ((char *) gn
->name
, (char *) name
);
72 fatal (const char *file
, int lineno
, const char *msg
)
75 fprintf (stderr
, "%s:%d: %s\n", file
, lineno
, msg
);
77 fprintf (stderr
, "%s: %s\n", file
, msg
);
82 #define MAX_GLYPHFILE 256
83 #define MAX_GLYPHNAME 10240
84 #define MAX_NAMELEN 1024
86 FcGlyphName
*raw
[MAX_GLYPHNAME
];
89 FcGlyphName
*name_to_ucs
[MAX_GLYPHNAME
*2];
90 FcGlyphName
*ucs_to_name
[MAX_GLYPHNAME
*2];
94 rawindex (const FcGlyphName
*gn
)
98 for (i
= 0; i
< nraw
; i
++)
105 scan (FILE *f
, char *filename
)
107 char buf
[MAX_NAMELEN
];
108 char name
[MAX_NAMELEN
];
114 while (fgets (buf
, sizeof (buf
), f
))
117 if (sscanf (buf
, "%[^;];%lx\n", name
, &ucs
) != 2)
119 gn
= FcAllocGlyphName ((FcChar32
) ucs
, (FcChar8
*) name
);
121 fatal (filename
, lineno
, "out of memory");
123 if (len
> max_name_len
)
129 static int compare_string (const void *a
, const void *b
)
131 const char *const *as
= a
, *const *bs
= b
;
132 return strcmp (*as
, *bs
);
135 static int compare_glyphname (const void *a
, const void *b
)
137 const FcGlyphName
*const *ag
= a
, *const *bg
= b
;
139 return strcmp ((char *) (*ag
)->name
, (char *) (*bg
)->name
);
174 for (t
= 3; t
<= l
; t
+= 2)
181 * Find a prime pair that leaves at least 25% of the hash table empty
192 while (!isprime(h
-2) || !isprime(h
))
199 FcHashGlyphName (const FcChar8
*name
)
204 while ((c
= *name
++))
206 h
= ((h
<< 1) | (h
>> 31)) ^ c
;
212 insert (FcGlyphName
*gn
, FcGlyphName
**table
, FcChar32 h
)
216 i
= (int) (h
% hash
);
219 if (!r
) r
= (int) (h
% rehash
);
228 dump (FcGlyphName
* const *table
, const char *name
)
232 printf ("static const FcGlyphName *%s[%d] = {\n", name
, hash
);
234 for (i
= 0; i
< hash
; i
++)
236 printf ("(FcGlyphName *) &glyph%d,\n", rawindex(table
[i
]));
244 main (int argc
, char **argv
)
246 char *files
[MAX_GLYPHFILE
];
254 if (i
== MAX_GLYPHFILE
)
255 fatal (*argv
, 0, "Too many glyphname files");
256 files
[i
] = argv
[i
+1];
260 qsort (files
, i
, sizeof (char *), compare_string
);
261 for (i
= 0; files
[i
]; i
++)
263 f
= fopen (files
[i
], "r");
265 fatal (files
[i
], 0, strerror (errno
));
269 qsort (raw
, nraw
, sizeof (FcGlyphName
*), compare_glyphname
);
273 for (i
= 0; i
< nraw
; i
++)
275 insert (raw
[i
], name_to_ucs
, FcHashGlyphName (raw
[i
]->name
));
276 insert (raw
[i
], ucs_to_name
, raw
[i
]->ucs
);
280 * Scan the input until the marker is found
283 while (fgets (line
, sizeof (line
), stdin
))
285 if (!strncmp (line
, "@@@", 3))
287 fputs (line
, stdout
);
290 printf ("/* %d glyphnames in %d entries, %d%% occupancy */\n\n",
291 nraw
, hash
, nraw
* 100 / hash
);
293 printf ("#define FC_GLYPHNAME_HASH %u\n", hash
);
294 printf ("#define FC_GLYPHNAME_REHASH %u\n", rehash
);
295 printf ("#define FC_GLYPHNAME_MAXLEN %d\n\n", max_name_len
);
301 for (i
= 0; i
< nraw
; i
++)
302 printf ("static const struct { const FcChar32 ucs; const FcChar8 name[%d]; }"
303 " glyph%d = { 0x%lx, \"%s\" };\n",
304 (int) strlen ((char *) raw
[i
]->name
) + 1,
305 i
, (unsigned long) raw
[i
]->ucs
, raw
[i
]->name
);
308 * Dump out name_to_ucs table
311 dump (name_to_ucs
, "name_to_ucs");
314 * Dump out ucs_to_name table
316 dump (ucs_to_name
, "ucs_to_name");
318 while (fgets (line
, sizeof (line
), stdin
))
319 fputs (line
, stdout
);
322 exit (ferror (stdout
));