gvc.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include "config.h"
  11. #include <gvc/gvc.h>
  12. #include <common/const.h>
  13. #include <gvc/gvcjob.h>
  14. #include <gvc/gvcint.h>
  15. #include <gvc/gvcproc.h>
  16. #include <gvc/gvconfig.h>
  17. #include <gvc/gvio.h>
  18. #include <math.h>
  19. #include <stdbool.h>
  20. #include <stdlib.h>
  21. GVC_t *gvContext(void)
  22. {
  23. GVC_t *gvc;
  24. agattr(NULL, AGNODE, "label", NODENAME_ESC);
  25. /* default to no builtins, demand loading enabled */
  26. gvc = gvNEWcontext(NULL, true);
  27. gvconfig(gvc, false); /* configure for available plugins */
  28. return gvc;
  29. }
  30. GVC_t *gvContextPlugins(const lt_symlist_t *builtins, int demand_loading)
  31. {
  32. GVC_t *gvc;
  33. agattr(NULL, AGNODE, "label", NODENAME_ESC);
  34. gvc = gvNEWcontext(builtins, demand_loading);
  35. gvconfig(gvc, false); /* configure for available plugins */
  36. return gvc;
  37. }
  38. /* gvLayout:
  39. * Selects layout based on engine and binds it to gvc;
  40. * does the layout and sets the graph's bbox.
  41. * Return 0 on success.
  42. */
  43. int gvLayout(GVC_t *gvc, graph_t *g, const char *engine)
  44. {
  45. char buf[256];
  46. int rc;
  47. rc = gvlayout_select(gvc, engine);
  48. if (rc == NO_SUPPORT) {
  49. agerrorf("Layout type: \"%s\" not recognized. Use one of:%s\n",
  50. engine, gvplugin_list(gvc, API_layout, engine));
  51. return -1;
  52. }
  53. if (gvLayoutJobs(gvc, g) == -1)
  54. return -1;
  55. /* set bb attribute for basic layout.
  56. * doesn't yet include margins, scaling or page sizes because
  57. * those depend on the renderer being used. */
  58. if (GD_drawing(g)->landscape)
  59. snprintf(buf, sizeof(buf), "%.0f %.0f %.0f %.0f",
  60. round(GD_bb(g).LL.y), round(GD_bb(g).LL.x),
  61. round(GD_bb(g).UR.y), round(GD_bb(g).UR.x));
  62. else
  63. snprintf(buf, sizeof(buf), "%.0f %.0f %.0f %.0f",
  64. round(GD_bb(g).LL.x), round(GD_bb(g).LL.y),
  65. round(GD_bb(g).UR.x), round(GD_bb(g).UR.y));
  66. agsafeset(g, "bb", buf, "");
  67. return 0;
  68. }
  69. /* Render layout in a specified format to an open FILE */
  70. int gvRender(GVC_t *gvc, graph_t *g, const char *format, FILE *out)
  71. {
  72. int rc;
  73. GVJ_t *job;
  74. /* create a job for the required format */
  75. bool r = gvjobs_output_langname(gvc, format);
  76. job = gvc->job;
  77. if (!r) {
  78. agerrorf("Format: \"%s\" not recognized. Use one of:%s\n",
  79. format, gvplugin_list(gvc, API_device, format));
  80. return -1;
  81. }
  82. job->output_lang = gvrender_select(job, job->output_langname);
  83. if (!LAYOUT_DONE(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
  84. agerrorf( "Layout was not done\n");
  85. return -1;
  86. }
  87. job->output_file = out;
  88. if (out == NULL)
  89. job->flags |= OUTPUT_NOT_REQUIRED;
  90. rc = gvRenderJobs(gvc, g);
  91. gvrender_end_job(job);
  92. gvjobs_delete(gvc);
  93. return rc;
  94. }
  95. /* Render layout in a specified format to a file with the given name */
  96. int gvRenderFilename(GVC_t *gvc, graph_t *g, const char *format, const char *filename)
  97. {
  98. int rc;
  99. GVJ_t *job;
  100. /* create a job for the required format */
  101. bool r = gvjobs_output_langname(gvc, format);
  102. job = gvc->job;
  103. if (!r) {
  104. agerrorf("Format: \"%s\" not recognized. Use one of:%s\n",
  105. format, gvplugin_list(gvc, API_device, format));
  106. return -1;
  107. }
  108. job->output_lang = gvrender_select(job, job->output_langname);
  109. if (!LAYOUT_DONE(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
  110. agerrorf( "Layout was not done\n");
  111. return -1;
  112. }
  113. gvjobs_output_filename(gvc, filename);
  114. rc = gvRenderJobs(gvc, g);
  115. gvrender_end_job(job);
  116. gvdevice_finalize(job);
  117. gvjobs_delete(gvc);
  118. return rc;
  119. }
  120. /* Render layout in a specified format to an external context */
  121. int gvRenderContext(GVC_t *gvc, graph_t *g, const char *format, void *context)
  122. {
  123. int rc;
  124. GVJ_t *job;
  125. /* create a job for the required format */
  126. bool r = gvjobs_output_langname(gvc, format);
  127. job = gvc->job;
  128. if (!r) {
  129. agerrorf("Format: \"%s\" not recognized. Use one of:%s\n",
  130. format, gvplugin_list(gvc, API_device, format));
  131. return -1;
  132. }
  133. job->output_lang = gvrender_select(job, job->output_langname);
  134. if (!LAYOUT_DONE(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
  135. agerrorf( "Layout was not done\n");
  136. return -1;
  137. }
  138. job->context = context;
  139. job->external_context = true;
  140. rc = gvRenderJobs(gvc, g);
  141. gvrender_end_job(job);
  142. gvdevice_finalize(job);
  143. gvjobs_delete(gvc);
  144. return rc;
  145. }
  146. /* Render layout in a specified format to a malloc'ed string */
  147. int gvRenderData(GVC_t *gvc, graph_t *g, const char *format, char **result, unsigned int *length)
  148. {
  149. int rc;
  150. GVJ_t *job;
  151. /* create a job for the required format */
  152. bool r = gvjobs_output_langname(gvc, format);
  153. job = gvc->job;
  154. if (!r) {
  155. agerrorf("Format: \"%s\" not recognized. Use one of:%s\n",
  156. format, gvplugin_list(gvc, API_device, format));
  157. return -1;
  158. }
  159. job->output_lang = gvrender_select(job, job->output_langname);
  160. if (!LAYOUT_DONE(g) && !(job->flags & LAYOUT_NOT_REQUIRED)) {
  161. agerrorf( "Layout was not done\n");
  162. return -1;
  163. }
  164. /* page size on Linux, Mac OS X and Windows */
  165. #define OUTPUT_DATA_INITIAL_ALLOCATION 4096
  166. if(!result || !(*result = malloc(OUTPUT_DATA_INITIAL_ALLOCATION))) {
  167. agerrorf("failure malloc'ing for result string");
  168. return -1;
  169. }
  170. job->output_data = *result;
  171. job->output_data_allocated = OUTPUT_DATA_INITIAL_ALLOCATION;
  172. job->output_data_position = 0;
  173. rc = gvRenderJobs(gvc, g);
  174. gvrender_end_job(job);
  175. if (rc == 0) {
  176. *result = job->output_data;
  177. *length = job->output_data_position;
  178. }
  179. gvjobs_delete(gvc);
  180. return rc;
  181. }
  182. /* gvFreeRenderData:
  183. * Utility routine to free memory allocated in gvRenderData, as the application code may use
  184. * a different runtime library.
  185. */
  186. void gvFreeRenderData (char* data)
  187. {
  188. free (data);
  189. }
  190. void gvAddLibrary(GVC_t *gvc, gvplugin_library_t *lib)
  191. {
  192. gvconfig_plugin_install_from_library(gvc, NULL, lib);
  193. }
  194. char **gvcInfo(GVC_t* gvc) { return gvc->common.info; }
  195. char *gvcVersion(GVC_t* gvc) { return gvc->common.info[1]; }
  196. char *gvcBuildDate(GVC_t* gvc) { return gvc->common.info[2]; }
  197. /**
  198. * @dir lib/gvc
  199. * @brief Graphviz context library, API gvc.h
  200. *
  201. * [man 3 gvc](https://graphviz.org/pdf/gvc.3.pdf)
  202. *
  203. */