Skip to content

Instantly share code, notes, and snippets.

@so298
Created August 5, 2024 16:17
Show Gist options
  • Save so298/1035062b9ea208516a3c128a14a1adb6 to your computer and use it in GitHub Desktop.
Save so298/1035062b9ea208516a3c128a14a1adb6 to your computer and use it in GitHub Desktop.
Script to check if font file contains font for a specific unicode character
#!/usr/bin/env python3
import sys
def unicode_escape(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
escaped_content = content.encode('unicode_escape').decode('ascii')
# 改行をエスケープ
escaped_content = escaped_content.replace('\\n', '\n').replace('\\r', '\r')
print(escaped_content)
except FileNotFoundError:
print(f"Error: The file {file_path} was not found.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <file_path>")
else:
unicode_escape(sys.argv[1])
import sys
from fontTools.ttLib import TTFont
def parse_unicode_escape(unicode_escape_str):
"""Convert a Unicode escape sequence to a character."""
return chr(int(unicode_escape_str[2:], 16))
def check_unicode_in_otf(otf_file, unicode_char):
"""Check if a specific Unicode character is in the OTF file."""
try:
font = TTFont(otf_file)
cmap = font['cmap']
for table in cmap.tables:
if ord(unicode_char) in table.cmap:
return True
return False
except FileNotFoundError:
print(f"Error: The file {otf_file} was not found.")
return False
except Exception as e:
print(f"An error occurred: {e}")
return False
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python script.py <otf_file> <U+XXXXXXXX>")
else:
otf_file = sys.argv[1]
unicode_escape_str = sys.argv[2]
if not unicode_escape_str.startswith("U+") or not unicode_escape_str[2:].isalnum():
print("Error: The second argument should be a Unicode escape sequence in the form U+XXXXXXXX.")
else:
try:
unicode_char = parse_unicode_escape(unicode_escape_str)
result = check_unicode_in_otf(otf_file, unicode_char)
if result:
print(f"The Unicode character '{unicode_char}' (U+{unicode_escape_str[2:]}) is present in the OTF file.")
else:
print(f"The Unicode character '{unicode_char}' (U+{unicode_escape_str[2:]}) is NOT present in the OTF file.")
except ValueError:
print("Error: Invalid Unicode escape sequence.")
import sys
def unicode_to_sfd_encoding(unicode_char):
"""Convert a Unicode character to its SFD encoding format."""
return ord(unicode_char)
def check_unicode_in_sfd(sfd_file, unicode_char):
"""Check if a specific Unicode character is in the SFD file."""
encoding = unicode_to_sfd_encoding(unicode_char)
try:
with open(sfd_file, 'r', encoding='utf-8') as file:
content = file.readlines()
for line in content:
if line.startswith("StartChar:"):
start_index = content.index(line)
encoding_line = content[start_index + 1]
if f"Encoding: {encoding} {encoding}" in encoding_line:
return True
return False
except FileNotFoundError:
print(f"Error: The file {sfd_file} was not found.")
return False
except Exception as e:
print(f"An error occurred: {e}")
return False
def parse_unicode_escape(unicode_escape_str):
"""Convert a Unicode escape sequence to a character."""
return chr(int(unicode_escape_str[2:], 16))
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python script.py <sfd_file> <U+XXXXXXXX>")
else:
sfd_file = sys.argv[1]
unicode_escape_str = sys.argv[2]
if not unicode_escape_str.startswith("U+") or not unicode_escape_str[2:].isalnum():
print("Error: The second argument should be a Unicode escape sequence in the form U+XXXXXXXX.")
else:
try:
unicode_char = parse_unicode_escape(unicode_escape_str)
result = check_unicode_in_sfd(sfd_file, unicode_char)
if result:
print(f"The Unicode character '{unicode_char}' (U+{unicode_escape_str[2:]}) is present in the SFD file.")
else:
print(f"The Unicode character '{unicode_char}' (U+{unicode_escape_str[2:]}) is NOT present in the SFD file.")
except ValueError:
print("Error: Invalid Unicode escape sequence.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment