]> git.wh0rd.org - fontconfig.git/blobdiff - fc-lang/fc-lang.c
Include more stub definitions to make HP-UX's C compiler happy.
[fontconfig.git] / fc-lang / fc-lang.c
index c8fec3ebe3e0d38dfd0588df36c9981c8c8f7521..6a863e4e6dea689d1fbb01f5ccbfa5d3e08ff23d 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * $XFree86: xc/lib/fontconfig/fc-lang/fc-lang.c,v 1.1 2002/07/06 23:21:36 keithp Exp $
+ * $RCSId: xc/lib/fontconfig/fc-lang/fc-lang.c,v 1.3 2002/08/22 07:36:43 keithp Exp $
  *
- * Copyright © 2002 Keith Packard, member of The XFree86 Project, Inc.
+ * 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
  */
 
 #include "fcint.h"
+#include "fccharset.c"
+#include "fcstr.c"
 
 /*
  * fc-lang
  *
  * Read a set of language orthographies and build C declarations for
  * charsets which can then be used to identify which languages are
- * supported by a given font.  Note that it would be nice if
- * this could be done while compiling the library, but this
- * code uses a number of routines from the library.  It's
- * expediant to just ship the pre-built version along with the
- * source orthographies.
+ * supported by a given font.  Note that this uses some utilities
+ * from the fontconfig library, so the necessary file is simply
+ * included in this compilation.  A couple of extra utility
+ * functions are also needed in slightly modified form
  */
 
+const FcChar16 langBankNumbers[1]; /* place holders so that externs resolve */
+const FcCharLeaf       langBankLeaves[1];
+const int langBankLeafIdx[1];
+
+void
+FcMemAlloc (int kind, int size)
+{
+}
+
+void
+FcMemFree (int kind, int size)
+{
+}
+
+int* _fcBankId = 0;
+int* _fcBankIdx = 0;
+FcValueList ** _fcValueLists = 0;
+FcPatternElt ** _fcPatternElts = 0;
+int FcDebugVal = 0;
+
+int
+FcCacheBankToIndexMTF (int bank)
+{
+    return -1;
+}
+
+FcChar8 *
+FcConfigHome (void)
+{
+    return (FcChar8 *) getenv ("HOME");
+}
+
 static void 
-fatal (char *file, int lineno, char *msg)
+fatal (const char *file, int lineno, const char *msg)
 {
-    fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
+    if (lineno)
+       fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
+    else
+       fprintf (stderr, "%s: %s\n", file, msg);
     exit (1);
 }
 
@@ -47,17 +83,43 @@ static char *
 get_line (FILE *f, char *line, int *lineno)
 {
     char    *hash;
+    int            end;
     if (!fgets (line, 1024, f))
        return 0;
     ++(*lineno);
     hash = strchr (line, '#');
     if (hash)
        *hash = '\0';
+
+    end = strlen (line);
+    while (end > 0 && isspace (line[end-1]))
+      line[--end] = '\0';
+
     if (line[0] == '\0' || line[0] == '\n' || line[0] == '\032' || line[0] == '\r')
        return get_line (f, line, lineno);
     return line;
 }
 
