generate_cjk.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """Regenerate cjk.pdf: non-embedded CJK fonts and radical code points in ToUnicode.
  2. Standard library only, byte-for-byte reproducible. Run from this directory:
  3. python3 generate_cjk.py
  4. Every CJK font on the page is deliberately NOT embedded, so the viewer has to pick system fonts:
  5. F0 STSong-Light, predefined CMap UniGB-UCS2-H (needs pdf.js cMaps)
  6. F1 a font named 黑体 in GBK bytes (/#BA#DA#CC#E5), flagged serif: the name must win
  7. F2 SimSun whose ToUnicode maps 文/玄/黄 to the radicals ⽂/⽞/⻩, as Chrome-printed PDFs do
  8. F3 KaiTi_GB2312
  9. """
  10. def tounicode(pairs):
  11. lines = "\n".join(f"<{cid:04X}> <{ord(char):04X}>" for cid, char in pairs)
  12. body = (
  13. "/CIDInit /ProcSet findresource begin 12 dict begin begincmap\n"
  14. "/CMapName /Adobe-Identity-UCS def /CMapType 2 def\n"
  15. "1 begincodespacerange <0000> <FFFF> endcodespacerange\n"
  16. f"{len(pairs)} beginbfchar\n{lines}\nendbfchar\n"
  17. "endcmap CMapName currentdict /CMap defineresource pop end end"
  18. ).encode()
  19. return b"<< /Length %d >>\nstream\n" % len(body) + body + b"\nendstream"
  20. def descriptor(name):
  21. return (
  22. f"<< /Type /FontDescriptor /FontName /{name} /Flags 6 /FontBBox [0 -141 996 855] "
  23. "/ItalicAngle 0 /Ascent 859 /Descent -141 /CapHeight 700 /StemV 80 >>"
  24. )
  25. def identity_font(first, name, text):
  26. """Type0 + CIDFontType2 + descriptor + ToUnicode at objects first..first+3; returns (objs, hex)."""
  27. cids = list(range(100, 100 + len(text)))
  28. objs = {
  29. first: f"<< /Type /Font /Subtype /Type0 /BaseFont /{name} /Encoding /Identity-H "
  30. f"/DescendantFonts [{first + 1} 0 R] /ToUnicode {first + 3} 0 R >>",
  31. first + 1: f"<< /Type /Font /Subtype /CIDFontType2 /BaseFont /{name} "
  32. "/CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> "
  33. f"/FontDescriptor {first + 2} 0 R /DW 1000 >>",
  34. first + 2: descriptor(name),
  35. first + 3: tounicode(list(zip(cids, text))),
  36. }
  37. return objs, "".join(f"{cid:04X}" for cid in cids)
  38. def main():
  39. objs = {
  40. 1: "<< /Type /Catalog /Pages 2 0 R >>",
  41. 2: "<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
  42. 3: "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Contents 4 0 R "
  43. "/Resources << /Font << /F0 10 0 R /F1 20 0 R /F2 30 0 R /F3 40 0 R /F9 50 0 R >> >> >>",
  44. 10: "<< /Type /Font /Subtype /Type0 /BaseFont /STSong-Light /Encoding /UniGB-UCS2-H "
  45. "/DescendantFonts [11 0 R] >>",
  46. 11: "<< /Type /Font /Subtype /CIDFontType0 /BaseFont /STSong-Light "
  47. "/CIDSystemInfo << /Registry (Adobe) /Ordering (GB1) /Supplement 4 >> "
  48. "/FontDescriptor 12 0 R /DW 1000 >>",
  49. 12: descriptor("STSong-Light"),
  50. 50: "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
  51. }
  52. hei, hei_hex = identity_font(20, "#BA#DA#CC#E5", "黑体名称:中文排版测试")
  53. sun, sun_hex = identity_font(30, "SimSun", "天地⽞⻩,中⽂排版")
  54. kai, kai_hex = identity_font(40, "KaiTi_GB2312", "楷体:中文排版测试")
  55. objs.update(hei)
  56. objs.update(sun)
  57. objs.update(kai)
  58. song_hex = "宋体:中文排版测试".encode("utf-16-be").hex().upper()
  59. lines = [
  60. ("F0", song_hex, "F0 STSong-Light UniGB-UCS2-H"),
  61. ("F1", hei_hex, "F1 GBK-named heiti, serif flag"),
  62. ("F2", sun_hex, "F2 SimSun, radicals in ToUnicode"),
  63. ("F3", kai_hex, "F3 KaiTi_GB2312"),
  64. ]
  65. content = ""
  66. for i, (font, hex_text, label) in enumerate(lines):
  67. y = 760 - i * 70
  68. content += f"BT /F9 9 Tf 60 {y + 26} Td ({label}) Tj ET\n"
  69. content += f"BT /{font} 22 Tf 60 {y} Td <{hex_text}> Tj ET\n"
  70. stream = content.encode("latin-1")
  71. objs[4] = b"<< /Length %d >>\nstream\n" % len(stream) + stream + b"\nendstream"
  72. last = max(objs)
  73. data = b"%PDF-1.4\n%\xe2\xe3\xcf\xd3\n"
  74. offsets = {}
  75. for num in range(1, last + 1):
  76. body = objs.get(num, "null")
  77. body = body.encode("latin-1") if isinstance(body, str) else body
  78. offsets[num] = len(data)
  79. data += b"%d 0 obj\n" % num + body + b"\nendobj\n"
  80. xref = len(data)
  81. data += b"xref\n0 %d\n0000000000 65535 f \n" % (last + 1)
  82. data += b"".join(b"%010d 00000 n \n" % offsets[num] for num in range(1, last + 1))
  83. data += b"trailer\n<< /Size %d /Root 1 0 R >>\nstartxref\n%d\n%%%%EOF\n" % (last + 1, xref)
  84. with open("cjk.pdf", "wb") as out:
  85. out.write(data)
  86. if __name__ == "__main__":
  87. main()