Remove IM from calibre_cover()

This commit is contained in:
Kovid Goyal 2016-05-05 10:05:21 +05:30
parent 0e282c10fc
commit 955a83a320
6 changed files with 34 additions and 49 deletions

View File

@ -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:

View File

@ -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,

View File

@ -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)$')

View File

@ -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*<body>', '<body>', res)
# res = re.sub('(?<=\n)\n{2}',
# u'<p>\u00a0</p>\n'.encode('utf-8'), res)
# u'<p>\u00a0</p>\n'.encode('utf-8'), res)
f.write(res)
self.write_inline_css(inline_class, border_styles)
stream.seek(0)

View File

@ -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 = '<b>' + escape_formatting(title), '<i>' + escape_formatting(series_string), '<b>' + 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])

View File

@ -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)