2
0

shape-description.cpp 9.9 KB

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