HTMLDisplay: Add resources to the document so they are not repeatedly loaded

This commit is contained in:
Kovid Goyal 2023-09-22 19:38:27 +05:30
parent ae029cf06a
commit c966582265
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -580,16 +580,22 @@ class HTMLDisplay(QTextBrowser):
data = f.read()
except OSError:
if path.rpartition('.')[-1].lower() in {'jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp'}:
return QByteArray(bytearray.fromhex(
r = QByteArray(bytearray.fromhex(
'89504e470d0a1a0a0000000d49484452'
'000000010000000108060000001f15c4'
'890000000a49444154789c6300010000'
'0500010d0a2db40000000049454e44ae'
'426082'))
self.document().addResource(rtype, qurl, r)
return r
else:
return QByteArray(data)
r = QByteArray(data)
self.document().addResource(rtype, qurl, r)
return r
elif qurl.scheme() == 'calibre-icon':
return QIcon.icon_as_png(qurl.path().lstrip('/'), as_bytearray=True)
r = QIcon.icon_as_png(qurl.path().lstrip('/'), as_bytearray=True)
self.document().addResource(rtype, qurl, r)
return r
elif self.notes_resource_scheme and qurl.scheme() == self.notes_resource_scheme and int(rtype) == int(QTextDocument.ResourceType.ImageResource):
from calibre.gui2.ui import get_gui
gui = get_gui()
@ -597,7 +603,9 @@ class HTMLDisplay(QTextBrowser):
db = gui.current_db.new_api
resource = db.get_notes_resource(f'{qurl.host()}:{qurl.path()[1:]}')
if resource is not None:
return QByteArray(resource['data'])
r = QByteArray(resource['data'])
self.document().addResource(rtype, qurl, r)
return r
else:
return QTextBrowser.loadResource(self, rtype, qurl)