git.iko.soy/typeable/pandoc
on master help sign in
RTF writer: Properly handle characters above the 0000-FFFF range.

Uses surrogate pairs. Thanks to Hiromi Ishii for the patch.

commit008273ef67ee846ad20ada6502d17b7c521e8630 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 2 May 2013, 19:42 (13 years ago) parent9d01c45
1 file changed+12−2
src/Text/Pandoc/Writers/RTF.hs+12−2
src/Text/Pandoc/Writers/RTF.hs+12−2
@@ -35,7 +35,7 @@ import Text.Pandoc.Readers.TeXMath
3535import Text.Pandoc.Templates (renderTemplate)
3636import Text.Pandoc.Generic (bottomUpM)
3737import Data.List ( isSuffixOf, intercalate )
38−import Data.Char ( ord, isDigit, toLower )
38+import Data.Char ( ord, chr, isDigit, toLower )
3939import System.FilePath ( takeExtension )
4040import qualified Data.ByteString as B
4141import Text.Printf ( printf )
@@ -113,8 +113,18 @@ handleUnicode :: String -> String
113113handleUnicode [] = []
114114handleUnicode (c:cs) =
115115 if ord c > 127
116− then '\\':'u':(show (ord c)) ++ "?" ++ handleUnicode cs
116+ then if surrogate c
117+ then let x = ord c - 0x10000
118+ (q, r) = x `divMod` 0x400
119+ upper = q + 0xd800
120+ lower = r + 0xDC00
121+ in enc (chr upper) ++ enc (chr lower) ++ handleUnicode cs
122+ else enc c ++ handleUnicode cs
117123 else c:(handleUnicode cs)
124+ where
125+ surrogate x = not ( (0x0000 <= ord x && ord x <= 0xd7ff)
126+ || (0xe000 <= ord x && ord x <= 0xffff) )
127+ enc x = '\\':'u':(show (ord x)) ++ "?"
118128
119129-- | Escape special characters.
120130escapeSpecial :: String -> String