def render(self, text, antialias, color, background=None): """ Render text onto surface. Arguments: text to render (string) antialias of text (bool) color of text (R,G,B) background color (R,G,B) """ w,h = self.size(text) surf = Surface((w,h), BufferedImage.TYPE_INT_ARGB) g2d = surf.createGraphics() if background: g2d.setColor(Color(background)) g2d.fillRect(0,0,w,h) g2d.setFont(self.font) if antialias: g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON) g2d.setColor(Color(color)) y = (h//2)-((self.fontMetrics.getAscent()+self.fontMetrics.getDescent())//2)+self.fontMetrics.getAscent() if not self.underline: g2d.drawString(text,0,y) else: try: text = AttributedString(text) text.addAttribute(TextAttribute.FONT,self.font) text.addAttribute(TextAttribute.UNDERLINE,TextAttribute.UNDERLINE_ON) g2d.drawString(text.getIterator(),0,y) except IllegalArgumentException: pass g2d.dispose() return surf
def rotozoom(surface, angle, size): """ Return Surface rotated and resized by the given angle and size. """ if not angle: width = int(surface.getWidth() * size) height = int(surface.getHeight() * size) return scale(surface, (width, height)) theta = angle * _deg_rad width_i = int(surface.getWidth() * size) height_i = int(surface.getHeight() * size) cos_theta = _fabs(_cos(theta)) sin_theta = _fabs(_sin(theta)) width_f = int(_ceil((width_i * cos_theta) + (height_i * sin_theta))) if width_f % 2: width_f += 1 height_f = int(_ceil((width_i * sin_theta) + (height_i * cos_theta))) if height_f % 2: height_f += 1 surf = Surface((width_f, height_f), BufferedImage.TYPE_INT_ARGB) at = AffineTransform() at.translate(width_f / 2.0, height_f / 2.0) at.rotate(-theta) g2d = surf.createGraphics() ot = g2d.getTransform() g2d.setTransform(at) g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) g2d.drawImage(surface, -width_i // 2, -height_i // 2, width_i, height_i, None) g2d.setTransform(ot) g2d.dispose() return surf
def rotozoom(self, surface, angle, size): """ Return Surface rotated and resized by the given angle and size. """ if not angle: width = int(surface.getWidth()*size) height = int(surface.getHeight()*size) return self.scale(surface, (width, height)) theta = angle*self.deg_rad width_i = int(surface.getWidth()*size) height_i = int(surface.getHeight()*size) cos_theta = _fabs( _cos(theta) ) sin_theta = _fabs( _sin(theta) ) width_f = int( _ceil((width_i*cos_theta)+(height_i*sin_theta)) ) if width_f % 2: width_f += 1 height_f = int( _ceil((width_i*sin_theta)+(height_i*cos_theta)) ) if height_f % 2: height_f += 1 surf = Surface((width_f,height_f), BufferedImage.TYPE_INT_ARGB) at = AffineTransform() at.translate(width_f/2, height_f/2) at.rotate(-theta) g2d = surf.createGraphics() ot = g2d.getTransform() g2d.setTransform(at) g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) g2d.drawImage(surface, -width_i//2, -height_i//2, width_i, height_i, None) g2d.setTransform(ot) g2d.dispose() return surf
def rotate(surface, angle): """ Return Surface rotated by the given angle. """ if not angle: return surface.copy() theta = angle * _deg_rad width_i = surface.getWidth() height_i = surface.getHeight() cos_theta = _fabs(_cos(theta)) sin_theta = _fabs(_sin(theta)) width_f = int((width_i * cos_theta) + (height_i * sin_theta)) height_f = int((width_i * sin_theta) + (height_i * cos_theta)) surf = Surface((width_f, height_f), BufferedImage.TYPE_INT_ARGB) at = AffineTransform() at.translate(width_f / 2.0, height_f / 2.0) at.rotate(-theta) g2d = surf.createGraphics() ot = g2d.getTransform() g2d.setTransform(at) g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) g2d.drawImage(surface, -width_i // 2, -height_i // 2, None) g2d.setTransform(ot) g2d.dispose() return surf
def rotate(self, surface, angle): """ Return Surface rotated by the given angle. """ if not angle: return surface.copy() theta = angle*self.deg_rad width_i = surface.getWidth() height_i = surface.getHeight() cos_theta = _fabs( _cos(theta) ) sin_theta = _fabs( _sin(theta) ) width_f = int( (width_i*cos_theta)+(height_i*sin_theta) ) height_f = int( (width_i*sin_theta)+(height_i*cos_theta) ) surf = Surface((width_f,height_f), BufferedImage.TYPE_INT_ARGB) at = AffineTransform() at.translate(width_f/2, height_f/2) at.rotate(-theta) g2d = surf.createGraphics() ot = g2d.getTransform() g2d.setTransform(at) g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) g2d.drawImage(surface, -width_i//2, -height_i//2, None) g2d.setTransform(ot) g2d.dispose() return surf
def render(self, text, antialias, color, background=None): """ Render text onto surface. Arguments: text to render (string) antialias of text (bool) color of text (R,G,B) background color (R,G,B) """ w,h = self.size(text) surf = Surface((w,h), BufferedImage.TYPE_INT_ARGB) g2d = surf.createGraphics() if background: g2d.setColor(Color(background)) g2d.fillRect(0,0,w,h) g2d.setFont(self.font) if antialias: g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON) g2d.setColor(Color(color)) g2d.drawString(text,0,(h//2)+(self.fontMetrics.getAscent()//2)) if self.underline: g2d.setStroke(BasicStroke(1)) g2d.drawLine(0,h-1,w-1,h-1) g2d.dispose() return surf
def init(): """ **pyj2d.font.init** Initialize font module. """ global _surf, _g2d, _initialized, match_font _surf = Surface((1,1), BufferedImage.TYPE_INT_RGB) _g2d = _surf.createGraphics() _initialized = True
def init(): """ **pyj2d.font.init** Initialize font module. """ global _surf, _g2d, _initialized, match_font _surf = Surface((1, 1), BufferedImage.TYPE_INT_RGB) _g2d = _surf.createGraphics() _initialized = True
def scale(self, surface, size, dest=None): """ Return Surface resized by the given size. An optional destination surface can be provided. """ if not dest: surf = Surface(size, BufferedImage.TYPE_INT_ARGB) else: surf = dest g2d = surf.createGraphics() g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) g2d.drawImage(surface, 0, 0, size[0], size[1], None) g2d.dispose() return surf
def scale(surface, size, dest=None): """ Return Surface resized by the given size. An optional destination surface can be provided. """ if not dest: surf = Surface(size, BufferedImage.TYPE_INT_ARGB) else: surf = dest g2d = surf.createGraphics() g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) g2d.drawImage(surface, 0, 0, size[0], size[1], None) g2d.dispose() return surf
def render(self, text, antialias, color, background=None): """ Render text onto surface. Arguments: text to render (string) antialias of text (bool) color of text (R,G,B) background color (R,G,B) """ w, h = self.size(text) surf = Surface((w, h), BufferedImage.TYPE_INT_ARGB) g2d = surf.createGraphics() if background: g2d.setColor(Color(background)) g2d.fillRect(0, 0, w, h) g2d.setFont(self.font) if antialias: g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON) g2d.setColor(Color(color)) y = ((h // 2) - ((self.fontMetrics.getAscent() + self.fontMetrics.getDescent()) // 2) + self.fontMetrics.getAscent()) if not self.underline: g2d.drawString(text, 0, y) else: try: text = AttributedString(text) text.addAttribute(TextAttribute.FONT, self.font) text.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON) g2d.drawString(text.getIterator(), 0, y) except IllegalArgumentException: pass g2d.dispose() return surf