From 95ceab6991b5b491ebe39374989d3bcf7d12314c Mon Sep 17 00:00:00 2001 From: Jaro Date: Sat, 17 Jun 2023 00:10:44 +0200 Subject: [PATCH] Implement calculation of safe paragraph width. --- .../Internal/Rich/ParagraphLayout.hs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Data/Text/ParagraphLayout/Internal/Rich/ParagraphLayout.hs b/src/Data/Text/ParagraphLayout/Internal/Rich/ParagraphLayout.hs index 719a6de..30bd3fa 100644 --- a/src/Data/Text/ParagraphLayout/Internal/Rich/ParagraphLayout.hs +++ b/src/Data/Text/ParagraphLayout/Internal/Rich/ParagraphLayout.hs @@ -7,6 +7,7 @@ module Data.Text.ParagraphLayout.Internal.Rich.ParagraphLayout , paragraphLayout , paragraphOriginX , paragraphOriginY + , paragraphSafeWidth , shapedRuns ) where @@ -72,3 +73,25 @@ appendFragments (ParagraphLayout _ a) (ParagraphLayout _ b) = -- | Return all shaped runs in the paragraph. shapedRuns :: ParagraphLayout d -> [ShapedRun] shapedRuns pl = map shapedRun $ paragraphFragments pl + +-- | Width of the widest line, including spacing. +-- +-- This is the smallest `Data.Text.ParagraphLayout.Rich.paragraphMaxWidth` +-- that will not introduce new line breaks. +-- +-- When `Data.Text.ParagraphLayout.Rich.paragraphMaxWidth` is set to `maxBound`, +-- `paragraphSafeWidth` can be used to determine the @max-content@ width of the +-- paragraph for CSS. +paragraphSafeWidth :: ParagraphLayout d -> Int32 +paragraphSafeWidth pl = maximum $ map (lineWidth pl) $ lineNumbers pl + +lineWidth :: ParagraphLayout d -> Int -> Int32 +lineWidth pl line = sum $ map fragmentWidth $ lineFragments pl line + +fragmentWidth :: Fragment d -> Int32 +fragmentWidth f = width $ fragmentSpacedRect f + +lineFragments :: ParagraphLayout d -> Int -> [Fragment d] +lineFragments pl line = filter byLine $ paragraphFragments pl + where + byLine frag = line == fragmentLine frag -- 2.30.2