shape-description.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #ifndef _CRT_SECURE_NO_WARNINGS
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #endif
  4. #include "shape-description.h"
  5. #include <cstdlib>
  6. namespace msdfgen {
  7. int readCharF(FILE *input) {
  8. int c = '\0';
  9. do {
  10. c = fgetc(input);
  11. } while (c == ' ' || c == '\t' || c == '\r' || c == '\n');
  12. return c;
  13. }
  14. int readCharS(const char **input) {
  15. int c = '\0';
  16. do {
  17. c = *(*input)++;
  18. } while (c == ' ' || c == '\t' || c == '\r' || c == '\n');
  19. if (!c) {
  20. --c;
  21. return EOF;
  22. }
  23. return c;
  24. }
  25. int readCoordF(FILE *input, Point2 &coord) {
  26. return fscanf(input, "%lf , %lf", &coord.x, &coord.y);
  27. }
  28. int readCoordS(const char **input, Point2 &coord) {
  29. char *end = NULL;
  30. coord.x = strtod(*input, &end);
  31. if (end <= *input)
  32. return 0;
  33. *input = end;
  34. while (**input == ' ' || **input == '\t' || **input == '\n' || **input == '\r')
  35. ++*input;
  36. if (**input != ',')
  37. return 1;
  38. ++*input;
  39. coord.y = strtod(*input, &end);
  40. if (end <= *input)
  41. return 1;
  42. *input = end;
  43. return 2;
  44. }
  45. bool matchStringS(const char **input, const char *str) {
  46. const char *cur = *input;
  47. while (*cur && *str && *cur == *str)
  48. ++cur, ++str;
  49. if (!*str) {
  50. *input = cur;
  51. return true;
  52. }
  53. return false;
  54. }
  55. static bool writeCoord(FILE *output, Point2 coord) {
  56. fprintf(output, "%.12g, %.12g", coord.x, coord.y);
  57. return true;
  58. }
  59. template <typename T, int (*readChar)(T *), int (*readCoord)(T *, Point2 &)>
  60. static int readControlPoints(T *input, Point2 *output) {
  61. int result = readCoord(input, output[0]);
  62. if (result == 2) {
  63. switch (readChar(input)) {
  64. case ')':
  65. return 1;
  66. case ';':
  67. break;
  68. default:
  69. return -1;
  70. }
  71. result = readCoord(input, output[1]);
  72. if (result == 2 && readChar(input) == ')')
  73. return 2;
  74. } else if (result != 1 && readChar(input) == ')')
  75. return 0;
  76. return -1;
  77. }
  78. template <typename T, int (*readChar)(T *), int (*readCoord)(T *, Point2 &)>
  79. static bool readContour(T *input, Contour &output, const Point2 *first, int terminator, bool &colorsSpecified) {
  80. Point2 p[4], start;
  81. if (first)
  82. p[0] = *first;
  83. else {
  84. int result = readCoord(input, p[0]);
  85. if (result != 2)
  86. return result != 1 && readChar(input) == terminator;
  87. }
  88. start = p[0];
  89. int c = '\0';
  90. while ((c = readChar(input)) != terminator) {
  91. if (c != ';')
  92. return false;
  93. EdgeColor color = WHITE;
  94. int result = readCoord(input, p[1]);
  95. if (result == 2) {
  96. output.addEdge(EdgeHolder(p[0], p[1], color));
  97. p[0] = p[1];
  98. continue;
  99. } else if (result == 1)
  100. return false;
  101. else {
  102. int controlPoints = 0;
  103. switch ((c = readChar(input))) {
  104. case '#':
  105. output.addEdge(EdgeHolder(p[0], start, color));
  106. p[0] = start;
  107. continue;
  108. case ';':
  109. goto FINISH_EDGE;
  110. case '(':
  111. goto READ_CONTROL_POINTS;
  112. case 'C': case 'c':
  113. color = CYAN;
  114. colorsSpecified = true;
  115. break;
  116. case 'M': case 'm':
  117. color = MAGENTA;
  118. colorsSpecified = true;
  119. break;
  120. case 'Y': case 'y':
  121. color = YELLOW;
  122. colorsSpecified = true;
  123. break;
  124. case 'W': case 'w':
  125. color = WHITE;
  126. colorsSpecified = true;
  127. break;
  128. default:
  129. return c == terminator;
  130. }
  131. switch (readChar(input)) {
  132. case ';':
  133. goto FINISH_EDGE;
  134. case '(':
  135. READ_CONTROL_POINTS:
  136. if ((controlPoints = readControlPoints<T, readChar, readCoord>(input, p+1)) < 0)
  137. return false;
  138. break;
  139. default:
  140. return false;
  141. }
  142. if (readChar(input) != ';')
  143. return false;
  144. FINISH_EDGE:
  145. result = readCoord(input, p[1+controlPoints]);
  146. if (result != 2) {
  147. if (result == 1)
  148. return false;
  149. else {
  150. if (readChar(input) == '#')
  151. p[1+controlPoints] = start;
  152. else
  153. return false;
  154. }
  155. }
  156. switch (controlPoints) {
  157. case 0:
  158. output.addEdge(EdgeHolder(p[0], p[1], color));
  159. p[0] = p[1];
  160. continue;
  161. case 1:
  162. output.addEdge(EdgeHolder(p[0], p[1], p[2], color));
  163. p[0] = p[2];
  164. continue;
  165. case 2:
  166. output.addEdge(EdgeHolder(p[0], p[1], p[2], p[3], color));
  167. p[0] = p[3];
  168. continue;
  169. }
  170. }
  171. }
  172. return true;
  173. }
  174. bool readShapeDescription(FILE *input, Shape &output, bool *colorsSpecified) {
  175. bool locColorsSpec = false;
  176. output.contours.clear();
  177. output.setYAxisOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION);
  178. Point2 p;
  179. int result = readCoordF(input, p);
  180. if (result == 2) {
  181. return readContour<FILE, readCharF, readCoordF>(input, output.addContour(), &p, EOF, locColorsSpec);
  182. } else if (result == 1)
  183. return false;
  184. else {
  185. int c = readCharF(input);
  186. if (c == '@') {
  187. char after = '\0';
  188. if (fscanf(input, "y-%c", &after) == 1 && (after == 'u' || after == 'd')) {
  189. switch (after) {
  190. case 'u':
  191. output.setYAxisOrientation(Y_UPWARD);
  192. break;
  193. case 'd':
  194. output.setYAxisOrientation(Y_DOWNWARD);
  195. break;
  196. }
  197. if (fscanf(input, after == 'u' ? "p%c" : "own%c", &after) != 1)
  198. return feof(input) != 0;
  199. } else if (fscanf(input, "invert-y%c", &after) == 1)
  200. output.inverseYAxis = true;
  201. else
  202. return feof(input) != 0;
  203. c = after;
  204. if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
  205. c = readCharF(input);
  206. }
  207. for (; c == '{'; c = readCharF(input))
  208. if (!readContour<FILE, readCharF, readCoordF>(input, output.addContour(), NULL, '}', locColorsSpec))
  209. return false;
  210. if (colorsSpecified)
  211. *colorsSpecified = locColorsSpec;
  212. return c == EOF && feof(input);
  213. }
  214. }
  215. bool readShapeDescription(const char *input, Shape &output, bool *colorsSpecified) {
  216. bool locColorsSpec = false;
  217. output.contours.clear();
  218. output.setYAxisOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION);
  219. Point2 p;
  220. int result = readCoordS(&input, p);
  221. if (result == 2) {
  222. return readContour<const char *, readCharS, readCoordS>(&input, output.addContour(), &p, EOF, locColorsSpec);
  223. } else if (result == 1)
  224. return false;
  225. else {
  226. int c = readCharS(&input);
  227. if (c == '@') {
  228. if (matchStringS(&input, "y-down"))
  229. output.setYAxisOrientation(Y_DOWNWARD);
  230. else if (matchStringS(&input, "y-up"))
  231. output.setYAxisOrientation(Y_UPWARD);
  232. else if (matchStringS(&input, "invert-y"))
  233. output.inverseYAxis = true;
  234. else
  235. return false;
  236. c = readCharS(&input);
  237. }
  238. for (; c == '{'; c = readCharS(&input))
  239. if (!readContour<const char *, readCharS, readCoordS>(&input, output.addContour(), NULL, '}', locColorsSpec))
  240. return false;
  241. if (colorsSpecified)
  242. *colorsSpecified = locColorsSpec;
  243. return c == EOF;
  244. }
  245. }
  246. static bool isColored(const Shape &shape) {
  247. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
  248. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge)
  249. if ((*edge)->color != WHITE)
  250. return true;
  251. return false;
  252. }
  253. bool writeShapeDescription(FILE *output, const Shape &shape) {
  254. if (!shape.validate())
  255. return false;
  256. bool writeColors = isColored(shape);
  257. switch (shape.getYAxisOrientation()) {
  258. case Y_UPWARD:
  259. fprintf(output, "@y-up\n");
  260. break;
  261. case Y_DOWNWARD:
  262. fprintf(output, "@y-down\n");
  263. break;
  264. }
  265. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) {
  266. fprintf(output, "{\n");
  267. if (!contour->edges.empty()) {
  268. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  269. char colorCode = '\0';
  270. if (writeColors) {
  271. switch ((*edge)->color) {
  272. case YELLOW: colorCode = 'y'; break;
  273. case MAGENTA: colorCode = 'm'; break;
  274. case CYAN: colorCode = 'c'; break;
  275. case WHITE: colorCode = 'w'; break;
  276. default:;
  277. }
  278. }
  279. const Point2 *p = (*edge)->controlPoints();
  280. switch ((*edge)->type()) {
  281. case (int) LinearSegment::EDGE_TYPE:
  282. fprintf(output, "\t");
  283. writeCoord(output, p[0]);
  284. fprintf(output, ";\n");
  285. if (colorCode)
  286. fprintf(output, "\t\t%c;\n", colorCode);
  287. break;
  288. case (int) QuadraticSegment::EDGE_TYPE:
  289. fprintf(output, "\t");
  290. writeCoord(output, p[0]);
  291. fprintf(output, ";\n\t\t");
  292. if (colorCode)
  293. fprintf(output, "%c", colorCode);
  294. fprintf(output, "(");
  295. writeCoord(output, p[1]);
  296. fprintf(output, ");\n");
  297. break;
  298. case (int) CubicSegment::EDGE_TYPE:
  299. fprintf(output, "\t");
  300. writeCoord(output, p[0]);
  301. fprintf(output, ";\n\t\t");
  302. if (colorCode)
  303. fprintf(output, "%c", colorCode);
  304. fprintf(output, "(");
  305. writeCoord(output, p[1]);
  306. fprintf(output, "; ");
  307. writeCoord(output, p[2]);
  308. fprintf(output, ");\n");
  309. break;
  310. }
  311. }
  312. fprintf(output, "\t#\n");
  313. }
  314. fprintf(output, "}\n");
  315. }
  316. return true;
  317. }
  318. }