@@ 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