Skip to content

Commit 7b3bc85

Browse files
authored
Merge pull request #66 from quchen/haddock/crc32-haddockrender
Make haddockRender report file size and CRC32 (instead of filename)
2 parents 68bf4a5 + 2018257 commit 7b3bc85

20 files changed

+177
-93
lines changed

package.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies:
1818
- base >= 4.7 && < 5
1919

2020
- alfred-margaret
21+
- bytestring
2122
- cairo
2223
- colour
2324
- containers

src/Data/Crc32.hs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module Data.Crc32 (
2+
crc32
3+
, Crc32(..)
4+
) where
5+
6+
7+
8+
import Data.Bits
9+
import Data.ByteString.Lazy (ByteString)
10+
import qualified Data.ByteString.Lazy as BSL
11+
import Data.Vector (Vector, (!))
12+
import qualified Data.Vector as V
13+
import Data.Word
14+
import Text.Printf
15+
16+
17+
18+
table :: Vector Word32
19+
table = createCrc32Table 0xedb88320
20+
21+
createCrc32Table :: Word32 -> Vector Word32
22+
createCrc32Table poly = V.generate 256 $ \n -> V.foldl'
23+
(\acc _ -> if odd acc
24+
then poly `xor` shiftR acc 1
25+
else shiftR acc 1
26+
)
27+
(fromIntegral n)
28+
(V.enumFromN 0 8)
29+
30+
-- fn crc32_compute_table() -> [u32; 256] {
31+
-- let mut crc32_table = [0; 256];
32+
--
33+
-- for n in 0..256 {
34+
-- crc32_table[n as usize] = (0..8).fold(n as u32, |acc, _| {
35+
-- match acc & 1 {
36+
-- 1 => 0xedb88320 ^ (acc >> 1),
37+
-- _ => acc >> 1,
38+
-- }
39+
-- });
40+
-- }
41+
--
42+
-- crc32_table
43+
-- }
44+
45+
newtype Crc32 = Crc32 Word32
46+
deriving (Eq, Ord)
47+
48+
instance Show Crc32 where show (Crc32 crc) = printf "%#08x" crc
49+
50+
-- | CRC32 checksum.
51+
--
52+
-- >>> import Data.ByteString.Lazy.Char8
53+
-- >>> crc32 (pack "The quick brown fox jumps over the lazy dog")
54+
-- 0x414fa339
55+
--
56+
-- >>> crc32 (pack "123456789")
57+
-- 0xcbf43926
58+
crc32 :: ByteString -> Crc32
59+
crc32 bs =
60+
let Crc32 crcResult = BSL.foldl' (\(Crc32 crc) byte -> Crc32 (xor (crc `shiftR` 8) (table ! fromIntegral (fromIntegral (crc .&. 0xff) `xor` byte)))) (Crc32 0xffffffff) bs
61+
in Crc32 (complement crcResult)

src/Draw.hs

+48-26
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,19 @@ module Draw (
6060

6161

6262

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
7276
import Draw.Color
7377
import Draw.Color.Schemes.Continuous
7478
import Draw.Color.Schemes.Discrete
@@ -142,7 +146,7 @@ data CoordinateSystem
142146
-- def {_arrowDrawBody=False})
143147
-- stroke
144148
-- :}
145-
-- docs/haddock/Draw/coordinate_system_cairo_standard.svg
149+
-- Generated file: size 2KB, crc32: 0x22a87e3e
146150

147151
| MathStandard_ZeroBottomLeft_XRight_YUp Double
148152
-- ^ __Right-handed coordinate system.__ Standard math coordinates, with
@@ -168,7 +172,7 @@ data CoordinateSystem
168172
-- def {_arrowDrawBody=False})
169173
-- stroke
170174
-- :}
171-
-- docs/haddock/Draw/coordinate_system_math_standard.svg
175+
-- Generated file: size 3KB, crc32: 0xd33a20ee
172176

173177
| MathStandard_ZeroCenter_XRight_YUp Double Double
174178
-- ^ __Right-handed coordinate system.__ Standard math coordinates, with
@@ -194,7 +198,7 @@ data CoordinateSystem
194198
-- def {_arrowDrawBody=False})
195199
-- stroke
196200
-- :}
197-
-- docs/haddock/Draw/coordinate_system_math_standard_centered.svg
201+
-- Generated file: size 3KB, crc32: 0xe6e10f11
198202

199203
deriving (Eq, Ord, Show)
200204

@@ -217,6 +221,10 @@ coordinateSystem cosy = do
217221

218222
-- | Render pictures for Haddock with doctests. Nomenclature: the 'FilePath' for
219223
-- /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.
220228
haddockRender :: FilePath -> Int -> Int -> Render () -> IO ()
221229
haddockRender filename w h actions = do
222230
let filepath = "docs/haddock/" ++ filename
@@ -244,7 +252,7 @@ haddockRender filename w h actions = do
244252
haddockAxes (Vec2 5 5) 15
245253

246254
normalizeSvgFile filepath
247-
putStrLn filepath
255+
haddockPrintInfo filepath
248256

249257
haddockGrid :: Int -> Int -> Render ()
250258
haddockGrid w h = grouped (paintWithAlpha 0.1) $ do
@@ -296,6 +304,20 @@ haddockAxes start len = grouped (paintWithAlpha 0.5) $ do
296304
| otherwise = mirrorYCoords
297305
in G.transform (G.translate (start +. Vec2 0 (len+5)) <> G.scale 2 <> directionFlip) y'
298306

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+
299321
-- | 'Vec2'-friendly version of Cairo’s 'moveTo'.
300322
moveToVec :: Vec2 -> Render ()
301323
moveToVec (Vec2 x y) = moveTo x y
@@ -314,7 +336,7 @@ lineToVec (Vec2 x y) = lineTo x y
314336
-- sketch (Bezier (Vec2 10 10) (Vec2 50 200) (Vec2 100 (-50)) (Vec2 140 90))
315337
-- stroke
316338
-- :}
317-
-- docs/haddock/Draw/instance_Sketch_Bezier.svg
339+
-- Generated file: size 2KB, crc32: 0xe17dab02
318340
instance Sketch Bezier where
319341
sketch (Bezier start (Vec2 x1 y1) (Vec2 x2 y2) (Vec2 x3 y3)) = do
320342
moveToVec start
@@ -365,7 +387,7 @@ data Arrow = Arrow !Line !ArrowSpec
365387
-- sketch (Arrow (Line (Vec2 10 10) (Vec2 140 90)) def)
366388
-- stroke
367389
-- :}
368-
-- docs/haddock/Draw/instance_Sketch_Arrow.svg
390+
-- Generated file: size 2KB, crc32: 0x2c724862
369391
instance Sketch Arrow where
370392
sketch (Arrow line ArrowSpec{..}) = do
371393
when _arrowDrawBody (sketch line)
@@ -426,7 +448,7 @@ instance Sketch a => Sketch (Maybe a) where
426448
-- sketch (Line (Vec2 10 10) (Vec2 140 90))
427449
-- stroke
428450
-- :}
429-
-- docs/haddock/Draw/instance_Sketch_Line.svg
451+
-- Generated file: size 2KB, crc32: 0x9287e4a8
430452
instance Sketch Line where
431453
sketch (Line start end) = do
432454
moveToVec start
@@ -443,7 +465,7 @@ instance Sketch Line where
443465
-- sketch (Polyline [Vec2 10 10, Vec2 90 90, Vec2 120 10, Vec2 140 50])
444466
-- stroke
445467
-- :}
446-
-- docs/haddock/Draw/instance_Sketch_Sequential_Vec2.svg
468+
-- Generated file: size 2KB, crc32: 0x5d5a0158
447469
instance Sketch Polyline where
448470
sketch (Polyline xs) = go xs
449471
where
@@ -462,7 +484,7 @@ instance Sketch Polyline where
462484
-- sketch (Polygon [Vec2 20 10, Vec2 10 80, Vec2 45 45, Vec2 60 90, Vec2 90 30])
463485
-- stroke
464486
-- :}
465-
-- docs/haddock/Draw/instance_Sketch_Polygon.svg
487+
-- Generated file: size 2KB, crc32: 0x7f620554
466488
instance Sketch Polygon where
467489
sketch (Polygon []) = pure ()
468490
sketch (Polygon xs) = sketch (Polyline xs) >> closePath
@@ -476,7 +498,7 @@ instance Sketch Polygon where
476498
-- sketch (Circle (Vec2 50 50) 45)
477499
-- stroke
478500
-- :}
479-
-- docs/haddock/Draw/instance_Sketch_Circle.svg
501+
-- Generated file: size 2KB, crc32: 0xebd35c6d
480502
instance Sketch Circle where
481503
sketch (Circle (Vec2 x y) r) = arc x y r 0 (2*pi)
482504

