From 1c22993e00283d5679122126732badfc554bd769 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 3 Oct 2019 16:51:33 +0530 Subject: [PATCH] Move outline creation into its own file --- setup/extensions.json | 2 +- src/calibre/utils/podofo/doc.cpp | 48 +--------------------- src/calibre/utils/podofo/global.h | 1 + src/calibre/utils/podofo/outlines.cpp | 58 +++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 48 deletions(-) create mode 100644 src/calibre/utils/podofo/outlines.cpp diff --git a/setup/extensions.json b/setup/extensions.json index a484d04ddd..6df1445dda 100644 --- a/setup/extensions.json +++ b/setup/extensions.json @@ -117,7 +117,7 @@ }, { "name": "podofo", - "sources": "calibre/utils/podofo/utils.cpp calibre/utils/podofo/output.cpp calibre/utils/podofo/doc.cpp calibre/utils/podofo/outline.cpp calibre/utils/podofo/fonts.cpp calibre/utils/podofo/impose.cpp calibre/utils/podofo/images.cpp calibre/utils/podofo/podofo.cpp", + "sources": "calibre/utils/podofo/utils.cpp calibre/utils/podofo/output.cpp calibre/utils/podofo/doc.cpp calibre/utils/podofo/outline.cpp calibre/utils/podofo/fonts.cpp calibre/utils/podofo/impose.cpp calibre/utils/podofo/images.cpp calibre/utils/podofo/outlines.cpp calibre/utils/podofo/podofo.cpp", "headers": "calibre/utils/podofo/global.h", "libraries": "podofo", "lib_dirs": "!podofo_lib", diff --git a/src/calibre/utils/podofo/doc.cpp b/src/calibre/utils/podofo/doc.cpp index 0134b28d02..013b588cd0 100644 --- a/src/calibre/utils/podofo/doc.cpp +++ b/src/calibre/utils/podofo/doc.cpp @@ -286,52 +286,6 @@ PDFDoc_set_box(PDFDoc *self, PyObject *args) { Py_RETURN_NONE; } // }}} -// create_outline() {{{ -static PyObject * -PDFDoc_create_outline(PDFDoc *self, PyObject *args) { - PDFOutlineItem *ans; - PyObject *title_buf; - unsigned int pagenum; - double left = 0, top = 0, zoom = 0; - PdfPage *page; - - if (!PyArg_ParseTuple(args, "UI|ddd", &title_buf, &pagenum, &left, &top, &zoom)) return NULL; - - ans = PyObject_New(PDFOutlineItem, &PDFOutlineItemType); - if (ans == NULL) goto error; - - try { - PdfString title = podofo_convert_pystring(title_buf); - PdfOutlines *outlines = self->doc->GetOutlines(); - if (outlines == NULL) {PyErr_NoMemory(); goto error;} - ans->item = outlines->CreateRoot(title); - if (ans->item == NULL) {PyErr_NoMemory(); goto error;} - ans->doc = self->doc; - try { - page = self->doc->GetPage(pagenum - 1); - } catch (const PdfError &err) { - (void)err; - PyErr_Format(PyExc_ValueError, "Invalid page number: %u", pagenum - 1); goto error; - } - PdfDestination dest(page, left, top, zoom); - ans->item->SetDestination(dest); - } catch(const PdfError & err) { - podofo_set_exception(err); goto error; - } catch(const std::exception & err) { - PyErr_Format(PyExc_ValueError, "An error occurred while trying to create the outline: %s", err.what()); - goto error; - } catch (...) { - PyErr_SetString(PyExc_ValueError, "An unknown error occurred while trying to create the outline"); - goto error; - } - - return (PyObject*)ans; -error: - Py_XDECREF(ans); - return NULL; - -} // }}} - // get_xmp_metadata() {{{ static PyObject * PDFDoc_get_xmp_metadata(PDFDoc *self, PyObject *args) { @@ -795,7 +749,7 @@ static PyMethodDef PDFDoc_methods[] = { {"set_box", (PyCFunction)PDFDoc_set_box, METH_VARARGS, "set_box(page_num, box, left, bottom, width, height) -> Set the PDF bounding box for the page numbered nu, box must be one of: MediaBox, CropBox, TrimBox, BleedBox, ArtBox. The numbers are interpreted as pts." }, - {"create_outline", (PyCFunction)PDFDoc_create_outline, METH_VARARGS, + {"create_outline", (PyCFunction)py_create_outline, METH_VARARGS, "create_outline(title, pagenum) -> Create an outline, return the first outline item." }, {"get_xmp_metadata", (PyCFunction)PDFDoc_get_xmp_metadata, METH_VARARGS, diff --git a/src/calibre/utils/podofo/global.h b/src/calibre/utils/podofo/global.h index bbe04d9ab3..c5ff9be795 100644 --- a/src/calibre/utils/podofo/global.h +++ b/src/calibre/utils/podofo/global.h @@ -102,6 +102,7 @@ PyObject* py_merge_fonts(PDFDoc *self, PyObject *args); PyObject* py_dedup_type3_fonts(PDFDoc *self, PyObject *args); PyObject* py_impose(PDFDoc *self, PyObject *args); PyObject* py_dedup_images(PDFDoc *self, PyObject *args); +PyObject* py_create_outline(PDFDoc *self, PyObject *args); } } diff --git a/src/calibre/utils/podofo/outlines.cpp b/src/calibre/utils/podofo/outlines.cpp new file mode 100644 index 0000000000..be7cc7352d --- /dev/null +++ b/src/calibre/utils/podofo/outlines.cpp @@ -0,0 +1,58 @@ +/* + * outlines.cpp + * Copyright (C) 2019 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +#include "global.h" +using namespace pdf; + + +// create_outline() {{{ +static PyObject * +create_outline(PDFDoc *self, PyObject *args) { + PDFOutlineItem *ans; + PyObject *title_buf; + unsigned int pagenum; + double left = 0, top = 0, zoom = 0; + PdfPage *page; + + if (!PyArg_ParseTuple(args, "UI|ddd", &title_buf, &pagenum, &left, &top, &zoom)) return NULL; + + ans = PyObject_New(PDFOutlineItem, &PDFOutlineItemType); + if (ans == NULL) goto error; + + try { + PdfString title = podofo_convert_pystring(title_buf); + PdfOutlines *outlines = self->doc->GetOutlines(); + if (outlines == NULL) {PyErr_NoMemory(); goto error;} + ans->item = outlines->CreateRoot(title); + if (ans->item == NULL) {PyErr_NoMemory(); goto error;} + ans->doc = self->doc; + try { + page = self->doc->GetPage(pagenum - 1); + } catch (const PdfError &err) { + (void)err; + PyErr_Format(PyExc_ValueError, "Invalid page number: %u", pagenum - 1); goto error; + } + PdfDestination dest(page, left, top, zoom); + ans->item->SetDestination(dest); + } catch(const PdfError & err) { + podofo_set_exception(err); goto error; + } catch(const std::exception & err) { + PyErr_Format(PyExc_ValueError, "An error occurred while trying to create the outline: %s", err.what()); + goto error; + } catch (...) { + PyErr_SetString(PyExc_ValueError, "An unknown error occurred while trying to create the outline"); + goto error; + } + + return (PyObject*)ans; +error: + Py_XDECREF(ans); + return NULL; + +} // }}} + +PYWRAP(create_outline)