Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. #pragma once
  2.  
  3. float min(float a, float b) {
  4. if (a - b > 0)
  5. return b;
  6. else
  7. return a;
  8. }
  9.  
  10.  
  11.  
  12. void SvgWork(string input, string output, float kx, float ky)
  13. {
  14. ifstream
  15. fin;
  16. string
  17. tag_name,
  18. okey_tag = "<svg",
  19. ckey_tag = "</sv",
  20. content = "",
  21. final = "",
  22. word = "";
  23.  
  24. char
  25. ch;
  26. int
  27. i = 0,
  28. N = 4;
  29. bool
  30. rect1 = false,
  31. space = false,
  32. is = false,
  33. viewBoxcheck=false,
  34. header = false,
  35. dt1 = false,
  36. uslovie = false,
  37. dt2 = false,
  38. svg = false;
  39.  
  40. fin.open(input);
  41. FILE *fout;
  42. string outputer(output);
  43. char *outfile = new char[output.length() + 1];
  44. outfile = stoch(output);
  45. fopen_s(&fout, outfile, "w");
  46.  
  47. while (fin.get(ch))
  48. {
  49.  
  50. /* Фильтр на тег svg */
  51. if (ch == '<')
  52. rect1 = true;
  53. if (ch == '>')
  54. rect1 = false;
  55. if (rect1)
  56. {
  57. if (i < N)
  58. {
  59. tag_name += ch;
  60. ++i;
  61. }
  62. else
  63. {
  64. i = 0;
  65. if (tag_name == okey_tag)
  66. svg = true;
  67. if (tag_name == ckey_tag)
  68. svg = false;
  69. tag_name = "";
  70. rect1 = false;
  71. }
  72. }
  73.  
  74.  
  75. if (svg)
  76. {
  77. /* Фильтр на атрибут */
  78. if (ch == ' ') {
  79. space = true;
  80. header = false;
  81. }
  82. if (ch == '=')
  83. is = true;
  84. if (ch == '\"' && dt1)
  85. dt2 = true;
  86. if ((ch == '<') || (ch == '>')) {
  87. space = is = dt1 = dt2 = false;
  88. word = "";
  89. }
  90. if (ch == '\"' && !dt1)
  91. {
  92. dt1 = true;
  93. space = is = false;
  94. }
  95.  
  96. if (ch == '<') {
  97. header = true;
  98. }
  99. if (ch == '>') {
  100. fprintf(fout, "%s", ">");
  101. //cout << '>';
  102. }
  103. if (header && !space && (ch != '>')) {
  104. fprintf(fout, "%c", ch);
  105. //cout << ch;
  106. }
  107.  
  108. /* Атрибут */
  109. if (space && !is && !dt1 && !dt2) {
  110. word += ch;
  111.  
  112. }
  113.  
  114. //ТУТ АТРИБУТЫ ГОТОВЫ
  115. if (is) {
  116.  
  117. fprintf(fout, "%s", stoch(word));
  118. fprintf(fout, "%s", "=\"");
  119. //cout << word<<"=\"";
  120. }
  121.  
  122. if (sravnenie(word) && is) {
  123. uslovie = true;
  124. if (word == " viewBox")
  125. viewBoxcheck = true;
  126. //cout << word<<endl;
  127. }
  128. if (uslovie) {
  129. if (dt1 && !dt2 && (ch != '\"'))
  130. content += ch;
  131. if (dt1&&dt2) {
  132. if (viewBoxcheck) {
  133. string z = viewBoxout(content, kx, ky);
  134. char *outword = new char[z.length() + 1];
  135. outword = stoch(z);
  136. cout << outword<<endl;
  137. fprintf(fout, "%s", outword);
  138.  
  139. viewBoxcheck = false;
  140. }
  141. else {
  142. fprintf(fout, "%s", stoch(DelFloat(changer(kx, ky, content))));
  143. fprintf(fout, "%s", "\"");
  144. //cout << stoch(DelFloat(changer(kx, ky, content))) << endl;
  145. content = "";
  146. uslovie = false;
  147. viewBoxcheck = false;
  148. }
  149. }
  150.  
  151. }
  152. else {
  153.  
  154. if (dt1 && !dt2 && (ch != '\"'))
  155. content += ch;
  156. if (dt1&&dt2) {
  157. fprintf(fout, "%s", stoch(DelFloat(content)));
  158. fprintf(fout, "%s", "\"");
  159. //cout << content << "\"";
  160. content = "";
  161. }
  162. }
  163.  
  164. /* Обнуляем условия */
  165. if (dt2) {
  166. //cout << word << endl;
  167. space = is = dt1 = dt2 = false;
  168. word = "";
  169. }
  170. }
  171. else {
  172. fprintf(fout, "%c", ch);
  173. //cout << ch;
  174. }
  175.  
  176.  
  177.  
  178. }
  179.  
  180. fin.close();
  181. fclose(fout);
  182. }
  183.  
  184.  
  185. string viewBoxout(string content, float kx, float ky) {
  186. int probels = 0;
  187. string qq1, cont1, cont2, cont3, cont4 = "";
  188. for (int i = 0; i < content.length(); i++) {
  189. if ((probels == 0)&&(content[i]!=' '))
  190. cont1 += content[i];
  191. if ((probels == 1) && (content[i] != ' '))
  192. cont2 += content[i];
  193. if ((probels == 2) && (content[i] != ' '))
  194. cont3 += content[i];
  195. if ((probels == 3) && (content[i] != ' '))
  196. cont4 += content[i];
  197. if (content[i] == ' ')
  198. probels++;
  199. }
  200. cont1 = (DelFloat(changer(kx, ky, cont1)));
  201. cont2 = (DelFloat(changer(kx, ky, cont2)));
  202. cont3 = (DelFloat(changer(kx, ky, cont3))) + " ";
  203. cont4 = (DelFloat(changer(kx, ky, cont4)));
  204. //cout << cont1;
  205. //cout << cont2;
  206. //cout << cont3;
  207. //cout << cont4 << endl;
  208. qq1 = cont1 + cont2 + cont3 + cont4;
  209. //cout << qq1<<endl;
  210. return qq1;
  211. }
  212.  
  213. bool sravnenie(string word) {
  214. if ((word == " cx") || (word == " cy") || (word == " rx") || (word == " ry") || (word == " viewBox") || (word == " r") || (word == " x1") || (word == " x2") || (word == " y1") || (word == " y2") || (word == " width") || (word == " height")) {
  215. return true;
  216. }
  217. else {
  218. return false;
  219. }
  220. }
  221.  
  222. string symbols(string input, int dlina) {
  223. string symbbolstogo = "";
  224. for (int i = 0; i < dlina; ++i) { //проходим по строке и записываем в worder все, кроме цифр (выделяем буквы)
  225. if ((input[i] < '0') || (input[i] > '9'))
  226. symbbolstogo += input[i];
  227.  
  228. }
  229. return symbbolstogo;
  230. }
  231.  
  232.  
  233. string changer(float kx, float ky, string param) {
  234. float koeff = min(kx, ky); //определяем нужный коэффицент
  235. //cout << koeff << endl;
  236. int dlina = param.length(); //определяем длину
  237. string worder = ""; // пустая строка для записи букв после числа
  238. float temp = (float)atof(param.c_str()); // конвертируем в числу данную нам строку
  239. if ((param[dlina - 1] != '%') && ((temp > 1) || (temp < (-1)))) { //если нет процентов и модуль числа больше единицы, то меняем значение
  240. worder = symbols(param, dlina);
  241. temp = koeff * temp;
  242. string qq = to_string(temp); //конвертируем
  243. qq += worder;
  244. return qq;
  245.  
  246. }
  247. else { // если есть процент или модуль числа меньше единицы, то просто возвращаем строку
  248. return param;
  249. }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement