2007-09-13 20:01:46 +00:00
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2008-03-09 03:33:11 +00:00
|
|
|
# Copyright (C) 2007 Zsolt Foldvari
|
|
|
|
# Copyright (C) 2008 Brian G. Matherly
|
2007-09-13 20:01:46 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
|
|
|
# $Id$
|
|
|
|
|
2007-09-28 22:32:00 +00:00
|
|
|
"""PDF output generator based on Cairo.
|
2007-09-13 20:01:46 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Python modules
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
2010-01-18 04:42:17 +00:00
|
|
|
from gen.ggettext import gettext as _
|
2010-10-09 12:33:47 +00:00
|
|
|
import sys
|
2007-09-13 20:01:46 +00:00
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Gramps modules
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
2009-03-19 12:08:52 +00:00
|
|
|
import libcairodoc
|
2007-09-13 20:01:46 +00:00
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Set up logging
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
import logging
|
2007-09-28 22:32:00 +00:00
|
|
|
LOG = logging.getLogger(".PdfDoc")
|
2007-09-13 20:01:46 +00:00
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GTK modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2007-09-28 22:32:00 +00:00
|
|
|
import pango
|
|
|
|
import cairo
|
|
|
|
import pangocairo
|
2007-09-13 20:01:46 +00:00
|
|
|
|
2007-09-28 22:32:00 +00:00
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Constants
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# resolution
|
|
|
|
DPI = 72.0
|
2007-09-13 20:01:46 +00:00
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# PdfDoc class
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
2009-03-19 12:08:52 +00:00
|
|
|
class PdfDoc(libcairodoc.CairoDoc):
|
2007-09-28 22:32:00 +00:00
|
|
|
"""Render the document into PDF file using Cairo.
|
2007-09-13 20:01:46 +00:00
|
|
|
"""
|
|
|
|
def run(self):
|
2007-09-28 22:32:00 +00:00
|
|
|
"""Create the PDF output.
|
2007-09-13 20:01:46 +00:00
|
|
|
"""
|
2007-09-28 22:32:00 +00:00
|
|
|
# get paper dimensions
|
|
|
|
paper_width = self.paper.get_size().get_width() * DPI / 2.54
|
|
|
|
paper_height = self.paper.get_size().get_height() * DPI / 2.54
|
|
|
|
page_width = round(self.paper.get_usable_width() * DPI / 2.54)
|
|
|
|
page_height = round(self.paper.get_usable_height() * DPI / 2.54)
|
|
|
|
left_margin = self.paper.get_left_margin() * DPI / 2.54
|
|
|
|
top_margin = self.paper.get_top_margin() * DPI / 2.54
|
|
|
|
|
|
|
|
# create cairo context and pango layout
|
2010-10-09 12:33:47 +00:00
|
|
|
filename = self._backend.filename.encode(sys.getfilesystemencoding())
|
|
|
|
surface = cairo.PDFSurface(filename, paper_width, paper_height)
|
2007-09-28 22:32:00 +00:00
|
|
|
surface.set_fallback_resolution(300, 300)
|
|
|
|
cr = pangocairo.CairoContext(cairo.Context(surface))
|
|
|
|
|
|
|
|
fontmap = pangocairo.cairo_font_map_get_default()
|
2008-03-09 03:33:11 +00:00
|
|
|
saved_resolution = fontmap.get_resolution()
|
2007-09-28 22:32:00 +00:00
|
|
|
fontmap.set_resolution(DPI)
|
2007-09-13 20:01:46 +00:00
|
|
|
|
2007-09-28 22:32:00 +00:00
|
|
|
pango_context = fontmap.create_context()
|
|
|
|
options = cairo.FontOptions()
|
|
|
|
options.set_hint_metrics(cairo.HINT_METRICS_OFF)
|
|
|
|
pangocairo.context_set_font_options(pango_context, options)
|
|
|
|
layout = pango.Layout(pango_context)
|
|
|
|
cr.update_context(pango_context)
|
2007-09-13 20:01:46 +00:00
|
|
|
|
2007-09-28 22:32:00 +00:00
|
|
|
# paginate the document
|
|
|
|
finished = self.paginate(layout, page_width, page_height, DPI, DPI)
|
|
|
|
while not finished:
|
|
|
|
finished = self.paginate(layout, page_width, page_height, DPI, DPI)
|
|
|
|
|
|
|
|
# render the pages
|
|
|
|
for page_nr in range(len(self._pages)):
|
|
|
|
cr.save()
|
|
|
|
cr.translate(left_margin, top_margin)
|
|
|
|
self.draw_page(page_nr, cr, layout,
|
|
|
|
page_width, page_height,
|
|
|
|
DPI, DPI)
|
|
|
|
cr.show_page()
|
|
|
|
cr.restore()
|
|
|
|
|
|
|
|
# close the surface (file)
|
|
|
|
surface.finish()
|
2008-03-09 03:33:11 +00:00
|
|
|
|
|
|
|
# Restore the resolution. On windows, Gramps UI fonts will be smaller
|
|
|
|
# if we don't restore the resolution.
|
|
|
|
fontmap.set_resolution(saved_resolution)
|
2007-09-28 22:32:00 +00:00
|
|
|
|