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-17 16:14:06 +02:00
|
|
|
, Flow(..)
|
2019-05-14 18:42:11 +02:00
|
|
|
, IndirectObjCoordinates(..)
|
2019-05-18 09:01:13 +02:00
|
|
|
, InputStructure(..)
|
2019-05-17 16:14:06 +02:00
|
|
|
, Name(..)
|
|
|
|
, Number(..)
|
2019-05-13 18:22:05 +02:00
|
|
|
, Object(..)
|
|
|
|
, Occurrence(..)
|
2019-05-17 16:14:06 +02:00
|
|
|
, Structure(..)
|
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-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-17 16:14:06 +02:00
|
|
|
, structure
|
2019-05-13 18:22:05 +02:00
|
|
|
) where
|
|
|
|
|
2019-05-24 10:48:09 +02:00
|
|
|
import Control.Applicative ((<|>))
|
|
|
|
import Data.ByteString.Char8 (ByteString)
|
|
|
|
import qualified Data.ByteString.Char8 as BS (
|
|
|
|
concat, cons, pack, singleton, unpack
|
|
|
|
)
|
2019-05-18 09:01:13 +02:00
|
|
|
import Data.Map (Map, (!), mapWithKey)
|
2019-05-17 16:14:06 +02:00
|
|
|
import qualified Data.Map as Map (elems, fromList, toList)
|
2019-05-16 17:04:45 +02:00
|
|
|
import qualified PDF.EOL as EOL (charset, parser)
|
|
|
|
import qualified PDF.Output as Output (string)
|
2019-05-17 16:14:06 +02:00
|
|
|
import PDF.Output (
|
|
|
|
OBuilder, Offset(..), Output(..)
|
|
|
|
, byteString, getOffsets, join, newLine, nextLine, saveOffset
|
|
|
|
)
|
2019-05-24 10:48:09 +02:00
|
|
|
import PDF.Parser (
|
|
|
|
Parser, (<?>)
|
|
|
|
, char, choice, count, decNumber, hexNumber, many, octDigit, oneOf, option
|
|
|
|
, sepBy, string, takeAll, takeAll1
|
|
|
|
)
|
2019-05-16 17:04:45 +02:00
|
|
|
import Text.Printf (printf)
|
2019-05-14 18:42:11 +02:00
|
|
|
|
2019-05-17 16:14:06 +02:00
|
|
|
line :: String -> Parser u ()
|
2019-05-31 15:06:20 +02:00
|
|
|
line l = (string (BS.pack l) *> EOL.parser *> return ()) <?> printf "line «%s»" l
|
2019-05-17 16:14:06 +02:00
|
|
|
|
2019-05-24 10:48:09 +02:00
|
|
|
magicNumber :: ByteString
|
2019-05-17 16:14:06 +02:00
|
|
|
magicNumber = "%PDF-"
|
|
|
|
|
|
|
|
eofMarker :: ByteString
|
|
|
|
eofMarker = "%%EOF"
|
|
|
|
|
|
|
|
whiteSpaceCharset :: String
|
|
|
|
whiteSpaceCharset = "\0\t\12 "
|
|
|
|
|
|
|
|
blank :: Parser u ()
|
2019-05-24 10:48:09 +02:00
|
|
|
blank = takeAll (`elem` (EOL.charset ++ whiteSpaceCharset)) *> return ()
|
2019-05-17 16:14:06 +02:00
|
|
|
|
|
|
|
delimiterCharset :: String
|
|
|
|
delimiterCharset = "()<>[]{}/%"
|
|
|
|
|
2019-05-24 10:48:09 +02:00
|
|
|
regular :: Char -> Bool
|
|
|
|
regular = not . (`elem` (EOL.charset ++ whiteSpaceCharset ++ delimiterCharset))
|
2019-05-17 16:14:06 +02:00
|
|
|
|
|
|
|
integer :: (Read a, Num a) => Parser u a
|
2019-05-24 10:48:09 +02:00
|
|
|
integer = read . BS.unpack <$> decNumber <* blank <?> "decimal integer"
|
2019-05-13 18:22:05 +02:00
|
|
|
|
2019-05-17 16:14:06 +02:00
|
|
|
-------------------------------------
|
|
|
|
-- OBJECTS
|
|
|
|
-------------------------------------
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Boolean
|
|
|
|
--
|
|
|
|
boolean :: Parser u Bool
|
2019-05-24 10:48:09 +02:00
|
|
|
boolean =
|
|
|
|
(string "true" *> return True) <|> (string "false" *> return False) <?> "boolean"
|
2019-05-17 16:14:06 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Number
|
|
|
|
--
|
|
|
|
newtype Number = Number Float deriving Show
|
|
|
|
|
|
|
|
instance Output Number where
|
|
|
|
output (Number f) = Output.string $
|
|
|
|
case properFraction f of
|
|
|
|
(n, 0) -> printf "%d" (n :: Int)
|
|
|
|
_ -> printf "%f" f
|
|
|
|
|
|
|
|
number :: Parser u Number
|
2019-05-24 10:48:09 +02:00
|
|
|
number = Number . read . BS.unpack <$>
|
|
|
|
(mappend <$> sign <*> (integerPart <|> BS.cons '0' <$> floatPart))
|
|
|
|
<?> "number"
|
2019-05-17 16:14:06 +02:00
|
|
|
where
|
|
|
|
sign = string "-" <|> option "" (char '+' >> return "")
|
2019-05-24 10:48:09 +02:00
|
|
|
integerPart = mappend <$> decNumber <*> option "" floatPart
|
|
|
|
floatPart = BS.cons <$> char '.' <*> (option "0" $ decNumber)
|
2019-05-17 16:14:06 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- StringObject
|
|
|
|
--
|
|
|
|
data StringObject = Literal String | Hexadecimal String deriving Show
|
|
|
|
|
|
|
|
instance Output StringObject where
|
|
|
|
output (Literal s) = Output.string (printf "(%s)" s)
|
|
|
|
output (Hexadecimal s) = Output.string (printf "<%s>" s)
|
|
|
|
|
|
|
|
stringObject :: Parser u StringObject
|
|
|
|
stringObject =
|
2019-05-24 10:48:09 +02:00
|
|
|
Literal . BS.unpack <$> (char '(' *> (BS.concat <$> literalString) <* char ')')
|
|
|
|
<|> Hexadecimal . BS.unpack <$> (char '<' *> hexNumber <* char '>')
|
|
|
|
<?> "string object (literal or hexadecimal)"
|
2019-05-17 16:14:06 +02:00
|
|
|
where
|
2019-05-24 10:48:09 +02:00
|
|
|
literalString = many literalStringBlock
|
|
|
|
literalStringBlock = takeAll1 normalChar <|> matchingParenthesis <|> escapedChar
|
|
|
|
normalChar = not . (`elem` ("\\()" :: String))
|
2019-05-17 16:14:06 +02:00
|
|
|
matchingParenthesis =
|
2019-05-24 10:48:09 +02:00
|
|
|
mappend <$> (BS.cons <$> char '(' <*> literalStringBlock) <*> string ")"
|
|
|
|
escapedChar =
|
|
|
|
BS.cons <$> char '\\' <*> (BS.singleton <$> oneOf "nrtbf()\\" <|> octalCode)
|
|
|
|
octalCode = choice $ (\n -> BS.pack <$> count n octDigit) <$> [1..3]
|
2019-05-17 16:14:06 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Name
|
|
|
|
--
|
|
|
|
newtype Name = Name String deriving (Eq, Ord, Show)
|
|
|
|
|
|
|
|
instance Output Name where
|
|
|
|
output (Name n) = "/" `mappend` Output.string n
|
|
|
|
|
|
|
|
name :: Parser u Name
|
2019-05-24 10:48:09 +02:00
|
|
|
name = Name . BS.unpack <$> (char '/' *> takeAll regular) <?> "name"
|
2019-05-17 16:14:06 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Array
|
|
|
|
--
|
|
|
|
array :: Parser u [DirectObject]
|
2019-05-24 10:48:09 +02:00
|
|
|
array =
|
|
|
|
char '[' *> blank *> directObject `sepBy` blank <* blank <* char ']' <?> "array"
|
2019-05-17 16:14:06 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Dictionary
|
|
|
|
--
|
|
|
|
type Dictionary = Map Name DirectObject
|
|
|
|
|
|
|
|
instance Output Dictionary where
|
2019-05-16 17:04:45 +02:00
|
|
|
output dict =
|
|
|
|
"<<" `mappend` keyValues `mappend` ">>"
|
|
|
|
where
|
|
|
|
keyValues = join " " $ outputKeyVal <$> Map.toList dict
|
2019-05-17 16:14:06 +02:00
|
|
|
outputKeyVal :: (Name, DirectObject) -> OBuilder
|
|
|
|
outputKeyVal (key, val) = output key `mappend` " " `mappend` output val
|
2019-05-16 17:04:45 +02:00
|
|
|
|
2019-05-17 16:14:06 +02:00
|
|
|
dictionary :: Parser u Dictionary
|
|
|
|
dictionary =
|
2019-05-24 10:48:09 +02:00
|
|
|
string "<<" *> blank *> keyValPairs <* string ">>" <?> "dictionary"
|
2019-05-17 16:14:06 +02:00
|
|
|
where
|
|
|
|
keyVal = (,) <$> name <* blank <*> directObject
|
2019-05-24 10:48:09 +02:00
|
|
|
keyValPairs = Map.fromList <$> keyVal `sepBy` blank <* blank
|
2019-05-13 18:22:05 +02:00
|
|
|
|
2019-05-17 16:14:06 +02:00
|
|
|
--
|
|
|
|
-- Null
|
|
|
|
--
|
|
|
|
nullObject :: Parser u ()
|
2019-05-24 10:48:09 +02:00
|
|
|
nullObject = string "null" *> return () <?> "null object"
|
2019-05-16 17:04:45 +02:00
|
|
|
|
2019-05-17 16:14:06 +02:00
|
|
|
--
|
|
|
|
-- Reference
|
|
|
|
--
|
|
|
|
data IndirectObjCoordinates = IndirectObjCoordinates {
|
|
|
|
objectId :: Int
|
|
|
|
, versionNumber :: Int
|
|
|
|
} deriving Show
|
|
|
|
|
|
|
|
reference :: Parser u IndirectObjCoordinates
|
2019-05-24 10:48:09 +02:00
|
|
|
reference = IndirectObjCoordinates <$> integer <*> integer <* char 'R' <?> "reference to an object"
|
2019-05-17 16:14:06 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- DirectObject
|
|
|
|
--
|
2019-05-13 18:22:05 +02:00
|
|
|
data DirectObject =
|
|
|
|
Boolean Bool
|
2019-05-17 16:14:06 +02:00
|
|
|
| NumberObject Number
|
|
|
|
| StringObject StringObject
|
|
|
|
| NameObject Name
|
2019-05-13 18:22:05 +02:00
|
|
|
| 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
|
2019-05-17 16:14:06 +02:00
|
|
|
output (NumberObject n) = output n
|
|
|
|
output (StringObject s) = output s
|
|
|
|
output (NameObject n) = output n
|
2019-05-16 22:41:14 +02:00
|
|
|
output (Array a) = "[" `mappend` join " " a `mappend` "]"
|
2019-05-16 17:04:45 +02:00
|
|
|
output (Dictionary d) = output d
|
|
|
|
output (Null) = "null"
|
|
|
|
output (Reference (IndirectObjCoordinates {objectId, versionNumber})) =
|
|
|
|
Output.string (printf "%d %d R" objectId versionNumber)
|
|
|
|
|
2019-05-17 16:14:06 +02:00
|
|
|
directObject :: Parser u DirectObject
|
|
|
|
directObject =
|
2019-05-24 10:48:09 +02:00
|
|
|
Boolean <$> boolean
|
|
|
|
<|> Reference <$> reference {- defined before Number because Number is a prefix of it -}
|
|
|
|
<|> NumberObject <$> number
|
|
|
|
<|> StringObject <$> stringObject
|
|
|
|
<|> NameObject <$> name
|
|
|
|
<|> Array <$> array
|
|
|
|
<|> Dictionary <$> dictionary
|
|
|
|
<|> const Null <$> nullObject
|
|
|
|
<?> "direct object"
|
2019-05-17 16:14:06 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Object
|
|
|
|
--
|
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
|
2019-05-16 22:41:14 +02:00
|
|
|
`mappend` "endstream"
|
2019-05-16 17:04:45 +02:00
|
|
|
|
2019-05-17 16:14:06 +02:00
|
|
|
--
|
|
|
|
-- Occurrence
|
|
|
|
--
|
2019-05-14 18:42:11 +02:00
|
|
|
data Occurrence = Comment String | Indirect IndirectObjCoordinates deriving Show
|
2019-05-13 18:22:05 +02:00
|
|
|
|
2019-05-16 22:41:14 +02:00
|
|
|
outputOccurrence :: Map Int Object -> Occurrence -> OBuilder
|
|
|
|
outputOccurrence _ (Comment c) =
|
2019-05-31 15:07:41 +02:00
|
|
|
Output.string c `mappend` newLine
|
2019-05-16 22:41:14 +02:00
|
|
|
outputOccurrence objects (Indirect (IndirectObjCoordinates {objectId, versionNumber})) =
|
2019-05-17 16:14:06 +02:00
|
|
|
saveOffset (ObjectId objectId)
|
|
|
|
>> Output.string (printf "%d %d obj" objectId versionNumber)
|
2019-05-16 22:41:14 +02:00
|
|
|
`nextLine` output (objects ! objectId)
|
|
|
|
`nextLine` "endobj" `mappend` newLine
|
|
|
|
|
2019-05-17 16:14:06 +02:00
|
|
|
-------------------------------------
|
|
|
|
-- XREF TABLE
|
|
|
|
-------------------------------------
|
|
|
|
|
|
|
|
--
|
|
|
|
-- XRefEntry
|
|
|
|
--
|
2019-05-13 18:22:05 +02:00
|
|
|
data XRefEntry = InUse {
|
2019-05-24 10:48:09 +02:00
|
|
|
offset :: Int
|
2019-05-13 18:22:05 +02:00
|
|
|
, generation :: Int
|
|
|
|
} | Free {
|
2019-05-24 10:48:09 +02:00
|
|
|
nextFree :: Int
|
2019-05-13 18:22:05 +02:00
|
|
|
, 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-17 16:14:06 +02:00
|
|
|
entry :: Parser u XRefEntry
|
|
|
|
entry = do
|
|
|
|
(big, small) <- (,) <$> integer <*> integer
|
2019-05-24 10:48:09 +02:00
|
|
|
(inUse big small <|> free big small <?> "XRef entry") <* blank
|
2019-05-17 16:14:06 +02:00
|
|
|
where
|
2019-05-24 10:48:09 +02:00
|
|
|
inUse :: Int -> Int -> Parser u XRefEntry
|
2019-05-17 16:14:06 +02:00
|
|
|
inUse offset generation = char 'n' *> return (InUse {offset, generation})
|
2019-05-24 10:48:09 +02:00
|
|
|
free :: Int -> Int -> Parser u XRefEntry
|
2019-05-17 16:14:06 +02:00
|
|
|
free nextFree generation = char 'f' *> return (Free {nextFree, generation})
|
|
|
|
|
|
|
|
--
|
|
|
|
-- XRefSubSection
|
|
|
|
--
|
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)
|
2019-05-16 22:41:14 +02:00
|
|
|
`nextLine` output (Map.elems entries)
|
2019-05-16 17:04:45 +02:00
|
|
|
|
2019-05-17 16:14:06 +02:00
|
|
|
xrefSubSection :: Parser u XRefSubSection
|
|
|
|
xrefSubSection = do
|
2019-05-24 10:48:09 +02:00
|
|
|
(firstObjectId, entriesNumber) <- (,) <$> integer <*> integer <?> "XRef subsection"
|
2019-05-17 16:14:06 +02:00
|
|
|
entries <- Map.fromList . zip [firstObjectId..] <$> count entriesNumber entry
|
|
|
|
return $ XRefSubSection {firstObjectId, entriesNumber, entries}
|
|
|
|
|
2019-05-14 18:42:11 +02:00
|
|
|
type XRefSection = [XRefSubSection]
|
|
|
|
|
2019-05-17 16:14:06 +02:00
|
|
|
--
|
|
|
|
-- Structure
|
|
|
|
--
|
2019-05-18 09:01:13 +02:00
|
|
|
data InputStructure = InputStructure {
|
2019-05-24 10:48:09 +02:00
|
|
|
startOffset :: Int
|
2019-05-18 09:01:13 +02:00
|
|
|
, inputStructure :: Structure
|
|
|
|
}
|
|
|
|
|
|
|
|
data Structure = Structure {
|
|
|
|
xrefSection :: XRefSection
|
|
|
|
, trailer :: Dictionary
|
2019-05-13 18:22:05 +02:00
|
|
|
} deriving Show
|
|
|
|
|
2019-05-17 16:14:06 +02:00
|
|
|
structure :: Parser u Structure
|
|
|
|
structure =
|
2019-05-18 09:01:13 +02:00
|
|
|
Structure
|
2019-05-17 16:14:06 +02:00
|
|
|
<$> (line "xref" *> xrefSubSection `sepBy` EOL.parser)
|
|
|
|
<*> (line "trailer" *> dictionary <* EOL.parser)
|
|
|
|
|
2019-05-24 10:48:09 +02:00
|
|
|
updateXrefs :: XRefSection -> Map Offset Int -> (XRefSection, Int)
|
2019-05-18 09:01:13 +02:00
|
|
|
updateXrefs xrefSection offsets = (
|
|
|
|
updateSubSection <$> xrefSection
|
2019-05-17 16:14:06 +02:00
|
|
|
, offsets ! StartXRef
|
|
|
|
)
|
2019-05-18 09:01:13 +02:00
|
|
|
where
|
|
|
|
updateSubSection subSection@(XRefSubSection {firstObjectId, entries}) =
|
|
|
|
subSection {entries = mapWithKey (updateEntry firstObjectId) entries}
|
|
|
|
updateEntry firstObjectId index e@(InUse {}) =
|
|
|
|
e {offset = offsets ! (ObjectId $ firstObjectId + index)}
|
|
|
|
updateEntry _ _ e = e
|
2019-05-17 16:14:06 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Flow
|
|
|
|
--
|
|
|
|
data Flow = Flow {
|
|
|
|
occurrencesStack :: [Occurrence]
|
|
|
|
, tmpObjects :: Map Int Object
|
|
|
|
} deriving Show
|
2019-05-13 18:22:05 +02:00
|
|
|
|
2019-05-17 16:14:06 +02:00
|
|
|
--
|
|
|
|
-- Content
|
|
|
|
--
|
|
|
|
data Content = Content {
|
|
|
|
occurrences :: [Occurrence]
|
|
|
|
, objects :: Map Int Object
|
2019-05-18 09:01:13 +02:00
|
|
|
, docStructure :: Structure
|
2019-05-17 16:14:06 +02:00
|
|
|
} deriving Show
|
2019-05-13 18:22:05 +02:00
|
|
|
|
2019-05-17 16:14:06 +02:00
|
|
|
outputBody :: ([Occurrence], Map Int Object) -> OBuilder
|
|
|
|
outputBody (occurrences, objects) =
|
|
|
|
output (outputOccurrence objects <$> occurrences) <* saveOffset StartXRef
|
2019-05-13 18:22:05 +02:00
|
|
|
|
2019-05-17 16:14:06 +02:00
|
|
|
instance Output Content where
|
2019-05-18 09:01:13 +02:00
|
|
|
output (Content {occurrences, objects, docStructure}) =
|
|
|
|
fmap (updateXrefs xrefSection) <$> getOffsets (outputBody (occurrences, objects))
|
2019-05-17 16:14:06 +02:00
|
|
|
>>= \(body, (xref, startXRef)) ->
|
|
|
|
body
|
|
|
|
`mappend` "xref"
|
2019-05-18 09:01:13 +02:00
|
|
|
`nextLine` output xref
|
|
|
|
`mappend` "trailer"
|
2019-05-17 16:14:06 +02:00
|
|
|
`nextLine` output trailer
|
|
|
|
`nextLine` "startxref"
|
|
|
|
`nextLine` (Output.string (printf "%d" startXRef))
|
|
|
|
`nextLine` byteString eofMarker
|
2019-05-18 09:01:13 +02:00
|
|
|
where
|
|
|
|
Structure {xrefSection, trailer} = docStructure
|