mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix icon loading
This commit is contained in:
parent
72d33ab385
commit
21e57640a3
@ -26,10 +26,10 @@ def pixmap_to_data(pixmap):
|
||||
buf = QBuffer(ba)
|
||||
buf.open(QBuffer.WriteOnly)
|
||||
pixmap.save(buf, 'PNG')
|
||||
return bytearray(ba.data())
|
||||
return bytes(bytearray(ba.data()))
|
||||
|
||||
|
||||
def load_icon_resource(icon_resource, as_data=False, size=ICON_SIZE):
|
||||
def load_icon_resource_as_pixmap(icon_resource, size=ICON_SIZE):
|
||||
if not icon_resource:
|
||||
return
|
||||
parts = tuple(filter(None, re.split(r',([-0-9]+$)', icon_resource)))
|
||||
@ -53,10 +53,57 @@ def load_icon_resource(icon_resource, as_data=False, size=ICON_SIZE):
|
||||
pixmaps.append(pixmap)
|
||||
if not pixmaps:
|
||||
return
|
||||
pixmaps.sort(key=lambda p: p.width())
|
||||
|
||||
def area(p):
|
||||
return p.width() * p.height()
|
||||
|
||||
pixmaps.sort(key=area)
|
||||
q = size * size
|
||||
for pmap in pixmaps:
|
||||
if pmap.width() >= size:
|
||||
if pmap.width() == size:
|
||||
if area(pmap) >= q:
|
||||
if area(pmap) == size:
|
||||
return pmap
|
||||
return pixmap.scaled(size, size, transformMode=Qt.SmoothTransformation)
|
||||
return pixmaps[-1].scaled(size, size, transformMode=Qt.SmoothTransformation)
|
||||
return pmap.scaled(size, size, aspectRatioMode=Qt.KeepAspectRatio, transformMode=Qt.SmoothTransformation)
|
||||
return pixmaps[-1].scaled(size, size, aspectRatioMode=Qt.KeepAspectRatio, transformMode=Qt.SmoothTransformation)
|
||||
|
||||
|
||||
def load_icon_resource(icon_resource, as_data=False, size=ICON_SIZE):
|
||||
ans = load_icon_resource_as_pixmap(icon_resource, size=size)
|
||||
if ans is not None:
|
||||
if as_data:
|
||||
ans = pixmap_to_data(ans)
|
||||
return ans
|
||||
|
||||
|
||||
def display_image(png_data):
|
||||
import sys
|
||||
from base64 import standard_b64encode
|
||||
|
||||
def serialize_gr_command(cmd, payload=None):
|
||||
cmd = ','.join('{}={}'.format(k, v) for k, v in cmd.items())
|
||||
ans = []
|
||||
w = ans.append
|
||||
w(b'\033_G'), w(cmd.encode('ascii'))
|
||||
if payload:
|
||||
w(b';')
|
||||
w(payload)
|
||||
w(b'\033\\')
|
||||
return b''.join(ans)
|
||||
|
||||
def write_chunked(cmd, data):
|
||||
data = standard_b64encode(data)
|
||||
while data:
|
||||
chunk, data = data[:4096], data[4096:]
|
||||
m = 1 if data else 0
|
||||
cmd['m'] = m
|
||||
sys.stdout.buffer.write(serialize_gr_command(cmd, chunk))
|
||||
sys.stdout.flush()
|
||||
cmd.clear()
|
||||
|
||||
write_chunked({'a': 'T', 'f': 100}, png_data)
|
||||
|
||||
|
||||
def test():
|
||||
import sys
|
||||
png_data = load_icon_resource(sys.argv[-1], as_data=True)
|
||||
display_image(png_data)
|
||||
|
Loading…
x
Reference in New Issue
Block a user