~jaro/balkon

bb6ef2fda49c191de4e171bd1c29554ed0aabee3 — Jaro 1 year, 8 months ago 96147a5
Add internal functions for "shaped runs" output.
M src/Data/Text/ParagraphLayout/Internal/Fragment.hs => src/Data/Text/ParagraphLayout/Internal/Fragment.hs +18 -1
@@ 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

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

M src/Data/Text/ParagraphLayout/Internal/Span.hs => src/Data/Text/ParagraphLayout/Internal/Span.hs +6 -1
@@ 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