module Diagrams.TwoD.Adjust (
adjustDia2D
, adjustSize
) where
import Graphics.Rendering.Diagrams
import Diagrams.Attributes (lw, lc)
import Diagrams.Util ((#))
import Diagrams.TwoD.Types (R2)
import Diagrams.TwoD.Util (size2D, center2D)
import Diagrams.TwoD.Text (fontSize)
import Diagrams.TwoD.Util (SizeSpec2D(..))
import Data.AffineSpace ((.-.))
import Data.Colour.Names (black)
import Data.Monoid (Monoid, mempty)
adjustDia2D :: Monoid m => (Options b R2 -> R2) -> b -> Options b R2 -> AnnDiagram b R2 m -> AnnDiagram b R2 m
adjustDia2D getSize _ opts d = d # lw 0.01 # lc black # fontSize 1 # freeze
# scale s
# translate tr
where (w,h) = getSize opts
(wd,hd) = size2D d
xscale = w / wd
yscale = h / hd
s' = min xscale yscale
s | isInfinite s' = 1
| otherwise = s'
tr = (0.5 *. P (w,h)) .-. (s *. center2D d)
adjustSize :: SizeSpec2D -> R2 -> Transformation R2
adjustSize Absolute _ = mempty
adjustSize (Width wSpec) (w,_)
| wSpec == 0 || w == 0 = mempty
| otherwise = scaling (wSpec / w)
adjustSize (Height hSpec) (_,h)
| hSpec == 0 || h == 0 = mempty
| otherwise = scaling (hSpec / h)
adjustSize (Dims wSpec hSpec) (w,h) = scaling s
where xscale = wSpec / w
yscale = hSpec / h
s' = min xscale yscale
s | isInfinite s' = 1
| otherwise = s'