~jaro/balkon

ref: 7e49e20750cc725749f4f9c89942a19b85049881 balkon/test/Data/Text/ParagraphLayout/RichSpec.hs -rw-r--r-- 19.2 KiB
7e49e207Jaro Prevent empty shaped runs. 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
module Data.Text.ParagraphLayout.RichSpec (spec) where

import Data.Int (Int32)
import Data.Word (Word32)
import Data.Text.Glyphize (Direction (DirLTR, DirRTL), codepoint)

import Test.Hspec
import System.FilePath ((</>))
import Data.Text.ParagraphLayout
import Data.Text.ParagraphLayout.FontLoader
import Data.Text.ParagraphLayout.Internal.Paginable (paginateAll)
import Data.Text.ParagraphLayout.Internal.ParagraphLine (forceLeftAlign)
import Data.Text.ParagraphLayout.Internal.Rich.ParagraphLayout (shapedRuns)
import Data.Text.ParagraphLayout.PrettyShow
import Data.Text.ParagraphLayout.PrettyShow.Golden
import Data.Text.ParagraphLayout.Rect
import Data.Text.ParagraphLayout.Rich
import Data.Text.ParagraphLayout.Rich.ParagraphData

fragmentRects :: ParagraphLayout d -> [(d, Rect Int32)]
fragmentRects p = map toItem $ paragraphFragments p
    where toItem (Fragment { fragmentUserData = d, fragmentRect = r }) = (d, r)

glyphRuns :: ParagraphLayout d -> [[Word32]]
glyphRuns = map (map (codepoint . fst) . fragmentGlyphs) . paragraphFragments

-- | A sufficiently large number to prevent line breaking,
-- while being round so that coordinates in test results are easy to read.
largeWidth :: Int32
largeWidth = 1000000000

-- | Expect two paragraph layouts to contain the same fragments,
-- except possibly shifted left or right.
shouldBeWrappedLike :: (Show d, Eq d) =>
    ParagraphLayout d -> ParagraphLayout d -> Expectation
shouldBeWrappedLike a b =
    forceLeftAlign a `shouldBe` forceLeftAlign b

-- | Expect two paragraph layouts to not contain the same fragments,
-- even if ignoring a possible shift to the left or to the right.
shouldNotBeWrappedLike :: (Show d, Eq d) =>
    ParagraphLayout d -> ParagraphLayout d -> Expectation
shouldNotBeWrappedLike a b =
    forceLeftAlign a `shouldNotBe` forceLeftAlign b

