~jaro/balkon

0f7131a5849c78c78573ed1abc34893b3ad1f64d — Jaro 11 months ago 0b2a78c
Propagate BiDi levels to layout algorithm.

BREAKING: Text direction of the root box is now correctly used for
ordering fragments. Previously, the order was always LTR.
M src/Data/Text/ParagraphLayout/Internal/Layout.hs => src/Data/Text/ParagraphLayout/Internal/Layout.hs +2 -1
@@ 295,9 295,10 @@ layoutRunsH runs = map layoutRunH runs
layoutRunH :: WithSpan d Run -> ProtoFragmentWithSpan d
layoutRunH (WithSpan rs run) = WithSpan rs pf
    where
        pf = PF.protoFragmentH dir glyphs
        pf = PF.protoFragmentH dir lvl glyphs
        glyphs = shapeRun (WithSpan rs run)
        dir = runDirection run
        lvl = runLevel run

-- | Calculate layout for the given run independently of its position.
shapeRun :: WithSpan d Run -> [(GlyphInfo, GlyphPos)]

M src/Data/Text/ParagraphLayout/Internal/ProtoFragment.hs => src/Data/Text/ParagraphLayout/Internal/ProtoFragment.hs +9 -12
@@ 7,12 7,15 @@ where
import Data.Int (Int32)
import Data.Text.Glyphize (Direction (..), GlyphInfo, GlyphPos (x_advance))

import Data.Text.ParagraphLayout.Internal.BiDiLevels
import qualified Data.Text.ParagraphLayout.Internal.BiDiLevels as BiDi

-- | A box fragment which has not been positioned yet.
data ProtoFragment = ProtoFragment
    { direction :: Maybe Direction
    -- ^ Text direction, which is constant within a fragment.
    , level :: BiDi.Level
    -- ^ BiDi embedding level.
    -- Should be even for LTR fragments and odd for RTL fragments.
    , advance :: Int32
    -- ^ Total advance of glyphs in this fragment,
    -- depending on the text direction.


@@ 21,17 24,11 @@ data ProtoFragment = ProtoFragment

-- | Construct a `ProtoFragment`, automatically calculating the total advance
-- for a horizontal text direction.
protoFragmentH :: Maybe Direction -> [(GlyphInfo, GlyphPos)] -> ProtoFragment
protoFragmentH dir gs = ProtoFragment dir adv gs
protoFragmentH :: Maybe Direction -> BiDi.Level -> [(GlyphInfo, GlyphPos)] ->
    ProtoFragment
protoFragmentH dir lvl gs = ProtoFragment dir lvl adv gs
    where
        adv = sum $ map (x_advance . snd) gs

instance WithLevel ProtoFragment where
    level pf = case direction pf of
        -- TODO: Allow externally set paragraph embedding level.
        -- TODO: Properly calculate BiDi levels.
        Just DirLTR -> 0
        Just DirRTL -> 1
        Just DirTTB -> 0
        Just DirBTT -> 1
        Nothing -> 0
instance BiDi.WithLevel ProtoFragment where
    level = level -- BiDi.level = ProtoFragment.level

M src/Data/Text/ParagraphLayout/Internal/TextOptions.hs => src/Data/Text/ParagraphLayout/Internal/TextOptions.hs +3 -1
@@ 33,10 33,12 @@ data TextOptions = TextOptions

    , textDirection :: Direction
    -- ^ Base text direction.
    --
    -- Used to determine which box fragment gets the left spacing and which
    -- gets the right spacing when broken over multiple lines.
    --
    -- TODO: Also use for the visual order of boxes.
    -- When applied to the root box in a paragraph, this is also used to
    -- determine the base direction of the paragraph.

    -- TODO: textVerticalAlign
    -- TODO: textLetterSpacing