~jaro/balkon

ref: 28ccd0eea7abd52f74e27f388aae4e0979909ca8 balkon/test/Data/Text/ParagraphLayout/PrettyShow.hs -rw-r--r-- 5.5 KiB
28ccd0eeJaro Prepare for extending ProtoRun. 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{-# OPTIONS_GHC -fno-warn-deprecations #-}
module Data.Text.ParagraphLayout.PrettyShow
    ( PrettyShow
    , prettyShow
    , Pages (..)
    , RichPages (..)
    , ShapedRuns (..)
    )
where

import Data.List (intersperse)
import Data.Text.ParagraphLayout
import Data.Text.ParagraphLayout.Internal.Fragment
    ( Fragment (Fragment)
    , ShapedRun
    )
import qualified Data.Text.ParagraphLayout.Plain as Plain
import qualified Data.Text.ParagraphLayout.Rich as Rich

class PrettyShow a where
    prettyShow :: a -> String

newtype ShapedRuns = ShapedRuns { getShapedRuns :: [ShapedRun] }
    deriving (Eq)

instance PrettyShow ShapedRuns where
    prettyShow (ShapedRuns xs) =
        concat $ commaFirstList indent0 $ map (prettyShow . ShapedRun') xs

newtype ShapedRun' = ShapedRun' ShapedRun

instance PrettyShow ShapedRun' where
    prettyShow (ShapedRun' (x, y, glyphs)) = concat
        [ "("
        , show x
        , ", "
        , show y
        , ", "
        , newline
        , concat $ commaFirstList indent1 $ map prettyShowPair glyphs
        , ")"
        ]

type Page d = (PageContinuity, Plain.ParagraphLayout d)

newtype Pages d = Pages { getPages :: [Page d] }
    deriving (Eq)

instance Show d => PrettyShow (Pages d) where
    prettyShow (Pages ps) =
        concat (commaFirstList indent0 $ map (prettyShow . Page') ps)
        ++ newline

newtype Page' d = Page' (Page d)

instance Show d => PrettyShow (Page' d) where
    prettyShow (Page' (c, pl)) = concat
        [ "("
        , show c
        , ", "
        , prettyShow pl
        , ")"
        ]

type RichPage d = (PageContinuity, Rich.ParagraphLayout d)

newtype RichPages d = RichPages { getRichPages :: [RichPage d] }
    deriving (Eq)

instance Show d => PrettyShow (RichPages d) where
    prettyShow (RichPages ps) =
        concat (commaFirstList indent0 $ map (prettyShow . RichPage') ps)
        ++ newline

newtype RichPage' d = RichPage' (RichPage d)

instance Show d => PrettyShow (RichPage' d) where
    prettyShow (RichPage' (c, pl)) = concat
        [ "("
        , show c
        , ", "
        , prettyShow pl
        , ")"
        ]

instance Show d => PrettyShow (Plain.ParagraphLayout d) where
    prettyShow (Plain.ParagraphLayout pr sls) = concat
        [ "ParagraphLayout"
        , newline
        , indent1
        , "{ paragraphRect = "
        , show pr
        , newline
        , indent1
        , ", spanLayouts = ["
        , newline
        , concat $ commaAloneList indent2 $ map prettyShow sls
        , newline
        , indent1
        , "]}"
        , newline
        ]

instance Show d => PrettyShow (Plain.SpanLayout d) where
    prettyShow (Plain.SpanLayout frags) = concat
        [ "SpanLayout"
        , newline
        , concat $ commaFirstList indent2 $ map prettyShow frags
        ]

instance Show d => PrettyShow (Fragment d) where
    prettyShow (Fragment d l bs r pen glyphs) = concat
        [ "Fragment"
        , newline
        , indent3
        , "{ fragmentUserData = "
        , show d
        , newline
        , indent3
        , ", fragmentLine = "
        , show l
        , newline
        , indent3
        , ", fragmentAncestorBoxes ="
        , newline
        , concat $ commaFirstList indent4 $ map show bs
        , newline
        , indent3
        , ", fragmentRect = "
        , show r
        , newline
        , indent3
        , ", fragmentPen = "
        , prettyShowPair pen
        , newline
        , indent3
        , ", fragmentGlyphs ="
        , newline
        , concat $ commaFirstList indent4 $ map prettyShowPair glyphs
        , newline
        , indent3
        , "}"
        ]

instance Show d => PrettyShow (Rich.ParagraphLayout d) where
    prettyShow (Rich.ParagraphLayout pr frags) = concat
        [ "ParagraphLayout"
        , newline
        , indent1
        , "{ paragraphRect = "
        , show pr
        , newline
        , indent1
        , ", paragraphFragments ="
        , newline
        , concat $ commaFirstList indent2 $ map prettyShow frags
        , newline
        , indent1
        , "}"
        , newline
        ]

prettyShowPair :: (Show a, Show b) => (a, b) -> String
prettyShowPair (a, b) = "(" ++ show a ++ ", " ++ show b ++ ")"

commaAloneList :: String -> [String] -> [String]
commaAloneList indent items =
    map (indent ++) $
    suffixInit newline $
    intersperse "," $
    items

commaFirstList :: String -> [String] -> [String]
commaFirstList indent [] = [indent ++ "[]"]
commaFirstList indent items =
    prefixHead (indent ++ "[ ") $
    prefixTail (indent ++ ", ") $
    suffixInit newline $
    suffixLast (newline ++ indent ++ "]") $
    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)

prefixHead :: String -> [String] -> [String]
prefixHead prefix = mapHead (prefix ++)

mapHead :: (a -> a) -> [a] -> [a]
mapHead _ [] = []
mapHead f (x : xs) = f x : xs

suffixLast :: String -> [String] -> [String]
suffixLast suffix = mapLast (++ suffix)

mapLast :: (a -> a) -> [a] -> [a]
mapLast _ [] = []
mapLast f [x] = [f x]
mapLast f (x : y : ys) = x : mapLast f (y : ys)

indent0 :: String
indent0 = ""

indent1 :: String
indent1 = "    "

indent2 :: String
indent2 = indent1 ++ indent1

indent3 :: String
indent3 = indent1 ++ indent2

indent4 :: String
indent4 = indent1 ++ indent3

newline :: String
newline = "\n"