@@ 1,6 1,5 @@
module Data.Text.ParagraphLayoutSpec (spec) where
-import Data.List (intersperse)
import Data.Text.Glyphize (Font)
import Test.Hspec
@@ 11,48 10,84 @@ import Data.Text.ParagraphLayout.FontLoader
import Data.Text.ParagraphLayout.ParagraphData
import Data.Text.ParagraphLayout.Rect
+class PrettyShow a where
+ prettyShow :: a -> String
-prettyShow :: ParagraphLayout -> String
-prettyShow (ParagraphLayout pr sls) = showParagraphLayout where
- showParagraphLayout = concat
+instance PrettyShow ParagraphLayout where
+ prettyShow (ParagraphLayout pr sls) = concat
[ "ParagraphLayout {paragraphRect = "
, show pr
- , ", spanLayouts = ["
+ , commaSpace
+ , "spanLayouts = ["
, newline
- , showSpanLayouts
+ , indent1
+ , concat $ indentedList indent1 $ map prettyShow sls
, newline
, "]}"
, newline
]
- showSpanLayouts = concat $ intersperse commaNewline $ map showSpanLayout sls
- showSpanLayout (SpanLayout frags) = concat
- [ indent1
- , "SpanLayout ["
- , concat $ intersperse ", " $ map showFrag frags
+
+instance PrettyShow SpanLayout where
+ prettyShow (SpanLayout frags) = concat
+ [ "SpanLayout ["
+ , concat $ inlineList $ map prettyShow frags
, "]"
]
- showFrag (Fragment r pen glyphs) = concat
+
+instance PrettyShow Fragment where
+ prettyShow (Fragment r pen glyphs) = concat
[ "Fragment {fragmentRect = "
, show r
- , ", "
+ , commaSpace
, "fragmentPen = "
, show pen
- , ", "
+ , commaSpace
, "fragmentGlyphs ="
, newline
, indent2
, "["
- , showGlyphs glyphs
+ , concat $ indentedList indent2 $ map show glyphs
, "]"
, newline
, indent1
, "}"
]
- showGlyphs = concat . intersperse (commaNewline ++ indent2) . map show
- indent1 = " "
- indent2 = indent1 ++ indent1
- newline = "\n"
- commaNewline = "," ++ newline
+
+inlineList :: [String] -> [String]
+inlineList items = suffixInit commaSpace items
+
+indentedList :: String -> [String] -> [String]
+indentedList indent items = prefixTail indent $ suffixInit commaNewline items
+
+suffixInit :: String -> [String] -> [String]
+suffixInit suffix = mapInit (++suffix)
+
+mapInit :: (a -> a) -> [a] -> [a]
+mapInit _ [] = []
+mapInit _ [x] = [x]
+mapInit f (x:y:ys) = f x : mapInit f (y:ys)
+
+prefixTail :: String -> [String] -> [String]
+prefixTail prefix = mapTail (prefix++)
+
+mapTail :: (a -> a) -> [a] -> [a]
+mapTail _ [] = []
+mapTail f (x:xs) = x:(map f xs)
+
+indent1 :: String
+indent1 = " "
+
+indent2 :: String
+indent2 = indent1 ++ indent1
+
+newline :: String
+newline = "\n"
+
+commaSpace :: String
+commaSpace = ", "
+
+commaNewline :: String
+commaNewline = "," ++ newline
shouldBeGolden :: ParagraphLayout -> FilePath -> Golden ParagraphLayout
shouldBeGolden output_ name = Golden