summaryrefslogtreecommitdiff
path: root/src/silx/gui/_glutils
diff options
context:
space:
mode:
authorPicca Frédéric-Emmanuel <picca@debian.org>2022-11-03 10:02:44 +0100
committerPicca Frédéric-Emmanuel <picca@debian.org>2022-11-03 10:02:44 +0100
commit1c380bfeff1e13a9f7d506460336659502ca052d (patch)
tree48081d47748d4563eeaa76662287eb19638c8591 /src/silx/gui/_glutils
parent4e774db12d5ebe7a20eded6dd434a289e27999e5 (diff)
New upstream version 1.1.0+dfsg
Diffstat (limited to 'src/silx/gui/_glutils')
-rw-r--r--src/silx/gui/_glutils/Context.py1
-rw-r--r--src/silx/gui/_glutils/FramebufferTexture.py1
-rw-r--r--src/silx/gui/_glutils/OpenGLWidget.py7
-rw-r--r--src/silx/gui/_glutils/Program.py1
-rw-r--r--src/silx/gui/_glutils/Texture.py1
-rw-r--r--src/silx/gui/_glutils/VertexBuffer.py1
-rw-r--r--src/silx/gui/_glutils/__init__.py1
-rw-r--r--src/silx/gui/_glutils/font.py73
-rw-r--r--src/silx/gui/_glutils/gl.py15
-rw-r--r--src/silx/gui/_glutils/utils.py1
10 files changed, 64 insertions, 38 deletions
diff --git a/src/silx/gui/_glutils/Context.py b/src/silx/gui/_glutils/Context.py
index c62dbb9..d2ddaa3 100644
--- a/src/silx/gui/_glutils/Context.py
+++ b/src/silx/gui/_glutils/Context.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2014-2019 European Synchrotron Radiation Facility
diff --git a/src/silx/gui/_glutils/FramebufferTexture.py b/src/silx/gui/_glutils/FramebufferTexture.py
index d12a6e0..75db264 100644
--- a/src/silx/gui/_glutils/FramebufferTexture.py
+++ b/src/silx/gui/_glutils/FramebufferTexture.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2014-2020 European Synchrotron Radiation Facility
diff --git a/src/silx/gui/_glutils/OpenGLWidget.py b/src/silx/gui/_glutils/OpenGLWidget.py
index 2ca4649..d35bb73 100644
--- a/src/silx/gui/_glutils/OpenGLWidget.py
+++ b/src/silx/gui/_glutils/OpenGLWidget.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017-2021 European Synchrotron Radiation Facility
@@ -45,7 +44,7 @@ _logger = logging.getLogger(__name__)
if not hasattr(qt, 'QOpenGLWidget') and not hasattr(qt, 'QGLWidget'):
- OpenGLWidget = None
+ _OpenGLWidget = None
else:
if hasattr(qt, 'QOpenGLWidget'): # PyQt>=5.4
@@ -70,7 +69,7 @@ else:
depthBufferSize=24,
stencilBufferSize=8,
version=(2, 0),
- f=qt.Qt.WindowFlags()):
+ f=qt.Qt.Widget):
# True if using QGLWidget, False if using QOpenGLWidget
self.__legacy = not hasattr(qt, 'QOpenGLWidget')
@@ -262,7 +261,7 @@ class OpenGLWidget(qt.QWidget):
depthBufferSize=24,
stencilBufferSize=8,
version=(2, 0),
- f=qt.Qt.WindowFlags()):
+ f=qt.Qt.Widget):
super(OpenGLWidget, self).__init__(parent, f)
layout = qt.QHBoxLayout(self)
diff --git a/src/silx/gui/_glutils/Program.py b/src/silx/gui/_glutils/Program.py
index 87eec5f..d61c07d 100644
--- a/src/silx/gui/_glutils/Program.py
+++ b/src/silx/gui/_glutils/Program.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2014-2019 European Synchrotron Radiation Facility
diff --git a/src/silx/gui/_glutils/Texture.py b/src/silx/gui/_glutils/Texture.py
index c72135a..76bdcd8 100644
--- a/src/silx/gui/_glutils/Texture.py
+++ b/src/silx/gui/_glutils/Texture.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2014-2020 European Synchrotron Radiation Facility
diff --git a/src/silx/gui/_glutils/VertexBuffer.py b/src/silx/gui/_glutils/VertexBuffer.py
index b74b748..65fff86 100644
--- a/src/silx/gui/_glutils/VertexBuffer.py
+++ b/src/silx/gui/_glutils/VertexBuffer.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2014-2017 European Synchrotron Radiation Facility
diff --git a/src/silx/gui/_glutils/__init__.py b/src/silx/gui/_glutils/__init__.py
index e88affd..a7a4bee 100644
--- a/src/silx/gui/_glutils/__init__.py
+++ b/src/silx/gui/_glutils/__init__.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2014-2019 European Synchrotron Radiation Facility
diff --git a/src/silx/gui/_glutils/font.py b/src/silx/gui/_glutils/font.py
index 3ea474d..bee9745 100644
--- a/src/silx/gui/_glutils/font.py
+++ b/src/silx/gui/_glutils/font.py
@@ -1,7 +1,6 @@
-# coding: utf-8
# /*##########################################################################
#
-# Copyright (c) 2016-2021 European Synchrotron Radiation Facility
+# Copyright (c) 2016-2022 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
@@ -32,8 +31,13 @@ __date__ = "13/10/2016"
import logging
import numpy
-from ..utils.image import convertQImageToArray
from .. import qt
+from ..utils.image import convertQImageToArray
+
+try:
+ from ..utils.matplotlib import rasterMathText
+except ImportError:
+ rasterMathText = None
_logger = logging.getLogger(__name__)
@@ -66,11 +70,7 @@ ULTRA_BLACK = 99
"""Thickest characters: Maximum font weight"""
-def rasterText(text, font,
- size=-1,
- weight=-1,
- italic=False,
- devicePixelRatio=1.0):
+def rasterTextQt(text, font, size=-1, weight=-1, italic=False, devicePixelRatio=1.0):
"""Raster text using Qt.
It supports multiple lines.
@@ -95,7 +95,7 @@ def rasterText(text, font,
"""
if not text:
_logger.info("Trying to raster empty text, replaced by white space")
- text = ' ' # Replace empty text by white space to produce an image
+ text = " " # Replace empty text by white space to produce an image
if not isinstance(font, qt.QFont):
font = qt.QFont(font, size, weight, italic)
@@ -107,7 +107,8 @@ def rasterText(text, font,
painter.setPen(qt.Qt.white)
painter.setFont(font)
bounds = painter.boundingRect(
- qt.QRect(0, 0, 4096, 4096), qt.Qt.TextExpandTabs, text)
+ qt.QRect(0, 0, 4096, 4096), qt.Qt.TextExpandTabs, text
+ )
painter.end()
metrics = qt.QFontMetrics(font)
@@ -123,9 +124,9 @@ def rasterText(text, font,
width = bounds.width() * devicePixelRatio + 2
# align line size to 32 bits to ease conversion to numpy array
width = 4 * ((width + 3) // 4)
- image = qt.QImage(int(width),
- int(bounds.height() * devicePixelRatio + 2),
- qt.QImage.Format_RGB888)
+ image = qt.QImage(
+ int(width), int(bounds.height() * devicePixelRatio + 2), qt.QImage.Format_RGB888
+ )
image.setDevicePixelRatio(devicePixelRatio)
# TODO if Qt5 use Format_Grayscale8 instead
@@ -144,13 +145,45 @@ def rasterText(text, font,
# RGB to R
array = numpy.ascontiguousarray(array[:, :, 0])
- # Remove leading and trailing empty columns but one on each side
- column_cumsum = numpy.cumsum(numpy.sum(array, axis=0))
- array = array[:, column_cumsum.argmin():column_cumsum.argmax() + 2]
+ # Remove leading and trailing empty columns/rows but one on each side
+ filled_rows = numpy.nonzero(numpy.sum(array, axis=1))[0]
+ filled_columns = numpy.nonzero(numpy.sum(array, axis=0))[0]
+ if len(filled_rows) == 0 or len(filled_columns) == 0:
+ return array, metrics.ascent()
- # Remove leading and trailing empty rows but one on each side
- row_cumsum = numpy.cumsum(numpy.sum(array, axis=1))
- min_row = row_cumsum.argmin()
- array = array[min_row:row_cumsum.argmax() + 2, :]
+ min_row = max(0, filled_rows[0] - 1)
+ array = array[
+ min_row : filled_rows[-1] + 2,
+ max(0, filled_columns[0] - 1) : filled_columns[-1] + 2,
+ ]
return array, metrics.ascent() - min_row
+
+
+def rasterText(text, font, size=-1, weight=-1, italic=False, devicePixelRatio=1.0):
+ """Raster text using Qt or matplotlib if there may be math syntax.
+
+ It supports multiple lines.
+
+ :param str text: The text to raster
+ :param font: Font name or QFont to use
+ :type font: str or :class:`QFont`
+ :param int size:
+ Font size in points
+ Used only if font is given as name.
+ :param int weight:
+ Font weight in [0, 99], see QFont.Weight.
+ Used only if font is given as name.
+ :param bool italic:
+ True for italic font (default: False).
+ Used only if font is given as name.
+ :param float devicePixelRatio:
+ The current ratio between device and device-independent pixel
+ (default: 1.0)
+ :return: Corresponding image in gray scale and baseline offset from top
+ :rtype: (HxW numpy.ndarray of uint8, int)
+ """
+ if rasterMathText is not None and text.count("$") >= 2:
+ return rasterMathText(text, font, size, weight, italic, devicePixelRatio)
+ else:
+ return rasterTextQt(text, font, size, weight, italic, devicePixelRatio)
diff --git a/src/silx/gui/_glutils/gl.py b/src/silx/gui/_glutils/gl.py
index 608d9ce..d33cf49 100644
--- a/src/silx/gui/_glutils/gl.py
+++ b/src/silx/gui/_glutils/gl.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2014-2017 European Synchrotron Radiation Facility
@@ -64,7 +63,7 @@ except NameError:
GLchar = c_char
-def testGL():
+def testGL() -> bool:
"""Test if required OpenGL version and extensions are available.
This MUST be run with an active OpenGL context.
@@ -72,18 +71,20 @@ def testGL():
version = glGetString(GL_VERSION).split()[0] # get version number
major, minor = int(version[0]), int(version[2])
if major < 2 or (major == 2 and minor < 1):
- raise RuntimeError(
- "Requires at least OpenGL version 2.1, running with %s" % version)
+ _logger.error("OpenGL version >=2.1 required, running with %s" % version)
+ return False
from OpenGL.GL.ARB.framebuffer_object import glInitFramebufferObjectARB
from OpenGL.GL.ARB.texture_rg import glInitTextureRgARB
if not glInitFramebufferObjectARB():
- raise RuntimeError(
- "OpenGL GL_ARB_framebuffer_object extension required !")
+ _logger.error("OpenGL GL_ARB_framebuffer_object extension required!")
+ return False
if not glInitTextureRgARB():
- raise RuntimeError("OpenGL GL_ARB_texture_rg extension required !")
+ _logger.error("OpenGL GL_ARB_texture_rg extension required!")
+ return False
+ return True
# Additional setup
diff --git a/src/silx/gui/_glutils/utils.py b/src/silx/gui/_glutils/utils.py
index 5886599..49b431a 100644
--- a/src/silx/gui/_glutils/utils.py
+++ b/src/silx/gui/_glutils/utils.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2014-2021 European Synchrotron Radiation Facility