| 1 |
# This script is meant to be run from the root level |
| 2 |
# of your font's git repository. For example, from a Unix terminal: |
| 3 |
# $ git clone my-font |
| 4 |
# $ cd my-font |
| 5 |
# $ python3 documentation/image1.py --output documentation/image1.png |
| 6 |
|
| 7 |
# Import moduels from external python packages: https://pypi.org/ |
| 8 |
from drawbot_skia.drawbot import * |
| 9 |
from fontTools.ttLib import TTFont |
| 10 |
from fontTools.misc.fixedTools import floatToFixedToStr |
| 11 |
|
| 12 |
# Import moduels from the Python Standard Library: https://docs.python.org/3/library/ |
| 13 |
import subprocess |
| 14 |
import sys |
| 15 |
import argparse |
| 16 |
|
| 17 |
# Constants, these are the main "settings" for the image |
| 18 |
WIDTH, HEIGHT, MARGIN, FRAMES = 2048, 2048, 128, 1 |
| 19 |
FONT_PATH = "fonts/ttf/Rubik-Regular.ttf" |
| 20 |
FONT_LICENSE = "OFL v1.1" |
| 21 |
AUXILIARY_FONT = "Helvetica" |
| 22 |
AUXILIARY_FONT_SIZE = 48 |
| 23 |
BIG_TEXT = "Aa" |
| 24 |
BIG_TEXT_FONT_SIZE = 1024 |
| 25 |
BIG_TEXT_SIDE_MARGIN = MARGIN * 3.1 |
| 26 |
BIG_TEXT_BOTTOM_MARGIN = MARGIN * 5.5 |
| 27 |
GRID_VIEW = False # Change this to "True" for a grid overlay |
| 28 |
|
| 29 |
# Handel the "--output" flag |
| 30 |
# For example: $ python3 documentation/image1.py --output documentation/image1.png |
| 31 |
parser = argparse.ArgumentParser() |
| 32 |
parser.add_argument("--output", metavar="PNG", help="where to write the PNG file") |
| 33 |
args = parser.parse_args() |
| 34 |
|
| 35 |
# Load the font with the parts of fonttools that are imported with the line: |
| 36 |
# from fontTools.ttLib import TTFont |
| 37 |
# Docs Link: https://fonttools.readthedocs.io/en/latest/ttLib/ttFont.html |
| 38 |
ttFont = TTFont(FONT_PATH) |
| 39 |
|
| 40 |
# Constants that are worked out dynamically |
| 41 |
MY_URL = subprocess.check_output("git remote get-url origin", shell=True).decode() |
| 42 |
MY_HASH = subprocess.check_output("git rev-parse --short HEAD", shell=True).decode() |
| 43 |
FONT_NAME = ttFont["name"].getDebugName(4) |
| 44 |
FONT_VERSION = "v%s" % floatToFixedToStr(ttFont["head"].fontRevision, 16) |
| 45 |
|
| 46 |
|
| 47 |
# Draws a grid |
| 48 |
def grid(): |
| 49 |
stroke(1, 0, 0, 0.75) |
| 50 |
strokeWidth(2) |
| 51 |
STEP_X, STEP_Y = 0, 0 |
| 52 |
INCREMENT_X, INCREMENT_Y = MARGIN / 2, MARGIN / 2 |
| 53 |
rect(MARGIN, MARGIN, WIDTH - (MARGIN * 2), HEIGHT - (MARGIN * 2)) |
| 54 |
for x in range(29): |
| 55 |
polygon((MARGIN + STEP_X, MARGIN), (MARGIN + STEP_X, HEIGHT - MARGIN)) |
| 56 |
STEP_X += INCREMENT_X |
| 57 |
for y in range(29): |
| 58 |
polygon((MARGIN, MARGIN + STEP_Y), (WIDTH - MARGIN, MARGIN + STEP_Y)) |
| 59 |
STEP_Y += INCREMENT_Y |
| 60 |
polygon((WIDTH / 2, 0), (WIDTH / 2, HEIGHT)) |
| 61 |
polygon((0, HEIGHT / 2), (WIDTH, HEIGHT / 2)) |
| 62 |
|
| 63 |
|
| 64 |
# Remap input range to VF axis range |
| 65 |
# This is useful for animation |
| 66 |
# (E.g. sinewave(-1,1) to wght(100,900)) |
| 67 |
def remap(value, inputMin, inputMax, outputMin, outputMax): |
| 68 |
inputSpan = inputMax - inputMin # FIND INPUT RANGE SPAN |
| 69 |
outputSpan = outputMax - outputMin # FIND OUTPUT RANGE SPAN |
| 70 |
valueScaled = float(value - inputMin) / float(inputSpan) |
| 71 |
return outputMin + (valueScaled * outputSpan) |
| 72 |
|
| 73 |
|
| 74 |
# Draw the page/frame and a grid if "GRID_VIEW" is set to "True" |
| 75 |
def draw_background(): |
| 76 |
newPage(WIDTH, HEIGHT) |
| 77 |
fill(0) |
| 78 |
rect(-2, -2, WIDTH + 2, HEIGHT + 2) |
| 79 |
if GRID_VIEW: |
| 80 |
grid() |
| 81 |
else: |
| 82 |
pass |
| 83 |
|
| 84 |
|
| 85 |
# Draw main text |
| 86 |
def draw_main_text(): |
| 87 |
fill(1) |
| 88 |
stroke(None) |
| 89 |
font(FONT_PATH) |
| 90 |
fontSize(BIG_TEXT_FONT_SIZE) |
| 91 |
# Adjust this line to center main text manually. |
| 92 |
# TODO: This should be done automatically when drawbot-skia |
| 93 |
# has support for textBox() and FormattedString |
| 94 |
#text(BIG_TEXT, ((WIDTH / 2) - MARGIN * 4.75, (HEIGHT / 2) - MARGIN * 2.5)) |
| 95 |
text(BIG_TEXT, (BIG_TEXT_SIDE_MARGIN, BIG_TEXT_BOTTOM_MARGIN)) |
| 96 |
|
| 97 |
|
| 98 |
# Divider lines |
| 99 |
def draw_divider_lines(): |
| 100 |
stroke(1) |
| 101 |
strokeWidth(4) |
| 102 |
lineCap("round") |
| 103 |
line((MARGIN, HEIGHT - MARGIN), (WIDTH - MARGIN, HEIGHT - MARGIN)) |
| 104 |
line((MARGIN, MARGIN + (MARGIN / 2)), (WIDTH - MARGIN, MARGIN + (MARGIN / 2))) |
| 105 |
stroke(None) |
| 106 |
|
| 107 |
|
| 108 |
# Draw text describing the font and it's git status & repo URL |
| 109 |
def draw_auxiliary_text(): |
| 110 |
# Setup |
| 111 |
font(AUXILIARY_FONT) |
| 112 |
fontSize(AUXILIARY_FONT_SIZE) |
| 113 |
POS_TOP_LEFT = (MARGIN, HEIGHT - MARGIN * 1.5) |
| 114 |
POS_TOP_RIGHT = (WIDTH - MARGIN, HEIGHT - MARGIN * 1.5) |
| 115 |
POS_BOTTOM_LEFT = (MARGIN, MARGIN) |
| 116 |
POS_BOTTOM_RIGHT = (WIDTH - MARGIN * 0.95, MARGIN) |
| 117 |
URL_AND_HASH = MY_URL + "at commit " + MY_HASH |
| 118 |
URL_AND_HASH = URL_AND_HASH.replace("\n", " ") |
| 119 |
# Draw Text |
| 120 |
text(FONT_NAME, POS_TOP_LEFT, align="left") |
| 121 |
text(FONT_VERSION, POS_TOP_RIGHT, align="right") |
| 122 |
text(URL_AND_HASH, POS_BOTTOM_LEFT, align="left") |
| 123 |
text(FONT_LICENSE, POS_BOTTOM_RIGHT, align="right") |
| 124 |
|
| 125 |
|
| 126 |
# Build and save the image |
| 127 |
if __name__ == "__main__": |
| 128 |
draw_background() |
| 129 |
draw_main_text() |
| 130 |
draw_divider_lines() |
| 131 |
draw_auxiliary_text() |
| 132 |
# Save output, using the "--output" flag location |
| 133 |
saveImage(args.output) |
| 134 |
# Print done in the terminal |
| 135 |
print("DrawBot: Done") |