~jaro/balkon

ref: 97faca5ffecc168c774af90056c365f03e047631 balkon/test/Data/Text/ParagraphLayout/Internal/RunSpec.hs -rw-r--r-- 4.9 KiB
97faca5fJaro Convert legacy span directions into levels. 1 year, 3 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
module Data.Text.ParagraphLayout.Internal.RunSpec (spec) where

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

import Test.Hspec
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.TextData

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

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

spec :: Spec
spec = do
    describe "spanToRuns" $ do
        it "handles span with no text" $ do
            let inputSpan = sampleSpan englishEmpty
            let runs = spanToRuns inputSpan
            runs `shouldBe` []
        it "handles Czech hello" $ do
            let inputSpan = sampleSpan czechHello
            let runs = spanToRuns inputSpan
            runs `shouldBe`
                [ Run
                    { runOffsetInSpan = 0
                    , runText = spanText inputSpan
                    , runDirection = Just DirLTR
                    , runScript = Just "Latn"
                    }
                ]
        it "handles Arabic hello" $ do
            let inputSpan = sampleSpan arabicHello
            let runs = spanToRuns inputSpan
            runs `shouldBe`
                [ Run
                    { runOffsetInSpan = 0
                    , runText = spanText inputSpan
                    , runDirection = Just DirRTL
                    , runScript = Just "Arab"
                    }
                ]
        it "handles Serbian with mixed script" $ do
            let inputSpan = sampleSpan serbianMixedScript
            let runs = spanToRuns inputSpan
            runs `shouldBe`
                [ Run
                    -- TODO: We might want both parentheses in the same run.
                    { runOffsetInSpan = 0
                    , runText = pack "Vikipedija ("
                    , runDirection = Just DirLTR
                    , runScript = Just "Latn"
                    }
                , Run
                    { runOffsetInSpan = 12
                    , runText = pack "Википедија)"
                    , runDirection = Just DirLTR
                    , runScript = Just "Cyrl"
                    }
                ]
        it "handles Arabic text with English inside" $ do
            let inputSpan = sampleSpan arabicAroundEnglish
            let runs = spanToRuns inputSpan
            runs `shouldBe`
                [ Run
                    { runOffsetInSpan = 0
                    , runText = pack "في "
                    , runDirection = Just DirRTL
                    , runScript = Just "Arab"
                    }
                , Run
                    { runOffsetInSpan = 5
                    , runText = pack "XHTML 1.0 "
                    , runDirection = Just DirLTR
                    , runScript = Just "Latn"
                    }
                , Run
                    { runOffsetInSpan = 15
                    , runText = pack "يتم تحقيق ذلك بإضافة العنصر المضمن "
                    , runDirection = Just DirRTL
                    , runScript = Just "Arab"
                    }
                , Run
                    { runOffsetInSpan = 79
                    , runText = pack "bdo."
                    , runDirection = Just DirLTR
                    , runScript = Just "Latn"
                    }
                ]
        it "handles English text with Arabic inside" $ do
            let inputSpan = sampleSpan englishAroundArabic
            let runs = spanToRuns inputSpan
            runs `shouldBe`
                [ Run
                    { runOffsetInSpan = 0
                    , runText = pack "The title is "
                    , runDirection = Just DirLTR
                    , runScript = Just "Latn"
                    }
                , Run
                    { runOffsetInSpan = 13
                    -- TODO: The final space should be excluded.
                    , runText = pack "مفتاح معايير الويب "
                    , runDirection = Just DirRTL
                    , runScript = Just "Arab"
                    }
                , Run
                    { runOffsetInSpan = 48
                    , runText = pack "in Arabic."
                    , runDirection = Just DirLTR
                    , runScript = Just "Latn"
                    }
                ]