Skip to content

Different characters that use the same glyph result in the same character when copied from PDFs #526

Closed

Description

Input:

birth\u{ad}day
birth­day // also contains a soft hyphen

In the resulting PDF both soft hyphens are searchable as spaces (birth day).
Expected result would be that the word is searchable as birthday (no soft hyphen included) or as birth­day (with soft hyphen included).
Including a space instead, changes the semantics of the text.

With LuaTeX, soft hyphens are not included in the searchable text.
Maybe related: #479

Activity

  1. added
    bug
    pdf
    on Apr 2, 2023
  2. Enivex commented on Feb 27, 2024

    @Enivex
    Collaborator

    The same is true for zero width space.

  3. TS60 commented on Jun 1, 2024

    @TS60

    I could debug it as follows:

    • The write_normal_text has the correct soft hyphen char for the soft hyphen glyph in the glyph_set
    • The create_cmap function already has the normal space for a soft hyphen glyph in the glyph_set

    Between these two something happens that replaces the text in the glyph_set for the soft hyphen glyph from the correct char to the normal space.

    • having the normal improve_glyph_sets will result in a space for the SHY
    • returning in the first line of improve_glyph_sets (do nothing) will give the correct text at create_cmap, but now, normal spaces are missing or even replaced by SHY.

    Also the PDF cmap seems to correctly contain the SHY after the change to improve_glyph_sets (00AD is the SHY):

    9 beginbfchar
    <0003> <00AD>
    <0044> <0061>
    <0045> <0062>
    <0047> <0064>
    <004B> <0068>
    <004C> <0069>
    <0055> <0072>
    <0057> <0074>
    <005C> <0079>
    endbfchar
    endcmap
    

    In the PDF readers I can now copy the SHY like expected. But as said above, the normal spaces don't copy as expected anymore. I think, we need a special case in the improve_glyph_sets: do like before but not for SHY and ZWS.

    Also, the word now seems to be created as multiple text block and it is only searchable up to the first SHY.
    With LuaTeX it is all in one block and searchable:

    \documentclass[a4paper]{article}
    
    \begin{document}
    
    birth­day birth­day
    
    \end{document}

    Maybe you have an idea about it, @laurmaedje?

  4. laurmaedje commented on Jun 1, 2024

    @laurmaedje
    Member

    The problem is that harfbuzz outputs the same glyph for both U+20 (SPACE) and U+AD (SHY), just with different advance width. And since the PDF content is just glyphs + a glyph -> char mapping, it will copy the same char. I don't think we can fix this by changing improve_glyph_sets.

    I guess we could try to filter out space glyphs with zero advance width or something like that. But I'm not sure whether that would be correct behaviour. Would need to look closer at what other software is doing.

  5. TS60 commented on Jun 1, 2024

    @TS60

    Thanks! Indeed it seems to vary with LuaTeX based on the font (not yet found the pattern). Maybe it works only if the font actually has a glyph for a soft hyphen. Otherwise, it just filters it out. It seems the harfbuzz flag is called REMOVE_DEFAULT_IGNORABLES to activate this.

    Update:
    I now tested also other fonts (Arial, Source Code Pro) and they have the same problem.

    LuaTeX does not include the soft hyphen in the searchable text and Typst does include a space instead in the searchable text. The rendered text looks the same for both with the fonts I tested. So, I assume, LuaTeX just activates the equivalent of REMOVE_DEFAULT_IGNORABLES.

  6. khaledhosny commented on Jun 2, 2024

    @khaledhosny

    The glyph to code point mapping in the PDF should be based on the input string not the output glyph alone (using HarfBuzz’s clusters to map glyphs to input code points), but OpenType and HarfBuzz allow complex glyph to code point mappings (one to one, one to many, many to one and many to many) while PDF cmap allows only one to one and one to many. The same glyph can also be output from different code points, but PDF cmap does not support that either (which is the case here). To get the best text extraction out of PDF, a combination of cmap ToUnicode as ActualText tagging needs to be used. Use cmap whenever possible (if the glyph to code point mapping is unique, and single glyph is mapped to single or multiple code points) and if not fallback to ActualText.

    Note that LuaTeX will do this differently when using luaotfload’s default shaper and when using HarfBuzz, the later should support more complex cases, so check that too when comparing LuaTeX PDFs with typst’s.

  7. laurmaedje commented on Jun 2, 2024

    @laurmaedje
    Member

    Thanks for the explanation! We already collect the reverse mapping from the cluster information, but don't yet write an ActualText if there are two different mappings. Good information that that's the way to go!

  8. TS60 commented on Jun 2, 2024

    @TS60

    Thanks for the information! In the LuaTeX PDF (compression turned off) I cannot find /ActualText and it still works. If I remember correct, /ActualText is very poorly supported by PDF readers and thus maybe wouldn't help for most pdf readers. When the behaviour of LuaTeX would work with Typst, /ActualText could be used as an addition, so that poor pdf readers would still have the correct result.

    As I said above, REMOVE_DEFAULT_IGNOREABLES sounds very much to be the difference between the searchable text in LuaTeX and Typst:
    "Flag indication that character with Default_Ignorable Unicode property should be removed from glyph string instead of hiding them (done by replacing them with the space glyph and zeroing the advance width." (source).

    The soft hyphen seems to be such a character and the replacement is a space char. This basically exactly matches the description. I tried to activate this, but have not found the place in the typst source code.

  9. khaledhosny commented on Jun 2, 2024

    @khaledhosny

    Removing Default_Ignorable might work here, but it will make things worse for other situations. For example, it will also remove ZWNJ, but this changes the meaning of the text in many languages.

  10. TS60 commented on Jun 2, 2024

    @TS60

    LuaTeX also does the zwnj as expected: the glyph is kept and the searchable text is a space.
    I think when harfbuzz/rustybuzz does not have a setting to support this directly, Typst should define a set of chars that should be removed and an other set of chars that should be kept and be searchable by a normal space.

    Of course, additionally the /ActualText method can be added later to get the original text. For LuaTeX I think they don't do it because afaik this requires PDF 1.7, but they require a minimum version of 1.5. Typst has 1.7 by default, so it can be done here.

  11. laurmaedje commented on Jul 15, 2024

    @laurmaedje
    Member

    Related: #4225, #1267, #671

  12. added
    text
    on Jul 15, 2024
  13. changed the title [-]Soft hyphens are searchable as spaces[/-] [+]Different characters that use the same glyph result in the same character when copied from PDFs[/+] on Jan 6, 2025
  14. laurmaedje commented on Jan 6, 2025

    @laurmaedje
    Member

    Smallcaps are of course also affected, so we should add a test for that too when fixing this issue: #text(features: ("smcp", "c2sc"))[Test] (originally reported in #5654).

  15. wade-cheng commented on May 21, 2026

    @wade-cheng

    https://link.springer.com/content/pdf/10.1007/s11948-017-9924-0.pdf found a pdf that's interesting because you can ctrl-f for words that are hyphenated across lines. that is, "pol-\nlution" in the first line of the abstract is searchable. and then I went digging in the issue tracker to find this closed issue. was there a regression?

    see

    #set par(justify: true)
    #set page(paper: "a9")
    
    dd dd bookkeeping
    

    on https://typst.app/play, where bookkeeping is linebroken and hyphenated as "bookkeep-ing," and can only be copy-pasted/ctrl-f'd via bookkeep ing. Presumably this is the problem that the above issue was meant to solve? I wonder if there's a font difference between the one used in the springer paper and the one in testing, but I can reproduce on the web app with at least the default font, Buenard, and Vollkorn.

    also, it seems that the springer paper's implementation of this eats true hyphens (ie. if policy-making gets broken at the hyphen or something, it can only be searched by policymaking). If I use bookkeep-ing in the mre above, it gets copied as bookkeep- ing, so we're better than it in this department.

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

    bugpdftext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions