index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const Fs = require('fs');
  2. const { dialog } = require('electron').remote;
  3. Editor.Panel.extend({
  4. style: Fs.readFileSync(Editor.url('packages://bitmapfont/panel/index.css', 'utf8')),
  5. template: Fs.readFileSync(Editor.url('packages://bitmapfont/panel/index.html', 'utf8')),
  6. $: {
  7. fontCanvas: "#fontCanvas"
  8. },
  9. ready() {
  10. let self = this;
  11. this.plugin = new window.Vue({
  12. el: this.shadowRoot,
  13. data() {
  14. return {
  15. filePath: "",
  16. imageList: [],
  17. updateImageList: [],
  18. canvasWidth: 256,
  19. canvasHeight: 256,
  20. fontInfo: '',
  21. }
  22. },
  23. methods: {
  24. dragEnter(e) {
  25. e.stopPropagation();
  26. e.preventDefault();
  27. },
  28. dragOver(e) {
  29. e.stopPropagation();
  30. e.preventDefault();
  31. },
  32. onDrop(e) {
  33. this.removeAll();
  34. e.stopPropagation();
  35. e.preventDefault();
  36. let dt = e.dataTransfer;
  37. let files = dt.files;
  38. this.showImgData(files);
  39. },
  40. showImgData(files) {
  41. let that = this;
  42. if (files.length) {
  43. for (var i = 0; i < files.length; i++) {
  44. let file = files[i];
  45. if (!/^image\//.test(file.type)) continue;
  46. let fileReader = new FileReader();
  47. fileReader.onload = (function() {
  48. return function(e) {
  49. if (that.updateImageList.indexOf(e.target.result) !== -1) return;
  50. var img = new Image();
  51. img.src = e.target.result;
  52. img.onload = () => {
  53. let fileName = file.name.split('.')[0];
  54. that.imageList.push({
  55. img: e.target.result,
  56. char: fileName.substr(fileName.length - 1, 1),
  57. width: img.width,
  58. height: img.height,
  59. x: 0,
  60. y: 0,
  61. });
  62. that.updateImageList.push(e.target.result);
  63. that.updateCanvas();
  64. };
  65. };
  66. })();
  67. fileReader.readAsDataURL(file);
  68. }
  69. }
  70. },
  71. updateCanvas() {
  72. if (!this.imageList.length) return;
  73. let that = this;
  74. let height = 0;
  75. let space = 2;
  76. let x = space;
  77. let y = space;
  78. this.imageList.forEach(img => {
  79. if (img.height > height) height = img.height;
  80. });
  81. height = Math.ceil(height);
  82. this.fontSize = height;
  83. let content = self.$fontCanvas.getContext('2d');
  84. content.clearRect(0, 0, this.canvasWidth, this.canvasHeight);
  85. this.imageList.forEach(img2 => {
  86. let img = new Image();
  87. img.src = img2.img;
  88. if (x + img2.width + space > that.canvasWidth) {
  89. x = space;
  90. y += height + space;
  91. }
  92. content.drawImage(img, x, y);
  93. img2.x = x;
  94. img2.y = y;
  95. x += img2.width + space;
  96. });
  97. },
  98. loadFileData() {
  99. let str = `info size=${this.fontSize} unicode=1 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1 outline=0
  100. common lineHeight=${this.fontSize} base=23 scaleW=${this.canvasWidth} scaleH=${this.canvasHeight} pages=1 packed=0
  101. page id=0 file="${this.fontName}.png"
  102. chars count=${this.imageList.length}`;
  103. this.imageList.forEach(img => {
  104. str += `char id=${this.caseConvertEasy(img.char).charCodeAt(0)} x=${img.x} y=${img.y} width=${img.width} height=${img.height} xoffset=0 yoffset=0 xadvance=${img.width} \n`;
  105. })
  106. this.fontInfo = str;
  107. },
  108. caseConvertEasy(str) {
  109. return str.split('').map(s => {
  110. if (s.charCodeAt() <= 90) {
  111. return s.toUpperCase()
  112. }
  113. return s.toLowerCase()
  114. }).join('')
  115. },
  116. removeAll() {
  117. this.imageList = [];
  118. this.updateImageList = [];
  119. this.updateCanvas();
  120. },
  121. save() {
  122. this.selectFolder(() => {
  123. this.loadFileData();
  124. this.savePng();
  125. this.saveFnt();
  126. })
  127. },
  128. selectFolder(func) {
  129. let fontPath = dialog.showSaveDialog({ properties: ['openDirectory', ] });
  130. if (fontPath) {
  131. let fontArr = fontPath.split("\\");
  132. this.fontName = fontArr[fontArr.length - 1];
  133. this.filePath = fontPath.replace("\\" + this.fontName, "");
  134. if (this.filePath) {
  135. Editor.log("选择完成,保存中");
  136. func();
  137. }
  138. }
  139. },
  140. saveFnt() {
  141. Fs.writeFileSync(this.filePath.replace(/\\/g, "/") + '/' + this.fontName + '.fnt', this.fontInfo);
  142. },
  143. savePng() {
  144. let src = self.$fontCanvas.toDataURL("image/png");
  145. let data = src.replace(/^data:image\/\w+;base64,/, "");
  146. let buffer = new window.Buffer(data, 'base64');
  147. Fs.writeFileSync(this.filePath.replace(/\\/g, "/") + '/' + this.fontName + '.png', buffer);
  148. Editor.log("保存成功");
  149. }
  150. }
  151. })
  152. },
  153. });