Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow UTF-8 with BOM for features.fea #3495

Merged
merged 2 commits into from
May 30, 2024

Conversation

NightFurySL2001
Copy link
Contributor

@@ -269,7 +269,7 @@ def make_lexer_(file_or_path):
fileobj, closing = file_or_path, False
else:
filename, closing = file_or_path, True
fileobj = open(filename, "r", encoding="utf-8")
fileobj = open(filename, "r", encoding="utf-8-sig")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if the file is a regular utf-8 that does not not start with a BOM? Will this still work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes (as evident by the tests passing): this encoding just let Python skip the BOM mark if present in UTF-8 text, otherwise functionally it's the same as normal utf-8.

https://docs.python.org/3/library/codecs.html#encodings-and-unicode

On decoding utf-8-sig will skip those three bytes if they appear as the first three bytes in the file.

Do note that this is useful for opening text files (especially made by Windows Notepad). Saving as utf-8-sig is not recommended as it will add the BOM mark in which will break compatibility.

I would personally suggest read everything as utf-8-sig for greatest compatibility and save as utf-8 for standardisation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would personally suggest read everything as utf-8-sig for greatest compatibility and save as utf-8 for standardisation.

SGTM.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the only place where fontTools reads in human-written (potentially MS Notepad edited) text files? probably not. But sure let's merge this if it helps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far in the UFO building process, only features.fea had caused this problem. Other components are loaded in through plistlib which probably stripped out the BOM by default.

@anthrotype anthrotype merged commit 4193aea into fonttools:main May 30, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reading UFO failed if feature.fea saved with BOM mark
3 participants