]> granicus.if.org Git - graphviz/commitdiff
LASI plugin: use job context instead of globals
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Fri, 9 Apr 2021 03:27:46 +0000 (20:27 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 10 Apr 2021 21:53:50 +0000 (14:53 -0700)
The LASI plugin was using two global variables, making it impossible to process
multiple graphs with it at the same time. This change refactors it to use the
jobs context as other rendering plugins do.

plugin/lasi/gvrender_lasi.cpp

index e62506c8b2a677e511a211bea4b980e540aef92b..69684276cce479c8369b935c96797ed08f7b8170 100644 (file)
@@ -36,31 +36,35 @@ using namespace std;
 
 typedef enum { FORMAT_PS, FORMAT_PS2, FORMAT_EPS } format_type;
 
-PostscriptDocument *doc;
-size_t (*save_write_fn) (GVJ_t *job, const char *s, size_t len);
+struct Context {
+  PostscriptDocument *doc;
+  size_t (*save_write_fn) (GVJ_t *job, const char *s, size_t len);
+};
 
 static size_t lasi_head_writer(GVJ_t * job, const char *s, size_t len)
 {
-    doc->osHeader() << s;
+    Context *ctxt = reinterpret_cast<Context*>(job->context);
+    ctxt->doc->osHeader() << s;
     return len;
 }
 
 static size_t lasi_body_writer(GVJ_t * job, const char *s, size_t len)
 {
-    doc->osBody() << s;
+    Context *ctxt = reinterpret_cast<Context*>(job->context);
+    ctxt->doc->osBody() << s;
     return len;
 }
 
 static size_t lasi_footer_writer(GVJ_t * job, const char *s, size_t len)
 {
-    doc->osFooter() << s;
+    Context *ctxt = reinterpret_cast<Context*>(job->context);
+    ctxt->doc->osFooter() << s;
     return len;
 }
 
 static void lasi_begin_job(GVJ_t * job)
 {
-    doc = new PostscriptDocument;
-    save_write_fn = job->gvc->write_fn;
+    job->context = new Context{new PostscriptDocument, job->gvc->write_fn};
     job->gvc->write_fn = lasi_head_writer;
 
     gvprintf(job, "%%%%Creator: %s version %s (%s)\n",
@@ -103,12 +107,14 @@ static void lasi_end_job(GVJ_t * job)
            std::ostream & str_;
         } swapper(cout, output);
     
-        doc->write(cout);
+        Context *ctxt = reinterpret_cast<Context*>(job->context);
+        ctxt->doc->write(cout);
     
-        job->gvc->write_fn = save_write_fn;
+        job->gvc->write_fn = ctxt->save_write_fn;
         gvputs(job, output.str().c_str());
 
-       delete doc;
+       delete ctxt->doc;
+       delete ctxt;
     }
 }
 
@@ -370,7 +376,8 @@ static void lasi_textspan(GVJ_t * job, pointf p, textspan_t * span)
     }
 
     ps_set_color(job, &(job->obj->pencolor));
-    doc->osBody() << setFont(font, style, weight, variant, stretch) << setFontSize(span->font->size) << endl;
+    Context *ctxt = reinterpret_cast<Context*>(job->context);
+    ctxt->doc->osBody() << setFont(font, style, weight, variant, stretch) << setFontSize(span->font->size) << endl;
     switch (span->just) {
     case 'r':
         p.x -= span->size.x;
@@ -386,7 +393,7 @@ static void lasi_textspan(GVJ_t * job, pointf p, textspan_t * span)
     p.y += span->yoffset_centerline;
     gvprintpointf(job, p);
     gvputs(job, " moveto ");
-    doc->osBody() << show(span->str) << endl;
+    ctxt->doc->osBody() << show(span->str) << endl;
 
 }