@@ -27,13 +27,34 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2727
2828Conversion of 'Pandoc' documents to RTF (rich text format).
2929-}
30−module Text.Pandoc.Writers.RTF ( writeRTF ) where
30+module Text.Pandoc.Writers.RTF ( writeRTF, rtfEmbedImage ) where
3131import Text.Pandoc.Definition
3232import Text.Pandoc.Shared
3333import Text.Pandoc.Readers.TeXMath
3434import Text.Pandoc.Templates (renderTemplate)
3535import Data.List ( isSuffixOf, intercalate )
36−import Data.Char ( ord, isDigit )
36+import Data.Char ( ord, isDigit, toLower )
37+import System.FilePath ( takeExtension )
38+import qualified Data.ByteString as B
39+import Text.Printf ( printf )
40+
41+-- | Convert Image inlines into a raw RTF embedded image, read from a file.
42+-- If file not found or filetype not jpeg or png, leave the inline unchanged.
43+rtfEmbedImage :: Inline -> IO Inline
44+rtfEmbedImage x@(Image _ (src,_))
45+ | map toLower (takeExtension src) `elem` [".jpg",".jpeg",".png"] = do
46+ imgdata <- catch (B.readFile src) (\_ -> return B.empty)
47+ let bytes = map (printf "%02x") $ B.unpack imgdata
48+ let filetype = case map toLower (takeExtension src) of
49+ ".jpg" -> "\\jpegblip"
50+ ".jpeg" -> "\\jpegblip"
51+ ".png" -> "\\pngblip"
52+ _ -> error "Unknown file type"
53+ let raw = "{\\pict" ++ filetype ++ " " ++ concat bytes ++ "}"
54+ return $ if B.null imgdata
55+ then x
56+ else RawInline "rtf" raw
57+rtfEmbedImage x = return x
3758
3859-- | Convert Pandoc to a string in rich text format.
3960writeRTF :: WriterOptions -> Pandoc -> String