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