From 70974c578414eb70ec53f799b7176d7232e3f3df Mon Sep 17 00:00:00 2001 From: Marc Foley Date: Mon, 14 Jun 2021 10:07:08 +0100 Subject: [PATCH 1/5] make designer dirs ascii --- Lib/gftools/utils.py | 5 +++++ bin/gftools-add-designer.py | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Lib/gftools/utils.py b/Lib/gftools/utils.py index 6a4ed06be..a29f06375 100644 --- a/Lib/gftools/utils.py +++ b/Lib/gftools/utils.py @@ -36,6 +36,7 @@ from configparser import ConfigParser else: from ConfigParser import ConfigParser +import unicodedata as uni # ===================================== # HELPER FUNCTIONS @@ -504,3 +505,7 @@ def read_proto(fp, schema): data = text_format.Parse(f.read(), schema) return data + +def strip_accents(string): + """Remove accents from a string e.g 'àce' --> 'ace'""" + return "".join([uni.normalize("NFD", x)[0] for x in string]) \ No newline at end of file diff --git a/bin/gftools-add-designer.py b/bin/gftools-add-designer.py index 6823029fd..60934a77a 100755 --- a/bin/gftools-add-designer.py +++ b/bin/gftools-add-designer.py @@ -34,6 +34,7 @@ from gftools.designers_pb2 import DesignerInfoProto from google.protobuf import text_format from pandas.core.base import PandasObject +from gftools.utils import strip_accents def process_image(fp): @@ -96,7 +97,9 @@ def make_designer( bio=None, urls=None, ): - designer_dir_name = name.lower().replace(" ", "").replace("-", "") + designer_dir_name = strip_accents( + name.lower().replace(" ", "").replace("-", "") + ) designer_dir = os.path.join(designer_directory, designer_dir_name) if not os.path.isdir(designer_dir): print(f"{name} isn't in catalog. Creating new dir {designer_dir}") From 3a3c91ee2f4618698b1808274c9725d77770d864 Mon Sep 17 00:00:00 2001 From: Marc Foley Date: Tue, 15 Jun 2021 14:42:10 +0100 Subject: [PATCH 2/5] add-designer: validate name --- bin/gftools-add-designer.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/bin/gftools-add-designer.py b/bin/gftools-add-designer.py index 60934a77a..3b1c282ab 100755 --- a/bin/gftools-add-designer.py +++ b/bin/gftools-add-designer.py @@ -90,6 +90,19 @@ def gen_hrefs(urls): return " | ".join(f"{v}" for k,v in res.items()) +def designer_name(name): + """Google Fonts only accepts designer names which use the Latin script""" + # Check all characters are within range U+0000 - U+024F (Basic Latin - Latin Ex-B) + # https://www.compart.com/en/unicode/plane/U+0000 + invalid_chars = [c for c in name if ord(c) > 0x024F] + if invalid_chars: + raise argparse.ArgumentTypeError( + f"'{name}' has the following illegal characters {invalid_chars}. " + "Please Latinize names." + ) + return name + + def make_designer( designer_directory, name, @@ -149,7 +162,7 @@ def make_designer( def main(): parser = argparse.ArgumentParser(usage=__doc__) parser.add_argument("designers_directory", help="path to google/fonts designer dir") - parser.add_argument("name", help="Designer name e.g 'Steve Matteson'") + parser.add_argument("name", help="Designer name e.g 'Steve Matteson'", type=designer_name) parser.add_argument("--img_path", help="Optional path to profile image", default=None) parser.add_argument( "--spreadsheet", help="Optional path to the Google Drive spreadsheet" From 33c520d944380c5fa046eb186e05adb47bd0a46e Mon Sep 17 00:00:00 2001 From: Marc Foley Date: Tue, 15 Jun 2021 14:51:47 +0100 Subject: [PATCH 3/5] use unidecode instead of custom strip_accents function --- Lib/gftools/utils.py | 5 ----- bin/gftools-add-designer.py | 6 +++--- requirements.txt | 1 + setup.py | 1 + 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Lib/gftools/utils.py b/Lib/gftools/utils.py index a29f06375..b3c168821 100644 --- a/Lib/gftools/utils.py +++ b/Lib/gftools/utils.py @@ -504,8 +504,3 @@ def read_proto(fp, schema): with open(fp, "rb") as f: data = text_format.Parse(f.read(), schema) return data - - -def strip_accents(string): - """Remove accents from a string e.g 'àce' --> 'ace'""" - return "".join([uni.normalize("NFD", x)[0] for x in string]) \ No newline at end of file diff --git a/bin/gftools-add-designer.py b/bin/gftools-add-designer.py index 3b1c282ab..5e456a6c2 100755 --- a/bin/gftools-add-designer.py +++ b/bin/gftools-add-designer.py @@ -34,7 +34,7 @@ from gftools.designers_pb2 import DesignerInfoProto from google.protobuf import text_format from pandas.core.base import PandasObject -from gftools.utils import strip_accents +from unidecode import unidecode def process_image(fp): @@ -110,8 +110,8 @@ def make_designer( bio=None, urls=None, ): - designer_dir_name = strip_accents( - name.lower().replace(" ", "").replace("-", "") + designer_dir_name = unidecode( + name.lower().replace(" ", "").replace("-", ""), "strict", ) designer_dir = os.path.join(designer_directory, designer_dir_name) if not os.path.isdir(designer_dir): diff --git a/requirements.txt b/requirements.txt index 83c8b1a9b..68efd3415 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,3 +23,4 @@ skia-pathops pandas xlrd openpyxl +unicdecode diff --git a/setup.py b/setup.py index 99589ea55..b32302a2a 100644 --- a/setup.py +++ b/setup.py @@ -102,5 +102,6 @@ def gftools_scripts(): 'pandas', 'xlrd', 'openpyxl', + 'unidecode', ] ) From 05a85a45c37cb22d9ed161c55c2a5487c7f80d7d Mon Sep 17 00:00:00 2001 From: Marc Foley Date: Tue, 15 Jun 2021 14:54:00 +0100 Subject: [PATCH 4/5] cleanup --- Lib/gftools/utils.py | 1 - requirements.txt | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/gftools/utils.py b/Lib/gftools/utils.py index b3c168821..e77840146 100644 --- a/Lib/gftools/utils.py +++ b/Lib/gftools/utils.py @@ -36,7 +36,6 @@ from configparser import ConfigParser else: from ConfigParser import ConfigParser -import unicodedata as uni # ===================================== # HELPER FUNCTIONS diff --git a/requirements.txt b/requirements.txt index 68efd3415..edf628d5b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,4 +23,4 @@ skia-pathops pandas xlrd openpyxl -unicdecode +unidecode From 7fe95670bc5939308c26aa5aa354206bb14d1b49 Mon Sep 17 00:00:00 2001 From: Marc Foley Date: Wed, 16 Jun 2021 10:34:52 +0100 Subject: [PATCH 5/5] crop rectangular images --- bin/gftools-add-designer.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bin/gftools-add-designer.py b/bin/gftools-add-designer.py index 5e456a6c2..e3701d195 100755 --- a/bin/gftools-add-designer.py +++ b/bin/gftools-add-designer.py @@ -43,7 +43,13 @@ def process_image(fp): img = Image.open(fp) width, height = img.size if width != height: - print("warning: img is rectangular when it should be square") + print("warning: img is rectangular. Cropping img") + if height < width: + crop_amount = (width - height) / 2 + img = img.crop((crop_amount, 0, width-crop_amount, height)) + elif width < height: + crop_amount = (height - width) / 2 + img = img.crop((0, crop_amount, width, height-crop_amount)) if width < 300 or height < 300: print("warning: img is smaller than 300x300px") return img