@@ -60,15 +60,19 @@ module Draw (
60
60
61
61
62
62
63
- import Control.Monad
64
- import Data.Default.Class
65
- import Data.Foldable
66
- import Data.List
67
- import Graphics.Rendering.Cairo as C hiding (x , y )
68
- import Graphics.Rendering.Cairo.Matrix (Matrix (.. ))
69
- import System.Directory
70
- import System.FilePath
71
-
63
+ import Control.Monad
64
+ import qualified Data.ByteString.Lazy as BSL
65
+ import Data.Default.Class
66
+ import Data.Foldable
67
+ import Data.Int
68
+ import Data.List
69
+ import Graphics.Rendering.Cairo as C hiding (x , y )
70
+ import Graphics.Rendering.Cairo.Matrix (Matrix (.. ))
71
+ import System.Directory
72
+ import System.FilePath
73
+ import Text.Printf
74
+
75
+ import Data.Crc32
72
76
import Draw.Color
73
77
import Draw.Color.Schemes.Continuous
74
78
import Draw.Color.Schemes.Discrete
@@ -142,7 +146,7 @@ data CoordinateSystem
142
146
-- def {_arrowDrawBody=False})
143
147
-- stroke
144
148
-- :}
145
- -- docs/haddock/Draw/coordinate_system_cairo_standard.svg
149
+ -- Generated file: size 2KB, crc32: 0x22a87e3e
146
150
147
151
| MathStandard_ZeroBottomLeft_XRight_YUp Double
148
152
-- ^ __Right-handed coordinate system.__ Standard math coordinates, with
@@ -168,7 +172,7 @@ data CoordinateSystem
168
172
-- def {_arrowDrawBody=False})
169
173
-- stroke
170
174
-- :}
171
- -- docs/haddock/Draw/coordinate_system_math_standard.svg
175
+ -- Generated file: size 3KB, crc32: 0xd33a20ee
172
176
173
177
| MathStandard_ZeroCenter_XRight_YUp Double Double
174
178
-- ^ __Right-handed coordinate system.__ Standard math coordinates, with
@@ -194,7 +198,7 @@ data CoordinateSystem
194
198
-- def {_arrowDrawBody=False})
195
199
-- stroke
196
200
-- :}
197
- -- docs/haddock/Draw/coordinate_system_math_standard_centered.svg
201
+ -- Generated file: size 3KB, crc32: 0xe6e10f11
198
202
199
203
deriving (Eq , Ord , Show )
200
204
@@ -217,6 +221,10 @@ coordinateSystem cosy = do
217
221
218
222
-- | Render pictures for Haddock with doctests. Nomenclature: the 'FilePath' for
219
223
-- /Foo.Bar.Baz/ is /Foo\/Bar\/Baz\/pic_name.svg/.
224
+ --
225
+ -- Prints status information about the generated file so that doctests fail when
226
+ -- the file contents change. Inspect the new output and update the output if the
227
+ -- result is OK.
220
228
haddockRender :: FilePath -> Int -> Int -> Render () -> IO ()
221
229
haddockRender filename w h actions = do
222
230
let filepath = " docs/haddock/" ++ filename
@@ -244,7 +252,7 @@ haddockRender filename w h actions = do
244
252
haddockAxes (Vec2 5 5 ) 15
245
253
246
254
normalizeSvgFile filepath
247
- putStrLn filepath
255
+ haddockPrintInfo filepath
248
256
249
257
haddockGrid :: Int -> Int -> Render ()
250
258
haddockGrid w h = grouped (paintWithAlpha 0.1 ) $ do
@@ -296,6 +304,20 @@ haddockAxes start len = grouped (paintWithAlpha 0.5) $ do
296
304
| otherwise = mirrorYCoords
297
305
in G. transform (G. translate (start +. Vec2 0 (len+ 5 )) <> G. scale 2 <> directionFlip) y'
298
306
307
+ haddockPrintInfo :: FilePath -> IO ()
308
+ haddockPrintInfo filepath = do
309
+ contents <- BSL. readFile filepath
310
+ printf " Generated file: size %s, crc32: %s" (humanFilesize (BSL. length contents)) (show (crc32 contents))
311
+
312
+ humanFilesize :: Int64 -> String
313
+ humanFilesize = go suffixes
314
+ where
315
+ go [] size = printf " Oh wow this file is %d byte large I ran out of suffixes" size
316
+ go (suffix: rest) size
317
+ | size < 1000 = show size ++ suffix
318
+ | otherwise = go rest (size `div` 1000 )
319
+ suffixes = [" B" , " KB" , " MB" , " GB" , " TB" , " PB" ] -- That should suffice.
320
+
299
321
-- | 'Vec2'-friendly version of Cairo’s 'moveTo'.
300
322
moveToVec :: Vec2 -> Render ()
301
323
moveToVec (Vec2 x y) = moveTo x y
@@ -314,7 +336,7 @@ lineToVec (Vec2 x y) = lineTo x y
314
336
-- sketch (Bezier (Vec2 10 10) (Vec2 50 200) (Vec2 100 (-50)) (Vec2 140 90))
315
337
-- stroke
316
338
-- :}
317
- -- docs/haddock/Draw/instance_Sketch_Bezier.svg
339
+ -- Generated file: size 2KB, crc32: 0xe17dab02
318
340
instance Sketch Bezier where
319
341
sketch (Bezier start (Vec2 x1 y1) (Vec2 x2 y2) (Vec2 x3 y3)) = do
320
342
moveToVec start
@@ -365,7 +387,7 @@ data Arrow = Arrow !Line !ArrowSpec
365
387
-- sketch (Arrow (Line (Vec2 10 10) (Vec2 140 90)) def)
366
388
-- stroke
367
389
-- :}
368
- -- docs/haddock/Draw/instance_Sketch_Arrow.svg
390
+ -- Generated file: size 2KB, crc32: 0x2c724862
369
391
instance Sketch Arrow where
370
392
sketch (Arrow line ArrowSpec {.. }) = do
371
393
when _arrowDrawBody (sketch line)
@@ -426,7 +448,7 @@ instance Sketch a => Sketch (Maybe a) where
426
448
-- sketch (Line (Vec2 10 10) (Vec2 140 90))
427
449
-- stroke
428
450
-- :}
429
- -- docs/haddock/Draw/instance_Sketch_Line.svg
451
+ -- Generated file: size 2KB, crc32: 0x9287e4a8
430
452
instance Sketch Line where
431
453
sketch (Line start end) = do
432
454
moveToVec start
@@ -443,7 +465,7 @@ instance Sketch Line where
443
465
-- sketch (Polyline [Vec2 10 10, Vec2 90 90, Vec2 120 10, Vec2 140 50])
444
466
-- stroke
445
467
-- :}
446
- -- docs/haddock/Draw/instance_Sketch_Sequential_Vec2.svg
468
+ -- Generated file: size 2KB, crc32: 0x5d5a0158
447
469
instance Sketch Polyline where
448
470
sketch (Polyline xs) = go xs
449
471
where
@@ -462,7 +484,7 @@ instance Sketch Polyline where
462
484
-- sketch (Polygon [Vec2 20 10, Vec2 10 80, Vec2 45 45, Vec2 60 90, Vec2 90 30])
463
485
-- stroke
464
486
-- :}
465
- -- docs/haddock/Draw/instance_Sketch_Polygon.svg
487
+ -- Generated file: size 2KB, crc32: 0x7f620554
466
488
instance Sketch Polygon where
467
489
sketch (Polygon [] ) = pure ()
468
490
sketch (Polygon xs) = sketch (Polyline xs) >> closePath
@@ -476,7 +498,7 @@ instance Sketch Polygon where
476
498
-- sketch (Circle (Vec2 50 50) 45)
477
499
-- stroke
478
500
-- :}
479
- -- docs/haddock/Draw/instance_Sketch_Circle.svg
501
+ -- Generated file: size 2KB, crc32: 0xebd35c6d
480
502
instance Sketch Circle where
481
503
sketch (Circle (Vec2 x y) r) = arc x y r 0 (2 * pi )
482
504
@@ -491,7 +513,7 @@ instance Sketch Circle where
491
513
-- (toEllipse (Circle zero 45)))
492
514
-- stroke
493
515
-- :}
494
- -- docs/haddock/Draw/instance_Sketch_Ellipse.svg
516
+ -- Generated file: size 2KB, crc32: 0x25bae2ef
495
517
--
496
518
instance Sketch Ellipse where
497
519
sketch (Ellipse t) = cairoScope $ do
@@ -518,7 +540,7 @@ data Cross = Cross
518
540
-- sketch (Cross (Vec2 60 20) 15) >> stroke
519
541
-- sketch (Circle (Vec2 60 20) 15) >> stroke
520
542
-- :}
521
- -- docs/haddock/Draw/instance_Sketch_Cross.svg
543
+ -- Generated file: size 2KB, crc32: 0xe2cb8567
522
544
instance Sketch Cross where
523
545
sketch (Cross center r) = do
524
546
let lowerRight = G. transform (rotateAround center (deg 45 )) (center +. Vec2 r 0 )
@@ -540,7 +562,7 @@ instance Sketch Cross where
540
562
-- setColor (mathematica97 1) >> sketch (G.translate (Vec2 110 50) <> G.rotate (deg 30)) >> stroke
541
563
-- setColor (mathematica97 2) >> sketch (G.shear 0.5 0.2 <> G.translate (Vec2 140 0)) >> stroke
542
564
-- :}
543
- -- docs/haddock/Draw/instance_Sketch_Transformation.svg
565
+ -- Generated file: size 4KB, crc32: 0x1f4ae5da
544
566
instance Sketch Transformation where
545
567
sketch t = do
546
568
let grid = [Line (Vec2 0 y) (Vec2 100 y) | y <- map fromIntegral [0 ,20 .. 100 ]]
@@ -579,7 +601,7 @@ arcSketchNegative (Vec2 x y) r angleStart angleEnd
579
601
-- sketch (boundingBox geometry)
580
602
-- stroke
581
603
-- :}
582
- -- docs/haddock/Draw/instance_Sketch_BoundingBox.svg
604
+ -- Generated file: size 3KB, crc32: 0xfed2c044
583
605
instance Sketch BoundingBox where
584
606
sketch (BoundingBox (Vec2 xlo ylo) (Vec2 xhi yhi)) = do
585
607
let w = xhi - xlo
@@ -622,7 +644,7 @@ instance Default CartesianParams where
622
644
-- >>> :{
623
645
-- haddockRender "Draw/cartesianCoordinateSystem.svg" 320 220 (cartesianCoordinateSystem def)
624
646
-- :}
625
- -- docs/haddock/Draw/cartesianCoordinateSystem.svg
647
+ -- Generated file: size 21KB, crc32: 0xf43aac0c
626
648
cartesianCoordinateSystem :: CartesianParams -> Render ()
627
649
cartesianCoordinateSystem params@ CartesianParams {.. } = grouped (paintWithAlpha _cartesianAlpha) $ do
628
650
let vec2 x y = Vec2 (fromIntegral x) (fromIntegral y)
@@ -684,7 +706,7 @@ instance Default PolarParams where
684
706
-- C.translate 50 50
685
707
-- radialCoordinateSystem def
686
708
-- :}
687
- -- docs/haddock/Draw/radialCoordinateSystem.svg
709
+ -- Generated file: size 26KB, crc32: 0x9b68b36
688
710
radialCoordinateSystem :: PolarParams -> Render ()
689
711
radialCoordinateSystem PolarParams {_polarCenter= center, _polarMaxRadius= maxR} = cairoScope $ do
690
712
setLineWidth 1
@@ -749,7 +771,7 @@ withOperator op actions = do
749
771
-- sketch line
750
772
-- stroke
751
773
-- :}
752
- -- docs/haddock/Draw/cairoScope.svg
774
+ -- Generated file: size 2KB, crc32: 0x2d7bee90
753
775
--
754
776
-- <<docs/haddock/Draw/cairoScope.svg>>
755
777
cairoScope :: Render a -> Render a
0 commit comments