Joint.js

  1. const java = require('java');
  2. const fs = require("fs");
  3. const URL = require('url');
  4. const https = require('https');
  5. function isPath(image)
  6. {
  7. if (image.length < 256 && image.includes("/") || image.includes("\\"))
  8. {
  9. if (fs.existsSync(image))
  10. {
  11. return true;
  12. }
  13. throw new BarcodeException("Path " + image + " does not exist");
  14. }
  15. return false;
  16. }
  17. function isValidURL(str) {
  18. try {
  19. new URL(str);
  20. return true;
  21. } catch (err) {
  22. return false;
  23. }
  24. }
  25. function isBase64(str)
  26. {
  27. try
  28. {
  29. return (Buffer.from(str, 'base64').toString('base64')) === str;
  30. }
  31. catch (err)
  32. {
  33. return false;
  34. }
  35. }
  36. function convertImageResourceToBase64(imageResource)
  37. {
  38. if(imageResource === null)
  39. {
  40. return null;
  41. }
  42. else if ((typeof imageResource === 'string') && (isBase64(imageResource)))
  43. {
  44. return imageResource;
  45. }
  46. else if (fs.existsSync(imageResource))
  47. {
  48. return fs.readFileSync(imageResource).toString('base64');
  49. }
  50. throw new BarcodeException("Image is not available or this resouce is not supported!");
  51. }
  52. function convertDecodeTypeToFormattedDecodeType(decodeTypes)
  53. {
  54. if (decodeTypes === undefined || decodeTypes == null)
  55. {
  56. return [DecodeType.ALL_SUPPORTED_TYPES];
  57. }
  58. else
  59. {
  60. if(!Array.isArray(decodeTypes))
  61. decodeTypes = [decodeTypes];
  62. for (let i = 0; i < decodeTypes.length; i++)
  63. {
  64. if(!Number.isInteger(decodeTypes[i]))
  65. throw new Error("Unsuported decodeType format");
  66. }
  67. return decodeTypes;
  68. }
  69. }
  70. function convertAreasToStringFormattedAreas(areas) // TODO move to Joint
  71. {
  72. let stringFormattedAreas = null;
  73. if (areas != null)
  74. {
  75. stringFormattedAreas = [];
  76. if(Array.isArray(areas))
  77. {
  78. if(!areas.every(area => area === null))
  79. {
  80. for(let i = 0; i < areas.length; i++)
  81. {
  82. let area = areas[i];
  83. if (area === null || !(area instanceof Rectangle))
  84. throw new BarcodeException('All elements of areas should be instances of Rectangle class');
  85. stringFormattedAreas.push(area.toString());
  86. }
  87. }
  88. }
  89. else
  90. {
  91. if (!(areas instanceof Rectangle))
  92. throw new BarcodeException('All elements of areas should be instances of Rectangle class');
  93. stringFormattedAreas.push(areas.toString());
  94. }
  95. stringFormattedAreas = stringFormattedAreas.length > 0 ? stringFormattedAreas : null
  96. }
  97. return stringFormattedAreas;
  98. }
  99. class BaseJavaClass
  100. {
  101. javaClass;
  102. javaClassName;
  103. constructor(javaClass)
  104. {
  105. this.javaClass = javaClass;
  106. if (this.javaClassName == null || this.javaClassName === "")
  107. {
  108. this.javaClassName = this.javaClass.__signature;
  109. }
  110. }
  111. init()
  112. {
  113. throw new BarcodeException('You have to implement the method init!');
  114. }
  115. /**
  116. * @return mixed
  117. */
  118. getJavaClass()
  119. {
  120. return this.javaClass;
  121. }
  122. setJavaClass(javaClass)
  123. {
  124. this.javaClass = javaClass;
  125. this.init();
  126. }
  127. getJavaClassName()
  128. {
  129. return this.javaClassName;
  130. }
  131. isNull()
  132. {
  133. return java_cast(this.javaClass.isNull(), "boolean");
  134. }
  135. printJavaClassName()
  136. {
  137. console.log("Java class name => '" + this.javaClassName + "'");
  138. }
  139. }
  140. /**
  141. * Provides methods to license the component.
  142. */
  143. class License extends BaseJavaClass
  144. {
  145. static get javaClassName()
  146. {
  147. return "com.aspose.nodejs.barcode.license.NodejsLicense"
  148. };
  149. /**
  150. * Initializes a new instance of this class.
  151. */
  152. constructor()
  153. {
  154. let javaLicense = java.import(License.javaClassName);
  155. super(new javaLicense());
  156. }
  157. /**
  158. * Licenses the component.
  159. *
  160. * @param licensePath path to license file
  161. */
  162. setLicense(licensePath)
  163. {
  164. try
  165. {
  166. let file_data = License.openFile(licensePath);
  167. this.getJavaClass().setLicenseSync(file_data);
  168. } catch (ex)
  169. {
  170. throw new BarcodeException(ex);
  171. }
  172. }
  173. resetLicense()
  174. {
  175. try
  176. {
  177. let javaClass = this.getJavaClass();
  178. javaClass.resetLicenseSync();
  179. } catch (ex)
  180. {
  181. throw new BarcodeException(ex);
  182. }
  183. }
  184. isLicensed()
  185. {
  186. let is_licensed = this.getJavaClass().isLicensedSync();
  187. return is_licensed.toString();
  188. }
  189. static openFile(filename)
  190. {
  191. let buffer = Buffer.from(fs.readFileSync(filename, 'utf8'));
  192. let array = [];
  193. array.push('');
  194. for (let i = 0; i < buffer.length; i++)
  195. {
  196. array.push(buffer[i] + '');
  197. }
  198. return array;
  199. }
  200. init()
  201. {
  202. // do nothing
  203. }
  204. }
  205. /**
  206. * Class BarcodeException
  207. */
  208. class BarcodeException extends Error
  209. {
  210. static get MAX_LINES()
  211. {
  212. return 4;
  213. };
  214. /**
  215. * BarcodeException constructor.
  216. * @param exc exception's instance
  217. */
  218. constructor(exc)
  219. {
  220. super();
  221. if ((typeof exc.toString()) === 'string')
  222. {
  223. this.setMessage(exc.toString());
  224. return;
  225. }
  226. let exc_message = "Exception occured in file:line" + nl;
  227. this.setMessage(exc_message);
  228. }
  229. getDetails(exc)
  230. {
  231. let details = "";
  232. if (typeof exc === 'string' || exc instanceof String)
  233. {
  234. return exc;
  235. }
  236. if (get_class(exc) != null)
  237. {
  238. details = "exception type : " + get_class(exc) + "\n";
  239. }
  240. if (method_exists(exc, "__toString"))
  241. {
  242. details += exc.__toString();
  243. }
  244. if (method_exists(exc, "getMessage"))
  245. {
  246. details += exc.getMessage();
  247. }
  248. if (method_exists(exc, "getCause"))
  249. {
  250. details += exc.getCause();
  251. }
  252. return details;
  253. }
  254. /**
  255. * @param mixed message
  256. */
  257. setMessage(message)
  258. {
  259. this.message = message;
  260. }
  261. }
  262. class Rectangle extends BaseJavaClass
  263. {
  264. /**
  265. * @return {Rectangle} empty Rectangle
  266. */
  267. static get EMPTY()
  268. {
  269. return new Rectangle(0, 0,0,0);
  270. }
  271. init()
  272. {
  273. }
  274. static get javaClassName()
  275. {
  276. return "java.awt.Rectangle";
  277. }
  278. /**
  279. * Rectangle constructor.
  280. * @param x
  281. * @param y
  282. * @param width
  283. * @param height
  284. */
  285. constructor(x, y, width, height)
  286. {
  287. let java_class_link = java.import(Rectangle.javaClassName);
  288. let java_class = new java_class_link(x, y, width, height);
  289. super(java_class);
  290. }
  291. static construct(...args)
  292. {
  293. let rectangle = this.EMPTY;
  294. rectangle.setJavaClass(args[0]);
  295. return rectangle;
  296. }
  297. /**
  298. * @return X
  299. */
  300. getX()
  301. {
  302. return this.getJavaClass().getXSync();
  303. }
  304. /**
  305. * @return Y
  306. */
  307. getY()
  308. {
  309. return this.getJavaClass().getYSync();
  310. }
  311. /**
  312. * @return Left
  313. */
  314. getLeft()
  315. {
  316. return this.getX();
  317. }
  318. /**
  319. * @return Top
  320. */
  321. getTop()
  322. {
  323. return this.getY();
  324. }
  325. /**
  326. * @return Right
  327. */
  328. getRight()
  329. {
  330. return this.getX() + this.getWidth();
  331. }
  332. /**
  333. * @return Bottom
  334. */
  335. getBottom()
  336. {
  337. return this.getY() + this.getHeight();
  338. }
  339. /**
  340. * @return Width
  341. */
  342. getWidth()
  343. {
  344. return this.getJavaClass().getWidthSync();
  345. }
  346. /**
  347. * @return Height
  348. */
  349. getHeight()
  350. {
  351. return this.getJavaClass().getHeightSync();
  352. }
  353. toString()
  354. {
  355. return this.getX() + ',' + this.getY() + ',' + this.getWidth() + ',' + this.getHeight();
  356. }
  357. equals(obj)
  358. {
  359. return this.getJavaClass().equalsSync(obj.getJavaClass());
  360. }
  361. /**
  362. * Determines if this rectangle intersects with rect.
  363. * @param rectangle
  364. * @returns {boolean}
  365. */
  366. intersectsWithInclusive(rectangle)
  367. {
  368. return !((this.getLeft() > rectangle.getRight()) || (this.getRight() < rectangle.getLeft()) ||
  369. (this.getTop() > rectangle.getBottom()) || (this.getBottom() < rectangle.getTop()));
  370. }
  371. /**
  372. * Intersect Shared Method
  373. * Produces a new Rectangle by intersecting 2 existing
  374. * Rectangles. Returns null if there is no intersection.
  375. */
  376. static intersect(a, b)
  377. {
  378. if (!a.intersectsWithInclusive(b))
  379. {
  380. return new Rectangle(0, 0, 0, 0);
  381. }
  382. return Rectangle.fromLTRB(Math.max(a.getLeft(), b.getLeft()),
  383. Math.max(a.getTop(), b.getTop()),
  384. Math.min(a.getRight(), b.getRight()),
  385. Math.min(a.getBottom(), b.getBottom()));
  386. }
  387. /**
  388. * FromLTRB Shared Method
  389. * Produces a Rectangle class from left, top, right,
  390. * and bottom coordinates.
  391. */
  392. static fromLTRB(left, top, right, bottom)
  393. {
  394. return new Rectangle(left, top, right - left, bottom - top);
  395. }
  396. isEmpty()
  397. {
  398. return (this.getWidth() <= 0) || (this.getHeight() <= 0);
  399. }
  400. }
  401. /**
  402. * Represents Point class
  403. */
  404. class Point extends BaseJavaClass
  405. {
  406. static get javaClassName()
  407. {
  408. return "java.awt.Point";
  409. }
  410. /**
  411. * Represents an empty Point
  412. */
  413. static get EMPTY()
  414. {
  415. return new Point(0, 0);
  416. }
  417. /**
  418. * Point constructor.
  419. * @param x
  420. * @param y
  421. */
  422. constructor(x, y)
  423. {
  424. let java_class_link = java.import(Point.javaClassName);
  425. let java_class = new java_class_link(x, y);
  426. super(java_class);
  427. }
  428. static construct(...args)
  429. {
  430. let point = Point.EMPTY;
  431. point.setJavaClass(args[0]);
  432. return point;
  433. }
  434. init()
  435. {
  436. }
  437. /**
  438. * @return X
  439. */
  440. getX()
  441. {
  442. return this.getJavaClass().getXSync();
  443. }
  444. /**
  445. * @return Y
  446. */
  447. getY()
  448. {
  449. return this.getJavaClass().getYSync();
  450. }
  451. /**
  452. * @param x X
  453. */
  454. setX(x)
  455. {
  456. this.getJavaClass().xSync = x;
  457. }
  458. /**
  459. * @param y Y
  460. */
  461. setY(y)
  462. {
  463. this.getJavaClass().ySync = y;
  464. }
  465. toString()
  466. {
  467. return this.getX() + ',' + this.getY();
  468. }
  469. equals(obj)
  470. {
  471. return this.getJavaClass().equalsSync(obj.getJavaClass());
  472. }
  473. }
  474. class BuildVersionInfo
  475. {
  476. static get javaClassName()
  477. {
  478. return "com.aspose.barcode.BuildVersionInfo";
  479. }
  480. static get javaClass()
  481. {
  482. let java_class_link = java.import(BuildVersionInfo.javaClassName);
  483. return java_class_link;
  484. }
  485. static get product()
  486. {
  487. return BuildVersionInfo.javaClass.PRODUCT;
  488. }
  489. static get assemblyVersion()
  490. {
  491. return BuildVersionInfo.javaClass.ASSEMBLY_VERSION;
  492. }
  493. }
  494. module.exports = {
  495. BaseJavaClass, BarcodeException, Rectangle, Point, License, BuildVersionInfo, isPath, convertImageResourceToBase64,
  496. convertDecodeTypeToFormattedDecodeType, convertAreasToStringFormattedAreas
  497. };