Skip to content

ToUnicode in PDF for a Type 0 CID Font might be Wrong #3416

Closed

Description

@YDX-2147483647

Source Han Serif (思源宋体) and other CJK fonts are embedded as Type 0 CID Fonts in PDF. Typst 0.10.0 (70ca0d2) might generate a wrong ToUnicode for them.

Note

Typst.app with default fonts is not affected.

It looks like that typst.app uses NotoSansCJKjp-Regular-Identity-H for CJK characters. Noto Sans CJK is identical to Source Han Sans (思源黑体). Nonetheless, typst compile with neither fonts works on my computer… Language-specific OTFs works, but region-specific Subset OTFs does not.

HaranoAjiMincho, implied by Source Serif Pro, also works on typst.app.

Tip

What is ToUnicode?

In PDF, it is often the case that text is not encoded in Unicode. However, modern
applications usually want them represented in Unicode to make it usable as text
information. The ToUnicode CMap (Character Map) is a bridge between PDF text string encodings and Unicode encodings,
and makes it possible to extract text in PDF files as
Unicode encoded strings. It is important to make resulting PDF search‐able and
copy‐and‐past‐able.

—§1.4.2 of The Dvipdfmx User’s Manual

To Reproduce on Typst.app

  1. Download 14_SourceHanSerifCN.zip from Region Specific Subset OTFs Simplified Chinese (简体中文).

  2. Create a new project in typst.app and upload SubsetOTF/CN/SourceHanSerifCN-Regular.otf in that ZIP.

  3. Compile the following.

    #set page(height: auto)
    
    #let fonts = (
      "Linux Libertine",
      "Source Han Serif",
    )
    
    #for f in fonts {
      text(font: f, size: 3em)[
        #f
    
        #h(1fr)ABC 孔乙己
    
      ]
    }

    minimal-web-with-otf.pdf

  4. Download the PDF, open it, copy all text and paste.

    • Adobe Acrobat / SumatraPDF:

      Linux Libertine
      ABC 孔乙己
      Source Han Serif
      ABC ���
      
    • Firefox (pdf.js) / MS Edge:

      Linux Libertine
      ABC 孔乙己
      Source Han Serif
      ABC 㯰□䄬
      

    Expected: ABC 孔乙己 or ABC孔乙己 (no space).

To Debug

  1. Upload the PDF to PDF Object Browser.

  2. Go to Trailer → Root → Pages → Resources → Font:

    • …

    • F1

      • Base Font: /UVRTQX+NotoSansCJKjp-Regular-Identity-H

      • ToUnicode:

        …
        <2581> <4E59>  # <2581> ↦ U+4E59 (乙)
        <3BFE> <5B54>  # <3BFE> ↦ U+5B54 (孔)
        <4115> <5DF1>  # <4115> ↦ U+5DF1 (己)
        …
        
    • F2

      • Base Font: /TLJDGM+SourceHanSerifCN-Regular-Identity-H

      • ToUnicode:

        …
        <22A1> <4E59>  # <22A1> ↦ U+4E59 (乙)
        <2F8B> <5B54>  # <2F8B> ↦ U+5B54 (孔)
        <3227> <5DF1>  # <3227> ↦ U+5DF1 (己)
        …
        
  3. Go to Trailer → Root → Pages → Kids → 0 → Contents, view contents:

    …
    /F1 33 Tf  # Use font F1 (Noto Sans CJK)
        …
        BT  # Begin text
        …
        [<3BFE25814115>] TJ # Show text
        ET  # End text
    /F2 33 Tf  # Use font F2 (Source Hans Serif CN)
        …
        BT  # Begin text
        …
        [<3BF025A1412C>] TJ  # Show text
        ET  # End text
    

    In the first case, F1’s ToUnicode contains <3BFE25814115>, so all readers convert to 孔乙己 as expected.

    In the second case, F2’s ToUnicode does not contain <3BF025A1412C>. Adobe Acrobat and SumatraPDF just say they don’t know (���), while Firefox and MS Edge parse them directly as Unicode:

    >>> '\u3BF0\u25A1\u412C'
    '㯰□䄬'
  4. Manullay edit the wrong ToUnicode in PDF, and now it becomes 孔乙己.

To Reproduce in the Repo

  1. testit cjk --pdf

  2. Open tests/pdf/layout/cjk-punctuation-adjustment.pdf, copy all text and paste.

    main

    Expected:

    to-unicode

Relevant Links

Acknowledgment / Anecdotes

I have noticed the issue since several days after I met Typst. But I cannot report it as a practical issue until I read Color gradients and my gradual descent into madness on Typst Blog. Thanks Sébastien d'Herbais de Thun and the community!

