@@ -19,36 +19,62 @@ def createBarcode(text: str, type: str):
19
19
case _:
20
20
return createDatamatrix (text )
21
21
22
- def createLabelImage (labelSize : tuple , text : str , textFont : ImageFont , textMaxLines : int , barcode : Image , dueDate : str , dueDateFont : ImageFont ):
22
+ def createLabelImage (labelSize : tuple , endlessMargin : int , text : str , textFont : ImageFont , textFontSize : int , textMaxLines : int , barcode : Image , dueDate : str , dueDateFont : ImageFont ):
23
+ (width , height ) = labelSize
24
+ # default line spacing used by multiline_text, doesn't seem to have an effect if changed though but we need to take into account
25
+ lineSpacing = 4
26
+ # margin to use for label
27
+ marginTop = 0
28
+ marginBottom = 0
29
+
30
+ # for endless labels with a height of zero
31
+ if height == 0 :
32
+ # height should be text size + spacing x max lines + margin x 2
33
+ height = (textFontSize + lineSpacing ) * textMaxLines + endlessMargin * 2
34
+ # negate the empty space above the text
35
+ (_ , tTop , _ , _ ) = textFont .getbbox ("testing" )
36
+ marginTop = endlessMargin - tTop
37
+ # regular bottom margin
38
+ marginBottom = endlessMargin
39
+ # make space for the due date
40
+ if dueDate :
41
+ (_ , _ , _ , ddBottom ) = dueDateFont .getbbox (dueDate )
42
+ height += ddBottom
43
+
23
44
# increase the size of the barcode if space permits
24
- if (barcode .size [1 ] * 4 ) < labelSize [1 ]:
45
+ if (barcode .size [1 ] * 8 ) < height :
46
+ barcode = barcode .resize ((barcode .size [0 ] * 8 , barcode .size [1 ] * 8 ), Image .Resampling .NEAREST )
47
+ if (barcode .size [1 ] * 6 ) < height :
48
+ barcode = barcode .resize ((barcode .size [0 ] * 6 , barcode .size [1 ] * 6 ), Image .Resampling .NEAREST )
49
+ if (barcode .size [1 ] * 4 ) < height :
25
50
barcode = barcode .resize ((barcode .size [0 ] * 4 , barcode .size [1 ] * 4 ), Image .Resampling .NEAREST )
26
- if (barcode .size [1 ] * 2 ) < labelSize [ 1 ] :
51
+ if (barcode .size [1 ] * 2 ) < height :
27
52
barcode = barcode .resize ((barcode .size [0 ] * 2 , barcode .size [1 ] * 2 ), Image .Resampling .NEAREST )
28
53
29
- label = Image .new ("RGB" , labelSize , ImageColor .getrgb ("#FFF" ))
30
- # vertically align barcode
54
+ label = Image .new ("RGB" , ( width , height ) , ImageColor .getrgb ("#FFF" ))
55
+ # vertically align barcode (ignoring margin)
31
56
barcode_padding = [0 , (int )((label .size [1 ] / 2 ) - (barcode .size [1 ] / 2 ))]
32
57
label .paste (barcode , barcode_padding )
33
58
34
59
draw = ImageDraw .Draw (label )
35
60
36
- (nameText , nameTextWidth ) = wrapText (text , textFont , label . size [ 0 ] - barcode .size [0 ], textMaxLines )
37
- nameMaxWidth = label . size [ 0 ] - barcode .size [0 ]
61
+ (nameText , nameTextWidth ) = wrapText (text , textFont , width - barcode .size [0 ], textMaxLines )
62
+ nameMaxWidth = width - barcode .size [0 ]
38
63
nameLeftMargin = (nameMaxWidth - nameTextWidth ) / 2
39
64
40
65
draw .multiline_text (
41
- [barcode .size [0 ] + nameLeftMargin , 0 ],
66
+ [barcode .size [0 ] + nameLeftMargin , marginTop ],
42
67
nameText ,
43
68
fill = ImageColor .getrgb ("#000" ),
44
69
font = textFont ,
45
- align = "center"
70
+ align = "center" ,
71
+ spacing = lineSpacing
46
72
)
47
73
48
74
if dueDate :
49
75
(_ , _ , ddRight , ddBottom ) = dueDateFont .getbbox (dueDate )
50
76
draw .text (
51
- [label .size [0 ] - ddRight , label .size [1 ] - ddBottom ],
77
+ [label .size [0 ] - ddRight , label .size [1 ] - ddBottom - marginBottom ],
52
78
dueDate ,
53
79
fill = ImageColor .getrgb ("#000" ),
54
80
font = dueDateFont
@@ -100,5 +126,8 @@ def wrapText(text : str, font : ImageFont, maxWidth : int, maxLines : int):
100
126
if len (lines ) > maxLines :
101
127
lines = lines [0 :maxLines ]
102
128
lines [- 1 ] += '...'
129
+ lineLength = font .getlength (lines [- 1 ])
130
+ if lineLength > longestLine :
131
+ longestLine = lineLength
103
132
104
- return ('\n ' .join (lines ), longestLine )
133
+ return ('\n ' .join (lines ), longestLine )
0 commit comments