
# ----------------------------
# D E S C R I P T I O N

# This script was written in DrawBot, a Python-base,
# Mac-only standalone app intended for generating
# 2D-graphics and animations.

# Download the newest version of DrawBot here:
# https://www.drawbot.com/content/download.html

# This script will combine a defined amount
# of styles from the pixel fonts BAGIxel2023
# and randomly choose stylistic varitions and
# colour values for each layer. In order to alter
# the result, change the text input and values
# under SETTINGS and COLOURS.

# The file output is defined at the bottom; this
# script generates .gif and .mp4 files. The amount
# and duration of each frame can be defined under
# SETTINGS as well.

# All use of the fonts and this script is at
# your own risk. Have fun!


# ----------------------------

import random
from datetime import datetime



# ----------------------------
# S E T T I N G S 

# Change the displayed text
text_string="This is it!"

# Change the number of layers
layers = 4

# Change the typesize
fnt_size = 1000

# Change the number of frames (do not overdo it; max. 50 frames!)
frames = 4

# Change the frame duration (in seconds)
frame_duration = .5



# ----------------------------
# C O L O U R S 
# Change the range of colour randomness

min_RED = 0
max_RED = 255

min_GREEN = 0
max_GREEN = 255

min_BLUE = 0
max_BLUE = 255
















# ----------------------------
# F O N T S

font_path = '../'

fnt_fam_name = font_path + "BAGIxel2023-"

styles = [
    "Aksel",
    "Alma",
    "Margit",
    "Martine",
    "Max",
    "Sebastian",
    "Selma",
    "Silje",
    "Vilde"
    ]

feature_names = [ f'ss{i:02d}' for i in range(1, 21)]

style_order = [random.choice(styles) for _ in range(len(text_string))]



# ----------------------------
#  F U N C T I O N S   

def get_width_of_text(txt):
    fstr = FormattedString(fontSize = fnt_size)
    for c, char in enumerate(txt):
        fnt = fnt_fam_name + style_order[c] +".ttf"
        fstr.append(char, font = fnt)
    return fstr.size()



# ----------------------------
#  D R A W I N G S  

margin = fnt_size * .3

fstr_w, fstr_h = get_width_of_text(text_string)
PW, PH = fstr_w + 2* margin, 2 * margin + fnt_size * 1.15 + fnt_size * 1.15 * text_string.count('\n')

for f in range(frames):
    newPage(PW, PH)
    frameDuration(frame_duration)
    fill(1)
    rect(0, 0, PW, PH)
    blendMode("multiply")    
    
    
    for l in range(layers):

        fstr = FormattedString(fontSize = fnt_size)

        for c, char in enumerate(text_string):
            fnt = fnt_fam_name + style_order[c] +".ttf"
            feature = random.choice(feature_names)
            features_dict = {feature : True}
            fstr.append(char, font = fnt, openTypeFeatures=features_dict, fill=(random.uniform(min_RED/255,max_RED/255),random.uniform(min_GREEN/255,max_GREEN/255),random.uniform(min_BLUE/255,max_BLUE/255)))
            features_dict = {feature : False}
            fstr.openTypeFeatures(**features_dict)
    
        text(fstr, (margin, PH-margin-fnt_size * .95))
        


# ----------------------------
# E X P O R T

timeStamp = datetime.now().strftime("%y%m%d-%H%M%S")
file_name = f'{text_string}-{timeStamp}'

saveImage(file_name+".gif")
saveImage(file_name+".mp4")