设置元数据| JavaScript

如何将字体元数据设置到“name”表中?

您可以使用 AsposeFontSetInfo 函数添加或更改元数据信息。

参数 nameId 定义记录的逻辑字符串类别。参数 platformIdplatformSpecificIdlanguageId 用于设置字符串的语言。最后一个参数 text 用于设置记录的字符串数据。

如果通过参数 platformIdplatformSpecificIdlanguageIdnameId 添加的记录已存在,则该方法不会添加新记录,而是使用参数 text 定义的值更新现有记录中的字符串数据。

设置元数据的示例

  1. 创建一个“FileReader”对象。

  2. 定义参数并运行 AsposeFontSetInfo 函数。

使用枚举类型 TtfNameTableNameIdTtfNameTablePlatformId 分别表示 nameIdplatformIdTtfNameTableMacPlatformSpecificIdTtfNameTableMSPlatformSpecificId,或对于 platformSpecificId,使用 TtfNameTableUnicodePlatformSpecificId;对于 languageId,使用 TtfNameTableMacLanguageIdTtfNameTableMSLanguageId

  1. 接下来,如果 json.errorCode 的值为 0,则可以获取结果数据。如果 json.errorCode 参数不等于 0,则表示文件中存在错误,错误信息将包含在 json.errorText 中。
  2. 生成的 JSON 对象包含一个 fileNameResult 字段,其中包含已保存到内存文件系统中的字体文件名。可以使用 DownloadFile 函数获取该文件。
 1  var fFontSetInfo = function (e) {
 2    const file_reader = new FileReader();
 3    file_reader.onload = (event) => {
 4
 5      const nameId = new Function("return Module.TtfNameTableNameId." + document.getElementById("NameId").value)();
 6      const platformId = Module.TtfNameTablePlatformId.Microsoft;
 7      const platformSpecificId = Module.TtfNameTableMSPlatformSpecificId.Unicode_BMP_UCS2.value;
 8      const text = document.getElementById("textValue").value;
 9      const langID = 1033;
10
11      const json = AsposeFontSetInfo(blob, file.name, nameId, platformId, platformSpecificId, langID, text);
12      if (json.errorCode == 0) {
13        DownloadFile(json.fileNameResult);
14        //DownloadFile(file.name);
15      }
16      else document.getElementById('output').textContent = json.errorText;
17    }
18    file_reader.readAsArrayBuffer(file);

或使用Web Worker:

 1<script type="text/javascript">
 2  /*Create Web Worker*/
 3  const AsposeFontWebWorker = new Worker("AsposeFontforJS.js");
 4  AsposeFontWebWorker.onerror = (evt) => console.log(`Error from Web Worker: ${evt.message}`);
 5  AsposeFontWebWorker.onmessage = (evt) => document.getElementById("output").textContent =
 6    (evt.data == 'ready') ? 'library loaded!' :
 7    (evt.data.json.errorCode == 0) ? `Result:\n${DownloadFile(evt.data.json.fileNameResult, "font/ttf", evt.data.params[0])}` : `Error: ${evt.data.json.errorText}`;
 8 
 9 /*Event handler*/
10  const ffileFontSetInfo = e => {
11    const file_reader = new FileReader();
12    file_reader.onload = event => {
13          const nameId = 'Module.TtfNameTableNameId.' + document.getElementById("NameId").value;
14          //Value will be changed for PlatformId = PlatformId.Microsoft, PlatformSpecificId = MSPlatformSpecificId.Unicode_BMP_UCS2 (1) and languageID = 1033 (English_United_States = 0x0409)
15          const platformId = 'Module.TtfNameTablePlatformId.Microsoft';
16          const platformSpecificId = 'Module.TtfNameTableMSPlatformSpecificId.Unicode_BMP_UCS2';
17          const langID = 'Module.TtfNameTableMSLanguageId.English_United_States';
18          const text = document.getElementById("textValue").value;
19          transfer = [event.target.result];
20          params = [event.target.result, e.target.files[0].name, nameId, platformId, platformSpecificId, langID, text];
21      AsposeFontWebWorker.postMessage({ "operation": 'AsposeFontSetInfo', "params": params }, transfer);
22    };
23    file_reader.readAsArrayBuffer(e.target.files[0]);
24  };
25</script>

如何获取解决方案?

如果您对该库感兴趣,请访问 Aspose.Font for JavaScript 产品页面*。您可以在那里找到该解决方案的更多功能。您可以从那里下载免费试用版或购买完整产品。

如果您还有任何问题或疑问,请随时在 免费支持论坛*Aspose.Font 产品系列** 版块发帖,我们的支持团队将在几个小时内为您解答所有问题。