From b03b0b9e48c60195976425cc5b666e1e6cd712ff Mon Sep 17 00:00:00 2001 From: Jaro Date: Wed, 29 Mar 2023 11:27:57 +0200 Subject: [PATCH] Prepare pagination for future layout types. --- balkon.cabal | 2 +- lib/Data/Text/ParagraphLayout.hs | 2 +- .../{ParagraphPagination.hs => Paginable.hs} | 37 ++++++++++++------- 3 files changed, 26 insertions(+), 15 deletions(-) rename src/Data/Text/ParagraphLayout/Internal/{ParagraphPagination.hs => Paginable.hs} (65%) diff --git a/balkon.cabal b/balkon.cabal index f01f83a..7233ebf 100644 --- a/balkon.cabal +++ b/balkon.cabal @@ -128,11 +128,11 @@ library balkon-internal Data.Text.ParagraphLayout.Internal.Fragment, Data.Text.ParagraphLayout.Internal.LineHeight, Data.Text.ParagraphLayout.Internal.LinePagination, + Data.Text.ParagraphLayout.Internal.Paginable, Data.Text.ParagraphLayout.Internal.Paragraph, Data.Text.ParagraphLayout.Internal.ParagraphConstruction, Data.Text.ParagraphLayout.Internal.ParagraphLayout, Data.Text.ParagraphLayout.Internal.ParagraphLine, - Data.Text.ParagraphLayout.Internal.ParagraphPagination, Data.Text.ParagraphLayout.Internal.Plain, Data.Text.ParagraphLayout.Internal.Rect, Data.Text.ParagraphLayout.Internal.ResolvedSpan, diff --git a/lib/Data/Text/ParagraphLayout.hs b/lib/Data/Text/ParagraphLayout.hs index fa9dcb9..028dd30 100644 --- a/lib/Data/Text/ParagraphLayout.hs +++ b/lib/Data/Text/ParagraphLayout.hs @@ -42,8 +42,8 @@ where import Data.Text.ParagraphLayout.Internal.Fragment import Data.Text.ParagraphLayout.Internal.LineHeight import Data.Text.ParagraphLayout.Internal.LinePagination +import Data.Text.ParagraphLayout.Internal.Paginable import Data.Text.ParagraphLayout.Internal.Paragraph import Data.Text.ParagraphLayout.Internal.ParagraphLayout -import Data.Text.ParagraphLayout.Internal.ParagraphPagination import Data.Text.ParagraphLayout.Internal.Plain import Data.Text.ParagraphLayout.Internal.Span diff --git a/src/Data/Text/ParagraphLayout/Internal/ParagraphPagination.hs b/src/Data/Text/ParagraphLayout/Internal/Paginable.hs similarity index 65% rename from src/Data/Text/ParagraphLayout/Internal/ParagraphPagination.hs rename to src/Data/Text/ParagraphLayout/Internal/Paginable.hs index d24dc0a..bb54e34 100644 --- a/src/Data/Text/ParagraphLayout/Internal/ParagraphPagination.hs +++ b/src/Data/Text/ParagraphLayout/Internal/Paginable.hs @@ -1,4 +1,8 @@ -module Data.Text.ParagraphLayout.Internal.ParagraphPagination +module Data.Text.ParagraphLayout.Internal.Paginable + (PageOptions(..) + ,Paginable + ,paginate + ) where import Data.Int (Int32) @@ -30,7 +34,7 @@ data PageOptions = PageOptions } --- | Break a paragraph in order to fit the given pagination constraints. +-- | Typeclass for layouts that can be broken into pages. -- -- The first component of the output determines whether a page break should -- be inserted before the paragraph. @@ -42,14 +46,21 @@ data PageOptions = PageOptions -- 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 +class Paginable pl where + paginate :: PageOptions -> pl -> (PageContinuity, pl, Maybe pl) + +instance Line a => Paginable [a] where + paginate opts ls = case paginateLines o w h1 h2 ls of + (c, p, []) -> (c, p, Nothing) + (c, p, rest) -> (c, p, Just rest) + where + o = pageOrphans opts + w = pageWidows opts + h1 = pageCurrentHeight opts + h2 = pageNextHeight opts + +-- | Break a paragraph in order to fit the given pagination constraints. +instance Paginable ParagraphLayout where + paginate opts pl = case paginate opts (cutLines pl) of + (c, p, Nothing) -> (c, mergeLines p, Nothing) + (c, p, Just rest) -> (c, mergeLines p, Just (mergeLines rest)) -- 2.30.2