Ejemplo n.º 1
0
def polish(file_map, opts, log, report):
    rt = lambda x: report('\n### ' + x)
    st = time.time()
    for inbook, outbook in file_map.iteritems():
        report(_('## Polishing: %s') % (inbook.rpartition('.')[-1].upper()))
        ebook = get_container(inbook, log)
        jacket = None

        if opts.subset:
            stats = StatsCollector(ebook)

        if opts.opf:
            rt(_('Updating metadata'))
            update_metadata(ebook, opts.opf)
            jacket = find_existing_jacket(ebook)
            if jacket is not None:
                replace_jacket(ebook, jacket)
                report(_('Updated metadata jacket'))
            report(_('Metadata updated\n'))

        if opts.cover:
            rt(_('Setting cover'))
            set_cover(ebook, opts.cover, report)
            report('')

        if opts.jacket:
            rt(_('Inserting metadata jacket'))
            if jacket is None:
                if add_or_replace_jacket(ebook):
                    report(_('Existing metadata jacket replaced'))
                else:
                    report(_('Metadata jacket inserted'))
            else:
                report(_('Existing metadata jacket replaced'))
            report('')

        if opts.remove_jacket:
            rt(_('Removing metadata jacket'))
            if remove_jacket(ebook):
                report(_('Metadata jacket removed'))
            else:
                report(_('No metadata jacket found'))
            report('')

        if opts.smarten_punctuation:
            rt(_('Smartening punctuation'))
            smarten_punctuation(ebook, report)
            report('')

        if opts.subset:
            rt(_('Subsetting embedded fonts'))
            subset_all_fonts(ebook, stats.font_stats, report)
            report('')

        ebook.commit(outbook)
        report('-' * 70)
    report(_('Polishing took: %.1f seconds') % (time.time() - st))
Ejemplo n.º 2
0
def polish_one(ebook, opts, report):
    rt = lambda x: report('\n### ' + x)
    jacket = None

    if opts.subset or opts.embed:
        stats = StatsCollector(ebook, do_embed=opts.embed)

    if opts.opf:
        rt(_('Updating metadata'))
        update_metadata(ebook, opts.opf)
        jacket = find_existing_jacket(ebook)
        if jacket is not None:
            replace_jacket(ebook, jacket)
            report(_('Updated metadata jacket'))
        report(_('Metadata updated\n'))

    if opts.cover:
        rt(_('Setting cover'))
        set_cover(ebook, opts.cover, report)
        report('')

    if opts.jacket:
        rt(_('Inserting metadata jacket'))
        if jacket is None:
            if add_or_replace_jacket(ebook):
                report(_('Existing metadata jacket replaced'))
            else:
                report(_('Metadata jacket inserted'))
        else:
            report(_('Existing metadata jacket replaced'))
        report('')

    if opts.remove_jacket:
        rt(_('Removing metadata jacket'))
        if remove_jacket(ebook):
            report(_('Metadata jacket removed'))
        else:
            report(_('No metadata jacket found'))
        report('')

    if opts.smarten_punctuation:
        rt(_('Smartening punctuation'))
        smarten_punctuation(ebook, report)
        report('')

    if opts.embed:
        rt(_('Embedding referenced fonts'))
        embed_all_fonts(ebook, stats, report)
        report('')

    if opts.subset:
        rt(_('Subsetting embedded fonts'))
        subset_all_fonts(ebook, stats.font_stats, report)
        report('')
Ejemplo n.º 3
0
 def get_stats(html, *fonts):
     styles = []
     html = '<html><head><link href="styles.css"></head><body>{}</body></html>'.format(html)
     files = {'index.html':html, 'X.otf':b'xxx', 'XB.otf': b'xbxb'}
     for font in fonts:
         styles.append('@font-face {')
         for k, v in font.iteritems():
             if k == 'src':
                 files[v] = b'xxx'
                 v = 'url(%s)' % v
             styles.append('%s : %s;' % (k, v))
         styles.append('}\n')
     html = '<html><head><link href="styles.css"></head><body>{}</body></html>'.format(html)
     files['styles.css'] = embeds + '\n'.join(styles)
     c = VirtualContainer(files)
     return StatsCollector(c, do_embed=True)
