From 8260355fb3f33df740336431f49546ec581eef92 Mon Sep 17 00:00:00 2001 From: Jaro Date: Sat, 25 Feb 2023 13:15:49 +0100 Subject: [PATCH] Store span index in ResolvedSpan. --- src/Data/Text/ParagraphLayout/Plain.hs | 15 +++++++++++++-- src/Data/Text/ParagraphLayout/ResolvedSpan.hs | 8 ++++++-- test/Data/Text/ParagraphLayout/SpanData.hs | 9 ++++++--- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Data/Text/ParagraphLayout/Plain.hs b/src/Data/Text/ParagraphLayout/Plain.hs index bee2bcf..01f1cef 100644 --- a/src/Data/Text/ParagraphLayout/Plain.hs +++ b/src/Data/Text/ParagraphLayout/Plain.hs @@ -93,6 +93,15 @@ data WithSpan a = WithSpan RS.ResolvedSpan a instance Functor WithSpan where fmap f (WithSpan s a) = WithSpan s (f a) +splitBySpanIndex :: [WithSpan a] -> [[a]] +splitBySpanIndex xs = [getBySpanIndex i xs | i <- [0..]] + +getBySpanIndex :: Int -> [WithSpan a] -> [a] +getBySpanIndex idx xs = map contents $ filter matchingIndex $ xs + where + matchingIndex (WithSpan rs _) = (RS.spanIndex rs) == idx + contents (WithSpan _ x) = x + spanRects :: SpanLayout -> [Rect Int32] spanRects (SpanLayout frags) = map fragmentRect frags @@ -190,9 +199,11 @@ shapeRun (WithSpan rs run) = shape font buffer features resolveSpans :: Paragraph -> [RS.ResolvedSpan] resolveSpans (Paragraph arr off spans opts) = do let texts = cuts arr off spans - (s, t) <- zip spans texts + let indexes = [0..] + (s, t, i) <- zip3 spans texts indexes return RS.ResolvedSpan - { RS.spanText = t + { RS.spanIndex = i + , RS.spanText = t , RS.spanFont = paragraphFont opts , RS.spanLineHeight = paragraphLineHeight opts , RS.spanLanguage = spanLanguage s diff --git a/src/Data/Text/ParagraphLayout/ResolvedSpan.hs b/src/Data/Text/ParagraphLayout/ResolvedSpan.hs index bc4485e..110c2cf 100644 --- a/src/Data/Text/ParagraphLayout/ResolvedSpan.hs +++ b/src/Data/Text/ParagraphLayout/ResolvedSpan.hs @@ -9,9 +9,13 @@ import Data.Text.ParagraphLayout.LineHeight -- | Internal structure containing resolved values that may be shared with -- other spans across the paragraph. data ResolvedSpan = ResolvedSpan - { spanText :: Text + { spanIndex :: Int + , spanText :: Text , spanFont :: Font , spanLineHeight :: LineHeight , spanLanguage :: String } - deriving (Eq, Show) + deriving (Show) + +instance Eq ResolvedSpan where + a == b = spanIndex a == spanIndex b diff --git a/test/Data/Text/ParagraphLayout/SpanData.hs b/test/Data/Text/ParagraphLayout/SpanData.hs index 2d9d987..ed23fe6 100644 --- a/test/Data/Text/ParagraphLayout/SpanData.hs +++ b/test/Data/Text/ParagraphLayout/SpanData.hs @@ -12,7 +12,8 @@ import Data.Text.ParagraphLayout.ResolvedSpan (ResolvedSpan(..)) emptySpan :: Font -> ResolvedSpan emptySpan font = ResolvedSpan - { spanText = pack "" + { spanIndex = 0 + , spanText = pack "" , spanFont = font , spanLineHeight = Normal , spanLanguage = "en" @@ -20,7 +21,8 @@ emptySpan font = ResolvedSpan czechHello :: Font -> ResolvedSpan czechHello font = ResolvedSpan - { spanText = pack "Ahoj, světe!" + { spanIndex = 0 + , spanText = pack "Ahoj, světe!" , spanFont = font , spanLineHeight = Normal , spanLanguage = "cs" @@ -28,7 +30,8 @@ czechHello font = ResolvedSpan serbianMixedScript :: Font -> ResolvedSpan serbianMixedScript font = ResolvedSpan - { spanText = pack "Vikipedija (Википедија)" + { spanIndex = 0 + , spanText = pack "Vikipedija (Википедија)" , spanFont = font , spanLineHeight = Normal , spanLanguage = "sr" -- 2.30.2