From 0f7131a5849c78c78573ed1abc34893b3ad1f64d Mon Sep 17 00:00:00 2001 From: Jaro Date: Thu, 25 May 2023 14:39:34 +0200 Subject: [PATCH] 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. --- .../Text/ParagraphLayout/Internal/Layout.hs | 3 ++- .../ParagraphLayout/Internal/ProtoFragment.hs | 21 ++++++++----------- .../ParagraphLayout/Internal/TextOptions.hs | 4 +++- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Data/Text/ParagraphLayout/Internal/Layout.hs b/src/Data/Text/ParagraphLayout/Internal/Layout.hs index f8d50be..df3cfe3 100644 --- a/src/Data/Text/ParagraphLayout/Internal/Layout.hs +++ b/src/Data/Text/ParagraphLayout/Internal/Layout.hs @@ -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)] diff --git a/src/Data/Text/ParagraphLayout/Internal/ProtoFragment.hs b/src/Data/Text/ParagraphLayout/Internal/ProtoFragment.hs index 12f73d9..39fe69f 100644 --- a/src/Data/Text/ParagraphLayout/Internal/ProtoFragment.hs +++ b/src/Data/Text/ParagraphLayout/Internal/ProtoFragment.hs @@ -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 diff --git a/src/Data/Text/ParagraphLayout/Internal/TextOptions.hs b/src/Data/Text/ParagraphLayout/Internal/TextOptions.hs index 3a1a9bb..b1c8b59 100644 --- a/src/Data/Text/ParagraphLayout/Internal/TextOptions.hs +++ b/src/Data/Text/ParagraphLayout/Internal/TextOptions.hs @@ -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 -- 2.30.2