~jaro/balkon

ref: 924bcdcbb55270f059795fd87cb28dae6e6d05e7 balkon/src/Data/Text/ParagraphLayout/Internal/ProtoFragment.hs -rw-r--r-- 1.8 KiB
924bcdcbJaro Separate vertical alignment from horizontal positioning. 10 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.ProtoFragment
    ( ProtoFragment (direction, verticalOffsets, advance, glyphs, hardBreak)
    , protoFragmentH
    , mapVerticalOffsets
    )
where

import Data.Int (Int32)
import Data.Text.Glyphize (Direction (..), GlyphInfo, GlyphPos (x_advance))

import qualified Data.Text.ParagraphLayout.Internal.BiDiLevels as BiDi
import qualified Data.Text.ParagraphLayout.Internal.VerticalOffsets as VO

-- | A box fragment which has not been positioned yet.
data ProtoFragment = ProtoFragment
    { direction :: Direction
    -- ^ Text direction, which is constant within a fragment.
    , level :: BiDi.Level
    -- ^ BiDi embedding level.
    -- Should be even for LTR fragments and odd for RTL fragments.
    , verticalOffsets :: VO.VerticalOffsets
    -- ^ Vertical offsets used for aligning the fragment within its line.
    , advance :: Int32
    -- ^ Total advance of glyphs in this fragment,
    -- depending on the text direction.
    , glyphs :: [(GlyphInfo, GlyphPos)]
    , hardBreak :: Bool
    -- ^ Marks fragment that ends with a forced line break.
    }

-- | Construct a `ProtoFragment`, automatically calculating the total advance
-- for a horizontal text direction.
protoFragmentH
    :: Direction
    -> BiDi.Level
    -> VO.VerticalOffsets
    -> [(GlyphInfo, GlyphPos)]
    -> Bool
    -> ProtoFragment
protoFragmentH dir lvl vo gs hard = ProtoFragment dir lvl vo adv gs hard
    where
        adv = sum $ map (x_advance . snd) gs

-- | Apply the given function to the `verticalOffsets` in the given fragment.
mapVerticalOffsets
    :: (VO.VerticalOffsets -> VO.VerticalOffsets)
    -> ProtoFragment
    -> ProtoFragment
mapVerticalOffsets mapFunc pf =
    pf { verticalOffsets = mapFunc $ verticalOffsets pf }

instance BiDi.WithLevel ProtoFragment where
    level = level -- BiDi.level = ProtoFragment.level