Beispiel #1
0
def create_cover_page(top_lines,
                      logo_path,
                      width=590,
                      height=750,
                      bgcolor='#ffffff',
                      output_format='jpg',
                      texture_data=None,
                      texture_opacity=1.0):
    '''
    Create the standard calibre cover page and return it as a byte string in
    the specified output_format.
    '''
    canvas = create_canvas(width, height, bgcolor)
    if texture_data and hasattr(canvas, 'texture'):
        texture = Image()
        texture.load(texture_data)
        texture.set_opacity(texture_opacity)
        canvas.texture(texture)

    bottom = 10
    for line in top_lines:
        twand = create_text_wand(line.font_size, font_path=line.font_path)
        bottom = draw_centered_text(canvas, twand, line.text, bottom)
        bottom += line.bottom_margin
    bottom -= top_lines[-1].bottom_margin

    foot_font = tweaks['generate_cover_foot_font']
    if not foot_font:
        foot_font = P('fonts/liberation/LiberationMono-Regular.ttf')
    vanity = create_text_arc(__appname__ + ' ' + __version__,
                             24,
                             font=foot_font,
                             bgcolor='#00000000')
    lwidth, lheight = vanity.size
    left = int(max(0, (width - lwidth) / 2.))
    top = height - lheight - 10
    canvas.compose(vanity, left, top)

    available = (width, int(top - bottom) - 20)
    if available[1] > 40:
        logo = Image()
        logo.open(logo_path)
        lwidth, lheight = logo.size
        scaled, lwidth, lheight = fit_image(lwidth, lheight, *available)
        if scaled:
            logo.size = (lwidth, lheight)
        left = int(max(0, (width - lwidth) / 2.))
        top = bottom + 10
        extra = int((available[1] - lheight) / 2.0)
        if extra > 0:
            top += extra
        canvas.compose(logo, left, top)

    return canvas.export(output_format)
Beispiel #2
0
def create_cover_page(
    top_lines,
    logo_path,
    width=590,
    height=750,
    bgcolor="#ffffff",
    output_format="jpg",
    texture_data=None,
    texture_opacity=1.0,
):
    """
    Create the standard calibre cover page and return it as a byte string in
    the specified output_format.
    """
    canvas = create_canvas(width, height, bgcolor)
    if texture_data and hasattr(canvas, "texture"):
        texture = Image()
        texture.load(texture_data)
        texture.set_opacity(texture_opacity)
        canvas.texture(texture)

    bottom = 10
    for line in top_lines:
        twand = create_text_wand(line.font_size, font_path=line.font_path)
        bottom = draw_centered_text(canvas, twand, line.text, bottom)
        bottom += line.bottom_margin
    bottom -= top_lines[-1].bottom_margin

    foot_font = P("fonts/liberation/LiberationMono-Regular.ttf")
    vanity = create_text_arc(__appname__ + " " + __version__, 24, font=foot_font, bgcolor="#00000000")
    lwidth, lheight = vanity.size
    left = int(max(0, (width - lwidth) / 2.0))
    top = height - lheight - 10
    canvas.compose(vanity, left, top)

    available = (width, int(top - bottom) - 20)
    if available[1] > 40:
        logo = Image()
        logo.open(logo_path)
        lwidth, lheight = logo.size
        scaled, lwidth, lheight = fit_image(lwidth, lheight, *available)
        if scaled:
            logo.size = (lwidth, lheight)
        left = int(max(0, (width - lwidth) / 2.0))
        top = bottom + 10
        extra = int((available[1] - lheight) / 2.0)
        if extra > 0:
            top += extra
        canvas.compose(logo, left, top)

    return canvas.export(output_format)