Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate
1
# -*- coding: utf-8 -*-
2
#Copyright (c) 2011 Walter Bender
3
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# You should have received a copy of the GNU General Public License
10
# along with this library; if not, write to the Free Software
11
# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
12
13
14
import gi
15
gi.require_version('Gtk', '3.0')
16
from gi.repository import Gdk
17
from gi.repository import GdkPixbuf
18
from glib import GError
19
import os.path
20
import time
21
import cairo
22
from gi.repository import Pango
23
from gi.repository import PangoCairo
24
from gettext import gettext as _
25
26
from utils import get_pixbuf_from_journal
27
28
29
HEAD = 32
30
BODY = 12
31
PAGE_WIDTH = 504
32
PAGE_HEIGHT = 648
33
LEFT_MARGIN = 10
34
TOP_MARGIN = 20
35
36
37
def save_pdf(activity, nick, description=None):
38
    ''' Output a PDF document from the title, pictures, and descriptions '''
39
40
    if len(activity.dsobjects) == 0:
41
        return None
42
43
    tmp_file = os.path.join(activity.datapath, 'output.pdf')
44
    pdf_surface = cairo.PDFSurface(tmp_file, 504, 648)
45
46
    fd = Pango.FontDescription('Sans')
47
    cr = cairo.Context(pdf_surface)
48
    cr.set_source_rgb(0, 0, 0)
49
50
    show_text(cr, fd, nick, HEAD, LEFT_MARGIN, TOP_MARGIN)
51
    show_text(cr, fd, time.strftime('%x', time.localtime()),
52
              BODY, LEFT_MARGIN, TOP_MARGIN + 3 * HEAD)
53
    if description is not None:
54
        show_text(cr, fd, description,
55
                  BODY, LEFT_MARGIN, TOP_MARGIN + 4 * HEAD)
56
    cr.show_page()
57
58
    for i, dsobj in enumerate(activity.dsobjects):
59
        if dsobj.metadata['keep'] == '0':
60
            continue
61
        if 'title' in dsobj.metadata:
62
            show_text(cr, fd, dsobj.metadata['title'], HEAD, LEFT_MARGIN,
63
                      TOP_MARGIN)
64
        else:
65
            show_text(cr, fd, _('untitled'), HEAD, LEFT_MARGIN,
66
                      TOP_MARGIN)
67
68
        w = 0
69
        h = 0
70
        pixbuf = None
71
        if os.path.exists(dsobj.file_path):
72
            print dsobj.file_path
73
            try:
74
                w = int(PAGE_WIDTH - LEFT_MARGIN * 2)
75
                h = int(w * 3 / 4)
76
                pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
77
                    dsobj.file_path, w, h)
78
            except: # (GError, IOError):
79
                try:
80
                    w = 300
81
                    h = 225
82
                    pixbuf = get_pixbuf_from_journal(dsobj, w, h)
83
                except: # (GError, IOError):
84
                    pass
85
86
        if pixbuf is not None:
87
            cr.save()
88
            Gdk.cairo_set_source_pixbuf(
89
                cr, pixbuf, LEFT_MARGIN, TOP_MARGIN + 150)
90
            cr.rectangle(LEFT_MARGIN, TOP_MARGIN + 150, w, h)
91
            cr.fill()
92
            cr.restore()
93
94
        if 'description' in dsobj.metadata:
95
            show_text(cr, fd, dsobj.metadata['description'], BODY,
96
                      LEFT_MARGIN, h + 175)
97
        cr.show_page()
98
99
    return tmp_file
100
101
102
def show_text(cr, fd, label, size, x, y):
103
    pl = PangoCairo.create_layout(cr)
104
    fd.set_size(int(size * Pango.SCALE))
105
    pl.set_font_description(fd)
106
    if type(label) == str or type(label) == unicode:
107
        pl.set_text(label.replace('\0', ' '), -1)
108
    else:
109
        pl.set_text(str(label), -1)
110
    pl.set_width((PAGE_WIDTH - LEFT_MARGIN * 2) * Pango.SCALE)
111
    cr.save()
112
    cr.translate(x, y)
113
    PangoCairo.update_layout(cr, pl)
114
    PangoCairo.show_layout(cr, pl)
115
    cr.restore()