@@ -491,7 +513,7 @@ instance Sketch Circle where
491513
-- (toEllipse (Circle zero 45)))
492514
-- stroke
493515
-- :}
494-
-- docs/haddock/Draw/instance_Sketch_Ellipse.svg
516+
-- Generated file: size 2KB, crc32: 0x25bae2ef
495517
--
496518
instance Sketch Ellipse where
497519
sketch (Ellipse t) = cairoScope $ do
@@ -518,7 +540,7 @@ data Cross = Cross
518540
-- sketch (Cross (Vec2 60 20) 15) >> stroke
519541
-- sketch (Circle (Vec2 60 20) 15) >> stroke
520542
-- :}
521-
-- docs/haddock/Draw/instance_Sketch_Cross.svg
543+
-- Generated file: size 2KB, crc32: 0xe2cb8567
522544
instance Sketch Cross where
523545
sketch (Cross center r) = do
524546
let lowerRight = G.transform (rotateAround center (deg 45)) (center +. Vec2 r 0)
@@ -540,7 +562,7 @@ instance Sketch Cross where
540562
-- setColor (mathematica97 1) >> sketch (G.translate (Vec2 110 50) <> G.rotate (deg 30)) >> stroke
541563
-- setColor (mathematica97 2) >> sketch (G.shear 0.5 0.2 <> G.translate (Vec2 140 0)) >> stroke
542564
-- :}
543-
-- docs/haddock/Draw/instance_Sketch_Transformation.svg
565+
-- Generated file: size 4KB, crc32: 0x1f4ae5da
544566
instance Sketch Transformation where
545567
sketch t = do
546568
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
579601
-- sketch (boundingBox geometry)
580602
-- stroke
581603
-- :}
582-
-- docs/haddock/Draw/instance_Sketch_BoundingBox.svg
604+
-- Generated file: size 3KB, crc32: 0xfed2c044
583605
instance Sketch BoundingBox where
584606
sketch (BoundingBox (Vec2 xlo ylo) (Vec2 xhi yhi)) = do
585607
let w = xhi - xlo
@@ -622,7 +644,7 @@ instance Default CartesianParams where
622644
-- >>> :{
623645
-- haddockRender "Draw/cartesianCoordinateSystem.svg" 320 220 (cartesianCoordinateSystem def)
624646
-- :}
625-
-- docs/haddock/Draw/cartesianCoordinateSystem.svg
647+
-- Generated file: size 21KB, crc32: 0xf43aac0c
626648
cartesianCoordinateSystem :: CartesianParams -> Render ()
627649
cartesianCoordinateSystem params@CartesianParams{..} = grouped (paintWithAlpha _cartesianAlpha) $ do
628650
let vec2 x y = Vec2 (fromIntegral x) (fromIntegral y)
@@ -684,7 +706,7 @@ instance Default PolarParams where
684706
-- C.translate 50 50
685707
-- radialCoordinateSystem def
686708
-- :}
687-
-- docs/haddock/Draw/radialCoordinateSystem.svg
709+
-- Generated file: size 26KB, crc32: 0x9b68b36
688710
radialCoordinateSystem :: PolarParams -> Render ()
689711
radialCoordinateSystem PolarParams{_polarCenter=center, _polarMaxRadius=maxR} = cairoScope $ do
690712
setLineWidth 1
@@ -749,7 +771,7 @@ withOperator op actions = do
749771
-- sketch line
750772
-- stroke
751773
-- :}
752-
-- docs/haddock/Draw/cairoScope.svg
774+
-- Generated file: size 2KB, crc32: 0x2d7bee90
753775
--
754776
-- <<docs/haddock/Draw/cairoScope.svg>>
755777
cairoScope :: Render a -> Render a

src/Draw/Color.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class CairoColor color where
5050
-- sketch (Circle (Vec2 x 20) 10)
5151
-- C.fill
5252
-- :}
53-
-- docs/haddock/Draw/Color/set_color.svg
53+
-- Generated file: size 4KB, crc32: 0xe0e16234
5454
--
5555
-- <<docs/haddock/Draw/Color/set_color.svg>>
5656
setColor :: color -> C.Render ()

