diff --git a/poppler/CairoOutputDev.cc b/poppler/CairoOutputDev.cc index d8f78d7..2312514 100644 --- a/poppler/CairoOutputDev.cc +++ b/poppler/CairoOutputDev.cc @@ -1158,6 +1158,13 @@ void CairoOutputDev::beginString(GfxState *state, GooString *s) } } + +#define UNICODE_VALID(Char) \ + ((Char) < 0x110000 && \ + (((Char) & 0xFFFFF800) != 0xD800) && \ + ((Char) < 0xFDD0 || (Char) > 0xFDEF) && \ + ((Char) & 0xFFFE) != 0xFFFE) + void CairoOutputDev::drawChar(GfxState *state, double x, double y, double dx, double dy, double originX, double originY, @@ -1179,7 +1186,13 @@ void CairoOutputDev::drawChar(GfxState *state, double x, double y, } clusters[clusterCount].num_bytes = 0; for (int i = 0; i < uLen; i++) { - int size = mapUTF8 (u[i], utf8 + utf8Count, utf8Max - utf8Count); + int size; + /* Replace the charcter with 0xfffe if it is not valid UTF-8. */ + if (UNICODE_VALID(u[i])) + size = mapUTF8 (u[i], utf8 + utf8Count, utf8Max - utf8Count); + else + size = mapUTF8((Unicode)0xfffd, utf8 + utf8Count, utf8Max - utf8Count); + utf8Count += size; clusters[clusterCount].num_bytes += size; }