~jaro/balkon

ref: b36dcd9859e8d5209a08186555b8f799ad2c8b0f balkon/src/Data/Text/ParagraphLayout/Internal/AncestorBox.hs -rw-r--r-- 2.9 KiB
b36dcd98Jaro Describe box edges with extensible type. 11 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module Data.Text.ParagraphLayout.Internal.AncestorBox
    ( AncestorBox (..)
    , BoxEdge (..)
    , totalLeftSpacing
    , totalRightSpacing
    )
where

import Data.Int (Int32)

-- | Describes the relationship of a fragment to an inline box that contains it.
--
-- A box can have many fragments, and a fragment contained by a box is also
-- contained by all ancestors of that box.
--
-- The root inline box, which forms the basis of each paragraph, is implied
-- and not described by this type of record.
data AncestorBox d = AncestorBox

    { boxUserData :: d
    -- ^ User-defined data associated with the inline box.

    , boxLeftEdge :: BoxEdge
    -- ^ Describes the left edge of the inline box.
    -- Typically applied to the leftmost fragment in the box,
    -- and set to `NoEdge` for all other fragments.
    --
    -- Equal to `boxStartEdge` for LTR boxes.
    --
    -- Equal to `boxEndEdge` for RTL boxes.

    , boxRightEdge :: BoxEdge
    -- ^ Describes the right edge of the inline box.
    -- Typically applied to the rightmost fragment in the box,
    -- and set to `NoEdge` for all other fragments.
    --
    -- Equal to `boxStartEdge` for RTL boxes.
    --
    -- Equal to `boxEndEdge` for LTR boxes.

    , boxStartEdge :: BoxEdge
    -- ^ Describes the start edge of the inline box.
    -- Typically applied to the startmost fragment in the box,
    -- and set to `NoEdge` for all other fragments.
    --
    -- Equal to `boxLeftEdge` for LTR boxes.
    --
    -- Equal to `boxRightEdge` for RTL boxes.

    , boxEndEdge :: BoxEdge
    -- ^ Describes the end edge of the inline box.
    -- Typically applied to the endmost fragment in the box,
    -- and set to `NoEdge` for all other fragments.
    --
    -- Equal to `boxLeftEdge` for RTL boxes.
    --
    -- Equal to `boxRightEdge` for LTR boxes.

    }
    deriving (Eq, Read, Show)

-- | Describes either the absence, or the presence and size, of a box edge at a
-- given position.
data BoxEdge

    = NoEdge
    -- ^ The given box does not have an edge here.
    --
    -- This typically means that the box was fragmented and that the given edge
    -- is associated with another fragment.

    | SpacedEdge Int32
    -- ^ The given box has an edge here, adding a given amount of empty space
    -- from the `Data.Text.ParagraphLayout.Rich.fragmentRect` outward.

    deriving (Eq, Read, Show)

-- | Amount of empty space to add on account of the given `BoxEdge`.
edgeSpacing :: BoxEdge -> Int32
edgeSpacing NoEdge = 0
edgeSpacing (SpacedEdge s) = s

-- | Amount of empty space to add to the left side
-- on account of all given boxes in sum.
totalLeftSpacing :: [AncestorBox d] -> Int32
totalLeftSpacing bs = sum $ map (edgeSpacing . boxLeftEdge) bs

-- | Amount of empty space to add to the right side
-- on account of all given boxes in sum.
totalRightSpacing :: [AncestorBox d] -> Int32
totalRightSpacing bs = sum $ map (edgeSpacing . boxRightEdge) bs