def cacheGlyph(self, ch): """ Add character and size to glyph caches @param ch: Character @return: Glyph instance """ # Font size self.glyphSizeCache[ch] = self.font.size(ch) # Font texture s = self.font.render(ch, True, (255, 255, 255)) t = Texture() t.setFilter(GL_LINEAR, GL_LINEAR) t.setRepeat(GL_CLAMP, GL_CLAMP) t.loadSurface(s, alphaChannel = True) del s self.glyphCache[ch] = t return t
def cacheGlyph(self, ch): """ Add character and size to glyph caches @param ch: Character @return: Glyph instance """ # Font size self.glyphSizeCache[ch] = self.font.size(ch) # Font texture s = self.font.render(ch, True, (255, 255, 255)) t = Texture() t.setFilter(GL_LINEAR, GL_LINEAR) t.setRepeat(GL_CLAMP, GL_CLAMP) t.loadSurface(s, alphaChannel=True) del s self.glyphCache[ch] = t return t
def getGlyph(self, ch): """ Get the L{Texture} for a given character. @param ch: Character @return: L{Texture} instance """ try: return self.glyphCache[ch] except KeyError: s = self.font.render(ch, True, (255, 255, 255)) """ # Draw outlines import Image, ImageFilter srcImg = Image.fromstring("RGBA", s.get_size(), pygame.image.tostring(s, "RGBA")) img = Image.fromstring("RGBA", s.get_size(), pygame.image.tostring(s, "RGBA")) for y in xrange(img.size[1]): for x in xrange(img.size[0]): a = 0 ns = 3 n = 0 for ny in range(max(0, y - ns), min(img.size[1], y + ns)): for nx in range(max(0, x - ns), min(img.size[0], x + ns)): a += srcImg.getpixel((nx, ny))[3] n += 1 if a and srcImg.getpixel((x, y))[3] == 0: img.putpixel((x, y), (0, 0, 0, a / n)) s = pygame.image.fromstring(img.tostring(), s.get_size(), "RGBA") """ t = Texture() t.setFilter(GL_LINEAR, GL_LINEAR) t.setRepeat(GL_CLAMP, GL_CLAMP) t.loadSurface(s, alphaChannel = True) del s self.glyphCache[ch] = t return t
def render(self, text, pos=(0, 0), rotate=0, scale=DEFAULT_SCALE, shadowoffset=(.0022, .0005), align=LEFT, new=False, shadow=False, outline=False, shadowOpacity=1.0): """ Draw some text. @param text: Text to draw @param pos: Text coordinate tuple (x, y) @param rotate: Angle to rotate text, in degrees @param scale: Scale factor """ # deufeufeu : new drawing relaying only on pygame.font.render # : I know me miss special unicodes characters, but the gain # : is really important. # evilynux : Use arrays to increase performance def drawSquare(w, h, tw, th): self.square_prim[1, 0] = self.square_prim[3, 0] = w self.square_prim[2, 1] = self.square_prim[3, 1] = h self.square_tex[0, 1] = self.square_tex[1, 1] = th self.square_tex[1, 0] = self.square_tex[3, 0] = tw cmgl.drawArrays(GL_TRIANGLE_STRIP, vertices=self.square_prim, texcoords=self.square_tex) if not text: return try: t, w, h = self.stringsCache.get(text) except KeyError: s = self.font.render(text, True, (255, 255, 255)) t = Texture() t.setFilter(GL_LINEAR, GL_LINEAR) t.setRepeat(GL_CLAMP, GL_CLAMP) t.loadSurface(s, alphaChannel=True) del s w, h = self.font.size(text) self.stringsCache.add(text, (t, w, h)) x, y = pos scale *= self.scale w, h = w * scale * self.aspectRatioFactor, h * scale if align == CENTER: #we have already done all the calculating. Why not add this? - akedrou x -= (w / 2) elif align == RIGHT: x -= w y -= (h / 2) tw, th = t.size glEnable(GL_TEXTURE_2D) with cmgl.PushedMatrix(): if rotate: if not isinstance(rotate, tuple): glRotatef(rotate, 0, 0, 1.0) else: glRotatef(0, *rotate) glTranslatef(x, y, 0) t.bind() if self.outline or outline: with cmgl.PushedAttrib(GL_CURRENT_BIT): glColor4f(0, 0, 0, .25 * glGetDoublev(GL_CURRENT_COLOR)[3]) blur = 2 * DEFAULT_SCALE for offset in [(-.7, -.7), (0, -1), (.7, -.7), (-1, 0), (1, 0), (-.7, .7), (0, 1), (.7, .7)]: with cmgl.PushedMatrix(): glTranslatef(blur * offset[0], blur * offset[1], 0) drawSquare(w, h, tw, th) if self.shadow or shadow: with cmgl.PushedAttrib(GL_CURRENT_BIT): glColor4f( 0, 0, 0, glGetDoublev(GL_CURRENT_COLOR)[3] * shadowOpacity) with cmgl.PushedMatrix(): glTranslatef(shadowoffset[0], shadowoffset[1], 0) drawSquare(w, h, tw, th) drawSquare(w, h, tw, th) glDisable(GL_TEXTURE_2D)
def render(self, text, pos = (0, 0), direction = (1, 0), scale = DEFAULT_SCALE, shadowoffset = (.0022, .0005)): """ Draw some text. @param text: Text to draw @param pos: Text coordinate tuple (x, y) @param direction: Text direction vector (x, y, z) @param scale: Scale factor """ # deufeufeu : new drawing relaying only on pygame.font.render # : I know me miss special unicodes characters, but the gain # : is really important. # evilynux : Use arrays to increase performance def drawSquare(w,h,tw,th): self.square_prim[1,0] = self.square_prim[3,0] = w self.square_prim[2,1] = self.square_prim[3,1] = h self.square_tex[0,1] = self.square_tex[1,1] = th self.square_tex[1,0] = self.square_tex[3,0] = tw glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointerf(self.square_prim) glTexCoordPointerf(self.square_tex) glDrawArrays(GL_TRIANGLE_STRIP, 0, self.square_prim.shape[0]) glDisableClientState(GL_VERTEX_ARRAY) glDisableClientState(GL_TEXTURE_COORD_ARRAY); if not text: return try: t,w,h = self.stringsCache.get(text) except KeyError: s = self.font.render(text, True, (255,255,255)) t = Texture() t.setFilter(GL_LINEAR, GL_LINEAR) t.setRepeat(GL_CLAMP, GL_CLAMP) t.loadSurface(s, alphaChannel = True) del s w, h = self.font.size(text) self.stringsCache.add(text,(t,w,h)) x, y = pos scale *= self.scale w, h = w*scale, h*scale tw,th = t.size glEnable(GL_TEXTURE_2D) glPushMatrix() glTranslatef(x,y,0) t.bind() if self.outline: glPushAttrib(GL_CURRENT_BIT) glPushMatrix() glColor4f(0, 0, 0, .25 * glGetDoublev(GL_CURRENT_COLOR)[3]) blur = 2 * DEFAULT_SCALE for offset in [(-.7, -.7), (0, -1), (.7, -.7), (-1, 0), (1, 0), (-.7, .7), (0, 1), (.7, .7)]: glPushMatrix() glTranslatef(blur * offset[0], blur * offset[1], 0) drawSquare(w,h,tw,th) glPopMatrix() glPopMatrix() glPopAttrib() if self.shadow: glPushAttrib(GL_CURRENT_BIT) glPushMatrix() glColor4f(0, 0, 0, 1) glTranslatef(shadowoffset[0], shadowoffset[1], 0) drawSquare(w,h,tw,th) glPopMatrix() glPopAttrib() drawSquare(w,h,tw,th) glPopMatrix() glDisable(GL_TEXTURE_2D) return
def render(self, text, pos = (0, 0), rotate = 0, scale = DEFAULT_SCALE, shadowoffset = (.0022, .0005), align = LEFT, new = False): """ Draw some text. @param text: Text to draw @param pos: Text coordinate tuple (x, y) @param rotate: Angle to rotate text, in degrees @param scale: Scale factor """ # deufeufeu : new drawing relaying only on pygame.font.render # : I know me miss special unicodes characters, but the gain # : is really important. # evilynux : Use arrays to increase performance def drawSquare(w,h,tw,th): self.square_prim[1,0] = self.square_prim[3,0] = w self.square_prim[2,1] = self.square_prim[3,1] = h self.square_tex[0,1] = self.square_tex[1,1] = th self.square_tex[1,0] = self.square_tex[3,0] = tw cmglDrawArrays(GL_TRIANGLE_STRIP, vertices=self.square_prim, texcoords=self.square_tex) if not text: return try: t,w,h = self.stringsCache.get(text) except KeyError: s = self.font.render(text, True, (255,255,255)) t = Texture() t.setFilter(GL_LINEAR, GL_LINEAR) t.setRepeat(GL_CLAMP, GL_CLAMP) t.loadSurface(s, alphaChannel = True) del s w, h = self.font.size(text) self.stringsCache.add(text,(t,w,h)) x, y = pos scale *= self.scale w, h = w*scale*self.aspectRatioFactor, h*scale if align == CENTER: #we have already done all the calculating. Why not add this? - akedrou x -= (w/2) elif align == RIGHT: x -= w y -= (h/2) tw,th = t.size glEnable(GL_TEXTURE_2D) with cmglPushedMatrix(): if rotate: if not isinstance(rotate, tuple): glRotatef(rotate, 0, 0, 1.0) else: glRotatef(0, *rotate) glTranslatef(x,y,0) t.bind() if self.outline: with cmglPushedAttrib(GL_CURRENT_BIT): glColor4f(0, 0, 0, .25 * glGetDoublev(GL_CURRENT_COLOR)[3]) blur = 2 * DEFAULT_SCALE for offset in [(-.7, -.7), (0, -1), (.7, -.7), (-1, 0), (1, 0), (-.7, .7), (0, 1), (.7, .7)]: with cmglPushedMatrix(): glTranslatef(blur * offset[0], blur * offset[1], 0) drawSquare(w,h,tw,th) if self.shadow: with cmglPushedAttrib(GL_CURRENT_BIT): glColor4f(0, 0, 0, 1) with cmglPushedMatrix(): glTranslatef(shadowoffset[0], shadowoffset[1], 0) drawSquare(w,h,tw,th) drawSquare(w,h,tw,th) glDisable(GL_TEXTURE_2D)
def render(self, text, pos = (0, 0), direction = (1, 0), scale = DEFAULT_SCALE, shadowoffset = (.0022, .0005)): """ Draw some text. @param text: Text to draw @param pos: Text coordinate tuple (x, y) @param direction: Text direction vector (x, y, z) @param scale: Scale factor """ # deufeufeu : new drawing relaying only on pygame.font.render # : I know me miss special unicodes characters, but the gain # : is really important. # evilynux : Use arrays to increase performance def drawSquare(w,h,tw,th): self.square_prim[1,0] = self.square_prim[3,0] = w self.square_prim[2,1] = self.square_prim[3,1] = h self.square_tex[0,1] = self.square_tex[1,1] = th self.square_tex[1,0] = self.square_tex[3,0] = tw glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointerf(self.square_prim) glTexCoordPointerf(self.square_tex) glDrawArrays(GL_TRIANGLE_STRIP, 0, self.square_prim.shape[0]) glDisableClientState(GL_VERTEX_ARRAY) glDisableClientState(GL_TEXTURE_COORD_ARRAY); if not text: return try: t,w,h = self.stringsCache.get(text) except KeyError: s = self.font.render(text, True, (255,255,255)) t = Texture() t.setFilter(GL_LINEAR, GL_LINEAR) t.setRepeat(GL_CLAMP, GL_CLAMP) t.loadSurface(s, alphaChannel = True) del s w, h = self.font.size(text) self.stringsCache.add(text,(t,w,h)) x, y = pos scale *= self.scale w, h = w*scale, h*scale tw,th = t.size glEnable(GL_TEXTURE_2D) glPushMatrix() glTranslatef(x,y,0) t.bind() if self.outline: glPushAttrib(GL_CURRENT_BIT) glColor4f(0, 0, 0, .25 * glGetDoublev(GL_CURRENT_COLOR)[3]) blur = 2 * DEFAULT_SCALE for offset in [(-.7, -.7), (0, -1), (.7, -.7), (-1, 0), (1, 0), (-.7, .7), (0, 1), (.7, .7)]: glTranslatef(blur * offset[0], blur * offset[1], 0) drawSquare(w,h,tw,th) glTranslatef(-blur * offset[0], -blur * offset[1], 0) glPopAttrib() if self.shadow: glPushAttrib(GL_CURRENT_BIT) glColor4f(0, 0, 0, 1) glTranslatef(shadowoffset[0], shadowoffset[1], 0) drawSquare(w,h,tw,th) glTranslatef(-shadowoffset[0], -shadowoffset[1], 0) glPopAttrib() drawSquare(w,h,tw,th) glPopMatrix() glDisable(GL_TEXTURE_2D) return
def render(self, text, pos = (0, 0), direction = (1, 0), scale = DEFAULT_SCALE): """ Draw some text. @param text: Text to draw @param pos: Text coordinate tuple (x, y) @param direction: Text direction vector (x, y, z) @param scale: Scale factor """ # deufeufeu : new drawing relaying only on pygame.font.render # : I know me miss special unicodes characters, but the gain # : is really important. def drawSquare(w,h,tw,th): glBegin(GL_TRIANGLE_STRIP) glTexCoord2f(0.0,th) glVertex2f(0,0) glTexCoord2f(tw,th) glVertex2f(w,0) glTexCoord2f(0.0,0.0) glVertex2f(0,h) glTexCoord2f(tw,0.0) glVertex2f(w,h) glEnd() if not text: return try: t,w,h = self.stringsCache.get(text) except KeyError: s = self.font.render(text, True, (255,255,255)) t = Texture() t.setFilter(GL_LINEAR, GL_LINEAR) t.setRepeat(GL_CLAMP, GL_CLAMP) t.loadSurface(s, alphaChannel = True) del s w, h = self.font.size(text) self.stringsCache.add(text,(t,w,h)) x, y = pos scale *= self.scale w, h = w*scale, h*scale tw,th = t.size glEnable(GL_TEXTURE_2D) glPushMatrix() glTranslatef(x,y,0) t.bind() if self.outline: glPushAttrib(GL_CURRENT_BIT) glPushMatrix() glColor4f(0, 0, 0, .25 * glGetDoublev(GL_CURRENT_COLOR)[3]) blur = 2 * DEFAULT_SCALE for offset in [(-.7, -.7), (0, -1), (.7, -.7), (-1, 0), (1, 0), (-.7, .7), (0, 1), (.7, .7)]: glPushMatrix() glTranslatef(blur * offset[0], blur * offset[1], 0) drawSquare(w,h,tw,th) glPopMatrix() glPopMatrix() glPopAttrib() if self.shadow: glPushAttrib(GL_CURRENT_BIT) glPushMatrix() glColor4f(0, 0, 0, 1) glTranslatef(.0022, .0005, 0) drawSquare(w,h,tw,th) glPopMatrix() glPopAttrib() drawSquare(w,h,tw,th) glPopMatrix() glDisable(GL_TEXTURE_2D) return
def render(self, text, pos=(0, 0), direction=(1, 0), scale=DEFAULT_SCALE): """ Draw some text. @param text: Text to draw @param pos: Text coordinate tuple (x, y) @param direction: Text direction vector (x, y, z) @param scale: Scale factor """ # deufeufeu : new drawing relaying only on pygame.font.render # : I know me miss special unicodes characters, but the gain # : is really important. def drawSquare(w, h, tw, th): glBegin(GL_TRIANGLE_STRIP) glTexCoord2f(0.0, th) glVertex2f(0, 0) glTexCoord2f(tw, th) glVertex2f(w, 0) glTexCoord2f(0.0, 0.0) glVertex2f(0, h) glTexCoord2f(tw, 0.0) glVertex2f(w, h) glEnd() if not text: return try: t, w, h = self.stringsCache.get(text) except KeyError: s = self.font.render(text, True, (255, 255, 255)) t = Texture() t.setFilter(GL_LINEAR, GL_LINEAR) t.setRepeat(GL_CLAMP, GL_CLAMP) t.loadSurface(s, alphaChannel=True) del s w, h = self.font.size(text) self.stringsCache.add(text, (t, w, h)) x, y = pos scale *= self.scale w, h = w * scale, h * scale tw, th = t.size glEnable(GL_TEXTURE_2D) glPushMatrix() glTranslatef(x, y, 0) t.bind() if self.outline: glPushAttrib(GL_CURRENT_BIT) glPushMatrix() glColor4f(0, 0, 0, .25 * glGetDoublev(GL_CURRENT_COLOR)[3]) blur = 2 * DEFAULT_SCALE for offset in [(-.7, -.7), (0, -1), (.7, -.7), (-1, 0), (1, 0), (-.7, .7), (0, 1), (.7, .7)]: glPushMatrix() glTranslatef(blur * offset[0], blur * offset[1], 0) drawSquare(w, h, tw, th) glPopMatrix() glPopMatrix() glPopAttrib() if self.shadow: glPushAttrib(GL_CURRENT_BIT) glPushMatrix() glColor4f(0, 0, 0, 1) glTranslatef(.0022, .0005, 0) drawSquare(w, h, tw, th) glPopMatrix() glPopAttrib() drawSquare(w, h, tw, th) glPopMatrix() glDisable(GL_TEXTURE_2D) return