summaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Writers
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2013-05-02 19:42:37 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2013-05-02 19:42:37 -0700
commit008273ef67ee846ad20ada6502d17b7c521e8630 (patch)
tree181ffaa9b65f5d8980d987b96070ccf6a2e4cc42 /src/Text/Pandoc/Writers
parent9d01c45b014f86bc3c2f5178e0d7c496c0d10300 (diff)
RTF writer: Properly handle characters above the 0000-FFFF range.
Uses surrogate pairs. Thanks to Hiromi Ishii for the patch.
Diffstat (limited to 'src/Text/Pandoc/Writers')
-rw-r--r--src/Text/Pandoc/Writers/RTF.hs14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/Text/Pandoc/Writers/RTF.hs b/src/Text/Pandoc/Writers/RTF.hs
index ca33cb0e9..0d4a22cd5 100644
--- a/src/Text/Pandoc/Writers/RTF.hs
+++ b/src/Text/Pandoc/Writers/RTF.hs
@@ -35,7 +35,7 @@ import Text.Pandoc.Readers.TeXMath
import Text.Pandoc.Templates (renderTemplate)
import Text.Pandoc.Generic (bottomUpM)
import Data.List ( isSuffixOf, intercalate )
-import Data.Char ( ord, isDigit, toLower )
+import Data.Char ( ord, chr, isDigit, toLower )
import System.FilePath ( takeExtension )
import qualified Data.ByteString as B
import Text.Printf ( printf )
@@ -113,8 +113,18 @@ handleUnicode :: String -> String
handleUnicode [] = []
handleUnicode (c:cs) =
if ord c > 127
- then '\\':'u':(show (ord c)) ++ "?" ++ handleUnicode cs
+ then if surrogate c
+ then let x = ord c - 0x10000
+ (q, r) = x `divMod` 0x400
+ upper = q + 0xd800
+ lower = r + 0xDC00
+ in enc (chr upper) ++ enc (chr lower) ++ handleUnicode cs
+ else enc c ++ handleUnicode cs
else c:(handleUnicode cs)
+ where
+ surrogate x = not ( (0x0000 <= ord x && ord x <= 0xd7ff)
+ || (0xe000 <= ord x && ord x <= 0xffff) )
+ enc x = '\\':'u':(show (ord x)) ++ "?"
-- | Escape special characters.
escapeSpecial :: String -> String