{-# LANGUAGE TupleSections #-}
-- | Sizes inline text & extracts positioned children,
-- wraps Balkón for the actual logic.
module Graphics.Layout.Inline(paragraphMap, layoutMap, treeMap,
    inlineMin, inlineSize, inlineChildren, layoutSize, layoutChildren,
    treeBox, positionTree, treeInner, treeInner', glyphs, codepoints,
    FragmentTree(..)) where

import Data.Text.ParagraphLayout.Rich (Paragraph(..), ParagraphOptions(..),
                                Fragment(..), ParagraphLayout(..), AncestorBox(..),
                                InnerNode(..), Box(..), RootNode(..),
                                layoutRich, boxSpacing, BoxSpacing(..),
                                activateBoxSpacing, paragraphSafeWidth, textAscender)
import Data.Text.ParagraphLayout.Rect (Rect(..),
                                width, height, x_max, x_min, y_min, y_max)
import qualified Data.Text.Glyphize as HB
import Data.Int (Int32)
import Data.Word (Word32)
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
import Graphics.Layout.CSS.Font (hbUnit)

-- | Convert from Harfbuzz units to device pixels as a Double
hbScale :: Int32 -> Double
hbScale :: Int32 -> Double
hbScale = (Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
hbUnit) (Double -> Double) -> (Int32 -> Double) -> Int32 -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral
-- | Convert from Harfbuzz units to device pixels as a Double or Length.
c :: CastDouble a => Int32 -> a
c :: forall a. CastDouble a => Int32 -> a
c = Double -> a
forall a. CastDouble a => Double -> a
fromDouble (Double -> a) -> (Int32 -> Double) -> Int32 -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Double
hbScale
-- | Convert from a CastDouble in device pixels to Harfbuzz units.
unscale :: CastDouble x => x -> Int32
unscale :: forall x. CastDouble x => x -> Int32
unscale = Double -> Int32
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (Double -> Int32) -> (x -> Double) -> x -> Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double
hbUnit) (Double -> Double) -> (x -> Double) -> x -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> Double
forall a. CastDouble a => a -> Double
toDouble

-- | Compute minimum width & height for some richtext.
inlineMin :: (CastDouble x, CastDouble y) => (z -> PaddedBox x y) ->
        Paragraph (a, Either (PaddedBox x y) z, c) -> Size x y
inlineMin :: forall x y z a c.
(CastDouble x, CastDouble y) =>
(z -> PaddedBox x y)
-> Paragraph (a, Either (PaddedBox x y) z, c) -> Size x y
inlineMin z -> PaddedBox x y
cb Paragraph (a, Either (PaddedBox x y) z, c)
self = Rect Int32 -> Size x y
forall {n} {m}.
(CastDouble n, CastDouble m) =>
Rect Int32 -> Size m n
layoutSize' (Rect Int32 -> Size x y) -> Rect Int32 -> Size x y
forall a b. (a -> b) -> a -> b
$ (z -> PaddedBox x y)
-> Paragraph (a, Either (PaddedBox x y) z, c)
-> Int32
-> Rect Int32
forall m n x a c.
(CastDouble m, CastDouble n) =>
(x -> PaddedBox m n)
-> Paragraph (a, Either (PaddedBox m n) x, c)
-> Int32
-> Rect Int32
layoutRich' z -> PaddedBox x y
cb Paragraph (a, Either (PaddedBox x y) z, c)
self Int32
0
-- | Compute width & height of some richtext at configured width.
inlineSize :: (CastDouble x, CastDouble y) => (z -> PaddedBox x y) ->
        Paragraph (a, Either (PaddedBox x y) z, c) -> Size x y
inlineSize :: forall x y z a c.
(CastDouble x, CastDouble y) =>
(z -> PaddedBox x y)
-> Paragraph (a, Either (PaddedBox x y) z, c) -> Size x y
inlineSize z -> PaddedBox x y
cb self :: Paragraph (a, Either (PaddedBox x y) z, c)
self@(Paragraph Array
_ Int
_ RootNode Int (a, Either (PaddedBox x y) z, c)
_ ParagraphOptions
opts) =
    Rect Int32 -> Size x y
forall {n} {m}.
(CastDouble n, CastDouble m) =>
Rect Int32 -> Size m n
layoutSize' (Rect Int32 -> Size x y)
-> (Int32 -> Rect Int32) -> Int32 -> Size x y
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (z -> PaddedBox x y)
-> Paragraph (a, Either (PaddedBox x y) z, c)
-> Int32
-> Rect Int32
forall m n x a c.
(CastDouble m, CastDouble n) =>
(x -> PaddedBox m n)
-> Paragraph (a, Either (PaddedBox m n) x, c)
-> Int32
-> Rect Int32
layoutRich' z -> PaddedBox x y
cb Paragraph (a, Either (PaddedBox x y) z, c)
self (Int32 -> Size x y) -> Int32 -> Size x y
forall a b. (a -> b) -> a -> b
$ ParagraphOptions -> Int32
paragraphMaxWidth ParagraphOptions
opts
-- | Retrieve children out of some richtext,
-- associating given userdata with them.
inlineChildren :: (CastDouble x, CastDouble y, Eq x, Eq y, Eq a, Eq c, Eq z) =>
        (z -> PaddedBox x y) ->
        Paragraph (a, Either (PaddedBox x y) z, c) ->
        [FragmentTree (a, Either (PaddedBox x y) z, c)]
inlineChildren :: forall x y a c z.
(CastDouble x, CastDouble y, Eq x, Eq y, Eq a, Eq c, Eq z) =>
(z -> PaddedBox x y)
-> Paragraph (a, Either (PaddedBox x y) z, c)
-> [FragmentTree (a, Either (PaddedBox x y) z, c)]
inlineChildren z -> PaddedBox x y
cb Paragraph (a, Either (PaddedBox x y) z, c)
self = ParagraphLayout (a, Either (PaddedBox x y) z, c)
-> [FragmentTree (a, Either (PaddedBox x y) z, c)]
forall a. Eq a => ParagraphLayout a -> [FragmentTree a]
layoutChildren (ParagraphLayout (a, Either (PaddedBox x y) z, c)
 -> [FragmentTree (a, Either (PaddedBox x y) z, c)])
-> ParagraphLayout (a, Either (PaddedBox x y) z, c)
-> [FragmentTree (a, Either (PaddedBox x y) z, c)]
forall a b. (a -> b) -> a -> b
$ Paragraph (a, Either (PaddedBox x y) z, c)
-> ParagraphLayout (a, Either (PaddedBox x y) z, c)
forall d. Paragraph d -> ParagraphLayout d
layoutRich (Paragraph (a, Either (PaddedBox x y) z, c)
 -> ParagraphLayout (a, Either (PaddedBox x y) z, c))
-> Paragraph (a, Either (PaddedBox x y) z, c)
-> ParagraphLayout (a, Either (PaddedBox x y) z, c)
forall a b. (a -> b) -> a -> b
$ (z -> PaddedBox x y)
-> Paragraph (a, Either (PaddedBox x y) z, c)
-> Paragraph (a, Either (PaddedBox x y) z, c)
forall m n x a c.
(CastDouble m, CastDouble n) =>
(x -> PaddedBox m n)
-> Paragraph (a, Either (PaddedBox m n) x, c)
-> Paragraph (a, Either (PaddedBox m n) x, c)
lowerSpacing z -> PaddedBox x y
cb Paragraph (a, Either (PaddedBox x y) z, c)
self

