From 737a00a219b56b4b5a55d4659a6309457e57099f Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sat, 27 Nov 2021 13:54:28 -0800 Subject: [PATCH] implement next 'gvputs_nonascii' function This is based on `fig_string`, `mp_string`, and `pic_string`, but outputting data directly into the output file rather than a temporary buffer. Related to #2051. --- CHANGELOG.md | 2 ++ lib/gvc/gvc.def | 1 + lib/gvc/gvdevice.c | 12 ++++++++++++ lib/gvc/gvio.h | 3 +++ 4 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d35708f12..f5b8ca51d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - hard-coded lookup tables for fallback font metrics for more fonts and font variants +- a new `gvputs_nonascii` API function has been implemented for GVC I/O with C + escaping ### Changed diff --git a/lib/gvc/gvc.def b/lib/gvc/gvc.def index e9f870950..6ba302595 100644 --- a/lib/gvc/gvc.def +++ b/lib/gvc/gvc.def @@ -149,6 +149,7 @@ gvprintpointf gvprintpointflist gvputc gvputs +gvputs_nonascii gvputs_xml gvRender gvRenderData diff --git a/lib/gvc/gvdevice.c b/lib/gvc/gvdevice.c index de53cdefb..40fce3c57 100644 --- a/lib/gvc/gvdevice.c +++ b/lib/gvc/gvdevice.c @@ -277,6 +277,18 @@ int gvputs_xml(GVJ_t *job, const char *s) { return xml_escape(s, flags, (int (*)(void *, const char *))gvputs, job); } +void gvputs_nonascii(GVJ_t *job, const char *s) { + for (; *s != '\0'; ++s) { + if (*s == '\\') { + gvputs(job, "\\\\"); + } else if (isascii((int)*s)) { + gvputc(job, *s); + } else { + gvprintf(job, "%03o", (unsigned)*s); + } + } +} + int gvputc(GVJ_t * job, int c) { const char cc = c; diff --git a/lib/gvc/gvio.h b/lib/gvc/gvio.h index 1be27c037..eaf6fc65e 100644 --- a/lib/gvc/gvio.h +++ b/lib/gvc/gvio.h @@ -42,6 +42,9 @@ extern "C" { // `gvputs`, but XML-escape the input string GVIO_API int gvputs_xml(GVJ_t* job, const char *s); + // `gvputs`, C-escaping '\' and non-ASCII bytes + GVIO_API void gvputs_nonascii(GVJ_t* job, const char *s); + GVIO_API int gvflush (GVJ_t * job); GVIO_API void gvprintf(GVJ_t * job, const char *format, ...); GVIO_API void gvprintdouble(GVJ_t * job, double num); -- 2.40.0