Skip to content

Copy-paste for hyphenated text copies hyphens #1267

Closed

Description

Description

Problem
Currently when hyphenation is applied to a word, the word copies as prefix- suffix. Some PDF readers will copy this as prefixsuffix because they are sure enough that the minus character was a hyphenation character.

Using this, we can't really know if this hyphenation was done by an algorithm or written like this by the author, because the minus sign is used as hyphenation character.

Solution
Use the hyphen character „‐“ (U+2010) if and only if the hyphen character came from the hyphenation algorithm.
If a line break appeared within a word that already had some hyphenation character (mostly the minus sign), the hyphen character should not be used. Instead, the character should be used that was already in the input text.
When the text had a soft hyphen, the hyphen character should also be used.

Maybe this solution can only be applied if the glyph is available in the font. If so and the glyph is available in the font, it should be used, otherwise, stay with the minus sign if the font does not have the hyphenation character.

Use Case

It will be possible to copy and recreate the original input text much better and will give PDF readers better knowledge about the text to copy. It can also improve the text searching in the PDF.

Activity

  1. ghost added
    feature request
    on May 20, 2023
  2. added
    text
    on May 20, 2023
  3. reknih commented on May 20, 2023

    @reknih
    Member

    The PDF 1.7 spec has the following to say on this in clause 14.8.2.2.3:

    Hyphenation. Among the artifacts introduced by text layout is the hyphen marking the incidental division of a word at the end of a line. In Tagged PDF, such an incidental word division shall be represented by a soft hyphen character, which the Unicode mapping algorithm (see "Unicode Mapping in Tagged PDF" in 14.8.2.4, "Extraction of Character Properties") translates to the Unicode value U+00AD. (This character is distinct from an ordinary hard hyphen, whose Unicode value is U+002D.) The producer of a Tagged PDF document shall distinguish explicitly between soft and hard hyphens so that the consumer does not have to guess which type a given character represents.

  4. reknih commented on May 21, 2023

    @reknih
    Member

    Hey, so they want us to draw a glyph (so either the glyph for U+2010 or, if not present in the font, U+002D) and map it to two different code points U+00AD for hyphens that Typst inserted and U+002D for user-defined hyphens. They want us to rewrite the font if you will. Does that clarify it?

    On a different note: Do you know whether it's common for fonts to include a distinct glyph for U+2010?

  5. laurmaedje commented on May 21, 2023

    @laurmaedje
    Member

    Do we have to handle this with the /ToUnicode mapping (and as such really with two separate glyphs) or we could we maybe also display the same glyph with different /ActualText?

  6. alerque commented on May 21, 2023

    @alerque
    Contributor

    Another possibility that came to my mind after writing this:

    No offense intended, but please stop inventing ad-hoc solutions to this problem. It's hard enough to get PDF tooling in the wild to follow the spec when there is a specific spec for something, it's 100× harder when actual PDFs invent their own rules.

  7. Enivex commented on May 21, 2023

    @Enivex
    Collaborator

    Indeed, the solution should be to follow the PDF spec and request that sumatrapdf and pdf.js follow it.

  8. reknih commented on May 23, 2023

    @reknih
    Member

    IMO both /ToUnicode cmaps and /ActualText tagged content sequences would comply with the spec's requirements since they are both valid mechanisms to describe a content stream's Unicode content, but the former seems to have greater viewer support while the latter is easier for us.

  9. nafkhanzam commented on May 31, 2023

    @nafkhanzam

    Why don't we just add a configurable parameter so that the Typst's user can decide what type of hyphen to use? With the default value, of course, following the PDF spec.

    Because currently now I have a case where I want to copy and paste paragraphs from the pdf and some other cases in the future where I want to follow the pdf spec.

  10. laurmaedje commented on May 31, 2023

    @laurmaedje
    Member

    I don't get the use case for this customization. I think we should simply follow the PDF spec here.

  11. nafkhanzam commented on Jun 1, 2023

    @nafkhanzam

    The problems that I have experienced were like:

    1. When I was discussing something with my supervisor, and I wanted to copy-paste a sentence from my report that was written as Pendekatan translate-­and-­parse (Uhrig et al. 2021) .... I pasted it to the WhatsApp chat and it became Pendekatan translateand­parse (Uhrig et al. 2021) .... Then, I had to awkwardly readd the hyphens and resend the text.
    2. I can't even find using Ctrl+F with string translate-and-parse on Chrome PDF viewer. But, translateandparse will find it.
    3. I want to translate a paragraph using Google Translate, I had to readd the hyphens manually to make sense the words for the machine translator. I also thought, why didn't I just copy and paste the source code? Well, it was more complicated since the source code was like this: Pendekatan _translate-and-parse_ @uhrig2021 ...

    Any professional paper PDF files use non-spec hyphens anyway, so I prefer usability than being too perfectionist. And adding a configurable customization could satisfy both sides of arguments.

    NB: I don't really understand much about the PDF spec, I'm just telling my experiences that I never had in LaTeX or ms word.

  12. 2 remaining items

  13. PPakalns commented on Nov 8, 2023

    @PPakalns

    We should disable auto conversion of characters at least for raw blocks.


    How to keep code/raw part of document (not text part) from converting minus sign to hyphenation characters?

    #raw("-5")
    

    Even raw block converts 0x2d to 0xad.

    This is very painful for documents which contain parts of raw data like example input/output for programming tasks that are copied to terminal and all symbols should be kept as in input.

    Example:
    image

    Copied to python shell:

    >>> ­5
      File "<stdin>", line 1
        ­5
        ^
    SyntaxError: invalid non-printable character U+00AD
    >>> 3
    3
    

    When converting docx and latex documents to pdf we didn't have this kind of problem.

    It is important that minus sign is copied as ascii minus sign (0x2d).

  14. laurmaedje commented on Nov 9, 2023

    @laurmaedje
    Member

    There is no conversion in Typst, neither for raw blocks nor other text. The conversion happens in your PDF reader because it has "smart" behaviour to get rid of hyphenation artifacts. There is no easy workaround until Typst implements the special behaviour mandated by the PDF spec.

  15. PPakalns commented on Nov 9, 2023

    @PPakalns

    We should disable auto conversion of characters at least for raw blocks.

    How to keep code/raw part of document (not text part) from converting minus sign to hyphenation characters?

    #raw("-5")
    

    Even raw block converts 0x2d to 0xad.

    This is very painful for documents which contain parts of raw data like example input/output for programming tasks that are copied to terminal and all symbols should be kept as in input.

    Example: image

    Copied to python shell:

    >>> ­5
      File "<stdin>", line 1
        ­5
        ^
    SyntaxError: invalid non-printable character U+00AD
    >>> 3
    3
    

    When converting docx and latex documents to pdf we didn't have this kind of problem.

    It is important that minus sign is copied as ascii minus sign (0x2d).

    Found out that this problem doesn't happen with another font.

    In my case Liberation Sans Mono => Noto Sans Mono. For now i have a workaround for this problem.

  16. added
    pdf
    on Nov 14, 2023
  17. nafkhanzam commented on Jan 17, 2024

    @nafkhanzam

    I can't seem to reproduce the problem anymore, has this been fixed?

  18. laurmaedje commented on Jan 17, 2024

    @laurmaedje
    Member

    That's strange, nothing in this direction has been implemented.

  19. PPakalns commented on Jan 17, 2024

    @PPakalns

    I can't seem to reproduce the problem anymore, has this been fixed?

    Tried changing back (Liberation Sans Mono => Noto Sans Mono), which fixed the issue for me.
    And the problem doesn't happen anymore for me too.

  20. nafkhanzam commented on Jan 17, 2024

    @nafkhanzam

    I can't seem to reproduce the problem anymore, has this been fixed?

    Oops, nevermind. The problem still persists when I was using "Times New Roman.ttf" (or otf), exactly like what @PPakalns was experiencing. It works again when I'm using the default font.

    To be clear: I think this is just a sub-issue from this original issue. The original issue still persists in any font.

  21. Heinenen commented on Jun 22, 2024

    @Heinenen
    Contributor

    From the PDF 1.7 spec, section 14.8.2.4.2 Unicode Mapping in Tagged PDF:

    NOTE 3 Some uses of Tagged PDF require characters that may not be available in all fonts, such as the soft hyphen
    (see 14.8.2.2.3, “Incidental Artifacts”). Such characters may be represented either by adding them to the font’s
    encoding or CMap and using ToUnicode to map them to appropriate Unicode values, or by using an
    ActualText entry in the associated structure element to provide substitute characters.

    This note seems to agree with @reknih's remark that both /ToUnicode and /ActualText are viable solutions:

    IMO both /ToUnicode cmaps and /ActualText tagged content sequences would comply with the spec's requirements since they are both valid mechanisms to describe a content stream's Unicode content, but the former seems to have greater viewer support while the latter is easier for us.

  22. Heinenen commented on Jun 25, 2024

    @Heinenen
    Contributor

    Hyphenation. Among the artifacts introduced by text layout is the hyphen marking the incidental division of a word at the end of a line. In Tagged PDF, such an incidental word division shall be represented by a soft hyphen character, which the Unicode mapping algorithm (see "Unicode Mapping in Tagged PDF" in 14.8.2.4, "Extraction of Character Properties") translates to the Unicode value U+00AD. (This character is distinct from an ordinary hard hyphen, whose Unicode value is U+002D.) The producer of a Tagged PDF document shall distinguish explicitly between soft and hard hyphens so that the consumer does not have to guess which type a given character represents.

    I just reread this part of the spec again and the way I understand it is: write U+002D (hyphen-minus) when the hyphen wasn't inserted by Typst and write U+00AD (soft-hyphen) with an appropriate hyphen glyph when it was inserted by Typst

    My interpretation especially leans on the sentence "an ordinary hard hyphen, whose Unicode value is U+002D" and that U+2010 (hyphen) is never mentioned (not once in the whole spec).

  23. added
    bug
    and removed
    feature request
    on Jul 15, 2024
  24. changed the title [-]Replace minus-hyphen by hyphen character in PDF output[/-] [+]Fix copy-paste of hyphenated text[/+] on Jan 6, 2025
  25. changed the title [-]Fix copy-paste of hyphenated text[/-] [+]Copy-paste for hyphenated text copies hyphens[/+] on Jan 6, 2025
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