You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.0 KiB
Python

import re # libary to search and replace strings in the postscript file
from dictionary import font_index
from random import choice
from generate_postscript import generate_postscript
# function with three arguments
def friction_label(previous_module, current_module, module):
font = font_index[module[1]] # use font_index from dictionary to translate the fontname
fontsize = module[2] # sensor3
rotation = module[3] # sensor2
linewidth = module[4]
x = module[5] # sensor4
y = module[6] # sensor 4, same the exact same value
graytone1 = module[7] # sensor5
graytone2 = module[8] # sensor5, opposite range
#use text from text file that was written by dynamic glyph module
textFile=open("dynamic_glyph.txt", "rt")
textContent = textFile.read()
if textContent != "": #use content of text file to define text variable
text = textContent
text = text.replace("\n", "")
elif textContent == "": #if text file is emptpy: randomly pick a default text (in case keyboard module is not used)
defaultText = ["Modular Matter", "MODULAR MATTER", "Rewire your prints!", "REWIRE YOUR PRINTS!"]
text = choice(defaultText)
# split the text into words to place them in separate lines
text = text.split(" ")
paragraph = ""
for word in text:
paragraph = paragraph + "(" + word + ") true charpath newline-friction-label "
script = """
newpath
% FRICTION LABEL
/"""+font+"""
% fontname
"""+fontsize+""" selectfont
% fontsize in points, establishes the font as the current one
/x-friction-label { """+x+""" } def
% define x position
/y-friction-label { """+y+""" } def
% define y position
/lg-friction-label { """+fontsize+""" 4 div """+fontsize+""" add } def
% define linespacing:
% fontsize divided by 4 plus fontsize
/newline-friction-label { y-friction-label lg-friction-label sub /y-friction-label exch def x-friction-label y-friction-label moveto } def
% define newline (position of new line) by using subtracting linespacing (lg) from y posiition and define that as the new y position (exch-ange), finally move to x position and re-defined y position
x-friction-label y-friction-label moveto
% x and y coordinates in px (origin is the lower-left corner of the page)
"""+rotation+""" rotate
% degree of rotation, counter clockwise around the given position
"""+paragraph+"""
gsave
% save the current path to apply stroke first and then go back to the saved path to apply fill (Both drawing commands destroy the current path)
"""+linewidth+""" setlinewidth
% linewidth for the outline of the text
"""+graytone2+""" setgray
% graytone for the outline (0 = black, 1 = white)
stroke
% stroke (outline) the text in parentheses
grestore
% restore the original path to apply fill
"""+graytone1+""" setgray
% graytone for the filling (0 = black, 1 = white)
fill
% fill the text in parentheses
-"""+rotation+""" rotate
% set rotation back to zero
showpage
% print all on a page
"""
generate_postscript(previous_module, current_module, script) # call function