shape-description.cpp 9.6 KB

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