~jaro/balkon

95ceab6991b5b491ebe39374989d3bcf7d12314c — Jaro 1 year, 5 months ago 0975c99
Implement calculation of safe paragraph width.
1 files changed, 23 insertions(+), 0 deletions(-)

M src/Data/Text/ParagraphLayout/Internal/Rich/ParagraphLayout.hs
M src/Data/Text/ParagraphLayout/Internal/Rich/ParagraphLayout.hs => src/Data/Text/ParagraphLayout/Internal/Rich/ParagraphLayout.hs +23 -0
@@ 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