+static char    *dir = 0;
+
+static FILE *
+scanopen (char *file)
+{
+    FILE    *f;
+
+    f = fopen (file, "r");
+    if (!f && dir)
+    {
+       char    path[1024];
+       
+       strcpy (path, dir);
+       strcat (path, "/");
+       strcat (path, file);
+       f = fopen (path, "r");
+    }
+    return f;
+}
+
 /*
  * build a single charset from a source file
  *
@@ -81,12 +143,9 @@ scan (FILE *f, char *file)
        if (!strncmp (line, "include", 7))
        {
            file = strchr (line, ' ');
-           while (*file == ' ')
+           while (isspace(*file))
                file++;
-           end = strlen (file);
-           if (file[end-1] == '\n')
-               file[end-1] = '\0';
-           f = fopen (file, "r");
+           f = scanopen (file);
            if (!f)
                fatal (file, 0, "can't open");
            c = scan (f, file);
@@ -147,8 +206,8 @@ get_lang (char *name)
 
     while ((c = *name++))
     {
-       if (isupper (c))
-           c = tolower (c);
+       if (isupper ((int) (unsigned char) c))
+           c = tolower ((int) (unsigned char) c);
        if (c == '_')
            c = '-';
        if (c == ' ')
@@ -159,26 +218,71 @@ get_lang (char *name)
     return lang;
 }
 
+static int compare (const void *a, const void *b)
+{
+    const FcChar8    *const *as = a, *const *bs = b;
+    return FcStrCmpIgnoreCase (*as, *bs);
+}
+
+#define MAX_LANG           1024
+#define MAX_LANG_SET_MAP    ((MAX_LANG + 31) / 32)
+
+#define BitSet(map, id)   ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
+#define BitGet(map, id)   ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
+
 int
 main (int argc, char **argv)
 {
-    FcCharSet  *sets[1024];
-    int                duplicate[1024];
-    char       *names[1024];
+    static char                *files[MAX_LANG];
+    static FcCharSet   *sets[MAX_LANG];
+    static int         duplicate[MAX_LANG];
+    static int         offsets[MAX_LANG];
+    static int         country[MAX_LANG];
+    static char                *names[MAX_LANG];
+    static char                *langs[MAX_LANG];
     FILE       *f;
+    int         offset = 0;
+    int                ncountry = 0;
     int                i = 0;
-    FcCharLeaf **leaves, **sleaves;
+    int                argi;
+    FcCharLeaf **leaves;
     int                total_leaves = 0;
+    int                offset_count = 0;
     int                l, sl, tl;
-    char       line[1024];
+    static char                line[1024];
+    static FcChar32    map[MAX_LANG_SET_MAP];
+    int                num_lang_set_map;
+    int                setRangeStart[26];
+    int                setRangeEnd[26];
+    FcChar8    setRangeChar;
     
-    while (*++argv)
+    argi = 1;
+    while (argv[argi])
+    {
+       if (!strcmp (argv[argi], "-d"))
+       {
+           argi++;
+           dir = argv[argi++];
+           continue;
+       }
+       if (i == MAX_LANG)
+           fatal (argv[0], 0, "Too many languages");
+       files[i++] = argv[argi++];
+    }
+    files[i] = 0;
+    qsort (files, i, sizeof (char *), compare);
+    i = 0;
+    while (files[i])
     {
-       f = fopen (*argv, "r");
+       f = scanopen (files[i]);
        if (!f)
-           fatal (*argv, 0, strerror (errno));
-       sets[i] = scan (f, *argv);
-       names[i] = get_name (*argv);
+           fatal (files[i], 0, strerror (errno));
+       sets[i] = scan (f, files[i]);
+       names[i] = get_name (files[i]);
+       langs[i] = get_lang(names[i]);
+       if (strchr (langs[i], '-'))
+           country[ncountry++] = i;
+
        total_leaves += sets[i]->num;
        i++;
        fclose (f);
@@ -191,14 +295,13 @@ main (int argc, char **argv)
      */
     for (i = 0; sets[i]; i++)
     {
-       sleaves = sets[i]->leaves;
        for (sl = 0; sl < sets[i]->num; sl++)
        {
            for (l = 0; l < tl; l++)
-               if (leaves[l] == sleaves[sl])
+               if (leaves[l] == FcCharSetGetLeaf(sets[i], sl))
                    break;
            if (l == tl)
-               leaves[tl++] = sleaves[sl];
+               leaves[tl++] = FcCharSetGetLeaf(sets[i], sl);
        }
     }
 
@@ -218,7 +321,7 @@ main (int argc, char **argv)
     /*
      * Dump leaves
      */
-    printf ("static const FcCharLeaf   leaves[%d] = {\n", tl);
+    printf ("const FcCharLeaf  langBankLeaves[%d] = {\n", tl);
     for (l = 0; l < tl; l++)
     {
        printf ("    { { /* %d */", l);
@@ -231,7 +334,6 @@ main (int argc, char **argv)
        printf ("\n    } },\n");
     }
     printf ("};\n\n");
-    printf ("#define L(n) ((FcCharLeaf *) &leaves[n])\n\n");
 
     /*
      * Find duplicate charsets
@@ -250,6 +352,21 @@ main (int argc, char **argv)
            }
     }
 
+    /*
+     * Find ranges for each letter for faster searching
+     */
+    setRangeChar = 'a';
+    for (i = 0; sets[i]; i++)
+    {
+       char    c = names[i][0];
+       
+       while (setRangeChar <= c && c <= 'z')
+           setRangeStart[setRangeChar++ - 'a'] = i;
+    }
+    for (setRangeChar = 'a'; setRangeChar < 'z'; setRangeChar++)
+       setRangeEnd[setRangeChar - 'a'] = setRangeStart[setRangeChar+1-'a'] - 1;
+    setRangeEnd[setRangeChar - 'a'] = i - 1;
+    
     /*
      * Dump arrays
      */
@@ -259,58 +376,145 @@ main (int argc, char **argv)
        
        if (duplicate[i] >= 0)
            continue;
