@chr33s/pdf-standard-fonts 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +41 -0
  3. package/dist/all-encodings.compressed.json +1 -0
  4. package/dist/courier-bold-oblique.compressed.json +1 -0
  5. package/dist/courier-bold.compressed.json +1 -0
  6. package/dist/courier-oblique.compressed.json +1 -0
  7. package/dist/courier.compressed.json +1 -0
  8. package/dist/encoding.d.ts +31 -0
  9. package/dist/encoding.js +58 -0
  10. package/dist/encoding.js.map +1 -0
  11. package/dist/font.d.ts +99 -0
  12. package/dist/font.js +105 -0
  13. package/dist/font.js.map +1 -0
  14. package/dist/helvetica-bold-oblique.compressed.json +1 -0
  15. package/dist/helvetica-bold.compressed.json +1 -0
  16. package/dist/helvetica-oblique.compressed.json +1 -0
  17. package/dist/helvetica.compressed.json +1 -0
  18. package/dist/index.d.ts +2 -0
  19. package/dist/index.js +3 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/symbol.compressed.json +1 -0
  22. package/dist/times-bold-italic.compressed.json +1 -0
  23. package/dist/times-bold.compressed.json +1 -0
  24. package/dist/times-italic.compressed.json +1 -0
  25. package/dist/times-roman.compressed.json +1 -0
  26. package/dist/utils.d.ts +2 -0
  27. package/dist/utils.js +17 -0
  28. package/dist/utils.js.map +1 -0
  29. package/dist/zapf-dingbats.compressed.json +1 -0
  30. package/encoding-metrics/all-encodings.compressed.json +1 -0
  31. package/encoding-metrics/all-encodings.json +1 -0
  32. package/encoding-metrics/symbol-encoding.json +1 -0
  33. package/encoding-metrics/symbol.txt +261 -0
  34. package/encoding-metrics/win1252-encoding.json +1 -0
  35. package/encoding-metrics/win1252.txt +279 -0
  36. package/encoding-metrics/zapfdingbats-encoding.json +1 -0
  37. package/encoding-metrics/zapfdingbats.txt +268 -0
  38. package/font-metrics/MustRead.html +19 -0
  39. package/font-metrics/courier-bold-oblique.afm +342 -0
  40. package/font-metrics/courier-bold-oblique.compressed.json +1 -0
  41. package/font-metrics/courier-bold-oblique.json +1 -0
  42. package/font-metrics/courier-bold.afm +342 -0
  43. package/font-metrics/courier-bold.compressed.json +1 -0
  44. package/font-metrics/courier-bold.json +1 -0
  45. package/font-metrics/courier-oblique.afm +342 -0
  46. package/font-metrics/courier-oblique.compressed.json +1 -0
  47. package/font-metrics/courier-oblique.json +1 -0
  48. package/font-metrics/courier.afm +342 -0
  49. package/font-metrics/courier.compressed.json +1 -0
  50. package/font-metrics/courier.json +1 -0
  51. package/font-metrics/helvetica-bold-oblique.afm +2827 -0
  52. package/font-metrics/helvetica-bold-oblique.compressed.json +1 -0
  53. package/font-metrics/helvetica-bold-oblique.json +1 -0
  54. package/font-metrics/helvetica-bold.afm +2827 -0
  55. package/font-metrics/helvetica-bold.compressed.json +1 -0
  56. package/font-metrics/helvetica-bold.json +1 -0
  57. package/font-metrics/helvetica-oblique.afm +3051 -0
  58. package/font-metrics/helvetica-oblique.compressed.json +1 -0
  59. package/font-metrics/helvetica-oblique.json +1 -0
  60. package/font-metrics/helvetica.afm +3051 -0
  61. package/font-metrics/helvetica.compressed.json +1 -0
  62. package/font-metrics/helvetica.json +1 -0
  63. package/font-metrics/symbol.afm +213 -0
  64. package/font-metrics/symbol.compressed.json +1 -0
  65. package/font-metrics/symbol.json +1 -0
  66. package/font-metrics/times-bold-italic.afm +2384 -0
  67. package/font-metrics/times-bold-italic.compressed.json +1 -0
  68. package/font-metrics/times-bold-italic.json +1 -0
  69. package/font-metrics/times-bold.afm +2588 -0
  70. package/font-metrics/times-bold.compressed.json +1 -0
  71. package/font-metrics/times-bold.json +1 -0
  72. package/font-metrics/times-italic.afm +2667 -0
  73. package/font-metrics/times-italic.compressed.json +1 -0
  74. package/font-metrics/times-italic.json +1 -0
  75. package/font-metrics/times-roman.afm +2419 -0
  76. package/font-metrics/times-roman.compressed.json +1 -0
  77. package/font-metrics/times-roman.json +1 -0
  78. package/font-metrics/zapf-dingbats.afm +225 -0
  79. package/font-metrics/zapf-dingbats.compressed.json +1 -0
  80. package/font-metrics/zapf-dingbats.json +1 -0
  81. package/package.json +51 -0
  82. package/scripts/encodings/parse-win1252.ts +120 -0
  83. package/scripts/encodings/parse-zapf-dingbats-or-symbol.ts +22 -0
  84. package/scripts/encodings/parse.ts +72 -0
  85. package/scripts/fonts/parse-character-metrics.ts +73 -0
  86. package/scripts/fonts/parse-font-metrics.ts +95 -0
  87. package/scripts/fonts/parse-kern-pairs.ts +30 -0
  88. package/scripts/fonts/parse.ts +91 -0
  89. package/scripts/fonts/utils.ts +43 -0
  90. package/src/all-encodings.compressed.json +1 -0
  91. package/src/courier-bold-oblique.compressed.json +1 -0
  92. package/src/courier-bold.compressed.json +1 -0
  93. package/src/courier-oblique.compressed.json +1 -0
  94. package/src/courier.compressed.json +1 -0
  95. package/src/encoding.ts +85 -0
  96. package/src/font.ts +164 -0
  97. package/src/helvetica-bold-oblique.compressed.json +1 -0
  98. package/src/helvetica-bold.compressed.json +1 -0
  99. package/src/helvetica-oblique.compressed.json +1 -0
  100. package/src/helvetica.compressed.json +1 -0
  101. package/src/index.ts +2 -0
  102. package/src/symbol.compressed.json +1 -0
  103. package/src/times-bold-italic.compressed.json +1 -0
  104. package/src/times-bold.compressed.json +1 -0
  105. package/src/times-italic.compressed.json +1 -0
  106. package/src/times-roman.compressed.json +1 -0
  107. package/src/utils.ts +20 -0
  108. package/src/zapf-dingbats.compressed.json +1 -0
  109. package/test/all-fonts.test.ts +133 -0
  110. package/test/encoding.test.ts +32 -0
  111. package/test/encodings-async.test.ts +78 -0
  112. package/test/font.test.ts +35 -0
  113. package/test/utils.test.ts +34 -0
  114. package/tsconfig.json +9 -0
  115. package/tsconfig.typecheck.json +14 -0
  116. package/vitest.config.ts +8 -0
