pdf-toolbox-document 0.1.2 → 0.1.3
raw patch · 4 files changed
+39/−16 lines, 4 filesdep ~base
Dependency ranges changed: base
Files
- changelog.md +4/−1
- lib/Pdf/Document/FontDict.hs +21/−13
- pdf-toolbox-document.cabal +2/−2
- test/test.hs +12/−0
changelog.md view
@@ -1,4 +1,7 @@-unreleased+0.1.3++* support ghc from 8.6 to 9.6 and drop older versions+* fix handling of indirect objects in Font Descriptor (#76) 0.1.2
lib/Pdf/Document/FontDict.hs view
@@ -285,32 +285,40 @@ , fdCharSet = charSet } where- required = requiredInDict "FontDescriptor"- optional = optionalInDict "FontDescriptor"+ required = requiredInDict pdf "FontDescriptor"+ optional = optionalInDict pdf "FontDescriptor" nameValue' = fmap Name.toByteString . nameValue -- | Parse a value from a required field of a dictionary. This will -- raise an exception if a) the field is not present or b) the field -- value has a false type.-requiredInDict :: String -- ^ a context for a failure notice+requiredInDict :: Pdf -- ^ in case the field is a reference+ -> String -- ^ a context for a failure notice -> Name -- ^ name of dictionary field -> (Object -> Maybe a) -- ^ function for type-casting the object -> Dict -- ^ the dictionary -> IO a-requiredInDict context key typeFun =- join .- sure . (`notice` (context ++ ": " ++ msg ++ " should exist")) .- fmap (sure . (`notice` (context ++ ": " ++ msg ++ " type failure")) . typeFun) .- HashMap.lookup key+requiredInDict pdf context key typeFun dict = do+ case HashMap.lookup key dict of+ Nothing -> throwIO $ Corrupted (context ++ ": " ++ msg ++ " should exist") []+ Just oIn -> do+ o <- deref pdf oIn+ case typeFun o of+ Nothing -> throwIO $ Corrupted (context ++ ": " ++ msg ++ " type failure") []+ Just v -> return v where msg = Text.unpack $ decodeUtf8With ignore $ Name.toByteString key -- | Parse a value from an optional field of a dictionary. This will -- raise an exception if the field value has a false type.-optionalInDict :: String -> Name -> (Object -> Maybe a) -> Dict -> IO (Maybe a)-optionalInDict context key typeFun =- maybe (return Nothing) (liftM Just) .- fmap (sure . (`notice` (context ++ ": " ++ msg ++ " type failure")) . typeFun) .- HashMap.lookup key+optionalInDict :: Pdf -> String -> Name -> (Object -> Maybe a) -> Dict -> IO (Maybe a)+optionalInDict pdf context key typeFun dict =+ case HashMap.lookup key dict of+ Nothing -> return Nothing+ Just oIn -> do+ o <- deref pdf oIn+ case typeFun o of+ Nothing -> throwIO $ Corrupted (context ++ ": " ++ msg ++ " type failure") []+ Just v -> return $ Just v where msg = Text.unpack $ decodeUtf8With ignore $ Name.toByteString key
pdf-toolbox-document.cabal view
@@ -1,5 +1,5 @@ name: pdf-toolbox-document-version: 0.1.2+version: 0.1.3 synopsis: A collection of tools for processing PDF files. license: BSD3 license-file: LICENSE@@ -36,7 +36,7 @@ Pdf.Document.Internal.Types Pdf.Document.Internal.Util other-modules: Prelude- build-depends: base >= 4.5 && < 5,+ build-depends: base >= 4.9 && < 5, containers, text, bytestring,
test/test.hs view
@@ -50,6 +50,18 @@ txt `shouldBe` Just "\nHello World!!!\nXObject is here\nnested XObject is here" + describe "FontDescription with indirect fields" $ do+ it "should have correct text" $ do+ withPdfFile "test/files/indirect_font_desc_fields.pdf" $ \pdf -> do+ doc <- document pdf+ catalog <- documentCatalog doc+ root <- catalogPageNode catalog+ page <- pageNodePageByNum root 0+ -- here timeout breaks infinite loop in case of a bug+ txt <- timeout 5000000 $ pageExtractText page+ txt `shouldBe` Just+ "\nHello World!!!\nXObject is here\nnested XObject is here"+ -- | Generate simple PDF file for tests withSimpleFile :: (Handle -> IO ()) -> IO () withSimpleFile action = do