~alcinnz/CatTrap

ref: 9c371dfe4e96a2134e36a583c792ca31b08d3ade CatTrap/app/Integration2.hs -rw-r--r-- 2.3 KiB
9c371dfe — Adrian Cochrane Give up on addressing all memory leaks. 1 year, 22 days 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
59
60
61
62
63
64
65
66
67
68
69
module Main where

import Text.HTML.DOM as HTML
import Text.XML as X
import Data.HTML2CSS (html2css, el2stylist)
import Network.URI (nullURI)

import Data.CSS.Preprocessor.Conditions as CSSCond
import qualified Data.CSS.Preprocessor.PsuedoClasses as CSSPseudo
import qualified Data.CSS.Style as Style
import Stylist.Tree (StyleTree(..), preorder, treeMap)
import qualified Data.CSS.Preprocessor.Text as CSSTxt
import Data.Maybe (fromMaybe)

import Graphics.Layout.CSS.Font (placeholderFont)
import Graphics.Layout.CSS (finalizeCSS', CSSBox)
import Graphics.Layout (LayoutItem, boxLayout)
import Graphics.Layout.Box (Length, Size(..), PaddedBox(..), zeroBox)

import Control.Exception (evaluate)
import qualified Graphics.Text.Font.Choose as FC

import Control.Concurrent.MVar (putMVar, newEmptyMVar, readMVar)
import Control.Concurrent (forkIO)
import Control.DeepSeq (NFData(..), ($!!))
--import System.Mem (performGC)

resolve' = CSSCond.resolve lowerVars lowerToks
lowerVars _ = CSSCond.B False
lowerToks _ = CSSCond.B False

stylize' style = preorder inner
  where
    inner parent _ el = Style.cascade style el [] $
            Style.inherit $ fromMaybe Style.temp parent

main :: IO ()
main = do
    FC.init
    doc <- HTML.readFile "test.html"
    let css' :: CSSCond.ConditionalStyles (Style.VarParser (CSSTxt.TextStyle
                (CSSBox Nil)))
        css' = html2css doc nullURI $ CSSCond.conditionalStyles nullURI "temp"
    css' `seq` print "Parsed page with CSS!"
    let pseudoFilter = CSSPseudo.htmlPsuedoFilter Style.queryableStyleSheet
    let css = CSSPseudo.inner $ resolve' pseudoFilter css'
    let styles = CSSTxt.resolve $ treeMap Style.innerParser $
            stylize' css $ el2stylist $ X.documentRoot doc
    styles `seq` print "Styled page!"
    let layout :: LayoutItem Length Length Nil
        layout = finalizeCSS' placeholderFont styles
    layout `seq` print "Laying out page!"
    res <- forkCompute $ boxLayout zeroBox { size = Size 1280 480 } layout False
    readMVar res
    --performGC
    --FC.fini -- FIXME: GC still left FontConfig references...
    return ()

data Nil = Nil deriving Eq
instance Style.PropertyParser Nil where
    temp = Nil
    inherit _ = Nil
    longhand _ _ _ _ = Nothing
instance NFData Nil where rnf Nil = ()

forkCompute dat = do
    ret <- newEmptyMVar
    forkIO $ putMVar ret $!! dat
    return ret