-       printf ("static const FcCharLeaf *leaves_%s[%d] = {\n",
-               names[i], sets[i]->num);
+
+       for (n = 0; n < sets[i]->num; n++)
+       {
+           for (l = 0; l < tl; l++)
+               if (leaves[l] == FcCharSetGetLeaf(sets[i], n))
+                   break;
+           if (l == tl)
+               fatal (names[i], 0, "can't find leaf");
+           offset_count++;
+       }
+       offsets[i] = offset;
+       offset += sets[i]->num;
+    }
+
+    printf ("const int langBankLeafIdx[%d] = {\n",
+           offset_count);
+    for (i = 0; sets[i]; i++)
+    {
+       int n;
+       
+       if (duplicate[i] >= 0)
+           continue;
        for (n = 0; n < sets[i]->num; n++)
        {
            if (n % 8 == 0)
                printf ("   ");
            for (l = 0; l < tl; l++)
-               if (leaves[l] == sets[i]->leaves[n])
+               if (leaves[l] == FcCharSetGetLeaf(sets[i], n))
                    break;
            if (l == tl)
                fatal (names[i], 0, "can't find leaf");
-           printf (" L(%3d),", l);
+           printf (" %3d,", l);
            if (n % 8 == 7)
                printf ("\n");
        }
        if (n % 8 != 0)
            printf ("\n");
-       printf ("};\n\n");
-       
+    }
+    printf ("};\n\n");
+
+    printf ("const FcChar16 langBankNumbers[%d] = {\n",
+           offset_count);
+
+    for (i = 0; sets[i]; i++)
+    {
+       int n;
 
-       printf ("static const FcChar16 numbers_%s[%d] = {\n",
-               names[i], sets[i]->num);
+       if (duplicate[i] >= 0)
+           continue;
        for (n = 0; n < sets[i]->num; n++)
        {
            if (n % 8 == 0)
                printf ("   ");
-           printf (" 0x%04x,", sets[i]->numbers[n]);
+           printf (" 0x%04x,", FcCharSetGetNumbers(sets[i])[n]);
            if (n % 8 == 7)
                printf ("\n");
        }
        if (n % 8 != 0)
            printf ("\n");
-       printf ("};\n\n");
     }
-    printf ("#undef L\n\n");
+    printf ("};\n\n");
+    
     /*
      * Dump sets
      */
-    printf ("static const FcLangCharSet  fcLangCharSets[] = {\n");
+
+    printf ("const FcLangCharSet  fcLangCharSets[] = {\n");
     for (i = 0; sets[i]; i++)
     {
        int     j = duplicate[i];
+
        if (j < 0)
            j = i;
+
        printf ("    { (FcChar8 *) \"%s\",\n"
-               "      { 1, FcTrue, %d, "
-               "(FcCharLeaf **) leaves_%s, "
-               "(FcChar16 *) numbers_%s } },\n",
-               get_lang(names[i]),
-               sets[j]->num, names[j], names[j]);
+               "      { FC_REF_CONSTANT, %d, FC_BANK_LANGS, "
+               "{ { %d, %d } } } }, /* %d */\n",
+               langs[i],
+               sets[j]->num, offsets[j], offsets[j], j);
+    }
+    printf ("};\n\n");
+    printf ("#define NUM_LANG_CHAR_SET %d\n", i);
+    num_lang_set_map = (i + 31) / 32;
+    printf ("#define NUM_LANG_SET_MAP  %d\n", num_lang_set_map);
+    /*
+     * Dump indices with country codes
+     */
+    if (ncountry)
+    {
+       int     c;
+       int     ncountry_ent = 0;
+       printf ("\n");
+       printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
+       for (c = 0; c < ncountry; c++)
+       {
+           i = country[c];
+           if (i >= 0)
+           {
+               int lang = strchr (langs[i], '-') - langs[i];
+               int d, k;
+
+               for (k = 0; k < num_lang_set_map; k++)
+                   map[k] = 0;
+
+               BitSet (map, i);
+               for (d = c + 1; d < ncountry; d++)
+               {
+                   int j = country[d];
+                   if (j >= 0 && !strncmp (langs[j], langs[i], l))
+                   {
+                       BitSet(map, j);
+                       country[d] = -1;
+                   }
+               }
+               printf ("    {");
+               for (k = 0; k < num_lang_set_map; k++)
+                   printf (" 0x%08x,", map[k]);
+               printf (" }, /* %*.*s */\n",
+                       lang, lang, langs[i]);
+               ++ncountry_ent;
+           }
+       }
+       printf ("};\n\n");
+       printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent);
+    }
+    
+
+    /*
+     * Dump sets start/finish for the fastpath
+     */
+    printf ("static const FcLangCharSetRange  fcLangCharSetRanges[] = {\n");
+    for (setRangeChar = 'a'; setRangeChar <= 'z' ; setRangeChar++)
+    {
+       printf ("    { %d, %d }, /* %c */\n",
+               setRangeStart[setRangeChar - 'a'],
+               setRangeEnd[setRangeChar - 'a'], setRangeChar);
     }
     printf ("};\n\n");
     while (fgets (line, sizeof (line), stdin))
        fputs (line, stdout);