2019-10-03 07:59:09 +02:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2019-09-23 23:19:27 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2019-11-30 12:39:40 +01:00
|
|
|
{-# LANGUAGE ConstraintKinds #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
module PDF.Text {-(
|
2019-11-29 11:48:28 +01:00
|
|
|
pageContents
|
2019-11-30 12:39:40 +01:00
|
|
|
)-} where
|
2019-09-23 18:00:47 +02:00
|
|
|
|
2019-09-27 18:16:12 +02:00
|
|
|
import Control.Applicative ((<|>))
|
2019-09-28 09:25:59 +02:00
|
|
|
import Control.Monad (foldM)
|
2020-02-08 08:15:32 +01:00
|
|
|
import Control.Monad.Fail (MonadFail)
|
2019-09-25 18:42:34 +02:00
|
|
|
import Control.Monad.Reader (ReaderT, runReaderT, asks)
|
2019-11-30 12:39:40 +01:00
|
|
|
import Control.Monad.State (MonadState, evalStateT, get, modify, put)
|
|
|
|
import Data.Attoparsec.ByteString.Char8 (choice, sepBy)
|
2019-09-27 18:16:12 +02:00
|
|
|
import Data.ByteString (ByteString)
|
2019-11-30 12:39:40 +01:00
|
|
|
import Data.ByteString.Char8 (pack, unpack)
|
2020-02-08 08:15:32 +01:00
|
|
|
import Data.Char (toLower)
|
2019-11-30 21:46:22 +01:00
|
|
|
import Data.Map ((!), (!?), Map)
|
2020-02-08 08:15:32 +01:00
|
|
|
import qualified Data.Map as Map (fromList)
|
|
|
|
import Data.Text (Text)
|
|
|
|
import PDF.Font (Font, FontSet, emptyFont)
|
2019-09-25 18:42:34 +02:00
|
|
|
import PDF.Object (
|
2019-09-27 18:16:12 +02:00
|
|
|
DirectObject(..), StringObject(..)
|
2019-10-04 18:46:07 +02:00
|
|
|
, array, blank, name, regular, stringObject, toByteString
|
2019-09-25 18:42:34 +02:00
|
|
|
)
|
2020-02-08 08:15:32 +01:00
|
|
|
import PDF.Parser (MonadParser(..), (<?>), Parser, evalParser)
|
2019-09-23 18:00:47 +02:00
|
|
|
|
2019-09-25 18:42:34 +02:00
|
|
|
data StateOperator =
|
2020-02-08 08:15:32 +01:00
|
|
|
C_m | W_ | J | J_ | M | D_ | R_i | I_ | G_s -- general graphic state
|
|
|
|
deriving (Bounded, Enum, Show)
|
|
|
|
data PathOperator =
|
|
|
|
M_ | L_ | C_ | V_ | Y_ | H_ | R_e -- path construction
|
|
|
|
| S | S_ | F_ | F | Fstar | B | Bstar | B_ | B_star | N_ -- path painting
|
|
|
|
| W | Wstar -- clipping path
|
|
|
|
deriving (Bounded, Enum, Show)
|
|
|
|
data ColorOperator =
|
|
|
|
CS | C_s | SC | SCN | S_c | S_cn | G | G_ | RG | R_g | K | K_
|
|
|
|
deriving (Bounded, Enum, Show)
|
2019-09-25 18:42:34 +02:00
|
|
|
data TextOperator =
|
|
|
|
Td | TD | Tm | Tstar -- text positioning
|
|
|
|
| TJ | Tj | Quote | DQuote -- text showing
|
|
|
|
| Tc | Tw | Tz | TL | Tf | Tr | Ts -- text state
|
2020-02-08 08:15:32 +01:00
|
|
|
deriving (Bounded, Enum, Show)
|
2019-09-30 14:13:12 +02:00
|
|
|
data Argument = Raw ByteString | Typed DirectObject deriving Show
|
2019-09-25 18:42:34 +02:00
|
|
|
type Call a = (a, [Argument])
|
|
|
|
|
2020-02-08 08:15:32 +01:00
|
|
|
type Operator a = (Bounded a, Enum a, Show a)
|
|
|
|
|
|
|
|
code :: Operator a => a -> ByteString
|
|
|
|
code = pack . expand . show
|
|
|
|
where
|
|
|
|
expand "" = ""
|
|
|
|
expand (c:'_':s) = toLower c : expand s
|
|
|
|
expand ('s':'t':'a':'r':s) = '*' : expand s
|
|
|
|
expand ('Q':'u':'o':'t':'e':s) = '\'' : expand s
|
|
|
|
expand ('D':'Q':'u':'o':'t':'e':s) = '"' : expand s
|
|
|
|
expand (c:s) = c : expand s
|
|
|
|
|
|
|
|
{-
|
2019-11-30 12:39:40 +01:00
|
|
|
instance Show StateOperator where
|
|
|
|
show Cm = "cm"
|
2020-02-08 08:15:32 +01:00
|
|
|
show W_ = "w"
|
2019-11-30 12:39:40 +01:00
|
|
|
show J = "J"
|
|
|
|
show J_ = "j"
|
|
|
|
show M = "M"
|
|
|
|
show D = "d"
|
|
|
|
show Ri = "ri"
|
|
|
|
show I = "i"
|
|
|
|
show Gs = "gs"
|
|
|
|
|
2020-02-08 08:15:32 +01:00
|
|
|
instance Show PathOperator where
|
|
|
|
show M_ = "m"
|
|
|
|
show L_ = "l"
|
|
|
|
show C_ = "c"
|
|
|
|
show V_ = "v"
|
|
|
|
show Y_ = "y"
|
|
|
|
show H_
|
|
|
|
("m", (M_, \l -> case l of [Raw _, Raw _] -> True ; _ -> False))
|
|
|
|
, ("l", (L_, \l -> case l of [Raw _, Raw _] -> True ; _ -> False))
|
|
|
|
, ("c", (L_, \l -> case l of [Raw _, Raw _, Raw _, Raw _, Raw _, Raw _] -> True ; _ -> False))
|
|
|
|
, ("v", (L_, \l -> case l of [Raw _, Raw _, Raw _, Raw _] -> True ; _ -> False))
|
|
|
|
, ("y", (L_, \l -> case l of [Raw _, Raw _, Raw _, Raw _] -> True ; _ -> False))
|
|
|
|
, ("h", (L_, \l -> case l of [] -> True ; _ -> False))
|
|
|
|
, ("re", (L_, \l -> case l of [Raw _, Raw _, Raw _, Raw _] -> True ; _ -> False))
|
|
|
|
, ("S", (L_, \l -> case l of [] -> True ; _ -> False))
|
|
|
|
, ("s", (L_, \l -> case l of [] -> True ; _ -> False))
|
|
|
|
, ("f", (L_, \l -> case l of [] -> True ; _ -> False))
|
|
|
|
, ("F", (L_, \l -> case l of [] -> True ; _ -> False))
|
|
|
|
, ("F*", (L_, \l -> case l of [] -> True ; _ -> False))
|
|
|
|
, ("B", (L_, \l -> case l of [] -> True ; _ -> False))
|
|
|
|
, ("B*", (L_, \l -> case l of [] -> True ; _ -> False))
|
|
|
|
, ("b", (L_, \l -> case l of [] -> True ; _ -> False))
|
|
|
|
, ("b*", (L_, \l -> case l of [] -> True ; _ -> False))
|
|
|
|
, ("n", (L_, \l -> case l of [] -> True ; _ -> False))
|
|
|
|
, ("W", (L_, \l -> case l of [] -> True ; _ -> False))
|
|
|
|
, ("W*", (L_, \l -> case l of [] -> True ; _ -> False))
|
|
|
|
|
|
|
|
instance Show ColorOperator where
|
|
|
|
|
2019-11-30 12:39:40 +01:00
|
|
|
instance Show TextOperator where
|
|
|
|
show Td = "Td"
|
|
|
|
show TD = "TD"
|
|
|
|
show Tm = "Tm"
|
|
|
|
show Tstar = "T*"
|
|
|
|
show TJ = "TJ"
|
|
|
|
show Tj = "Tj"
|
|
|
|
show Quote = "'"
|
|
|
|
show DQuote = "\""
|
|
|
|
show Tc = "Tc"
|
|
|
|
show Tw = "Tw"
|
|
|
|
show Tz = "Tz"
|
|
|
|
show TL = "TL"
|
|
|
|
show Tf = "Tf"
|
|
|
|
show Tr = "Tr"
|
|
|
|
show Ts = "Ts"
|
2020-02-08 08:15:32 +01:00
|
|
|
-}
|
2019-11-30 12:39:40 +01:00
|
|
|
|
2019-11-30 21:46:22 +01:00
|
|
|
stateOperator :: OperatorTable StateOperator
|
2020-02-08 08:15:32 +01:00
|
|
|
stateOperator = Map.fromList $ (\(op, checker) -> (code op, (op, checker))) <$> [
|
|
|
|
(C_m, \l -> case l of [Raw _, Raw _, Raw _, Raw _, Raw _, Raw _] -> True ; _ -> False)
|
|
|
|
, (W_, \l -> case l of [Raw _] -> True ; _ -> False)
|
|
|
|
, (J, \l -> case l of [Raw _] -> True ; _ -> False)
|
|
|
|
, (J_, \l -> case l of [Raw _] -> True ; _ -> False)
|
|
|
|
, (M, \l -> case l of [Raw _] -> True ; _ -> False)
|
|
|
|
, (D_, \l -> case l of [Raw _, Raw _] -> True ; _ -> False)
|
|
|
|
, (R_i, \l -> case l of [Raw _] -> True ; _ -> False)
|
|
|
|
, (I_, \l -> case l of [Raw _] -> True ; _ -> False)
|
|
|
|
, (G_s, \l -> case l of [Typed (NameObject _)] -> True ; _ -> False)
|
|
|
|
]
|
|
|
|
|
|
|
|
pathOperator :: OperatorTable PathOperator
|
|
|
|
pathOperator = Map.fromList $ (\(op, checker) -> (code op, (op, checker))) <$> [
|
|
|
|
(M_, \l -> case l of [Raw _, Raw _] -> True ; _ -> False)
|
|
|
|
, (L_, \l -> case l of [Raw _, Raw _] -> True ; _ -> False)
|
|
|
|
, (C_, \l -> case l of [Raw _, Raw _, Raw _, Raw _, Raw _, Raw _] -> True ; _ -> False)
|
|
|
|
, (V_, \l -> case l of [Raw _, Raw _, Raw _, Raw _] -> True ; _ -> False)
|
|
|
|
, (Y_, \l -> case l of [Raw _, Raw _, Raw _, Raw _] -> True ; _ -> False)
|
|
|
|
, (H_, \l -> case l of [] -> True ; _ -> False)
|
|
|
|
, (R_e, \l -> case l of [Raw _, Raw _, Raw _, Raw _] -> True ; _ -> False)
|
|
|
|
, (S, \l -> case l of [] -> True ; _ -> False)
|
|
|
|
, (S_, \l -> case l of [] -> True ; _ -> False)
|
|
|
|
, (F_, \l -> case l of [] -> True ; _ -> False)
|
|
|
|
, (F, \l -> case l of [] -> True ; _ -> False)
|
|
|
|
, (Fstar, \l -> case l of [] -> True ; _ -> False)
|
|
|
|
, (B, \l -> case l of [] -> True ; _ -> False)
|
|
|
|
, (Bstar, \l -> case l of [] -> True ; _ -> False)
|
|
|
|
, (B_, \l -> case l of [] -> True ; _ -> False)
|
|
|
|
, (B_star, \l -> case l of [] -> True ; _ -> False)
|
|
|
|
, (N_, \l -> case l of [] -> True ; _ -> False)
|
|
|
|
, (W, \l -> case l of [] -> True ; _ -> False)
|
|
|
|
, (Wstar, \l -> case l of [] -> True ; _ -> False)
|
|
|
|
]
|
|
|
|
|
|
|
|
colorOperator :: OperatorTable ColorOperator
|
|
|
|
colorOperator = Map.fromList $ (\(op, checker) -> (code op, (op, checker))) <$> [
|
|
|
|
(CS, \l -> case l of [Typed (NameObject _)] -> True ; _ -> False)
|
|
|
|
, (C_s, \l -> case l of [Typed (NameObject _)] -> True ; _ -> False)
|
|
|
|
, (SC, \_ -> True)
|
|
|
|
, (SCN, \_ -> True)
|
|
|
|
, (S_c, \_ -> True)
|
|
|
|
, (S_cn, \_ -> True)
|
|
|
|
, (G, \l -> case l of [Raw _] -> True ; _ -> False)
|
|
|
|
, (G_, \l -> case l of [Raw _] -> True ; _ -> False)
|
|
|
|
, (RG, \l -> case l of [Raw _, Raw _, Raw _] -> True ; _ -> False)
|
|
|
|
, (R_g, \l -> case l of [Raw _, Raw _, Raw _] -> True ; _ -> False)
|
|
|
|
, (K, \l -> case l of [Raw _, Raw _, Raw _, Raw _] -> True ; _ -> False)
|
|
|
|
, (K_, \l -> case l of [Raw _, Raw _, Raw _, Raw _] -> True ; _ -> False)
|
2019-11-30 21:46:22 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
{-
|
2019-11-30 12:39:40 +01:00
|
|
|
stateOperator (Cm, [Raw _, Raw _, Raw _, Raw _, Raw _, Raw _]) = True
|
|
|
|
stateOperator (W, [Raw _]) = True
|
|
|
|
stateOperator (J, [Raw _]) = True
|
|
|
|
stateOperator (J_, [Raw _]) = True
|
|
|
|
stateOperator (M, [Raw _]) = True
|
|
|
|
stateOperator (D, [Raw _, Raw _]) = True
|
|
|
|
stateOperator (Ri, [Raw _]) = True
|
|
|
|
stateOperator (I, [Raw _]) = True
|
|
|
|
stateOperator (Gs, [Typed (NameObject _)]) = True
|
|
|
|
stateOperator _ = False
|
2019-11-30 21:46:22 +01:00
|
|
|
-}
|
|
|
|
|
|
|
|
textOperator :: OperatorTable TextOperator
|
2020-02-08 08:15:32 +01:00
|
|
|
textOperator = Map.fromList $ (\(op, checker) -> (code op, (op, checker))) <$> [
|
2019-11-30 21:46:22 +01:00
|
|
|
(Td, \l -> case l of [Raw _, Raw _] -> True ; _ -> False)
|
|
|
|
, (TD, \l -> case l of [Raw _, Raw _] -> True ; _ -> False)
|
|
|
|
, (Tm, \l -> case l of [Raw _, Raw _, Raw _, Raw _, Raw _, Raw _] -> True ; _ -> False)
|
|
|
|
, (Tstar, \l -> case l of [] -> True ; _ -> False)
|
|
|
|
, (TJ, \l -> case l of [Typed (Array _)] -> True ; _ -> False)
|
|
|
|
, (Tj, \l -> case l of [Typed (StringObject _)] -> True ; _ -> False)
|
|
|
|
, (Quote, \l -> case l of [Typed (StringObject _)] -> True ; _ -> False)
|
|
|
|
, (DQuote, \l -> case l of [Typed (StringObject _)] -> True ; _ -> False)
|
|
|
|
, (Tc, \l -> case l of [Raw _] -> True ; _ -> False)
|
|
|
|
, (Tw, \l -> case l of [Raw _] -> True ; _ -> False)
|
|
|
|
, (Tz, \l -> case l of [Raw _] -> True ; _ -> False)
|
|
|
|
, (TL, \l -> case l of [Raw _] -> True ; _ -> False)
|
|
|
|
, (Tf, \l -> case l of [Typed (NameObject _), Raw _] -> True ; _ -> False)
|
|
|
|
, (Tr, \l -> case l of [Raw _] -> True ; _ -> False)
|
|
|
|
, (Ts, \l -> case l of [Raw _] -> True ; _ -> False)
|
|
|
|
]
|
|
|
|
|
|
|
|
{-
|
2019-11-30 12:39:40 +01:00
|
|
|
textOperator (Td, [Raw _, Raw _]) = True
|
|
|
|
textOperator (TD, [Raw _, Raw _]) = True
|
|
|
|
textOperator (Tm, [Raw _, Raw _, Raw _, Raw _, Raw _, Raw _]) = True
|
|
|
|
textOperator (Tstar, []) = True
|
|
|
|
textOperator (TJ, [Typed (Array _)]) = True
|
|
|
|
textOperator (Tj, [Typed (StringObject _)]) = True
|
|
|
|
textOperator (Quote, [Typed (StringObject _)]) = True
|
|
|
|
textOperator (DQuote, [Typed (StringObject _)]) = True
|
|
|
|
textOperator (Tc, [Raw _]) = True
|
|
|
|
textOperator (Tw, [Raw _]) = True
|
|
|
|
textOperator (Tz, [Raw _]) = True
|
|
|
|
textOperator (TL, [Raw _]) = True
|
|
|
|
textOperator (Tf, [Typed (NameObject _), Raw _]) = True
|
|
|
|
textOperator (Tr, [Raw _]) = True
|
|
|
|
textOperator (Ts, [Raw _]) = True
|
|
|
|
textOperator _ = False
|
2019-11-30 21:46:22 +01:00
|
|
|
-}
|
2019-11-30 12:39:40 +01:00
|
|
|
|
|
|
|
type ArgumentStackParser m = (MonadState [Argument] m, MonadParser m)
|
2020-02-08 08:15:32 +01:00
|
|
|
--type Operator a = (Bounded a, Enum a, Show a)
|
2019-11-30 21:46:22 +01:00
|
|
|
type OperatorTable a = Map ByteString (TypeChecker a)
|
|
|
|
type TypeChecker a = (a, [Argument] -> Bool)
|
2019-11-30 12:39:40 +01:00
|
|
|
|
|
|
|
parseShowable :: (Show a, MonadParser m) => a -> m a
|
|
|
|
parseShowable textOp = string (pack $ show textOp) *> return textOp
|
|
|
|
|
2019-11-30 21:46:22 +01:00
|
|
|
callChunk :: MonadParser m => OperatorTable a -> m (Either (TypeChecker a) Argument)
|
|
|
|
callChunk table =
|
|
|
|
(Right <$> choice [stringArg, nameArg, arrayArg])
|
|
|
|
<|> operatorOrRawArg
|
2019-11-30 12:39:40 +01:00
|
|
|
<?> "call chunk"
|
|
|
|
where
|
2019-11-30 21:46:22 +01:00
|
|
|
operatorOrRawArg = do
|
|
|
|
chunk <- takeAll1 regular <* blank
|
|
|
|
case table !? chunk of
|
|
|
|
Nothing -> return . Right $ Raw chunk
|
|
|
|
Just typeChecker -> return $ Left typeChecker
|
|
|
|
|
2020-02-08 08:15:32 +01:00
|
|
|
stackParser :: (ArgumentStackParser m, Operator a) => OperatorTable a -> m (Call a)
|
2019-11-30 21:46:22 +01:00
|
|
|
stackParser table = either popCall push =<< (callChunk table)
|
|
|
|
where
|
|
|
|
push arg = modify (arg:) >> stackParser table
|
|
|
|
popCall (operator, predicate) = do
|
|
|
|
arguments <- reverse <$> get
|
|
|
|
let call = (operator, arguments)
|
2020-02-08 08:15:32 +01:00
|
|
|
if predicate arguments then return call else fail (unpack $ code operator)
|
2019-11-30 21:46:22 +01:00
|
|
|
|
|
|
|
a :: (Operator a, MonadParser m) => OperatorTable a -> m (Call a)
|
|
|
|
a table = evalStateT (stackParser table) []
|
2019-09-25 18:42:34 +02:00
|
|
|
|
2019-09-27 12:21:06 +02:00
|
|
|
argument :: MonadParser m => m Argument
|
2019-11-30 12:39:40 +01:00
|
|
|
argument = Raw <$> takeAll1 regular <* blank
|
2019-09-23 18:00:47 +02:00
|
|
|
|
2019-09-28 09:25:59 +02:00
|
|
|
arrayArg :: MonadParser m => m Argument
|
|
|
|
arrayArg = Typed . Array <$> array <* blank
|
|
|
|
|
|
|
|
nameArg :: MonadParser m => m Argument
|
|
|
|
nameArg = Typed . NameObject <$> name <* blank
|
|
|
|
|
|
|
|
stringArg :: MonadParser m => m Argument
|
|
|
|
stringArg = Typed . StringObject <$> stringObject <* blank
|
|
|
|
|
2020-02-08 08:15:32 +01:00
|
|
|
type ParserWithFont = ReaderT FontSet (Parser Font)
|
|
|
|
|
|
|
|
pageContents :: FontSet -> ByteString -> Either String [Text]
|
|
|
|
pageContents fontSet input =
|
|
|
|
evalParser (runReaderT (page) fontSet) emptyFont input
|
2019-09-24 18:38:12 +02:00
|
|
|
|
2020-02-08 08:15:32 +01:00
|
|
|
several :: MonadParser m => m [a] -> m [a]
|
|
|
|
several p = concat <$> (p `sepBy` blank)
|
2019-09-24 18:38:12 +02:00
|
|
|
|
2020-02-08 08:15:32 +01:00
|
|
|
page :: ParserWithFont [Text]
|
|
|
|
page = several (graphicState <|> text) <* blank <* endOfInput <?> "Text page contents"
|
2019-09-24 18:38:12 +02:00
|
|
|
|
2020-02-08 08:15:32 +01:00
|
|
|
graphicState :: ParserWithFont [Text]
|
2019-09-23 23:19:27 +02:00
|
|
|
graphicState =
|
2020-02-08 08:15:32 +01:00
|
|
|
string "q" *> blank *> insideQ <* blank <* string "Q" <?> "Graphic state"
|
2019-09-23 23:19:27 +02:00
|
|
|
where
|
2020-02-08 08:15:32 +01:00
|
|
|
insideQ = several (command <|> graphicState <|> text)
|
|
|
|
ignore x = a x *> return []
|
|
|
|
command =
|
|
|
|
ignore stateOperator <|> ignore pathOperator <|> ignore colorOperator
|
2019-09-23 23:19:27 +02:00
|
|
|
|
2020-02-08 08:15:32 +01:00
|
|
|
text :: ParserWithFont [Text]
|
2019-09-25 18:42:34 +02:00
|
|
|
text =
|
2019-09-28 09:25:59 +02:00
|
|
|
string "BT" *> blank *> commands <* blank <* string "ET" <?> "Text operators"
|
2019-09-25 18:42:34 +02:00
|
|
|
where
|
2020-02-08 08:15:32 +01:00
|
|
|
commands = several (a textOperator >>= runOperator)
|
2019-09-25 18:42:34 +02:00
|
|
|
|
2020-02-08 08:15:32 +01:00
|
|
|
runOperator :: Call TextOperator -> ParserWithFont [Text]
|
2019-09-25 18:42:34 +02:00
|
|
|
runOperator (Tf, [Typed (NameObject fontName), _]) =
|
|
|
|
asks (! fontName) >>= put >> return []
|
|
|
|
|
|
|
|
runOperator (Tstar, []) = return ["\n"]
|
|
|
|
|
|
|
|
runOperator (TJ, [Typed (Array arrayObject)]) =
|
|
|
|
replicate 1 <$> foldM appendText "" arrayObject
|
|
|
|
where
|
|
|
|
appendText bs (StringObject outputString) =
|
|
|
|
mappend bs <$> decodeString outputString
|
|
|
|
appendText bs _ = return bs
|
2019-09-30 14:13:12 +02:00
|
|
|
|
2019-09-25 18:42:34 +02:00
|
|
|
runOperator (Tj, [Typed (StringObject outputString)]) =
|
|
|
|
replicate 1 <$> decodeString outputString
|
|
|
|
|
|
|
|
runOperator (Quote, [Typed (StringObject outputString)]) =
|
|
|
|
(\bs -> ["\n", bs]) <$> decodeString outputString
|
|
|
|
|
|
|
|
runOperator (DQuote, [Typed (StringObject outputString)]) =
|
|
|
|
(\bs -> ["\n", bs]) <$> decodeString outputString
|
2019-09-24 18:38:12 +02:00
|
|
|
|
2019-09-25 18:42:34 +02:00
|
|
|
runOperator _ = return []
|
2019-09-23 23:19:27 +02:00
|
|
|
|
2020-02-08 08:15:32 +01:00
|
|
|
decodeString :: (MonadFail m, MonadState Font m) => StringObject -> m Text
|
|
|
|
decodeString input = do
|
|
|
|
font <- get
|
|
|
|
either fail return . font $ toByteString input
|