~jaro/balkon

ref: b7e2da83c6eaeb921f5113e58f2f1fa4e580e292 balkon/test/Data/Text/ParagraphLayout/Internal/RunSpec.hs -rw-r--r-- 11.4 KiB
b7e2da83Jaro Do not pass around unnecessary data. 1 year, 4 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
module Data.Text.ParagraphLayout.Internal.RunSpec (spec) where

import Data.Text (Text, empty, pack)
import Data.Text.Glyphize (Direction (..), emptyFont)

import Test.Hspec
import Data.Text.ParagraphLayout.Internal.BiDiLevels
import Data.Text.ParagraphLayout.Internal.BoxOptions
import Data.Text.ParagraphLayout.Internal.LineHeight
import Data.Text.ParagraphLayout.Internal.ResolvedBox
import Data.Text.ParagraphLayout.Internal.ResolvedSpan
import Data.Text.ParagraphLayout.Internal.Run
import Data.Text.ParagraphLayout.Internal.TextOptions
import Data.Text.ParagraphLayout.RunLengthEncoding
import Data.Text.ParagraphLayout.TextData

defaultBox :: Direction -> ResolvedBox ()
defaultBox dir =
    ResolvedBox () 0 (defaultTextOptions dir) defaultBoxOptions dir

sampleSpan :: (Direction, String, Text, a) -> TextLevels -> ResolvedSpan ()
sampleSpan (dir, lang, text, _) levels = ResolvedSpan
    { spanUserData = ()
    , spanIndex = 0
    , spanOffsetInParagraph = 0
    , spanText = text
    , spanTextOptions = (defaultTextOptions dir)
        { textFont = emptyFont
        , textLineHeight = Normal
        , textLanguage = lang
        }
    , spanBoxes = [defaultBox dir]
    , spanBiDiLevels = levels
    , spanLineBreaks = []
    , spanCharacterBreaks = []
    }

allLTR :: TextLevels
allLTR = TextLevels (repeat 0) 0

allRTL :: TextLevels
allRTL = TextLevels (repeat 1) 1

levelsRLE :: [(Int, Level)] -> TextLevels
levelsRLE rls = TextLevels (runLengthDecode rls) undefined

