~alcinnz/Mondrian

ec7d175bb6a8a59addc03e21219a6128d61c4f9b — Adrian Cochrane 1 year, 6 months ago bd4c880
Parse radial gradient extents, slightly differs for circles vs ellipse.
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 cc) = Radial a b cc
    atlasLookup' (Radial a b cc d) = Radial a b cc d

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

M lib/Graphics/Rendering/Rect/Backgrounds.hs => lib/Graphics/Rendering/Rect/Backgrounds.hs +2 -2
@@ 145,12 145,12 @@ 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 org 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 org 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,

M lib/Graphics/Rendering/Rect/CSS/Backgrounds.hs => lib/Graphics/Rendering/Rect/CSS/Backgrounds.hs +68 -15
@@ 24,8 24,11 @@ data Backgrounds img = Backgrounds {
type C = AlphaColour Float

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

-- We need to resolve images before we can compute the actual lengths!
data Resize = Cover | Contain | Size Length Length deriving (Eq, Show, Read)


@@ 79,33 82,83 @@ 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 center cs
            | Just (shp, org, ts) <- radArgs toks,
                Just cs@(_:_:_) <- colourStops pp ts = Just $ Radial shp org cs
                Just $ Radial Ellipse FarthestCorner center cs
            | (shp, org, ext, ts) <- radArgs toks,
                Just cs@(_:_:_) <- colourStops pp ts = Just $ Radial shp ext org cs
          where
            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
            radArgs (Ident s:Ident "at":ts) | Just shape <- radShape s,
                    Just (org, ts') <- position ts =
                (shape, org, FarthestCorner, ts')
            radArgs (Ident "at":ts) | Just (org, ts') <- position ts =
                (Ellipse, org, FarthestCorner, ts')
            radArgs (Ident "circle":Ident "at":ts)
                | Just (org, ts') <- position ts = (Circle,org,FarthestCorner,ts')
            radArgs (Ident "circle":ts)
                | Just (_, Ident "at":ts') <- circleExt ts,
                        Just (org, stops) <- position ts' =
                    (Circle, org, FarthestCorner, stops)
                | Just (_,ts')<-circleExt ts=(Circle,center,FarthestCorner,ts')
                | otherwise = (Circle, center, FarthestCorner, ts)
            radArgs (Ident "ellipse":Ident "at":ts)
                | Just (org,ts')<-position ts=(Ellipse,org,FarthestCorner,ts')
            radArgs (Ident "ellipse":ts)
                | Just (_, Ident "at":ts') <- ellipseExt ts,
                        Just (org, stops) <- position ts' =
                    (Ellipse, org, FarthestCorner, stops)
                | Just (_,ts')<-ellipseExt ts=(Ellipse,center,FarthestCorner,ts')
                | otherwise = (Ellipse, center, FarthestCorner, ts)
            radArgs ts | Just (_, Ident "at":ts') <- ellipseExt ts,
                        Just (org, stops) <- position ts' =
                    (Ellipse, org, FarthestCorner, stops)
                | Just (_, Ident "ellipse":Ident "at":ts') <- ellipseExt ts,
                        Just (org, stops) <- position ts' =
                    (Ellipse, org, FarthestCorner, stops)
                | Just (_, Ident "ellipse":ts') <- ellipseExt ts =
                    (Ellipse, center, FarthestCorner, ts')
                | Just (_, Ident "circle":Ident "at":ts') <- circleExt ts,
                        Just (org, stops) <- position ts' =
                    (Circle, org, FarthestCorner, stops)
                | Just (_, Ident "circle":ts') <- circleExt ts =
                    (Circle, center, FarthestCorner, ts')
                | otherwise = (Ellipse, center, FarthestCorner, ts)
            radShape "circle" = Just Circle
            radShape "ellipse" = Just Ellipse
            radShape _ = Nothing
            radExt (Ident "closest-corner":ts) = Just (ClosestCorner, ts)
            radExt (Ident "closest-side":ts) = Just (ClosestSide, ts)
            radExt (Ident "farthest-corner":ts) = Just (FarthestCorner, ts)
            radExt (Ident "farthest-side":ts) = Just (FarthestSide, ts)
            radExt _ = Nothing
            ellipseExt ts | Just ret <- radExt ts = Just ret
            ellipseExt (Percentage _ x:Percentage _ y:ts) =
                Just (p' x `Extent` p' y, ts)
            ellipseExt (Percentage _ x:Dimension _ y "px":ts) =
                Just (p' x `Extent` f' y, ts)
            ellipseExt (Dimension _ x "px":Percentage _ y:ts) =
                Just (f' x `Extent` p' y, ts)
            ellipseExt (Dimension _ x "px":Dimension _ y "px":ts) =
                Just (f' x `Extent` p' y, ts)
            ellipseExt _ = Nothing
            circleExt (Dimension _ x "px":ts) = Just (f' x `Extent` f' x, ts)
            circleExt ts = radExt ts
            p' = Scale . p
            f' = Absolute . f
            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
                    Percentage _ a -> p' a
                    Dimension _ a "px" -> 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
                    Percentage _ a -> p' a
                    Dimension _ a "px" -> f' a
                    _ -> Auto),
                ts) of
                    ((Auto, _), _) -> Nothing