diff --git a/src/PDF/Content/Text.hs b/src/PDF/Content/Text.hs index 6cf6ae7..fdaf2dd 100644 --- a/src/PDF/Content/Text.hs +++ b/src/PDF/Content/Text.hs @@ -14,9 +14,9 @@ import Control.Monad (foldM) import Control.Monad.Fail (MonadFail(..)) import Control.Monad.Reader (MonadReader(..), asks, runReaderT) import Control.Monad.State (MonadState(..), evalStateT) -import Data.ByteString.Char8 (pack) import Data.Map ((!)) -import Data.Text (Text) +import Data.Text (Text, breakOn) +import qualified Data.Text as Text (drop) import PDF.Box (Box(..)) import PDF.Content ( Content, ContentUnit(..), Id(..), Indexed, GraphicContextUnit(..) @@ -27,6 +27,7 @@ import PDF.Content.Operator (Instruction, Operator(..)) import PDF.Content.Operator.Text (Operator(..)) import PDF.Font (Font(..), FontSet, emptyFont) import PDF.Object (DirectObject(..), StringObject(..), toByteString) +import PDF.Pages (Page(..)) import Prelude hiding (fail) data ROContext = ROContext { @@ -91,9 +92,13 @@ renderInstruction (Text DQuote, [StringObject outputString]) = renderInstruction _ = return [] -format :: String -> [Instruction] -format s = - case break (== '\n') s of - ("", "") -> [] - ("", left) -> (Text Tstar, []) : format (drop 1 left) - (line, left) -> (Text Tj, [StringObject . Literal $ pack line]) : format left +format :: (MonadFail m, MonadReader Font m) => Text -> m [Instruction] +format input = + case breakOn "\n" input of + ("", "") -> return [] + ("", left) -> ((Text Tstar, []) :) <$> format (Text.drop 1 left) + (line, left) -> (:) <$> tj line <*> format left + where + tj t = do + literal <- either fail (return . Literal) =<< asks (($t) . encode) + return (Text Tj, [StringObject literal])