module Graphics.PDF.Navigation(
OutlineStyle(..)
, newSection
, newSectionWithPage
) where
import Graphics.PDF.Pages
import Graphics.PDF.Draw
import Graphics.PDF.LowLevel.Types
import Control.Monad.State(gets)
import Control.Monad(when)
import Data.Maybe(isNothing)
import qualified Data.Text as T
isFirst :: [Bool] -> Bool
isFirst :: [Bool] -> Bool
isFirst [Bool]
r = forall a. [a] -> a
head [Bool]
r
startNew :: PDF ()
startNew :: PDF ()
startNew = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modifyStrict forall a b. (a -> b) -> a -> b
$ \PdfState
s -> PdfState
s{firstOutline :: [Bool]
firstOutline = Bool
Trueforall a. a -> [a] -> [a]
:(PdfState -> [Bool]
firstOutline PdfState
s)}
addedOutline :: PDF ()
addedOutline :: PDF ()
addedOutline = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modifyStrict forall a b. (a -> b) -> a -> b
$ \PdfState
s -> PdfState
s{firstOutline :: [Bool]
firstOutline = Bool
Falseforall a. a -> [a] -> [a]
:forall a. [a] -> [a]
tail (PdfState -> [Bool]
firstOutline PdfState
s)}
closeNew :: PDF()
closeNew :: PDF ()
closeNew = do
[Bool]
r <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets PdfState -> [Bool]
firstOutline
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not ([Bool] -> Bool
isFirst [Bool]
r)) forall a b. (a -> b) -> a -> b
$ PDF ()
moveToParent
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modifyStrict forall a b. (a -> b) -> a -> b
$ \PdfState
s -> PdfState
s{firstOutline :: [Bool]
firstOutline = forall a. [a] -> [a]
tail (PdfState -> [Bool]
firstOutline PdfState
s)}
newSection :: T.Text
-> Maybe Color
-> Maybe OutlineStyle
-> PDF ()
-> PDF ()
newSection :: Text -> Maybe Color -> Maybe OutlineStyle -> PDF () -> PDF ()
newSection Text
myS Maybe Color
col Maybe OutlineStyle
style PDF ()
p = PDFString
-> Maybe Color
-> Maybe OutlineStyle
-> Maybe (PDFReference PDFPage)
-> PDF ()
-> PDF ()
newSectionPrivate (Text -> PDFString
toPDFString Text
myS) Maybe Color
col Maybe OutlineStyle
style forall a. Maybe a
Nothing PDF ()
p
newSectionWithPage :: T.Text
-> Maybe Color
-> Maybe OutlineStyle
-> PDFReference PDFPage
-> PDF ()
-> PDF ()
newSectionWithPage :: Text
-> Maybe Color
-> Maybe OutlineStyle
-> PDFReference PDFPage
-> PDF ()
-> PDF ()
newSectionWithPage Text
myS Maybe Color
col Maybe OutlineStyle
style PDFReference PDFPage
page PDF ()
p = PDFString
-> Maybe Color
-> Maybe OutlineStyle
-> Maybe (PDFReference PDFPage)
-> PDF ()
-> PDF ()
newSectionPrivate (Text -> PDFString
toPDFString Text
myS) Maybe Color
col Maybe OutlineStyle
style (forall a. a -> Maybe a
Just PDFReference PDFPage
page) PDF ()
p
newSectionPrivate :: PDFString
-> Maybe Color
-> Maybe OutlineStyle
-> Maybe (PDFReference PDFPage)
-> PDF ()
-> PDF ()
newSectionPrivate :: PDFString
-> Maybe Color
-> Maybe OutlineStyle
-> Maybe (PDFReference PDFPage)
-> PDF ()
-> PDF ()
newSectionPrivate PDFString
myS Maybe Color
col Maybe OutlineStyle
style Maybe (PDFReference PDFPage)
page PDF ()
p = do
let newlevel :: PDF ()
newlevel = do
PDF ()
startNew
PDF ()
p
PDF ()
closeNew
[Bool]
r <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets PdfState -> [Bool]
firstOutline
if [Bool] -> Bool
isFirst [Bool]
r
then do
if forall (t :: * -> *) a. Foldable t => t a -> Int
length [Bool]
r forall a. Ord a => a -> a -> Bool
> Int
1
then do
PDFString
-> Maybe Color
-> Maybe OutlineStyle
-> Maybe (PDFReference PDFPage)
-> PDF ()
newChild PDFString
myS Maybe Color
col Maybe OutlineStyle
style Maybe (PDFReference PDFPage)
page
PDF ()
addedOutline
PDF ()
newlevel
else do
PDFString
-> Maybe Color
-> Maybe OutlineStyle
-> Maybe (PDFReference PDFPage)
-> PDF ()
newSibling PDFString
myS Maybe Color
col Maybe OutlineStyle
style Maybe (PDFReference PDFPage)
page
PDF ()
newlevel
else do
PDFString
-> Maybe Color
-> Maybe OutlineStyle
-> Maybe (PDFReference PDFPage)
-> PDF ()
newSibling PDFString
myS Maybe Color
col Maybe OutlineStyle
style Maybe (PDFReference PDFPage)
page
PDF ()
newlevel
newSibling :: PDFString
-> Maybe Color
-> Maybe OutlineStyle
-> Maybe (PDFReference PDFPage)
-> PDF ()
newSibling :: PDFString
-> Maybe Color
-> Maybe OutlineStyle
-> Maybe (PDFReference PDFPage)
-> PDF ()
newSibling PDFString
myS Maybe Color
col Maybe OutlineStyle
style Maybe (PDFReference PDFPage)
page = do
Maybe (PDFReference PDFPage)
p <- if forall a. Maybe a -> Bool
isNothing Maybe (PDFReference PDFPage)
page then forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets PdfState -> Maybe (PDFReference PDFPage)
currentPage else forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (PDFReference PDFPage)
page
case Maybe (PDFReference PDFPage)
p of
Maybe (PDFReference PDFPage)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just PDFReference PDFPage
aPage -> do
Maybe Outline
ot <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets PdfState -> Maybe Outline
outline
let myValue :: (PDFString, Maybe Color, Maybe OutlineStyle, Destination)
myValue = (PDFString
myS,Maybe Color
col,Maybe OutlineStyle
style,PDFReference PDFPage -> Destination
Destination PDFReference PDFPage
aPage)
case Maybe Outline
ot of
Maybe Outline
Nothing -> forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modifyStrict forall a b. (a -> b) -> a -> b
$ \PdfState
s -> PdfState
s {outline :: Maybe Outline
outline = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> OutlineLoc a -> OutlineLoc a
insertDown (PDFString, Maybe Color, Maybe OutlineStyle, Destination)
myValue (forall a. Tree a -> OutlineCtx a -> OutlineLoc a
OutlineLoc (forall a. a -> [Tree a] -> Tree a
Node (PDFString, Maybe Color, Maybe OutlineStyle, Destination)
myValue []) forall a. OutlineCtx a
Top)}
Just Outline
r -> forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modifyStrict forall a b. (a -> b) -> a -> b
$ \PdfState
s -> PdfState
s {outline :: Maybe Outline
outline = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> OutlineLoc a -> OutlineLoc a
insertRight (PDFString, Maybe Color, Maybe OutlineStyle, Destination)
myValue Outline
r}
newChild :: PDFString
-> Maybe Color
-> Maybe OutlineStyle
-> Maybe (PDFReference PDFPage)
-> PDF ()
newChild :: PDFString
-> Maybe Color
-> Maybe OutlineStyle
-> Maybe (PDFReference PDFPage)
-> PDF ()
newChild PDFString
myS Maybe Color
col Maybe OutlineStyle
style Maybe (PDFReference PDFPage)
page = do
Maybe (PDFReference PDFPage)
p <- if forall a. Maybe a -> Bool
isNothing Maybe (PDFReference PDFPage)
page then forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets PdfState -> Maybe (PDFReference PDFPage)
currentPage else forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (PDFReference PDFPage)
page
case Maybe (PDFReference PDFPage)
p of
Maybe (PDFReference PDFPage)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just PDFReference PDFPage
aPage -> do
Maybe Outline
ot <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets PdfState -> Maybe Outline
outline
let myValue :: (PDFString, Maybe Color, Maybe OutlineStyle, Destination)
myValue = (PDFString
myS,Maybe Color
col,Maybe OutlineStyle
style,PDFReference PDFPage -> Destination
Destination PDFReference PDFPage
aPage)
case Maybe Outline
ot of
Maybe Outline
Nothing -> forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modifyStrict forall a b. (a -> b) -> a -> b
$ \PdfState
s -> PdfState
s {outline :: Maybe Outline
outline = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> OutlineLoc a -> OutlineLoc a
insertDown (PDFString, Maybe Color, Maybe OutlineStyle, Destination)
myValue (forall a. Tree a -> OutlineCtx a -> OutlineLoc a
OutlineLoc (forall a. a -> [Tree a] -> Tree a
Node (PDFString, Maybe Color, Maybe OutlineStyle, Destination)
myValue []) forall a. OutlineCtx a
Top)}
Just Outline
r -> forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modifyStrict forall a b. (a -> b) -> a -> b
$ \PdfState
s -> PdfState
s {outline :: Maybe Outline
outline = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> OutlineLoc a -> OutlineLoc a
insertDown (PDFString, Maybe Color, Maybe OutlineStyle, Destination)
myValue Outline
r}
moveToParent :: PDF ()
moveToParent :: PDF ()
moveToParent = do
Maybe Outline
ot <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets PdfState -> Maybe Outline
outline
case Maybe Outline
ot of
Maybe Outline
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just Outline
r -> forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modifyStrict forall a b. (a -> b) -> a -> b
$ \PdfState
s -> PdfState
s {outline :: Maybe Outline
outline = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. OutlineLoc a -> OutlineLoc a
up Outline
r}