~alcinnz/fontconfig-pure

ref: a16ba32a067ae0025cc83c69377be991682cd927 fontconfig-pure/Graphics/Text/Font/Choose/FontSet/API.hs -rw-r--r-- 4.4 KiB
a16ba32a — Adrian Cochrane Document how to contribute. 1 year, 10 months ago
                                                                                
9e1e5029 Adrian Cochrane
ce6932f3 Adrian Cochrane
9e1e5029 Adrian Cochrane
ce6932f3 Adrian Cochrane
9e1e5029 Adrian Cochrane
ce6932f3 Adrian Cochrane
9e1e5029 Adrian Cochrane
ce6932f3 Adrian Cochrane
9e1e5029 Adrian Cochrane
ce6932f3 Adrian Cochrane
9e1e5029 Adrian Cochrane
ce6932f3 Adrian Cochrane
9e1e5029 Adrian Cochrane
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
-- Here to break recursive imports...
module Graphics.Text.Font.Choose.FontSet.API where

import Graphics.Text.Font.Choose.FontSet
import Graphics.Text.Font.Choose.Pattern
import Graphics.Text.Font.Choose.Config
import Graphics.Text.Font.Choose.ObjectSet
import Graphics.Text.Font.Choose.CharSet
import Graphics.Text.Font.Choose.Result (throwPtr)

import Foreign.Ptr (Ptr, castPtr, nullPtr)
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Marshal.Alloc (alloca)
import System.IO.Unsafe (unsafePerformIO)

-- | Selects fonts matching pattern from sets,
-- creates patterns from those fonts containing only the objects in object_set
-- and returns the set of unique such patterns.
fontSetList :: Config -> [FontSet] -> Pattern -> ObjectSet -> FontSet
fontSetList config fontss pattern objs = unsafePerformIO $ withForeignPtr config $ \config' ->
    withFontSets fontss $ \fontss' n -> withPattern pattern $ \pattern' ->
        withObjectSet objs $ \objs' ->
            thawFontSet_ $ fcFontSetList config' fontss' n pattern' objs'
-- | Variant of `fontSetList` operating upon register default `Config`.
fontSetList' :: [FontSet] -> Pattern -> ObjectSet -> FontSet
fontSetList' fontss pattern objs = unsafePerformIO $ withFontSets fontss $ \fontss' n ->
    withPattern pattern $ \pattern' -> withObjectSet objs $ \objs' ->
        thawFontSet_ $ fcFontSetList nullPtr fontss' n pattern' objs'
foreign import ccall "FcFontSetList" fcFontSetList ::
    Config_ -> Ptr FontSet_ -> Int -> Pattern_ -> ObjectSet_ -> IO FontSet_

-- | Finds the font in sets most closely matching pattern
-- and returns the result of `fontRenderPrepare` for that font and
-- the provided pattern. This function should be called only after
-- `configSubstitute` and `defaultSubstitute` have been called for pattern;
-- otherwise the results will not be correct.
fontSetMatch :: Config -> [FontSet] -> Pattern -> Maybe Pattern
fontSetMatch config fontss pattern = unsafePerformIO $ withForeignPtr config $ \config' ->
    withFontSets fontss $ \fontss' n -> withPattern pattern $ \pattern' -> 
        alloca $ \res' -> do
            ret <- fcFontSetMatch config' fontss' n pattern' res'
            throwPtr res' $ thawPattern_ $ pure ret
-- | Variant of `fontSetMatch` operating upon registered default `Config`.
fontSetMatch' :: [FontSet] -> Pattern -> Maybe Pattern
fontSetMatch' fontss pattern = unsafePerformIO $ withFontSets fontss $ \fontss' n ->
    withPattern pattern $ \pattern' -> alloca $ \res' -> do
        ret <- fcFontSetMatch nullPtr fontss' n pattern' res'
        throwPtr res' $ thawPattern_ $ pure ret
foreign import ccall "FcFontSetMatch" fcFontSetMatch ::
    Config_ -> Ptr FontSet_ -> Int -> Pattern_ -> Ptr Int -> IO Pattern_

-- | Returns the list of fonts from sets sorted by closeness to pattern.
-- If trim is `True`, elements in the list which don't include Unicode coverage
-- not provided by earlier elements in the list are elided.
-- The union of Unicode coverage of all of the fonts is returned in csp.
-- This function should be called only after `configSubstitute` and
-- `defaultSubstitute` have been called for p;
-- otherwise the results will not be correct.
-- The returned FcFontSet references `Pattern` structures which may be shared by
-- the return value from multiple `fontSort` calls, applications cannot modify
-- these patterns. Instead, they should be passed, along with pattern to
-- `fontRenderPrepare` which combines them into a complete pattern.
fontSetSort :: Config -> [FontSet] -> Pattern -> Bool -> CharSet -> Maybe FontSet
fontSetSort config fontss pattern trim csp = unsafePerformIO $
    withForeignPtr config $ \config' -> withFontSets fontss $ \fontss' n ->
        withPattern pattern $ \pattern' -> withCharSet csp $ \csp' -> alloca $ \res' -> do
            ret' <- fcFontSetSort config' fontss' n pattern' trim csp' res'
            throwPtr res' $ thawFontSet_ $ pure ret'
-- | Variant of `fontSetSort` operating upon registered default `Config`.
fontSetSort' :: [FontSet] -> Pattern -> Bool -> CharSet -> Maybe FontSet
fontSetSort' fontss pattern trim csp = unsafePerformIO $
    withFontSets fontss $ \fontss' n -> withPattern pattern $ \pattern' ->
        withCharSet csp $ \csp' -> alloca $ \res' -> do
            ret' <- fcFontSetSort nullPtr fontss' n pattern' trim csp' res'
            throwPtr res' $ thawFontSet_ $ pure ret'
foreign import ccall "FcFontSetSort" fcFontSetSort :: Config_ -> Ptr FontSet_
    -> Int -> Pattern_ -> Bool -> CharSet_ -> Ptr Int -> IO FontSet_