multimodal-marker-png.mjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { crc32, deflateSync } from "node:zlib";
  2. // 测试标记只需可读字符;直接绘制 5×7 点阵,避免为测试图片携带 Sharp/native 依赖。
  3. // 每行用五个低位表示像素。字符图案在此定义,不依赖系统字体或第三方字体文件。
  4. const glyphs = {
  5. A: [14, 17, 17, 31, 17, 17, 17],
  6. B: [30, 17, 17, 30, 17, 17, 30],
  7. C: [14, 17, 16, 16, 16, 17, 14],
  8. D: [30, 17, 17, 17, 17, 17, 30],
  9. E: [31, 16, 16, 30, 16, 16, 31],
  10. F: [31, 16, 16, 30, 16, 16, 16],
  11. G: [14, 17, 16, 23, 17, 17, 15],
  12. H: [17, 17, 17, 31, 17, 17, 17],
  13. I: [31, 4, 4, 4, 4, 4, 31],
  14. J: [7, 2, 2, 2, 18, 18, 12],
  15. K: [17, 18, 20, 24, 20, 18, 17],
  16. L: [16, 16, 16, 16, 16, 16, 31],
  17. M: [17, 27, 21, 21, 17, 17, 17],
  18. N: [17, 25, 21, 19, 17, 17, 17],
  19. O: [14, 17, 17, 17, 17, 17, 14],
  20. P: [30, 17, 17, 30, 16, 16, 16],
  21. Q: [14, 17, 17, 17, 21, 18, 13],
  22. R: [30, 17, 17, 30, 20, 18, 17],
  23. S: [15, 16, 16, 14, 1, 1, 30],
  24. T: [31, 4, 4, 4, 4, 4, 4],
  25. U: [17, 17, 17, 17, 17, 17, 14],
  26. V: [17, 17, 17, 17, 17, 10, 4],
  27. W: [17, 17, 17, 21, 21, 27, 17],
  28. X: [17, 17, 10, 4, 10, 17, 17],
  29. Y: [17, 17, 10, 4, 4, 4, 4],
  30. Z: [31, 1, 2, 4, 8, 16, 31],
  31. a: [0, 0, 14, 1, 15, 17, 15],
  32. b: [16, 16, 30, 17, 17, 17, 30],
  33. c: [0, 0, 14, 17, 16, 17, 14],
  34. d: [1, 1, 15, 17, 17, 17, 15],
  35. e: [0, 0, 14, 17, 31, 16, 14],
  36. f: [6, 9, 8, 28, 8, 8, 8],
  37. g: [0, 15, 17, 17, 15, 1, 14],
  38. h: [16, 16, 30, 17, 17, 17, 17],
  39. i: [4, 0, 12, 4, 4, 4, 14],
  40. j: [2, 0, 6, 2, 2, 18, 12],
  41. k: [16, 16, 18, 20, 24, 20, 18],
  42. l: [12, 4, 4, 4, 4, 4, 14],
  43. m: [0, 0, 26, 21, 21, 21, 21],
  44. n: [0, 0, 30, 17, 17, 17, 17],
  45. o: [0, 0, 14, 17, 17, 17, 14],
  46. p: [0, 0, 30, 17, 30, 16, 16],
  47. q: [0, 0, 15, 17, 15, 1, 1],
  48. r: [0, 0, 22, 25, 16, 16, 16],
  49. s: [0, 0, 15, 16, 14, 1, 30],
  50. t: [8, 8, 28, 8, 8, 9, 6],
  51. u: [0, 0, 17, 17, 17, 19, 13],
  52. v: [0, 0, 17, 17, 17, 10, 4],
  53. w: [0, 0, 17, 17, 21, 21, 10],
  54. x: [0, 0, 17, 10, 4, 10, 17],
  55. y: [0, 0, 17, 17, 15, 1, 14],
  56. z: [0, 0, 31, 2, 4, 8, 31],
  57. 0: [14, 17, 19, 21, 25, 17, 14],
  58. 1: [4, 12, 4, 4, 4, 4, 14],
  59. 2: [14, 17, 1, 2, 4, 8, 31],
  60. 3: [30, 1, 1, 14, 1, 1, 30],
  61. 4: [2, 6, 10, 18, 31, 2, 2],
  62. 5: [31, 16, 16, 30, 1, 1, 30],
  63. 6: [14, 16, 16, 30, 17, 17, 14],
  64. 7: [31, 1, 2, 4, 8, 8, 8],
  65. 8: [14, 17, 17, 14, 17, 17, 14],
  66. 9: [14, 17, 17, 15, 1, 1, 14],
  67. _: [0, 0, 0, 0, 0, 0, 31],
  68. };
  69. function chunk(type, data) {
  70. const body = Buffer.concat([Buffer.from(type, "ascii"), data]);
  71. const result = Buffer.alloc(body.length + 8);
  72. result.writeUInt32BE(data.length, 0);
  73. body.copy(result, 4);
  74. result.writeUInt32BE(crc32(body), result.length - 4);
  75. return result;
  76. }
  77. export function renderMarkerPng(label) {
  78. if (typeof label !== "string" || !/^[A-Za-z0-9_]{1,128}$/u.test(label)) {
  79. throw new Error("PNG marker must contain 1–128 ASCII letters, digits or underscores");
  80. }
  81. const scale = 8;
  82. const margin = 30;
  83. const width = Math.max(1000, margin * 2 + label.length * 6 * scale);
  84. const height = 180;
  85. const top = Math.floor((height - 7 * scale) / 2);
  86. // PNG 灰度扫描行,每行前面的 0 表示不使用像素滤波。
  87. const pixels = Buffer.alloc((width + 1) * height, 255);
  88. for (let y = 0; y < height; y++) pixels[y * (width + 1)] = 0;
  89. for (let i = 0; i < label.length; i++) {
  90. glyphs[label[i]].forEach((bits, row) => {
  91. for (let col = 0; col < 5; col++) {
  92. if (!(bits & (1 << (4 - col)))) continue;
  93. for (let dy = 0; dy < scale; dy++) {
  94. const start = (top + row * scale + dy) * (width + 1) + 1 + margin + (i * 6 + col) * scale;
  95. pixels.fill(0, start, start + scale);
  96. }
  97. }
  98. });
  99. }
  100. const header = Buffer.alloc(13);
  101. header.writeUInt32BE(width, 0);
  102. header.writeUInt32BE(height, 4);
  103. header[8] = 8; // 8-bit grayscale, no alpha/interlacing
  104. return Buffer.concat([
  105. Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
  106. chunk("IHDR", header),
  107. chunk("IDAT", deflateSync(pixels)),
  108. chunk("IEND", Buffer.alloc(0)),
  109. ]);
  110. }