From 7bc6a4f495cf792a2ba50f4c2e9cf52087bcd2e7 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Fri, 17 Mar 2023 17:38:12 +1300 Subject: [PATCH] Implement rudimentary pagination. --- Graphics/Layout.hs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Graphics/Layout.hs b/Graphics/Layout.hs index 000e92a..dd66e9c 100644 --- a/Graphics/Layout.hs +++ b/Graphics/Layout.hs @@ -292,6 +292,37 @@ boxHeight parent (LayoutInline val font self' vals) = boxHeight _ (LayoutSpan val font self') = (B.block $ fragmentSize' font self', LayoutSpan val font self') +boxSplit :: Double -> Double -> LayoutItem Double Double x -> + (LayoutItem Double Double x, Maybe (LayoutItem Double Double x)) +boxSplit maxheight _ node | height (layoutGetBox node) <= maxheight = (node, Nothing) +boxSplit maxheight pageheight (LayoutFlow val self childs) + | (next:_) <- childs1, ((y,_):_) <- childs0', + (tail,Just nextpage) <- boxSplit (maxheight - y) pageheight next = + (LayoutFlow val self { + size = (size self) { B.block = y } + } (childs0 ++ [tail]), + Just $ LayoutFlow val self { + size = (size self) { B.block = B.block (size self) - y } + } (nextpage:childs1)) + | otherwise = + (LayoutFlow val self { size = (size self) { B.block = maxheight } } childs0, + Just $ LayoutFlow val self childs1) -- TODO recompute height + where + childs0 = map snd childs0' + childs1 = map snd childs1' + (childs0', childs1') = break overflowed $ inner 0 childs + overflowed (y, _) = y >= maxheight + inner start (child:childs) = (start', child):inner start' childs -- TODO margin collapse? + where start' = start + height (layoutGetBox child) + inner _ [] = [] +boxSplit _ _ self@(LayoutGrid _ _ _) = (self, Nothing) -- TODO +boxSplit _ _ self@(LayoutInline _ _ _ _) = (self, Nothing) -- TODO +boxSplit _ _ self@(LayoutSpan _ _ _) = (self, Nothing) -- Can't split! +boxPaginate maxheight pageheight node + | (page, Just overflow) <- boxSplit maxheight pageheight node = + page:boxPaginate maxheight pageheight overflow + | otherwise = [node] + boxPosition :: (Double, Double) -> LayoutItem Double Double x -> LayoutItem Double Double ((Double, Double), x) boxPosition pos@(x, y) (LayoutFlow val self childs) = LayoutFlow (pos, val) self childs' -- 2.30.2