47 lines
1.5 KiB
Haskell
47 lines
1.5 KiB
Haskell
{-# LANGUAGE NamedFieldPuns #-}
|
|
module PDF.Update (
|
|
unify
|
|
) where
|
|
|
|
import Data.Map (member)
|
|
import qualified Data.Map as Map (empty, union)
|
|
import PDF.Object (
|
|
Content(..), IndexedObjects, IndirectObjCoordinates(..), Occurrence(..)
|
|
, Structure(..)
|
|
)
|
|
|
|
emptyContent :: Content
|
|
emptyContent = Content {
|
|
docStructure = Structure {xRef = Map.empty, trailer = Map.empty}
|
|
, objects = Map.empty
|
|
, occurrences = []
|
|
}
|
|
|
|
unify :: [Content] -> Content
|
|
unify = foldl complete emptyContent
|
|
where
|
|
complete tmpContent older =
|
|
let mergedObjects = Map.union (objects tmpContent) (objects older) in
|
|
Content {
|
|
docStructure =
|
|
unifyDocStructure (docStructure tmpContent) (docStructure older)
|
|
, objects = mergedObjects
|
|
, occurrences =
|
|
unifyOccurrences mergedObjects (occurrences tmpContent) (occurrences older)
|
|
}
|
|
|
|
unifyDocStructure :: Structure -> Structure -> Structure
|
|
unifyDocStructure update original = Structure {
|
|
xRef = Map.union (xRef update) (xRef original)
|
|
, trailer = Map.union (trailer update) (trailer original)
|
|
}
|
|
|
|
unifyOccurrences :: IndexedObjects -> [Occurrence] -> [Occurrence] -> [Occurrence]
|
|
unifyOccurrences objects update = foldr addOlder update
|
|
where
|
|
addOlder occurrence@(Comment _) existing = occurrence : existing
|
|
addOlder occurrence@(Indirect indirect) existing =
|
|
if objectId indirect `member` objects
|
|
then occurrence : existing
|
|
else existing
|
|
|