Debian Codegraph @

src/Diagrams/Attributes/Compile.hs

raw symbols SCIP
1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE GADTs #-}
3 {-# LANGUAGE ScopedTypeVariables #-}
4 {-# LANGUAGE TypeFamilies #-}
5
6 {-# OPTIONS_GHC -fno-warn-unused-imports #-}
7 -- for Data.Semigroup
8
9 -----------------------------------------------------------------------------
10 -- |
11 -- Module : Diagrams.Attributes.Compile
12 -- Copyright : (c) 2014 diagrams-lib team (see LICENSE)
13 -- License : BSD-style (see LICENSE)
14 -- Maintainer : [email protected]
15 --
16 -- XXX
17 --
18 -----------------------------------------------------------------------------
19
20 module Diagrams.Attributes.Compile (
21 SplitAttribute(..), splitAttr
22 ) where
23
24 import Data.Typeable
25
26 import Control.Arrow (second)
27 import Control.Lens ((%~), (&), _Wrapping')
28 import Data.Kind (Type)
29 import qualified Data.HashMap.Strict as HM
30 import Data.Semigroup
31 import Data.Tree (Tree (..))
32
33 import Diagrams.Core
34 import Diagrams.Core.Style (Style (..), attributeToStyle)
35 import Diagrams.Core.Types (RNode (..), RTree)
36
37 ------------------------------------------------------------
38
39 -- This is a sort of roundabout, overly-general way to define
40 -- splitFills; it's done this way to facilitate testing.
41
42 class (AttributeClass (AttrType code), Typeable (PrimType code)) => SplitAttribute code where
43 type AttrType code :: Type
44 type PrimType code :: Type
45
46 primOK :: code -> PrimType code -> Bool
47
48 -- | Push certain attributes down until they are at the roots of trees
49 -- containing only "safe" nodes. In particular this is used to push
50 -- fill attributes down until they are over only loops; see
51 -- 'splitFills'.
52 splitAttr :: forall code b v n a. SplitAttribute code => code -> RTree b v n a -> RTree b v n a
53 splitAttr code = fst . splitAttr' Nothing
54 where
55
56 -- splitAttr' is where the most interesting logic happens.
57 -- Mutually recursive with splitAttr'Forest. rebuildNode and
58 -- applyMfc are helper functions.
59 --
60 -- Input: attribute to apply to "safe" subtrees.
61 --
62 -- Output: tree with attributes pushed down appropriately, and
63 -- a Bool indicating whether the tree contains only "safe" prims (True) or
64 -- contains some unsafe ones (False).
65 splitAttr' :: Maybe (AttrType code) -> RTree b v n a -> (RTree b v n a, Bool)
66
67 -- RStyle node: Check for the special attribute, and split it out of
68 -- the style, combining it with the incoming attribute. Recurse and
69 -- rebuild. The tricky bit is that we use some knot-tying to
70 -- determine the right attribute to pass down to the subtrees based
71 -- on this computed Bool: if all subtrees are safe, then we will
72 -- apply the attribute at the root of this tree, and pass Nothing to
73 -- all the subtrees. Otherwise, we pass the given attribute along.
74 -- This works out because the attribute does not need to be
75 -- pattern-matched until actually applying it at some root, so the
76 -- recursion can proceed and the Bool values be computed with the
77 -- actual value of the attributes nodes filled in lazily.
78 splitAttr' mattr (Node (RStyle sty) cs) = (t', ok)
79 where
80 mattr' = mattr <> getAttr sty
81 sty' = sty & _Wrapping' Style %~ HM.delete ty
82 ty = typeOf (undefined :: AttrType code)
83 (cs', ok) = splitAttr'Forest mattr' cs
84 t' | ok = rebuildNode Nothing ok (RStyle sty) cs'
85 | otherwise = rebuildNode mattr ok (RStyle sty') cs'
86
87 -- RPrim node: check whether it
88 -- * is some sort of prim not under consideration: don't apply the attribute; return True
89 -- * is unsafe: don't apply the attribute; return False
90 -- * is safe : do apply the attribute; return True
91 splitAttr' mattr (Node rp@(RPrim (Prim prm)) _) =
92 case cast prm :: Maybe (PrimType code) of
93 Nothing -> (Node rp [], True)
94 Just p ->
95 if primOK code p
96 then (rebuildNode mattr True rp [], True)
97 else (Node rp [], False)
98
99 -- RFrozenTr, RAnnot, REmpty cases: just recurse and rebuild. Note
100 -- we assume that transformations do not affect the attributes.
101 splitAttr' mattr (Node nd cs) = (t', ok)
102 where
103 (cs', ok) = splitAttr'Forest mattr cs
104 t' = rebuildNode mattr ok nd cs'
105
106 -- Recursively call splitAttr' on all subtrees, returning the
107 -- logical AND of the Bool results returned (the whole forest is
108 -- safe iff all subtrees are).
109 splitAttr'Forest :: Maybe (AttrType code) -> [RTree b v n a] -> ([RTree b v n a], Bool)
110 splitAttr'Forest mattr cs = (cs', ok)
111 where
112 (cs', ok) = second and . unzip . map (splitAttr' mattr) $ cs
113
114 -- Given a fill attribute, a Bool indicating whether the given
115 -- subforest contains only loops, a node, and a subforest, rebuild a
116 -- tree, applying the fill attribute as appropriate (only if the
117 -- Bool is true and the attribute is not Nothing).
118 rebuildNode :: Maybe (AttrType code) -> Bool -> RNode b v n a -> [RTree b v n a] -> RTree b v n a
119 rebuildNode mattr ok nd cs
120 | ok = applyMattr mattr (Node nd cs)
121 | otherwise = Node nd cs
122
123 -- Prepend a new fill color node if Just; the identity function if
124 -- Nothing.
125 applyMattr :: Maybe (AttrType code) -> RTree b v n a -> RTree b v n a
126 applyMattr Nothing t = t
127 applyMattr (Just a) t = Node (RStyle $ attributeToStyle (Attribute a)) [t]