~alcinnz/fontconfig-pure

ref: dfce3326f7f5b2367df56675e92742b3d5f7fbc3 fontconfig-pure/lib/Graphics/Text/Font/Choose/Config.hs -rw-r--r-- 3.0 KiB
dfce3326 — Adrian Cochrane Test @font-face parsing. 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{-# LANGUAGE CApiFFI #-}
-- | Load system fonts configuration.
module Graphics.Text.Font.Choose.Config(Config, fini, version,
        initLoadConfig, initLoadConfigAndFonts, initFonts, reinit, bringUptoDate,
        -- For the sake of Graphics.Font.Choose.Config.Accessors
        Config', fcConfigDestroy) where

import Foreign.Ptr (Ptr, FunPtr)
import Foreign.ForeignPtr (ForeignPtr, newForeignPtr)

import Graphics.Text.Font.Choose.Result (throwBool, throwNull)

-- | Internal placeholder underlying `Config`.
data Config'
-- | holds the internal representation of a configuration.
type Config = ForeignPtr Config'


-- | Loads the default configuration file and returns the resulting configuration. Does not load any font information.
initLoadConfig :: IO Config
initLoadConfig = newForeignPtr fcConfigDestroy =<< throwNull =<< fcInitLoadConfig -- FIXME: What's proper memory-management here?

-- | Loads the default configuration file and builds information about the available fonts. Returns the resulting configuration.
initLoadConfigAndFonts :: IO Config
initLoadConfigAndFonts = newForeignPtr fcConfigDestroy =<< throwNull =<< fcInitLoadConfigAndFonts -- FIXME: What's proper memory-management here?

-- | Loads the default configuration file and the fonts referenced therein and sets the default configuration to that result.
-- Returns whether this process succeeded or not. If the default configuration has already been loaded, this routine does nothing and returns True.
initFonts :: IO ()
initFonts = throwBool =<< fcInit
foreign import capi "fontconfig/fontconfig.h FcFini" fini :: IO ()

-- | Returns the version number of the library.
foreign import capi "fontconfig/fontconfig.h FcGetVersion" version :: Int

-- | Forces the default configuration file to be reloaded and resets the default configuration.
-- Returns False if the configuration cannot be reloaded (due to configuration file errors,
-- allocation failures or other issues) and leaves the existing configuration unchanged. Otherwise returns True.
reinit :: IO ()
reinit = throwBool =<< fcInitReinitialize

-- | Checks the rescan interval in the default configuration, checking the configuration
-- if the interval has passed and reloading the configuration if when any changes are detected.
-- Returns False if the configuration cannot be reloaded (see `reinit`). Otherwise returns True.
bringUptoDate :: IO ()
bringUptoDate = throwBool =<< fcInitBringUptoDate

foreign import capi "fontconfig/fontconfig.h FcInitLoadConfig" fcInitLoadConfig :: IO (Ptr Config')
foreign import capi "fontconfig/fontconfig.h FcInitLoadConfigAndFonts" fcInitLoadConfigAndFonts :: IO (Ptr Config')
foreign import capi "fontconfig/fontconfig.h FcInit" fcInit :: IO Bool
-- | Internal ForeignPtr destructor for `Config`.
foreign import capi "fontconfig/fontconfig.h &FcConfigDestroy" fcConfigDestroy :: FunPtr (Ptr Config' -> IO ())

foreign import capi "fontconfig/fontconfig.h FcInitReinitialize" fcInitReinitialize :: IO Bool
foreign import capi "fontconfig/fontconfig.h FcInitBringUptoDate" fcInitBringUptoDate :: IO Bool