Blame view

public/assets/addons/fastflow/plugins/jsPDF-master/src/modules/ttfsupport.js 1.92 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  /**
   * @license
   * Licensed under the MIT License.
   * http://opensource.org/licenses/mit-license
   */
  
  import { jsPDF } from "../jspdf.js";
  import { atob } from "../libs/AtobBtoa.js";
  
  /**
   * @name ttfsupport
   * @module
   */
  (function(jsPDF) {
    "use strict";
  
    var binaryStringToUint8Array = function(binary_string) {
      var len = binary_string.length;
      var bytes = new Uint8Array(len);
      for (var i = 0; i < len; i++) {
        bytes[i] = binary_string.charCodeAt(i);
      }
      return bytes;
    };
  
    var addFont = function(font, file) {
      // eslint-disable-next-line no-control-regex
      if (/^\x00\x01\x00\x00/.test(file)) {
        file = binaryStringToUint8Array(file);
      } else {
        file = binaryStringToUint8Array(atob(file));
      }
      font.metadata = jsPDF.API.TTFFont.open(file);
      font.metadata.Unicode = font.metadata.Unicode || {
        encoding: {},
        kerning: {},
        widths: []
      };
      font.metadata.glyIdsUsed = [0];
    };
  
    jsPDF.API.events.push([
      "addFont",
      function(data) {
        var file = undefined;
        var font = data.font;
        var instance = data.instance;
        if (font.isStandardFont) {
          return;
        }
        if (typeof instance !== "undefined") {
          if (instance.existsFileInVFS(font.postScriptName) === false) {
            file = instance.loadFile(font.postScriptName);
          } else {
            file = instance.getFileFromVFS(font.postScriptName);
          }
          if (typeof file !== "string") {
            throw new Error(
              "Font is not stored as string-data in vFS, import fonts or remove declaration doc.addFont('" +
                font.postScriptName +
                "')."
            );
          }
          addFont(font, file);
        } else {
          throw new Error(
            "Font does not exist in vFS, import fonts or remove declaration doc.addFont('" +
              font.postScriptName +
              "')."
          );
        }
      }
    ]); // end of adding event handler
  })(jsPDF);