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"
}
]