diff --git a/kindlecomicconverter/comic2ebook.py b/kindlecomicconverter/comic2ebook.py index 0b69ec5e..75c67479 100755 --- a/kindlecomicconverter/comic2ebook.py +++ b/kindlecomicconverter/comic2ebook.py @@ -513,6 +513,7 @@ def buildEPUB(path, chapternames, tomenumber): tomenumber), options.uuid)) # Overwrite chapternames if tree is flat and ComicInfo.xml has bookmarks if not chapternames and options.chapters: + filelen = len(filelist) chapterlist = [] global_diff = 0 @@ -525,16 +526,25 @@ def buildEPUB(path, chapternames, tomenumber): elif options.splitter == 2: diff_delta = 2 + if options.spreadadjust and options.splitter != 1: + diff_delta -= 1 + for aChapter in options.chapters: pageid = aChapter[0] cur_diff = global_diff global_diff = 0 for x in range(0, pageid + cur_diff + 1): - if '-kcc-b' in filelist[x][1]: - pageid += diff_delta - global_diff += diff_delta - + if x < filelen: + if '-kcc-b' in filelist[x][1]: + pageid += diff_delta + global_diff += diff_delta + if options.spreadadjust and options.splitter == 1 and '-kcc-a' in filelist[x][1]: + pageid -= 1 + global_diff -= 1 + + if pageid >= filelen: + pageid = filelen - 1 filename = filelist[pageid][1] chapterlist.append((filelist[pageid][0].replace('Images', 'Text'), filename)) chapternames[filename] = aChapter[1] @@ -576,7 +586,8 @@ def imgDirectoryProcessing(path): raise UserWarning("Source directory is empty.") -def imgFileProcessingTick(output): +def imgFileProcessingTick(result): + output, spreadadjust = result if isinstance(output, tuple): workerOutput.append(output) workerPool.terminate() @@ -585,6 +596,9 @@ def imgFileProcessingTick(output): if page is not None: options.imgMetadata[page[0]] = page[1] options.imgOld.append(page[2]) + # options.doublepageattribute is True when there is a DoublePage attribute already found in the xml + if not options.doublepageattribute and not options.spreadadjust: + options.spreadadjust = spreadadjust if GUI: GUI.progressBarTick.emit('tick') if not GUI.conversionAlive: @@ -598,6 +612,7 @@ def imgFileProcessing(work): opt = work[2] output = [] workImg = image.ComicPageParser((dirpath, afile), opt) + spreadadjust = workImg.opt.spreadadjust for i in workImg.payload: img = image.ComicPage(opt, *i) if opt.cropping == 2 and not opt.webtoon: @@ -609,7 +624,7 @@ def imgFileProcessing(work): if opt.forcepng and not opt.forcecolor: img.quantizeImage() output.append(img.saveToDir()) - return output + return (output, spreadadjust) except Exception: return str(sys.exc_info()[1]), sanitizeTrace(sys.exc_info()[2]) @@ -691,6 +706,9 @@ def getComicInfo(path, originalpath): xmlPath = os.path.join(path, 'ComicInfo.xml') options.authors = ['KCC'] options.chapters = [] + options.doublepageattribute = False + # toggled only when splitCheck finds spread pages in source files AND the ComicInfo.xml does not find a DoublePage attribute in Pages list + options.spreadadjust = False options.summary = '' titleSuffix = '' if options.title == 'defaulttitle': @@ -728,6 +746,8 @@ def getComicInfo(path, originalpath): options.authors = ['KCC'] if xml.data['Bookmarks']: options.chapters = xml.data['Bookmarks'] + if xml.data['DoublePages']: + options.doublepageattribute = xml.data['DoublePages'] if xml.data['Summary']: options.summary = hescape(xml.data['Summary']) os.remove(xmlPath) diff --git a/kindlecomicconverter/image.py b/kindlecomicconverter/image.py index 9df8049f..7f41b442 100755 --- a/kindlecomicconverter/image.py +++ b/kindlecomicconverter/image.py @@ -174,6 +174,7 @@ def splitCheck(self): if self.opt.splitter > 0: self.payload.append(['R', self.source, self.image.rotate(90, Image.Resampling.BICUBIC, True), self.color, self.fill]) + self.opt.spreadadjust = True else: self.payload.append(['N', self.source, self.image, self.color, self.fill]) diff --git a/kindlecomicconverter/metadata.py b/kindlecomicconverter/metadata.py index 51ade3b5..e653e143 100644 --- a/kindlecomicconverter/metadata.py +++ b/kindlecomicconverter/metadata.py @@ -35,6 +35,7 @@ def __init__(self, source): 'Colorists': [], 'Summary': '', 'Bookmarks': [], + 'DoublePages': False, 'Title': ''} self.rawdata = None self.format = None @@ -72,6 +73,8 @@ def parseXML(self): if 'Bookmark' in page.attributes and 'Image' in page.attributes: self.data['Bookmarks'].append((int(page.attributes['Image'].value), page.attributes['Bookmark'].value)) + if 'DoublePage' in page.attributes: + self.data['DoublePages'] = True def saveXML(self): if self.rawdata: