| 1 | {-# LANGUAGE ConstraintKinds #-} |
| 2 | {-# LANGUAGE FlexibleContexts #-} |
| 3 | {-# LANGUAGE MultiParamTypeClasses #-} |
| 4 | {-# LANGUAGE ScopedTypeVariables #-} |
| 5 | {-# LANGUAGE TypeFamilies #-} |
| 6 | |
| 7 | {-# OPTIONS_GHC -fno-warn-unused-imports #-} |
| 8 | -- for Data.Semigroup |
| 9 | |
| 10 | ----------------------------------------------------------------------------- |
| 11 | -- | |
| 12 | -- Module : Diagrams.TwoD.Combinators |
| 13 | -- Copyright : (c) 2011 diagrams-lib team (see LICENSE) |
| 14 | -- License : BSD-style (see LICENSE) |
| 15 | -- Maintainer : [email protected] |
| 16 | -- |
| 17 | -- Diagram combinators specialized to two dimensions. For more general |
| 18 | -- combinators, see "Diagrams.Combinators". |
| 19 | -- |
| 20 | ----------------------------------------------------------------------------- |
| 21 | |
| 22 | module Diagrams.TwoD.Combinators |
| 23 | ( |
| 24 | -- * Binary combinators |
| 25 | |
| 26 | (===), (|||) |
| 27 | |
| 28 | -- * n-ary combinators |
| 29 | , hcat, hcat', hsep |
| 30 | , vcat, vcat', vsep |
| 31 | |
| 32 | -- * Spacing/envelopes |
| 33 | , strutR2 |
| 34 | , strutX, strutY |
| 35 | , padX, padY |
| 36 | |
| 37 | , extrudeLeft, extrudeRight, extrudeBottom, extrudeTop |
| 38 | |
| 39 | , rectEnvelope, crop |
| 40 | |
| 41 | , boundingRect, bg, bgFrame |
| 42 | |
| 43 | ) where |
| 44 | |
| 45 | import Control.Lens ((&), (.~)) |
| 46 | import Data.Colour |
| 47 | import Data.Default |
| 48 | import Data.Semigroup |
| 49 | |
| 50 | import Diagrams.Core |
| 51 | |
| 52 | import Diagrams.Attributes (lwO) |
| 53 | import Diagrams.BoundingBox |
| 54 | import Diagrams.Combinators |
| 55 | import Diagrams.Path |
| 56 | import Diagrams.Query (value) |
| 57 | import Diagrams.Segment |
| 58 | import Diagrams.TrailLike |
| 59 | import Diagrams.TwoD.Align |
| 60 | import Diagrams.TwoD.Attributes (fc) |
| 61 | import Diagrams.TwoD.Path () |
| 62 | import Diagrams.TwoD.Shapes |
| 63 | import Diagrams.TwoD.Transform (scaleX, scaleY) |
| 64 | import Diagrams.TwoD.Types |
| 65 | import Diagrams.TwoD.Vector |
| 66 | import Diagrams.Util (( # )) |
| 67 | |
| 68 | import Linear.Affine |
| 69 | import Linear.Metric |
| 70 | import Linear.Vector |
| 71 | |
| 72 | infixl 6 === |
| 73 | infixl 6 ||| |
| 74 | |
| 75 | -- | Place two diagrams (or other objects) vertically adjacent to one |
| 76 | -- another, with the first diagram above the second. Since Haskell |
| 77 | -- ignores whitespace in expressions, one can thus write |
| 78 | -- |
| 79 | -- @ |
| 80 | -- c |
| 81 | -- === |
| 82 | -- d |
| 83 | -- @ |
| 84 | -- |
| 85 | -- to place @c@ above @d@. The local origin of the resulting |
| 86 | -- combined diagram is the same as the local origin of the first. |
| 87 | -- @(===)@ is associative and has 'mempty' as an identity. See the |
| 88 | -- documentation of 'beside' for more information. |
| 89 | (===) :: (InSpace V2 n a, Juxtaposable a, Semigroup a) => a -> a -> a |
| 90 | (===) = beside unit_Y |
| 91 | |
| 92 | -- | Place two diagrams (or other juxtaposable objects) horizontally |
| 93 | -- adjacent to one another, with the first diagram to the left of |
| 94 | -- the second. The local origin of the resulting combined diagram |
| 95 | -- is the same as the local origin of the first. @(|||)@ is |
| 96 | -- associative and has 'mempty' as an identity. See the |
| 97 | -- documentation of 'beside' for more information. |
| 98 | (|||) :: (InSpace V2 n a, Juxtaposable a, Semigroup a) => a -> a -> a |
| 99 | (|||) = beside unitX |
| 100 | |
| 101 | -- | Lay out a list of juxtaposable objects in a row from left to right, |
| 102 | -- so that their local origins lie along a single horizontal line, |
| 103 | -- with successive envelopes tangent to one another. |
| 104 | -- |
| 105 | -- * For more control over the spacing, see 'hcat''. |
| 106 | -- |
| 107 | -- * To align the diagrams vertically (or otherwise), use alignment |
| 108 | -- combinators (such as 'alignT' or 'alignB') from |
| 109 | -- "Diagrams.TwoD.Align" before applying 'hcat'. |
| 110 | -- |
| 111 | -- * For non-axis-aligned layout, see 'cat'. |
| 112 | hcat :: (InSpace V2 n a, Floating n, Juxtaposable a, HasOrigin a, Monoid' a) |
| 113 | => [a] -> a |
| 114 | hcat = hcat' def |
| 115 | |
| 116 | -- | A variant of 'hcat' taking an extra 'CatOpts' record to control |
| 117 | -- the spacing. See the 'cat'' documentation for a description of |
| 118 | -- the possibilities. For the common case of setting just a |
| 119 | -- separation amount, see 'hsep'. |
| 120 | hcat' :: (InSpace V2 n a, Floating n, Juxtaposable a, HasOrigin a, Monoid' a) |
| 121 | => CatOpts n -> [a] -> a |
| 122 | hcat' = cat' unitX |
| 123 | |
| 124 | -- | A convenient synonym for horizontal concatenation with |
| 125 | -- separation: @hsep s === hcat' (with & sep .~ s)@. |
| 126 | hsep :: (InSpace V2 n a, Floating n, Juxtaposable a, HasOrigin a, Monoid' a) |
| 127 | => n -> [a] -> a |
| 128 | hsep s = hcat' (def & sep .~ s) |
| 129 | |
| 130 | -- | Lay out a list of juxtaposable objects in a column from top to |
| 131 | -- bottom, so that their local origins lie along a single vertical |
| 132 | -- line, with successive envelopes tangent to one another. |
| 133 | -- |
| 134 | -- * For more control over the spacing, see 'vcat''. |
| 135 | -- |
| 136 | -- * To align the diagrams horizontally (or otherwise), use alignment |
| 137 | -- combinators (such as 'alignL' or 'alignR') from |
| 138 | -- "Diagrams.TwoD.Align" before applying 'vcat'. |
| 139 | -- |
| 140 | -- * For non-axis-aligned layout, see 'cat'. |
| 141 | vcat :: (InSpace V2 n a, Floating n, Juxtaposable a, HasOrigin a, Monoid' a) |
| 142 | => [a] -> a |
| 143 | vcat = vcat' def |
| 144 | |
| 145 | -- | A variant of 'vcat' taking an extra 'CatOpts' record to control |
| 146 | -- the spacing. See the 'cat'' documentation for a description of |
| 147 | -- the possibilities. For the common case of setting just a |
| 148 | -- separation amount, see 'vsep'. |
| 149 | vcat' :: (InSpace V2 n a, Floating n, Juxtaposable a, HasOrigin a, Monoid' a) |
| 150 | => CatOpts n -> [a] -> a |
| 151 | vcat' = cat' unit_Y |
| 152 | |
| 153 | -- | A convenient synonym for vertical concatenation with |
| 154 | -- separation: @vsep s === vcat' (with & sep .~ s)@. |
| 155 | vsep :: (InSpace V2 n a, Floating n, Juxtaposable a, HasOrigin a, Monoid' a) |
| 156 | => n -> [a] -> a |
| 157 | vsep s = vcat' (def & sep .~ s) |
| 158 | |
| 159 | -- | @strutR2 v@ is a two-dimensional diagram which produces no |
| 160 | -- output, but with respect to alignment, envelope, /and trace/ acts |
| 161 | -- like a 1-dimensional segment oriented along the vector @v@, with |
| 162 | -- local origin at its center. If you don't care about the trace |
| 163 | -- then there's no difference between @strutR2@ and the more general |
| 164 | -- 'strut'. |
| 165 | strutR2 :: (RealFloat n, Monoid' m) => V2 n -> QDiagram b V2 n m |
| 166 | strutR2 v = phantom seg |
| 167 | where |
| 168 | seg = FLinear (origin .+^ 0.5 *^ v) (origin .+^ (-0.5) *^ v) |
| 169 | |
| 170 | -- | @strutX w@ is an empty diagram with width @w@, height 0, and a |
| 171 | -- centered local origin. Note that @strutX (-w)@ behaves the same as |
| 172 | -- @strutX w@. |
| 173 | strutX :: (Metric v, R1 v, OrderedField n) => n -> QDiagram b v n m |
| 174 | strutX d = strut (zero & _x .~ d) |
| 175 | |
| 176 | -- | @strutY h@ is an empty diagram with height @h@, width 0, and a |
| 177 | -- centered local origin. Note that @strutY (-h)@ behaves the same as |
| 178 | -- @strutY h@. |
| 179 | strutY :: (Metric v, R2 v, OrderedField n) => n -> QDiagram b v n m |
| 180 | strutY d = strut (zero & _y .~ d) |
| 181 | |
| 182 | -- | @padX s@ \"pads\" a diagram in the x-direction, expanding its |
| 183 | -- envelope horizontally by a factor of @s@ (factors between 0 and 1 |
| 184 | -- can be used to shrink the envelope). Note that the envelope will |
| 185 | -- expand with respect to the local origin, so if the origin is not |
| 186 | -- centered horizontally the padding may appear \"uneven\". If this |
| 187 | -- is not desired, the origin can be centered (using 'centerX') |
| 188 | -- before applying @padX@. |
| 189 | padX :: (Metric v, R2 v, OrderedField n, Monoid' m) |
| 190 | => n -> QDiagram b v n m -> QDiagram b v n m |
| 191 | padX s d = withEnvelope (d # scaleX s) d |
| 192 | |
| 193 | -- | @padY s@ \"pads\" a diagram in the y-direction, expanding its |
| 194 | -- envelope vertically by a factor of @s@ (factors between |
| 195 | -- 0 and 1 can be used to shrink the envelope). Note that |
| 196 | -- the envelope will expand with respect to the local origin, |
| 197 | -- so if the origin is not centered vertically the padding may appear |
| 198 | -- \"uneven\". If this is not desired, the origin can be centered |
| 199 | -- (using 'centerY') before applying @padY@. |
| 200 | padY :: (Metric v, R2 v, Monoid' m, OrderedField n) |
| 201 | => n -> QDiagram b v n m -> QDiagram b v n m |
| 202 | padY s d = withEnvelope (d # scaleY s) d |
| 203 | |
| 204 | -- | @extrudeLeft s@ \"extrudes\" a diagram in the negative x-direction, |
| 205 | -- offsetting its envelope by the provided distance. When @ s < 0 @, |
| 206 | -- the envelope is inset instead. |
| 207 | -- |
| 208 | -- See the documentation for 'extrudeEnvelope' for more information. |
| 209 | extrudeLeft :: (OrderedField n, Monoid' m) => n -> QDiagram b V2 n m -> QDiagram b V2 n m |
| 210 | extrudeLeft s |
| 211 | | s >= 0 = extrudeEnvelope $ unitX ^* negate s |
| 212 | | otherwise = intrudeEnvelope $ unitX ^* negate s |
| 213 | |
| 214 | -- | @extrudeRight s@ \"extrudes\" a diagram in the positive x-direction, |
| 215 | -- offsetting its envelope by the provided distance. When @ s < 0 @, |
| 216 | -- the envelope is inset instead. |
| 217 | -- |
| 218 | -- See the documentation for 'extrudeEnvelope' for more information. |
| 219 | extrudeRight :: (OrderedField n, Monoid' m) => n -> QDiagram b V2 n m -> QDiagram b V2 n m |
| 220 | extrudeRight s |
| 221 | | s >= 0 = extrudeEnvelope $ unitX ^* s |
| 222 | | otherwise = intrudeEnvelope $ unitX ^* s |
| 223 | |
| 224 | -- | @extrudeBottom s@ \"extrudes\" a diagram in the negative y-direction, |
| 225 | -- offsetting its envelope by the provided distance. When @ s < 0 @, |
| 226 | -- the envelope is inset instead. |
| 227 | -- |
| 228 | -- See the documentation for 'extrudeEnvelope' for more information. |
| 229 | extrudeBottom :: (OrderedField n, Monoid' m) => n -> QDiagram b V2 n m -> QDiagram b V2 n m |
| 230 | extrudeBottom s |
| 231 | | s >= 0 = extrudeEnvelope $ unitY ^* negate s |
| 232 | | otherwise = intrudeEnvelope $ unitY ^* negate s |
| 233 | |
| 234 | -- | @extrudeTop s@ \"extrudes\" a diagram in the positive y-direction, |
| 235 | -- offsetting its envelope by the provided distance. When @ s < 0 @, |
| 236 | -- the envelope is inset instead. |
| 237 | -- |
| 238 | -- See the documentation for 'extrudeEnvelope' for more information. |
| 239 | extrudeTop :: (OrderedField n, Monoid' m) => n -> QDiagram b V2 n m -> QDiagram b V2 n m |
| 240 | extrudeTop s |
| 241 | | s >= 0 = extrudeEnvelope $ unitY ^* s |
| 242 | | otherwise = intrudeEnvelope $ unitY ^* s |
| 243 | |
| 244 | -- | @rectEnvelope p v@ sets the envelope of a diagram to a rectangle whose |
| 245 | -- lower-left corner is at @p@ and whose upper-right corner is at @p |
| 246 | -- .+^ v@. Useful for selecting the rectangular portion of a |
| 247 | -- diagram which should actually be \"viewed\" in the final render, |
| 248 | -- if you don't want to see the entire diagram. |
| 249 | rectEnvelope :: forall b n m. (OrderedField n, Monoid' m) |
| 250 | => Point V2 n -> V2 n -> QDiagram b V2 n m -> QDiagram b V2 n m |
| 251 | rectEnvelope p (V2 w h) = withEnvelope (rect w h # alignBL # moveTo p :: Path V2 n) |
| 252 | |
| 253 | -- | A synonym for 'rectEnvelope'. |
| 254 | crop :: forall b n m. (OrderedField n, Monoid' m) |
| 255 | => Point V2 n -> V2 n -> QDiagram b V2 n m -> QDiagram b V2 n m |
| 256 | crop = rectEnvelope |
| 257 | |
| 258 | -- | Construct a bounding rectangle for an enveloped object, that is, |
| 259 | -- the smallest axis-aligned rectangle which encloses the object. |
| 260 | boundingRect :: ( InSpace V2 n a, SameSpace a t |
| 261 | , Enveloped t, Transformable t, TrailLike t, Monoid t |
| 262 | , Enveloped a) |
| 263 | => a -> t |
| 264 | boundingRect = (`boxFit` rect 1 1) . boundingBox |
| 265 | |
| 266 | -- | \"Set the background color\" of a diagram. That is, place a |
| 267 | -- diagram atop a bounding rectangle of the given color. |
| 268 | -- The background does not change the result of queries. |
| 269 | bg :: (TypeableFloat n, Renderable (Path V2 n) b, Monoid' q) |
| 270 | => Colour Double -> QDiagram b V2 n q -> QDiagram b V2 n q |
| 271 | bg c d = d <> boundingRect d # lwO 0 # fc c # value mempty |
| 272 | |
| 273 | -- | Similar to 'bg' but makes the colored background rectangle larger than |
| 274 | -- the diagram. The first parameter is used to set how far the background |
| 275 | -- extends beyond the diagram. |
| 276 | -- The background does not change the result of queries. |
| 277 | bgFrame :: (TypeableFloat n, Renderable (Path V2 n) b, Monoid' q) |
| 278 | => n -> Colour Double -> QDiagram b V2 n q -> QDiagram b V2 n q |
| 279 | bgFrame f c d = d <> boundingRect (frame f d) # lwO 0 # fc c # value mempty |