From: Radek Czajka Date: Tue, 9 Aug 2011 11:22:23 +0000 (+0200) Subject: arta tech X-Git-Url: https://git.mdrn.pl/librarian.git/commitdiff_plain/6cf1239c51e07bd17a4fd45269f583d0fc3f92c8?ds=sidebyside arta tech --- diff --git a/librarian/cover.py b/librarian/cover.py index 9070344..ddc85ca 100644 --- a/librarian/cover.py +++ b/librarian/cover.py @@ -13,6 +13,7 @@ class Cover(object): background_color = '#fff' background_img = None + author_align = 'c' author_top = 100 author_margin_left = 20 author_margin_right = 20 @@ -20,7 +21,9 @@ class Cover(object): author_color = '#000' author_shadow = None author_font = None + author_wrap = True + title_align = 'c' title_top = 100 title_margin_left = 20 title_margin_right = 20 @@ -28,6 +31,7 @@ class Cover(object): title_color = '#000' title_shadow = None title_font = None + title_wrap = True logo_bottom = None logo_width = None @@ -45,27 +49,60 @@ class Cover(object): 'PNG': 'image/png', } + @staticmethod + def person_shortener(text): + yield text + chunks = text.split() + n_chunks = len(chunks) + # make initials from given names, starting from last + for i in range(n_chunks - 2, -1, -1): + chunks[i] = chunks[i][0] + '.' + yield " ".join(chunks) + # remove given names initials, starting from last + while len(chunks) > 2: + del chunks[1] + yield " ".join(chunks) + + @staticmethod + def title_shortener(text): + yield text + chunks = text.split() + n_chunks = len(chunks) + # remove words, starting from last one + while len(chunks) > 1: + del chunks[-1] + yield " ".join(chunks) + u'…' @staticmethod - def draw_centered_text(text, img, font, margin_left, width, pos_y, lineskip, color, shadow_color): + def draw_text(text, img, font, align, shortener, 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 text_draw.textsize(line, font=font)[0] > width: - try: - line, ext = line.rsplit(' ', 1) - except: - break - pos_x = margin_left + (width - text_draw.textsize(line, font=font)[0]) / 2 + if shortener: + for line in shortener(text): + if text_draw.textsize(line, font=font)[0] <= width: + break + text = '' + else: + line = text + while text_draw.textsize(line, font=font)[0] > width: + try: + line, ext = line.rsplit(' ', 1) + except: + break + text = text[len(line)+1:] + pos_x = margin_left + if align == 'c': + pos_x += (width - text_draw.textsize(line, font=font)[0]) / 2 + elif align == 'r': + pos_x += (width - text_draw.textsize(line, font=font)[0]) 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) @@ -88,7 +125,10 @@ class Cover(object): if self.background_img: background = Image.open(self.background_img) - img.paste(background, None, background) + try: + img.paste(background, None, background) + except ValueError, e: + img.paste(background) del background # WL logo @@ -98,11 +138,13 @@ class Cover(object): img.paste(logo, ((self.width - self.logo_width) / 2, img.size[1] - logo.size[1] - self.logo_bottom)) author_font = self.author_font or ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), 30) - title_y = self.draw_centered_text(self.pretty_author(), img, author_font, + author_shortener = None if self.author_wrap else self.person_shortener + title_y = self.draw_text(self.pretty_author(), img, author_font, self.author_align, author_shortener, self.author_margin_left, self.width - self.author_margin_left - self.author_margin_right, self.author_top, self.author_lineskip, self.author_color, self.author_shadow) + self.title_top + title_shortener = None if self.title_wrap else self.title_shortener title_font = self.title_font or ImageFont.truetype(get_resource('fonts/DejaVuSerif.ttf'), 40) - self.draw_centered_text(self.pretty_title(), img, title_font, + self.draw_text(self.pretty_title(), img, title_font, self.title_align, title_shortener, self.title_margin_left, self.width - self.title_margin_left - self.title_margin_right, title_y, self.title_lineskip, self.title_color, self.title_shadow) @@ -184,3 +226,42 @@ class GandalfCover(Cover): logo_bottom = 25 logo_width = 250 format = 'PNG' + + +class ArtaTechCover(Cover): + width = 600 + height = 800 + background_img = get_resource('res/cover-arta-tech.jpg') + author_top = 132 + author_margin_left = 235 + author_margin_right = 23 + author_align = 'r' + author_font = ImageFont.truetype(get_resource('fonts/DroidSans.ttf'), 32) + author_color = '#555555' + author_wrap = False + title_top = 17 + title_margin_right = 21 + title_margin_left = 60 + title_align = 'r' + title_font = ImageFont.truetype(get_resource('fonts/EBGaramond-Regular.ttf'), 42) + title_color = '#222222' + title_wrap = False + format = 'PNG' + + def pretty_author(self): + return self.author.upper() + + +def ImageCover(img): + """ a class factory for simple image covers """ + img = Image.open(img) + + class ImgCover(Cover): + def image(self): + return img + + @property + def format(self): + return self.image().format + + return ImgCover diff --git a/librarian/epub.py b/librarian/epub.py index 0f23923..348df0c 100644 --- a/librarian/epub.py +++ b/librarian/epub.py @@ -5,6 +5,7 @@ # from __future__ import with_statement +from copy import deepcopy import os import os.path import subprocess @@ -19,6 +20,7 @@ import sys from librarian import XMLNamespace, RDFNS, DCNS, WLNS, NCXNS, OPFNS, XHTMLNS, NoDublinCore from librarian.dcparser import BookInfo +from librarian.cover import ImageCover from librarian import functions, get_resource @@ -277,7 +279,7 @@ def transform(provider, slug=None, file_path=None, output_file=None, output_dir= make_dir: writes output to //.epub instead of /.epub sample=n: generate sample e-book (with at least n paragraphs) cover: a cover.Cover object - flags: less-advertising, + flags: less-advertising, images, not-wl """ def transform_file(input_xml, chunk_counter=1, first=True, sample=None): @@ -382,6 +384,10 @@ def transform(provider, slug=None, file_path=None, output_file=None, output_dir= else: output_file = open(os.path.join(output_dir, os.path.splitext(os.path.basename(file_path))[0] + '.epub'), 'w') + opf = xslt(metadata, get_resource('epub/xsltContent.xsl')) + manifest = opf.find('.//' + OPFNS('manifest')) + spine = opf.find('.//' + OPFNS('spine')) + zip = zipfile.ZipFile(output_file, 'w', zipfile.ZIP_DEFLATED) # write static elements @@ -396,11 +402,10 @@ def transform(provider, slug=None, file_path=None, output_file=None, output_dir= 'media-type="application/oebps-package+xml" />' \ '') 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, get_resource('epub/xsltContent.xsl')) - manifest = opf.find('.//' + OPFNS('manifest')) - spine = opf.find('.//' + OPFNS('spine')) + if not flags or 'not-wl' not in flags: + manifest.append(etree.fromstring( + '')) + zip.write(get_resource('res/wl-logo-small.png'), os.path.join('OPS', 'logo_wolnelektury.png')) if cover: cover_file = StringIO() @@ -423,6 +428,26 @@ def transform(provider, slug=None, file_path=None, output_file=None, output_dir= opf.getroot()[0].append(etree.fromstring('')) opf.getroot().append(etree.fromstring('')) + if flags and 'images' in flags: + for ilustr in input_xml.findall('//ilustr'): + src = ilustr.get('src') + mime = ImageCover(src)().mime_type() + zip.write(src, os.path.join('OPS', src)) + manifest.append(etree.fromstring( + '' % (src, src, mime))) + # get it up to master + after = ilustr + while after.getparent().tag not in ['powiesc', 'opowiadanie', 'liryka_l', 'liryka_lp', 'dramat_wierszowany_l', 'dramat_wierszowany_lp', 'dramat_wspolczesny']: + after = after.getparent() + if not(after is ilustr): + moved = deepcopy(ilustr) + ilustr.tag = 'extra' + ilustr.text = None + moved.tail = None + after.addnext(moved) + else: + for ilustr in input_xml.findall('//ilustr'): + ilustr.tag = 'extra' annotations = etree.Element('annotations') @@ -464,7 +489,13 @@ def transform(provider, slug=None, file_path=None, output_file=None, output_dir= '')) spine.append(etree.fromstring( '')) - html_tree = xslt(input_xml, get_resource('epub/xsltLast.xsl')) + stopka = input_xml.find('//stopka') + if stopka is not None: + stopka.tag = 'stopka_' + replace_by_verse(stopka) + html_tree = xslt(stopka, get_resource('epub/xsltScheme.xsl')) + else: + html_tree = xslt(input_xml, get_resource('epub/xsltLast.xsl')) chars.update(used_chars(html_tree.getroot())) zip.writestr('OPS/last.html', etree.tostring( html_tree, method="html", pretty_print=True)) diff --git a/librarian/epub/style.css b/librarian/epub/style.css index 249ae09..7fb53a3 100644 --- a/librarian/epub/style.css +++ b/librarian/epub/style.css @@ -46,7 +46,8 @@ a img { #book-text { - margin: 2em; + /*margin: 2em;*/ + margin: 5px; /*margin-right: 9em;*/ } @@ -370,3 +371,12 @@ p.minor { p.footer { margin-top: 2em; } + +.ilustr { + margin-top: 1em; + margin-bottom: 1em; +} + +.ilustr img { + max-width: 100%; +} diff --git a/librarian/epub/xsltContent.xsl b/librarian/epub/xsltContent.xsl index 27a317f..65bf808 100644 --- a/librarian/epub/xsltContent.xsl +++ b/librarian/epub/xsltContent.xsl @@ -29,7 +29,6 @@ - @@ -47,4 +46,4 @@ - \ No newline at end of file + diff --git a/librarian/epub/xsltScheme.xsl b/librarian/epub/xsltScheme.xsl index 7e866fd..c9fcae5 100644 --- a/librarian/epub/xsltScheme.xsl +++ b/librarian/epub/xsltScheme.xsl @@ -278,6 +278,18 @@
+ +
+ ilustracja + + + + +
+
+ + + @@ -314,4 +326,4 @@ - \ No newline at end of file + diff --git a/librarian/epub/xsltTitle.xsl b/librarian/epub/xsltTitle.xsl index 544b4b9..1b211e0 100644 --- a/librarian/epub/xsltTitle.xsl +++ b/librarian/epub/xsltTitle.xsl @@ -36,6 +36,7 @@ +

