From d5253a67c07ac5f110471f7644c55b5ef40695ba Mon Sep 17 00:00:00 2001 From: Jaro Date: Sun, 2 Jul 2023 08:44:57 +0200 Subject: [PATCH] Do not pass around text direction unnecessarily. --- .../Text/ParagraphLayout/Internal/Layout.hs | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/Data/Text/ParagraphLayout/Internal/Layout.hs b/src/Data/Text/ParagraphLayout/Internal/Layout.hs index 829d35a..baa5708 100644 --- a/src/Data/Text/ParagraphLayout/Internal/Layout.hs +++ b/src/Data/Text/ParagraphLayout/Internal/Layout.hs @@ -172,9 +172,8 @@ verticalAlignment originY pl = (bottomY, PL.mapFragments setOrigin pl) boxVerticalAlignment (RB.boxOptions rb) == AlignLineBottom fragBoxes (WithSpan rs _) = RS.spanBoxes rs vors = sconcat $ fmap vor $ PL.protoFragments pl - vor (WithSpan rs pf) = + vor (WithSpan rs _) = verticalOffsetsRecursiveStruts - (PF.direction pf) (RS.spanTextOptions rs) (RS.spanBoxes rs) @@ -429,12 +428,12 @@ layoutRunH (WithSpan rs run) = WithSpan rs pf glyphs = shapeRun (WithSpan rs run) dir = runDirection run lvl = runLevel run - vo = verticalOffsets dir (RS.spanTextOptions rs) + vo = verticalOffsets (RS.spanTextOptions rs) hard = runHardBreak run -- | Vertical offsets for the given fragment, with baseline set to 0. -verticalOffsets :: Direction -> TextOptions -> VO.VerticalOffsets -verticalOffsets dir opts = VO.VerticalOffsets +verticalOffsets :: TextOptions -> VO.VerticalOffsets +verticalOffsets opts = VO.VerticalOffsets { VO.layoutTop = ascent + topHalfLeading , VO.fontTop = ascent , VO.baseline = 0 @@ -453,6 +452,11 @@ verticalOffsets dir opts = VO.VerticalOffsets -- `descent` >= 0 for horizontal fonts descent = - (descender extents `fromMaybe` textDescender opts) extents = fontExtentsForDir (textFont opts) (Just dir) + -- Actual shaped text direction may differ from the direction set in + -- `TextOptions` (for example RTL characters in a LTR box), but + -- HarfBuzz only distinguished horizontal and vertical extents, + -- so this should make no difference. + dir = textDirection opts lineHeight = case textLineHeight opts of Normal -> normalLineHeight Absolute h -> h @@ -464,9 +468,9 @@ verticalOffsets dir opts = VO.VerticalOffsets -- Note: The font extents are calculated using the same direction for the whole -- ancestry path regardless of the actual direction of these boxes, but -- this should not matter for text that is only horizontal. -verticalOffsetsRecursive :: Direction -> TextOptions -> [RB.ResolvedBox d] -> +verticalOffsetsRecursive :: TextOptions -> [RB.ResolvedBox d] -> (Maybe (RB.ResolvedBox d), VO.VerticalOffsets) -verticalOffsetsRecursive dir opts boxes = case boxes of +verticalOffsetsRecursive opts boxes = case boxes of [] -> -- Inline content directly in the root box. (Nothing, vo) (b : bs) -> case boxVerticalAlignment $ RB.boxOptions b of @@ -474,20 +478,20 @@ verticalOffsetsRecursive dir opts boxes = case boxes of AlignLineBottom -> (Just b, vo) AlignBaseline offset -> let parentOpts = RB.boxParentTextOptions b - (anchor, parentVO) = verticalOffsetsRecursive dir parentOpts bs + (anchor, parentVO) = verticalOffsetsRecursive parentOpts bs in (anchor, VO.alignBaseline (VO.baseline parentVO + offset) vo) where - vo = verticalOffsets dir opts + vo = verticalOffsets opts -- | Like `verticalOffsetsRecursive`, but also generate struts for every -- ancestor box. -verticalOffsetsRecursiveStruts :: Direction -> TextOptions -> [RB.ResolvedBox d] - -> NonEmpty (Maybe (RB.ResolvedBox d), VO.VerticalOffsets) -verticalOffsetsRecursiveStruts dir opts [] = - verticalOffsetsRecursive dir opts [] :| [] -verticalOffsetsRecursiveStruts dir opts boxes@(b : bs) = - verticalOffsetsRecursive dir opts boxes <| - verticalOffsetsRecursiveStruts dir (RB.boxParentTextOptions b) bs +verticalOffsetsRecursiveStruts :: TextOptions -> [RB.ResolvedBox d] -> + NonEmpty (Maybe (RB.ResolvedBox d), VO.VerticalOffsets) +verticalOffsetsRecursiveStruts opts [] = + verticalOffsetsRecursive opts [] :| [] +verticalOffsetsRecursiveStruts opts boxes@(b : bs) = + verticalOffsetsRecursive opts boxes <| + verticalOffsetsRecursiveStruts (RB.boxParentTextOptions b) bs -- | Calculate layout for the given run independently of its position. shapeRun :: WithSpan d Run -> [(GlyphInfo, GlyphPos)] -- 2.30.2