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.
90 lines
2.8 KiB
Python
90 lines
2.8 KiB
Python
import re # libary to search and replace strings in the postscript file
|
|
from dictionary import font_index
|
|
|
|
# function with two arguments
|
|
def friction_label(previous_module, module):
|
|
#text = input("Enter text:") # create input field for text input
|
|
text = "enter text here"
|
|
submit="0"
|
|
|
|
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
|
|
submit = module[9] # sensor6 # in this case it opens one ps, replaces some lines and writes to a new ps
|
|
|
|
|
|
script = """
|
|
|
|
newpath
|
|
|
|
/"""+font+"""
|
|
% fontname
|
|
|
|
"""+fontsize+""" selectfont
|
|
% fontsize in points, establishes the font as the current one
|
|
|
|
"""+x+""" """+y+""" 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
|
|
|
|
("""+text+""") true charpath
|
|
% normally text works with "show", but to use the characters in the string as a path that can be stroked and filled use "true charpath"
|
|
|
|
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
|
|
|
|
showpage
|
|
% print all on a page
|
|
"""
|
|
|
|
if submit == "1": # sensor6
|
|
print("starting to write the ps-file")
|
|
if previous_module == "":
|
|
#input file
|
|
fileInput = open("empty.ps", "rt") # rt = read text mode
|
|
#output file to write the result to
|
|
fileOutput = open("friction_label.ps", "wt") # wt = write text mode
|
|
|
|
else:
|
|
#input file
|
|
fileInput = open(previous_module + ".ps", "rt") # rt = read text mode
|
|
#output file to write the result to
|
|
fileOutput = open("friction_label.ps", "wt") # wt = write text mode
|
|
|
|
# read the input file, search for a patterns, replace it with new patterns (that take the values from the input sensor)
|
|
# uses regular expressions library (re)
|
|
content = fileInput.read()
|
|
content = re.sub(r"showpage\n% print all on a page", (script), content)
|
|
|
|
# write the new text to the output file
|
|
fileOutput.write(content)
|
|
#close input and output files
|
|
fileInput.close()
|
|
fileOutput.close()
|