You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Copy-paste for hyphenated text copies hyphens #1267
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.
Reacted by Santtu Söderholm, ndg6, Andrew Voynov, Philipp Niedermayer and Dae
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.
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?
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?
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.
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.
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.
Reacted by Thorsten Sommer, kkdd-ddkk, Santtu Söderholm and Liara
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 translateandparse (Uhrig et al. 2021) .... Then, I had to awkwardly readd the hyphens and resend the text.
I can't even find using Ctrl+F with string translate-and-parse on Chrome PDF viewer. But, translateandparse will find it.
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.
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:
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).
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.
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:
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.
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.
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.
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).
Activity
reknih commentedon May 20, 2023
The PDF 1.7 spec has the following to say on this in clause 14.8.2.2.3:
reknih commentedon May 21, 2023
Hey, so they want us to draw a glyph (so either the glyph for
U+2010or, if not present in the font,U+002D) and map it to two different code pointsU+00ADfor hyphens that Typst inserted andU+002Dfor 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?laurmaedje commentedon May 21, 2023
Do we have to handle this with the
/ToUnicodemapping (and as such really with two separate glyphs) or we could we maybe also display the same glyph with different/ActualText?alerque commentedon May 21, 2023
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.
Enivex commentedon May 21, 2023
Indeed, the solution should be to follow the PDF spec and request that sumatrapdf and pdf.js follow it.
reknih commentedon May 23, 2023
IMO both
/ToUnicodecmaps and/ActualTexttagged 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.nafkhanzam commentedon May 31, 2023
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.
laurmaedje commentedon May 31, 2023
I don't get the use case for this customization. I think we should simply follow the PDF spec here.
nafkhanzam commentedon Jun 1, 2023
The problems that I have experienced were like:
Pendekatan translate-and-parse (Uhrig et al. 2021) .... I pasted it to the WhatsApp chat and it becamePendekatan translateandparse (Uhrig et al. 2021) .... Then, I had to awkwardly readd the hyphens and resend the text.Ctrl+Fwith stringtranslate-and-parseon Chrome PDF viewer. But,translateandparsewill find it.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.
2 remaining items
PPakalns commentedon Nov 8, 2023
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?
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:

Copied to python shell:
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).
laurmaedje commentedon Nov 9, 2023
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.
PPakalns commentedon Nov 9, 2023
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.
nafkhanzam commentedon Jan 17, 2024
I can't seem to reproduce the problem anymore, has this been fixed?
laurmaedje commentedon Jan 17, 2024
That's strange, nothing in this direction has been implemented.
PPakalns commentedon Jan 17, 2024
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.
nafkhanzam commentedon Jan 17, 2024
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.
Heinenen commentedon Jun 22, 2024
From the PDF 1.7 spec, section 14.8.2.4.2 Unicode Mapping in Tagged PDF:
This note seems to agree with @reknih's remark that both
/ToUnicodeand/ActualTextare viable solutions:Heinenen commentedon Jun 25, 2024
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).
[-]Replace minus-hyphen by hyphen character in PDF output[/-][+]Fix copy-paste of hyphenated text[/+][-]Fix copy-paste of hyphenated text[/-][+]Copy-paste for hyphenated text copies hyphens[/+]