From b6c1f670ef5eeae5185432fa3a0612b2d1c00504 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Wed, 11 Mar 2020 10:47:52 +0100 Subject: [PATCH] Generalize the search for FlateDecode (there can be several filters in an array) --- src/PDF/Object/Navigation.hs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/PDF/Object/Navigation.hs b/src/PDF/Object/Navigation.hs index be967c5..4f2bb22 100644 --- a/src/PDF/Object/Navigation.hs +++ b/src/PDF/Object/Navigation.hs @@ -61,7 +61,7 @@ getKey :: PDFContent m => String -> Object -> m DirectObject getKey key object = getDictionary object >>= catchMaybe . Map.lookup (Name key) where errorMessage = - printf "Key %s not found in dictionary %s" key (show object) + printf "Key %s not found in object %s" key (show object) catchMaybe = maybe (fail errorMessage) return objectById :: PDFContent m => ObjectId -> m Object @@ -96,17 +96,27 @@ data Raw = Raw onLazy :: (Lazy.ByteString -> Lazy.ByteString) -> ByteString -> ByteString onLazy f = Lazy.toStrict . f . Lazy.fromStrict +contains :: String -> DirectObject -> Bool +contains needle (NameObject (Name n)) = needle == n +contains needle (Array directObjects) = oneOf directObjects (contains needle) + where + oneOf [] _ = False + oneOf (x:xs) p = p x || oneOf xs p +contains _ _ = False + instance MonadFail m => Box m Clear Object ByteString where r Clear (Stream {header, streamContent}) = return $ case Map.lookup (Name "Filter") header of - Just (NameObject (Name "FlateDecode")) -> onLazy decompress streamContent + Just directObject + | contains "FlateDecode" directObject -> onLazy decompress streamContent _ -> streamContent r _ obj = expected "stream" obj w Clear streamContent obj@(Stream {header}) = return $ case Map.lookup (Name "Filter") header of - Just (NameObject (Name "FlateDecode")) -> - obj {streamContent = onLazy compress streamContent} + Just directObject + | contains "FlateDecode" directObject -> + obj {streamContent = onLazy compress streamContent} _ -> obj {streamContent} w _ _ obj = expected "stream" obj