~jaro/balkon

ce4f4845a30de2bbcbce4c8c9eb2a0607b57588d — Jaro 10 months ago e294320
Generalise shiftFragment function.

Intended for testing paragraphSafeWidth.
M src/Data/Text/ParagraphLayout/Internal/Fragment.hs => src/Data/Text/ParagraphLayout/Internal/Fragment.hs +10 -0
@@ 3,6 3,7 @@ module Data.Text.ParagraphLayout.Internal.Fragment
    , ShapedRun
    , fragmentSpacedRect
    , shapedRun
    , shiftFragment
    )
where



@@ 101,3 102,12 @@ shapedRun f = (x, y, g)
        g = fragmentGlyphs f
        (px, py) = fragmentPen f
        r = fragmentRect f

-- | Add @dx@ and @dy@ to the fragment's `x_origin` and `y_origin`,
-- respectively.
shiftFragment :: Int32 -> Int32 -> Fragment d -> Fragment d
shiftFragment dx dy f = f'
    where
        f' = f { fragmentRect = r' }
        r' = r { x_origin = x_origin r + dx, y_origin = y_origin r + dy }
        r = fragmentRect f

M src/Data/Text/ParagraphLayout/Internal/ParagraphLine.hs => src/Data/Text/ParagraphLayout/Internal/ParagraphLine.hs +7 -13
@@ 28,8 28,9 @@ class GenericLayout pl where
    -- | Keep only fragments with the given line number.
    limitFragments :: Int -> pl -> pl

    -- | Add @dy@ to each fragment's `y_origin`.
    shiftFragments :: Int32 -> pl -> pl
    -- | Add @dx@ and @dy@ to each fragment's `x_origin` and `y_origin`,
    -- respectively.
    shiftFragments :: Int32 -> Int32 -> pl -> pl

    -- | Combine fragments from two layouts into one,
    -- without any adjustment of coordinates.


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

instance GenericLayout (R.ParagraphLayout d) where


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

-- | Split the given paragraph layout into single-line layouts.


@@ 61,7 62,7 @@ cutLine n pl = trimTop $ limitFragments n pl

-- | Add a constant to each fragment's `y_origin` so that their maximum is zero.
trimTop :: GenericLayout pl => pl -> pl
trimTop pl = shiftFragments (-topDistance pl) pl
trimTop pl = shiftFragments 0 (-topDistance pl) pl

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


@@ 75,15 76,8 @@ mergeLine :: GenericLayout pl => pl -> pl -> pl
mergeLine pl nextLine = pl'
    where
        -- Quadratic time complexity. TODO: Consider optimising.
        pl' = appendFragments pl $ shiftFragments y nextLine
        pl' = appendFragments pl $ shiftFragments 0 y nextLine
        y = y_terminus $ rect pl

shiftFragment :: Int32 -> Fragment d -> Fragment d
shiftFragment dy f = f'
    where
        f' = f { fragmentRect = r' }
        r' = r { y_origin = y_origin r + dy }
        r = fragmentRect f

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