Skip to content

Commit

Permalink
Merge pull request #28 from hhansen06/main
Browse files Browse the repository at this point in the history
line breaks in text payload in drawcustom function
  • Loading branch information
hhansen06 authored Aug 15, 2023
2 parents c5a1fd1 + eae4cec commit bc5a2e6
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 7 deletions.
46 changes: 43 additions & 3 deletions custom_components/open_epaper_link/imagegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def downloadimg(entity_id, service, hass):
byte_im = buf.getvalue()
return byte_im

def get_wrapped_text(text: str, font: ImageFont.ImageFont,
line_length: int):
lines = ['']
for word in text.split():
line = f'{lines[-1]} {word}'.strip()
if font.getlength(line) <= line_length:
lines[-1] = line
else:
lines.append(word)
return '\n'.join(lines)

# converts a color name to the corresponding color index for the palette
def getIndexColor(color):
Expand Down Expand Up @@ -103,7 +113,20 @@ def customimage(entity_id, service, hass):
for element in payload:
if element["type"] == "line":
img_line = ImageDraw.Draw(img)
img_line.line([(element['x_start'],element['y_start']),(element['x_end'],element['y_end'])],fill = red, width=4)

if not "y_start" in element:
if "y_padding" in element:
y_start = pos_y + element["y_padding"]
else:
y_start = pos_y
y_end = y_start
else:
y_start = element["y_start"]
y_end = element["y_end"]


img_line.line([(element['x_start'],y_start),(element['x_end'],y_end)],fill = getIndexColor(element['fill']), width=element['width'])
pos_y = y_start

if element["type"] == "rectangle":
img_rect = ImageDraw.Draw(img)
Expand Down Expand Up @@ -141,9 +164,26 @@ def customimage(entity_id, service, hass):
anchor = "lt"
else:
anchor = element['anchor']

if not "align" in element:
align = "left"
else:
align = element['align']

if not "spacing" in element:
spacing = 5
else:
spacing = element['spacing']

if "max_width" in element:
text = get_wrapped_text(str(element['value']), font, line_length=element['max_width'])
anchor = None
else:
text = str(element['value'])

d.text((element['x'], akt_pos_y), str(element['value']), fill=getIndexColor(color), font=font, anchor=anchor)
pos_y = akt_pos_y
d.text((element['x'], akt_pos_y), text, fill=getIndexColor(color), font=font, anchor=anchor, align=align, spacing=spacing)
textbbox = d.textbbox((element['x'], akt_pos_y), text, font=font, anchor=anchor, align=align, spacing=spacing)
pos_y = textbbox[3]

if element["type"] == "multiline":
font_file = os.path.join(os.path.dirname(__file__), element['font'])
Expand Down
59 changes: 55 additions & 4 deletions docs/drawcustom/supported_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ data:
color: red
```
### text
```
- type: text
value: "Hello World!"
font: "ppb.ttf"
x: 0
"y": 0
size: 40
color: red
```
#### Parameters:
- value (required) The to show text
- x (required) position on x axis
Expand All @@ -36,6 +45,15 @@ data:

### multiline
this payload takes a string and a delimiter, and will break the string on every delimiter and move the cursor the amount if offset_y down the canvas.
```
- type: multiline
value: "adb|asd"
delimiter: "|"
font: "ppb.ttf"
x: 0
size: 40
color: red
```
#### Parameters:
- value (required) The to show text
- delimiter (required) The delimiting character, to split value. e.g.: #
Expand All @@ -44,18 +62,43 @@ this payload takes a string and a delimiter, and will break the string on every
- font (required) name of ttf file from custom_component folder. e.g.: ppb.ttf
- color (required) frontcolor of text. e.g.: black
- start_y (optional) position on y axis
- align (optional) left,center,right default: left (if text contains \n this set the alignment of the lines)
- spacing (optional) if multiline text, spacing between single lines
- max_width (optional) creats line breaks in the provided text, if text is longer than max_width defines (this will disable the anchor attribute)
- y_padding (optional) offset to last text or multiline y position. works only if start_y is not provided. e.g.: 10

### line
Due to a bug in upstream, this isnt working. Use rectangle instead!
Draws a line
```
- type: line
x_start: 20
x_end: 380
y_start: 15
y_end: 15
width: 1
fill: red
```
#### Parameters:
- x_start (required)
- y_start (required)
- y_start (optional) if y_start is not provided, it will automaticly try to add the line at the bottom of the last text blck
- y_padding (optional) if no y_start is provided, this will offset the start of the line to the last text block
- x_end (required)
- y_end (required)
- color (optional) default: black
- y_end (optional)
- fill (required)
- width (required)

### rectangle
```
- type: rectangle
x_start: 20
x_end: 80
y_start: 15
y_end: 30
width: 1
fill: red
outline: black
width: 2
```
#### Parameters:
- x_start (required)
- y_start (required)
Expand All @@ -66,6 +109,14 @@ Due to a bug in upstream, this isnt working. Use rectangle instead!
- width (required) width of outline e.g. 2

### icon
```
- type: icon
value: account-cowboy-hat
x: 60
y: 120
size: 120
color: red
```
#### Parameters:
- value (required) name of icon. from: https://pictogrammers.com/library/mdi/
- size (required) e.g. 20
Expand Down

0 comments on commit bc5a2e6

Please sign in to comment.