def render(self): "Render the text as an Image" font = Font.get(self.font, self.fontSize, self.fontStyle) try: focus = self is self.sketch.evMgr.focus except: focus = False prompt = self.prompt and not self.data and not focus if prompt: color = self.promptColor text = self.prompt else: color = self.color text = self.data try: srf = font.render(text, True, color) except: text = "[Unable to render!]" srf = font.render(text, True, color) if prompt: self.prompt = text else: self.data = text self.cursor = 0 srf = style(srf, self.bg, self.border, self.weight, self.padding) # Highlight selection and draw cursor c = self.cursor if self.data: p0 = text[c - 1:c] if c else text[0] p1 = text[c:c + 1] if c < len(self.data) else text[-1] self._scrollPad = [font.size(p)[0] for p in (p0, p1)] else: self._scrollPad = 0, 0 x = font.size(text[:c])[0] p = self.padding x += p self._cursorX = x h = srf.get_height() s = self._selection if s not in (None, self.cursor): x0 = font.size(text[:s])[0] + p w = abs(x - x0) s = pygame.Surface((w, h - 2 * p), pygame.SRCALPHA) s.fill(self.highlight) srf.blit(s, (min(x, x0), p)) if self.cursorStatus: pygame.draw.line(srf, self.color, (x, p), (x, h - 1 - p), 2) return srf
def _widthTo(self, i): font = Font.get(self.font, self.fontSize, self.fontStyle) d = self.data return (font.size(d[:i])[0] + font.size(d[:i+1])[0]) // 2