file-parser.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import * as ObjectClass from "./../enums/elements-class-enum";
  2. const TYPE = 0; // identifica se Ponto, Texto, Reta...
  3. const ID = 1; // identificador unico do objeto
  4. const DEFINITION = 2; // parametros dos quais o objeto depende (exemplo Reta depende de Ponto e Ponto => "2:IDPONTO1 IDPONTO2")
  5. const LIST = 3; // lista de identificadores de objetos dependentes do atual
  6. const LABEL = 4; // definicao do rotulo (valor/nome e se escondido ou nao)
  7. const DEFINED = 6; // 0 ou 1 (???)
  8. const COLOR = 5; // cor usando valor inteiro
  9. const HIDDEN = 7; // 0 (visivel) ou 1 (escondido)
  10. const PIXEL = 8; // LeScript usa (coordenada X e Y - inteiros)
  11. const FONT = 9; // 'Arial' T Tam: T = FONT.PLAIN, FONT.BOLD, FONT.ITALIC; Tam = 8, 9,...
  12. // PROIBIDO: "Arial" T Tam, pois em caso contrario nao sera lido como parametro
  13. // usar MARCAFNT = "\'" nao pode ser "\"" <[28/09/2006] agora vazio "">
  14. const LABEL_COLOR = 10; // cor de rotulo (tambem um inteiro) - inserido apos iGeom versao >= 2.9.9.31
  15. const OBJECT_LINE_REGEX = /^\{\w*(?:(?:\d+:@\|[^@]+@\|)|(?:\d+:[^,@|]+))\w(?:,\w*(?:(?:\d+:@\|[^@]+@\|)|(?:\d+:[^,@\|]+)))+\w*\}!?$/.compile();
  16. const ALPHA_MASK = 255 << 24;
  17. export class FileParser {
  18. // @param {string} content
  19. constructor (content) {
  20. this.content = content;
  21. }
  22. parse () {
  23. return this.content
  24. .split("\n")
  25. .filter(str => !str.match(/^[#\[]/))
  26. .map(str => {
  27. if (!OBJECT_LINE_REGEX.test(str.trim())) {
  28. console.error("Malformed object string in igeom file: ", str);
  29. return undefined;
  30. }
  31. return this.parseObjectLine(str.trim());
  32. });
  33. }
  34. // @param {string} object_line
  35. parseObjectLine (object_line) {
  36. const map = new Map();
  37. object_line = object_line.substring(1);
  38. if (object_line.endsWith("}!")) {
  39. object_line = object_line.substring(0, object_line.length - 2);
  40. } else {
  41. object_line = object_line.substring(0, object_line.length - 1);
  42. }
  43. const properties = object_line.split(",");
  44. for (const index in properties) {
  45. const prop = properties[index];
  46. const prop_info = prop.trim().split(":");
  47. const key = parseInt(prop_info[0]);
  48. if (prop_info[1] == undefined) continue;
  49. if (key == TYPE) {
  50. const type = prop_info[1];
  51. map.set("type", ObjectClass.fromCodeToClass(parseInt(type)));
  52. } else if (key == ID) {
  53. const id = prop_info[1];
  54. map.set("id", parseInt(id));
  55. } else if (key == DEFINITION) {
  56. var aux = "";
  57. const param_list = [];
  58. const list = prop_info[1].trim().split(" ");
  59. aux += "#prop_info[1]=" + list.length + ", ";
  60. var ii=0;
  61. for (const i in list) {
  62. const param = list[i];
  63. const maybe_number = Number(param);
  64. aux += "(" + ii + "," + param + ", " + maybe_number + "); ";
  65. ii++;
  66. if (Number.isNaN(maybe_number)) {
  67. param_list.push(param);
  68. } else {
  69. param_list.push(maybe_number);
  70. }
  71. }
  72. console.log("file-parser.js: DEFINITION: " + aux);
  73. map.set("param", param_list);
  74. } else if (key == LIST) {
  75. const id_list = [];
  76. const list = prop_info[1].trim().split(" ");
  77. for (const i in list) {
  78. const id = list[i];
  79. id_list.push(id);
  80. }
  81. map.set("deps", id_list);
  82. } else if (key == LABEL) {
  83. const param_list = [];
  84. const list = prop_info[1].trim().split(" ");
  85. for (const i in list) {
  86. const param = list[i];
  87. const maybe_number = Number(param);
  88. if (Number.isNaN(maybe_number)) {
  89. param_list.push(param);
  90. } else {
  91. param_list.push(maybe_number);
  92. }
  93. }
  94. map.set("label", param_list);
  95. } else if (key == DEFINED) {
  96. const value = prop_info[1];
  97. map.set("defined", parseInt(value));
  98. } else if (key == COLOR) {
  99. const value = prop_info[1];
  100. let number = Number(value);
  101. const alpha = ((number & ALPHA_MASK) >>> 24) / 255;
  102. number = number ^ ALPHA_MASK;
  103. const r = (number >> 16) & 0xff;
  104. const g = (number >> 8) & 0xff;
  105. const b = number & 0xff;
  106. map.set("color", { alpha: alpha, r: r, g: g, b: b });
  107. } else if (key == HIDDEN) {
  108. const value = prop_info[1];
  109. map.set("hidden", parseInt(value));
  110. } else if (key == PIXEL) {
  111. const position = [];
  112. const list = prop_info[1].trim().split(" ");
  113. for (const index in list) {
  114. const i = list[index];
  115. position.push(parseInt(i));
  116. }
  117. map.set("position", position);
  118. } else if (key == FONT) {
  119. const param_list = prop_info[1].trim().split(" ");
  120. map.set("font", param_list);
  121. } else if (key == LABEL_COLOR) {
  122. const value = prop_info[1];
  123. let number = Number(value);
  124. const alpha = ((number & ALPHA_MASK) >>> 24) / 255;
  125. number = number ^ ALPHA_MASK;
  126. const r = (number >> 16) & 0xff;
  127. const g = (number >> 8) & 0xff;
  128. const b = number & 0xff;
  129. map.set("label_color", { alpha: alpha, r: r, g: g, b: b });
  130. }
  131. }
  132. return map;
  133. }
  134. }