pdf-toolbox-document 0.0.3.0 → 0.0.4.0
raw patch · 4 files changed
+40/−8 lines, 4 filesdep ~pdf-toolbox-corePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: pdf-toolbox-core
API changes (from Hackage documentation)
+ Pdf.Toolbox.Document.Pdf: instance (Monad m, Functor m) => Applicative (Pdf' m)
Files
- changelog.md +6/−0
- lib/Pdf/Toolbox/Document/Page.hs +28/−4
- lib/Pdf/Toolbox/Document/Pdf.hs +2/−1
- pdf-toolbox-document.cabal +4/−3
+ changelog.md view
@@ -0,0 +1,6 @@++0.0.4.0++* extracting text: try to insert spaces and newlines+* fix attoparsec module deprication warnings+* fix AMP warnings
lib/Pdf/Toolbox/Document/Page.hs view
@@ -13,10 +13,12 @@ ) where +import Data.Monoid import Data.Functor import qualified Data.Traversable as Traversable import Data.Text (Text)-import qualified Data.Text as Text+import qualified Data.Text.Lazy as TextL+import qualified Data.Text.Lazy.Builder as TextB import qualified Data.Map as Map import Control.Monad @@ -94,8 +96,8 @@ -- | Extract text from the page ----- Right now it doesn't even try to insert additional spaces or newlines,--- and returns text as it is embeded. But someday it will.+-- It tries to add spaces between chars if they don't present+-- as actual characters in content stream. pageExtractText :: (MonadPdf m, MonadIO m) => Page -> PdfE m Text pageExtractText page = do -- load fonts and create glyph decoder@@ -125,8 +127,30 @@ let loop p = do next <- readNextOperator is case next of- Nothing -> return $ Text.concat $ mapMaybe glyphText $ concat $ prGlyphs p Just op -> processOp op p >>= loop+ Nothing -> return $ glyphsToText (prGlyphs p) loop $ mkProcessor { prGlyphDecoder = glyphDecoder }++-- | Convert glyphs to text, trying to add spaces and newlines+--+-- It takes list of spans. Each span is a list of glyphs that are outputed in one shot.+-- So we don't need to add space inside span, only between them.+glyphsToText :: [[Glyph]] -> Text+glyphsToText = TextL.toStrict . TextB.toLazyText . snd . foldl step ((Vector 0 0, False), mempty)+ where+ step acc [] = acc+ step ((Vector lx2 ly2, wasSpace), res) sp =+ let Vector x1 y1 = glyphTopLeft (head sp)+ Vector x2 _ = glyphBottomRight (last sp)+ Vector _ y2 = glyphTopLeft (last sp)+ space =+ if abs (ly2 - y1) < 1.8+ then if wasSpace || abs (lx2 - x1) < 1.8+ then mempty+ else TextB.singleton ' '+ else TextB.singleton '\n'+ txt = TextB.fromLazyText $ TextL.fromChunks $ mapMaybe glyphText sp+ endWithSpace = glyphText (last sp) == Just " "+ in ((Vector x2 y2, endWithSpace), mconcat [res, space, txt])
lib/Pdf/Toolbox/Document/Pdf.hs view
@@ -27,6 +27,7 @@ import qualified Data.ByteString as BS import Data.Map (Map) import qualified Data.Map as Map+import Control.Applicative import Control.Monad import Control.Monad.Trans.Class import Control.Monad.Trans.State@@ -51,7 +52,7 @@ -- | Basic implementation of pdf monad newtype Pdf' m a = Pdf' (StateT PdfState m a)- deriving (Monad, Functor, MonadIO, MonadTrans)+ deriving (Monad, Functor, Applicative, MonadIO, MonadTrans) -- | Convenient type alias type Pdf m = PdfE (Pdf' m)
pdf-toolbox-document.cabal view
@@ -1,15 +1,16 @@ name: pdf-toolbox-document-version: 0.0.3.0+version: 0.0.4.0 synopsis: A collection of tools for processing PDF files. license: BSD3 license-file: LICENSE author: Yuras Shumovich maintainer: Yuras Shumovich <shumovichy@gmail.com>-copyright: Copyright (c) Yuras Shumovich 2012-2013+copyright: Copyright (c) Yuras Shumovich 2012-2014 category: PDF build-type: Simple cabal-version: >=1.8 homepage: https://github.com/Yuras/pdf-toolbox+extra-source-files: changelog.md description: Mid level tools for processing PDF files. .@@ -42,5 +43,5 @@ io-streams, cipher-rc4, cryptohash,- pdf-toolbox-core ==0.0.2.*,+ pdf-toolbox-core >=0.0.2 && <0.0.4, pdf-toolbox-content ==0.0.3.*