-- | Retrieve a laid-out paragraph's rect & convert to CatTrap types.
layoutSize :: (CastDouble x, CastDouble y) => ParagraphLayout a -> Size x y
layoutSize :: forall x y a.
(CastDouble x, CastDouble y) =>
ParagraphLayout a -> Size x y
layoutSize = Rect Int32 -> Size x y
forall {n} {m}.
(CastDouble n, CastDouble m) =>
Rect Int32 -> Size m n
layoutSize' (Rect Int32 -> Size x y)
-> (ParagraphLayout a -> Rect Int32)
-> ParagraphLayout a
-> Size x y
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParagraphLayout a -> Rect Int32
forall d. ParagraphLayout d -> Rect Int32
paragraphRect
layoutSize' :: Rect Int32 -> Size m n
layoutSize' Rect Int32
r = n -> m -> Size m n
forall m n. n -> m -> Size m n
Size (Int32 -> n
forall a. CastDouble a => Int32 -> a
c (Int32 -> n) -> Int32 -> n
forall a b. (a -> b) -> a -> b
$ Rect Int32 -> Int32
forall a. Num a => Rect a -> a
width Rect Int32
r) (Int32 -> m
forall a. CastDouble a => Int32 -> a
c (Int32 -> m) -> Int32 -> m
forall a b. (a -> b) -> a -> b
$ Rect Int32 -> Int32
forall a. Num a => Rect a -> a
height Rect Int32
r)
-- | Retrieve a laid-out paragraph's children & associate with given userdata.
layoutChildren :: Eq a => ParagraphLayout a -> [FragmentTree a]
layoutChildren :: forall a. Eq a => ParagraphLayout a -> [FragmentTree a]
layoutChildren ParagraphLayout a
self = ParagraphLayout a -> [FragmentTree a]
forall a. Eq a => ParagraphLayout a -> [FragmentTree a]
reconstructTree ParagraphLayout a
self

-- | Layout a paragraph at given width & retrieve resulting rect.
-- LEGACY.
layoutRich' :: (CastDouble m, CastDouble n) => (x -> PaddedBox m n) ->
        Paragraph (a, Either (PaddedBox m n) x, c) -> Int32 -> Rect Int32
layoutRich' :: forall m n x a c.
(CastDouble m, CastDouble n) =>
(x -> PaddedBox m n)
-> Paragraph (a, Either (PaddedBox m n) x, c)
-> Int32
-> Rect Int32
layoutRich' x -> PaddedBox m n
cb (Paragraph Array
a Int
b RootNode Int (a, Either (PaddedBox m n) x, c)
c ParagraphOptions
d) Int32
width =
    (ParagraphLayout (a, Either (PaddedBox m n) x, c) -> Rect Int32
forall d. ParagraphLayout d -> Rect Int32
paragraphRect ParagraphLayout (a, Either (PaddedBox m n) x, c)
layout) { x_size :: Int32
x_size = ParagraphLayout (a, Either (PaddedBox m n) x, c) -> Int32
forall d. ParagraphLayout d -> Int32
paragraphSafeWidth ParagraphLayout (a, Either (PaddedBox m n) x, c)
layout}
  where
    layout :: ParagraphLayout (a, Either (PaddedBox m n) x, c)
layout = Paragraph (a, Either (PaddedBox m n) x, c)
-> ParagraphLayout (a, Either (PaddedBox m n) x, c)
forall d. Paragraph d -> ParagraphLayout d
layoutRich (Paragraph (a, Either (PaddedBox m n) x, c)
 -> ParagraphLayout (a, Either (PaddedBox m n) x, c))
-> Paragraph (a, Either (PaddedBox m n) x, c)
-> ParagraphLayout (a, Either (PaddedBox m n) x, c)
forall a b. (a -> b) -> a -> b
$ (x -> PaddedBox m n)
-> Paragraph (a, Either (PaddedBox m n) x, c)
-> Paragraph (a, Either (PaddedBox m n) x, c)
forall m n x a c.
(CastDouble m, CastDouble n) =>
(x -> PaddedBox m n)
-> Paragraph (a, Either (PaddedBox m n) x, c)
-> Paragraph (a, Either (PaddedBox m n) x, c)
lowerSpacing x -> PaddedBox m n
cb (Paragraph (a, Either (PaddedBox m n) x, c)
 -> Paragraph (a, Either (PaddedBox m n) x, c))
-> Paragraph (a, Either (PaddedBox m n) x, c)
-> Paragraph (a, Either (PaddedBox m n) x, c)
forall a b. (a -> b) -> a -> b
$ Array
-> Int
-> RootNode Int (a, Either (PaddedBox m n) x, c)
-> ParagraphOptions
-> Paragraph (a, Either (PaddedBox m n) x, c)
forall d.
Array -> Int -> RootNode Int d -> ParagraphOptions -> Paragraph d
Paragraph Array
a Int
b RootNode Int (a, Either (PaddedBox m n) x, c)
c ParagraphOptions
d {
        paragraphMaxWidth :: Int32
paragraphMaxWidth = Int32
width
      }

-- | Copy surrounding whitespace into Balkon properties.
lowerSpacing :: (CastDouble m, CastDouble n) => (x -> PaddedBox m n) ->
    Paragraph (a, Either (PaddedBox m n) x, c) ->
    Paragraph (a, Either (PaddedBox m n) x, c)
lowerSpacing :: forall m n x a c.
(CastDouble m, CastDouble n) =>
(x -> PaddedBox m n)
-> Paragraph (a, Either (PaddedBox m n) x, c)
-> Paragraph (a, Either (PaddedBox m n) x, c)
lowerSpacing x -> PaddedBox m n
cb (Paragraph Array
a Int
b (RootBox Box Int (a, Either (PaddedBox m n) x, c)
c) ParagraphOptions
d) = Array
-> Int
-> RootNode Int (a, Either (PaddedBox m n) x, c)
-> ParagraphOptions
-> Paragraph (a, Either (PaddedBox m n) x, c)
forall d.
Array -> Int -> RootNode Int d -> ParagraphOptions -> Paragraph d
Paragraph Array
a Int
b (Box Int (a, Either (PaddedBox m n) x, c)
-> RootNode Int (a, Either (PaddedBox m n) x, c)
forall t d. Box t d -> RootNode t d
RootBox (Box Int (a, Either (PaddedBox m n) x, c)
 -> RootNode Int (a, Either (PaddedBox m n) x, c))
-> Box Int (a, Either (PaddedBox m n) x, c)
-> RootNode Int (a, Either (PaddedBox m n) x, c)
forall a b. (a -> b) -> a -> b
$ Box Int (a, Either (PaddedBox m n) x, c)
-> Box Int (a, Either (PaddedBox m n) x, c)
forall {n} {m} {t} {a} {c}.
(CastDouble n, CastDouble m) =>
Box t (a, Either (PaddedBox m n) x, c)
-> Box t (a, Either (PaddedBox m n) x, c)
inner Box Int (a, Either (PaddedBox m n) x, c)
c) ParagraphOptions
d
  where
    inner :: Box t (a, Either (PaddedBox m n) x, c)
-> Box t (a, Either (PaddedBox m n) x, c)
inner (Box [InnerNode t (a, Either (PaddedBox m n) x, c)]
childs TextOptions
opts) = ([InnerNode t (a, Either (PaddedBox m n) x, c)]
 -> TextOptions -> Box t (a, Either (PaddedBox m n) x, c))
-> TextOptions
-> [InnerNode t (a, Either (PaddedBox m n) x, c)]
-> Box t (a, Either (PaddedBox m n) x, c)
forall a b c. (a -> b -> c) -> b -> a -> c
flip [InnerNode t (a, Either (PaddedBox m n) x, c)]
-> TextOptions -> Box t (a, Either (PaddedBox m n) x, c)
forall t d. [InnerNode t d] -> TextOptions -> Box t d
Box TextOptions
opts ([InnerNode t (a, Either (PaddedBox m n) x, c)]
 -> Box t (a, Either (PaddedBox m n) x, c))
-> [InnerNode t (a, Either (PaddedBox m n) x, c)]
-> Box t (a, Either (PaddedBox m n) x, c)
forall a b. (a -> b) -> a -> b
$ (InnerNode t (a, Either (PaddedBox m n) x, c)
 -> InnerNode t (a, Either (PaddedBox m n) x, c))
-> [InnerNode t (a, Either (PaddedBox m n) x, c)]
-> [InnerNode t (a, Either (PaddedBox m n) x, c)]
forall a b. (a -> b) -> [a] -> [b]
map InnerNode t (a, Either (PaddedBox m n) x, c)
-> InnerNode t (a, Either (PaddedBox m n) x, c)
inner' [InnerNode t (a, Either (PaddedBox m n) x, c)]
childs
    inner' :: InnerNode t (a, Either (PaddedBox m n) x, c)
-> InnerNode t (a, Either (PaddedBox m n) x, c)
inner' (InlineBox e :: (a, Either (PaddedBox m n) x, c)
e@(a
_, Left PaddedBox m n
box, c
_) Box t (a, Either (PaddedBox m n) x, c)
child BoxOptions
opts) =
        (a, Either (PaddedBox m n) x, c)
-> Int32
-> Int32
-> Box t (a, Either (PaddedBox m n) x, c)
-> BoxOptions
-> InnerNode t (a, Either (PaddedBox m n) x, c)
inlineBox (a, Either (PaddedBox m n) x, c)
e (PaddedBox Int32 Int32 -> Int32
forall {a} {m}. Num a => PaddedBox m a -> a
leftSpace PaddedBox Int32 Int32
box') (PaddedBox Int32 Int32 -> Int32
forall {a} {m}. Num a => PaddedBox m a -> a
rightSpace PaddedBox Int32 Int32
box') Box t (a, Either (PaddedBox m n) x, c)
child BoxOptions
opts
      where box' :: PaddedBox Int32 Int32
box' = (n -> Int32) -> PaddedBox Int32 n -> PaddedBox Int32 Int32
forall n nn m. (n -> nn) -> PaddedBox m n -> PaddedBox m nn
mapX' n -> Int32
forall x. CastDouble x => x -> Int32
unscale (PaddedBox Int32 n -> PaddedBox Int32 Int32)
-> PaddedBox Int32 n -> PaddedBox Int32 Int32
forall a b. (a -> b) -> a -> b
$ (m -> Int32) -> PaddedBox m n -> PaddedBox Int32 n
forall m mm n. (m -> mm) -> PaddedBox m n -> PaddedBox mm n
mapY' m -> Int32
forall x. CastDouble x => x -> Int32
unscale PaddedBox m n
box
    inner' (InlineBox e :: (a, Either (PaddedBox m n) x, c)
e@(a
_, Right x
k, c
_) (Box [InnerNode t (a, Either (PaddedBox m n) x, c)]
childs TextOptions
opts') BoxOptions
opts) = let box :: PaddedBox m n
box = x -> PaddedBox m n
cb x
k
        in (a, Either (PaddedBox m n) x, c)
-> Int32
-> Int32
-> Box t (a, Either (PaddedBox m n) x, c)
-> BoxOptions
-> InnerNode t (a, Either (PaddedBox m n) x, c)
inlineBox (a, Either (PaddedBox m n) x, c)
e (PaddedBox m Int32 -> Int32
forall {a} {m}. Num a => PaddedBox m a -> a
Box.width (PaddedBox m Int32 -> Int32) -> PaddedBox m Int32 -> Int32
forall a b. (a -> b) -> a -> b
$ (n -> Int32) -> PaddedBox m n -> PaddedBox m Int32
forall n nn m. (n -> nn) -> PaddedBox m n -> PaddedBox m nn
mapX' n -> Int32
forall x. CastDouble x => x -> Int32
unscale PaddedBox m n
box) Int32
0 ([InnerNode t (a, Either (PaddedBox m n) x, c)]
-> TextOptions -> Box t (a, Either (PaddedBox m n) x, c)
forall t d. [InnerNode t d] -> TextOptions -> Box t d
Box [InnerNode t (a, Either (PaddedBox m n) x, c)]
childs TextOptions
opts' {
            textAscender :: Maybe Int32
textAscender = Int32 -> Maybe Int32
forall a. a -> Maybe a
Just (Int32 -> Maybe Int32) -> Int32 -> Maybe Int32
forall a b. (a -> b) -> a -> b
$ PaddedBox Int32 n -> Int32
forall {a} {n}. Num a => PaddedBox a n -> a
Box.height (PaddedBox Int32 n -> Int32) -> PaddedBox Int32 n -> Int32
forall a b. (a -> b) -> a -> b
$ (m -> Int32) -> PaddedBox m n -> PaddedBox Int32 n
forall m mm n. (m -> mm) -> PaddedBox m n -> PaddedBox mm n
mapY' m -> Int32
forall x. CastDouble x => x -> Int32
unscale PaddedBox m n
box
          }) BoxOptions
opts
    inner' self :: InnerNode t (a, Either (PaddedBox m n) x, c)
self@(TextSequence (a, Either (PaddedBox m n) x, c)
_ t
_) = InnerNode t (a, Either (PaddedBox m n) x, c)
self
    inlineBox :: (a, Either (PaddedBox m n) x, c)
-> Int32
-> Int32
-> Box t (a, Either (PaddedBox m n) x, c)
-> BoxOptions
-> InnerNode t (a, Either (PaddedBox m n) x, c)
inlineBox (a, Either (PaddedBox m n) x, c)
dat Int32
left Int32
right Box t (a, Either (PaddedBox m n) x, c)
child BoxOptions
opts = (a, Either (PaddedBox m n) x, c)
-> Box t (a, Either (PaddedBox m n) x, c)
-> BoxOptions
-> InnerNode t (a, Either (PaddedBox m n) x, c)
forall t d. d -> Box t d -> BoxOptions -> InnerNode t d
InlineBox (a, Either (PaddedBox m n) x, c)
dat (Box t (a, Either (PaddedBox m n) x, c)
-> Box t (a, Either (PaddedBox m n) x, c)
inner Box t (a, Either (PaddedBox m n) x, c)
child) (BoxOptions -> InnerNode t (a, Either (PaddedBox m n) x, c))
-> BoxOptions -> InnerNode t (a, Either (PaddedBox m n) x, c)
forall a b. (a -> b) -> a -> b
$
        (BoxSpacing -> BoxOptions -> BoxOptions)
-> BoxOptions -> BoxSpacing -> BoxOptions
forall a b c. (a -> b -> c) -> b -> a -> c
flip BoxSpacing -> BoxOptions -> BoxOptions
activateBoxSpacing BoxOptions
opts (BoxSpacing -> BoxOptions) -> BoxSpacing -> BoxOptions
forall a b. (a -> b) -> a -> b
$ Int32 -> Int32 -> BoxSpacing
BoxSpacingLeftRight Int32
left Int32
right



