Created
December 23, 2021 23:10
-
-
Save kpmiller/ce0da166963e4e513ca123e629577bb8 to your computer and use it in GitHub Desktop.
Source for making labels for spice jars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import Quartz | |
from Quartz.CoreGraphics import * | |
import Cocoa | |
# element : [ element width, name width word 1, name width word 2 (or 0.0), name ] | |
labels = { | |
"Cp" : [0.0, 0.0, 0.0, "Chili Powder"], | |
"Bs" : [0.0, 0.0, 0.0, "Black Sesame"], | |
"T" : [0.0, 0.0, 0.0, "Tumeric"], | |
"Cn" : [0.0, 0.0, 0.0, "Cinnamon"], | |
"Gm" : [0.0, 0.0, 0.0, "Garam Masala"], | |
"Ss" : [0.0, 0.0, 0.0, "Seasoned Salt"], | |
"Cy" : [0.0, 0.0, 0.0, "Cayenne Pepper"], | |
} | |
def GetWidth(page, by, by_len): | |
CGContextSetTextDrawingMode(page, kCGTextInvisible) | |
CGContextSetTextPosition(page, 10.0, 10.0) | |
CGContextShowText(page, by, by_len) | |
pos = CGContextGetTextPosition(page) | |
CGContextSetTextDrawingMode(page, kCGTextFill) | |
return pos.x - 10.0 | |
def MakeLabel(page, x, y, initials, words, size=60, initialsfontsize = 30, wordsfontsize=9): | |
r = CGRectMake(x, y, size, size) | |
CGContextSetLineWidth(page, 1.0) | |
CGContextStrokeRect(page,r) | |
#draw big "symbol" | |
init_y = y + size / 2 - 2 | |
CGContextSelectFont(page, b"Helvetica", initialsfontsize, kCGEncodingMacRoman) | |
w = words[0] | |
init_x = x + size/2 - (w/2) | |
CGContextSetTextPosition(page, init_x, init_y) | |
by = initials.encode('utf-8') | |
CGContextShowText(page, by, len(initials)) | |
#draw little "name" | |
CGContextSelectFont(page, b"Helvetica", wordsfontsize, kCGEncodingMacRoman) | |
init_y = y + size/4 | |
widx = 1 | |
for word in words[3].split(' '): | |
w = words[widx] | |
init_x = x + size/2 - (w/2) | |
CGContextSetTextPosition(page, init_x, init_y) | |
by = word.encode('utf-8') | |
CGContextShowText(page, by, len(word)) | |
widx = widx+1 | |
init_y = init_y - (wordsfontsize + 2.0) | |
if __name__ == '__main__': | |
initialsfontsize = 30 | |
wordsfontsize = 9 | |
keys = list(labels.keys()) | |
keys.sort() | |
#find out the widths the different labels | |
url = Cocoa.CFURLCreateWithFileSystemPath(None,"/tmp/labels.pdf",kCFURLPOSIXPathStyle,False) | |
pr = CGRectMake(0,0,612,792) | |
page = CGPDFContextCreateWithURL(url, pr, None) | |
CGPDFContextBeginPage(page, None) | |
CGContextSelectFont(page, b"Helvetica", initialsfontsize, kCGEncodingMacRoman) | |
for k in keys: | |
by = k.encode('utf-8') | |
elemwidth = GetWidth(page, by, len(k)) | |
labels[k][0] = elemwidth | |
CGContextSelectFont(page, b"Helvetica", wordsfontsize, kCGEncodingMacRoman) | |
for k in keys: | |
words = labels[k][3].split(' ') | |
w = 1 #index of | |
for word in words: | |
by = word.encode('utf-8') | |
elemwidth = GetWidth(page, by, len(word)) | |
labels[k][w] = elemwidth | |
w = w + 1 | |
CGPDFContextEndPage(page) | |
CGPDFContextClose(page) | |
url = Cocoa.CFURLCreateWithFileSystemPath(None,"/tmp/labels.pdf",kCFURLPOSIXPathStyle,False) | |
pr = CGRectMake(0,0,612,792) | |
page = CGPDFContextCreateWithURL(url, pr, None) | |
CGPDFContextBeginPage(page, None) | |
CGContextStrokeRect(page,pr) | |
for yyy in range(0, 7): | |
initials = keys[yyy] | |
words = labels[initials] | |
for xxx in range(0, 7): | |
x = xxx * 80 + 20 | |
y = yyy * 80 + 20 | |
MakeLabel(page, x, y, initials, words, initialsfontsize=initialsfontsize, wordsfontsize=wordsfontsize) | |
CGPDFContextEndPage(page) | |
CGPDFContextClose(page) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment