From 266032950e10434f6105e46d9dba7c777e56e30e Mon Sep 17 00:00:00 2001 From: Tissevert Date: Thu, 27 Feb 2020 17:24:23 +0100 Subject: [PATCH] Use the new Box instance between Object and Bytestring that generalizes the code that had been written specificly for this demo tool, and get rid of zlib (directly in Hufflepdf now) and Lazy Bytestrings --- reveal.cabal | 1 - src/Main.hs | 50 +++++++++++++++------------------------- src/PDF/TextRendering.hs | 4 ++-- 3 files changed, 20 insertions(+), 35 deletions(-) diff --git a/reveal.cabal b/reveal.cabal index 9286822..9ae0354 100644 --- a/reveal.cabal +++ b/reveal.cabal @@ -26,7 +26,6 @@ executable reveal , containers , mtl , Hufflepdf >= 0.2.0 - , zlib hs-source-dirs: src ghc-options: -Wall default-language: Haskell2010 diff --git a/src/Main.hs b/src/Main.hs index 33ae567..d3f248d 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,60 +1,46 @@ {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeApplications #-} module Main where -import Codec.Compression.Zlib (compress, decompress) import Control.Monad.State (execState, execStateT) import Data.ByteString.Char8 (ByteString) -import qualified Data.ByteString.Char8 as BS ( - length, readFile - ) -import qualified Data.ByteString.Lazy.Char8 as Lazy ( - fromStrict, intercalate, split, toStrict, writeFile - ) -import qualified Data.Map as Map (insert, lookup) +import qualified Data.ByteString.Char8 as BS (readFile, split, intercalate) +import qualified Data.ByteString.Lazy.Char8 as Lazy (writeFile) import PDF (Layers(..), Document(..), UnifiedLayers(..), parseDocument, render) -import PDF.Box (I(..), at, atAll, edit) -import PDF.Layer (Objects(..)) -import PDF.Object (DirectObject(..), Object(..), Name(..), Number(..)) +import PDF.Box (Index(..), Maybe_(..), at, atAll, edit) +import PDF.Layer (Layer, Objects(..)) +import PDF.Object.Navigation (StreamContent(..)) import PDF.TextRendering (TextRendering(..), update) import Prelude hiding (lines, unlines) import System.Environment (getArgs) import System.IO (hPutStrLn, stderr) -revealFlateEncodedText :: ByteString -> ByteString -revealFlateEncodedText = - Lazy.toStrict . compress . revealText . decompress . Lazy.fromStrict +revealText :: ByteString -> ByteString +revealText = unlines . fmap fill . lines where - lines = Lazy.split '\n' - unlines = Lazy.intercalate "\n" - revealText = unlines . fmap fill . lines + lines = BS.split '\n' fill = update $ const Fill + unlines = BS.intercalate "\n" -revealObject :: Object -> Object -revealObject obj@(Stream {header, streamContent}) = - case Map.lookup (Name "Filter") header of - Just (NameObject (Name "FlateDecode")) -> - let newStreamContent = revealFlateEncodedText streamContent in - let newLength = Number . fromIntegral $ BS.length newStreamContent in - Stream { - header = Map.insert (Name "Length") (NumberObject newLength) header - , streamContent = newStreamContent - } - _ -> obj -revealObject obj = obj +revealLayer :: Monad m => Layer -> m Layer +revealLayer = + atAll Objects + .atAll (Maybe_ StreamContent) $ + return . revealText reveal :: Document -> Document reveal = execState $ - edit .at UnifiedLayers .atAll Objects $ return . revealObject + edit .at UnifiedLayers $ revealLayer revealFirst :: Document -> Document revealFirst document = maybe document id $ execStateT ( - edit .at Layers .at(I 0) .atAll Objects $ return . revealObject + edit .at Layers .at(Index 0) $ revealLayer ) document deepReveal :: Document -> Document deepReveal = execState $ - edit .atAll Layers .atAll Objects $ return . revealObject + edit .atAll Layers $ revealLayer main :: IO () main = do diff --git a/src/PDF/TextRendering.hs b/src/PDF/TextRendering.hs index 2b81c6a..692dc0a 100644 --- a/src/PDF/TextRendering.hs +++ b/src/PDF/TextRendering.hs @@ -4,8 +4,8 @@ module PDF.TextRendering ( , update ) where -import Data.ByteString.Lazy.Char8 (ByteString) -import qualified Data.ByteString.Lazy.Char8 as BS (intercalate, pack, readInt, split) +import Data.ByteString.Char8 (ByteString) +import qualified Data.ByteString.Char8 as BS (intercalate, pack, readInt, split) import Prelude hiding (unwords, words) data TextRendering =