~alcinnz/Mondrian

b49146ea31faeb0a52b374e202495b1c1571180a — Adrian Cochrane 10 months ago 483226a
Parse radial gradient center-position.
M lib/Graphics/Rendering/Rect.hs => lib/Graphics/Rendering/Rect.hs +1 -1
@@ 37,7 37,7 @@ styleResolveImages atlas self =
    atlasLookup' None = None
    atlasLookup' (Img path) = Img $ atlasLookup path atlas
    atlasLookup' (Linear a b) = Linear a b
    atlasLookup' (Radial a b) = Radial a b
    atlasLookup' (Radial a b cc) = Radial a b cc

atlasFromStyles :: MonadIO m =>
        (Text -> IO DynamicImage) -> [RectStyle Text] -> m Atlas

M lib/Graphics/Rendering/Rect/Backgrounds.hs => lib/Graphics/Rendering/Rect/Backgrounds.hs +13 -4
@@ 145,12 145,14 @@ renderBackgrounds = do
                    u $ v2 $ size', u angle, cs 10 $ map fst stops,
                    us $ ls2fs size' $ map snd $ take 10 stops, u $ length stops
                ] clip0 a b
            Radial Ellipse stops -> let sz@(w,h) = size $ clip0 a in ellipse [] [
                    u $ v2 sz, u $ v2 (w/2, h/2), cs 10 $ map fst stops,
            Radial Ellipse org stops ->
                let sz@(_,h) = size $ clip0 a in ellipse [] [
                    u $ v2 sz, u $ v2 $ l2f' org sz, cs 10 $ map fst stops,
                    us $ ls2fs (0,h/2) $ map snd $ take 10 stops, u $ length stops
                ] clip0 a b
            Radial Circle stops -> let (w,h) = size $ clip0 a in circle [] [
                    u $ v2 (w/2, h/2), u (min w h/2), cs 10 $ map fst stops,
            Radial Circle org stops ->
                let sz@(w,h) = size $ clip0 a in circle [] [
                    u $ v2 $ l2f' org sz, u (min w h/2), cs 10 $ map fst stops,
                    us $ ls2fs (0,min w h/2) $ map snd $ take 10 stops,
                    u $ length stops
                ] clip0 a b


@@ 187,3 189,10 @@ ls2fs (_,h) ls = resolveAutos 0 $ inner True 0 ls
        next | (x:_) <- fs = x
            | otherwise = 1 -- Step 1 should've taken care of this...
        grad = (next - prev)/(n + 1)

l2f :: Length -> Float -> Float
l2f Auto x = x/2
l2f (Scale x) y = x*y
l2f (Absolute x) _ = x
l2f' :: (Length, Length) -> (Float, Float) -> (Float, Float)
l2f' (w,h) (x,y) = (l2f w x, l2f h y)

M lib/Graphics/Rendering/Rect/CSS/Backgrounds.hs => lib/Graphics/Rendering/Rect/CSS/Backgrounds.hs +31 -6
@@ 24,7 24,7 @@ data Backgrounds img = Backgrounds {
type C = AlphaColour Float

data Pattern img = None | Img img | Linear Float [(C, Length)]
    | Radial RadialShape [(C, Length)] deriving (Eq, Show, Read)
    | Radial RadialShape (Length, Length) [(C, Length)] deriving (Eq, Show, Read)
data RadialShape = Circle | Ellipse deriving (Eq, Show, Read)

-- We need to resolve images before we can compute the actual lengths!


@@ 79,13 79,38 @@ instance PropertyParser (Backgrounds Text) where
                Just cs@(_:_:_) <- colourStops pp toks = Just $ Linear angle cs
        inner (Function "radial-gradient":toks)
            | Just cs@(_:_:_) <- colourStops pp (Comma:toks) =
                Just $ Radial Ellipse cs
            | Just (shp, ts) <- radArgs toks, Just cs@(_:_:_) <- colourStops pp ts
                = Just $ Radial shp cs
                Just $ Radial Ellipse center cs
            | Just (shp, org, ts) <- radArgs toks,
                Just cs@(_:_:_) <- colourStops pp ts = Just $ Radial shp org cs
          where
            radArgs (Ident "circle":ts) = Just (Circle, ts)
            radArgs (Ident "ellipse":ts) = Just (Ellipse, ts)
            center = (Scale 0.5, Scale 0.5)
            radArgs ts | (ts', Ident "at":posStops) <- break (== Ident "at") ts,
                    Just (shape, _, []) <- radArgs ts',
                    Just (org, stops) <- position posStops =
                Just (shape, org, stops)
            radArgs (Ident "circle":ts) = Just (Circle, center, ts)
            radArgs (Ident "ellipse":ts) = Just (Ellipse, center, ts)
            radArgs _ = Nothing
            position (x:y:ts) = position' x y ts *> position' y x ts
            position _ = Nothing
            position' x y ts = case ((case x of
                    Ident "left" -> Scale 0
                    Ident "center" -> Scale 0.5
                    Ident "right" -> Scale 1
                    Percentage _ a -> Scale $ p a
                    Dimension _ a "px" -> Absolute $ f a
                    _ -> Auto,
                case y of
                    Ident "top" -> Scale 0
                    Ident "center" -> Scale 0.5
                    Ident "right" -> Scale 1
                    Percentage _ a -> Scale $ p a
                    Dimension _ a "px" -> Absolute $ f a
                    _ -> Auto),
                ts) of
                    ((Auto, _), _) -> Nothing
                    ((_, Auto), _) -> Nothing
                    ret -> Just ret
        inner _ = Nothing
    longhand _ self "background-size" t | val@(_:_) <- parseCSSList inner t =
        Just self { bgSize = reverse val }