spec :: Spec
spec = do
    describe "spanToRuns" $ do
        it "handles span with no text" $ do
            let inputSpan = sampleSpan englishEmpty allLTR
            let runs = spanToRuns inputSpan
            runs `shouldBe`
                [ Run
                    { runOffsetInSpan = 0
                    , runText = empty
                    , runLevel = 0
                    , runDirection = DirLTR
                    , runScript = Nothing
                    , runHardBreak = False
                    }
                ]
        it "handles Czech hello" $ do
            let inputSpan = sampleSpan czechHello allLTR
            let runs = spanToRuns inputSpan
            runs `shouldBe`
                [ Run
                    { runOffsetInSpan = 0
                    , runText = spanText inputSpan
                    , runLevel = 0
                    , runDirection = DirLTR
                    , runScript = Just "Latn"
                    , runHardBreak = False
                    }
                ]
        it "handles Arabic hello" $ do
            let inputSpan = sampleSpan arabicHello allRTL
            let runs = spanToRuns inputSpan
            runs `shouldBe`
                [ Run
                    { runOffsetInSpan = 0
                    , runText = spanText inputSpan
                    , runLevel = 1
                    , runDirection = DirRTL
                    , runScript = Just "Arab"
                    , runHardBreak = False
                    }
                ]
        it "handles Serbian with mixed script" $ do
            let inputSpan = sampleSpan serbianMixedScript allLTR
            let runs = spanToRuns inputSpan
            runs `shouldBe`
                [ Run
                    -- TODO: We might want both parentheses in the same run.
                    { runOffsetInSpan = 0
                    , runText = pack "Vikipedija ("
                    , runLevel = 0
                    , runDirection = DirLTR
                    , runScript = Just "Latn"
                    , runHardBreak = False
                    }
                , Run
                    { runOffsetInSpan = 12
                    , runText = pack "Википедија)"
                    , runLevel = 0
                    , runDirection = DirLTR
                    , runScript = Just "Cyrl"
                    , runHardBreak = False
                    }
                ]
        it "handles mixed direction with base LTR" $ do
            let levels = levelsRLE [(7, 0), (3, 1), (6, 0)]
            let inputSpan = sampleSpan (mixedDirectionSimple DirLTR) levels
            let runs = spanToRuns inputSpan
            runs `shouldBe`
                [ Run
                    { runOffsetInSpan = 0
                    , runText = pack "bahrain"
                    , runLevel = 0
                    , runDirection = DirLTR
                    , runScript = Just "Latn"
                    , runHardBreak = False
                    }
                , Run
                    { runOffsetInSpan = 7
                    , runText = pack "مصر"
                    , runLevel = 1
                    , runDirection = DirRTL
                    , runScript = Just "Arab"
                    , runHardBreak = False
                    }
                , Run
                    { runOffsetInSpan = 13
                    , runText = pack "kuwait"
                    , runLevel = 0
                    , runDirection = DirLTR
                    , runScript = Just "Latn"
                    , runHardBreak = False
                    }
                ]
        it "handles mixed direction with base RTL" $ do
            let levels = levelsRLE [(7, 2), (3, 1), (6, 2)]
            let inputSpan = sampleSpan (mixedDirectionSimple DirRTL) levels
            let runs = spanToRuns inputSpan
            runs `shouldBe`
                [ Run
                    { runOffsetInSpan = 0
                    , runText = pack "bahrain"
                    , runLevel = 2
                    , runDirection = DirLTR
                    , runScript = Just "Latn"
                    , runHardBreak = False
                    }
                , Run
                    { runOffsetInSpan = 7
                    , runText = pack "مصر"
                    , runLevel = 1
                    , runDirection = DirRTL
                    , runScript = Just "Arab"
                    , runHardBreak = False
                    }
                , Run
                    { runOffsetInSpan = 13
                    , runText = pack "kuwait"
                    , runLevel = 2
                    , runDirection = DirLTR
                    , runScript = Just "Latn"
                    , runHardBreak = False
                    }
                ]
        it "handles Arabic text with English inside" $ do
            let levels = levelsRLE [(3, 1), (9, 2), (36, 1), (3, 2), (1, 1)]
            let inputSpan = sampleSpan arabicAroundEnglish levels
            let runs = spanToRuns inputSpan
            runs `shouldBe`
                [ Run
                    { runOffsetInSpan = 0
                    , runText = pack "في "
                    , runLevel = 1
                    , runDirection = DirRTL
                    , runScript = Just "Arab"
                    , runHardBreak = False
                    }
                , Run
                    { runOffsetInSpan = 5
                    , runText = pack "XHTML 1.0"
                    , runLevel = 2
                    , runDirection = DirLTR
                    , runScript = Just "Latn"
                    , runHardBreak = False
                    }
                , Run
                    { runOffsetInSpan = 14
                    , runText = pack " يتم تحقيق ذلك بإضافة العنصر المضمن "
                    , runLevel = 1
                    , runDirection = DirRTL
                    , runScript = Just "Arab"
                    , runHardBreak = False
                    }
                , Run
                    { runOffsetInSpan = 79
                    , runText = pack "bdo"
                    , runLevel = 2
                    , runDirection = DirLTR
                    , runScript = Just "Latn"
                    , runHardBreak = False
                    }
                , Run
                    { runOffsetInSpan = 82
                    , runText = pack "."
                    , runLevel = 1
                    , runDirection = DirRTL
                    , runScript = Just "Zyyy"
                    , runHardBreak = False
                    }
                ]
        it "handles English text with Arabic inside" $ do
            let levels = levelsRLE [(13, 0), (18, 1), (11, 0)]
            let inputSpan = sampleSpan englishAroundArabic levels
            let runs = spanToRuns inputSpan
            runs `shouldBe`
                [ Run
                    { runOffsetInSpan = 0
                    , runText = pack "The title is "
                    , runLevel = 0
                    , runDirection = DirLTR
                    , runScript = Just "Latn"
                    , runHardBreak = False
                    }
                , Run
                    { runOffsetInSpan = 13
                    , runText = pack "مفتاح معايير الويب"
                    , runLevel = 1
                    , runDirection = DirRTL
                    , runScript = Just "Arab"
                    , runHardBreak = False
                    }
                , Run
                    { runOffsetInSpan = 47
                    , runText = pack " in Arabic."
                    , runLevel = 0
                    , runDirection = DirLTR
                    , runScript = Just "Latn"
                    , runHardBreak = False
                    }
                ]
        -- Unrealistic example where text changes direction
        -- without changing script.
        it "handles script and direction changes" $ do
            let levels = levelsRLE [(4, 1), (4, 2), (8, 1), (4, 2), (3, 1)]
            let inputSpan = sampleSpan serbianMixedScript levels
            let runs = spanToRuns inputSpan
            runs `shouldBe`
                [ Run
                    { runOffsetInSpan = 0
                    , runText = pack "Viki"
                    , runLevel = 1
                    , runDirection = DirRTL
                    , runScript = Just "Latn"
                    , runHardBreak = False
                    }
                -- direction change
                , Run
                    { runOffsetInSpan = 4
                    , runText = pack "pedi"
                    , runLevel = 2
                    , runDirection = DirLTR
                    , runScript = Just "Latn"
                    , runHardBreak = False
                    }
                -- direction change
                , Run
                    { runOffsetInSpan = 8
                    , runText = pack "ja ("
                    , runLevel = 1
                    , runDirection = DirRTL
                    , runScript = Just "Latn"
                    , runHardBreak = False
                    }
                -- script change
                , Run
                    { runOffsetInSpan = 12
                    , runText = pack "Вики"
                    , runLevel = 1
                    , runDirection = DirRTL
                    , runScript = Just "Cyrl"
                    , runHardBreak = False
                    }
                -- direction change
                , Run
                    { runOffsetInSpan = 20
                    , runText = pack "педи"
                    , runLevel = 2
                    , runDirection = DirLTR
                    , runScript = Just "Cyrl"
                    , runHardBreak = False
                    }
                -- direction change
                , Run
                    { runOffsetInSpan = 28
                    , runText = pack "ја)"
                    , runLevel = 1
                    , runDirection = DirRTL
                    , runScript = Just "Cyrl"
                    , runHardBreak = False
                    }
                ]