Last active
June 17, 2024 20:43
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip3 install fonttools | |
pyftsubset ${FONT}.ttf --output-file=${FONT}-Subset.ttf --unicodes-file=1_example-glyphs.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
adapted this into a blog post btw: https://fiveoutofnine.com/blog/on-chain-font