~alcinnz/CatTrap

ref: 7beb48286350645043875941093f076bca84e81b CatTrap/Graphics/Layout/CSS/Internal.hs -rw-r--r-- 2.7 KiB
7beb4828 — Adrian Cochrane Draft grid preprocessor which sucessfully compiles. 1 year, 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{-# LANGUAGE OverloadedStrings #-}
module Graphics.Layout.CSS.Internal where

import Data.CSS.Syntax.Tokens (Token(..), NumericValue(..))
import qualified Data.Text as Txt
import Data.Scientific (toRealFloat)
import Debug.Trace (trace) -- For warnings.

import Graphics.Layout.Box hiding (lowerLength)

type Unitted = (Double, Txt.Text)
auto :: Unitted
auto = (0,"auto")

parseLength :: [Token] -> Maybe Unitted
parseLength [Percentage _ x] = Just (n2f x,"%")
parseLength [Dimension _ x unit]
    | n2f x == 0 && unit == "" = Just (0,"px")
    | unit `elem` units = Just (n2f x,unit)
parseLength [Ident "auto"] = Just (0,"auto")
parseLength [Ident "initial"] = Just (0,"auto")
parseLength _ = Nothing
parseLength' [Ident "min-content"] = Just (0,"min-content")
parseLength' [Ident "max-content"] = Just (0,"max-content")
parseLength' toks = parseLength toks

units = Txt.words "cap ch em ex ic lh rem rlh vh vw vmax vmin vb vi px cm mm Q in pc pt %"

n2f (NVInteger x) = realToFrac x
n2f (NVNumber x) = toRealFloat x

lowerLength :: Unitted -> Font' -> Length
lowerLength (x,"cap") f = Pixels $ x*fontHeight f 'A'
lowerLength (x,"ch") f = Pixels $ x*fontAdvance f '0'
lowerLength (x,"em") f = Pixels $ x*fontSize f
lowerLength (x,"ex") f = Pixels $ x*fontHeight f 'x'
lowerLength (x,"ic") f = Pixels $ x*fontHeight f '水' -- CJK water ideograph
lowerLength (x,"lh") f = Pixels $ x*lineheight f -- Store conversion factors in `f`...
lowerLength (x,"rem") f = Pixels $ x*rootEm f
lowerLength (x,"rlh") f = Pixels $ x*rlh f
lowerLength (x,"vh") f = Pixels $ x*vh f
lowerLength (x,"vw") f = Pixels $ x*vw f
lowerLength (x,"vmax") f = Percent $ x*vmax f
lowerLength (x,"vmin") f = Percent $ x*vmin f
lowerLength (x,"vb") f = Percent $ x*vb f -- This'll be trickier to populate
lowerLength (x,"vi") f = Percent $ x*vi f -- This'll be trickier to populate
lowerLength (x,"px") f = Pixels $ x*scale f
lowerLength (x,"cm") f = Pixels $ x*scale f*96/2.54
lowerLength (x,"mm") f | Pixels x' <- lowerLength (x,"cm") f = Pixels $ x'/10
lowerLength (x,"Q") f | Pixels x' <- lowerLength (x,"cm") f = Pixels $ x'/40
lowerLength (x,"pc") f | Pixels x' <- lowerLength (x,"in") f = Pixels $ x'/6
lowerLength (x,"pt") f | Pixels x' <- lowerLength (x,"in") f = Pixels $ x'/72
lowerLength (x,"%") _ = Percent $ x/100
lowerLength (_,"auto") _ = Auto
lowerLength (_,unit) _ = trace ("Invalid unit " ++ Txt.unpack unit) $ Pixels 0

data Font' = Font' {
    fontHeight :: Char -> Double,
    fontAdvance :: Char -> Double,
    fontSize :: Double,
    rootEm :: Double,
    lineheight :: Double,
    rlh :: Double,
    vh :: Double,
    vw :: Double,
    vmax :: Double,
    vmin :: Double,
    vb :: Double,
    vi :: Double,
    scale :: Double
}