From bb6ef2fda49c191de4e171bd1c29554ed0aabee3 Mon Sep 17 00:00:00 2001 From: Jaro Date: Sat, 18 Mar 2023 15:38:13 +0100 Subject: [PATCH] Add internal functions for "shaped runs" output. --- .../Text/ParagraphLayout/Internal/Fragment.hs | 19 ++++++++++++++++++- .../ParagraphLayout/Internal/Paragraph.hs | 11 +++++++++++ .../Text/ParagraphLayout/Internal/Span.hs | 7 ++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Data/Text/ParagraphLayout/Internal/Fragment.hs b/src/Data/Text/ParagraphLayout/Internal/Fragment.hs index c306dfc..b16991a 100644 --- a/src/Data/Text/ParagraphLayout/Internal/Fragment.hs +++ b/src/Data/Text/ParagraphLayout/Internal/Fragment.hs @@ -1,4 +1,7 @@ -module Data.Text.ParagraphLayout.Internal.Fragment (Fragment(..)) +module Data.Text.ParagraphLayout.Internal.Fragment + (Fragment(..) + ,ShapedRun + ,shapedRun) where import Data.Int (Int32) @@ -41,3 +44,17 @@ data Fragment = Fragment } deriving (Eq, Read, Show) + +-- | A simplified representation of a box fragment, suitable for passing to a +-- text drawing library but lacking detailed size information. +type ShapedRun = (Int32, Int32, [(GlyphInfo, GlyphPos)]) + +-- | Convert a `Fragment` to a `ShapedRun`. +shapedRun :: Fragment -> ShapedRun +shapedRun f = (x, y, g) + where + x = x_origin r + px + y = y_origin r + py + g = fragmentGlyphs f + (px, py) = fragmentPen f + r = fragmentRect f diff --git a/src/Data/Text/ParagraphLayout/Internal/Paragraph.hs b/src/Data/Text/ParagraphLayout/Internal/Paragraph.hs index 34fa076..4f9c42e 100644 --- a/src/Data/Text/ParagraphLayout/Internal/Paragraph.hs +++ b/src/Data/Text/ParagraphLayout/Internal/Paragraph.hs @@ -6,6 +6,7 @@ module Data.Text.ParagraphLayout.Internal.Paragraph ,paragraphOriginX ,paragraphOriginY ,paragraphSpanBounds + ,shapedRuns ) where @@ -15,6 +16,7 @@ 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 @@ -115,3 +117,12 @@ containRects = foldr union empty 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/Span.hs b/src/Data/Text/ParagraphLayout/Internal/Span.hs index b8750b1..564313c 100644 --- a/src/Data/Text/ParagraphLayout/Internal/Span.hs +++ b/src/Data/Text/ParagraphLayout/Internal/Span.hs @@ -2,6 +2,7 @@ module Data.Text.ParagraphLayout.Internal.Span (Span(..) ,SpanLayout(..) ,SpanOptions(..) + ,spanFragments ,spanRects ) where @@ -46,5 +47,9 @@ data SpanLayout = SpanLayout [Fragment] -- TODO: Consider merging fragments created by script changes. deriving (Eq, Read, Show) +-- | Return all fragments of shaped text from the given span. +spanFragments :: SpanLayout -> [Fragment] +spanFragments (SpanLayout frags) = frags + spanRects :: SpanLayout -> [Rect Int32] -spanRects (SpanLayout frags) = map fragmentRect frags +spanRects sl = map fragmentRect $ spanFragments sl -- 2.30.2