~alcinnz/fontconfig-pure

fontconfig-pure/fontconfig-harfbuzz/app/Main.hs -rw-r--r-- 1.8 KiB
dfb515ef — Adrian Cochrane Segfault fixes. 1 year, 2 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
56
57
58
{-# LANGUAGE OverloadedStrings #-}
module Main where

import Graphics.UI.GLUT
import Graphics.GL.Core32

import FreeType.Core.Base
import FreeType.FontConfig (instantiatePattern, bmpAndMetricsForIndex, FTFC_Subpixel(..))

import Graphics.Text.Font.Choose as Font
import Typograffiti (makeDrawGlyphs, allocAtlas, AllocatedRendering(..), move)
import Linear (V2(..))

import Data.Text.Glyphize
import Data.Text.Glyphize.Choose

import System.Environment (getArgs)
import System.Exit (die)
import Control.Monad.Except (runExceptT, liftIO)
import Data.Maybe (fromMaybe)

main :: IO ()
main = do
    (progName, args) <- getArgsAndInitialize

    w <- createWindow progName

    args <- getArgs
    let query = nameParse $ case args of
            [] -> "serif"
            (q:_) -> q
    let query' = defaultSubstitute $ configSubstitute' query MatchPattern
    font <- case fontSort' query' False of
        Just (f:_, _) -> return f
        _ -> do
            die ("Failed to locate font " ++ show query)

    let buf = defaultBuffer { text = "sphinx of black quartz judge my vow" }
    let glyphs = shape (pattern2hbfont font []) buf []

    ft_With_FreeType $ \ft -> do
        inst <- instantiatePattern ft font (fromMaybe 12 $ getValue' "size" font, 20)
        res <- runExceptT $ do
            render <- makeDrawGlyphs
            let codepoints = map codepoint $ map fst $ glyphs
            atlas <- allocAtlas (liftIO . bmpAndMetricsForIndex inst SubpixelNone) codepoints
            renderer <- render atlas glyphs
            return renderer
        renderer <- case res of
            Left err -> die $ show err
            Right r -> return r

        displayCallback $= do
            clear [ ColorBuffer ]
            Size x y <- get windowSize
            arDraw renderer [move 0 50] $ V2 (fromEnum x) (fromEnum y)
            flush
        mainLoop