~jaro/balkon

976fc3031890f97177c3ce984476022c9099bb66 — Jaro 1 year, 5 months ago 232a082
Add forceLeftAlign function for testing.
1 files changed, 22 insertions(+), 1 deletions(-)

M src/Data/Text/ParagraphLayout/Internal/ParagraphLine.hs
M src/Data/Text/ParagraphLayout/Internal/ParagraphLine.hs => src/Data/Text/ParagraphLayout/Internal/ParagraphLine.hs +22 -1
@@ 3,6 3,7 @@ module Data.Text.ParagraphLayout.Internal.ParagraphLine
    ( GenericLayout
    , cutLines
    , mergeLines
    , forceLeftAlign
    )
where



@@ 22,9 23,14 @@ class GenericLayout pl where
    -- | Rectangle surrounding the layout, to be used for appending.
    rect :: pl -> Rect Int32

    -- | Actual distance between the paragraph origin and the nearest fragment.
    -- | Actual distance between the paragraph origin and the nearest fragment
    -- on the Y axis.
    topDistance :: pl -> Int32

    -- | Actual distance between the paragraph origin and the nearest fragment
    -- on the X axis.
    leftDistance :: pl -> Int32

    -- | Keep only fragments with the given line number.
    limitFragments :: Int -> pl -> pl



@@ 40,6 46,7 @@ instance GenericLayout (P.ParagraphLayout d) where
    empty = P.emptyParagraphLayout
    rect = P.paragraphRect
    topDistance pl = topFragmentOrigin $ P.paragraphFragments pl
    leftDistance pl = leftmostFragmentOrigin $ P.paragraphFragments pl
    limitFragments n = P.filterFragments (fragmentIsOnLine n)
    shiftFragments dx dy = P.mapFragments (shiftFragment dx dy)
    appendFragments = P.appendFragments


@@ 48,6 55,7 @@ instance GenericLayout (R.ParagraphLayout d) where
    empty = R.emptyParagraphLayout
    rect = R.paragraphRect
    topDistance pl = topFragmentOrigin $ R.paragraphFragments pl
    leftDistance pl = leftmostFragmentOrigin $ R.paragraphFragments pl
    limitFragments n = R.filterFragments (fragmentIsOnLine n)
    shiftFragments dx dy = R.mapFragments (shiftFragment dx dy)
    appendFragments = R.appendFragments


@@ 64,9 72,16 @@ cutLine n pl = trimTop $ limitFragments n pl
trimTop :: GenericLayout pl => pl -> pl
trimTop pl = shiftFragments 0 (-topDistance pl) pl

-- | Add a constant to each fragment's `x_origin` so that their minimum is zero.
trimLeft :: GenericLayout pl => pl -> pl
trimLeft pl = shiftFragments (-leftDistance pl) 0 pl

topFragmentOrigin :: [Fragment d] -> Int32
topFragmentOrigin frags = maximum $ map (y_origin . fragmentRect) frags

leftmostFragmentOrigin :: [Fragment d] -> Int32
leftmostFragmentOrigin frags = minimum $ map (x_origin . fragmentRect) frags

-- | Put the given paragraph layouts together as a vertically contiguous
-- sequence.
mergeLines :: GenericLayout pl => [pl] -> pl


@@ 81,3 96,9 @@ mergeLine pl nextLine = pl'

fragmentIsOnLine :: Int -> Fragment d -> Bool
fragmentIsOnLine n frag = n == fragmentLine frag

-- | Rearrange fragments on each line so that they are stacked from the left.
--
-- Intended for comparing paragraph layouts of different maximum widths.
forceLeftAlign :: (GenericLayout pl, LineNumbers pl) => pl -> pl
forceLeftAlign pl = mergeLines $ map trimLeft $ cutLines pl