字体转换器| JavaScript的API解决方案
Contents
[
Hide
Show
]概述
JavaScript库的Aspose.font是一种解决方案,可让您在流行的 支持格式之间转换字体。
如何将字体转换为所需的格式?
- 创建一个“ fileReader”。
- 调用 AsposeFontConvert函数。
- 在
json.filenameresult
中设置了结果名称文件。 - 接下来,如果“ JSON.ERRORCODE”为0,则可以获取结果文件的链接。如果“ JSON.ERRORCODE”参数不等于0,因此您的文件中会有错误,则有关此类错误的信息将包含在`json.erortext“中中。
- 结果,下载文件函数生成链接,并允许您将结果文件下载到用户的操作系统。
以下是将字体从
ttf
转换为woff
格式的示例。
1 var fTTF2WOFF = function (e) {
2 const file_reader = new FileReader();
3 file_reader.onload = (event) => {
4 const json = AsposeFontConvert(event.target.result, e.target.files[0].name, Module.FontType.TTF, Module.FontSavingFormats.WOFF);
5 if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
6 else document.getElementById('output').textContent = json.errorText;
7 DownloadFile(json.fileNameResult, "woff");
8 }
9 file_reader.readAsArrayBuffer(e.target.files[0]);
10 }
或使用Web Worker:
1 /*Create Web Worker*/
2 const AsposeFontWebWorker = new Worker("AsposeFontforJS.js");
3 AsposeFontWebWorker.onerror = evt => console.log(`Error from Web Worker: ${evt.message}`);
4 AsposeFontWebWorker.onmessage = evt => document.getElementById('output').textContent =
5 (evt.data == 'ready') ? 'library loaded!' :
6 (evt.data.json.errorCode == 0) ? `Result:\n${DownloadFile(evt.data.json.fileNameResult, "font/ttf", evt.data.params[0])}` : `Error: ${evt.data.json.errorText}`;
7
8 /*Event handler*/
9 const fOTFtoTTF = e => {
10 const file_reader = new FileReader();
11 file_reader.onload = event => {
12 /*Convert a OTF fonts to TTF and save - Ask Web Worker*/
13 AsposeFontWebWorker.postMessage({ "operation": 'AsposeFontConvert', "params": [event.target.result, e.target.files[0].name, 'Module.FontType.OTF', 'Module.FontSavingFormats.TTF'] }, [event.target.result]);
14 };
15 file_reader.readAsArrayBuffer(e.target.files[0]);
16 };
17
18 /*Make a link to download the result file*/
19 const DownloadFile = function (filename, mime, content) {
20 mime = mime || "application/octet-stream";
21 var link = document.createElement("a");
22 link.href = URL.createObjectURL(new Blob([content], {type: mime}));
23 link.download = filename;
24 link.textContent = filename;
25 link.title = "Click here to download the file";
26 document.getElementById('fileDownload').appendChild(link);
27 document.getElementById('fileDownload').appendChild(document.createElement("br"));
28 }
如何获取解决方案?
如果要获得库,请转到 Java 版 Aspose.FontScript产品页面。在这里,您会发现解决方案给您的更多机会。您可以从那里下载免费试用版或在那里购买产品。
如果您有任何问题或疑问,请随时在 ASPOSE.FONT.FORPODUCT family 免费支持论坛的部分中发布它们,我们的支持团队将为您清除所有内容。