~jaro/balkon

ref: 3e3fef4440b5d982144bdb4b13a3d784ba686233 balkon/src/Data/Text/ParagraphLayout/Internal/Span.hs -rw-r--r-- 2.1 KiB
3e3fef44Jaro Add content rect to Fragment. 1 year, 2 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
module Data.Text.ParagraphLayout.Internal.Span
    ( Span (..)
    , SpanLayout (..)
    , SpanOptions (..)
    , defaultSpanOptions
    , spanFragments
    , spanRects
    )
where

import Data.Int (Int32)

import Data.Text.ParagraphLayout.Internal.Fragment
import Data.Text.ParagraphLayout.Internal.Rect

-- | A paragraph is broken into spans by the caller.
--
-- Each span could have a different font family, size, style, text decoration,
-- colour, language, etc.
data Span d = Span

    { spanUserData :: d
    -- ^ User-defined data associated with the span.

    , spanLength :: Int
    -- ^ Byte offset to the next span or the end of the paragraph text.

    , spanOptions :: SpanOptions
    -- ^ Options applying to this specific span.

    }
    deriving (Eq)

-- | Defines options relevant to the layout of a single span of text.
--
-- This record type is likely to be extended in the future.
-- Use `defaultSpanOptions` and update it with specific record selectors
-- instead of constructing `SpanOptions` directly.
data SpanOptions = SpanOptions

    { spanLanguage :: String
    -- ^ IETF BCP 47 language tag, such as the value expected to be found in
    -- the HTML @lang@ attribute, specifying the primary language for the
    -- span's text content. An empty string explicitly means "language unknown".
    --
    -- Used for selecting the appropriate glyphs and line breaking rules,
    -- primarily in East Asian languages.

    -- TODO: Add all relevant attributes.
    }
    deriving (Eq)

-- | `SpanOptions` with default values.
defaultSpanOptions :: SpanOptions
defaultSpanOptions = SpanOptions
    { spanLanguage = ""
    }

-- | The resulting layout of each span, which may include multiple fragments
-- as required by line breaking, text writing direction, and changes of script.
data SpanLayout d = SpanLayout [Fragment d]
    -- TODO: Consider merging fragments created by script changes.
    deriving (Eq, Read, Show)

-- | Return all fragments of shaped text from the given span.
spanFragments :: SpanLayout d -> [Fragment d]
spanFragments (SpanLayout frags) = frags

spanRects :: SpanLayout d -> [Rect Int32]
spanRects sl = map fragmentRect $ spanFragments sl