Ejemplo n.º 4
0
def polish_one(ebook, opts, report, customization=None):
    rt = lambda x: report('\n### ' + x)
    jacket = None
    changed = False
    customization = customization or CUSTOMIZATION.copy()

    if opts.subset or opts.embed:
        stats = StatsCollector(ebook, do_embed=opts.embed)

    if opts.opf:
        changed = True
        rt(_('Updating metadata'))
        update_metadata(ebook, opts.opf)
        jacket = find_existing_jacket(ebook)
        if jacket is not None:
            replace_jacket(ebook, jacket)
            report(_('Updated metadata jacket'))
        report(_('Metadata updated\n'))

    if opts.cover:
        changed = True
        rt(_('Setting cover'))
        set_cover(ebook, opts.cover, report)
        report('')

    if opts.jacket:
        changed = True
        rt(_('Inserting metadata jacket'))
        if jacket is None:
            if add_or_replace_jacket(ebook):
                report(_('Existing metadata jacket replaced'))
            else:
                report(_('Metadata jacket inserted'))
        else:
            report(_('Existing metadata jacket replaced'))
        report('')

    if opts.remove_jacket:
        rt(_('Removing metadata jacket'))
        if remove_jacket(ebook):
            report(_('Metadata jacket removed'))
            changed = True
        else:
            report(_('No metadata jacket found'))
        report('')

    if opts.smarten_punctuation:
        rt(_('Smartening punctuation'))
        if smarten_punctuation(ebook, report):
            changed = True
        report('')

    if opts.embed:
        rt(_('Embedding referenced fonts'))
        if embed_all_fonts(ebook, stats, report):
            changed = True
        report('')

    if opts.subset:
        rt(_('Subsetting embedded fonts'))
        if subset_all_fonts(ebook, stats.font_stats, report):
            changed = True
        report('')

    if opts.remove_unused_css:
        rt(_('Removing unused CSS rules'))
        if remove_unused_css(
                ebook, report, remove_unused_classes=customization['remove_unused_classes'], merge_rules=customization['merge_identical_selectors']):
            changed = True
        report('')

    if opts.compress_images:
        rt(_('Losslessly compressing images'))
        if compress_images(ebook, report)[0]:
            changed = True
        report('')

    return changed
Ejemplo n.º 5
0
                        sheet = container.parse_css(style.text, name)
                        if remove_font_face_rules(container, sheet, remove, name):
                            style.text = sheet.cssText
                            container.dirty(name)
    if total_old > 0:
        report(_('Reduced total font size to %.1f%% of original')%(
            total_new/total_old*100))
    else:
        report(_('No embedded fonts found'))
    return changed


if __name__ == '__main__':
    from calibre.ebooks.oeb.polish.container import get_container
    from calibre.ebooks.oeb.polish.stats import StatsCollector
    from calibre.utils.logging import default_log
    default_log.filter_level = default_log.DEBUG
    inbook = sys.argv[-1]
    ebook = get_container(inbook, default_log)
    report = []
    stats = StatsCollector(ebook).font_stats
    subset_all_fonts(ebook, stats, report.append)
    outbook, ext = inbook.rpartition('.')[0::2]
    outbook += '_subset.'+ext
    ebook.commit(outbook)
    prints('\nReport:')
    for msg in report:
        prints(msg)
    print()
    prints('Output written to:', outbook)
