diff --git a/common.typ b/common.typ index 21fec70..67568f0 100644 --- a/common.typ +++ b/common.typ @@ -5,25 +5,10 @@ #let place_avatar(text_height, card) = { style(styles => { let text_offset = (text_height - measure(address_text(card), styles).height) / 2 - place(top + left, - block( - width: 1in, - height: text_offset, - fill: luma(180), - ) - ) - place(top + left, - dy: text_offset, - block( - width: 1in, - height: 4pt, - fill: luma(160), - ) - ) place( top + left, dx: 0.1in, - dy: text_offset + 4pt - 0.15in, + dy: calc.max(text_offset + 4pt - 0.15in, 0pt), image(card.avatar, width: 0.3in) ) }) @@ -43,7 +28,6 @@ block( width: width - 0.5in, height: text_height, - fill: luma(230), align( start + horizon, address_text(card) @@ -57,7 +41,6 @@ block( width: 100%, height: 1in/8, - fill: luma(220), align( top + center, text(font: "USPSIMBCompact", size: 12pt, card.imb) diff --git a/flake.nix b/flake.nix index f6ee314..6db4336 100644 --- a/flake.nix +++ b/flake.nix @@ -27,7 +27,7 @@ packages = [ typst python3 - python3Packages.autopep8 + python3Packages.black python3Packages.requests ]; diff --git a/format.py b/format.py index b7560c1..64af6b3 100755 --- a/format.py +++ b/format.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import argparse +import base64 import csv import json import os @@ -19,18 +20,39 @@ def iso_code(s: str) -> str: return s -def get_avatar(url: str) -> str: +def get_orig_avatar(url: str, name: str) -> typing.Optional[bytes]: if not os.path.exists("cache"): os.mkdir("cache") - name = url.split("?")[0].split("/")[-1] if os.path.exists("cache/" + name): - return "cache/" + name + with open("cache/" + name, "rb") as infile: + return infile.read() result = requests.get(url) if result.ok: with open("cache/" + name, "wb") as outfile: outfile.write(result.content) - return "cache/" + name - return "" + return result.content + return None + + +def get_avatar(url: str) -> str: + name = url.split("?")[0].split("/")[-1] + if os.path.exists(f"cache/{name}.svg"): + return f"cache/{name}.svg" + avatar_raster = get_orig_avatar(url, name) + if avatar_raster is None: + return "" + + svg_text = f""" + + + + + """ + + with open(f"cache/{name}.svg", "w") as svgfile: + svgfile.write(svg_text) + return f"cache/{name}.svg" def get_country_name( @@ -51,8 +73,7 @@ def get_country_name( parser = argparse.ArgumentParser( prog="format", description="format postcards with latex" ) -parser.add_argument("template", help="template to use", - nargs="?", default="2card") +parser.add_argument("template", help="template to use", nargs="?", default="2card") parser.add_argument( "-o", "--origin", help="origin country code", default="us", type=iso_code )