summaryrefslogtreecommitdiff
path: root/silx/gui/_glutils/font.py
blob: 566ae493d9818518eecb051ef65443effd818b8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 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
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""Text rasterisation feature leveraging Qt font and text layout support."""

__authors__ = ["T. Vincent"]
__license__ = "MIT"
__date__ = "13/10/2016"


import logging
import sys
import numpy
from .. import qt
from .._utils import convertQImageToArray


_logger = logging.getLogger(__name__)


def getDefaultFontFamily():
    """Returns the default font family of the application"""
    return qt.QApplication.instance().font().family()


# Font weights
ULTRA_LIGHT = 0
"""Lightest characters: Minimum font weight"""

LIGHT = 25
"""Light characters"""

NORMAL = 50
"""Normal characters"""

SEMI_BOLD = 63
"""Between normal and bold characters"""

BOLD = 74
"""Thicker characters"""

BLACK = 87
"""Really thick characters"""

ULTRA_BLACK = 99
"""Thickest characters: Maximum font weight"""


def rasterText(text, font,
               size=-1,
               weight=-1,
               italic=False,
               devicePixelRatio=1.0):
    """Raster text using Qt.

    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 not text:
        _logger.info("Trying to raster empty text, replaced by white space")
        text = ' '  # Replace empty text by white space to produce an image

    if not isinstance(font, qt.QFont):
        font = qt.QFont(font, size, weight, italic)

    metrics = qt.QFontMetrics(font)
    size = metrics.size(qt.Qt.TextExpandTabs, text)
    bounds = metrics.boundingRect(
        qt.QRect(0, 0, size.width(), size.height()),
        qt.Qt.TextExpandTabs,
        text)

    if (devicePixelRatio != 1.0 and
            not hasattr(qt.QImage, 'setDevicePixelRatio')):  # Qt 4
        _logger.error('devicePixelRatio not supported')
        devicePixelRatio = 1.0

    # Add extra border and handle devicePixelRatio
    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(width,
                      bounds.height() * devicePixelRatio,
                      qt.QImage.Format_RGB888)
    if (devicePixelRatio != 1.0 and
            hasattr(image, 'setDevicePixelRatio')):  # Qt 5
        image.setDevicePixelRatio(devicePixelRatio)

    # TODO if Qt5 use Format_Grayscale8 instead
    image.fill(0)

    # Raster text
    painter = qt.QPainter()
    painter.begin(image)
    painter.setPen(qt.Qt.white)
    painter.setFont(font)
    painter.drawText(bounds, qt.Qt.TextExpandTabs, text)
    painter.end()

    array = convertQImageToArray(image)

    # 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 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, :]

    return array, metrics.ascent() - min_row