~jaro/balkon

ref: bc4ffa11a836f5dc52f2c60bfe6f9525c2539a01 balkon/src/Data/Text/ParagraphLayout/Internal/ParagraphPagination.hs -rw-r--r-- 2.0 KiB
bc4ffa11Jaro Add paragraph pagination. 1 year, 1 month 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
module Data.Text.ParagraphLayout.Internal.ParagraphPagination
where

import Data.Int (Int32)

import Data.Text.ParagraphLayout.Internal.LinePagination
import Data.Text.ParagraphLayout.Internal.ParagraphLayout
import Data.Text.ParagraphLayout.Internal.ParagraphLine

data PageOptions = PageOptions

    { pageCurrentHeight :: Int32
    -- ^ Amount of vertical space available for the paragraph
    -- on the current page.

    , pageNextHeight :: Int32
    -- ^ Expected amount of vertical space available for the paragraph
    -- on the next page.
    --
    -- If this is greater than `pageCurrentHeight`, the paragraph may be pushed
    -- onto the next page in order to better satisfy orphan/widow constraints.

    , pageOrphans :: Word
    -- ^ If a page break is required inside the paragraph, this will be the
    -- minimum number of lines to keep at the bottom of this page, if possible.

    , pageWidows :: Word
    -- ^ If a page break is required inside the paragraph, this will be the
    -- minimum number of lines to keep at the top of the next page, if possible.

    }

-- | Break a paragraph in order to fit the given pagination constraints.
--
-- The first component of the output determines whether a page break should
-- be inserted before the paragraph.
--
-- The second component of the output contains the portion of the paragraph
-- that fits on the page and satisfies the given constraints as much as
-- possible.
--
-- The third component of the output will be `Just` the remainder of the
-- paragraph that can be passed to this function again, or `Nothing` if there
-- is nothing left to put on further pages.
paginate :: PageOptions -> ParagraphLayout ->
    (PageContinuity, ParagraphLayout, Maybe ParagraphLayout)
paginate opts pl = case paginateLines o w h1 h2 ls of
    (c, ls1, []) -> (c, mergeLines ls1, Nothing)
    (c, ls1, ls2) -> (c, mergeLines ls1, Just (mergeLines ls2))
    where
        o = pageOrphans opts
        w = pageWidows opts
        h1 = pageCurrentHeight opts
        h2 = pageNextHeight opts
        ls = cutLines pl