Besides, KaiTi (楷体), the default font on Windows, embedded as a Type 2 CID Font, turns out to be 孔\r\n乙\r\n己 in SumatraPDF. Those \r\n are not desired. (Similar to #526) Even so, “KaiTi + Acrobat/Firefox/Edge” and “typst.app default fonts + SumatraPDF” give the expected 孔乙己. Therefore it might be SumatraPDF’s issue.

Activity

  1. YDX-2147483647 commented on Feb 15, 2024

    @YDX-2147483647
    ContributorAuthor

    Direct Cause: CID ≠ GID

    NOTE: Although in many fonts the CID value and GID value are the same, the CID and GID values may differ.

    —§9.7.4.2 Glyph Selection in CIDFonts, PDF32000_2008.pdf

    flowchart
    GID["Glyph ID (GID)"]
    CID["Character ID (CID)<br>PDF's Contents"]
    Text["Text / Unicode"]
    GID -->|"ttf_parser::cff::Table::glyph_cid"| CID
    CID -->|"PDF's ToUnicode, expected"| Text
    GID -.->|"PDF's ToUnicode, current"| Text
    
    Loading

    This is the case of some characters in Subset OTFs!
    (GIDs must be contiguous, and CIDs need not. As for Source Han Serif, CID in OTF/**/*.otf = GID in OTF/**/*.otf = CID in SubsetOTF/**/*.otf ≥ GID in SubsetOTF/**/*.otf.)

    Typst version 1d78c3e writes:

    • <225D>, <2F50>, <31EF> in ToUnicode, according to GIDs (glyph.id)
    • [<3CB7259A4241>] TJ in Contents, according to CIDs (font::glyph_cid(font, glyph.id))
    write_text using Font(Source Han Serif, Normal-400-FontStretch(1000))
      Update glyph_set: GID <0022> ↦ Text "A"
      Update glyph_set: GID <0023> ↦ Text "B"
      Update glyph_set: GID <0024> ↦ Text "C"
      Update glyph_set: GID <0001> ↦ Text " "
      Push encoded: CID = <0022>, Glyph ID = <0022>
      Push encoded: CID = <0023>, Glyph ID = <0023>
      Push encoded: CID = <0024>, Glyph ID = <0024>
      Push encoded: CID = <0001>, Glyph ID = <0001>
    write_text using Font(Source Han Serif, Normal-400-FontStretch(1000))
      Update glyph_set: GID <2F50> ↦ Text "孔"
      Update glyph_set: GID <225D> ↦ Text "乙"
      Update glyph_set: GID <31EF> ↦ Text "己"
      Push encoded: CID = <3CB7>, Glyph ID = <2F50>
      Push encoded: CID = <259A>, Glyph ID = <225D>
      Push encoded: CID = <4241>, Glyph ID = <31EF>
    
    The log is generated by …

    for g in &text.glyphs {
    let segment = &text.text[g.range()];
    glyph_set.entry(g.id).or_insert_with(|| segment.into());
    }

    + println!("write_text using {:#?}", text.font);
      for g in &text.glyphs {
          let segment = &text.text[g.range()];
    +     println!("  Update glyph_set: GID <{:04X}> ↦ Text {:?}", g.id, segment);
          glyph_set.entry(g.id).or_insert_with(|| segment.into());
      }

    let cid = crate::font::glyph_cid(&text.font, glyph.id);
    encoded.push((cid >> 8) as u8);
    encoded.push((cid & 0xff) as u8);

      let cid = crate::font::glyph_cid(&text.font, glyph.id);
    + println!("  Push encoded: CID = <{:04X}>, Glyph ID = <{:04X}>", cid, glyph.id);
      encoded.push((cid >> 8) as u8);
      encoded.push((cid & 0xff) as u8);
  2. added 2 commits that reference this issue on Feb 15, 2024
    45bf9b2
    b483605
  3. YDX-2147483647 commented on Feb 16, 2024

    @YDX-2147483647
    ContributorAuthor
    1. We are calculating glyph_cid(font, glyph_id) 2 times1 in typst-pdf now. We can calculate the third time when saving ToUnicode character map, and fix this issue in simple 3 lines. Or should we refactor?

    2. “PDF are not tested automatically”, and testit cjk --pdf is enough for manual test. Then should I add a test case in copy-paste.typ?

    @laurmaedje Could you tell me what you think?

    Footnotes

    1. The first when writing glyphs, the second when extracting glyphs' widths. ↩

  4. laurmaedje commented on Feb 17, 2024

    @laurmaedje
    Member

    Thanks for the thorough investigation! The fix definitely makes sense. The same bug existed before for text writing and I missed that it's also a problem for /ToUnicode.

    We are calculating glyph_cid(font, glyph_id) 2 times1 in typst-pdf now. We can calculate the third time when saving ToUnicode character map, and fix this issue in simple 3 lines. Or should we refactor?

    It's fine to do it a third time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions