mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add pict.rtf if debugging + simplify extract images in RTFinput
This commit is contained in:
parent
5ffbfc89b5
commit
a335d86cd5
@ -128,34 +128,51 @@ class RTFInput(InputFormatPlugin):
|
||||
def extract_images(self, picts):
|
||||
self.log('Extracting images...')
|
||||
|
||||
count = 0
|
||||
raw = open(picts, 'rb').read()
|
||||
starts = []
|
||||
for match in re.finditer(r'\{\\pict([^}]+)\}', raw):
|
||||
starts.append(match.start(1))
|
||||
picts = filter(len, re.findall(r'\{\\pict([^}]+)\}', raw))
|
||||
hex = re.compile(r'[^a-zA-Z0-9]')
|
||||
encs = [hex.sub('', pict) for pict in picts]
|
||||
|
||||
count = 0
|
||||
imap = {}
|
||||
|
||||
for start in starts:
|
||||
pos, bc = start, 1
|
||||
while bc > 0:
|
||||
if raw[pos] == '}': bc -= 1
|
||||
elif raw[pos] == '{': bc += 1
|
||||
pos += 1
|
||||
pict = raw[start:pos+1]
|
||||
enc = re.sub(r'[^a-zA-Z0-9]', '', pict)
|
||||
for enc in encs:
|
||||
if len(enc) % 2 == 1:
|
||||
enc = enc[:-1]
|
||||
data = enc.decode('hex')
|
||||
count += 1
|
||||
name = (('%4d'%count).replace(' ', '0'))+'.wmf'
|
||||
name = '%04d.wmf' % count
|
||||
open(name, 'wb').write(data)
|
||||
imap[count] = name
|
||||
#open(name+'.hex', 'wb').write(enc)
|
||||
return self.convert_images(imap)
|
||||
|
||||
# count = 0
|
||||
# raw = open(picts, 'rb').read()
|
||||
# starts = []
|
||||
# for match in re.finditer(r'\{\\pict([^}]+)\}', raw):
|
||||
# starts.append(match.start(1))
|
||||
|
||||
# imap = {}
|
||||
# for start in starts:
|
||||
# pos, bc = start, 1
|
||||
# while bc > 0:
|
||||
# if raw[pos] == '}': bc -= 1
|
||||
# elif raw[pos] == '{': bc += 1
|
||||
# pos += 1
|
||||
# pict = raw[start:pos+1]
|
||||
# enc = re.sub(r'[^a-zA-Z0-9]', '', pict)
|
||||
# if len(enc) % 2 == 1:
|
||||
# enc = enc[:-1]
|
||||
# data = enc.decode('hex')
|
||||
# count += 1
|
||||
# name = (('%4d'%count).replace(' ', '0'))+'.wmf'
|
||||
# open(name, 'wb').write(data)
|
||||
# imap[count] = name
|
||||
# #open(name+'.hex', 'wb').write(enc)
|
||||
# return self.convert_images(imap)
|
||||
|
||||
def convert_images(self, imap):
|
||||
for count, val in imap.items():
|
||||
for count, val in imap.iteritems():
|
||||
try:
|
||||
imap[count] = self.convert_image(val)
|
||||
except:
|
||||
|
@ -146,7 +146,8 @@ class Pict:
|
||||
|
||||
def process_pict(self):
|
||||
self.__make_dir()
|
||||
with open(self.__file) as read_obj, open(self.__write_to, 'w') as write_obj:
|
||||
with open(self.__file) as read_obj, \
|
||||
open(self.__write_to, 'w') as write_obj:
|
||||
for line in read_obj:
|
||||
self.__token_info = line[:16]
|
||||
if self.__token_info == 'ob<nu<open-brack':
|
||||
@ -167,6 +168,7 @@ class Pict:
|
||||
copy_obj = copy.Copy(bug_handler = self.__bug_handler)
|
||||
if self.__copy:
|
||||
copy_obj.copy_file(self.__write_to, "pict.data")
|
||||
copy_obj.copy_file(self.__pict_file, "pict.rtf")
|
||||
copy_obj.rename(self.__write_to, self.__file)
|
||||
os.remove(self.__write_to)
|
||||
if self.__pict_count == 0:
|
||||
|
Loading…
x
Reference in New Issue
Block a user