|
17 | 17 | from __future__ import print_function
|
18 | 18 | import json
|
19 | 19 | import os
|
| 20 | +import hashlib |
20 | 21 |
|
21 | 22 | # Third party imports
|
22 | 23 | from qtpy.QtCore import QObject, QPoint, QRect, qRound, Qt
|
@@ -121,6 +122,10 @@ def _paint_icon(self, iconic, painter, rect, mode, state, options):
|
121 | 122 | painter.restore()
|
122 | 123 |
|
123 | 124 |
|
| 125 | +class FontError(Exception): |
| 126 | + """Exception for font errors.""" |
| 127 | + |
| 128 | + |
124 | 129 | class CharIconEngine(QIconEngine):
|
125 | 130 |
|
126 | 131 | """Specialization of QIconEngine used to draw font-based icons."""
|
@@ -197,14 +202,35 @@ def hook(obj):
|
197 | 202 | with open(os.path.join(directory, charmap_filename), 'r') as codes:
|
198 | 203 | self.charmap[prefix] = json.load(codes, object_hook=hook)
|
199 | 204 |
|
200 |
| - id_ = QFontDatabase.addApplicationFont(os.path.join(directory, ttf_filename)) |
| 205 | + md5_hashes = {'fontawesome-webfont.ttf': |
| 206 | + 'a3de2170e4e9df77161ea5d3f31b2668', |
| 207 | + 'elusiveicons-webfont.ttf': |
| 208 | + '207966b04c032d5b873fd595a211582e'} |
| 209 | + ttf_hash = md5_hashes.get(ttf_filename, None) |
| 210 | + if ttf_hash is not None: |
| 211 | + hasher = hashlib.md5() |
| 212 | + with open(os.path.join(directory, ttf_filename), 'rb') as tff_font: |
| 213 | + buffer = tff_font.read() |
| 214 | + hasher.update(buffer) |
| 215 | + ttf_calculated_hash_code = hasher.hexdigest() |
| 216 | + if ttf_calculated_hash_code != ttf_hash: |
| 217 | + raise FontError(u"Font is corrupt at: '{0}'".format( |
| 218 | + os.path.join(directory, ttf_filename))) |
| 219 | + |
| 220 | + id_ = QFontDatabase.addApplicationFont(os.path.join(directory, |
| 221 | + ttf_filename)) |
201 | 222 |
|
202 | 223 | loadedFontFamilies = QFontDatabase.applicationFontFamilies(id_)
|
203 | 224 |
|
204 | 225 | if(loadedFontFamilies):
|
205 | 226 | self.fontname[prefix] = loadedFontFamilies[0]
|
206 | 227 | else:
|
207 |
| - print('Font is empty') |
| 228 | + raise FontError(u"Font at '{0}' appears to be empty. " |
| 229 | + "If you are on Windows 10, please read " |
| 230 | + "https://support.microsoft.com/en-us/kb/3053676 " |
| 231 | + "to know how to prevent Windows from blocking " |
| 232 | + "the fonts that come with QtAwesome.".format( |
| 233 | + os.path.join(directory, ttf_filename))) |
208 | 234 |
|
209 | 235 | def icon(self, *names, **kwargs):
|
210 | 236 | """
|
|
0 commit comments