2
0

shape-description.cpp 10 KB

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