First working version, no help and can only set the text rendering mode to 'Fill'

This commit is contained in:
Tissevert 2019-05-18 11:04:02 +02:00
commit 19f8dd6344
6 changed files with 147 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
dist*/
.ghc.environment.*

5
CHANGELOG.md Normal file
View file

@ -0,0 +1,5 @@
# Revision history for reveal
## 0.1.0.0 -- 2019-05-18
* First version. Released on an unsuspecting world.

30
LICENSE Normal file
View file

@ -0,0 +1,30 @@
Copyright (c) 2019, Tissevert
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Tissevert nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

30
reveal.cabal Normal file
View file

@ -0,0 +1,30 @@
cabal-version: >=1.10
-- Initial package description 'reveal.cabal' generated by 'cabal init'.
-- For further documentation, see http://haskell.org/cabal/users-guide/
name: reveal
version: 0.1.0.0
synopsis: A simple program to change the text rendering mode of a PDF
-- description:
homepage: https://git.marvid.fr/Tissevert/reveal
-- bug-reports:
license: BSD3
license-file: LICENSE
author: Tissevert
maintainer: tissevert+devel@marvid.fr
-- copyright:
category: Data
build-type: Simple
extra-source-files: CHANGELOG.md
executable reveal
main-is: Main.hs
other-modules: PDF.TextRendering
-- other-extensions:
build-depends: base >=4.12 && <4.13
, bytestring
, containers
, Hufflepdf
, zlib
hs-source-dirs: src
default-language: Haskell2010

51
src/Main.hs Normal file
View file

@ -0,0 +1,51 @@
{-# LANGUAGE NamedFieldPuns #-}
module Main where
import Codec.Compression.Zlib (compress, decompress)
import Data.ByteString.Lazy.Char8 (ByteString)
import qualified Data.ByteString.Lazy.Char8 as BS (
length, lines, readFile, unlines, writeFile
)
import qualified Data.Map as Map (insert, lookup)
import PDF (Document(..), parseDocument, render)
import PDF.Object (Content(..), DirectObject(..), Object(..), Name(..), Number(..))
import PDF.TextRendering (TextRendering(..), update)
import System.Environment (getArgs)
import System.IO (hPutStrLn, stderr)
revealFlateEncodedText :: ByteString -> ByteString
revealFlateEncodedText = compress . revealText . decompress
where
revealText = BS.unlines . fmap fill . BS.lines
fill = update $ const Fill
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
revealContent :: Content -> Content
revealContent content = content {
objects = revealObject <$> (objects content)
}
reveal :: Document -> Document
reveal document = document {
contents = revealContent <$> (contents document)
}
main :: IO ()
main = do
[inputPath, outputPath] <- getArgs
result <- parseDocument <$> BS.readFile inputPath
case result of
Left parseError -> hPutStrLn stderr $ show parseError
Right doc -> BS.writeFile outputPath . render $ reveal doc

29
src/PDF/TextRendering.hs Normal file
View file

@ -0,0 +1,29 @@
{-# LANGUAGE OverloadedStrings #-}
module PDF.TextRendering (
TextRendering(..)
, update
) where
import Data.ByteString.Lazy.Char8 (ByteString)
import qualified Data.ByteString.Lazy.Char8 as BS (pack, readInt, unwords, words)
data TextRendering =
Fill
| Stroke
| FillStroke
| Invisible
| FillAddPath
| StrokeAddPath
| FillStrokeAddPath
| AddPath
deriving (Enum, Show)
update :: (TextRendering -> TextRendering) -> ByteString -> ByteString
update f = BS.unwords . changeTextRendering . BS.words
where
changeTextRendering :: [ByteString] -> [ByteString]
changeTextRendering l@[value, "Tr"] =
case BS.readInt value of
Just (n, _) -> [BS.pack . show . fromEnum . f $ toEnum n, "Tr"]
_ -> l
changeTextRendering l = l