From 1fbac99bd88d40c83615f70387ffd7f8bd03a3df Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Sat, 18 Jan 2025 18:21:33 +0100 Subject: [PATCH] add space recognition --- __init__.py | 2 +- common/Font.py | 42 +++++++++++++++++++++++++++++++++++----- common/spacesUnicode.txt | 23 ++++++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 common/spacesUnicode.txt diff --git a/__init__.py b/__init__.py index 72193c4..cb3c10d 100644 --- a/__init__.py +++ b/__init__.py @@ -1589,7 +1589,7 @@ def register(): # bpy.ops.abc3d.load_installed_fonts() - Font.name_to_glyph_d = Font.generate_name_to_glyph_d() + Font.init() def unregister(): diff --git a/common/Font.py b/common/Font.py index aa936db..7b36da5 100644 --- a/common/Font.py +++ b/common/Font.py @@ -37,6 +37,8 @@ name_to_glyph_d = { "space": " ", } +space_d = {} + known_misspellings = { # simple misspelling "excent" : "accent", @@ -74,17 +76,47 @@ def name_to_glyph(name): else: return None -def generate_name_to_glyph_d(): + +def is_space(character): + for name in space_d: + if character == space_d[name][0]: + return space_d[name][1] + return False + + +def generate_from_file_d(filepath): + print(f"{filepath=}") d = {} - with open(f"{Path(__file__).parent}/glyphNamesToUnicode.txt") as f: + with open(filepath) as f: for line in f: if line[0] == '#': continue - (name, hexstr) = line.split(' ') - val = chr(int(hexstr, base=16)) - d[name] = val + split = line.split(' ') + if len(split) == 2: + (name, hexstr) = line.split(' ') + val = chr(int(hexstr, base=16)) + d[name] = val + print(f"{name=} {val=}") + if len(split) == 3: + # we might have a parameter, like for the spaces + (name, hexstr, parameter) = line.split(' ') + parameter_value = float(parameter) + val = chr(int(hexstr, base=16)) + d[name] = [val, parameter_value] + print(f"{name=} {val=}, {parameter_value=}, {parameter_value * 2}") return d +def generate_name_to_glyph_d(): + return generate_from_file_d(f"{Path(__file__).parent}/glyphNamesToUnicode.txt") + +def generate_space_d(): + return generate_from_file_d(f"{Path(__file__).parent}/spacesUnicode.txt") + +def init(): + global name_to_glyph_d + global space_d + name_to_glyph_d = generate_name_to_glyph_d() + space_d = generate_space_d() class FontFace: """FontFace is a class holding glyphs diff --git a/common/spacesUnicode.txt b/common/spacesUnicode.txt new file mode 100644 index 0000000..da6a7c9 --- /dev/null +++ b/common/spacesUnicode.txt @@ -0,0 +1,23 @@ +# The space value derives from The Elements of Typographic Style +# same for en-/em values. Rest are rough guesses. +space 0020 0.25 +nbspace 00A0 0.25 +# ethi:wordspace 1361 # NOTE: has shape +enquad 2000 0.5 +emquad 2001 1 +enspace 2002 0.5 +emspace 2003 1 +threeperemspace 2004 3 +fourperemspace 2005 4 +sixperemspace 2006 6 +figurespace 2007 1 +punctuationspace 2008 1 +thinspace 2009 0.1 +hairspace 200A 0.05 +zerowidthspace 200B 0 +narrownobreakspace 202F 0.1 +mediummathematicalspace 205F 1 +cntr:space 2420 0.25 +ideographicspace 3000 1 +# ideographichalffillspace 303F # NOTE: has shape +zerowidthnobreakspace FEFF 0