From e64a1ae7a1eb6d0b77d88246976641fd61df0f61 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Sat, 3 Jun 2023 13:35:31 +1200 Subject: [PATCH] Correct coordinate-space involved in rect unionizing. --- Graphics/Layout/Inline.hs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/Graphics/Layout/Inline.hs b/Graphics/Layout/Inline.hs index f5a53a7..283fbfb 100644 --- a/Graphics/Layout/Inline.hs +++ b/Graphics/Layout/Inline.hs @@ -12,6 +12,7 @@ import Data.Text.ParagraphLayout.Rich (Paragraph(..), ParagraphOptions(..), import Data.Text.ParagraphLayout.Rect (Rect(..), width, height, x_max, x_min, y_min, y_max) import Data.Int (Int32) +import Debug.Trace (trace) -- To warn about unexpected branches! import Graphics.Layout.Box hiding (min, max, width, height) import qualified Graphics.Layout.Box as Box @@ -113,13 +114,8 @@ fragmentSize self = Size (c $ width r) (c $ height r) treeRect :: (CastDouble m, CastDouble n) => FragmentTree (a, PaddedBox m n, c) -> Rect Int32 treeRect (Branch AncestorBox { boxUserData = (_, box', _)} childs) = - foldr unionRect (Rect 0 0 0 0) $ map treeRect childs + unions $ map treeRect childs where - unionRect a b = Rect - (x_min a `min` x_min b - leftSpace box) - (y_min a `min` y_min b - topSpace box) - (x_max a `max` x_max b - x_min a `min` x_min b + hSpace box) - (y_max a `max` y_max b - y_min a `min` x_min b + vSpace box) box :: PaddedBox Int32 Int32 box = mapX' unscale $ mapY' unscale box' treeRect (Leaf self) = fragmentRect self @@ -191,3 +187,26 @@ positionTree (x, y) self@(Leaf (Fragment (a, b, c) d _ f g h)) = treeInner :: FragmentTree (a, b, c) -> c treeInner (Branch AncestorBox { boxUserData = (_, _, ret) } _) = ret treeInner (Leaf Fragment { fragmentUserData = (_, _, ret) }) = ret + +------ +--- Taken from Balkón +------ +-- | Calculate the smallest rectangle that completely contains all the given +-- rectangles. +unions [] = trace "No rects to union!" $ Rect 0 0 0 0 +unions rects = foldr1 union rects + +-- | Calculate the smallest rectangle that completely contains the given two +-- rectangles. +-- +-- The origin of the resulting rectangle will be the corner with the lowest +-- X coordinate and the highest Y coordinate, regardless of the origin of the +-- input rectangles. +union :: (Num a, Ord a) => Rect a -> Rect a -> Rect a +union a b = Rect x_low y_high dx (-dy) where + x_low = x_min a `min` x_min b + y_low = y_min a `min` y_min b + x_high = x_max a `max` x_max b + y_high = y_max a `max` y_max b + dx = x_high - x_low + dy = y_high - y_low -- 2.30.2