Skip to content

Commit

Permalink
reworked backend to work with both RTrees and flattend DUALTrees
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyrosenbluth committed Oct 17, 2013
1 parent ec4f86b commit c95f285
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions src/Diagrams/Core/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -753,12 +753,10 @@ class (HasLinearMap v, Monoid (Render b v)) => Backend b v where
-- | Backend-specific rendering options.
data Options b v :: *

-- | Render an RTree
renderRTree :: RTree b v a -> Render b v
renderRTree _ = undefined


-- | Perform a rendering operation with a local style.
-- | Perform a rendering operation with a local style. The default
-- implementation should be overidden by backends that use `withStyle`
-- it is provided so that backends that override renderData do not need
-- to implement `withStyle`.
withStyle :: b -- ^ Backend token (needed only for type inference)
-> Style v -- ^ Style to use
-> Transformation v -- ^ Transformation to be applied to the style
Expand All @@ -783,27 +781,17 @@ class (HasLinearMap v, Monoid (Render b v)) => Backend b v where
-> QDiagram b v m -> (Options b v, QDiagram b v m)
adjustDia _ o d = (o,d)

-- XXX expand this comment. Explain about freeze, split
-- transformations, etc.
-- | Render a diagram. This has a default implementation in terms
-- of 'adjustDia', 'withStyle', 'doRender', and the 'render'
-- operation from the 'Renderable' class (first 'adjustDia' is
-- used, then 'withStyle' and 'render' are used to render each
-- primitive, the resulting operations are combined with
-- 'mconcat', and the final operation run with 'doRender') but
-- backends may override it if desired.
renderDia :: (InnerSpace v, OrderedField (Scalar v), Monoid' m)
=> b -> Options b v -> QDiagram b v m -> Result b v
renderDia b opts d =
doRender b opts' . mconcat . map renderOne . prims $ d'
where (opts', d') = adjustDia b opts d
renderOne :: (Prim b v, (Split (Transformation v), Style v))
-> Render b v
renderOne (p, (M t, s))
= withStyle b s mempty (render b (transform t p))

renderOne (p, (t1 :| t2, s))
= withStyle b s t1 (render b (transformWithFreeze t1 t2 p))
renderDia b opts d = doRender b opts' . renderData b $ d'
where (opts', d') = adjustDia b opts d

renderData :: Monoid' m => b -> QDiagram b v m -> Render b v
renderData b = mconcat . map renderOne . prims
where
renderOne :: (Prim b v, (Split (Transformation v), Style v)) -> Render b v
renderOne (p, (M t, s)) = withStyle b s mempty (render b (transform t p))
renderOne (p, (t1 :| t2, s)) = withStyle b s t1 (render b (transformWithFreeze t1 t2 p))

-- See Note [backend token]

Expand Down

2 comments on commit c95f285

@jeffreyrosenbluth
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes wont break the old backends that rely on withStyle and prim list. And at the same time will also accommodate backends that use the new RTree structure. I'ts probably not the solution we want for the long term, but will allow us a gradual transition to exposing tree structures to the backends. The default instance of withStyle's sole purpose is so that backends that don't use the default implementation of renderData don't have to implement it.

@byorgey
Copy link
Member

@byorgey byorgey commented on c95f285 Nov 2, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it!

Please sign in to comment.