Skip to content

Commit

Permalink
Merge pull request #4034 from t20100/gl-text-padding
Browse files Browse the repository at this point in the history
silx.gui.plot.PlotWidget: Improved text background rendering for OpenGL backend
  • Loading branch information
vallsv authored Jan 8, 2024
2 parents 42472e1 + 182859e commit 32bb9b0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/silx/gui/plot/backends/BackendOpenGL.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ class BackendOpenGL(BackendBase.BackendBase, glu.OpenGLWidget):
So, the caller should not modify these arrays afterwards.
"""

_TEXT_MARKER_PADDING = 4

def __init__(self, plot, parent=None, f=qt.Qt.Widget):
glu.OpenGLWidget.__init__(
self,
Expand Down Expand Up @@ -645,6 +647,7 @@ def _renderItems(self, overlay=False):
align=glutils.RIGHT,
valign=glutils.BOTTOM,
devicePixelRatio=self.getDevicePixelRatio(),
padding=self._TEXT_MARKER_PADDING,
)
labels.append(label)

Expand Down Expand Up @@ -679,6 +682,7 @@ def _renderItems(self, overlay=False):
align=glutils.LEFT,
valign=glutils.TOP,
devicePixelRatio=self.getDevicePixelRatio(),
padding=self._TEXT_MARKER_PADDING,
)
labels.append(label)

Expand Down Expand Up @@ -722,6 +726,7 @@ def _renderItems(self, overlay=False):
align=glutils.LEFT,
valign=valign,
devicePixelRatio=self.getDevicePixelRatio(),
padding=self._TEXT_MARKER_PADDING,
)
labels.append(label)

Expand Down Expand Up @@ -907,7 +912,7 @@ def _lineStyleToDashOffsetPattern(
pattern = ()
if len(pattern) == 2:
pattern = pattern * 2
return offset, pattern
return float(offset), tuple(float(v) for v in pattern)

def addCurve(
self,
Expand Down Expand Up @@ -1162,6 +1167,7 @@ def getDefaultFont(self):
"""Returns the default font, used by raw markers and axes labels"""
if self._defaultFont is None:
from matplotlib.font_manager import findfont, FontProperties

font_filename = findfont(FontProperties(family=["sans-serif"]))
_logger.debug("Load font from mpl: %s", font_filename)
id = qt.QFontDatabase.addApplicationFont(font_filename)
Expand Down
38 changes: 29 additions & 9 deletions src/silx/gui/plot/backends/glutils/GLText.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ class Text2D:
varying vec2 vCoords;
void main(void) {
gl_FragColor = mix(bgColor, color, texture2D(texText, vCoords).r);
if (vCoords.x < 0.0 || vCoords.x > 1.0 || vCoords.y < 0.0 || vCoords.y > 1.0) {
gl_FragColor = bgColor;
} else {
gl_FragColor = mix(bgColor, color, texture2D(texText, vCoords).r);
}
}
""",
}

_TEX_COORDS = numpy.array(
((0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0)), dtype=numpy.float32
).ravel()

_program = Program(_SHADERS["vertex"], _SHADERS["fragment"], attrib0="position")

# Discard texture objects when removed from the cache
Expand All @@ -146,11 +146,13 @@ def __init__(
valign: str = BASELINE,
rotate: float = 0.0,
devicePixelRatio: float = 1.0,
padding: int = 0,
):
self.devicePixelRatio = devicePixelRatio
self.font = font
self._vertices = None
self._text = text
self._padding = padding
self.x = x
self.y = y
self.color = color
Expand Down Expand Up @@ -204,6 +206,10 @@ def _getTexture(self) -> tuple[Texture, int]:
def text(self) -> str:
return self._text

@property
def padding(self) -> int:
return self._padding

@property
def size(self) -> tuple[int, int]:
textureKey = self._textureKey()
Expand Down Expand Up @@ -282,17 +288,31 @@ def render(self, matrix: numpy.ndarray):
bgColor = self.color[0], self.color[1], self.color[2], 0.0
gl.glUniform4f(prog.uniforms["bgColor"], *bgColor)

vertices = self.getVertices(offset, texture.shape)
paddingOffset = max(0, int(self.padding * self.devicePixelRatio))
height, width = texture.shape
vertices = self.getVertices(
offset, (height + 2 * paddingOffset, width + 2 * paddingOffset)
)

posAttrib = prog.attributes["position"]
gl.glEnableVertexAttribArray(posAttrib)
gl.glVertexAttribPointer(posAttrib, 2, gl.GL_FLOAT, gl.GL_FALSE, 0, vertices)

xoffset = paddingOffset / width
yoffset = paddingOffset / height
texCoords = numpy.array(
(
(-xoffset, -yoffset),
(1.0 + xoffset, -yoffset),
(-xoffset, 1.0 + yoffset),
(1.0 + xoffset, 1.0 + yoffset),
),
dtype=numpy.float32,
).ravel()

texAttrib = prog.attributes["texCoords"]
gl.glEnableVertexAttribArray(texAttrib)
gl.glVertexAttribPointer(
texAttrib, 2, gl.GL_FLOAT, gl.GL_FALSE, 0, self._TEX_COORDS
)
gl.glVertexAttribPointer(texAttrib, 2, gl.GL_FLOAT, gl.GL_FALSE, 0, texCoords)

with texture:
gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4)

0 comments on commit 32bb9b0

Please sign in to comment.