src/Draw/Plotting.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
-- for_ geometry $ \logoPart -> plot logoPart
1717
-- _plotPreview plotResult
1818
-- :}
19-
-- docs/haddock/Draw/Plotting/example.svg
19+
-- Generated file: size 10KB, crc32: 0x1e175925
2020
module Draw.Plotting (
2121
-- * 'Plot' type
2222
Plot()

src/Draw/Text.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ data HAlign = HLeft | HCenter | HRight deriving (Eq, Ord, Show)
4545
-- C.scale 3 3
4646
-- showTextAligned HCenter VCenter "Hello world!"
4747
-- :}
48-
-- docs/haddock/Draw/Text/show_text_aligned.svg
48+
-- Generated file: size 8KB, crc32: 0xefcaecf4
4949
showTextAligned
5050
:: C.CairoString string
5151
=> HAlign -- ^ Horizontal alignment
@@ -103,7 +103,7 @@ instance Default PlotTextOptions where
103103
-- glyphs = plotText opts "Hello world!"
104104
-- for_ glyphs $ \glyph -> sketch glyph >> stroke
105105
-- :}
106-
-- docs/haddock/Draw/Text/plot_text.svg
106+
-- Generated file: size 7KB, crc32: 0xd253d9dd
107107
plotText :: PlotTextOptions -> String -> [Polyline]
108108
plotText options text = transform (translate (_textStartingPoint options) <> scaleToHeight <> halign <> valign) glyphs
109109
where

src/Geometry/Algorithms/Clipping.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
-- setColor (mathematica97 i)
3636
-- stroke
3737
-- :}
38-
-- docs/haddock/Geometry/Algorithms/Clipping/complicated_intersection.svg
38+
-- Generated file: size 5KB, crc32: 0xd81cbd60
3939
module Geometry.Algorithms.Clipping (
4040
cutLineWithLine
4141
, CutLine(..)
@@ -85,7 +85,7 @@ import Geometry.Core
8585
-- sketch polygon
8686
-- stroke
8787
-- :}
88-
-- docs/haddock/Geometry/Algorithms/Clipping/hatched_polygon.svg
88+
-- Generated file: size 2KB, crc32: 0x81dce6d7
8989
hatch
9090
:: Polygon
9191
-> Angle -- ^ Direction in which the lines will point. @'deg' 0@ is parallel to the x axis.

src/Geometry/Algorithms/Clipping/CohenSutherland.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import Util
4646
-- sketch (boundingBoxPolygon mask)
4747
-- C.stroke
4848
-- :}
49-
-- docs/haddock/Geometry/Algorithms/Clipping/CohenSutherland/cohenSutherland.svg
49+
-- Generated file: size 22KB, crc32: 0xa5dc883f
5050
cohenSutherland :: BoundingBox -> Line -> Maybe Line
5151
cohenSutherland bb = \line -> let Line start end = line in loop line (outCode start) (outCode end)
5252
where

src/Geometry/Algorithms/Clipping/MargalitKnott.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ margalitKnott op Regular (polygonA, polygonA_Type) (polygonB', polygonB_Type) =
320320
-- fill
321321
-- sketch (p1, p2) >> stroke
322322
-- :}
323-
-- docs/haddock/Geometry/Algorithms/Clipping/MargalitKnott/union.svg
323+
-- Generated file: size 2KB, crc32: 0xcc4c9f5e
324324
unionPP :: Polygon -> Polygon -> [(Polygon, IslandOrHole)]
325325
unionPP = ppBinop Union
326326

@@ -342,7 +342,7 @@ unionPP = ppBinop Union
342342
-- fill
343343
-- sketch (p1, p2) >> stroke
344344
-- :}
345-
-- docs/haddock/Geometry/Algorithms/Clipping/MargalitKnott/intersection.svg
345+
-- Generated file: size 2KB, crc32: 0xdaf13db5
346346
intersectionPP :: Polygon -> Polygon -> [(Polygon, IslandOrHole)]
347347
intersectionPP = ppBinop Intersection
348348

@@ -361,7 +361,7 @@ intersectionPP = ppBinop Intersection
361361
-- fill
362362
-- sketch (p1, p2) >> stroke
363363
-- :}
364-
-- docs/haddock/Geometry/Algorithms/Clipping/MargalitKnott/difference.svg
364+
-- Generated file: size 2KB, crc32: 0x9388b325
365365
differencePP
366366
:: Polygon -- ^ A
367367
-> Polygon -- ^ B

src/Geometry/Algorithms/Clipping/SutherlandHodgman.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Geometry.Core
2828
-- sketch subject >> setColor (mathematica97 1) >> stroke
2929
-- sketch scissors >> setColor (mathematica97 3) >> setDash [3,3] 0 >> stroke
3030
-- :}
31-
-- docs/haddock/Geometry/Algorithms/Clipping/SutherlandHodgman/sutherland_hodgman.svg
31+
-- Generated file: size 2KB, crc32: 0x722cb97a
3232
sutherlandHodgman
3333
:: Polygon -- ^ Subject
3434
-> Polygon -- ^ __Convex__ scissors

0 commit comments

Comments
 (0)