utils.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const fs = require("fs");
  2. /**
  3. * 生成hiero配置文件
  4. */
  5. function writeHiero(hieroPath, config) {
  6. try {
  7. let textStr = "";
  8. if (config.textFrom === 0) {
  9. textStr = config.textStr;
  10. } else if (config.textFrom === 1) {
  11. if (config.textPath) {
  12. textStr = fs.readFileSync(config.textPath, "utf-8");
  13. }
  14. }
  15. let data = `
  16. font.name=${config.exportName}
  17. font.size=${config.fontSize}
  18. font.bold=false
  19. font.italic=false
  20. font.gamma=1.8
  21. font.mono=false
  22. font2.file=${config.fontPath}
  23. font2.use=true
  24. pad.top=${config.padding}
  25. pad.right=${config.padding}
  26. pad.bottom=${config.padding}
  27. pad.left=${config.padding}
  28. pad.advance.x=${-2 * config.padding}
  29. pad.advance.y=${-2 * config.padding}
  30. glyph.native.rendering=false
  31. glyph.page.width=${config.width}
  32. glyph.page.height=${config.height}
  33. glyph.text=${textStr.replace(/[\r\n]/g, "")}
  34. render_type=0
  35. effect.class=com.badlogic.gdx.tools.hiero.unicodefont.effects.DistanceFieldEffect
  36. effect.Color=ffffff
  37. effect.Scale=${config.scale}
  38. effect.Spread=${config.padding}
  39. `;
  40. fs.writeFileSync(hieroPath, data);
  41. } catch (error) {
  42. Editor.error(`[textmeshpro-tool writeHiero] error: ${error}`);
  43. }
  44. }
  45. function parse(fontPath) {
  46. try {
  47. let data = fs.readFileSync(fontPath, "utf-8");
  48. let arr = data.split(/\r\n|\n|\r/);
  49. let size = Number(arr[0].match(/(?<=size=)([\S]+?)(?=[\s])/)[0]);
  50. let bold = Number(arr[0].match(/(?<=bold=)([\S]+?)(?=[\s])/)[0]);
  51. let italic = Number(arr[0].match(/(?<=italic=)([\S]+?)(?=[\s])/)[0]);
  52. let padding = arr[0].match(/(?<=padding=)([\S]+?)(?=[\s])/)[0];
  53. let spacing = "";
  54. let outline = 0;
  55. let lineHeight = Number(arr[1].match(/(?<=lineHeight=)([\S]+?)(?=[\s])/)[0]);
  56. let base = Number(arr[1].match(/(?<=base=)([\S]+?)(?=[\s])/)[0]);
  57. let scaleW = Number(arr[1].match(/(?<=scaleW=)([\S]+?)(?=[\s])/)[0]);
  58. let scaleH = Number(arr[1].match(/(?<=scaleH=)([\S]+?)(?=[\s])/)[0]);
  59. let pages = Number(arr[1].match(/(?<=pages=)([\S]+?)(?=[\s])/)[0]);
  60. let packed = 0;
  61. let alphaChnl = 0;
  62. let redChnl = 0;
  63. let greenChnl = 0;
  64. let blueChnl = 0;
  65. let pageData = [];
  66. for (let i = 2; i < 2 + pages; i++) {
  67. pageData.push({
  68. id: Number(arr[i].match(/(?<=id=)([\S]+?)(?=[\s])/)[0]),
  69. file: String(arr[i].match(/(?<=file=")([\S]+?)(?=")/)[0])
  70. });
  71. }
  72. let charData = [];
  73. for (let i = 2 + pages + 1; i < arr.length; i++) {
  74. if (!/char/.test(arr[i])) {
  75. continue;
  76. }
  77. charData.push({
  78. id: Number(arr[i].match(/(?<=id=)([\S]+?)(?=[\s])/)[0]),
  79. x: Number(arr[i].match(/(?<=x=)([\S]+?)(?=[\s])/)[0]),
  80. y: Number(arr[i].match(/(?<=y=)([\S]+?)(?=[\s])/)[0]),
  81. width: Number(arr[i].match(/(?<=width=)([\S]+?)(?=[\s])/)[0]),
  82. height: Number(arr[i].match(/(?<=height=)([\S]+?)(?=[\s])/)[0]),
  83. xoffset: Number(arr[i].match(/(?<=xoffset=)([\S]+?)(?=[\s])/)[0]),
  84. yoffset: Number(arr[i].match(/(?<=yoffset=)([\S]+?)(?=[\s])/)[0]),
  85. xadvance: Number(arr[i].match(/(?<=xadvance=)([\S]+?)(?=[\s])/)[0]),
  86. page: Number(arr[i].match(/(?<=page=)([\S]+?)(?=[\s])/)[0]),
  87. chnl: Number(arr[i].match(/(?<=chnl=)([\S]+?)/)[0]),
  88. });
  89. }
  90. let out = {
  91. size: size,
  92. bold: bold,
  93. italic: italic,
  94. padding: padding,
  95. spacing: spacing,
  96. outline: outline,
  97. lineHeight: lineHeight,
  98. base: base,
  99. scaleW: scaleW,
  100. scaleH: scaleH,
  101. pages: pages,
  102. packed: packed,
  103. alphaChnl: alphaChnl,
  104. redChnl: redChnl,
  105. greenChnl: greenChnl,
  106. blueChnl: blueChnl,
  107. pageData: pageData,
  108. charData: charData
  109. };
  110. fs.writeFileSync(fontPath.replace(".fnt", ".json"), JSON.stringify(out));
  111. } catch (error) {
  112. Editor.error(`[textmeshpro-tool parse] error: ${error}`);
  113. }
  114. }
  115. module.exports = {
  116. writeHiero: writeHiero,
  117. parse: parse
  118. };