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
Different characters that use the same glyph result in the same character when copied from PDFs #526
birth\u{ad}day
birthday // 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 birthday (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
Reacted by Arthur Araruna, Trebor Huang, astrale-sharp and kkdd-ddkk
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):
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:
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.
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.
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.
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!
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.
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.
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.
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
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).
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.
Activity
Enivex commentedon Feb 27, 2024
The same is true for zero width space.
TS60 commentedon Jun 1, 2024
I could debug it as follows:
write_normal_texthas the correct soft hyphen char for the soft hyphen glyph in theglyph_setcreate_cmapfunction already has the normal space for a soft hyphen glyph in theglyph_setBetween these two something happens that replaces the text in the
glyph_setfor the soft hyphen glyph from the correct char to the normal space.improve_glyph_setswill result in a space for the SHYimprove_glyph_sets(do nothing) will give the correct text atcreate_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):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:
Maybe you have an idea about it, @laurmaedje?
laurmaedje commentedon Jun 1, 2024
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.
TS60 commentedon Jun 1, 2024
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_IGNORABLESto 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.khaledhosny commentedon Jun 2, 2024
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.
laurmaedje commentedon Jun 2, 2024
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!
TS60 commentedon Jun 2, 2024
Thanks for the information! In the LuaTeX PDF (compression turned off) I cannot find
/ActualTextand it still works. If I remember correct,/ActualTextis 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,/ActualTextcould be used as an addition, so that poor pdf readers would still have the correct result.As I said above,
REMOVE_DEFAULT_IGNOREABLESsounds very much to be the difference between the searchable text in LuaTeX and Typst:"Flag indication that character with
Default_IgnorableUnicode 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.
khaledhosny commentedon Jun 2, 2024
Removing
Default_Ignorablemight 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.TS60 commentedon Jun 2, 2024
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
/ActualTextmethod 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.laurmaedje commentedon Jul 15, 2024
Related: #4225, #1267, #671
[-]Soft hyphens are searchable as spaces[/-][+]Different characters that use the same glyph result in the same character when copied from PDFs[/+]laurmaedje commentedon Jan 6, 2025
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).wade-cheng commentedon May 21, 2026
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
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-makinggets broken at the hyphen or something, it can only be searched bypolicymaking). If I usebookkeep-ingin the mre above, it gets copied asbookkeep- ing, so we're better than it in this department.