2020-02-23 22:21:09 +01:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2020-02-09 22:42:57 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2020-02-23 22:21:09 +01:00
|
|
|
{-# LANGUAGE ConstraintKinds #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2020-03-05 17:44:38 +01:00
|
|
|
{-# LANGUAGE TypeSynonymInstances #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
2020-02-09 22:42:57 +01:00
|
|
|
module PDF.Content (
|
|
|
|
Content(..)
|
|
|
|
, ContentUnit(..)
|
|
|
|
, GraphicContextUnit(..)
|
|
|
|
, TextContext
|
|
|
|
, parse
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Applicative ((<|>))
|
2020-03-08 22:16:23 +01:00
|
|
|
import Control.Monad.Fail (MonadFail)
|
2020-02-23 22:21:09 +01:00
|
|
|
import Control.Monad.Reader (asks, runReader)
|
2020-03-17 08:39:29 +01:00
|
|
|
import Control.Monad.State (evalStateT, modify)
|
2020-02-09 22:42:57 +01:00
|
|
|
import Data.Attoparsec.ByteString.Char8 (sepBy)
|
|
|
|
import Data.ByteString (ByteString)
|
2020-03-17 08:39:29 +01:00
|
|
|
import Data.Id (Id(..), Indexed, at, empty, register)
|
2020-03-05 17:44:38 +01:00
|
|
|
import PDF.Box (Box(..))
|
2020-02-11 17:26:47 +01:00
|
|
|
import PDF.Content.Operator (Instruction, operator)
|
2020-02-09 22:42:57 +01:00
|
|
|
import PDF.Object (blank, directObject)
|
2020-02-11 17:26:47 +01:00
|
|
|
import PDF.Output (Output(..), line)
|
2020-03-08 22:16:23 +01:00
|
|
|
import PDF.Parser (Parser, runParser, string)
|
2020-02-23 22:21:09 +01:00
|
|
|
|
2020-03-05 17:44:38 +01:00
|
|
|
data Instructions = Instructions
|
2020-02-09 22:42:57 +01:00
|
|
|
|
|
|
|
data GraphicContextUnit =
|
2020-03-06 19:21:16 +01:00
|
|
|
GraphicInstruction (Id Instruction)
|
2020-02-09 22:42:57 +01:00
|
|
|
| ContentUnit ContentUnit
|
|
|
|
deriving Show
|
2020-03-06 19:21:16 +01:00
|
|
|
type TextContext = [Id Instruction]
|
2020-02-09 22:42:57 +01:00
|
|
|
data ContentUnit =
|
|
|
|
GraphicContext [GraphicContextUnit]
|
|
|
|
| TextContext TextContext
|
|
|
|
deriving Show
|
2020-02-23 22:21:09 +01:00
|
|
|
data Content = Content {
|
|
|
|
contentUnits :: [ContentUnit]
|
2020-03-06 19:21:16 +01:00
|
|
|
, indexedInstructions :: Indexed Instruction
|
2020-02-23 22:21:09 +01:00
|
|
|
} deriving Show
|
|
|
|
|
2020-03-08 22:16:23 +01:00
|
|
|
type InstructionParser = Parser (Indexed Instruction)
|
2020-02-23 22:21:09 +01:00
|
|
|
|
2020-03-06 19:21:16 +01:00
|
|
|
instance Monad m => Box m Instructions Content (Indexed Instruction) where
|
2020-03-05 17:44:38 +01:00
|
|
|
r Instructions = return . indexedInstructions
|
|
|
|
w Instructions indexedInstructions someContent =
|
|
|
|
return $ someContent {indexedInstructions}
|
|
|
|
|
2020-03-08 22:16:23 +01:00
|
|
|
parse :: MonadFail m => ByteString -> m Content
|
|
|
|
parse =
|
2020-03-15 15:13:00 +01:00
|
|
|
either fail (return . uncurry Content) . runParser contentUnits empty
|
2020-02-23 22:21:09 +01:00
|
|
|
where
|
2020-03-08 22:16:23 +01:00
|
|
|
contentUnits = contentUnit `sepBy` blank
|
2020-02-09 22:42:57 +01:00
|
|
|
|
2020-03-08 22:16:23 +01:00
|
|
|
contentUnit :: InstructionParser ContentUnit
|
2020-02-09 22:42:57 +01:00
|
|
|
contentUnit =
|
|
|
|
(GraphicContext <$> graphicContext)
|
|
|
|
<|> (TextContext <$> textContext)
|
|
|
|
where
|
|
|
|
graphicContext =
|
|
|
|
string "q" *> blank *> graphicContextUnit `sepBy` blank <* blank <* string "Q"
|
|
|
|
|
2020-03-08 22:16:23 +01:00
|
|
|
graphicContextUnit :: InstructionParser GraphicContextUnit
|
2020-02-09 22:42:57 +01:00
|
|
|
graphicContextUnit =
|
|
|
|
(GraphicInstruction <$> instruction)
|
|
|
|
<|> (ContentUnit <$> contentUnit)
|
|
|
|
|
2020-03-08 22:16:23 +01:00
|
|
|
instruction :: InstructionParser (Id Instruction)
|
2020-02-23 22:21:09 +01:00
|
|
|
instruction = evalStateT stackParser [] >>= register
|
2020-02-09 22:42:57 +01:00
|
|
|
where
|
|
|
|
stackParser = ((directObject <* blank) >>= push) <|> operator
|
|
|
|
push arg = modify (arg:) *> stackParser
|
|
|
|
|
2020-03-08 22:16:23 +01:00
|
|
|
textContext :: InstructionParser TextContext
|
2020-02-09 22:42:57 +01:00
|
|
|
textContext =
|
|
|
|
string "BT" *> blank *> instruction `sepBy` blank <* blank <* string "ET"
|
2020-02-11 17:26:47 +01:00
|
|
|
|
|
|
|
instance Output Content where
|
2020-02-23 22:21:09 +01:00
|
|
|
output (Content {contentUnits, indexedInstructions}) =
|
|
|
|
runReader (mconcat <$> mapM outputCU contentUnits) indexedInstructions
|
|
|
|
where
|
|
|
|
outputCU (GraphicContext gc) = do
|
|
|
|
inside <- mconcat <$> mapM outputGCU gc
|
|
|
|
return (line "q" `mappend` inside `mappend` line "Q")
|
|
|
|
outputCU (TextContext tc) = do
|
|
|
|
inside <- mconcat <$> mapM outputIId tc
|
|
|
|
return (line "BT" `mappend` inside `mappend` line "ET")
|
|
|
|
outputGCU (GraphicInstruction gi) = outputIId gi
|
|
|
|
outputGCU (ContentUnit cu) = outputCU cu
|
2020-03-15 15:13:00 +01:00
|
|
|
outputIId instructionId = asks (output . (`at` instructionId))
|