Skip to content

Instantly share code, notes, and snippets.

@fiveoutofnine
Last active June 17, 2024 20:43
Show Gist options
  • Save fiveoutofnine/48d7f344433bba862c4151a7f3cf318f to your computer and use it in GitHub Desktop.
Save fiveoutofnine/48d7f344433bba862c4151a7f3cf318f to your computer and use it in GitHub Desktop.
Quick snippets/tutorial on how to condense a font by selecting a subset of characters. First, create a `.txt` file with the characters (as unicode chars) you want included.
U+0039
U+003A
U+002F
U+0023
U+0050
U+0075
U+007A
U+006C
U+0065
U+0041
U+0042
U+0043
U+0044
U+0045
U+0046
# pip3 install fonttools
pyftsubset ${FONT}.ttf --output-file=${FONT}-Subset.ttf --unicodes-file=1_example-glyphs.txt
import base64
FONT = "Font"
with open(f"{FONT}.txt", "w") as output_file:
with open(f"{FONT}.ttf", "rb") as input_file:
output_file.write(
f"data:font/ttf;utf-8;base64,{base64.b64encode(input_file.read()).decode('utf-8')}"
)
"""
You can now use it as follows:
<svg {...} >
<style type="text/css">
@font-face {
font-family: Font-Name;
src: url(...);
}
</style>
<text font-family="Font-Name">
some text
</text>
</svg>
"""
CHARACTERS = "This is the string with whatever characters you want to include"
for char in sorted(CHARACTERS):
print(f"U+{str(hex(ord(char))[2:]).zfill(4).upper()}")
@fiveoutofnine
Copy link
Author

adapted this into a blog post btw: https://fiveoutofnine.com/blog/on-chain-font

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment