2019-05-16 17:04:45 +02:00
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2019-05-13 18:22:05 +02:00
|
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2019-05-16 17:04:45 +02:00
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
2019-05-13 18:22:05 +02:00
|
|
|
|
module PDF.Object (
|
|
|
|
|
Content(..)
|
|
|
|
|
, DirectObject(..)
|
2019-05-14 18:42:11 +02:00
|
|
|
|
, IndirectObjCoordinates(..)
|
2019-05-13 18:22:05 +02:00
|
|
|
|
, Object(..)
|
|
|
|
|
, Occurrence(..)
|
2019-05-15 09:04:17 +02:00
|
|
|
|
, Parser
|
2019-05-13 18:22:05 +02:00
|
|
|
|
, XRefEntry(..)
|
2019-05-14 18:42:11 +02:00
|
|
|
|
, XRefSection
|
2019-05-13 18:22:05 +02:00
|
|
|
|
, XRefSubSection(..)
|
2019-05-15 15:03:55 +02:00
|
|
|
|
, blank
|
2019-05-13 18:22:05 +02:00
|
|
|
|
, content
|
2019-05-14 18:42:11 +02:00
|
|
|
|
, dictionary
|
|
|
|
|
, directObject
|
2019-05-16 17:04:45 +02:00
|
|
|
|
, eofMarker
|
2019-05-14 18:42:11 +02:00
|
|
|
|
, integer
|
|
|
|
|
, line
|
2019-05-16 17:04:45 +02:00
|
|
|
|
, magicNumber
|
2019-05-13 18:22:05 +02:00
|
|
|
|
) where
|
|
|
|
|
|
|
|
|
|
import Data.ByteString.Lazy.Char8 (ByteString)
|
|
|
|
|
import Data.Int (Int64)
|
|
|
|
|
import Data.Map (Map)
|
2019-05-16 17:04:45 +02:00
|
|
|
|
import qualified Data.Map as Map (empty, elems, fromList, toList)
|
|
|
|
|
import qualified PDF.EOL as EOL (charset, parser)
|
|
|
|
|
import qualified PDF.Output as Output (string)
|
|
|
|
|
import PDF.Output (Output(..), byteString, join, newLine, nextLine)
|
2019-05-13 18:22:05 +02:00
|
|
|
|
import Text.Parsec
|
2019-05-16 17:04:45 +02:00
|
|
|
|
import Text.Printf (printf)
|
2019-05-14 18:42:11 +02:00
|
|
|
|
|
|
|
|
|
type Parser u = Parsec ByteString u
|
2019-05-13 18:22:05 +02:00
|
|
|
|
|
|
|
|
|
type Dictionary = Map String DirectObject
|
|
|
|
|
|
2019-05-16 17:04:45 +02:00
|
|
|
|
instance Output (Map String DirectObject) where
|
|
|
|
|
output dict =
|
|
|
|
|
"<<" `mappend` keyValues `mappend` ">>"
|
|
|
|
|
where
|
|
|
|
|
keyValues = join " " $ outputKeyVal <$> Map.toList dict
|
|
|
|
|
outputKeyVal (key, val) =
|
|
|
|
|
Output.string (printf "/%s " key) `mappend` output val
|
|
|
|
|
|
2019-05-13 18:22:05 +02:00
|
|
|
|
data StringObj = Literal String | Hexadecimal String deriving Show
|
|
|
|
|
|
2019-05-16 17:04:45 +02:00
|
|
|
|
instance Output StringObj where
|
|
|
|
|
output (Literal s) = Output.string (printf "(%s)" s)
|
|
|
|
|
output (Hexadecimal s) = Output.string (printf "<%s>" s)
|
|
|
|
|
|
2019-05-13 18:22:05 +02:00
|
|
|
|
data DirectObject =
|
|
|
|
|
Boolean Bool
|
|
|
|
|
| Number Float
|
|
|
|
|
| String StringObj
|
|
|
|
|
| Name String
|
|
|
|
|
| Array [DirectObject]
|
|
|
|
|
| Dictionary Dictionary
|
|
|
|
|
| Null
|
2019-05-14 18:42:11 +02:00
|
|
|
|
| Reference IndirectObjCoordinates
|
2019-05-13 18:22:05 +02:00
|
|
|
|
deriving Show
|
|
|
|
|
|
2019-05-16 17:04:45 +02:00
|
|
|
|
instance Output DirectObject where
|
|
|
|
|
output (Boolean b) = output b
|
|
|
|
|
output (Number n) = output n
|
|
|
|
|
output (String s) = output s
|
|
|
|
|
output (Name n) = "/" `mappend` Output.string n
|
|
|
|
|
output (Array a) = join " " a
|
|
|
|
|
output (Dictionary d) = output d
|
|
|
|
|
output (Null) = "null"
|
|
|
|
|
output (Reference (IndirectObjCoordinates {objectId, versionNumber})) =
|
|
|
|
|
Output.string (printf "%d %d R" objectId versionNumber)
|
|
|
|
|
|
2019-05-13 18:22:05 +02:00
|
|
|
|
data Object =
|
|
|
|
|
Direct DirectObject
|
|
|
|
|
| Stream {
|
|
|
|
|
header :: Dictionary
|
|
|
|
|
, streamContent :: ByteString
|
|
|
|
|
}
|
|
|
|
|
deriving Show
|
|
|
|
|
|
2019-05-16 17:04:45 +02:00
|
|
|
|
instance Output Object where
|
|
|
|
|
output (Direct d) = output d
|
|
|
|
|
output (Stream {header, streamContent}) =
|
|
|
|
|
output header
|
|
|
|
|
`nextLine` "stream"
|
|
|
|
|
`nextLine` byteString streamContent
|
|
|
|
|
`mappend` "endstream" `mappend` newLine
|
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
data IndirectObjCoordinates = IndirectObjCoordinates {
|
|
|
|
|
objectId :: Int
|
|
|
|
|
, versionNumber :: Int
|
|
|
|
|
} deriving Show
|
|
|
|
|
|
|
|
|
|
data Occurrence = Comment String | Indirect IndirectObjCoordinates deriving Show
|
2019-05-13 18:22:05 +02:00
|
|
|
|
|
|
|
|
|
data XRefEntry = InUse {
|
|
|
|
|
offset :: Int64
|
|
|
|
|
, generation :: Int
|
|
|
|
|
} | Free {
|
|
|
|
|
nextFree :: Int64
|
|
|
|
|
, generation :: Int
|
|
|
|
|
} deriving Show
|
|
|
|
|
|
2019-05-16 17:04:45 +02:00
|
|
|
|
instance Output XRefEntry where
|
|
|
|
|
output (InUse {offset, generation}) =
|
|
|
|
|
Output.string (printf "%010d %05d n " offset generation) `mappend` newLine
|
|
|
|
|
output (Free {nextFree, generation}) =
|
|
|
|
|
Output.string (printf "%010d %05d f " nextFree generation) `mappend` newLine
|
|
|
|
|
|
2019-05-13 18:22:05 +02:00
|
|
|
|
data XRefSubSection = XRefSubSection {
|
|
|
|
|
firstObjectId :: Int
|
2019-05-14 18:42:11 +02:00
|
|
|
|
, entriesNumber :: Int
|
2019-05-13 18:22:05 +02:00
|
|
|
|
, entries :: Map Int XRefEntry
|
|
|
|
|
} deriving Show
|
|
|
|
|
|
2019-05-16 17:04:45 +02:00
|
|
|
|
instance Output XRefSubSection where
|
|
|
|
|
output (XRefSubSection {firstObjectId, entriesNumber, entries}) =
|
|
|
|
|
Output.string (printf "%d %d" firstObjectId entriesNumber)
|
|
|
|
|
`nextLine` output (Map.elems entries) `mappend` newLine
|
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
type XRefSection = [XRefSubSection]
|
|
|
|
|
|
2019-05-13 18:22:05 +02:00
|
|
|
|
data Content = Content {
|
2019-05-15 19:12:38 +02:00
|
|
|
|
startOffset :: Int64
|
|
|
|
|
, body :: [Occurrence]
|
2019-05-14 18:42:11 +02:00
|
|
|
|
, objects :: Map Int Object
|
|
|
|
|
, xrefSection :: XRefSection
|
2019-05-13 18:22:05 +02:00
|
|
|
|
, trailer :: Dictionary
|
|
|
|
|
, startXrefPosition :: Int64
|
|
|
|
|
} deriving Show
|
|
|
|
|
|
2019-05-16 17:04:45 +02:00
|
|
|
|
instance Output Content where
|
|
|
|
|
output (Content {xrefSection, startXrefPosition}) =
|
|
|
|
|
output xrefSection
|
|
|
|
|
`mappend` "trailer"
|
|
|
|
|
`nextLine` "startxref"
|
|
|
|
|
`nextLine` Output.string (printf "%d" startXrefPosition)
|
|
|
|
|
`nextLine` byteString eofMarker
|
2019-05-13 18:22:05 +02:00
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
line :: String -> Parser u ()
|
2019-05-16 17:04:45 +02:00
|
|
|
|
line l = string l *> EOL.parser *> return ()
|
|
|
|
|
|
|
|
|
|
magicNumber :: String
|
|
|
|
|
magicNumber = "%PDF-"
|
|
|
|
|
|
|
|
|
|
eofMarker :: ByteString
|
|
|
|
|
eofMarker = "%%EOF"
|
2019-05-13 18:22:05 +02:00
|
|
|
|
|
|
|
|
|
whiteSpaceCharset :: String
|
|
|
|
|
whiteSpaceCharset = "\0\t\12 "
|
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
whiteSpace :: Parser u ()
|
2019-05-16 17:04:45 +02:00
|
|
|
|
whiteSpace = oneOf whiteSpaceCharset *> return () <|> EOL.parser *> return ()
|
2019-05-13 18:22:05 +02:00
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
blank :: Parser u ()
|
2019-05-13 18:22:05 +02:00
|
|
|
|
blank = skipMany whiteSpace
|
|
|
|
|
|
|
|
|
|
delimiterCharset :: String
|
|
|
|
|
delimiterCharset = "()<>[]{}/%"
|
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
regular :: Parser u Char
|
2019-05-16 17:04:45 +02:00
|
|
|
|
regular = noneOf $ EOL.charset ++ whiteSpaceCharset ++ delimiterCharset
|
2019-05-13 18:22:05 +02:00
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
integer :: (Read a, Num a) => Parser u a
|
2019-05-13 18:22:05 +02:00
|
|
|
|
integer = read <$> many1 digit <* whiteSpace
|
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
directObject :: Parser u DirectObject
|
2019-05-13 18:22:05 +02:00
|
|
|
|
directObject =
|
|
|
|
|
Boolean <$> try boolean
|
|
|
|
|
<|> Reference <$> try reference {- defined before Number because Number is a prefix of it -}
|
|
|
|
|
<|> Number <$> try number
|
|
|
|
|
<|> String <$> try stringObj
|
|
|
|
|
<|> Name <$> try name
|
|
|
|
|
<|> Array <$> try array
|
|
|
|
|
<|> Dictionary <$> try dictionary
|
|
|
|
|
<|> const Null <$> try nullObject
|
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
boolean :: Parser u Bool
|
2019-05-13 18:22:05 +02:00
|
|
|
|
boolean = (string "true" *> return True) <|> (string "false" *> return False)
|
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
number :: Parser u Float
|
2019-05-13 18:22:05 +02:00
|
|
|
|
number = read <$> (mappend <$> sign <*> (integerPart <|> ('0':) <$> floatPart))
|
|
|
|
|
where
|
|
|
|
|
sign = string "-" <|> option "" (char '+' >> return "")
|
|
|
|
|
integerPart = mappend <$> many1 digit <*> option "" floatPart
|
|
|
|
|
floatPart = (:) <$> char '.' <*> (option "0" $ many1 digit)
|
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
stringObj :: Parser u StringObj
|
2019-05-13 18:22:05 +02:00
|
|
|
|
stringObj =
|
|
|
|
|
Literal <$> (char '(' *> (concat <$> many literalStringBlock) <* char ')')
|
|
|
|
|
<|> Hexadecimal <$> (char '<' *> many hexDigit <* char '>')
|
|
|
|
|
where
|
2019-05-15 15:03:55 +02:00
|
|
|
|
literalStringBlock = many1 (noneOf "\\()") <|> matchingParenthesis <|> escapedChar
|
2019-05-13 18:22:05 +02:00
|
|
|
|
matchingParenthesis =
|
|
|
|
|
(++) <$> ((:) <$> char '(' <*> literalStringBlock) <*> string ")"
|
|
|
|
|
escapedChar = (:) <$> char '\\' <*> ((:[]) <$> oneOf "nrtbf()\\" <|> octalCode)
|
|
|
|
|
octalCode = choice $ (\n -> count n octDigit) <$> [1..3]
|
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
name :: Parser u String
|
2019-05-13 18:22:05 +02:00
|
|
|
|
name = char '/' *> many regular
|
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
array :: Parser u [DirectObject]
|
2019-05-13 18:22:05 +02:00
|
|
|
|
array = char '[' *> blank *> directObject `endBy` blank <* char ']'
|
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
dictionary :: Parser u Dictionary
|
2019-05-13 18:22:05 +02:00
|
|
|
|
dictionary =
|
|
|
|
|
try (string "<<" *> blank *> keyValPairs <* string ">>")
|
|
|
|
|
where
|
|
|
|
|
keyVal = (,) <$> name <* blank <*> directObject
|
|
|
|
|
keyValPairs = Map.fromList <$> keyVal `endBy` blank
|
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
nullObject :: Parser u ()
|
2019-05-13 18:22:05 +02:00
|
|
|
|
nullObject = string "null" *> return ()
|
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
reference :: Parser u IndirectObjCoordinates
|
|
|
|
|
reference = IndirectObjCoordinates <$> integer <*> integer <* char 'R'
|
2019-05-13 18:22:05 +02:00
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
entry :: Parser u XRefEntry
|
2019-05-13 18:22:05 +02:00
|
|
|
|
entry = do
|
|
|
|
|
(big, small) <- (,) <$> integer <*> integer
|
|
|
|
|
(inUse big small <|> free big small) <* blank
|
|
|
|
|
where
|
2019-05-14 18:42:11 +02:00
|
|
|
|
inUse :: Int64 -> Int -> Parser u XRefEntry
|
2019-05-13 18:22:05 +02:00
|
|
|
|
inUse offset generation = char 'n' *> return (InUse {offset, generation})
|
2019-05-14 18:42:11 +02:00
|
|
|
|
free :: Int64 -> Int -> Parser u XRefEntry
|
2019-05-13 18:22:05 +02:00
|
|
|
|
free nextFree generation = char 'f' *> return (Free {nextFree, generation})
|
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
xrefSubSection :: Parser u XRefSubSection
|
2019-05-13 18:22:05 +02:00
|
|
|
|
xrefSubSection = do
|
|
|
|
|
(firstObjectId, entriesNumber) <- (,) <$> integer <*> integer
|
|
|
|
|
entries <- Map.fromList . zip [firstObjectId..] <$> count entriesNumber entry
|
2019-05-14 18:42:11 +02:00
|
|
|
|
return $ XRefSubSection {firstObjectId, entriesNumber, entries}
|
2019-05-13 18:22:05 +02:00
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
|
content :: Parser u Content
|
2019-05-13 18:22:05 +02:00
|
|
|
|
content =
|
2019-05-16 11:01:50 +02:00
|
|
|
|
Content 0 [] Map.empty
|
2019-05-16 17:04:45 +02:00
|
|
|
|
<$> (line "xref" *> xrefSubSection `sepBy` EOL.parser)
|
|
|
|
|
<*> (line "trailer" *> dictionary <* EOL.parser)
|
2019-05-13 18:22:05 +02:00
|
|
|
|
<*> (line "startxref" *> integer)
|