diff options
-rw-r--r-- | include/mupdf/fitz/output.h | 1 | ||||
-rw-r--r-- | source/fitz/printf.c | 39 |
2 files changed, 40 insertions, 0 deletions
diff --git a/include/mupdf/fitz/output.h b/include/mupdf/fitz/output.h index 01d4d227..9c0c9dd0 100644 --- a/include/mupdf/fitz/output.h +++ b/include/mupdf/fitz/output.h @@ -97,6 +97,7 @@ fz_write_byte(fz_output *out, int x) see fz_ftoa for specifics. %C outputs a utf8 encoded int. %M outputs a fz_matrix*. %R outputs a fz_rect*. %P outputs a fz_point*. + %q and %( output escaped strings in C/PDF syntax. */ int fz_vsnprintf(char *buffer, int space, const char *fmt, va_list args); diff --git a/source/fitz/printf.c b/source/fitz/printf.c index ef70aca1..c6cb90a9 100644 --- a/source/fitz/printf.c +++ b/source/fitz/printf.c @@ -123,6 +123,35 @@ static void fmtint(struct fmtbuf *out, int value, int z, int base) fmtputc(out, buf[--i]); } +static void fmtquote(struct fmtbuf *out, const char *s, int sq, int eq) +{ + int c; + fmtputc(out, sq); + while ((c = *s++)) { + switch (c) { + default: + if (c < 32 || c > 127) { + fmtputc(out, '\\'); + fmtputc(out, '0' + ((c >> 6) & 7)); + fmtputc(out, '0' + ((c >> 3) & 7)); + fmtputc(out, '0' + ((c) & 7)); + } else { + if (c == sq || c == eq) + fmtputc(out, '\\'); + fmtputc(out, c); + } + break; + case '\\': fmtputc(out, '\\'); fmtputc(out, '\\'); break; + case '\b': fmtputc(out, '\\'); fmtputc(out, 'b'); break; + case '\f': fmtputc(out, '\\'); fmtputc(out, 'f'); break; + case '\n': fmtputc(out, '\\'); fmtputc(out, 'n'); break; + case '\r': fmtputc(out, '\\'); fmtputc(out, 'r'); break; + case '\t': fmtputc(out, '\\'); fmtputc(out, 't'); break; + } + } + fmtputc(out, eq); +} + int fz_vsnprintf(char *buffer, int space, const char *fmt, va_list args) { @@ -217,6 +246,16 @@ fz_vsnprintf(char *buffer, int space, const char *fmt, va_list args) while ((c = *s++)) fmtputc(&out, c); break; + case 'q': + s = va_arg(args, char*); + if (!s) s = ""; + fmtquote(&out, s, '"', '"'); + break; + case '(': + s = va_arg(args, char*); + if (!s) s = ""; + fmtquote(&out, s, '(', ')'); + break; } } else { fmtputc(&out, c); |