From 18a72965fc7b90155130e87d4f1a4dd4aa354549 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Thu, 13 Jan 2011 10:42:58 +0100 Subject: [PATCH] some cleaning some more cover generation --- librarian/cover.py | 71 ++++++++++++++---- librarian/epub.py | 28 +++---- .../{pdf => fonts}/JunicodeWL-Italic.ttf | Bin .../{pdf => fonts}/JunicodeWL-Regular.ttf | Bin librarian/pdf.py | 4 +- librarian/{xslt => pdf}/wl2tex.xslt | 0 .../wl-logo-small.png} | Bin librarian/{pdf => res}/wl-logo.png | Bin scripts/book2partner | 4 +- setup.py | 2 +- 10 files changed, 73 insertions(+), 36 deletions(-) rename librarian/{pdf => fonts}/JunicodeWL-Italic.ttf (100%) rename librarian/{pdf => fonts}/JunicodeWL-Regular.ttf (100%) rename librarian/{xslt => pdf}/wl2tex.xslt (100%) rename librarian/{epub/logo_wolnelektury.png => res/wl-logo-small.png} (100%) rename librarian/{pdf => res}/wl-logo.png (100%) diff --git a/librarian/cover.py b/librarian/cover.py index a7fc674..c946d43 100644 --- a/librarian/cover.py +++ b/librarian/cover.py @@ -3,37 +3,78 @@ # This file is part of Librarian, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # -import Image, ImageFont, ImageDraw +import Image, ImageFont, ImageDraw, ImageFilter from librarian import get_resource -def cover(width, height, author, title): - def draw_centered_text(text, draw, font, width, pos_y, lineskip): +def cover(author, title, + width, height, background_color, background_img, + author_top, author_margin_left, author_margin_right, author_lineskip, author_color, author_font, author_shadow, + title_top, title_margin_left, title_margin_right, title_lineskip, title_color, title_font, title_shadow, + logo_width, logo_bottom + ): + def draw_centered_text(text, img, font, margin_left, width, pos_y, lineskip, color, shadow_color): + if shadow_color: + shadow_img = Image.new('RGBA', img.size) + shadow_draw = ImageDraw.Draw(shadow_img) + text_img = Image.new('RGBA', img.size) + text_draw = ImageDraw.Draw(text_img) while text: line = text - while draw.textsize(line, font=font)[0] > width: + while text_draw.textsize(line, font=font)[0] > width: try: line, ext = line.rsplit(' ', 1) except: break - draw.text(((img.size[0] - draw.textsize(line, font=font)[0]) / 2, pos_y), line, font=font, fill='#000') + pos_x = margin_left + (width - text_draw.textsize(line, font=font)[0]) / 2 + if shadow_color: + shadow_draw.text((pos_x + 3, pos_y + 3), line, font=font, fill=shadow_color) + text_draw.text((pos_x, pos_y), line, font=font, fill=color) pos_y += lineskip text = text[len(line)+1:] + if shadow_color: + shadow_img = shadow_img.filter(ImageFilter.BLUR) + img.paste(shadow_img, None, shadow_img) + img.paste(text_img, None, text_img) return pos_y - img = Image.new('RGB', (width, height), (255, 255, 255)) + img = Image.new('RGB', (width, height), background_color) - # WL logo - logo = Image.open(get_resource('pdf/wl-logo.png')) - logo = logo.resize((img.size[0] / 2, logo.size[1] * img.size[0] / 2 / logo.size[0])) - img.paste(logo, (width / 4, img.size[1] - logo.size[1])) + if background_img: + background = Image.open(background_img) + img.paste(background) + del background - draw = ImageDraw.Draw(img) - author_font = ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), width/20) - title_y = draw_centered_text(author, draw, author_font, width*9/10, height/10, width/15) + height/10 + # WL logo + if logo_width: + logo = Image.open(get_resource('res/wl-logo.png')) + logo = logo.resize((logo_width, logo.size[1] * logo_width / logo.size[0])) + img.paste(logo, ((width - logo_width) / 2, img.size[1] - logo.size[1] - logo_bottom)) - title_font = ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), width/15) - draw_centered_text(title, draw, title_font, width*9/10, title_y, width/11) + title_y = draw_centered_text(author, img, author_font, + author_margin_left, width - author_margin_left - author_margin_right, author_top, + author_lineskip, author_color, author_shadow) + title_top + draw_centered_text(title, img, title_font, + title_margin_left, width - title_margin_left - title_margin_right, title_y, + title_lineskip, title_color, title_shadow) return img + + +def virtualo_cover(author, title): + return cover(author, title, + 600, 730, '#fff', None, + 73, 20, 20, 40, '#000', ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), 30), None, + 73, 20, 20, 54, '#000', ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), 40), None, + 300, 0 + ) + +def asbis_cover(author, title): + return cover(author, u"„%s”" % title, + 800, 800, '#000', '', + 455, 230, 170, 60, '#fff', ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50), '#000', + 0, 230, 170, 60, '#fff', ImageFont.truetype(get_resource('fonts/JunicodeWL-Italic.ttf'), 50), '#000', + None, None + ) + diff --git a/librarian/epub.py b/librarian/epub.py index 2fec331..666bcc9 100644 --- a/librarian/epub.py +++ b/librarian/epub.py @@ -19,7 +19,7 @@ import sys from librarian import XMLNamespace, RDFNS, DCNS, WLNS, NCXNS, OPFNS, NoDublinCore from librarian.dcparser import BookInfo -from librarian import functions +from librarian import functions, get_resource functions.reg_person_name() @@ -73,11 +73,6 @@ def xslt(xml, sheet): return xml.xslt(etree.parse(xsltf)) -_resdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'epub') -def res(fname): - return os.path.join(_resdir, fname) - - def replace_characters(node): def replace_chars(text): if text is None: @@ -257,13 +252,13 @@ def transform_chunk(chunk_xml, chunk_no, annotations, empty=False, _empty_html_s element.set('sub', str(subnumber)) if empty: if not _empty_html_static: - _empty_html_static.append(open(res('emptyChunk.html')).read()) + _empty_html_static.append(open(get_resource('epub/emptyChunk.html')).read()) chars = set() output_html = _empty_html_static[0] else: find_annotations(annotations, chunk_xml, chunk_no) replace_by_verse(chunk_xml) - html_tree = xslt(chunk_xml, res('xsltScheme.xsl')) + html_tree = xslt(chunk_xml, get_resource('epub/xsltScheme.xsl')) chars = used_chars(html_tree.getroot()) output_html = etree.tostring(html_tree, method="html", pretty_print=True) return output_html, toc, chars @@ -293,7 +288,7 @@ def transform(provider, slug=None, file_path=None, output_file=None, output_dir= chars = set() if first: # write book title page - html_tree = xslt(input_xml, res('xsltTitle.xsl')) + html_tree = xslt(input_xml, get_resource('epub/xsltTitle.xsl')) chars = used_chars(html_tree.getroot()) zip.writestr('OPS/title.html', etree.tostring(html_tree, method="html", pretty_print=True)) @@ -301,9 +296,9 @@ def transform(provider, slug=None, file_path=None, output_file=None, output_dir= # write title page for every parent if sample is not None and sample <= 0: chars = set() - html_string = open(res('emptyChunk.html')).read() + html_string = open(get_resource('epub/emptyChunk.html')).read() else: - html_tree = xslt(input_xml, res('xsltChunkTitle.xsl')) + html_tree = xslt(input_xml, get_resource('epub/xsltChunkTitle.xsl')) chars = used_chars(html_tree.getroot()) html_string = etree.tostring(html_tree, method="html", pretty_print=True) zip.writestr('OPS/part%d.html' % chunk_counter, html_string) @@ -391,10 +386,10 @@ def transform(provider, slug=None, file_path=None, output_file=None, output_dir= '' \ '') - for fname in 'style.css', 'logo_wolnelektury.png': - zip.write(res(fname), os.path.join('OPS', fname)) + zip.write(get_resource('epub/style.css'), os.path.join('OPS', 'style.css')) + zip.write(get_resource('res/wl-logo-small.png'), os.path.join('OPS', 'logo_wolnelektury.png')) - opf = xslt(metadata, res('xsltContent.xsl')) + opf = xslt(metadata, get_resource('epub/xsltContent.xsl')) manifest = opf.find('.//' + OPFNS('manifest')) spine = opf.find('.//' + OPFNS('spine')) @@ -425,7 +420,7 @@ def transform(provider, slug=None, file_path=None, output_file=None, output_dir= spine.append(etree.fromstring( '')) replace_by_verse(annotations) - html_tree = xslt(annotations, res("xsltAnnotations.xsl")) + html_tree = xslt(annotations, get_resource('epub/xsltAnnotations.xsl')) chars = chars.union(used_chars(html_tree.getroot())) zip.writestr('OPS/annotations.html', etree.tostring( html_tree, method="html", pretty_print=True)) @@ -436,7 +431,8 @@ def transform(provider, slug=None, file_path=None, output_file=None, output_dir= os.chdir(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'font-optimizer')) for fname in 'DejaVuSerif.ttf', 'DejaVuSerif-Bold.ttf', 'DejaVuSerif-Italic.ttf', 'DejaVuSerif-BoldItalic.ttf': - optimizer_call = ['perl', 'subset.pl', '--chars', ''.join(chars).encode('utf-8'), res('../fonts/' + fname), os.path.join(tmpdir, fname)] + optimizer_call = ['perl', 'subset.pl', '--chars', ''.join(chars).encode('utf-8'), + get_resource('fonts/' + fname), os.path.join(tmpdir, fname)] if verbose: print "Running font-optimizer" subprocess.check_call(optimizer_call) diff --git a/librarian/pdf/JunicodeWL-Italic.ttf b/librarian/fonts/JunicodeWL-Italic.ttf similarity index 100% rename from librarian/pdf/JunicodeWL-Italic.ttf rename to librarian/fonts/JunicodeWL-Italic.ttf diff --git a/librarian/pdf/JunicodeWL-Regular.ttf b/librarian/fonts/JunicodeWL-Regular.ttf similarity index 100% rename from librarian/pdf/JunicodeWL-Regular.ttf rename to librarian/fonts/JunicodeWL-Regular.ttf diff --git a/librarian/pdf.py b/librarian/pdf.py index 1989239..baf7be4 100644 --- a/librarian/pdf.py +++ b/librarian/pdf.py @@ -32,7 +32,7 @@ functions.reg_ends_white() functions.reg_texcommand() STYLESHEETS = { - 'wl2tex': 'xslt/wl2tex.xslt', + 'wl2tex': 'pdf/wl2tex.xslt', } @@ -224,7 +224,7 @@ def transform(provider, slug=None, file_path=None, # LaTeX -> PDF shutil.copy(get_resource('pdf/wl.sty'), temp) - shutil.copy(get_resource('pdf/wl-logo.png'), temp) + shutil.copy(get_resource('res/wl-logo.png'), temp) cwd = os.getcwd() os.chdir(temp) diff --git a/librarian/xslt/wl2tex.xslt b/librarian/pdf/wl2tex.xslt similarity index 100% rename from librarian/xslt/wl2tex.xslt rename to librarian/pdf/wl2tex.xslt diff --git a/librarian/epub/logo_wolnelektury.png b/librarian/res/wl-logo-small.png similarity index 100% rename from librarian/epub/logo_wolnelektury.png rename to librarian/res/wl-logo-small.png diff --git a/librarian/pdf/wl-logo.png b/librarian/res/wl-logo.png similarity index 100% rename from librarian/pdf/wl-logo.png rename to librarian/res/wl-logo.png diff --git a/scripts/book2partner b/scripts/book2partner index 5866cc3..e33ae13 100755 --- a/scripts/book2partner +++ b/scripts/book2partner @@ -14,7 +14,7 @@ from librarian.dcparser import BookInfo def utf_trunc(text, limit): -""" truncates text to at most `limit' bytes in utf-8 """ + """ truncates text to at most `limit' bytes in utf-8 """ if text is None: return text orig_text = text @@ -66,7 +66,7 @@ def virtualo(filenames, output_dir, verbose): product_elem[4][0][1].text = utf_trunc(info.author.last_name, 100) xml.append(product_elem) - cover.cover(600, 730, + cover.virtualo_cover( u' '.join(info.author.first_names + (info.author.last_name,)), info.title ).save(os.path.join(outfile_dir, slug+'.jpg')) diff --git a/setup.py b/setup.py index 98488dd..017cc57 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,7 @@ setup( maintainer_email='radek.czajka@gmail.com', url='http://github.com/fnp/librarian', packages=['librarian'], - package_data={'librarian': ['xslt/*.xslt', 'epub/*', 'pdf/*', 'fonts/*'] + + package_data={'librarian': ['xslt/*.xslt', 'epub/*', 'pdf/*', 'fonts/*', 'res/*'] + whole_tree(os.path.join(os.path.dirname(__file__), 'librarian'), 'font-optimizer')}, include_package_data=True, install_requires=['lxml>=2.2'], -- 2.20.1