]>
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.
28 FcAllocGlyphName (FcChar32 ucs
, FcChar8
*name
)
32 gn
= malloc (sizeof (FcGlyphName
) + strlen ((char *) name
));
36 strcpy ((char *) gn
->name
, (char *) name
);
41 fatal (char *file
, int lineno
, char *msg
)
43 fprintf (stderr
, "%s:%d: %s\n", file
, lineno
, msg
);
47 #define MAX_GLYPHFILE 256
48 #define MAX_GLYPHNAME 10240
49 #define MAX_NAMELEN 1024
51 FcGlyphName
*raw
[MAX_GLYPHNAME
];
54 FcGlyphName
*name_to_ucs
[MAX_GLYPHNAME
*2];
55 FcGlyphName
*ucs_to_name
[MAX_GLYPHNAME
*2];
59 rawindex (FcGlyphName
*gn
)
63 for (i
= 0; i
< nraw
; i
++)
70 scan (FILE *f
, char *filename
)
72 char buf
[MAX_NAMELEN
];
73 char name
[MAX_NAMELEN
];
79 while (fgets (buf
, sizeof (buf
), f
))
82 if (sscanf (buf
, "%[^;];%lx\n", name
, &ucs
) != 2)
84 gn
= FcAllocGlyphName ((FcChar32
) ucs
, (FcChar8
*) name
);
86 fatal (filename
, lineno
, "out of memory");
87 len
= strlen ((FcChar8
*) name
);
88 if (len
> max_name_len
)
94 static int compare_string (const void *a
, const void *b
)
96 const char *const *as
= a
, *const *bs
= b
;
97 return strcmp (*as
, *bs
);
100 static int compare_glyphname (const void *a
, const void *b
)
102 const FcGlyphName
*const *ag
= a
, *const *bg
= b
;
104 return strcmp ((char *) (*ag
)->name
, (char *) (*bg
)->name
);
139 for (t
= 3; t
<= l
; t
+= 2)
146 * Find a prime pair that leaves at least 25% of the hash table empty
157 while (!isprime(h
-2) || !isprime(h
))
164 FcHashGlyphName (const FcChar8
*name
)
169 while ((c
= *name
++))
171 h
= ((h
<< 1) | (h
>> 31)) ^ c
;
177 insert (FcGlyphName
*gn
, FcGlyphName
**table
, FcChar32 h
)
181 i
= (int) (h
% hash
);
184 if (!r
) r
= (int) (h
% rehash
);
193 dump (FcGlyphName
**table
, char *name
)
197 printf ("static FcGlyphName *%s[%d] = {\n", name
, hash
);
199 for (i
= 0; i
< hash
; i
++)
201 printf ("(FcGlyphName *) &glyph%d,\n", rawindex(table
[i
]));
209 main (int argc
, char **argv
)
211 char *files
[MAX_GLYPHFILE
];
219 if (i
== MAX_GLYPHFILE
)
220 fatal (*argv
, 0, "Too many glyphname files");
224 qsort (files
, i
, sizeof (char *), compare_string
);
225 for (i
= 0; files
[i
]; i
++)
227 f
= fopen (files
[i
], "r");
229 fatal (files
[i
], 0, strerror (errno
));
233 qsort (raw
, nraw
, sizeof (FcGlyphName
*), compare_glyphname
);
237 for (i
= 0; i
< nraw
; i
++)
239 insert (raw
[i
], name_to_ucs
, FcHashGlyphName (raw
[i
]->name
));
240 insert (raw
[i
], ucs_to_name
, raw
[i
]->ucs
);
244 * Scan the input until the marker is found
247 while (fgets (line
, sizeof (line
), stdin
))
249 if (!strncmp (line
, "@@@", 3))
251 fputs (line
, stdout
);
254 printf ("/* %d glyphnames in %d entries, %d%% occupancy */\n\n",
255 nraw
, hash
, nraw
* 100 / hash
);
257 printf ("#define FC_GLYPHNAME_HASH %u\n", hash
);
258 printf ("#define FC_GLYPHNAME_REHASH %u\n", rehash
);
259 printf ("#define FC_GLYPHNAME_MAXLEN %d\n\n", max_name_len
);
265 for (i
= 0; i
< nraw
; i
++)
266 printf ("static struct { FcChar32 ucs; FcChar8 name[%d]; }"
267 " glyph%d = { 0x%lx, \"%s\" };\n",
268 strlen (raw
[i
]->name
) + 1,
269 i
, (unsigned long) raw
[i
]->ucs
, raw
[i
]->name
);
272 * Dump out name_to_ucs table
275 dump (name_to_ucs
, "name_to_ucs");
278 * Dump out ucs_to_name table
280 dump (ucs_to_name
, "ucs_to_name");
282 while (fgets (line
, sizeof (line
), stdin
))
283 fputs (line
, stdout
);
286 exit (ferror (stdout
));