[gs-commits] mupdf 1.16.1.15 Support surrogate pairs when encoding PDF

[email protected] (Tor Andersson)
Newsgroups gmane.comp.printing.ghostscript.cvs
Message-ID <[email protected]>
commit 6ee8a076b07a37d121994e7f6d29a2a8535254f9
Author: Tor Andersson <[email protected]>
Date:   Tue Aug 20 11:38:00 2019 +0200

    Support surrogate pairs when encoding PDF text strings.

diff --git a/source/pdf/pdf-parse.c b/source/pdf/pdf-parse.c
index 04a7722..80d9a69 100644
--- a/source/pdf/pdf-parse.c
+++ b/source/pdf/pdf-parse.c
@@ -291,17 +291,42 @@ pdf_load_stream_or_string_as_utf8(fz_context *ctx, pdf_obj *src)
 static pdf_obj *
 pdf_new_text_string_utf16be(fz_context *ctx, const char *s)
 {
-	int c, i = 0, n = fz_utflen(s);
-	unsigned char *p = fz_malloc(ctx, n * 2 + 2);
+	const char *ss;
+	int c, i, n, a, b;
+	unsigned char *p;
 	pdf_obj *obj;
+
+	ss = s;
+	n = 0;
+	while (*ss)
+	{
+		ss += fz_chartorune(&c, ss);
+		n += (c >= 0x10000) ? 2 : 1;
+	}
+
+	p = fz_malloc(ctx, n * 2 + 2);
+	i = 0;
 	p[i++] = 254;
 	p[i++] = 255;
 	while (*s)
 	{
 		s += fz_chartorune(&c, s);
-		p[i++] = (c>>8) & 0xff;
-		p[i++] = (c) & 0xff;
+		if (c >= 0x10000)
+		{
+			a = (((c - 0x10000) >> 10) & 0x3ff) + 0xD800;
+			p[i++] = (a>>8) & 0xff;
+			p[i++] = (a) & 0xff;
+			b = (((c - 0x10000)) & 0x3ff) + 0xDC00;
+			p[i++] = (b>>8) & 0xff;
+			p[i++] = (b) & 0xff;
+		}
+		else
+		{
+			p[i++] = (c>>8) & 0xff;
+			p[i++] = (c) & 0xff;
+		}
 	}
+
 	fz_try(ctx)
 		obj = pdf_new_string(ctx, (char*)p, i);
 	fz_always(ctx)

http://git.ghostscript.com/?p=mupdf.git;a=commit;h=6ee8a076b07a37d121994e7f6d29a2a8535254f9

--
MuPDF library
Artifex Software, Inc.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.