23 #include "gvplugin_skillgui_cairo.h" 25 #include <utils/system/argparser.h> 30 #include <sys/types.h> 50 : argp(argc, argv,
"hi:o:f:wps:")
52 if (! (argp.has_arg(
"i") && argp.has_arg(
"o") && argp.has_arg(
"f"))
53 || argp.has_arg(
"h")) {
58 format = argp.arg(
"f");
61 white_bg = argp.has_arg(
"w");
62 postproc_required =
false;
63 do_postproc = argp.has_arg(
"p");
64 maxwidth = maxheight = 0;
67 if ( (format !=
"pdf") && (format !=
"svg") && (format !=
"png") ) {
68 printf(
"Unknown format '%s'\n\n", format.c_str());
73 if ( do_postproc && (format !=
"png") ) {
74 printf(
"Post-processing only available for PNG output format.\n");
78 if (argp.has_arg(
"s")) {
80 scale = strtod(argp.arg(
"s"), &endptr);
82 printf(
"Invalid scale value '%s', could not convert to number (failed at '%s').\n",
83 argp.arg(
"s"), endptr);
88 indir = argp.arg(
"i");
89 outdir = argp.arg(
"o");
91 struct stat statbuf_in, statbuf_out;
92 if (stat(indir.c_str(), &statbuf_in) != 0) {
93 perror(
"Unable to stat input directory");
96 if (stat(outdir.c_str(), &statbuf_out) != 0) {
97 perror(
"Unable to stat output directory");
100 if (! S_ISDIR(statbuf_in.st_mode) || ! S_ISDIR(statbuf_out.st_mode)) {
101 printf(
"Input or output directory is not a directory.\n\n");
105 char outdir_real[PATH_MAX];
106 if (realpath(outdir.c_str(), outdir_real)) {
107 outdir = outdir_real;
110 directory = opendir(indir.c_str());
112 printf(
"Could not open input directory\n");
117 gvplugin_skillgui_cairo_setup(gvc,
this);
130 printf(
"\nUsage: %s -i <dir> -o <dir> -f <format> [-w] [-s scale]\n" 131 " -i dir Input directory containing dot graphs\n" 132 " -o dir Output directory for generated graphs\n" 133 " -f format Output format, one of pdf, svg, or png\n" 134 " -w White background\n" 135 " -p Postprocess frames to same size (PNG only)\n" 136 " -s scale Scale factor to apply during rendering\n" 138 argp.program_name());
144 if (format ==
"pdf") {
145 surface = Cairo::PdfSurface::create(outfile, bbw * scale, bbh * scale);
146 printf(
"Creating PDF context of size %f x %f\n", bbw * scale, bbh * scale);
147 }
else if (format ==
"svg") {
148 surface = Cairo::SvgSurface::create(outfile, bbw * scale, bbh * scale);
149 }
else if (format ==
"png") {
150 surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32,
151 (
int)ceilf(bbw * scale),
152 (
int)ceilf(bbh * scale));
155 cairo = Cairo::Context::create(surface);
157 cairo->set_source_rgb(1, 1, 1);
169 height = bbh * scale;
180 ty = (bbh - pad_y) * scale;
183 virtual void set_bb(
double bbw,
double bbh)
188 if ( bbw * scale > maxwidth ) {
189 postproc_required = (maxwidth != 0);
190 maxwidth = bbw * scale;
192 if ( bbh * scale > maxheight * scale ) {
193 postproc_required = (maxheight != 0);
194 maxheight = bbh * scale;
198 virtual void set_pad(
double pad_x,
double pad_y)
205 virtual void get_pad(
double &pad_x,
double &pad_y)
215 FILE *f = fopen(infile.c_str(),
"r");
216 #if defined(GRAPHVIZ_ATLEAST_230) && defined(WITH_CGRAPH) 217 Agraph_t *g = agread(f,0);
219 Agraph_t *g = agread(f);
222 gvLayout(gvc, g, (
char *)
"dot");
223 gvRender(gvc, g, (
char *)
"skillguicairo", NULL);
224 gvFreeLayout(gvc, g);
230 surface->write_to_png(outfile);
242 while ((d = readdir(directory)) != NULL) {
243 if (fnmatch(
"*.dot", d->d_name, FNM_PATHNAME | FNM_PERIOD) == 0) {
244 char infile_real[PATH_MAX];
245 infile = indir +
"/" + d->d_name;
246 if (realpath(infile.c_str(), infile_real)) {
247 infile = infile_real;
249 char *basefile = strdup(infile.c_str());
250 std::string basen = basename(basefile);
252 outfile = outdir +
"/" + basen.substr(0, basen.length() - 3) + format;
253 printf(
"Converting %s to %s\n", infile.c_str(), outfile.c_str());
256 printf(
"%s does not match pattern\n", d->d_name);
260 if (do_postproc && postproc_required) {
272 const unsigned char *data,
unsigned int length)
274 FILE *f = (FILE *)closure;
275 if (fwrite(data, length, 1, f)) {
276 return CAIRO_STATUS_SUCCESS;
278 return CAIRO_STATUS_WRITE_ERROR;
285 printf(
"Post-processing PNG files, resizing to %fx%f\n", maxwidth, maxheight);
287 DIR *output_dir = opendir(outdir.c_str());
288 while ((d = readdir(output_dir)) != NULL) {
289 if (fnmatch(
"*.png", d->d_name, FNM_PATHNAME | FNM_PERIOD) == 0) {
290 infile = outdir +
"/" + d->d_name;
291 Cairo::RefPtr<Cairo::ImageSurface> imgs = Cairo::ImageSurface::create_from_png(infile);
292 if ( (imgs->get_height() != maxheight) || (imgs->get_width() != maxwidth)) {
294 char *tmpout = strdup((outdir +
"/tmpXXXXXX").c_str());
295 FILE *f = fdopen(mkstemp(tmpout),
"w");
299 Cairo::RefPtr<Cairo::ImageSurface> outs = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32,
300 (
int)ceilf(maxwidth),
301 (
int)ceilf(maxheight));
302 double tx = (maxwidth - imgs->get_width()) / 2.0;
303 double ty = (maxheight - imgs->get_height()) / 2.0;
304 printf(
"Re-creating %s for post-processing, " 305 "resizing from %ix%i, tx=%f, ty=%f\n", infile.c_str(),
306 imgs->get_width(), imgs->get_height(), tx, ty);
307 Cairo::RefPtr<Cairo::Context> cc = Cairo::Context::create(outs);
309 cc->set_source_rgb(1, 1, 1);
312 cc->set_source(imgs, tx, ty);
319 rename(outfile.c_str(), infile.c_str());
323 closedir(output_dir);
330 Cairo::RefPtr<Cairo::Surface> surface;
331 Cairo::RefPtr<Cairo::Context> cairo;
341 double maxwidth, maxheight;
342 bool postproc_required;
350 main(
int argc,
char **argv)
virtual void get_pad(double &pad_x, double &pad_y)
Get padding.
Graphviz Cairo render plugin instructor.
Fawkes library namespace.
virtual bool scale_override()
Check if scale override is enabled.
virtual void set_translation(double tx, double ty)
Set translation.
Parse command line arguments.
DOT graph batch renderer.
void usage()
Show usage instructions.
virtual void get_translation(double &tx, double &ty)
Get translation values.
void run()
Run the renderer.
void render()
Render graph.
virtual void get_dimensions(double &width, double &height)
Get available space dimensions.
static cairo_status_t write_func(void *closure, const unsigned char *data, unsigned int length)
Write function for Cairo.
virtual void set_pad(double pad_x, double pad_y)
Set padding.
virtual Cairo::RefPtr< Cairo::Context > get_cairo()
Get Cairo context.
void postprocess()
Post-process files.
virtual double get_scale()
Get scale factor.
virtual void set_scale(double scale)
Set scale.
virtual void set_bb(double bbw, double bbh)
Set the bounding box.
SkillGuiBatchRenderer(int argc, char **argv)
Constructor.
~SkillGuiBatchRenderer()
Destructor.