2020-03-11 18:55:18 +01:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2020-03-04 18:19:10 +01:00
|
|
|
import Control.Monad ((>=>))
|
2020-03-14 16:57:16 +01:00
|
|
|
import Control.Monad.IO.Class (liftIO)
|
2020-02-08 08:15:32 +01:00
|
|
|
import qualified Data.ByteString.Char8 as BS (readFile)
|
2020-03-15 15:13:00 +01:00
|
|
|
import Data.Id (Id(..), mapWithKey)
|
2020-03-14 16:57:16 +01:00
|
|
|
import qualified Data.Map as Map (toList)
|
2020-03-11 18:55:18 +01:00
|
|
|
import Data.OrderedMap (mapi)
|
2020-03-10 22:57:11 +01:00
|
|
|
import qualified Data.Text as Text (unpack)
|
2020-03-04 18:19:10 +01:00
|
|
|
import PDF (UnifiedLayers(..), parseDocument)
|
|
|
|
import PDF.Box (Box(..))
|
2020-03-10 22:57:11 +01:00
|
|
|
import PDF.Content.Text (Chunks(..))
|
2020-03-04 18:19:10 +01:00
|
|
|
import PDF.Layer (Layer)
|
2020-03-11 18:55:18 +01:00
|
|
|
import PDF.Pages (
|
2020-03-14 16:57:16 +01:00
|
|
|
Contents(..), FontCache, Page(..), PageNumber(..), Pages(..), cacheFonts, withResources
|
2020-03-11 18:55:18 +01:00
|
|
|
)
|
2019-09-23 18:00:47 +02:00
|
|
|
import System.Environment (getArgs)
|
2019-11-29 11:51:35 +01:00
|
|
|
import System.Exit (die)
|
|
|
|
import System.IO (BufferMode(..), hSetBuffering, stdout)
|
2020-03-10 22:57:11 +01:00
|
|
|
import Text.Printf (printf)
|
2019-09-23 18:00:47 +02:00
|
|
|
|
2020-03-17 08:36:02 +01:00
|
|
|
displayPage :: Int -> Page -> FontCache IO ()
|
|
|
|
displayPage n = withResources (
|
2020-03-14 16:57:16 +01:00
|
|
|
r Contents
|
|
|
|
>=> sequence_ . mapi (\objectId ->
|
|
|
|
r Chunks >=> sequence_ . mapWithKey (display objectId)
|
|
|
|
)
|
2020-03-11 18:55:18 +01:00
|
|
|
)
|
2020-03-10 22:57:11 +01:00
|
|
|
where
|
2020-03-11 18:55:18 +01:00
|
|
|
display a b v =
|
|
|
|
liftIO . putStrLn $
|
2020-03-17 08:36:02 +01:00
|
|
|
printf "p#%d obj#%d instr#%d: %s" n (getId a) (getId b) (Text.unpack v)
|
2019-09-23 18:00:47 +02:00
|
|
|
|
2020-03-04 18:19:10 +01:00
|
|
|
getAll :: Layer -> IO ()
|
2020-03-11 18:55:18 +01:00
|
|
|
getAll layer =
|
2020-03-17 08:36:02 +01:00
|
|
|
Map.toList <$> r Pages layer
|
2020-03-17 10:33:47 +01:00
|
|
|
>>= cacheFonts layer . mapM_ (uncurry displayPage)
|
2019-09-23 18:00:47 +02:00
|
|
|
|
2020-03-04 18:19:10 +01:00
|
|
|
get :: Int -> Layer -> IO ()
|
2020-03-17 08:36:02 +01:00
|
|
|
get n layer =
|
2020-03-17 10:33:47 +01:00
|
|
|
r (P n) layer >>= cacheFonts layer . displayPage n
|
2020-03-04 18:19:10 +01:00
|
|
|
|
|
|
|
onDoc :: FilePath -> (Layer -> IO ()) -> IO ()
|
|
|
|
onDoc inputFile f = do
|
|
|
|
(parseDocument <$> BS.readFile inputFile)
|
|
|
|
>>= either die (r UnifiedLayers >=> f)
|
2019-09-23 18:00:47 +02:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2019-11-29 11:51:35 +01:00
|
|
|
hSetBuffering stdout LineBuffering
|
|
|
|
args <- getArgs
|
|
|
|
case args of
|
2020-03-04 18:19:10 +01:00
|
|
|
[inputFile] -> onDoc inputFile getAll
|
|
|
|
[inputFile, pageNumber] -> onDoc inputFile (get $ read pageNumber)
|
2019-11-29 11:51:35 +01:00
|
|
|
_ -> die "Syntax: getText INPUT_FILE [PAGE_NUMBER]"
|