def test_textsize(): """ The bounding box of the text, as drawn in the specified font, is correctly calculated. """ assert textsize("Hello world") == (88, 8) assert textsize("Hello world", font=proportional(CP437_FONT)) == (71, 8)
def printDaysWithoutDie(self, daysWithoutDie, recorDaysWithoutDie): with canvas(device1) as draw: draw.rectangle([0, 0, device1.width, device1.height], fill="black") singleRowWidth = device1.width // 2 daysWithoutDieText = str(daysWithoutDie) recordDaysWithoutDieText = str(recorDaysWithoutDie) daysWithouDieTextWidth = textsize(daysWithoutDieText, font=proportional(LCD_FONT))[0] firstLineOffsetStart = (singleRowWidth - daysWithouDieTextWidth) // 2 recordDaysWithouDieTextWidth = textsize( recordDaysWithoutDieText, font=proportional(LCD_FONT))[0] secondLineOffsetStart = singleRowWidth + \ ((singleRowWidth - recordDaysWithouDieTextWidth) // 2) text(draw, (firstLineOffsetStart, 0), daysWithoutDieText, fill="white", font=proportional(LCD_FONT)) text(draw, (secondLineOffsetStart, 0), recordDaysWithoutDieText, fill="white", font=proportional(LCD_FONT))
def run(self): logger.debug('run') active = self.active i = 0 while True: while (active == self.active): if (i < (textsize(self.items[active], font=proportional(LCD_FONT))[0] + 3 - self.device.width)): self.virtual.set_position((i, active * 8)) i += 1 else: i = 0 break time.sleep(0.2) if (self.active == 255): return if (self.active > active): for j in range(9): self.virtual.set_position((0, (active * 8) + j)) time.sleep(0.05) if (self.active < active): for j in range(9): self.virtual.set_position((0, (active * 8) - j)) time.sleep(0.05) active = self.active i = 0
def drawTemp(self, draw): s = u'{:+.1f} \xf8c'.format(self.weather.temp) w, _ = textsize(s, font=utils.proportional2(TINY_FONT)) text(draw, (36 - w, 2), s, fill="white", font=utils.proportional2(TINY_FONT))
def __init__(self, text, font, duration=1): self.text = text self.font = font self.color = "white" self.backColor = "black" self.size = textsize(text, font) self.width, self.height = self.size subtleSnapshot.__init__(self, self.width, self.height, self.update, duration)
def drawLed(self, draw): date_str = u"LED: %d%%" % (intensity) txtlen, _ = textsize(date_str, font=utils.proportional2(TINY_FONT)) coords = (36 - txtlen, 2) text(draw, coords, date_str, fill="white", font=utils.proportional2(TINY_FONT))
def drawDesc(self, draw): date_str = self.weather.lastData['weather'][0]['main'].lower() txtlen, _ = textsize(date_str, font=utils.proportional2(TINY_FONT)) coords = (36 - txtlen, 2) text(draw, coords, date_str, fill="white", font=utils.proportional2(TINY_FONT))
def drawDay(self, draw): date_str = time.strftime("%A").upper() txtlen, _ = textsize(date_str, font=utils.proportional2(TINY_FONT)) coords = (36 - txtlen, 2) text(draw, coords, date_str, fill="white", font=utils.proportional2(TINY_FONT))
def __init__(self, text, font, color="white"): self.text = text self.font = font self.color = color self.width = 32 self.height = 8 txtlen, _ = textsize(self.text, font=self.font) self.coords = (self.width - txtlen + 1, 0) subtleSnapshot.__init__(self, self.width, self.height, self.update, 3600.0)
def test_font(fontname): font = getattr(luma.core.legacy.font, fontname) w, h = textsize(charset, proportional(font)) device = dummy(width=w, height=h, mode="1") with canvas(device) as draw: text(draw, (0, 0), charset, "white", font=proportional(font)) img_path = get_reference_image(f'{fontname}.png') with open(img_path, 'rb') as fp: reference = Image.open(fp) assert_identical_image(reference, device.image, img_path)
def update(self, draw): self.currentView = 1 if self.currentView == 0 else 0 date_str = time.strftime( "%d %B") if self.currentView == 1 else time.strftime("%A") txtlen, _ = textsize(date_str, font=utils.proportional2(TINY_FONT)) coords = (36 - txtlen, 2) text(draw, coords, date_str, fill="white", font=utils.proportional2(TINY_FONT))
def __init__(self, text, width, height, font=None, fill="white", doneFunc=None): self.text = text self.font = font or utils.proportional2(TINY_FONT) self.fill = fill self.doneFunc = doneFunc self.viewportWidth = width self.viewportHeight = height self.textWidth, self.textHeight = textsize(self.text, font=self.font) self.offsetX = self.viewportWidth self.offsetY = self.viewportHeight - self.textHeight + 2 hotspot.__init__(self, self.viewportWidth, self.viewportHeight, self.update)
def on_timer(self): now = datetime.datetime.now() if now.microsecond > 500000: time_string = now.strftime("%H:%M") else: time_string = now.strftime("%H\t%M") x = (self.width - textsize(time_string, self.font)[0] + 1) / 2 image = Image.new('1', (self.width, self.height)) canvas = ImageDraw.Draw(image) text(canvas, (x, self.clock_y), time_string, 255, self.font) del canvas self.manager.draw_activity(self, image) self.clock_y += self.direction if self.clock_y == 0 or self.clock_y + self.clock_height == self.height: self.direction = -self.direction