spec :: Spec
spec = do

    describe "layoutRich" $ do
        let
            goldenDir = ".golden" </> "richParagraphLayout"
            shouldBeGolden = goldenTest goldenDir id id

        describe "with Latin font" $ do
            font <- runIO $ loadFont latinFont 0 testingOptions
            fontSmall <- runIO $ loadFont latinFont 0 testingOptionsSmall

            let commonWrapWidth = 20000
            let expectedSafeWidth = 19791

            describe "lorem ipsum at 20em, left aligned" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignLeft
                        }
                let input w = loremIpsumParagraph font $
                        opts { paragraphMaxWidth = w }
                let result20em = layoutRich $ input commonWrapWidth
                let resultSafe = layoutRich $ input expectedSafeWidth
                let resultUnsafe = layoutRich $ input (expectedSafeWidth - 1)

                it "wraps correctly" $
                    result20em `shouldBeGolden` "loremIpsum20em"

                it "gives correct safe width" $
                    paragraphSafeWidth result20em `shouldBe` expectedSafeWidth

                it "wraps the same at safe width" $
                    resultSafe `shouldBeWrappedLike` result20em

                it "does not wrap the same at smaller width" $
                    resultUnsafe `shouldNotBeWrappedLike` result20em

            describe "lorem ipsum at 20em, right aligned" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignRight
                        }
                let input w = loremIpsumParagraph font $
                        opts { paragraphMaxWidth = w }
                let result20em = layoutRich $ input commonWrapWidth
                let resultSafe = layoutRich $ input expectedSafeWidth
                let resultUnsafe = layoutRich $ input (expectedSafeWidth - 1)

                it "wraps correctly" $
                    result20em `shouldBeGolden` "loremIpsum20emRight"

                it "gives correct safe width" $
                    paragraphSafeWidth result20em `shouldBe` expectedSafeWidth

                it "wraps the same at safe width" $
                    resultSafe `shouldBeWrappedLike` result20em

                it "does not wrap the same at smaller width" $
                    resultUnsafe `shouldNotBeWrappedLike` result20em

            describe "lorem ipsum at 20em, centred" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignCentreH
                        }
                let input w = loremIpsumParagraph font $
                        opts { paragraphMaxWidth = w }
                let result20em = layoutRich $ input commonWrapWidth
                let resultSafe = layoutRich $ input expectedSafeWidth
                let resultUnsafe = layoutRich $ input (expectedSafeWidth - 1)
                let resultUnsafe2 = layoutRich $ input (expectedSafeWidth - 2)

                it "wraps correctly" $
                    result20em `shouldBeGolden` "loremIpsum20emCentre"

                it "gives correct safe width" $
                    paragraphSafeWidth result20em `shouldBe` expectedSafeWidth

                it "wraps the same at safe width" $
                    resultSafe `shouldBeWrappedLike` result20em

                it "does not wrap the same at smaller width" $
                    resultUnsafe `shouldNotBeWrappedLike` result20em

                -- Cover for false negatives when halving odd numbers.
                it "does not wrap the same at 2 units smaller width" $
                    resultUnsafe2 `shouldNotBeWrappedLike` result20em

            it "handles mixed line height" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let lineHeights = (800, 1300, 1700)
                let input = mixedLineHeightParagraph lineHeights font opts
                let result = layoutRich input
                result `shouldBeGolden` "mixedLineHeight"

            it "handles mixed sizes" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = mixedSizesParagraph (font, fontSmall) opts
                let result = layoutRich input
                result `shouldBeGolden` "mixedSizes"

            it "handles mixed script" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = mixedScriptParagraph font opts
                let result = layoutRich input
                result `shouldBeGolden` "mixedScript"

            it "handles mixed script with line wraps" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = 5000
                        }
                let input = mixedScriptParagraph font opts
                let result = layoutRich input
                result `shouldBeGolden` "mixedScriptWrap"

            it "handles nested boxes" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = nestedBoxesParagraph font opts
                let result = layoutRich input
                result `shouldBeGolden` "nestedBoxes"

            it "handles hard break in LTR boxes" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = hardBoxBreakLTRParagraph font opts
                let result = layoutRich input
                result `shouldBeGolden` "hardBoxBreakLTR"

            it "breaks line early if box spacing does not fit" $ do
                let lineH = -1121
                let lineY n = lineH * (n - 1)
                let wordAdvancesWithSpace = [3134, 2954, 2659, 1332, 2601]
                let spaceAdvance = 231
                let wordW n = wordAdvancesWithSpace !! (n - 1)
                let wordW_ n = wordW n - spaceAdvance
                let originX = 0
                let wordX n =
                        originX + (sum $ take (n - 1) wordAdvancesWithSpace)
                let endX = originX + (sum wordAdvancesWithSpace)
                -- A paragraph exactly wide enough to fit the whole text.
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = endX
                        }
                -- Small spacing that should push one word onto the next line.
                let s = 5
                -- Large spacing that should push two words onto the next line.
                let s' = wordW 5 + spaceAdvance + 1
                let inputNoSpacing = softBreakParagraph 0 font opts
                let inputSmallSpacing = softBreakParagraph s font opts
                let inputLargeSpacing = softBreakParagraph s' font opts
                let resultNoSpacing = layoutRich inputNoSpacing
                let resultSmallSpacing = layoutRich inputSmallSpacing
                let resultLargeSpacing = layoutRich inputLargeSpacing
                fragmentRects resultNoSpacing `shouldBe`
                    [ ("text1", Rect (wordX 1) (lineY 1) (wordW 1) lineH)
                    , ("text2", Rect (wordX 2) (lineY 1) (wordW 2) lineH)
                    , ("text3", Rect (wordX 3) (lineY 1) (wordW 3) lineH)
                    , ("text4", Rect (wordX 4) (lineY 1) (wordW 4) lineH)
                    , ("text5", Rect (wordX 5) (lineY 1) (wordW 5) lineH)
                    ]
                fragmentRects resultSmallSpacing `shouldBe`
                    [ ("text1", Rect (wordX 1) (lineY 1) (wordW 1) lineH)
                    , ("text2", Rect (wordX 2) (lineY 1) (wordW 2) lineH)
                    , ("text3", Rect (wordX 3 + s) (lineY 1) (wordW 3) lineH)
                    , ("text4", Rect (wordX 4 + s) (lineY 1) (wordW_ 4) lineH)
                    , ("text5", Rect originX (lineY 2) (wordW 5) lineH)
                    ]
                fragmentRects resultLargeSpacing `shouldBe`
                    [ ("text1", Rect (wordX 1) (lineY 1) (wordW 1) lineH)
                    , ("text2", Rect (wordX 2) (lineY 1) (wordW 2) lineH)
                    , ("text3", Rect (wordX 3 + s') (lineY 1) (wordW_ 3) lineH)
                    , ("text4", Rect originX (lineY 2) (wordW 4) lineH)
                    , ("text5",
                        Rect (originX + wordW 4) (lineY 2) (wordW 5) lineH)
                    ]

        describe "with Arabic font" $ do
            font <- runIO $ loadFont arabicFont 0 testingOptions

            it "makes space for one newline" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = newline1Paragraph font opts
                let result = layoutRich input
                result `shouldBeGolden` "newline1Paragraph"

            it "makes space for one newline plus text" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = newline1TextParagraph font opts
                let result = layoutRich input
                result `shouldBeGolden` "newline1TextParagraph"

            it "makes space for two newlines" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = newline2Paragraph font opts
                let result = layoutRich input
                result `shouldBeGolden` "newline2Paragraph"

            it "makes space for two newlines plus text" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = newline2TextParagraph font opts
                let result = layoutRich input
                result `shouldBeGolden` "newline2TextParagraph"

            it "handles hard break in RTL boxes" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = hardBoxBreakRTLParagraph font opts
                let result = layoutRich input
                result `shouldBeGolden` "hardBoxBreakRTL"

            it "handles neutral characters in RTL paragraph" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = neutralDirectionParagraph DirRTL font opts
                let result = layoutRich input
                result `shouldBeGolden` "neutralDirectionRTL"

            it "handles neutral characters in LTR paragraph" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = neutralDirectionParagraph DirLTR font opts
                let result = layoutRich input
                result `shouldBeGolden` "neutralDirectionLTR"

            it "handles simple mixed direction in RTL paragraph" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = mixedDirectionSimpleParagraph DirRTL font opts
                let result = layoutRich input
                result `shouldBeGolden` "mixedDirectionSimpleRTL"

            it "handles simple mixed direction in LTR paragraph" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = mixedDirectionSimpleParagraph DirLTR font opts
                let result = layoutRich input
                result `shouldBeGolden` "mixedDirectionSimpleLTR"

            it "handles complex mixed direction in RTL paragraph" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = mixedDirectionComplexParagraph DirRTL font opts
                let result = layoutRich input
                result `shouldBeGolden` "mixedDirectionComplexRTL"

            it "handles complex mixed direction in LTR paragraph" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = largeWidth
                        }
                let input = mixedDirectionComplexParagraph DirLTR font opts
                let result = layoutRich input
                result `shouldBeGolden` "mixedDirectionComplexLTR"

            it "preserves joining forms across soft breaks" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = 4500
                        }
                let input = intraWordBreakParagraph font opts
                let glyphRunsVisual = glyphRuns $ layoutRich input
                let glyphRunsLogical = map reverse glyphRunsVisual
                -- Glyph indexes for the corresponding Arabic letters/ligatures:
                let n_initial = 407
                let v_final = 971
                let sh_initial = 373
                let tn_final = 1363
                -- Glyph index for the underscore used to break repetitions:
                let sp = 89
                -- Unconventional code formatting here shows that there should
                -- be three repetitions of the same glyph sequence, unaffected
                -- by the line break inserted after an initial form and before
                -- a final form.
                glyphRunsLogical `shouldBe`
                    [[n_initial, v_final, sh_initial, tn_final, sp,
                    n_initial, v_final, sh_initial], [tn_final, sp,
                    n_initial, v_final, sh_initial, tn_final]]

    describe "paginate" $ do
        let
            goldenDir = ".golden" </> "paginatedRichParagraphLayout"
            shouldBeGolden = goldenTest goldenDir getRichPages RichPages

        describe "with Latin font" $ do
            font <- runIO $ loadFont latinFont 0 testingOptions

            it "wraps lorem ipsum at 20em" $ do
                let opts = defaultParagraphOptions
                        { paragraphAlignment = AlignStart
                        , paragraphMaxWidth = 20000
                        }
                let input = loremIpsumParagraph font opts
                let pl = layoutRich input
                let popts = PageOptions
                        { pageCurrentHeight = 2500
                        , pageNextHeight = 8500
                        , pageOrphans = 2
                        , pageWidows = 3
                        }
                let pages = paginateAll popts pl
                pages `shouldBeGolden` "loremIpsum20em"

    describe "shaped runs for demo" $ do
        let
            goldenDir = ".golden" </> "shapedRuns"
            shouldBeGolden = goldenTest goldenDir getShapedRuns ShapedRuns
            -- | Test shaped runs against an expected value,
            -- and write metadata about the used font afterwards.
            shapedRunsSpecWithFont fontPath font subject name result = do
                let infoPath = fontInfoPath goldenDir name
                let writeInfo = writeFontInfo infoPath fontPath font
                after_ writeInfo $ it subject $
                    shapedRuns result `shouldBeGolden` name

        describe "with Arabic font" $ do
            let fontPath = arabicFont
            font <- runIO $ loadFont fontPath 0 demoOptions

            let shapedRunsSpec = shapedRunsSpecWithFont fontPath font

            shapedRunsSpec
                "handles complex mixed direction in RTL paragraph"
                "mixedDirectionComplexRTL" $
                layoutRich $
                mixedDirectionComplexParagraph DirRTL font $
                defaultParagraphOptions
                    { paragraphAlignment = AlignStart
                    , paragraphMaxWidth = 640
                    }

            shapedRunsSpec
                "preserves joining forms across soft breaks"
                "intraWordBreak" $
                layoutRich $
                intraWordBreakParagraph font $
                defaultParagraphOptions
                    { paragraphAlignment = AlignStart
                    , paragraphMaxWidth = 144
                    }