@@ -55,6 +56,12 @@

+
+ @@ -132,4 +139,4 @@ - \ No newline at end of file + diff --git a/librarian/fonts/DroidSans.ttf b/librarian/fonts/DroidSans.ttf new file mode 100644 index 0000000..2537cc3 Binary files /dev/null and b/librarian/fonts/DroidSans.ttf differ diff --git a/librarian/fonts/EBGaramond-Regular.ttf b/librarian/fonts/EBGaramond-Regular.ttf new file mode 100644 index 0000000..dde4869 Binary files /dev/null and b/librarian/fonts/EBGaramond-Regular.ttf differ diff --git a/librarian/packagers.py b/librarian/packagers.py index 054f068..2c543da 100644 --- a/librarian/packagers.py +++ b/librarian/packagers.py @@ -60,6 +60,12 @@ class GandalfEpubPackager(EpubPackager): class GandalfPdfPackager(PdfPackager): cover = cover.GandalfCover +class ArtaTechEpubPackager(EpubPackager): + cover = cover.ArtaTechCover + +class ArtaTechPdfPackager(PdfPackager): + cover = cover.ArtaTechCover + class BookotekaEpubPackager(EpubPackager): cover = cover.BookotekaCover diff --git a/librarian/res/cover-arta-tech.jpg b/librarian/res/cover-arta-tech.jpg new file mode 100644 index 0000000..b69f469 Binary files /dev/null and b/librarian/res/cover-arta-tech.jpg differ diff --git a/scripts/book2epub b/scripts/book2epub index 9adf4b4..82aaa2b 100755 --- a/scripts/book2epub +++ b/scripts/book2epub @@ -8,6 +8,7 @@ import os.path import optparse from librarian import epub, DirDocProvider, ParseError +from librarian.cover import ImageCover if __name__ == '__main__': @@ -25,6 +26,14 @@ if __name__ == '__main__': help='specifies the output file') parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR', help='specifies the directory for output') + parser.add_option('-i', '--with-images', action='store_true', dest='images', default=False, + help='add images with ') + parser.add_option('-A', '--less-advertising', action='store_true', dest='less_advertising', default=False, + help='less advertising, for commercial purposes') + parser.add_option('-W', '--not-wl', action='store_true', dest='not_wl', default=False, + help='not a WolneLektury book') + parser.add_option('-c', '--cover', dest='cover', metavar='FILE', + help='specifies the cover file') options, input_filenames = parser.parse_args() @@ -48,7 +57,20 @@ if __name__ == '__main__': else: output_dir = path - epub.transform(provider, file_path=main_input, output_dir=output_dir, output_file=output_file, make_dir=options.make_dir) + cover = None + if options.cover: + cover = ImageCover(options.cover) + + flags = [] + if options.images: + flags.append('images') + if options.less_advertising: + flags.append('less-advertising') + if options.not_wl: + flags.append('not-wl') + + epub.transform(provider, file_path=main_input, output_dir=output_dir, output_file=output_file, make_dir=options.make_dir, + cover=cover, flags=flags) except ParseError, e: print '%(file)s:%(name)s:%(message)s' % { 'file': main_input, diff --git a/scripts/book2partner b/scripts/book2partner index 0488273..dcc8919 100755 --- a/scripts/book2partner +++ b/scripts/book2partner @@ -26,6 +26,10 @@ if __name__ == '__main__': help='prepare EPUB files for Gandalf') parser.add_option('--gandalf-pdf', action='store_true', dest='gandalf_pdf', default=False, help='prepare PDF files for Gandalf') + parser.add_option('--artatech', action='store_true', dest='artatech', default=False, + help='prepare EPUB files for Arta-Tech') + parser.add_option('--artatech-pdf', action='store_true', dest='artatech_pdf', default=False, + help='prepare PDF files for Arta-Tech') parser.add_option('--virtualo', action='store_true', dest='virtualo', default=False, help='prepare files for Virtualo API') parser.add_option('--prestigio', action='store_true', dest='prestigio', default=False, @@ -45,6 +49,10 @@ if __name__ == '__main__': packagers.GandalfEpubPackager.prepare(input_filenames, options.output_dir, options.verbose) if options.gandalf_pdf: packagers.GandalfPdfPackager.prepare(input_filenames, options.output_dir, options.verbose) + if options.artatech: + packagers.ArtaTechEpubPackager.prepare(input_filenames, options.output_dir, options.verbose) + if options.artatech_pdf: + packagers.ArtaTechPdfPackager.prepare(input_filenames, options.output_dir, options.verbose) if options.virtualo: packagers.VirtualoEpubPackager.prepare(input_filenames, options.output_dir, options.verbose) if options.prestigio: