From 4c2d255d8632df305efd07f167b6aa1a984da910 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 28 Dec 2019 12:36:59 +0530 Subject: [PATCH] RTF Output: Fix a regression that slowed down conversion. Fixes #1857732 [RTF Conversion Failure](https://bugs.launchpad.net/calibre/+bug/1857732) --- src/calibre/ebooks/rtf/rtfml.py | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/calibre/ebooks/rtf/rtfml.py b/src/calibre/ebooks/rtf/rtfml.py index 11736ffaef..d3814d8228 100644 --- a/src/calibre/ebooks/rtf/rtfml.py +++ b/src/calibre/ebooks/rtf/rtfml.py @@ -12,6 +12,7 @@ Transform OEB content into RTF markup import os import re import io +from binascii import hexlify from lxml import etree @@ -184,24 +185,14 @@ class RTFMLizer(object): return text def image_to_hexstring(self, data): + # Images must be hex-encoded in 128 character lines data = save_cover_data_to(data) width, height = identify(data)[1:] - - raw_hex = '' - for char in bytearray(data): - raw_hex += hex(char).replace('0x', '').rjust(2, '0') - - # Images must be broken up so that they are no longer than 129 chars - # per line - hex_string = '' - col = 1 - for char in raw_hex: - if col == 129: - hex_string += '\n' - col = 1 - col += 1 - hex_string += char - + lines = [] + v = memoryview(data) + for i in range(0, len(data), 64): + lines.append(hexlify(v[i:i+64])) + hex_string = b'\n'.join(lines).decode('ascii') return hex_string, width, height def clean_text(self, text):