@@ -0,0 +1,32 @@
1
+ import { describe, expect, test } from "vitest";
2
+
3
+ import { Encodings } from "../src/index.js";
4
+
5
+ describe("Encodings", () => {
6
+ test("lists supported code points in ascending order", () => {
7
+ const winAnsi = Encodings.WinAnsi;
8
+ const { supportedCodePoints } = winAnsi;
9
+
10
+ expect(supportedCodePoints.length).toBeGreaterThan(0);
11
+ expect(supportedCodePoints).toEqual([...supportedCodePoints].sort((a, b) => a - b));
12
+ });
13
+
14
+ test("encodes mapped code points and reports unsupported ones", () => {
15
+ const encoding = Encodings.Symbol;
16
+ const [firstSupported] = encoding.supportedCodePoints;
17
+
18
+ expect(firstSupported).toBeDefined();
19
+ if (firstSupported) {
20
+ const result = encoding.encodeUnicodeCodePoint(firstSupported);
21
+ expect(typeof result.code).toBe("number");
22
+ expect(typeof result.name).toBe("string");
23
+ expect(encoding.canEncodeUnicodeCodePoint(firstSupported)).toBe(true);
24
+ }
25
+
26
+ const unsupportedCodePoint = 0xffff;
27
+ expect(encoding.canEncodeUnicodeCodePoint(unsupportedCodePoint)).toBe(false);
28
+ expect(() => encoding.encodeUnicodeCodePoint(unsupportedCodePoint)).toThrow(
29
+ /Symbol cannot encode/,
30
+ );
31
+ });
32
+ });
@@ -0,0 +1,78 @@
1
+ import { describe, expect, test } from "vitest";
2
+
3
+ import { getEncodings } from "../src/index.js";
4
+
5
+ describe("getEncodings", () => {
6
+ test("returns all three encodings", async () => {
7
+ const encodings = await getEncodings();
8
+
9
+ expect(encodings.Symbol).toBeDefined();
10
+ expect(encodings.ZapfDingbats).toBeDefined();
11
+ expect(encodings.WinAnsi).toBeDefined();
12
+ });
13
+
14
+ test("WinAnsi encoding supports common ASCII characters", async () => {
15
+ const encodings = await getEncodings();
16
+ const winAnsi = encodings.WinAnsi;
17
+
18
+ // Space (0x20)
19
+ expect(winAnsi.canEncodeUnicodeCodePoint(0x0020)).toBe(true);
20
+ // 'A' (0x41)
21
+ expect(winAnsi.canEncodeUnicodeCodePoint(0x0041)).toBe(true);
22
+ // 'a' (0x61)
23
+ expect(winAnsi.canEncodeUnicodeCodePoint(0x0061)).toBe(true);
24
+ // '0' (0x30)
25
+ expect(winAnsi.canEncodeUnicodeCodePoint(0x0030)).toBe(true);
26
+ });
27
+
28
+ test("Symbol encoding handles Greek letters", async () => {
29
+ const encodings = await getEncodings();
30
+ const symbol = encodings.Symbol;
31
+
32
+ // Symbol encoding uses different mappings
33
+ expect(symbol.supportedCodePoints.length).toBeGreaterThan(0);
34
+ });
35
+
36
+ test("ZapfDingbats encoding has supported code points", async () => {
37
+ const encodings = await getEncodings();
38
+ const zapf = encodings.ZapfDingbats;
39
+
40
+ expect(zapf.supportedCodePoints.length).toBeGreaterThan(0);
41
+ });
42
+
43
+ test("encodeUnicodeCodePoint returns code and name", async () => {
44
+ const encodings = await getEncodings();
45
+ const winAnsi = encodings.WinAnsi;
46
+
47
+ // Encode 'A'
48
+ const result = winAnsi.encodeUnicodeCodePoint(0x0041);
49
+ expect(typeof result.code).toBe("number");
50
+ expect(typeof result.name).toBe("string");
51
+ expect(result.name).toBe("A");
52
+ });
53
+
54
+ test("encodeUnicodeCodePoint throws for unsupported code points", async () => {
55
+ const encodings = await getEncodings();
56
+ const winAnsi = encodings.WinAnsi;
57
+
58
+ // High Unicode code point not in WinAnsi
59
+ expect(() => winAnsi.encodeUnicodeCodePoint(0x10000)).toThrow(/WinAnsi cannot encode/);
60
+ });
61
+
62
+ test("encodings are memoized", async () => {
63
+ const first = await getEncodings();
64
+ const second = await getEncodings();
65
+
66
+ expect(first).toBe(second);
67
+ });
68
+
69
+ test("supportedCodePoints are sorted in ascending order", async () => {
70
+ const encodings = await getEncodings();
71
+
72
+ for (const encoding of [encodings.WinAnsi, encodings.Symbol, encodings.ZapfDingbats]) {
73
+ const { supportedCodePoints } = encoding;
74
+ const sorted = [...supportedCodePoints].sort((a, b) => a - b);
75
+ expect(supportedCodePoints).toEqual(sorted);
76
+ }
77
+ });
78
+ });
@@ -0,0 +1,35 @@
1
+ import { describe, expect, test } from "vitest";
2
+
3
+ import { Font, FontNames } from "../src/index.js";
4
+
5
+ describe("Font.load", () => {
6
+ test("returns memoized instances", async () => {
7
+ const firstLoad = await Font.load(FontNames.Courier);
8
+ const secondLoad = await Font.load(FontNames.Courier);
9
+
10
+ expect(firstLoad).toBe(secondLoad);
11
+ });
12
+
13
+ test("computes glyph widths and kerning lookups", async () => {
14
+ const font = await Font.load(FontNames.Helvetica);
15
+
16
+ const charMetrics = font.CharMetrics.find((metric) => metric.N === "A");
17
+ expect(charMetrics).toBeDefined();
18
+ expect(font.getWidthOfGlyph("A")).toBe(charMetrics?.WX);
19
+
20
+ const kernPair = font.KernPairs.find(
21
+ ([left, right]) => font.getXAxisKerningForPair(left, right) !== undefined,
22
+ );
23
+ expect(kernPair).toBeDefined();
24
+ if (kernPair) {
25
+ expect(font.getXAxisKerningForPair(kernPair[0], kernPair[1])).toBe(kernPair[2]);
26
+ }
27
+ });
28
+
29
+ test("accepts canonical font name aliases", async () => {
30
+ const fromAlias = await Font.load(FontNames.HelveticaBold);
31
+ const fromLiteral = await Font.load("Helvetica-Bold");
32
+
33
+ expect(fromAlias).toBe(fromLiteral);
34
+ });
35
+ });
@@ -0,0 +1,34 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { decompressJson, padStart } from "../src/utils.js";
3
+
4
+ describe("utils", () => {
5
+ describe("padStart", () => {
6
+ test("should pad string to specified length", () => {
7
+ expect(padStart("42", 4, "0")).toBe("0042");
8
+ });
9
+
10
+ test("should not pad if string is already long enough", () => {
11
+ expect(padStart("1234", 4, "0")).toBe("1234");
12
+ });
13
+
14
+ test("should not pad if string is longer than specified length", () => {
15
+ expect(padStart("12345", 4, "0")).toBe("12345");
16
+ });
17
+
18
+ test("should handle empty string", () => {
19
+ expect(padStart("", 3, "x")).toBe("xxx");
20
+ });
21
+
22
+ test("should handle single character padding", () => {
23
+ expect(padStart("a", 5, "-")).toBe("----a");
24
+ });
25
+ });
26
+
27
+ describe("decompressJson", () => {
28
+ // Note: This test requires a valid compressed JSON string
29
+ // The actual compressed format is base64-encoded deflated data
30
+ test("should be a function", () => {
31
+ expect(typeof decompressJson).toBe("function");
32
+ });
33
+ });
34
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "allowJs": true,
5
+ "rootDir": "src",
6
+ "outDir": "dist"
7
+ },
8
+ "include": ["src/**/*"]
9
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "noEmit": true,
5
+ "rootDir": "."
6
+ },
7
+ "include": [
8
+ "**/*.ts",
9
+ ],
10
+ "exclude": [
11
+ "dist",
12
+ "node_modules"
13
+ ]
14
+ }
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from "vitest/config";
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ environment: "node",
6
+ include: ["test/**/*.test.ts"],
7
+ },
8
+ });