diff options
Diffstat (limited to 'silx/gui/plot/backends/glutils/GLText.py')
-rw-r--r-- | silx/gui/plot/backends/glutils/GLText.py | 60 |
1 files changed, 35 insertions, 25 deletions
diff --git a/silx/gui/plot/backends/glutils/GLText.py b/silx/gui/plot/backends/glutils/GLText.py index 725c12c..d6ae6fa 100644 --- a/silx/gui/plot/backends/glutils/GLText.py +++ b/silx/gui/plot/backends/glutils/GLText.py @@ -1,7 +1,7 @@ # coding: utf-8 # /*########################################################################## # -# Copyright (c) 2014-2019 European Synchrotron Radiation Facility +# Copyright (c) 2014-2020 European Synchrotron Radiation Facility # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -140,7 +140,9 @@ class Text2D(object): color=(0., 0., 0., 1.), bgColor=None, align=LEFT, valign=BASELINE, - rotate=0): + rotate=0, + devicePixelRatio= 1.): + self.devicePixelRatio = devicePixelRatio self._vertices = None self._text = text self.x = x @@ -160,30 +162,35 @@ class Text2D(object): self._rotate = numpy.radians(rotate) - def _getTexture(self, text): + def _getTexture(self, text, devicePixelRatio): # Retrieve/initialize texture cache for current context + textureKey = text, devicePixelRatio + context = Context.getCurrent() if context not in self._textures: self._textures[context] = _Cache( callback=lambda key, value: value[0].discard()) textures = self._textures[context] - if text not in textures: - image, offset = font.rasterText(text, - font.getDefaultFontFamily()) - if text not in self._sizes: - self._sizes[text] = image.shape[1], image.shape[0] - - textures[text] = ( - Texture(gl.GL_RED, - data=image, - minFilter=gl.GL_NEAREST, - magFilter=gl.GL_NEAREST, - wrap=(gl.GL_CLAMP_TO_EDGE, - gl.GL_CLAMP_TO_EDGE)), - offset) - - return textures[text] + if textureKey not in textures: + image, offset = font.rasterText( + text, + font.getDefaultFontFamily(), + devicePixelRatio=self.devicePixelRatio) + if textureKey not in self._sizes: + self._sizes[textureKey] = image.shape[1], image.shape[0] + + texture = Texture( + gl.GL_RED, + data=image, + minFilter=gl.GL_NEAREST, + magFilter=gl.GL_NEAREST, + wrap=(gl.GL_CLAMP_TO_EDGE, + gl.GL_CLAMP_TO_EDGE)) + texture.prepare() + textures[textureKey] = texture, offset + + return textures[textureKey] @property def text(self): @@ -191,11 +198,14 @@ class Text2D(object): @property def size(self): - if self.text not in self._sizes: - image, offset = font.rasterText(self.text, - font.getDefaultFontFamily()) - self._sizes[self.text] = image.shape[1], image.shape[0] - return self._sizes[self.text] + textureKey = self.text, self.devicePixelRatio + if textureKey not in self._sizes: + image, offset = font.rasterText( + self.text, + font.getDefaultFontFamily(), + devicePixelRatio=self.devicePixelRatio) + self._sizes[textureKey] = image.shape[1], image.shape[0] + return self._sizes[textureKey] def getVertices(self, offset, shape): height, width = shape @@ -238,7 +248,7 @@ class Text2D(object): prog.use() texUnit = 0 - texture, offset = self._getTexture(self.text) + texture, offset = self._getTexture(self.text, self.devicePixelRatio) gl.glUniform1i(prog.uniforms['texText'], texUnit) |