never executed always true always false
1 module Reanimate.Svg.Unuse
2 ( replaceUses
3 , unbox
4 , embedDocument
5 ) where
6
7 import Control.Lens ((%~), (&), (.~), (?~), (^.))
8 import qualified Data.Map as Map
9 import Data.Maybe
10 import Graphics.SvgTree hiding (line, path, use)
11 import Reanimate.Constants
12 import Reanimate.Svg.Constructors
13
14 -- | Replace all @<use>@ nodes with their definition.
15 replaceUses :: Document -> Document
16 replaceUses doc = doc & elements %~ map (mapTree replace)
17 where
18 replaceDefinition PathTree{} = None
19 replaceDefinition t = t
20
21 replace t@DefinitionTree{} = mapTree replaceDefinition t
22 replace (UseTree _ Just{}) = error "replaceUses: subtree in use?"
23 replace (UseTree use Nothing) =
24 case Map.lookup (use^.useName) idMap of
25 Nothing -> error $ "Unknown id: " ++ (use^.useName)
26 Just tree -> mapTree replace $
27 GroupTree $
28 defaultSvg & groupChildren .~ [tree]
29 & transform ?~
30 fromMaybe [] (use^.transform) ++
31 [baseToTransformation (use^.useBase)]
32 replace x = x
33 baseToTransformation (x,y) =
34 case (toUserUnit defaultDPI x, toUserUnit defaultDPI y) of
35 (Num a, Num b) -> Translate a b
36 _ -> TransformUnknown
37 docTree = mkGroup (doc^.elements)
38 idMap = foldTree updMap Map.empty docTree
39 updMap m tree =
40 case tree^.attrId of
41 Nothing -> m
42 Just tid -> Map.insert tid tree m
43
44 -- FIXME: the viewbox is ignored. Can we use the viewbox as a mask?
45 -- | Transform out viewbox. Definitions and CSS rules are discarded.
46 unbox :: Document -> Tree
47 unbox doc@Document{_viewBox = Just (_minx, _minw, _width, _height)} =
48 GroupTree $ defaultSvg
49 & groupChildren .~ doc^.elements
50 unbox doc =
51 GroupTree $ defaultSvg
52 & groupChildren .~ doc^.elements
53
54 -- | Embed 'Document'. This keeps the entire document intact but makes
55 -- it more difficult to use, say, `Reanimate.Svg.pathify` on it.
56 embedDocument :: Document -> Tree
57 embedDocument doc =
58 translate (-screenWidth/2) (screenHeight/2) $
59 withFillOpacity 1 $
60 withStrokeWidth 0 $
61 flipYAxis $
62 SvgTree $ doc & width .~ Nothing
63 & height .~ Nothing