Skip to content

Commit bfa36fd

Browse files
committed
new center text positions
1 parent f6d46a5 commit bfa36fd

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

anki_vector/annotate.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class AnnotationPosition(Enum):
7272
RIGHT = 2
7373
TOP = 4
7474
BOTTOM = 8
75+
CENTER = 16
7576

7677
#: Top left position
7778
TOP_LEFT = TOP | LEFT
@@ -85,6 +86,15 @@ class AnnotationPosition(Enum):
8586
#: Bottom right position
8687
BOTTOM_RIGHT = BOTTOM | RIGHT
8788

89+
#: Top center position
90+
TOP_CENTER = TOP | CENTER
91+
92+
#: Middle of the screen position
93+
CENTER_CENTER = CENTER | CENTER
94+
95+
#: Bottom center position
96+
BOTTOM_CENTER = BOTTOM | CENTER
97+
8898

8999
class ImageText: # pylint: disable=too-few-public-methods
90100
"""ImageText represents some text that can be applied to an image.
@@ -156,15 +166,25 @@ def render(self, draw: ImageDraw.ImageDraw, bounds: tuple) -> ImageDraw.ImageDra
156166
(bx1, by1, bx2, by2) = bounds
157167
text_width, text_height = draw.textsize(self.text, font=self.font)
158168

159-
if self.position.value & AnnotationPosition.TOP.value:
160-
y = by1
169+
if self.position.value == AnnotationPosition.CENTER_CENTER.value:
170+
y = by1 + (by2 - by1 - (text_height))/2
171+
x = bx1 + (bx2 - bx1 - (text_width))/2
161172
else:
162-
y = by2 - text_height
173+
if (self.position.value & AnnotationPosition.LEFT.value or self.position.value & AnnotationPosition.RIGHT.value)\
174+
and self.position.value & AnnotationPosition.CENTER.value:
175+
y = by1 + (by2 - by1 - (text_height)) / 2
176+
if self.position.value & AnnotationPosition.TOP.value:
177+
y = by1
178+
else:
179+
y = by2 - text_height
163180

164-
if self.position.value & AnnotationPosition.LEFT.value:
165-
x = bx1
166-
else:
167-
x = bx2 - text_width
181+
if (self.position.value & AnnotationPosition.TOP.value or self.position.value & AnnotationPosition.BOTTOM.value)\
182+
and self.position.value & AnnotationPosition.CENTER.value:
183+
x = bx1 + (bx2 - bx1 - (text_width)) / 2
184+
elif self.position.value & AnnotationPosition.LEFT.value:
185+
x = bx1
186+
else:
187+
x = bx2 - text_width
168188

169189
# helper method for each draw call below
170190
def _draw_text(pos, color):

0 commit comments

Comments
 (0)