~jaro/balkon

ref: 817606194d4715b422c43d82fd37925b3794809e balkon/src/Data/Text/ParagraphLayout/Internal/ResolvedSpan.hs -rw-r--r-- 2.0 KiB
81760619Jaro Improve terminology around fragments. 1 year, 6 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
module Data.Text.ParagraphLayout.Internal.ResolvedSpan
    ( ResolvedSpan (..)
    , WithSpan (WithSpan)
    , splitBySpanIndex
    )
where

import Data.Text (Text)
import Data.Text.Glyphize (Font)
import qualified Data.Text.ICU as BreakStatus (Line)

import Data.Text.ParagraphLayout.Internal.BiDiReorder
import Data.Text.ParagraphLayout.Internal.LineHeight
import Data.Text.ParagraphLayout.Internal.TextContainer

-- | Internal structure containing resolved values that may be shared with
-- other spans across the paragraph.
data ResolvedSpan = ResolvedSpan
    { spanIndex :: Int
    , spanOffsetInParagraph :: Int
    , spanText :: Text
    , spanFont :: Font
    , spanLineHeight :: LineHeight
    , spanLanguage :: String
    , spanLineBreaks :: [(Int, BreakStatus.Line)]
    -- TODO: Can be optimised by starting with the shortest line break.
    , spanCharacterBreaks :: [(Int, ())]
    }
    deriving (Show)

instance Eq ResolvedSpan where
    a == b = spanIndex a == spanIndex b

instance TextContainer ResolvedSpan where
    getText = spanText

-- | Wrapper for temporarily mapping the relationship to a `ResolvedSpan`.
data WithSpan a = WithSpan ResolvedSpan a

instance Functor WithSpan where
    fmap f (WithSpan s a) = WithSpan s (f a)

instance TextContainer a => TextContainer (WithSpan a) where
    getText (WithSpan _ c) = getText c

instance SeparableTextContainer a => SeparableTextContainer (WithSpan a) where
    splitTextAt8 n (WithSpan rs c) = (WithSpan rs c1, WithSpan rs c2)
        where (c1, c2) = splitTextAt8 n c
    dropWhileStart p (WithSpan rs c) = WithSpan rs (dropWhileStart p c)
    dropWhileEnd p (WithSpan rs c) = WithSpan rs (dropWhileEnd p c)

instance WithLevel a => WithLevel (WithSpan a) where
    level (WithSpan _ x) = level x

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 _) = (spanIndex rs) == idx
        contents (WithSpan _ x) = x