def create_cover_page(self, input_fmt): templ = ''' <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><style> html, body, img { height: 100%%; display: block; margin: 0; padding: 0; border-width: 0; } img { width: auto; margin-left:auto; margin-right: auto; } </style></head><body><img src="%s"/></body></html> ''' if input_fmt == 'epub': def cover_path(action, data): if action == 'write_image': data.write(BLANK_JPEG) return set_epub_cover(self, cover_path, (lambda *a: None), options={'template':templ}) raster_cover_name = find_cover_image(self, strict=True) if raster_cover_name is None: item = self.generate_item(name='cover.jpeg', id_prefix='cover') raster_cover_name = self.href_to_name(item.get('href'), self.opf_name) with self.open(raster_cover_name, 'wb') as dest: dest.write(BLANK_JPEG) item = self.generate_item(name='titlepage.html', id_prefix='titlepage') titlepage_name = self.href_to_name(item.get('href'), self.opf_name) raw = templ % prepare_string_for_xml(self.name_to_href(raster_cover_name, titlepage_name), True) with self.open(titlepage_name, 'wb') as f: f.write(raw.encode('utf-8')) spine = self.opf_xpath('//opf:spine')[0] ref = spine.makeelement(OPF('itemref'), idref=item.get('id')) self.insert_into_xml(spine, ref, index=0) self.dirty(self.opf_name) return raster_cover_name, titlepage_name
def create_cover_page(self, input_fmt): if input_fmt == 'epub': def cover_path(action, data): if action == 'write_image': data.write(BLANK_JPEG) return set_epub_cover(self, cover_path, (lambda *a: None)) from calibre.ebooks.oeb.transforms.cover import CoverManager raster_cover_name = find_cover_image(self, strict=True) if raster_cover_name is None: item = self.generate_item(name='cover.jpeg', id_prefix='cover') raster_cover_name = self.href_to_name(item.get('href'), self.opf_name) with self.open(raster_cover_name, 'wb') as dest: dest.write(BLANK_JPEG) item = self.generate_item(name='titlepage.html', id_prefix='titlepage') titlepage_name = self.href_to_name(item.get('href'), self.opf_name) templ = CoverManager.SVG_TEMPLATE raw = templ % self.name_to_href(raster_cover_name, titlepage_name) with self.open(titlepage_name, 'wb') as f: f.write(raw.encode('utf-8')) spine = self.opf_xpath('//opf:spine')[0] ref = spine.makeelement(OPF('itemref'), idref=item.get('id')) self.insert_into_xml(spine, ref, index=0) self.dirty(self.opf_name) return raster_cover_name, titlepage_name
def create_cover_page(self, input_fmt): templ = ''' <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><style> html, body, img { height: 100%%; display: block; margin: 0; padding: 0; border-width: 0; } img { width: auto; margin-left:auto; margin-right: auto; } </style></head><body><img src="%s"/></body></html> ''' if input_fmt == 'epub': def cover_path(action, data): if action == 'write_image': data.write(BLANK_JPEG) return set_epub_cover(self, cover_path, (lambda *a: None), options={'template': templ}) raster_cover_name = find_cover_image(self, strict=True) if raster_cover_name is None: item = self.generate_item(name='cover.jpeg', id_prefix='cover') raster_cover_name = self.href_to_name(item.get('href'), self.opf_name) with self.open(raster_cover_name, 'wb') as dest: dest.write(BLANK_JPEG) item = self.generate_item(name='titlepage.html', id_prefix='titlepage') titlepage_name = self.href_to_name(item.get('href'), self.opf_name) raw = templ % prepare_string_for_xml( self.name_to_href(raster_cover_name, titlepage_name), True) with self.open(titlepage_name, 'wb') as f: f.write(raw.encode('utf-8')) spine = self.opf_xpath('//opf:spine')[0] ref = spine.makeelement(OPF('itemref'), idref=item.get('id')) self.insert_into_xml(spine, ref, index=0) self.dirty(self.opf_name) return raster_cover_name, titlepage_name
def create_cover_page(self, input_fmt): templ = ''' <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><style> html, body, img { height: 100vh; display: block; margin: 0; padding: 0; border-width: 0; } img { width: 100%%; height: 100%%; object-fit: contain; margin-left: auto; margin-right: auto; max-width: 100vw; max-height: 100vh; top: 50vh; transform: translateY(-50%%); position: relative; } body.cover-fill img { object-fit: fill; } </style></head><body><img src="%s"/></body></html> ''' def generic_cover(): if self.book_metadata is not None: from calibre.ebooks.covers import create_cover mi = self.book_metadata return create_cover(mi.title, mi.authors, mi.series, mi.series_index) return BLANK_JPEG if input_fmt == 'epub': def image_callback(cover_image, wrapped_image): if cover_image: image_callback.cover_data = self.raw_data(cover_image, decode=False) if wrapped_image and not getattr(image_callback, 'cover_data', None): image_callback.cover_data = self.raw_data(wrapped_image, decode=False) def cover_path(action, data): if action == 'write_image': cdata = getattr(image_callback, 'cover_data', None) or generic_cover() data.write(cdata) raster_cover_name, titlepage_name = set_epub_cover( self, cover_path, (lambda *a: None), options={'template':templ}, image_callback=image_callback) else: raster_cover_name = find_cover_image(self, strict=True) if raster_cover_name is None: item = self.generate_item(name='cover.jpeg', id_prefix='cover') raster_cover_name = self.href_to_name(item.get('href'), self.opf_name) with self.open(raster_cover_name, 'wb') as dest: dest.write(generic_cover()) item = self.generate_item(name='titlepage.html', id_prefix='titlepage') titlepage_name = self.href_to_name(item.get('href'), self.opf_name) raw = templ % prepare_string_for_xml(self.name_to_href(raster_cover_name, titlepage_name), True) with self.open(titlepage_name, 'wb') as f: f.write(raw.encode('utf-8')) spine = self.opf_xpath('//opf:spine')[0] ref = spine.makeelement(OPF('itemref'), idref=item.get('id')) self.insert_into_xml(spine, ref, index=0) self.dirty(self.opf_name) return raster_cover_name, titlepage_name
def create_cover_page(self, input_fmt): templ = ''' <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><style> html, body, img { height: 100vh; display: block; margin: 0; padding: 0; border-width: 0; } img { width: auto; height: auto; margin-left: auto; margin-right: auto; max-width: 100vw; max-height: 100vh } </style></head><body><img src="%s"/></body></html> ''' blank = {'q': False} if input_fmt == 'epub': def cover_path(action, data): if action == 'write_image': data.write(BLANK_JPEG) blank['q'] = True raster_cover_name, titlepage_name = set_epub_cover( self, cover_path, (lambda *a: None), options={'template': templ}) else: raster_cover_name = find_cover_image(self, strict=True) if raster_cover_name is None: item = self.generate_item(name='cover.jpeg', id_prefix='cover') raster_cover_name = self.href_to_name(item.get('href'), self.opf_name) with self.open(raster_cover_name, 'wb') as dest: dest.write(BLANK_JPEG) blank['q'] = True item = self.generate_item(name='titlepage.html', id_prefix='titlepage') titlepage_name = self.href_to_name(item.get('href'), self.opf_name) raw = templ % prepare_string_for_xml( self.name_to_href(raster_cover_name, titlepage_name), True) with self.open(titlepage_name, 'wb') as f: f.write(raw.encode('utf-8')) spine = self.opf_xpath('//opf:spine')[0] ref = spine.makeelement(OPF('itemref'), idref=item.get('id')) self.insert_into_xml(spine, ref, index=0) self.dirty(self.opf_name) if blank['q'] and self.book_metadata is not None: authors = authors_to_string(self.book_metadata.authors) title = self.book_metadata.title with self.open(titlepage_name, 'wb') as f: f.write(''' <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><meta charset="utf-8"/></head> <body> <div style="position: fixed; top: 50%; width: 100vw; transform: translateY(-50%)"> <h1 style="text-align: center; margin: auto">{title}</h1> <p>\xa0</p> <h3 style="text-align: center; margin: auto; font-style: italic">{authors}</h3> </div> </body> </html> '''.format(title=title, authors=authors).encode('utf-8')) return raster_cover_name, titlepage_name