Debian Codegraph @

src/Diagrams/TwoD/Adjust.hs

raw symbols SCIP
1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE Rank2Types #-}
4 {-# LANGUAGE TypeFamilies #-}
5
6 {-# OPTIONS_GHC -fno-warn-unused-imports #-}
7 -- for Data.Semigroup
8
9 -----------------------------------------------------------------------------
10 -- |
11 -- Module : Diagrams.TwoD.Adjust
12 -- Copyright : (c) 2011 diagrams-lib team (see LICENSE)
13 -- License : BSD-style (see LICENSE)
14 -- Maintainer : [email protected]
15 --
16 -- A default diagram-adjustment implementation for two-dimensional
17 -- diagrams, useful for backend implementors.
18 --
19 -----------------------------------------------------------------------------
20
21 module Diagrams.TwoD.Adjust
22 ( setDefault2DAttributes
23 , adjustSize2D
24 , adjustDia2D
25 ) where
26
27 import Diagrams.Attributes
28 import Diagrams.BoundingBox
29 import Diagrams.Core
30 import Diagrams.Size
31 import Diagrams.TwoD.Attributes (lineTextureA)
32 import Diagrams.TwoD.Types
33 import Diagrams.Util (( # ))
34
35 import Control.Lens (Lens', set, (^.))
36 import Data.Default
37 import Data.Semigroup
38
39
40 -- | Set default attributes of a 2D diagram (in case they have not
41 -- been set):
42 --
43 -- * 'LineWidth': 0.01
44 --
45 -- * 'LineTexture': solid black
46 --
47 -- * 'LineCap': LineCapButt
48 --
49 -- * 'LineJoin': miter
50 --
51 -- * 'MiterLimit': 10
52 setDefault2DAttributes :: (TypeableFloat n, Semigroup m)
53 => QDiagram b V2 n m -> QDiagram b V2 n m
54 setDefault2DAttributes d
55 = d # lineWidthM def
56 # lineTextureA def
57 # lineCap def
58 # lineJoin def
59 # lineMiterLimitA def
60
61 -- | Adjust the size and position of a 2D diagram to fit within the
62 -- requested size. The first argument is a lens into the output
63 -- size contained in the rendering options. Returns an updated
64 -- options record, any transformation applied to the diagram (the
65 -- inverse of which can be used, say, to translate output/device
66 -- coordinates back into local diagram coordinates), and the
67 -- modified diagram itself.
68 adjustSize2D
69 :: (TypeableFloat n, Monoid' m)
70 => Lens' (Options b V2 n) (SizeSpec V2 n)
71 -> b
72 -> Options b V2 n
73 -> QDiagram b V2 n m
74 -> (Options b V2 n, Transformation V2 n, QDiagram b V2 n m)
75 adjustSize2D szL _ opts d = (set szL spec opts, t, d # transform t)
76 where
77 spec = dims sz
78 (sz, t) = sizeAdjustment (opts ^. szL) (boundingBox d)
79
80 -- | @adjustDia2D@ provides a useful default implementation of
81 -- the 'adjustDia' method from the 'Backend' type class.
82 --
83 -- As its first argument it requires a lens into the output size
84 -- contained in the rendering options.
85 --
86 -- It then performs the following adjustments:
87 --
88 -- * Set default attributes (see 'setDefault2DAttributes')
89 --
90 -- * Scale and translate the diagram to fit within the requested
91 -- size (see 'adjustDiaSize2D')
92 --
93 -- It returns an updated options record, any transformation applied
94 -- to the diagram (the inverse of which can be used, say, to
95 -- translate output/device coordinates back into local diagram
96 -- coordinates), and the modified diagram itself.
97 adjustDia2D :: (TypeableFloat n, Monoid' m)
98 => Lens' (Options b V2 n) (SizeSpec V2 n)
99 -> b -> Options b V2 n -> QDiagram b V2 n m
100 -> (Options b V2 n, Transformation V2 n, QDiagram b V2 n m)
101 adjustDia2D szL b opts d
102 = adjustSize2D szL b opts (d # setDefault2DAttributes)
103