def calibre_cover(title, author_string, series_string=None, output_format='jpg', title_size=46, author_size=36, logo_path=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)
def __init__(self, text, font_size, bottom_margin=30, font_path=None): self.text, self.font_size, = text, font_size self.bottom_margin = bottom_margin if font_path is None: if not isinstance(text, unicode): text = force_unicode(text) from calibre.utils.fonts.utils import get_font_for_text fd = get_font_for_text(text) if fd is not None: from calibre.ptempfile import PersistentTemporaryFile pt = PersistentTemporaryFile('.ttf') pt.write(fd) pt.close() font_path = pt.name self.font_path = font_path
def calibre_cover(title, author_string, series_string=None, output_format='jpg', title_size=46, author_size=36, logo_path=None): from calibre.utils.config_base import tweaks title = normalize(title) author_string = normalize(author_string) series_string = normalize(series_string) from calibre.utils.magick.draw import create_cover_page, TextLine text = title + author_string + (series_string or u'') font_path = tweaks['generate_cover_title_font'] if font_path is None: 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(title, title_size, font_path=font_path), TextLine(author_string, author_size, font_path=font_path) ] if series_string: lines.append(TextLine(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)
def calibre_cover( title, author_string, series_string=None, output_format="jpg", title_size=46, author_size=36, logo_path=None ): from calibre.utils.config_base import tweaks title = normalize(title) author_string = normalize(author_string) series_string = normalize(series_string) from calibre.utils.magick.draw import create_cover_page, TextLine text = title + author_string + (series_string or u"") font_path = tweaks["generate_cover_title_font"] if font_path is None: 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(title, title_size, font_path=font_path), TextLine(author_string, author_size, font_path=font_path), ] if series_string: lines.append(TextLine(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)
def calibre_cover(title, author_string, series_string=None, output_format='jpg', title_size=46, author_size=36, logo_path=None): from calibre.utils.config_base import tweaks 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 = tweaks['generate_cover_title_font'] if font_path is None: 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)