diff --git a/src/PDF/Object.hs b/src/PDF/Object.hs index 55984d3..a8c6cf5 100644 --- a/src/PDF/Object.hs +++ b/src/PDF/Object.hs @@ -37,7 +37,9 @@ import Control.Applicative ((<|>), many) import Data.Attoparsec.ByteString.Char8 (choice, count, option, sepBy) import Data.ByteString (ByteString) import qualified Data.ByteString as BS (concat) -import qualified Data.ByteString.Char8 as Char8 (cons, pack, singleton, unpack) +import qualified Data.ByteString.Char8 as Char8 ( + cons, length, pack, singleton, snoc, unpack + ) import Data.ByteString.Char8.Util (B16Int(..), b16ToBytes, unescape) import Data.Map (Map, (!), mapWithKey) import qualified Data.Map as Map ( @@ -122,7 +124,7 @@ instance Output StringObject where stringObject :: MonadParser m => m StringObject stringObject = Literal <$> (char '(' *> (BS.concat <$> literalString) <* char ')') - <|> Hexadecimal <$> (char '<' *> hexNumber <* char '>') + <|> Hexadecimal . roundBytes <$> (char '<' *> hexNumber <* char '>') "string object (literal or hexadecimal)" where literalString = many literalStringBlock @@ -133,6 +135,9 @@ stringObject = escapedChar = Char8.cons <$> char '\\' <*> (Char8.singleton <$> oneOf "nrtbf()\\\n" <|> octalCode) octalCode = choice $ (\n -> Char8.pack <$> count n octDigit) <$> [1..3] + roundBytes (B16Int bs) + | Char8.length bs `mod` 2 == 1 = B16Int (bs `Char8.snoc` '0') + | otherwise = B16Int bs toByteString :: StringObject -> ByteString toByteString (Hexadecimal h) = b16ToBytes h