]>
git.wh0rd.org - fontconfig.git/blob - fc-glyphname/fc-glyphname.c
2 * fontconfig/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 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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 rawindex (const FcGlyphName
*gn
);
31 scan (FILE *f
, char *filename
);
40 FcHashGlyphName (const FcChar8
*name
);
43 insert (FcGlyphName
*gn
, FcGlyphName
**table
, FcChar32 h
);
46 dump (FcGlyphName
* const *table
, const char *name
);
49 FcAllocGlyphName (FcChar32 ucs
, FcChar8
*name
)
53 gn
= malloc (sizeof (FcGlyphName
) + strlen ((char *) name
));
57 strcpy ((char *) gn
->name
, (char *) name
);
62 fatal (const char *file
, int lineno
, const char *msg
)
65 fprintf (stderr
, "%s:%d: %s\n", file
, lineno
, msg
);
67 fprintf (stderr
, "%s: %s\n", file
, msg
);
72 #define MAX_GLYPHFILE 256
73 #define MAX_GLYPHNAME 10240
74 #define MAX_NAMELEN 1024
76 static FcGlyphName
*raw
[MAX_GLYPHNAME
];
78 static int max_name_len
;
79 static FcGlyphName
*name_to_ucs
[MAX_GLYPHNAME
*2];
80 static FcGlyphName
*ucs_to_name
[MAX_GLYPHNAME
*2];
81 static unsigned int hash
, rehash
;
84 rawindex (const FcGlyphName
*gn
)
88 for (i
= 0; i
< nraw
; i
++)
95 scan (FILE *f
, char *filename
)
97 char buf
[MAX_NAMELEN
];
98 char name
[MAX_NAMELEN
];
104 while (fgets (buf
, sizeof (buf
), f
))
107 if (sscanf (buf
, "%[^;];%lx\n", name
, &ucs
) != 2)
109 gn
= FcAllocGlyphName ((FcChar32
) ucs
, (FcChar8
*) name
);
111 fatal (filename
, lineno
, "out of memory");
113 if (len
> max_name_len
)
119 static int compare_string (const void *a
, const void *b
)
121 const char *const *as
= a
, *const *bs
= b
;
122 return strcmp (*as
, *bs
);
125 static int compare_glyphname (const void *a
, const void *b
)
127 const FcGlyphName
*const *ag
= a
, *const *bg
= b
;
129 return strcmp ((char *) (*ag
)->name
, (char *) (*bg
)->name
);
164 for (t
= 3; t
<= l
; t
+= 2)
171 * Find a prime pair that leaves at least 25% of the hash table empty
182 while (!isprime(h
-2) || !isprime(h
))
189 FcHashGlyphName (const FcChar8
*name
)
194 while ((c
= *name
++))
196 h
= ((h
<< 1) | (h
>> 31)) ^ c
;
202 insert (FcGlyphName
*gn
, FcGlyphName
**table
, FcChar32 h
)
206 i
= (int) (h
% hash
);
209 if (!r
) r
= (int) (h
% rehash
+ 1);
218 dump (FcGlyphName
* const *table
, const char *name
)
222 printf ("static const FcGlyphId %s[%d] = {\n", name
, hash
);
224 for (i
= 0; i
< hash
; i
++)
226 printf (" %d,\n", rawindex(table
[i
]));
234 main (int argc
, char **argv
)
236 char *files
[MAX_GLYPHFILE
];
245 if (i
== MAX_GLYPHFILE
)
246 fatal (*argv
, 0, "Too many glyphname files");
247 files
[i
] = argv
[i
+1];
251 qsort (files
, i
, sizeof (char *), compare_string
);
252 for (i
= 0; files
[i
]; i
++)
254 f
= fopen (files
[i
], "r");
256 fatal (files
[i
], 0, strerror (errno
));
260 qsort (raw
, nraw
, sizeof (FcGlyphName
*), compare_glyphname
);
264 for (i
= 0; i
< nraw
; i
++)
266 insert (raw
[i
], name_to_ucs
, FcHashGlyphName (raw
[i
]->name
));
267 insert (raw
[i
], ucs_to_name
, raw
[i
]->ucs
);
271 * Scan the input until the marker is found
274 while (fgets (line
, sizeof (line
), stdin
))
276 if (!strncmp (line
, "@@@", 3))
278 fputs (line
, stdout
);
281 printf ("/* %d glyphnames in %d entries, %d%% occupancy */\n\n",
282 nraw
, hash
, nraw
* 100 / hash
);
284 printf ("#define FC_GLYPHNAME_HASH %u\n", hash
);
285 printf ("#define FC_GLYPHNAME_REHASH %u\n", rehash
);
286 printf ("#define FC_GLYPHNAME_MAXLEN %d\n\n", max_name_len
);
289 else if (nraw
< 32768)
294 printf ("typedef %s FcGlyphId;\n\n", type
);
300 printf ("static const struct { const FcChar32 ucs; const FcChar8 name[%d]; } _fc_glyph_names[%d] = {\n",
301 max_name_len
+ 1, nraw
);
303 for (i
= 0; i
< nraw
; i
++)
304 printf (" { 0x%lx, \"%s\" },\n",
305 (unsigned long) raw
[i
]->ucs
, raw
[i
]->name
);
310 * Dump out name_to_ucs table
313 dump (name_to_ucs
, "_fc_name_to_ucs");
316 * Dump out ucs_to_name table
318 dump (ucs_to_name
, "_fc_ucs_to_name");
320 while (fgets (line
, sizeof (line
), stdin
))
321 fputs (line
, stdout
);
324 exit (ferror (stdout
));