From 6e011c818a62f3d3ae7554f67f24fc8f3abf031b Mon Sep 17 00:00:00 2001 From: Jaro Date: Mon, 19 Jun 2023 20:56:24 +0200 Subject: [PATCH] Allow generating an empty run. --- src/Data/Text/ParagraphLayout/Internal/Run.hs | 17 ++++++++++++++++- .../Text/ParagraphLayout/Internal/RunSpec.hs | 13 +++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Data/Text/ParagraphLayout/Internal/Run.hs b/src/Data/Text/ParagraphLayout/Internal/Run.hs index 5f118bd..62bb083 100644 --- a/src/Data/Text/ParagraphLayout/Internal/Run.hs +++ b/src/Data/Text/ParagraphLayout/Internal/Run.hs @@ -78,7 +78,22 @@ considerNext z = case next z of data Merged a = Incompatible | Merged a spanToRuns :: ResolvedSpan d -> [Run] -spanToRuns s = snd $ mapAccumL run 0 $ protoRuns zipper levels +spanToRuns s + | Text.null (spanText s) = [emptyRun] + | otherwise = spanToRuns' s + where + emptyRun = Run + { runOffsetInSpan = 0 + , runText = Text.empty + , runLevel = defaultLevel + , runDirection = levelDirectionH defaultLevel + , runScript = Nothing + , runHardBreak = False + } + defaultLevel = baseLevel $ spanBiDiLevels s + +spanToRuns' :: ResolvedSpan d -> [Run] +spanToRuns' s = snd $ mapAccumL run 0 $ protoRuns zipper levels where wholeText = spanText s zipper = start wholeText diff --git a/test/Data/Text/ParagraphLayout/Internal/RunSpec.hs b/test/Data/Text/ParagraphLayout/Internal/RunSpec.hs index 4da8e80..8c7019c 100644 --- a/test/Data/Text/ParagraphLayout/Internal/RunSpec.hs +++ b/test/Data/Text/ParagraphLayout/Internal/RunSpec.hs @@ -1,6 +1,6 @@ module Data.Text.ParagraphLayout.Internal.RunSpec (spec) where -import Data.Text (Text, pack) +import Data.Text (Text, empty, pack) import Data.Text.Glyphize (Direction (..), emptyFont) import Test.Hspec @@ -49,7 +49,16 @@ spec = do it "handles span with no text" $ do let inputSpan = sampleSpan englishEmpty allLTR let runs = spanToRuns inputSpan - runs `shouldBe` [] + 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 -- 2.30.2