shape-description.cpp 9.9 KB

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