fcinit.c revision ca08ab68
12c393a42Smrg/*
2a6844aabSmrg * fontconfig/src/fcinit.c
32c393a42Smrg *
42c393a42Smrg * Copyright © 2001 Keith Packard
52c393a42Smrg *
62c393a42Smrg * Permission to use, copy, modify, distribute, and sell this software and its
72c393a42Smrg * documentation for any purpose is hereby granted without fee, provided that
82c393a42Smrg * the above copyright notice appear in all copies and that both that
92c393a42Smrg * copyright notice and this permission notice appear in supporting
10ca08ab68Smrg * documentation, and that the name of the author(s) not be used in
112c393a42Smrg * advertising or publicity pertaining to distribution of the software without
12ca08ab68Smrg * specific, written prior permission.  The authors make no
132c393a42Smrg * representations about the suitability of this software for any purpose.  It
142c393a42Smrg * is provided "as is" without express or implied warranty.
152c393a42Smrg *
16a6844aabSmrg * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
172c393a42Smrg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18a6844aabSmrg * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
192c393a42Smrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
202c393a42Smrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
212c393a42Smrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
222c393a42Smrg * PERFORMANCE OF THIS SOFTWARE.
232c393a42Smrg */
242c393a42Smrg
252c393a42Smrg#include "fcint.h"
262c393a42Smrg#include <stdlib.h>
272c393a42Smrg
282c393a42Smrgstatic FcConfig *
292c393a42SmrgFcInitFallbackConfig (void)
302c393a42Smrg{
312c393a42Smrg    FcConfig	*config;
322c393a42Smrg
332c393a42Smrg    config = FcConfigCreate ();
342c393a42Smrg    if (!config)
352c393a42Smrg	goto bail0;
362c393a42Smrg    if (!FcConfigAddDir (config, (FcChar8 *) FC_DEFAULT_FONTS))
372c393a42Smrg	goto bail1;
382c393a42Smrg    if (!FcConfigAddCacheDir (config, (FcChar8 *) FC_CACHEDIR))
392c393a42Smrg	goto bail1;
402c393a42Smrg    return config;
412c393a42Smrg
422c393a42Smrgbail1:
432c393a42Smrg    FcConfigDestroy (config);
442c393a42Smrgbail0:
452c393a42Smrg    return 0;
462c393a42Smrg}
472c393a42Smrg
482c393a42Smrgint
492c393a42SmrgFcGetVersion (void)
502c393a42Smrg{
512c393a42Smrg    return FC_VERSION;
522c393a42Smrg}
532c393a42Smrg
542c393a42Smrg/*
552c393a42Smrg * Load the configuration files
562c393a42Smrg */
572c393a42SmrgFcConfig *
582c393a42SmrgFcInitLoadConfig (void)
592c393a42Smrg{
602c393a42Smrg    FcConfig	*config;
61ca08ab68Smrg
622c393a42Smrg    FcInitDebug ();
632c393a42Smrg    config = FcConfigCreate ();
642c393a42Smrg    if (!config)
65ca08ab68Smrg	return NULL;
66ca08ab68Smrg
672c393a42Smrg    if (!FcConfigParseAndLoad (config, 0, FcTrue))
682c393a42Smrg    {
692c393a42Smrg	FcConfigDestroy (config);
702c393a42Smrg	return FcInitFallbackConfig ();
712c393a42Smrg    }
72ca08ab68Smrg
732c393a42Smrg    if (config->cacheDirs && config->cacheDirs->num == 0)
742c393a42Smrg    {
75ca08ab68Smrg	FcChar8 *prefix;
76ca08ab68Smrg	size_t plen;
77ca08ab68Smrg
782c393a42Smrg	fprintf (stderr,
792c393a42Smrg		 "Fontconfig warning: no <cachedir> elements found. Check configuration.\n");
802c393a42Smrg	fprintf (stderr,
812c393a42Smrg		 "Fontconfig warning: adding <cachedir>%s</cachedir>\n",
822c393a42Smrg		 FC_CACHEDIR);
83ca08ab68Smrg	prefix = FcConfigXdgCacheHome ();
84ca08ab68Smrg	plen = prefix ? strlen ((const char *)prefix) : 0;
85ca08ab68Smrg	if (!prefix)
86ca08ab68Smrg	    goto bail;
87ca08ab68Smrg	prefix = realloc (prefix, plen + 12);
88ca08ab68Smrg	if (!prefix)
89ca08ab68Smrg	    goto bail;
90ca08ab68Smrg	memcpy (&prefix[plen], FC_DIR_SEPARATOR_S "fontconfig", 11);
91ca08ab68Smrg	prefix[plen + 11] = 0;
922c393a42Smrg	fprintf (stderr,
93ca08ab68Smrg		 "Fontconfig warning: adding <cachedir prefix=\"xdg\">fontconfig</cachedir>\n");
94ca08ab68Smrg
952c393a42Smrg	if (!FcConfigAddCacheDir (config, (FcChar8 *) FC_CACHEDIR) ||
96ca08ab68Smrg	    !FcConfigAddCacheDir (config, (FcChar8 *) prefix))
972c393a42Smrg	{
98ca08ab68Smrg	  bail:
992c393a42Smrg	    fprintf (stderr,
1002c393a42Smrg		     "Fontconfig error: out of memory");
101ca08ab68Smrg	    free (prefix);
1022c393a42Smrg	    FcConfigDestroy (config);
1032c393a42Smrg	    return FcInitFallbackConfig ();
1042c393a42Smrg	}
105ca08ab68Smrg	free (prefix);
1062c393a42Smrg    }
1072c393a42Smrg
1082c393a42Smrg    return config;
1092c393a42Smrg}
1102c393a42Smrg
1112c393a42Smrg/*
1122c393a42Smrg * Load the configuration files and scan for available fonts
1132c393a42Smrg */
1142c393a42SmrgFcConfig *
1152c393a42SmrgFcInitLoadConfigAndFonts (void)
1162c393a42Smrg{
1172c393a42Smrg    FcConfig	*config = FcInitLoadConfig ();
1182c393a42Smrg
1192c393a42Smrg    FcInitDebug ();
1202c393a42Smrg    if (!config)
1212c393a42Smrg	return 0;
1222c393a42Smrg    if (!FcConfigBuildFonts (config))
1232c393a42Smrg    {
1242c393a42Smrg	FcConfigDestroy (config);
1252c393a42Smrg	return 0;
1262c393a42Smrg    }
1272c393a42Smrg    return config;
1282c393a42Smrg}
1292c393a42Smrg
1302c393a42Smrg/*
1312c393a42Smrg * Initialize the default library configuration
1322c393a42Smrg */
1332c393a42SmrgFcBool
1342c393a42SmrgFcInit (void)
1352c393a42Smrg{
1362c393a42Smrg    FcConfig	*config;
1372c393a42Smrg
1382c393a42Smrg    if (_fcConfig)
1392c393a42Smrg	return FcTrue;
1402c393a42Smrg    config = FcInitLoadConfigAndFonts ();
1412c393a42Smrg    if (!config)
1422c393a42Smrg	return FcFalse;
1432c393a42Smrg    FcConfigSetCurrent (config);
1442c393a42Smrg    if (FcDebug() & FC_DBG_MEMORY)
1452c393a42Smrg	FcMemReport ();
1462c393a42Smrg    return FcTrue;
1472c393a42Smrg}
1482c393a42Smrg
1492c393a42Smrg/*
1502c393a42Smrg * Free all library-allocated data structures.
1512c393a42Smrg */
1522c393a42Smrgvoid
1532c393a42SmrgFcFini (void)
1542c393a42Smrg{
1552c393a42Smrg    if (_fcConfig)
1562c393a42Smrg	FcConfigDestroy (_fcConfig);
1572c393a42Smrg
158ca08ab68Smrg    FcObjectFini ();
1592c393a42Smrg    FcCacheFini ();
1602c393a42Smrg    if (FcDebug() & FC_DBG_MEMORY)
1612c393a42Smrg	FcMemReport ();
1622c393a42Smrg}
1632c393a42Smrg
1642c393a42Smrg/*
1652c393a42Smrg * Reread the configuration and available font lists
1662c393a42Smrg */
1672c393a42SmrgFcBool
1682c393a42SmrgFcInitReinitialize (void)
1692c393a42Smrg{
1702c393a42Smrg    FcConfig	*config;
1712c393a42Smrg
1722c393a42Smrg    config = FcInitLoadConfigAndFonts ();
1732c393a42Smrg    if (!config)
1742c393a42Smrg	return FcFalse;
1752c393a42Smrg    FcConfigSetCurrent (config);
1762c393a42Smrg    return FcTrue;
1772c393a42Smrg}
1782c393a42Smrg
1792c393a42SmrgFcBool
1802c393a42SmrgFcInitBringUptoDate (void)
1812c393a42Smrg{
1822c393a42Smrg    FcConfig	*config = FcConfigGetCurrent ();
1832c393a42Smrg    time_t	now;
1842c393a42Smrg
1852c393a42Smrg    /*
1862c393a42Smrg     * rescanInterval == 0 disables automatic up to date
1872c393a42Smrg     */
1882c393a42Smrg    if (config->rescanInterval == 0)
1892c393a42Smrg	return FcTrue;
1902c393a42Smrg    /*
1912c393a42Smrg     * Check no more often than rescanInterval seconds
1922c393a42Smrg     */
1932c393a42Smrg    now = time (0);
1942c393a42Smrg    if (config->rescanTime + config->rescanInterval - now > 0)
1952c393a42Smrg	return FcTrue;
1962c393a42Smrg    /*
1972c393a42Smrg     * If up to date, don't reload configuration
1982c393a42Smrg     */
1992c393a42Smrg    if (FcConfigUptoDate (0))
2002c393a42Smrg	return FcTrue;
2012c393a42Smrg    return FcInitReinitialize ();
2022c393a42Smrg}
2032c393a42Smrg
2042c393a42Smrgstatic struct {
2052c393a42Smrg    char    name[16];
2062c393a42Smrg    int	    alloc_count;
2072c393a42Smrg    int	    alloc_mem;
2082c393a42Smrg    int	    free_count;
2092c393a42Smrg    int	    free_mem;
2102c393a42Smrg} FcInUse[FC_MEM_NUM] = {
2112c393a42Smrg    { "charset" },
2122c393a42Smrg    { "charleaf" },
2132c393a42Smrg    { "fontset" },
2142c393a42Smrg    { "fontptr" },
2152c393a42Smrg    { "objectset" },
2162c393a42Smrg    { "objectptr" },
2172c393a42Smrg    { "matrix" },
2182c393a42Smrg    { "pattern" },
2192c393a42Smrg    { "patelt" },
2202c393a42Smrg    { "vallist" },
2212c393a42Smrg    { "substate" },
2222c393a42Smrg    { "string" },
2232c393a42Smrg    { "listbuck" },
2242c393a42Smrg    { "strset" },
2252c393a42Smrg    { "strlist" },
2262c393a42Smrg    { "config" },
2272c393a42Smrg    { "langset" },
2282c393a42Smrg    { "atomic" },
2292c393a42Smrg    { "blanks" },
2302c393a42Smrg    { "cache" },
2312c393a42Smrg    { "strbuf" },
2322c393a42Smrg    { "subst" },
2332c393a42Smrg    { "objecttype" },
2342c393a42Smrg    { "constant" },
2352c393a42Smrg    { "test" },
2362c393a42Smrg    { "expr" },
2372c393a42Smrg    { "vstack" },
2382c393a42Smrg    { "attr" },
2392c393a42Smrg    { "pstack" },
240ca08ab68Smrg    { "sharedstr" },
2412c393a42Smrg};
2422c393a42Smrg
2432c393a42Smrgstatic int  FcAllocCount, FcAllocMem;
2442c393a42Smrgstatic int  FcFreeCount, FcFreeMem;
2452c393a42Smrg
2462c393a42Smrgstatic int  FcMemNotice = 1*1024*1024;
2472c393a42Smrg
2482c393a42Smrgstatic int  FcAllocNotify, FcFreeNotify;
2492c393a42Smrg
2502c393a42Smrgvoid
2512c393a42SmrgFcMemReport (void)
2522c393a42Smrg{
2532c393a42Smrg    int	i;
2542c393a42Smrg    printf ("Fc Memory Usage:\n");
2552c393a42Smrg    printf ("\t   Which       Alloc           Free           Active\n");
2562c393a42Smrg    printf ("\t           count   bytes   count   bytes   count   bytes\n");
2572c393a42Smrg    for (i = 0; i < FC_MEM_NUM; i++)
2582c393a42Smrg	printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
2592c393a42Smrg		FcInUse[i].name,
2602c393a42Smrg		FcInUse[i].alloc_count, FcInUse[i].alloc_mem,
2612c393a42Smrg		FcInUse[i].free_count, FcInUse[i].free_mem,
2622c393a42Smrg		FcInUse[i].alloc_count - FcInUse[i].free_count,
2632c393a42Smrg		FcInUse[i].alloc_mem - FcInUse[i].free_mem);
2642c393a42Smrg    printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
2652c393a42Smrg	    "Total",
2662c393a42Smrg	    FcAllocCount, FcAllocMem,
2672c393a42Smrg	    FcFreeCount, FcFreeMem,
2682c393a42Smrg	    FcAllocCount - FcFreeCount,
2692c393a42Smrg	    FcAllocMem - FcFreeMem);
2702c393a42Smrg    FcAllocNotify = 0;
2712c393a42Smrg    FcFreeNotify = 0;
2722c393a42Smrg}
2732c393a42Smrg
2742c393a42Smrgvoid
2752c393a42SmrgFcMemAlloc (int kind, int size)
2762c393a42Smrg{
2772c393a42Smrg    if (FcDebug() & FC_DBG_MEMORY)
2782c393a42Smrg    {
2792c393a42Smrg	FcInUse[kind].alloc_count++;
2802c393a42Smrg	FcInUse[kind].alloc_mem += size;
2812c393a42Smrg	FcAllocCount++;
2822c393a42Smrg	FcAllocMem += size;
2832c393a42Smrg	FcAllocNotify += size;
2842c393a42Smrg	if (FcAllocNotify > FcMemNotice)
2852c393a42Smrg	    FcMemReport ();
2862c393a42Smrg    }
2872c393a42Smrg}
2882c393a42Smrg
2892c393a42Smrgvoid
2902c393a42SmrgFcMemFree (int kind, int size)
2912c393a42Smrg{
2922c393a42Smrg    if (FcDebug() & FC_DBG_MEMORY)
2932c393a42Smrg    {
2942c393a42Smrg	FcInUse[kind].free_count++;
2952c393a42Smrg	FcInUse[kind].free_mem += size;
2962c393a42Smrg	FcFreeCount++;
2972c393a42Smrg	FcFreeMem += size;
2982c393a42Smrg	FcFreeNotify += size;
2992c393a42Smrg	if (FcFreeNotify > FcMemNotice)
3002c393a42Smrg	    FcMemReport ();
3012c393a42Smrg    }
3022c393a42Smrg}
3032c393a42Smrg#define __fcinit__
3042c393a42Smrg#include "fcaliastail.h"
3052c393a42Smrg#undef __fcinit__
306