soiz1 commited on
Commit
dc2154f
·
verified ·
1 Parent(s): cadce95

Update src/serialization/tw-costume-import-export.js

Browse files
src/serialization/tw-costume-import-export.js CHANGED
@@ -27,6 +27,39 @@ const regex = new RegExp(
27
  `${HTML_COMMENT_START}rotationCenter:(-?[\\d\\.]+):(-?[\\d\\.]+)${HTML_COMMENT_END}$`
28
  );
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  /**
31
  * @param {string} svgString SVG source
32
  * @returns {[number, number]|null} The detected rotation center of the SVG, if any.
@@ -49,9 +82,10 @@ const parseVectorMetadata = svgString => {
49
 
50
  /**
51
  * @param {Costume} costume scratch-vm costume object
 
52
  * @returns {Uint8Array} Binary data to export
53
  */
54
- const exportCostume = costume => {
55
  /** @type {Uint8Array} */
56
  const originalData = costume.asset.data;
57
 
@@ -69,10 +103,21 @@ const exportCostume = costume => {
69
  const extraData = `${HTML_COMMENT_START}rotationCenter:${centerX}:${centerY}${HTML_COMMENT_END}`;
70
  decodedData += extraData;
71
 
 
 
 
 
 
 
 
 
 
 
 
72
  return new _TextEncoder().encode(decodedData);
73
  };
74
 
75
  module.exports = {
76
  parseVectorMetadata,
77
  exportCostume
78
- };
 
27
  `${HTML_COMMENT_START}rotationCenter:(-?[\\d\\.]+):(-?[\\d\\.]+)${HTML_COMMENT_END}$`
28
  );
29
 
30
+ const uint8ToBase64 = function(uint8) {
31
+ let binary = '';
32
+ const chunkSize = 0x8000;
33
+ for (let i = 0; i < uint8.length; i += chunkSize) {
34
+ const chunk = uint8.subarray(i, i + chunkSize);
35
+ binary += String.fromCharCode.apply(null, chunk);
36
+ }
37
+ return btoa(binary);
38
+ }
39
+
40
+ /**
41
+ * @param {array} fonts array of fonts to compile
42
+ * @returns {string} css for directions to custom fonts
43
+ */
44
+ const generateCustomFontsCSS = (fonts) => {
45
+ let fontCSS = '';
46
+ for (const font of fonts) {
47
+ const base64 = uint8ToBase64(font.asset.data);
48
+
49
+ // normalize format for browser compatibility
50
+ let format = font.asset.dataFormat.toLowerCase();
51
+ if (format === 'otf') format = 'opentype';
52
+ if (format === 'ttf') format = 'truetype';
53
+
54
+ fontCSS += "@font-face {";
55
+ fontCSS += `font-family: "${font.family}";`;
56
+ fontCSS += `src: url('data:font/${format};base64,${base64}') format('${format}');`;
57
+ fontCSS += "}";
58
+ }
59
+
60
+ return fontCSS;
61
+ };
62
+
63
  /**
64
  * @param {string} svgString SVG source
65
  * @returns {[number, number]|null} The detected rotation center of the SVG, if any.
 
82
 
83
  /**
84
  * @param {Costume} costume scratch-vm costume object
85
+ * @param {Boolean} optIncludeExtras determines if we add things like custom fonts to the export
86
  * @returns {Uint8Array} Binary data to export
87
  */
88
+ const exportCostume = (costume, optIncludeExtras) => {
89
  /** @type {Uint8Array} */
90
  const originalData = costume.asset.data;
91
 
 
103
  const extraData = `${HTML_COMMENT_START}rotationCenter:${centerX}:${centerY}${HTML_COMMENT_END}`;
104
  decodedData += extraData;
105
 
106
+ if (optIncludeExtras && vm?.runtime?.fontManager?.fonts) {
107
+ const fonts = vm.runtime.fontManager.fonts.filter(f => !f.system)
108
+ .filter(f => decodedData.includes(`font-family="&quot;${f.family}&quot;, ${f.fallback}"`))
109
+
110
+ const cssText = generateCustomFontsCSS(fonts);
111
+ if (cssText) {
112
+ const styleElement = `<style type="text/css">${cssText}</style>`;
113
+ decodedData = decodedData.replace(new RegExp(`<svg[^>]*?>`), match => `${match}${styleElement}`);
114
+ }
115
+ }
116
+
117
  return new _TextEncoder().encode(decodedData);
118
  };
119
 
120
  module.exports = {
121
  parseVectorMetadata,
122
  exportCostume
123
+ };