From 3f953d1a770ba18cdea4bd6e7209969bae6c6819 Mon Sep 17 00:00:00 2001 From: Jaro Date: Sun, 26 Mar 2023 13:27:40 +0200 Subject: [PATCH] Separate module for the ParagraphLayout type. --- balkon.cabal | 1 + lib/Data/Text/ParagraphLayout.hs | 1 + .../ParagraphLayout/Internal/Paragraph.hs | 56 ---------------- .../Internal/ParagraphLayout.hs | 64 +++++++++++++++++++ .../Text/ParagraphLayout/Internal/Plain.hs | 1 + test/Data/Text/ParagraphLayoutSpec.hs | 2 +- 6 files changed, 68 insertions(+), 57 deletions(-) create mode 100644 src/Data/Text/ParagraphLayout/Internal/ParagraphLayout.hs diff --git a/balkon.cabal b/balkon.cabal index 006d320..fbaa9c8 100644 --- a/balkon.cabal +++ b/balkon.cabal @@ -100,6 +100,7 @@ library balkon-internal Data.Text.ParagraphLayout.Internal.LineHeight, Data.Text.ParagraphLayout.Internal.Paragraph, Data.Text.ParagraphLayout.Internal.ParagraphConstruction, + Data.Text.ParagraphLayout.Internal.ParagraphLayout, 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 dbc2de9..3823d50 100644 --- a/lib/Data/Text/ParagraphLayout.hs +++ b/lib/Data/Text/ParagraphLayout.hs @@ -33,5 +33,6 @@ where import Data.Text.ParagraphLayout.Internal.Fragment import Data.Text.ParagraphLayout.Internal.LineHeight import Data.Text.ParagraphLayout.Internal.Paragraph +import Data.Text.ParagraphLayout.Internal.ParagraphLayout import Data.Text.ParagraphLayout.Internal.Plain import Data.Text.ParagraphLayout.Internal.Span diff --git a/src/Data/Text/ParagraphLayout/Internal/Paragraph.hs b/src/Data/Text/ParagraphLayout/Internal/Paragraph.hs index 4f9c42e..443f218 100644 --- a/src/Data/Text/ParagraphLayout/Internal/Paragraph.hs +++ b/src/Data/Text/ParagraphLayout/Internal/Paragraph.hs @@ -1,24 +1,14 @@ module Data.Text.ParagraphLayout.Internal.Paragraph (Paragraph(..) - ,ParagraphLayout(..) ,ParagraphOptions(..) - ,paragraphLayout - ,paragraphOriginX - ,paragraphOriginY - ,paragraphSpanBounds - ,shapedRuns ) where import Data.Int (Int32) -import Data.List.NonEmpty (NonEmpty) -import qualified Data.List.NonEmpty as NonEmpty import Data.Text.Array (Array) import Data.Text.Glyphize (Font) -import Data.Text.ParagraphLayout.Internal.Fragment import Data.Text.ParagraphLayout.Internal.LineHeight -import Data.Text.ParagraphLayout.Internal.Rect import Data.Text.ParagraphLayout.Internal.Span -- | Text to be laid out as a single paragraph. @@ -80,49 +70,3 @@ data ParagraphOptions = ParagraphOptions } deriving (Eq, Show) - --- | The resulting layout of the whole paragraph. -data ParagraphLayout = ParagraphLayout - { paragraphRect :: Rect Int32 - -- ^ The containing block (CSS3). - , spanLayouts :: [SpanLayout] - } - deriving (Eq, Read, Show) - --- | Calculate the offsets into the `Paragraph`'s underlying `Array` where each --- span starts and ends, in ascending order. The resulting list will be one --- larger than the list of input spans. -paragraphSpanBounds :: Paragraph -> NonEmpty Int -paragraphSpanBounds (Paragraph _ initialOffset spans _) = - NonEmpty.scanl (+) initialOffset (map spanLength spans) - -paragraphOriginX :: (Num a) => a -paragraphOriginX = 0 - -paragraphOriginY :: (Num a) => a -paragraphOriginY = 0 - -empty :: (Num a) => Rect a -empty = Rect - { x_origin = paragraphOriginX - , y_origin = paragraphOriginY - , x_size = 0 - , y_size = 0 - } - -containRects :: (Ord a, Num a) => [Rect a] -> Rect a -containRects = foldr union empty - --- | Wrap the given `SpanLayout`s and compute their containing rectangle. -paragraphLayout :: [SpanLayout] -> ParagraphLayout -paragraphLayout sls = ParagraphLayout pRect sls - where pRect = containRects $ concat $ map spanRects sls - --- | Return all fragments of shaped text in one flat list, --- discarding information about their associated spans. -paragraphFragments :: ParagraphLayout -> [Fragment] -paragraphFragments pl = concat $ map spanFragments $ spanLayouts pl - --- | Return all shaped runs in the paragraph. -shapedRuns :: ParagraphLayout -> [ShapedRun] -shapedRuns pl = map shapedRun $ paragraphFragments pl diff --git a/src/Data/Text/ParagraphLayout/Internal/ParagraphLayout.hs b/src/Data/Text/ParagraphLayout/Internal/ParagraphLayout.hs new file mode 100644 index 0000000..1c1f86c --- /dev/null +++ b/src/Data/Text/ParagraphLayout/Internal/ParagraphLayout.hs @@ -0,0 +1,64 @@ +module Data.Text.ParagraphLayout.Internal.ParagraphLayout + (ParagraphLayout(..) + ,paragraphLayout + ,paragraphOriginX + ,paragraphOriginY + ,paragraphSpanBounds + ,shapedRuns + ) +where + +import Data.Int (Int32) +import Data.List.NonEmpty (NonEmpty) +import qualified Data.List.NonEmpty as NonEmpty + +import Data.Text.ParagraphLayout.Internal.Fragment +import Data.Text.ParagraphLayout.Internal.Paragraph +import Data.Text.ParagraphLayout.Internal.Rect +import Data.Text.ParagraphLayout.Internal.Span + +-- | The resulting layout of the whole paragraph. +data ParagraphLayout = ParagraphLayout + { paragraphRect :: Rect Int32 + -- ^ The containing block (CSS3). + , spanLayouts :: [SpanLayout] + } + deriving (Eq, Read, Show) + +-- | Calculate the offsets into the `Paragraph`'s underlying `Data.Text.Array` +-- where each span starts and ends, in ascending order. The resulting list +-- will be one larger than the list of input spans. +paragraphSpanBounds :: Paragraph -> NonEmpty Int +paragraphSpanBounds (Paragraph _ initialOffset spans _) = + NonEmpty.scanl (+) initialOffset (map spanLength spans) + +paragraphOriginX :: (Num a) => a +paragraphOriginX = 0 + +paragraphOriginY :: (Num a) => a +paragraphOriginY = 0 + +empty :: (Num a) => Rect a +empty = Rect + { x_origin = paragraphOriginX + , y_origin = paragraphOriginY + , x_size = 0 + , y_size = 0 + } + +containRects :: (Ord a, Num a) => [Rect a] -> Rect a +containRects = foldr union empty + +-- | Wrap the given `SpanLayout`s and compute their containing rectangle. +paragraphLayout :: [SpanLayout] -> ParagraphLayout +paragraphLayout sls = ParagraphLayout pRect sls + where pRect = containRects $ concat $ map spanRects sls + +-- | Return all fragments of shaped text in one flat list, +-- discarding information about their associated spans. +paragraphFragments :: ParagraphLayout -> [Fragment] +paragraphFragments pl = concat $ map spanFragments $ spanLayouts pl + +-- | Return all shaped runs in the paragraph. +shapedRuns :: ParagraphLayout -> [ShapedRun] +shapedRuns pl = map shapedRun $ paragraphFragments pl diff --git a/src/Data/Text/ParagraphLayout/Internal/Plain.hs b/src/Data/Text/ParagraphLayout/Internal/Plain.hs index 28cf0a9..1848878 100644 --- a/src/Data/Text/ParagraphLayout/Internal/Plain.hs +++ b/src/Data/Text/ParagraphLayout/Internal/Plain.hs @@ -33,6 +33,7 @@ import Data.Text.ParagraphLayout.Internal.Break import Data.Text.ParagraphLayout.Internal.Fragment import Data.Text.ParagraphLayout.Internal.LineHeight import Data.Text.ParagraphLayout.Internal.Paragraph +import Data.Text.ParagraphLayout.Internal.ParagraphLayout import qualified Data.Text.ParagraphLayout.Internal.ProtoFragment as PF import Data.Text.ParagraphLayout.Internal.Rect import Data.Text.ParagraphLayout.Internal.ResolvedSpan (WithSpan(WithSpan)) diff --git a/test/Data/Text/ParagraphLayoutSpec.hs b/test/Data/Text/ParagraphLayoutSpec.hs index 7129700..aa7a3fe 100644 --- a/test/Data/Text/ParagraphLayoutSpec.hs +++ b/test/Data/Text/ParagraphLayoutSpec.hs @@ -12,7 +12,7 @@ import System.FilePath (()) import Data.Text.ParagraphLayout import Data.Text.ParagraphLayout.FontLoader import Data.Text.ParagraphLayout.Internal.Fragment -import Data.Text.ParagraphLayout.Internal.Paragraph +import Data.Text.ParagraphLayout.Internal.ParagraphLayout import Data.Text.ParagraphLayout.ParagraphData import Data.Text.ParagraphLayout.Rect -- 2.30.2