-- | A tree extracted from Balkón's inline layout.
data FragmentTree x = Branch (AncestorBox x) [FragmentTree x]
    | Leaf (Fragment x)
    deriving (Int -> FragmentTree x -> ShowS
[FragmentTree x] -> ShowS
FragmentTree x -> String
(Int -> FragmentTree x -> ShowS)
-> (FragmentTree x -> String)
-> ([FragmentTree x] -> ShowS)
-> Show (FragmentTree x)
forall x. Show x => Int -> FragmentTree x -> ShowS
forall x. Show x => [FragmentTree x] -> ShowS
forall x. Show x => FragmentTree x -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall x. Show x => Int -> FragmentTree x -> ShowS
showsPrec :: Int -> FragmentTree x -> ShowS
$cshow :: forall x. Show x => FragmentTree x -> String
show :: FragmentTree x -> String
$cshowList :: forall x. Show x => [FragmentTree x] -> ShowS
showList :: [FragmentTree x] -> ShowS
Show, FragmentTree x -> FragmentTree x -> Bool
(FragmentTree x -> FragmentTree x -> Bool)
-> (FragmentTree x -> FragmentTree x -> Bool)
-> Eq (FragmentTree x)
forall x. Eq x => FragmentTree x -> FragmentTree x -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall x. Eq x => FragmentTree x -> FragmentTree x -> Bool
== :: FragmentTree x -> FragmentTree x -> Bool
$c/= :: forall x. Eq x => FragmentTree x -> FragmentTree x -> Bool
/= :: FragmentTree x -> FragmentTree x -> Bool
Eq)

-- | Apply an operation to the 2nd field of the paragraph's userdata,
-- for it's entire subtree.
paragraphMap :: (b -> b') -> Paragraph (a, b, c) -> Paragraph (a, b', c)
paragraphMap :: forall b b' a c.
(b -> b') -> Paragraph (a, b, c) -> Paragraph (a, b', c)
paragraphMap b -> b'
cb (Paragraph Array
a Int
b (RootBox Box Int (a, b, c)
c) ParagraphOptions
d) =
    Array
-> Int
-> RootNode Int (a, b', c)
-> ParagraphOptions
-> Paragraph (a, b', c)
forall d.
Array -> Int -> RootNode Int d -> ParagraphOptions -> Paragraph d
Paragraph Array
a Int
b (Box Int (a, b', c) -> RootNode Int (a, b', c)
forall t d. Box t d -> RootNode t d
RootBox (Box Int (a, b', c) -> RootNode Int (a, b', c))
-> Box Int (a, b', c) -> RootNode Int (a, b', c)
forall a b. (a -> b) -> a -> b
$ Box Int (a, b, c) -> Box Int (a, b', c)
forall {t} {a} {c}. Box t (a, b, c) -> Box t (a, b', c)
inner Box Int (a, b, c)
c) ParagraphOptions
d
  where
    inner :: Box t (a, b, c) -> Box t (a, b', c)
inner (Box [InnerNode t (a, b, c)]
childs TextOptions
opts) = ([InnerNode t (a, b', c)] -> TextOptions -> Box t (a, b', c))
-> TextOptions -> [InnerNode t (a, b', c)] -> Box t (a, b', c)
forall a b c. (a -> b -> c) -> b -> a -> c
flip [InnerNode t (a, b', c)] -> TextOptions -> Box t (a, b', c)
forall t d. [InnerNode t d] -> TextOptions -> Box t d
Box TextOptions
opts ([InnerNode t (a, b', c)] -> Box t (a, b', c))
-> [InnerNode t (a, b', c)] -> Box t (a, b', c)
forall a b. (a -> b) -> a -> b
$ (InnerNode t (a, b, c) -> InnerNode t (a, b', c))
-> [InnerNode t (a, b, c)] -> [InnerNode t (a, b', c)]
forall a b. (a -> b) -> [a] -> [b]
map InnerNode t (a, b, c) -> InnerNode t (a, b', c)
inner' [InnerNode t (a, b, c)]
childs
    inner' :: InnerNode t (a, b, c) -> InnerNode t (a, b', c)
inner' (InlineBox (a
e, b
f, c
g) Box t (a, b, c)
child BoxOptions
opts) =
        (a, b', c)
-> Box t (a, b', c) -> BoxOptions -> InnerNode t (a, b', c)
forall t d. d -> Box t d -> BoxOptions -> InnerNode t d
InlineBox (a
e, b -> b'
cb b
f, c
g) (Box t (a, b, c) -> Box t (a, b', c)
inner Box t (a, b, c)
child) BoxOptions
opts
    inner' (TextSequence (a
e, b
f, c
g) t
leaf) = (a, b', c) -> t -> InnerNode t (a, b', c)
forall t d. d -> t -> InnerNode t d
TextSequence (a
e, b -> b'
cb b
f, c
g) t
leaf

-- | Apply an operation to the 2nd field of a laid-out paragraph's userdata,
-- for it's entire subtree.
layoutMap :: (b -> b') -> ParagraphLayout (a, b, c) -> ParagraphLayout (a, b', c)
layoutMap :: forall b b' a c.
(b -> b')
-> ParagraphLayout (a, b, c) -> ParagraphLayout (a, b', c)
layoutMap b -> b'
cb (ParagraphLayout Rect Int32
a [Line]
b [Fragment (a, b, c)]
c) = Rect Int32
-> [Line] -> [Fragment (a, b', c)] -> ParagraphLayout (a, b', c)
forall d. Rect Int32 -> [Line] -> [Fragment d] -> ParagraphLayout d
ParagraphLayout Rect Int32
a [Line]
b ([Fragment (a, b', c)] -> ParagraphLayout (a, b', c))
-> [Fragment (a, b', c)] -> ParagraphLayout (a, b', c)
forall a b. (a -> b) -> a -> b
$ (Fragment (a, b, c) -> Fragment (a, b', c))
-> [Fragment (a, b, c)] -> [Fragment (a, b', c)]
forall a b. (a -> b) -> [a] -> [b]
map Fragment (a, b, c) -> Fragment (a, b', c)
forall {a} {c}. Fragment (a, b, c) -> Fragment (a, b', c)
inner [Fragment (a, b, c)]
c
  where
    inner :: Fragment (a, b, c) -> Fragment (a, b', c)
inner self :: Fragment (a, b, c)
self@Fragment { fragmentUserData :: forall d. Fragment d -> d
fragmentUserData = (a
a, b
b, c
c) } = Fragment (a, b, c)
self {
        fragmentUserData :: (a, b', c)
fragmentUserData = (a
a, b -> b'
cb b
b, c
c),
        fragmentAncestorBoxes :: [AncestorBox (a, b', c)]
fragmentAncestorBoxes = (AncestorBox (a, b, c) -> AncestorBox (a, b', c))
-> [AncestorBox (a, b, c)] -> [AncestorBox (a, b', c)]
forall a b. (a -> b) -> [a] -> [b]
map AncestorBox (a, b, c) -> AncestorBox (a, b', c)
forall {a} {c}. AncestorBox (a, b, c) -> AncestorBox (a, b', c)
inner' ([AncestorBox (a, b, c)] -> [AncestorBox (a, b', c)])
-> [AncestorBox (a, b, c)] -> [AncestorBox (a, b', c)]
forall a b. (a -> b) -> a -> b
$ Fragment (a, b, c) -> [AncestorBox (a, b, c)]
forall d. Fragment d -> [AncestorBox d]
fragmentAncestorBoxes Fragment (a, b, c)
self
      }
    inner' :: AncestorBox (a, b, c) -> AncestorBox (a, b', c)
inner' self :: AncestorBox (a, b, c)
self@AncestorBox { boxUserData :: forall d. AncestorBox d -> d
boxUserData = (a
a, b
b, c
c) } = AncestorBox (a, b, c)
self {
        boxUserData :: (a, b', c)
boxUserData = (a
a, b -> b'
cb b
b, c
c)
      }

-- | Apply an operation to the 2nd field of the tree extracted from a laid-out
-- paragraph, for all nodes.
treeMap :: (b -> b') -> FragmentTree (a, b, c) -> FragmentTree (a, b', c)
treeMap :: forall b b' a c.
(b -> b') -> FragmentTree (a, b, c) -> FragmentTree (a, b', c)
treeMap b -> b'
cb (Branch self :: AncestorBox (a, b, c)
self@AncestorBox { boxUserData :: forall d. AncestorBox d -> d
boxUserData = (a
a, b
b, c
c) } [FragmentTree (a, b, c)]
childs) =
    AncestorBox (a, b', c)
-> [FragmentTree (a, b', c)] -> FragmentTree (a, b', c)
forall x. AncestorBox x -> [FragmentTree x] -> FragmentTree x
Branch AncestorBox (a, b, c)
self { boxUserData :: (a, b', c)
boxUserData = (a
a, b -> b'
cb b
b, c
c) } ([FragmentTree (a, b', c)] -> FragmentTree (a, b', c))
-> [FragmentTree (a, b', c)] -> FragmentTree (a, b', c)
forall a b. (a -> b) -> a -> b
$ (FragmentTree (a, b, c) -> FragmentTree (a, b', c))
-> [FragmentTree (a, b, c)] -> [FragmentTree (a, b', c)]
forall a b. (a -> b) -> [a] -> [b]
map ((b -> b') -> FragmentTree (a, b, c) -> FragmentTree (a, b', c)
forall b b' a c.
(b -> b') -> FragmentTree (a, b, c) -> FragmentTree (a, b', c)
treeMap b -> b'
cb) [FragmentTree (a, b, c)]
childs
treeMap b -> b'
cb (Leaf self :: Fragment (a, b, c)
self@Fragment { fragmentUserData :: forall d. Fragment d -> d
fragmentUserData = (a
a, b
b, c
c) }) =
    Fragment (a, b', c) -> FragmentTree (a, b', c)
forall x. Fragment x -> FragmentTree x
Leaf Fragment (a, b, c)
self { fragmentUserData :: (a, b', c)
fragmentUserData = (a
a, b -> b'
cb b
b, c
c), fragmentAncestorBoxes :: [AncestorBox (a, b', c)]
fragmentAncestorBoxes = [] }

-- | Retrieve the rect for a fragment & convert to CatTrap types.
fragmentSize :: (CastDouble x, CastDouble y) =>
        FragmentTree (a, PaddedBox x y, c) -> Size x y
fragmentSize :: forall x y a c.
(CastDouble x, CastDouble y) =>
FragmentTree (a, PaddedBox x y, c) -> Size x y
fragmentSize FragmentTree (a, PaddedBox x y, c)
self = y -> x -> Size x y
forall m n. n -> m -> Size m n
Size (Int32 -> y
forall a. CastDouble a => Int32 -> a
c (Int32 -> y) -> Int32 -> y
forall a b. (a -> b) -> a -> b
$ Rect Int32 -> Int32
forall a. Num a => Rect a -> a
width Rect Int32
r) (Int32 -> x
forall a. CastDouble a => Int32 -> a
c (Int32 -> x) -> Int32 -> x
forall a b. (a -> b) -> a -> b
$ Rect Int32 -> Int32
forall a. Num a => Rect a -> a
height Rect Int32
r)
    where r :: Rect Int32
r = FragmentTree (a, PaddedBox x y, c) -> Rect Int32
forall a b c. FragmentTree (a, b, c) -> Rect Int32
treeRect FragmentTree (a, PaddedBox x y, c)
self
-- | Compute the unioned rect for a subtree.
treeRect :: FragmentTree (a, b, c) -> Rect Int32
treeRect :: forall a b c. FragmentTree (a, b, c) -> Rect Int32
treeRect (Branch AncestorBox { boxUserData :: forall d. AncestorBox d -> d
boxUserData = (a
_, b
box', c
_)} [FragmentTree (a, b, c)]
childs) =
        [Rect Int32] -> Rect Int32
forall {a}. (Num a, Ord a) => [Rect a] -> Rect a
unions ([Rect Int32] -> Rect Int32) -> [Rect Int32] -> Rect Int32
forall a b. (a -> b) -> a -> b
$ (FragmentTree (a, b, c) -> Rect Int32)
-> [FragmentTree (a, b, c)] -> [Rect Int32]
forall a b. (a -> b) -> [a] -> [b]
map FragmentTree (a, b, c) -> Rect Int32
forall a b c. FragmentTree (a, b, c) -> Rect Int32
treeRect [FragmentTree (a, b, c)]
childs
treeRect (Leaf Fragment (a, b, c)
self) = Fragment (a, b, c) -> Rect Int32
forall d. Fragment d -> Rect Int32
fragmentRect Fragment (a, b, c)
self

-- | Compute the paddedbox for a subtree.
treeBox :: (CastDouble m, CastDouble n) =>
    FragmentTree (a, PaddedBox m n, c) -> PaddedBox m n
treeBox :: forall m n a c.
(CastDouble m, CastDouble n) =>
FragmentTree (a, PaddedBox m n, c) -> PaddedBox m n
treeBox self :: FragmentTree (a, PaddedBox m n, c)
self@(Branch AncestorBox { boxUserData :: forall d. AncestorBox d -> d
boxUserData = (a
_, PaddedBox m n
box', c
_)} [FragmentTree (a, PaddedBox m n, c)]
_) = PaddedBox m n
box' {
    min :: Size m n
Box.min = Size m n
size', max :: Size m n
Box.max = Size m n
size', size :: Size m n
Box.size = Size m n
size', nat :: Size Double Double
Box.nat = Size Double Double
size
  } where
    size' :: Size m n
size' = (Double -> n) -> Size m Double -> Size m n
forall n nn m. (n -> nn) -> Size m n -> Size m nn
mapSizeX Double -> n
forall a. CastDouble a => Double -> a
fromDouble (Size m Double -> Size m n) -> Size m Double -> Size m n
forall a b. (a -> b) -> a -> b
$ (Double -> m) -> Size Double Double -> Size m Double
forall m mm n. (m -> mm) -> Size m n -> Size mm n
mapSizeY Double -> m
forall a. CastDouble a => Double -> a
fromDouble Size Double Double
size
    size :: Size Double Double
size = (Double -> Double) -> Size Double Double -> Size Double Double
forall n nn m. (n -> nn) -> Size m n -> Size m nn
mapSizeX (Double -> Double -> Double
forall a. Num a => a -> a -> a
subtract (Double -> Double -> Double) -> Double -> Double -> Double
forall a b. (a -> b) -> a -> b
$ PaddedBox Double Double -> Double
forall {a} {m}. Num a => PaddedBox m a -> a
hSpace PaddedBox Double Double
box) (Size Double Double -> Size Double Double)
-> Size Double Double -> Size Double Double
forall a b. (a -> b) -> a -> b
$ (Double -> Double) -> Size Double Double -> Size Double Double
forall m mm n. (m -> mm) -> Size m n -> Size mm n
mapSizeY (Double -> Double -> Double
forall a. Num a => a -> a -> a
subtract (Double -> Double -> Double) -> Double -> Double -> Double
forall a b. (a -> b) -> a -> b
$ PaddedBox Double Double -> Double
forall {a} {n}. Num a => PaddedBox a n -> a
vSpace PaddedBox Double Double
box)(Size Double Double -> Size Double Double)
-> Size Double Double -> Size Double Double
forall a b. (a -> b) -> a -> b
$
         (n -> Double) -> Size Double n -> Size Double Double
forall n nn m. (n -> nn) -> Size m n -> Size m nn
mapSizeX n -> Double
forall a. CastDouble a => a -> Double
toDouble (Size Double n -> Size Double Double)
-> Size Double n -> Size Double Double
forall a b. (a -> b) -> a -> b
$ (m -> Double) -> Size m n -> Size Double n
forall m mm n. (m -> mm) -> Size m n -> Size mm n
mapSizeY m -> Double
forall a. CastDouble a => a -> Double
toDouble (Size m n -> Size Double n) -> Size m n -> Size Double n
forall a b. (a -> b) -> a -> b
$ FragmentTree (a, PaddedBox m n, c) -> Size m n
forall x y a c.
(CastDouble x, CastDouble y) =>
FragmentTree (a, PaddedBox x y, c) -> Size x y
fragmentSize FragmentTree (a, PaddedBox m n, c)
self
    box :: PaddedBox Double Double
box = (n -> Double) -> PaddedBox Double n -> PaddedBox Double Double
forall n nn m. (n -> nn) -> PaddedBox m n -> PaddedBox m nn
mapX' n -> Double
forall a. CastDouble a => a -> Double
toDouble (PaddedBox Double n -> PaddedBox Double Double)
-> PaddedBox Double n -> PaddedBox Double Double
forall a b. (a -> b) -> a -> b
$ (m -> Double) -> PaddedBox m n -> PaddedBox Double n
forall m mm n. (m -> mm) -> PaddedBox m n -> PaddedBox mm n
mapY' m -> Double
forall a. CastDouble a => a -> Double
toDouble PaddedBox m n
box'
treeBox self :: FragmentTree (a, PaddedBox m n, c)
self@(Leaf Fragment { fragmentUserData :: forall d. Fragment d -> d
fragmentUserData = (a
_, PaddedBox m n
box', c
_)}) = PaddedBox m n
box' {
    min :: Size m n
Box.min = Size m n
size', max :: Size m n
Box.max = Size m n
size', size :: Size m n
Box.size = Size m n
size', nat :: Size Double Double
Box.nat = Size Double Double
size
  } where
    size' :: Size m n
size' = (Double -> n) -> Size m Double -> Size m n
forall n nn m. (n -> nn) -> Size m n -> Size m nn
mapSizeX Double -> n
forall a. CastDouble a => Double -> a
fromDouble (Size m Double -> Size m n) -> Size m Double -> Size m n
forall a b. (a -> b) -> a -> b
$ (Double -> m) -> Size Double Double -> Size m Double
forall m mm n. (m -> mm) -> Size m n -> Size mm n
mapSizeY Double -> m
forall a. CastDouble a => Double -> a
fromDouble Size Double Double
size
    size :: Size Double Double
size = (Double -> Double) -> Size Double Double -> Size Double Double
forall n nn m. (n -> nn) -> Size m n -> Size m nn
mapSizeX (Double -> Double -> Double
forall a. Num a => a -> a -> a
subtract (Double -> Double -> Double) -> Double -> Double -> Double
forall a b. (a -> b) -> a -> b
$ PaddedBox Double Double -> Double
forall {a} {m}. Num a => PaddedBox m a -> a
hSpace PaddedBox Double Double
box) (Size Double Double -> Size Double Double)
-> Size Double Double -> Size Double Double
forall a b. (a -> b) -> a -> b
$ (Double -> Double) -> Size Double Double -> Size Double Double
forall m mm n. (m -> mm) -> Size m n -> Size mm n
mapSizeY (Double -> Double -> Double
forall a. Num a => a -> a -> a
subtract (Double -> Double -> Double) -> Double -> Double -> Double
forall a b. (a -> b) -> a -> b
$ PaddedBox Double Double -> Double
forall {a} {n}. Num a => PaddedBox a n -> a
vSpace PaddedBox Double Double
box) (Size Double Double -> Size Double Double)
-> Size Double Double -> Size Double Double
forall a b. (a -> b) -> a -> b
$
        (n -> Double) -> Size Double n -> Size Double Double
forall n nn m. (n -> nn) -> Size m n -> Size m nn
mapSizeX n -> Double
forall a. CastDouble a => a -> Double
toDouble (Size Double n -> Size Double Double)
-> Size Double n -> Size Double Double
forall a b. (a -> b) -> a -> b
$ (m -> Double) -> Size m n -> Size Double n
forall m mm n. (m -> mm) -> Size m n -> Size mm n
mapSizeY m -> Double
forall a. CastDouble a => a -> Double
toDouble (Size m n -> Size Double n) -> Size m n -> Size Double n
forall a b. (a -> b) -> a -> b
$ FragmentTree (a, PaddedBox m n, c) -> Size m n
forall x y a c.
(CastDouble x, CastDouble y) =>
FragmentTree (a, PaddedBox x y, c) -> Size x y
fragmentSize FragmentTree (a, PaddedBox m n, c)
self
    box :: PaddedBox Double Double
box = (n -> Double) -> PaddedBox Double n -> PaddedBox Double Double
forall n nn m. (n -> nn) -> PaddedBox m n -> PaddedBox m nn
mapX' n -> Double
forall a. CastDouble a => a -> Double
toDouble (PaddedBox Double n -> PaddedBox Double Double)
-> PaddedBox Double n -> PaddedBox Double Double
forall a b. (a -> b) -> a -> b
$ (m -> Double) -> PaddedBox m n -> PaddedBox Double n
forall m mm n. (m -> mm) -> PaddedBox m n -> PaddedBox mm n
mapY' m -> Double
forall a. CastDouble a => a -> Double
toDouble PaddedBox m n
box'

-- | Variant of `fragmentSize` asserting to the typesystem that both fields
-- of the resulting `Size` are of the same type.
fragmentSize' :: CastDouble x => FragmentTree (a, PaddedBox x x, c) -> Size x x
fragmentSize' :: forall x a c.
CastDouble x =>
FragmentTree (a, PaddedBox x x, c) -> Size x x
fragmentSize' = FragmentTree (a, PaddedBox x x, c) -> Size x x
forall x y a c.
(CastDouble x, CastDouble y) =>
FragmentTree (a, PaddedBox x y, c) -> Size x y
fragmentSize -- Work around for typesystem.
-- | Retrieve the position of a fragment.
fragmentPos :: (Double, Double) -> Fragment a -> (Double, Double)
fragmentPos :: forall a. (Double, Double) -> Fragment a -> (Double, Double)
fragmentPos (Double
x, Double
y) Fragment a
self = (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Int32 -> Double
hbScale (Rect Int32 -> Int32
forall a. (Num a, Ord a) => Rect a -> a
x_min Rect Int32
r), Double
y Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Int32 -> Double
hbScale (Rect Int32 -> Int32
forall a. (Num a, Ord a) => Rect a -> a
y_min Rect Int32
r))
    where r :: Rect Int32
r = Fragment a -> Rect Int32
forall d. Fragment d -> Rect Int32
fragmentRect Fragment a
self

-- | Extract the tree datastructure out of Balkón's ParagraphLayout
reconstructTree :: Eq x => ParagraphLayout x -> [FragmentTree x]
reconstructTree :: forall a. Eq a => ParagraphLayout a -> [FragmentTree a]
reconstructTree ParagraphLayout { paragraphFragments :: forall d. ParagraphLayout d -> [Fragment d]
paragraphFragments = [Fragment x]
frags } =
    [Fragment x] -> [FragmentTree x]
forall x. Eq x => [Fragment x] -> [FragmentTree x]
reconstructTree' [Fragment x
frag {
            fragmentAncestorBoxes :: [AncestorBox x]
fragmentAncestorBoxes = [AncestorBox x] -> [AncestorBox x]
forall a. [a] -> [a]
reverse ([AncestorBox x] -> [AncestorBox x])
-> [AncestorBox x] -> [AncestorBox x]
forall a b. (a -> b) -> a -> b
$ Fragment x -> [AncestorBox x]
forall d. Fragment d -> [AncestorBox d]
fragmentAncestorBoxes Fragment x
frag
        } | Fragment x
frag <- [Fragment x]
frags]
-- | Extract the tree datastructure out of Balkón's fragments.
reconstructTree' :: Eq x => [Fragment x] -> [FragmentTree x]
reconstructTree' :: forall x. Eq x => [Fragment x] -> [FragmentTree x]
reconstructTree' (self :: Fragment x
self@Fragment { fragmentAncestorBoxes :: forall d. Fragment d -> [AncestorBox d]
fragmentAncestorBoxes = [] }:[Fragment x]
frags) =
    Fragment x -> FragmentTree x
forall x. Fragment x -> FragmentTree x
Leaf Fragment x
selfFragmentTree x -> [FragmentTree x] -> [FragmentTree x]
forall a. a -> [a] -> [a]
:[Fragment x] -> [FragmentTree x]
forall x. Eq x => [Fragment x] -> [FragmentTree x]
reconstructTree' [Fragment x]
frags
reconstructTree' frags :: [Fragment x]
frags@(Fragment {
        fragmentAncestorBoxes :: forall d. Fragment d -> [AncestorBox d]
fragmentAncestorBoxes = AncestorBox x
branch:[AncestorBox x]
_, fragmentLine :: forall d. Fragment d -> Int
fragmentLine = Int
line
  }:[Fragment x]
_) =
    AncestorBox x -> [FragmentTree x] -> FragmentTree x
forall x. AncestorBox x -> [FragmentTree x] -> FragmentTree x
Branch AncestorBox x
branch ([Fragment x] -> [FragmentTree x]
forall x. Eq x => [Fragment x] -> [FragmentTree x]
reconstructTree' [ Fragment x
child { fragmentAncestorBoxes :: [AncestorBox x]
fragmentAncestorBoxes = [AncestorBox x]
ancestors }
            | child :: Fragment x
child@Fragment { fragmentAncestorBoxes :: forall d. Fragment d -> [AncestorBox d]
fragmentAncestorBoxes = AncestorBox x
_:[AncestorBox x]
ancestors } <- [Fragment x]
childs])
        FragmentTree x -> [FragmentTree x] -> [FragmentTree x]
forall a. a -> [a] -> [a]
:[Fragment x] -> [FragmentTree x]
forall x. Eq x => [Fragment x] -> [FragmentTree x]
reconstructTree' [Fragment x]
sibs
  where
    ([Fragment x]
childs, [Fragment x]
sibs) = (Fragment x -> Bool)
-> [Fragment x] -> ([Fragment x], [Fragment x])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span Fragment x -> Bool
sameBranch [Fragment x]
frags
    -- Cluster ancestor branches, breaking them per-line.
    sameBranch :: Fragment x -> Bool
sameBranch Fragment {fragmentAncestorBoxes :: forall d. Fragment d -> [AncestorBox d]
fragmentAncestorBoxes=AncestorBox x
branch':[AncestorBox x]
_, fragmentLine :: forall d. Fragment d -> Int
fragmentLine=Int
line'} =
        AncestorBox x
branch AncestorBox x -> AncestorBox x -> Bool
forall a. Eq a => a -> a -> Bool
== AncestorBox x
branch' Bool -> Bool -> Bool
&& Int
line Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
line'
    -- Leaves are always in their own branch.
    sameBranch Fragment { fragmentAncestorBoxes :: forall d. Fragment d -> [AncestorBox d]
fragmentAncestorBoxes = [] } = Bool
False
reconstructTree' [] = []

-- | Add an X,Y offset to all positions, annotating the userdata.
positionTree :: (Double, Double) -> ((Double, Double) -> b -> b') ->
        FragmentTree (a, b, c) -> FragmentTree (a, b', ((Double, Double), c))
positionTree :: forall b b' a c.
(Double, Double)
-> ((Double, Double) -> b -> b')
-> FragmentTree (a, b, c)
-> FragmentTree (a, b', ((Double, Double), c))
positionTree (Double
x, Double
y) (Double, Double) -> b -> b'
cb self :: FragmentTree (a, b, c)
self@(Branch (AncestorBox (a
a, b
b, c
c) BoxEdge
d BoxEdge
e BoxEdge
f BoxEdge
g) [FragmentTree (a, b, c)]
childs) =
    AncestorBox (a, b', ((Double, Double), c))
-> [FragmentTree (a, b', ((Double, Double), c))]
-> FragmentTree (a, b', ((Double, Double), c))
forall x. AncestorBox x -> [FragmentTree x] -> FragmentTree x
Branch ((a, b', ((Double, Double), c))
-> BoxEdge
-> BoxEdge
-> BoxEdge
-> BoxEdge
-> AncestorBox (a, b', ((Double, Double), c))
forall d.
d -> BoxEdge -> BoxEdge -> BoxEdge -> BoxEdge -> AncestorBox d
AncestorBox (a
a, (Double, Double) -> b -> b'
cb (Double, Double)
pos b
b, ((Double, Double)
pos, c
c)) BoxEdge
d BoxEdge
e BoxEdge
f BoxEdge
g) ([FragmentTree (a, b', ((Double, Double), c))]
 -> FragmentTree (a, b', ((Double, Double), c)))
-> [FragmentTree (a, b', ((Double, Double), c))]
-> FragmentTree (a, b', ((Double, Double), c))
forall a b. (a -> b) -> a -> b
$
        (FragmentTree (a, b, c)
 -> FragmentTree (a, b', ((Double, Double), c)))
-> [FragmentTree (a, b, c)]
-> [FragmentTree (a, b', ((Double, Double), c))]
forall a b. (a -> b) -> [a] -> [b]
map ((Double, Double)
-> ((Double, Double) -> b -> b')
-> FragmentTree (a, b, c)
-> FragmentTree (a, b', ((Double, Double), c))
forall b b' a c.
(Double, Double)
-> ((Double, Double) -> b -> b')
-> FragmentTree (a, b, c)
-> FragmentTree (a, b', ((Double, Double), c))
positionTree (Double, Double)
pos (Double, Double) -> b -> b'
cb) [FragmentTree (a, b, c)]
childs
  where
    pos :: (Double, Double)
pos = (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Int32 -> Double
hbScale (Rect Int32 -> Int32
forall a. (Num a, Ord a) => Rect a -> a
x_min Rect Int32
rect), Double
y Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Int32 -> Double
hbScale (Rect Int32 -> Int32
forall a. (Num a, Ord a) => Rect a -> a
y_min Rect Int32
rect))
    rect :: Rect Int32
rect = FragmentTree (a, b, c) -> Rect Int32
forall a b c. FragmentTree (a, b, c) -> Rect Int32
treeRect FragmentTree (a, b, c)
self
positionTree (Double
x, Double
y) (Double, Double) -> b -> b'
cb self :: FragmentTree (a, b, c)
self@(Leaf (Fragment (a
a, b
b, c
c) Int
d [AncestorBox (a, b, c)]
_ Rect Int32
f Rect Int32
g (Int32, Int32)
h [(GlyphInfo, GlyphPos)]
i)) =
    Fragment (a, b', ((Double, Double), c))
-> FragmentTree (a, b', ((Double, Double), c))
forall x. Fragment x -> FragmentTree x
Leaf ((a, b', ((Double, Double), c))
-> Int
-> [AncestorBox (a, b', ((Double, Double), c))]
-> Rect Int32
-> Rect Int32
-> (Int32, Int32)
-> [(GlyphInfo, GlyphPos)]
-> Fragment (a, b', ((Double, Double), c))
forall d.
d
-> Int
-> [AncestorBox d]
-> Rect Int32
-> Rect Int32
-> (Int32, Int32)
-> [(GlyphInfo, GlyphPos)]
-> Fragment d
Fragment (a
a, (Double, Double) -> b -> b'
cb (Double, Double)
pos b
b, ((Double, Double)
pos, c
c)) Int
d [] Rect Int32
f Rect Int32
g (Int32, Int32)
h [(GlyphInfo, GlyphPos)]
i)
  where
    pos :: (Double, Double)
pos = (Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Int32 -> Double
hbScale (Rect Int32 -> Int32
forall a. (Num a, Ord a) => Rect a -> a
x_min Rect Int32
rect), Double
y Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Int32 -> Double
hbScale (Rect Int32 -> Int32
forall a. (Num a, Ord a) => Rect a -> a
y_min Rect Int32
rect))
    rect :: Rect Int32
rect = FragmentTree (a, b, c) -> Rect Int32
forall a b c. FragmentTree (a, b, c) -> Rect Int32
treeRect FragmentTree (a, b, c)
self
-- | Retrieve 3rd userdata field.
treeInner :: FragmentTree (a, b, c) -> c
treeInner :: forall a b c. FragmentTree (a, b, c) -> c
treeInner (Branch AncestorBox { boxUserData :: forall d. AncestorBox d -> d
boxUserData = (a
_, b
_, c
ret) } [FragmentTree (a, b, c)]
_) = c
ret
treeInner (Leaf Fragment { fragmentUserData :: forall d. Fragment d -> d
fragmentUserData = (a
_, b
_, c
ret) }) = c
ret
-- | Retrieve userdata field.
treeInner' :: FragmentTree a -> a
treeInner' :: forall a. FragmentTree a -> a
treeInner' (Branch AncestorBox a
self [FragmentTree a]
_) = AncestorBox a -> a
forall d. AncestorBox d -> d
boxUserData AncestorBox a
self
treeInner' (Leaf Fragment a
self) = Fragment a -> a
forall d. Fragment d -> d
fragmentUserData Fragment a
self

-- | Retrieve Harfbuzz data out of the tree extracted from Balkón.
glyphs :: FragmentTree x -> [(HB.GlyphInfo, HB.GlyphPos)]
glyphs :: forall x. FragmentTree x -> [(GlyphInfo, GlyphPos)]
glyphs (Branch AncestorBox x
_ [FragmentTree x]
_) = []
glyphs (Leaf Fragment x
self) = Fragment x -> [(GlyphInfo, GlyphPos)]
forall d. Fragment d -> [(GlyphInfo, GlyphPos)]
fragmentGlyphs Fragment x
self
-- | Retrieve the Unicode codepoints out of the tree extracted from Balkón.
codepoints :: FragmentTree x -> [Word32]
codepoints :: forall x. FragmentTree x -> [Word32]
codepoints FragmentTree x
self = (GlyphInfo -> Word32) -> [GlyphInfo] -> [Word32]
forall a b. (a -> b) -> [a] -> [b]
map GlyphInfo -> Word32
HB.codepoint ([GlyphInfo] -> [Word32]) -> [GlyphInfo] -> [Word32]
forall a b. (a -> b) -> a -> b
$ ((GlyphInfo, GlyphPos) -> GlyphInfo)
-> [(GlyphInfo, GlyphPos)] -> [GlyphInfo]
forall a b. (a -> b) -> [a] -> [b]
map (GlyphInfo, GlyphPos) -> GlyphInfo
forall a b. (a, b) -> a
fst ([(GlyphInfo, GlyphPos)] -> [GlyphInfo])
-> [(GlyphInfo, GlyphPos)] -> [GlyphInfo]
forall a b. (a -> b) -> a -> b
$ FragmentTree x -> [(GlyphInfo, GlyphPos)]
forall x. FragmentTree x -> [(GlyphInfo, GlyphPos)]
glyphs FragmentTree x
self

------
--- Taken from Balkón
------
-- | Calculate the smallest rectangle that completely contains all the given
-- rectangles.
unions :: [Rect a] -> Rect a
unions [] = String -> Rect a -> Rect a
forall a. String -> a -> a
trace String
"No rects to union!" (Rect a -> Rect a) -> Rect a -> Rect a
forall a b. (a -> b) -> a -> b
$ a -> a -> a -> a -> Rect a
forall a. a -> a -> a -> a -> Rect a
Rect a
0 a
0 a
0 a
0
unions [Rect a]
rects = (Rect a -> Rect a -> Rect a) -> [Rect a] -> Rect a
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 Rect a -> Rect a -> Rect a
forall a. (Num a, Ord a) => Rect a -> Rect a -> Rect a
union [Rect a]
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 :: forall a. (Num a, Ord a) => Rect a -> Rect a -> Rect a
union Rect a
a Rect a
b = a -> a -> a -> a -> Rect a
forall a. a -> a -> a -> a -> Rect a
Rect a
x_low a
y_high a
dx (-a
dy) where
    x_low :: a
x_low = Rect a -> a
forall a. (Num a, Ord a) => Rect a -> a
x_min Rect a
a a -> a -> a
forall a. Ord a => a -> a -> a
`min` Rect a -> a
forall a. (Num a, Ord a) => Rect a -> a
x_min Rect a
b
    y_low :: a
y_low = Rect a -> a
forall a. (Num a, Ord a) => Rect a -> a
y_min Rect a
a a -> a -> a
forall a. Ord a => a -> a -> a
`min` Rect a -> a
forall a. (Num a, Ord a) => Rect a -> a
y_min Rect a
b
    x_high :: a
x_high = Rect a -> a
forall a. (Num a, Ord a) => Rect a -> a
x_max Rect a
a a -> a -> a
forall a. Ord a => a -> a -> a
`max` Rect a -> a
forall a. (Num a, Ord a) => Rect a -> a
x_max Rect a
b
    y_high :: a
y_high = Rect a -> a
forall a. (Num a, Ord a) => Rect a -> a
y_max Rect a
a a -> a -> a
forall a. Ord a => a -> a -> a
`max` Rect a -> a
forall a. (Num a, Ord a) => Rect a -> a
y_max Rect a
b
    dx :: a
dx = a
x_high a -> a -> a
forall a. Num a => a -> a -> a
- a
x_low
    dy :: a
dy = a
y_high a -> a -> a
forall a. Num a => a -> a -> a
- a
y_low