|
| 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, 1024, 128, 1 |
| 19 | +FONT_PATH = "fonts/ttf/SutasomaDisplay-Regular.ttf" |
| 20 | +FONT_LICENSE = "OFL v1.1" |
| 21 | +AUXILIARY_FONT = "Helvetica" |
| 22 | +AUXILIARY_FONT_SIZE = 48 |
| 23 | + |
| 24 | +LINE_ONE = "ABCDEFGHIJKLMNOPQ" |
| 25 | +LINE_TWO = "RSTUVWXYZ123456789" |
| 26 | +LINE_THREE = "abcdefghijklmnopqrstu" |
| 27 | +LINE_FOUR = "vwxyz,.;:!@#$%^&*(){}[]" |
| 28 | +BIG_TEXT_FONT_SIZE = 160 |
| 29 | +BIG_TEXT_SIDE_MARGIN = MARGIN * 1 |
| 30 | +BIG_TEXT_BOTTOM_MARGIN = MARGIN * 5.45 |
| 31 | + |
| 32 | +GRID_VIEW = False # Toggle this for a grid overlay |
| 33 | + |
| 34 | +# Handel the "--output" flag |
| 35 | +# For example: $ python3 documentation/image1.py --output documentation/image1.png |
| 36 | +parser = argparse.ArgumentParser() |
| 37 | +parser.add_argument("--output", metavar="PNG", help="where to write the PNG file") |
| 38 | +args = parser.parse_args() |
| 39 | + |
| 40 | +# Load the font with the parts of fonttools that are imported with the line: |
| 41 | +# from fontTools.ttLib import TTFont |
| 42 | +# Docs Link: https://fonttools.readthedocs.io/en/latest/ttLib/ttFont.html |
| 43 | +ttFont = TTFont(FONT_PATH) |
| 44 | + |
| 45 | +# Constants that are worked out dynamically |
| 46 | +MY_URL = subprocess.check_output("git remote get-url origin", shell=True).decode() |
| 47 | +MY_HASH = subprocess.check_output("git rev-parse --short HEAD", shell=True).decode() |
| 48 | +FONT_NAME = ttFont["name"].getDebugName(4) |
| 49 | +FONT_VERSION = "v%s" % floatToFixedToStr(ttFont["head"].fontRevision, 16) |
| 50 | + |
| 51 | + |
| 52 | +# Draws a grid |
| 53 | +def grid(): |
| 54 | + stroke(1, 0, 0, 0.75) |
| 55 | + strokeWidth(2) |
| 56 | + STEP_X, STEP_Y = 0, 0 |
| 57 | + INCREMENT_X, INCREMENT_Y = MARGIN / 2, MARGIN / 2 |
| 58 | + rect(MARGIN, MARGIN, WIDTH - (MARGIN * 2), HEIGHT - (MARGIN * 2)) |
| 59 | + for x in range(29): |
| 60 | + polygon((MARGIN + STEP_X, MARGIN), (MARGIN + STEP_X, HEIGHT - MARGIN)) |
| 61 | + STEP_X += INCREMENT_X |
| 62 | + for y in range(29): |
| 63 | + polygon((MARGIN, MARGIN + STEP_Y), (WIDTH - MARGIN, MARGIN + STEP_Y)) |
| 64 | + STEP_Y += INCREMENT_Y |
| 65 | + polygon((WIDTH / 2, 0), (WIDTH / 2, HEIGHT)) |
| 66 | + polygon((0, HEIGHT / 2), (WIDTH, HEIGHT / 2)) |
| 67 | + |
| 68 | + |
| 69 | +# Remap input range to VF axis range |
| 70 | +# This is useful for animation |
| 71 | +# (E.g. sinewave(-1,1) to wght(100,900)) |
| 72 | +def remap(value, inputMin, inputMax, outputMin, outputMax): |
| 73 | + inputSpan = inputMax - inputMin # FIND INPUT RANGE SPAN |
| 74 | + outputSpan = outputMax - outputMin # FIND OUTPUT RANGE SPAN |
| 75 | + valueScaled = float(value - inputMin) / float(inputSpan) |
| 76 | + return outputMin + (valueScaled * outputSpan) |
| 77 | + |
| 78 | + |
| 79 | +# Draw the page/frame and a grid if "GRID_VIEW" is set to "True" |
| 80 | +def draw_background(): |
| 81 | + newPage(WIDTH, HEIGHT) |
| 82 | + fill(0) |
| 83 | + rect(-2, -2, WIDTH + 2, HEIGHT + 2) |
| 84 | + if GRID_VIEW: |
| 85 | + grid() |
| 86 | + else: |
| 87 | + pass |
| 88 | + |
| 89 | + |
| 90 | +# Draw main text |
| 91 | +def draw_main_text(): |
| 92 | + fill(1) |
| 93 | + stroke(None) |
| 94 | + font(FONT_PATH) |
| 95 | + fontSize(BIG_TEXT_FONT_SIZE) |
| 96 | + # Adjust this line to center main text manually. |
| 97 | + # TODO: This should be done automatically when drawbot-skia |
| 98 | + # has support for textBox() and FormattedString |
| 99 | + LEADING = 1.2 |
| 100 | + text(LINE_ONE, (BIG_TEXT_SIDE_MARGIN, BIG_TEXT_BOTTOM_MARGIN)) |
| 101 | + text(LINE_TWO, (BIG_TEXT_SIDE_MARGIN, BIG_TEXT_BOTTOM_MARGIN - (MARGIN * LEADING))) |
| 102 | + text( |
| 103 | + LINE_THREE, |
| 104 | + (BIG_TEXT_SIDE_MARGIN, BIG_TEXT_BOTTOM_MARGIN - (MARGIN * (LEADING * 2))), |
| 105 | + ) |
| 106 | + text( |
| 107 | + LINE_FOUR, |
| 108 | + (BIG_TEXT_SIDE_MARGIN, BIG_TEXT_BOTTOM_MARGIN - (MARGIN * (LEADING * 3))), |
| 109 | + ) |
| 110 | + |
| 111 | + |
| 112 | +# Divider lines |
| 113 | +def draw_divider_lines(): |
| 114 | + stroke(1) |
| 115 | + strokeWidth(5) |
| 116 | + lineCap("round") |
| 117 | + line((MARGIN, HEIGHT - (MARGIN * 1.5)), (WIDTH - MARGIN, HEIGHT - (MARGIN * 1.5))) |
| 118 | + line((MARGIN, MARGIN + (MARGIN / 2)), (WIDTH - MARGIN, MARGIN + (MARGIN / 2))) |
| 119 | + stroke(None) |
| 120 | + |
| 121 | + |
| 122 | +# Draw text describing the font and it's git status & repo URL |
| 123 | +def draw_auxiliary_text(): |
| 124 | + # Setup |
| 125 | + font(AUXILIARY_FONT) |
| 126 | + fontSize(AUXILIARY_FONT_SIZE) |
| 127 | + POS_TOP_LEFT = (MARGIN, HEIGHT - MARGIN * 1.25) |
| 128 | + POS_TOP_RIGHT = (WIDTH - MARGIN, HEIGHT - MARGIN * 1.25) |
| 129 | + POS_BOTTOM_LEFT = (MARGIN, MARGIN) |
| 130 | + POS_BOTTOM_RIGHT = (WIDTH - MARGIN * 0.95, MARGIN) |
| 131 | + # URL_AND_HASH = "github.com/googlefonts/googlefonts-project-template " + "at commit " + MY_HASH |
| 132 | + URL_AND_HASH = MY_URL + "at commit " + MY_HASH |
| 133 | + URL_AND_HASH = URL_AND_HASH.replace("\n", " ") |
| 134 | + # Draw Text |
| 135 | + # text("Your Font Regular", POS_TOP_LEFT, align="left") |
| 136 | + text(FONT_NAME, POS_TOP_LEFT, align="left") |
| 137 | + text(FONT_VERSION, POS_TOP_RIGHT, align="right") |
| 138 | + text(URL_AND_HASH, POS_BOTTOM_LEFT, align="left") |
| 139 | + text(FONT_LICENSE, POS_BOTTOM_RIGHT, align="right") |
| 140 | + |
| 141 | + |
| 142 | +# Build and save the image |
| 143 | +if __name__ == "__main__": |
| 144 | + draw_background() |
| 145 | + draw_main_text() |
| 146 | + draw_divider_lines() |
| 147 | + draw_auxiliary_text() |
| 148 | + # Save output, using the "--output" flag location |
| 149 | + saveImage(args.output) |
| 150 | + # Print done in the terminal |
| 151 | + print("DrawBot: Done") |
0 commit comments