~jaro/balkon

ref: 7dc45d1deab5d7d7e967fe2056f322f0dabac0bc balkon/src/Data/Text/ParagraphLayout/Internal/VerticalOffsets.hs -rw-r--r-- 1.5 KiB
7dc45d1dJaro Implement vertical alignment. 1 year, 4 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
module Data.Text.ParagraphLayout.Internal.VerticalOffsets
    ( VerticalOffsets (..)
    , alignBaseline
    , alignLayoutTop
    , shift
    )
where

import Data.Int (Int32)

-- | Metrics used for vertical alignment of text fragments.
data VerticalOffsets = VerticalOffsets

    { layoutTop :: Int32
    -- ^ Y coordinate of the top edge of the fragment,
    -- including half-leading.

    , fontTop :: Int32
    -- ^ Y coordinate of the font's ascender.

    , baseline :: Int32
    -- ^ Y coordinate of the font's baseline.

    , fontBottom :: Int32
    -- ^ Y coordinate of the font's descender.

    , layoutBottom :: Int32
    -- ^ Y coordinate of the bottom edge of the fragment,
    -- including half-leading.

    }
    deriving (Eq, Show)

-- | Add a constant to each of the coordinates, effectively moving them
-- up by the given amount while preserving distances between them.
shift :: Int32 -> VerticalOffsets -> VerticalOffsets
shift d vo = vo
    { layoutTop = layoutTop vo + d
    , fontTop = fontTop vo + d
    , baseline = baseline vo + d
    , fontBottom = fontBottom vo + d
    , layoutBottom = layoutBottom vo + d
    }

-- | Set `layoutTop` to the given value and update all other coordinates
-- so that distances are preserved.
alignLayoutTop :: Int32 -> VerticalOffsets -> VerticalOffsets
alignLayoutTop x vo = shift (x - layoutTop vo) vo

-- | Set `baseline` to the given value and update all other coordinates
-- so that distances are preserved.
alignBaseline :: Int32 -> VerticalOffsets -> VerticalOffsets
alignBaseline x vo = shift (x - baseline vo) vo