Move outline creation into its own file

This commit is contained in:
Kovid Goyal 2019-10-03 16:51:33 +05:30
parent 247aa959e4
commit 1c22993e00
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 61 additions and 48 deletions

View File

@ -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",

View File

@ -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,

View File

@ -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);
}
}

View File

@ -0,0 +1,58 @@
/*
* outlines.cpp
* Copyright (C) 2019 Kovid Goyal <kovid at kovidgoyal.net>
*
* 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)