shape-description.cpp 10.0 KB

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