def measure_text(self, text, font): tw, th = TextRenderer(font).get_size(text) return tw * self.ortho_pscalex, th * self.ortho_pscaley
def text( self, text, font, x, y, w=0, h=0, color=(1.0, 1.0, 1.0, 1.0), shadow=False, shadow_color=(0, 0, 0), halign=-1, ): if not text: return 0, 0 # if len(color) == 3: # color = (color[0], color[1], color[2], 1.0 try: alpha = color[3] except IndexError: alpha = 1.0 color = ( int(round(color[0] * 255)), int(round(color[1] * 255)), int(round(color[2] * 255)), ) cache_key = (text, hash(font), font.size, color, alpha) try: self.text_cache_history.remove(cache_key) except ValueError: texture = None else: texture, txtsize = self.text_cache[cache_key] fs_emu_blending(True) fs_emu_texturing(True) gl.glDisable(gl.GL_DEPTH_TEST) if texture: gl.glBindTexture(gl.GL_TEXTURE_2D, texture) else: txtdata, txtsize = TextRenderer(font).render_text(text, color) texture = Render.get().create_texture() gl.glBindTexture(gl.GL_TEXTURE_2D, texture) gl.glTexImage2D( gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, txtsize[0], txtsize[1], 0, gl.GL_BGRA, gl.GL_UNSIGNED_BYTE, txtdata, ) gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR ) gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR ) tw, th = ( txtsize[0] * self.ortho_pscalex, txtsize[1] * self.ortho_pscaley, ) tx = x ty = y if w > 0: if tw > w: tw = w else: if halign == 0: tx += (w - tw) / 2 if h > 0: ty += (h - th) / 2 # ts = 4 / cls.display_height # Step # glDisable(GL_TEXTURE_RECTANGLE_ARB) # glTexEnv(GL_TEXTURE_2D, GL_MODULATE) # glDisable(GL_TEXTURE_RECTANGLE) # fs_emu_blending(True) gl.glBegin(gl.GL_QUADS) gl.glColor4f(alpha, alpha, alpha, alpha) gl.glTexCoord2f(0.0, 0.0) gl.glVertex2f(tx, ty + th) gl.glTexCoord2f(1.0, 0.0) gl.glVertex2f(tx + tw, ty + th) gl.glTexCoord2f(1.0, 1.0) gl.glVertex2f(tx + tw, ty) gl.glTexCoord2f(0.0, 1.0) gl.glVertex2f(tx, ty) # glRasterPos2f(tx, ty) # glDrawPixels(txtsize[0], txtsize[1], GL_RGBA, GL_UNSIGNED_BYTE, # txtdata) gl.glEnd() # fs_emu_blending(False) gl.glEnable(gl.GL_DEPTH_TEST) self.text_cache_history.append(cache_key) self.text_cache[cache_key] = texture, txtsize if len(self.text_cache) > TEXT_CACHE_SIZE: cache_key = self.text_cache_history.pop(0) texture, txtsize = self.text_cache.pop(cache_key) Render.get().delete_texture_list.append(texture) # # FIXME: # shadow = False # # glDisable(GL_DEPTH_TEST) # fs_emu_blending(True) # #text = current_menu.selected_item.title # #if shadow: # txtdata, txtsize = TextRenderer(font).render_text(text, shadow_color) # tw, th = txtsize[0] * ortho_pscalex, txtsize[1] * ortho_pscaley # tx = x # ty = y # if w > 0: # tx = tx + (w - tw) / 2 # if h > 0: # ty = ty + (h - th) / 2 # #tx = 0 - tw / 2 # #ty = -0.67 # ts = 4 / State.get().display_height # Step # if shadow: # glPixelTransferf(GL_ALPHA_SCALE, 0.04) # for fx, fy in [(1, 1), (-1, -1), (1, -1), (-1, 1), (1, 0), (-1, # 0), # (0, -1), (0, 1)]: # glRasterPos2f(tx - fx * ts, ty - fy * ts) # glDrawPixels(txtsize[0], txtsize[1], GL_RGBA, # GL_UNSIGNED_BYTE, # txtdata) # glPixelTransferf(GL_ALPHA_SCALE, 0.01) # for fx, fy in [(0, 2), (2, 0), (0, -2), (-2, 0), # (1, 2), (2, 1), (-1, 2), (-2, 1), (1, -2), # (2, -1), (-1, -2), (-2, -1)]: # glRasterPos2f(tx - fx * ts, ty - fy * ts) # glDrawPixels(txtsize[0], txtsize[1], GL_RGBA, # GL_UNSIGNED_BYTE, # txtdata) # glPixelTransferf(GL_ALPHA_SCALE, alpha) # rendered = font.render(text, True, color) # txtsize = rendered.get_size() # txtdata = pygame.image.tostring(rendered, "RGBA", 1) # glRasterPos2f(tx, ty) # glDrawPixels(txtsize[0], txtsize[1], GL_RGBA, GL_UNSIGNED_BYTE, # txtdata) # #glPopAttrib() # glPixelTransferf(GL_ALPHA_SCALE, 1.0) # fs_emu_blending(False) # glEnable(GL_DEPTH_TEST) return tw, th