diff --git a/recipes/readitlater.recipe b/recipes/readitlater.recipe index ee8cdf4a28..91c4d417e9 100644 --- a/recipes/readitlater.recipe +++ b/recipes/readitlater.recipe @@ -202,12 +202,12 @@ class Pocket(BasicNewsRecipe): This override adds time to the cover """ try: - from calibre.ebooks import calibre_cover + from calibre.ebooks.covers import calibre_cover2 title = self.title if isinstance(self.title, unicode) else \ self.title.decode('utf-8', 'replace') date = strftime(self.timefmt) time = strftime('[%I:%M %p]') - img_data = calibre_cover(title, date, time) + img_data = calibre_cover2(title, date, time) cover_file.write(img_data) cover_file.flush() except: diff --git a/src/calibre/devices/hanvon/driver.py b/src/calibre/devices/hanvon/driver.py index 755ec3cdfc..bd5c54db36 100644 --- a/src/calibre/devices/hanvon/driver.py +++ b/src/calibre/devices/hanvon/driver.py @@ -106,13 +106,13 @@ class ALEX(N516): return os.path.join(base, 'covers', name) def upload_cover(self, path, filename, metadata, filepath): - from calibre.ebooks import calibre_cover + from calibre.ebooks.covers import calibre_cover2 from calibre.utils.img import scale_image coverdata = getattr(metadata, 'thumbnail', None) if coverdata and coverdata[2]: cover = coverdata[2] else: - cover = calibre_cover(metadata.get('title', _('Unknown')), + cover = calibre_cover2(metadata.get('title', _('Unknown')), metadata.get('authors', _('Unknown'))) cover = scale_image(cover, width=self.THUMBNAIL_HEIGHT, diff --git a/src/calibre/ebooks/__init__.py b/src/calibre/ebooks/__init__.py index 28b0288b4e..50a957f8ec 100644 --- a/src/calibre/ebooks/__init__.py +++ b/src/calibre/ebooks/__init__.py @@ -207,37 +207,10 @@ def calibre_cover(title, author_string, series_string=None, title = normalize(title) author_string = normalize(author_string) series_string = normalize(series_string) - from calibre.utils.magick.draw import create_cover_page, TextLine - import regex - pat = regex.compile(ur'\p{Cf}+', flags=regex.VERSION1) # remove non-printing chars like the soft hyphen - text = pat.sub(u'', title + author_string + (series_string or u'')) - font_path = P('fonts/liberation/LiberationSerif-Bold.ttf') - - from calibre.utils.fonts.utils import get_font_for_text - font = open(font_path, 'rb').read() - c = get_font_for_text(text, font) - cleanup = False - if c is not None and c != font: - from calibre.ptempfile import PersistentTemporaryFile - pt = PersistentTemporaryFile('.ttf') - pt.write(c) - pt.close() - font_path = pt.name - cleanup = True - - lines = [TextLine(pat.sub(u'', title), title_size, font_path=font_path), - TextLine(pat.sub(u'', author_string), author_size, font_path=font_path)] - if series_string: - lines.append(TextLine(pat.sub(u'', series_string), author_size, font_path=font_path)) - if logo_path is None: - logo_path = I('library.png') - try: - return create_cover_page(lines, logo_path, output_format='jpg', - texture_opacity=0.3, texture_data=I('cover_texture.png', - data=True)) - finally: - if cleanup: - os.remove(font_path) + from calibre.ebooks.covers import calibre_cover2 + from calibre.utils.img import image_to_data + ans = calibre_cover2(title, author_string or '', series_string or '', logo_path=logo_path, as_qimage=True) + return image_to_data(ans, fmt=output_format) UNIT_RE = re.compile(r'^(-*[0-9]*[.]?[0-9]*)\s*(%|em|ex|en|px|mm|cm|in|pt|pc|rem|q)$') diff --git a/src/calibre/ebooks/conversion/plugins/rtf_input.py b/src/calibre/ebooks/conversion/plugins/rtf_input.py index 709e026cc7..0996881f6a 100644 --- a/src/calibre/ebooks/conversion/plugins/rtf_input.py +++ b/src/calibre/ebooks/conversion/plugins/rtf_input.py @@ -139,7 +139,7 @@ class RTFInput(InputFormatPlugin): f.write(data) imap[count] = name # with open(name+'.hex', 'wb') as f: - # f.write(enc) + # f.write(enc) return self.convert_images(imap) def convert_images(self, imap): @@ -164,14 +164,13 @@ class RTFInput(InputFormatPlugin): if self.opts.ignore_wmf: os.remove(name) return '__REMOVE_ME__' - from calibre.ebooks import calibre_cover + from calibre.ebooks.covers import message_image if self.default_img is None: - self.default_img = calibre_cover('Conversion of WMF images is not supported', - 'Use Microsoft Word or OpenOffice to save this RTF file' - ' as HTML and convert that in calibre.', title_size=36, - author_size=20) + self.default_img = message_image('Conversion of WMF images is not supported.', + ' Use Microsoft Word or OpenOffice to save this RTF file' + ' as HTML and convert that in calibre.') name = name.replace('.wmf', '.jpg') - with open(name, 'wb') as f: + with lopen(name, 'wb') as f: f.write(self.default_img) return name @@ -292,7 +291,7 @@ class RTFInput(InputFormatPlugin): # Replace newlines inserted by the 'empty_paragraphs' option in rtf2xml with html blank lines # res = re.sub('\s*
', '', res) # res = re.sub('(?<=\n)\n{2}', - # u'\u00a0
\n'.encode('utf-8'), res) + # u'\u00a0
\n'.encode('utf-8'), res) f.write(res) self.write_inline_css(inline_class, border_styles) stream.seek(0) diff --git a/src/calibre/ebooks/covers.py b/src/calibre/ebooks/covers.py index abf57fa00c..fd3b6cf0ae 100644 --- a/src/calibre/ebooks/covers.py +++ b/src/calibre/ebooks/covers.py @@ -535,7 +535,7 @@ def create_cover(title, authors, series=None, series_index=1, prefs=None, as_qim prefs or cprefs, title_template=d['title_template'], subtitle_template=d['subtitle_template'], footer_template=d['footer_template']) return generate_cover(mi, prefs=prefs, as_qimage=as_qimage) -def calibre_cover2(title, author_string='', series_string='', prefs=None, as_qimage=False): +def calibre_cover2(title, author_string='', series_string='', prefs=None, as_qimage=False, logo_path=None): init_environment() title, subtitle, footer = '' + escape_formatting(title), '' + escape_formatting(series_string), '' + escape_formatting(author_string) prefs = prefs or cprefs @@ -555,7 +555,7 @@ def calibre_cover2(title, author_string='', series_string='', prefs=None, as_qim height = title_block.height + subtitle_block.height + extra_spacing + title_block.leading top += height + 25 bottom = footer_block.position.y - 50 - logo = QImage(I('library.png')) + logo = QImage(logo_path or I('library.png')) pwidth, pheight = rect.width(), bottom - top scaled, width, height = fit_image(logo.width(), logo.height(), pwidth, pheight) x, y = (pwidth - width) // 2, (pheight - height) // 2 @@ -578,6 +578,19 @@ def calibre_cover2(title, author_string='', series_string='', prefs=None, as_qim return img return pixmap_to_data(img) +def message_image(text, width=500, height=400, font_size=20): + init_environment() + img = QImage(width, height, QImage.Format_ARGB32) + img.fill(Qt.white) + p = QPainter(img) + f = QFont() + f.setPixelSize(font_size) + p.setFont(f) + r = img.rect().adjusted(10, 10, -10, -10) + p.drawText(r, Qt.AlignJustify | Qt.AlignVCenter | Qt.TextWordWrap, text) + p.end() + return pixmap_to_data(img) + def scale_cover(prefs, scale): for x in ('cover_width', 'cover_height', 'title_font_size', 'subtitle_font_size', 'footer_font_size'): prefs[x] = int(scale * prefs[x]) diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index 0be866ec57..61b98b8ce1 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -90,20 +90,20 @@ def get_gui(): def add_quick_start_guide(library_view, refresh_cover_browser=None): from calibre.ebooks.metadata.meta import get_metadata - from calibre.ebooks import calibre_cover + from calibre.ebooks.covers import calibre_cover2 from calibre.utils.zipfile import safe_replace from calibre.utils.localization import get_lang, canonicalize_lang from calibre.ptempfile import PersistentTemporaryFile l = canonicalize_lang(get_lang()) or 'eng' gprefs['quick_start_guide_added'] = True - imgbuf = BytesIO(calibre_cover(_('Quick Start Guide'), '', author_size=8)) + imgbuf = BytesIO(calibre_cover2(_('Quick Start Guide'), '')) try: - with open(P('quick_start/%s.epub' % l), 'rb') as src: + with lopen(P('quick_start/%s.epub' % l), 'rb') as src: buf = BytesIO(src.read()) except EnvironmentError as err: if err.errno != errno.ENOENT: raise - with open(P('quick_start/eng.epub'), 'rb') as src: + with lopen(P('quick_start/eng.epub'), 'rb') as src: buf = BytesIO(src.read()) safe_replace(buf, 'images/cover.jpg', imgbuf) buf.seek(0)