2
0

shape-description.cpp 9.8 KB

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