Ejemplo n.º 6
0
def polish_one(ebook, opts, report, customization=None):
    rt = lambda x: report('\n### ' + x)
    jacket = None
    changed = False
    customization = customization or CUSTOMIZATION.copy()
    has_subsettable_fonts = False
    for x in iter_subsettable_fonts(ebook):
        has_subsettable_fonts = True
        break

    if (opts.subset and has_subsettable_fonts) or opts.embed:
        stats = StatsCollector(ebook, do_embed=opts.embed)

    if opts.opf:
        changed = True
        rt(_('Updating metadata'))
        update_metadata(ebook, opts.opf)
        jacket = find_existing_jacket(ebook)
        if jacket is not None:
            replace_jacket(ebook, jacket)
            report(_('Updated metadata jacket'))
        report(_('Metadata updated\n'))

    if opts.cover:
        changed = True
        rt(_('Setting cover'))
        set_cover(ebook, opts.cover, report)
        report('')

    if opts.jacket:
        changed = True
        rt(_('Inserting metadata jacket'))
        if jacket is None:
            if add_or_replace_jacket(ebook):
                report(_('Existing metadata jacket replaced'))
            else:
                report(_('Metadata jacket inserted'))
        else:
            report(_('Existing metadata jacket replaced'))
        report('')

    if opts.remove_jacket:
        rt(_('Removing metadata jacket'))
        if remove_jacket(ebook):
            report(_('Metadata jacket removed'))
            changed = True
        else:
            report(_('No metadata jacket found'))
        report('')

    if opts.smarten_punctuation:
        rt(_('Smartening punctuation'))
        if smarten_punctuation(ebook, report):
            changed = True
        report('')

    if opts.embed:
        rt(_('Embedding referenced fonts'))
        if embed_all_fonts(ebook, stats, report):
            changed = True
            has_subsettable_fonts = True
        report('')

    if opts.subset:
        if has_subsettable_fonts:
            rt(_('Subsetting embedded fonts'))
            if subset_all_fonts(ebook, stats.font_stats, report):
                changed = True
        else:
            rt(_('No embedded fonts to subset'))
        report('')

    if opts.remove_unused_css:
        rt(_('Removing unused CSS rules'))
        if remove_unused_css(
                ebook,
                report,
                remove_unused_classes=customization['remove_unused_classes'],
                merge_rules=customization['merge_identical_selectors'],
                merge_rules_with_identical_properties=customization[
                    'merge_rules_with_identical_properties'],
                remove_unreferenced_sheets=customization[
                    'remove_unreferenced_sheets']):
            changed = True
        report('')

    if opts.compress_images:
        rt(_('Losslessly compressing images'))
        if compress_images(ebook, report)[0]:
            changed = True
        report('')

    if opts.upgrade_book:
        rt(_('Upgrading book, if possible'))
        if upgrade_book(ebook, report, remove_ncx=customization['remove_ncx']):
            changed = True
        report('')

    if opts.remove_soft_hyphens:
        rt(_('Removing soft hyphens'))
        remove_soft_hyphens(ebook, report)
        changed = True
    elif opts.add_soft_hyphens:
        rt(_('Adding soft hyphens'))
        add_soft_hyphens(ebook, report)
        changed = True

    return changed
Ejemplo n.º 7
0
        root = container.parsed(spine_name)
        head = root.xpath('//*[local-name()="head"][1]')[0]
        href = container.name_to_href(name, spine_name)
        etree.SubElement(head,
                         XHTML('link'),
                         rel='stylesheet',
                         type='text/css',
                         href=href).tail = '\n'
        container.dirty(spine_name)
    return True


if __name__ == '__main__':
    from calibre.ebooks.oeb.polish.container import get_container
    from calibre.ebooks.oeb.polish.stats import StatsCollector
    from calibre.utils.logging import default_log
    default_log.filter_level = default_log.DEBUG
    inbook = sys.argv[-1]
    ebook = get_container(inbook, default_log)
    report = []
    stats = StatsCollector(ebook, do_embed=True)
    embed_all_fonts(ebook, stats, report.append)
    outbook, ext = inbook.rpartition('.')[0::2]
    outbook += '_subset.' + ext
    ebook.commit(outbook)
    prints('\nReport:')
    for msg in report:
        prints(msg)
    print()
    prints('Output written to:', outbook)