Implement r for access by PageNumber and clean the mess a bit

This commit is contained in:
Tissevert 2020-03-05 10:09:09 +01:00
parent 2b9abc24b6
commit 50ac0692b2
1 changed files with 10 additions and 8 deletions

View File

@ -12,13 +12,11 @@ module PDF.Pages (
, cacheFonts
) where
import Control.Applicative (Alternative(..))
import Control.Applicative ((<|>))
import Control.Applicative (Alternative, (<|>))
import Control.Monad (foldM)
import Control.Monad.Fail (MonadFail(..))
import Control.Monad.Reader (runReaderT)
import Control.Monad.State (MonadState, StateT(..), evalStateT)
import Control.Monad.RWS (RWST(..), evalRWST, modify)
import Control.Monad.State (MonadState, StateT(..), evalStateT, modify)
import qualified Control.Monad.RWS as RWS (get)
import Data.Map (Map)
import qualified Data.Map as Map (empty, fromList, insert, lookup, toList)
@ -35,16 +33,14 @@ import PDF.Object (
, Name(..), Object(..)
,)
import PDF.Object.Navigation (
Clear(..), Error(..), PDFContent, (./), (//), (>./), (>//), castObject
, getDictionary, objectById, origin
Clear(..), PDFContent, (//), (>./), (>//), castObject, getDictionary
, objectById, origin
)
import PDF.Output (ObjectId(..))
import Prelude hiding (fail)
import Text.Printf (printf)
type CachedFonts = Map ObjectId Font
type T = RWST Layer () CachedFonts Error
type PageContext = StateT CachedFonts
type FontCache m = (MonadState CachedFonts m, PDFContent m)
data Page = Page {
contents :: [Text]
@ -129,6 +125,12 @@ instance (MonadFail m, Alternative m, MonadState CachedFonts m) => Box m Pages L
numbered = Map.fromList . zip [1..]
instance (MonadFail m, Alternative m, MonadState CachedFonts m) => Box m PageNumber Layer Page where
r (P i) layer
| i < 1 = fail "Pages start at 1"
| otherwise = runReaderT (drop (i - 1) <$> pagesList >>= firstPage) layer
where
firstPage [] = fail "Page is out of bounds"
firstPage (p:_) = loadPage p
cacheFonts :: Monad m => StateT CachedFonts m a -> m a
cacheFonts = flip evalStateT Map.empty