cjkFonts.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { describe, expect, it } from "vitest";
  2. import { cjkFamilies, classifyFontName, withCjkFamilies } from "#ui/cjkFonts.ts";
  3. /** pdf.js 把字体名的原始字节逐个当作 Latin-1 字符保存(TextEncoder 只有 UTF-8,样例字节写死)。 */
  4. const bytes = (...codes: number[]) => String.fromCharCode(...codes);
  5. const NAMES = {
  6. 宋体: bytes(0xcb, 0xce, 0xcc, 0xe5), // GBK
  7. 黑体: bytes(0xba, 0xda, 0xcc, 0xe5), // GBK
  8. 楷体: bytes(0xbf, 0xac, 0xcc, 0xe5), // GBK
  9. 仿宋: bytes(0xb7, 0xc2, 0xcb, 0xce), // GBK
  10. 標楷體: bytes(0xbc, 0xd0, 0xb7, 0xa2, 0xc5, 0xe9), // Big5
  11. "MS 明朝": bytes(0x82, 0x6c, 0x82, 0x72, 0x20, 0x96, 0xbe, 0x92, 0xa9), // Shift-JIS
  12. };
  13. describe("classifyFontName", () => {
  14. it.each([
  15. ["SimSun", "sc", "serif"],
  16. ["ABCDEF+SimHei", "sc", "sans"],
  17. ["STSong-Light", "sc", "serif"],
  18. ["STSongStd-Light", "sc", "serif"],
  19. ["Microsoft YaHei", "sc", "sans"],
  20. ["KaiTi_GB2312", "sc", "kai"],
  21. ["FangSong", "sc", "fang"],
  22. ["FZShuSong-Z01", "sc", "serif"],
  23. ["MSung-Light", "tc", "serif"],
  24. ["MingLiU", "tc", "serif"],
  25. ["DFKai-SB", "tc", "kai"],
  26. ["HeiseiMin-W3", "jp", "serif"],
  27. ["HeiseiKakuGo-W5", "jp", "sans"],
  28. ["MS-Mincho", "jp", "serif"],
  29. ["KozGoPr6N-Medium", "jp", "sans"],
  30. ["HYSMyeongJo-Medium", "kr", "serif"],
  31. ["HYGoThic-Medium", "kr", "sans"],
  32. ["Batang", "kr", "serif"],
  33. ])("%s → %s %s", (name, script, style) => {
  34. expect(classifyFontName(name)).toEqual({ script, style });
  35. });
  36. it("decodes multi-byte font names", () => {
  37. expect(classifyFontName(NAMES.宋体)).toEqual({ script: "sc", style: "serif" });
  38. expect(classifyFontName(NAMES.黑体)).toEqual({ script: "sc", style: "sans" });
  39. expect(classifyFontName(NAMES.楷体)).toEqual({ script: "sc", style: "kai" });
  40. expect(classifyFontName(NAMES.仿宋)).toEqual({ script: "sc", style: "fang" });
  41. expect(classifyFontName(NAMES.標楷體)).toEqual({ script: "tc", style: "kai" });
  42. expect(classifyFontName(NAMES["MS 明朝"])).toEqual({ script: "jp", style: "serif" });
  43. });
  44. it("knows nothing about Latin fonts", () => {
  45. expect(classifyFontName("Arial")).toBeNull();
  46. expect(classifyFontName("ABCDEF+Calibri-Bold")).toBeNull();
  47. });
  48. });
  49. describe("cjkFamilies", () => {
  50. const font = (name: string, fallbackName = "sans-serif") => ({
  51. name,
  52. fallbackName,
  53. missingFile: true,
  54. });
  55. it("leaves embedded and Type 3 fonts alone", () => {
  56. expect(cjkFamilies({ name: "SimSun", missingFile: false }, false)).toEqual([]);
  57. expect(cjkFamilies({ name: "SimSun", missingFile: true, isType3Font: true }, false)).toEqual(
  58. [],
  59. );
  60. });
  61. it("uses the name first, then the PDF's serif flag, Simplified Chinese by default", () => {
  62. expect(cjkFamilies(font("SimSun"), false)).toEqual(["zcode-cjk-sc-serif"]);
  63. expect(cjkFamilies(font("Unknown", "serif"), false)).toEqual(["zcode-cjk-sc-serif"]);
  64. expect(cjkFamilies(font("Unknown"), false)).toEqual(["zcode-cjk-sc-sans"]);
  65. expect(cjkFamilies(font("HeiseiMin-W3"), false)).toEqual(["zcode-cjk-jp-serif"]);
  66. });
  67. it("falls back from kai / fang to the serif face, then to the bundled font", () => {
  68. expect(cjkFamilies(font("KaiTi"), true)).toEqual([
  69. "zcode-cjk-sc-kai",
  70. "zcode-cjk-sc-serif",
  71. "zcode-cjk-fallback",
  72. ]);
  73. });
  74. });
  75. describe("withCjkFamilies", () => {
  76. it("inserts the families before the trailing generic family", () => {
  77. expect(withCjkFamilies('"SimSun",g_d0_s1,sans-serif', ["zcode-cjk-sc-serif"])).toBe(
  78. '"SimSun",g_d0_s1,"zcode-cjk-sc-serif",sans-serif',
  79. );
  80. expect(withCjkFamilies('"g_d0_f2", serif', ["zcode-cjk-sc-serif"])).toBe(
  81. '"g_d0_f2","zcode-cjk-sc-serif",serif',
  82. );
  83. });
  84. it("appends when the chain has no generic family", () => {
  85. expect(withCjkFamilies('"SimSun",g_d0_s1', ["zcode-cjk-sc-serif"])).toBe(
  86. '"SimSun",g_d0_s1,"zcode-cjk-sc-serif"',
  87. );
  88. });
  89. it("returns the chain unchanged without families", () => {
  90. expect(withCjkFamilies('"g_d0_f1", sans-serif', [])).toBe('"g_d0_f1", sans-serif');
  91. });
  92. });