xdot.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @file
  3. * @ingroup public_apis
  4. * @brief parsing and deparsing of [xdot](https://graphviz.org/docs/outputs/canon/#xdot) operations
  5. *
  6. * **libxdot** provides support for parsing and deparsing graphical operations specified by the xdot language.
  7. * [xdot](https://graphviz.org/docs/outputs/canon/#xdot) is extended dot format containing complete layout information.
  8. *
  9. * [man 3 xdot](https://graphviz.org/pdf/xdot.3.pdf)
  10. *
  11. */
  12. /*************************************************************************
  13. * Copyright (c) 2011 AT&T Intellectual Property
  14. * All rights reserved. This program and the accompanying materials
  15. * are made available under the terms of the Eclipse Public License v1.0
  16. * which accompanies this distribution, and is available at
  17. * https://www.eclipse.org/legal/epl-v10.html
  18. *
  19. * Contributors: Details at https://graphviz.org
  20. *************************************************************************/
  21. #pragma once
  22. #include <stddef.h>
  23. #include <stdio.h>
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #ifdef GVDLL
  28. #ifdef EXPORT_XDOT
  29. #define XDOT_API __declspec(dllexport)
  30. #else
  31. #define XDOT_API __declspec(dllimport)
  32. #endif
  33. #endif
  34. #ifndef XDOT_API
  35. #define XDOT_API /* nothing */
  36. #endif
  37. typedef enum {
  38. xd_none,
  39. xd_linear,
  40. xd_radial
  41. } xdot_grad_type;
  42. typedef struct {
  43. float frac;
  44. char* color;
  45. } xdot_color_stop;
  46. typedef struct {
  47. double x0, y0;
  48. double x1, y1;
  49. int n_stops;
  50. xdot_color_stop* stops;
  51. } xdot_linear_grad;
  52. typedef struct {
  53. double x0, y0, r0;
  54. double x1, y1, r1;
  55. int n_stops;
  56. xdot_color_stop* stops;
  57. } xdot_radial_grad;
  58. typedef struct {
  59. xdot_grad_type type;
  60. union {
  61. char* clr;
  62. xdot_linear_grad ling;
  63. xdot_radial_grad ring;
  64. } u;
  65. } xdot_color;
  66. typedef enum {
  67. xd_left, xd_center, xd_right
  68. } xdot_align;
  69. typedef struct {
  70. double x, y, z;
  71. } xdot_point;
  72. typedef struct {
  73. double x, y, w, h;
  74. } xdot_rect;
  75. typedef struct {
  76. size_t cnt;
  77. xdot_point* pts;
  78. } xdot_polyline;
  79. typedef struct {
  80. double x, y;
  81. xdot_align align;
  82. double width;
  83. char* text;
  84. } xdot_text;
  85. typedef struct {
  86. xdot_rect pos;
  87. char* name;
  88. } xdot_image;
  89. typedef struct {
  90. double size;
  91. char* name;
  92. } xdot_font;
  93. typedef enum {
  94. xd_filled_ellipse, xd_unfilled_ellipse,
  95. xd_filled_polygon, xd_unfilled_polygon,
  96. xd_filled_bezier, xd_unfilled_bezier,
  97. xd_polyline, xd_text,
  98. xd_fill_color, xd_pen_color, xd_font, xd_style, xd_image,
  99. xd_grad_fill_color, xd_grad_pen_color,
  100. xd_fontchar
  101. } xdot_kind;
  102. typedef enum {
  103. xop_ellipse,
  104. xop_polygon,
  105. xop_bezier,
  106. xop_polyline, xop_text,
  107. xop_fill_color, xop_pen_color, xop_font, xop_style, xop_image,
  108. xop_grad_color,
  109. xop_fontchar
  110. } xop_kind;
  111. typedef struct _xdot_op xdot_op;
  112. typedef void (*drawfunc_t)(xdot_op*, int);
  113. typedef void (*freefunc_t)(xdot_op*);
  114. struct _xdot_op {
  115. xdot_kind kind;
  116. union {
  117. xdot_rect ellipse; /* xd_filled_ellipse, xd_unfilled_ellipse */
  118. xdot_polyline polygon; /* xd_filled_polygon, xd_unfilled_polygon */
  119. xdot_polyline polyline; /* xd_polyline */
  120. xdot_polyline bezier; /* xd_filled_bezier, xd_unfilled_bezier */
  121. xdot_text text; /* xd_text */
  122. xdot_image image; /* xd_image */
  123. char* color; /* xd_fill_color, xd_pen_color */
  124. xdot_color grad_color; /* xd_grad_fill_color, xd_grad_pen_color */
  125. xdot_font font; /* xd_font */
  126. char* style; /* xd_style */
  127. unsigned int fontchar; /* xd_fontchar */
  128. } u;
  129. drawfunc_t drawfunc;
  130. };
  131. #define XDOT_PARSE_ERROR 1
  132. typedef struct {
  133. size_t cnt; /* no. of xdot ops */
  134. size_t sz; /* sizeof structure containing xdot_op as first field */
  135. xdot_op* ops;
  136. freefunc_t freefunc;
  137. int flags;
  138. } xdot;
  139. typedef struct {
  140. size_t cnt; /* no. of xdot ops */
  141. size_t n_ellipse;
  142. size_t n_polygon;
  143. size_t n_polygon_pts;
  144. size_t n_polyline;
  145. size_t n_polyline_pts;
  146. size_t n_bezier;
  147. size_t n_bezier_pts;
  148. size_t n_text;
  149. size_t n_font;
  150. size_t n_style;
  151. size_t n_color;
  152. size_t n_image;
  153. size_t n_gradcolor;
  154. size_t n_fontchar;
  155. } xdot_stats;
  156. /* ops are indexed by xop_kind */
  157. XDOT_API xdot *parseXDotF(char*, drawfunc_t opfns[], size_t sz);
  158. XDOT_API xdot *parseXDotFOn(char*, drawfunc_t opfns[], size_t sz, xdot*);
  159. XDOT_API xdot* parseXDot (char*);
  160. XDOT_API char* sprintXDot (xdot*);
  161. XDOT_API void fprintXDot (FILE*, xdot*);
  162. XDOT_API void jsonXDot (FILE*, xdot*);
  163. XDOT_API void freeXDot (xdot*);
  164. XDOT_API int statXDot (xdot*, xdot_stats*);
  165. XDOT_API xdot_grad_type colorTypeXDot (char*);
  166. XDOT_API char* parseXDotColor (char* cp, xdot_color* clr);
  167. XDOT_API void freeXDotColor (xdot_color*);
  168. #ifdef __cplusplus
  169. }
  170. #endif