mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
DRYer
This commit is contained in:
parent
3eb3484cc0
commit
ba15429264
@ -731,20 +731,21 @@ PDFDoc_version_getter(PDFDoc *self, void *closure) {
|
|||||||
return PyUnicode_FromString("");
|
return PyUnicode_FromString("");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static PdfDictionary*
|
||||||
get_or_create_info(PDFDoc *self, PdfDictionary **dict) {
|
get_or_create_info(PDFDoc *self) {
|
||||||
PdfObject *info = self->doc->GetTrailer().GetDictionary().FindKey("Info");
|
PdfObject *info = self->doc->GetTrailer().GetDictionary().FindKey("Info");
|
||||||
if (info && info->TryGetDictionary(*dict)) return;
|
PdfDictionary *ans;
|
||||||
|
if (info && info->TryGetDictionary(ans)) return ans;
|
||||||
info = &self->doc->GetObjects().CreateDictionaryObject();
|
info = &self->doc->GetObjects().CreateDictionaryObject();
|
||||||
self->doc->GetTrailer().GetDictionary().AddKeyIndirect("Info", *info);
|
self->doc->GetTrailer().GetDictionary().AddKeyIndirect("Info", *info);
|
||||||
info->TryGetDictionary(*dict);
|
info->TryGetDictionary(ans);
|
||||||
|
return ans;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static inline PyObject*
|
static inline PyObject*
|
||||||
string_metadata_getter(PDFDoc *self, const std::string_view name) {
|
string_metadata_getter(PDFDoc *self, const std::string_view name) {
|
||||||
PdfDictionary *info_dict;
|
PdfDictionary *info_dict = get_or_create_info(self);
|
||||||
get_or_create_info(self, &info_dict);
|
|
||||||
auto obj = info_dict->FindKey(name);
|
auto obj = info_dict->FindKey(name);
|
||||||
const PdfString* str;
|
const PdfString* str;
|
||||||
return (obj == nullptr || !obj->TryGetString(str)) ? PyUnicode_FromString("") : podofo_convert_pdfstring(*str);
|
return (obj == nullptr || !obj->TryGetString(str)) ? PyUnicode_FromString("") : podofo_convert_pdfstring(*str);
|
||||||
@ -784,8 +785,7 @@ PDFDoc_producer_getter(PDFDoc *self, void *closure) {
|
|||||||
static inline int
|
static inline int
|
||||||
string_metadata_setter(PDFDoc *self, const std::string_view name, PyObject *val) {
|
string_metadata_setter(PDFDoc *self, const std::string_view name, PyObject *val) {
|
||||||
if (!PyUnicode_Check(val)) { PyErr_SetString(PyExc_TypeError, "Must use unicode to set metadata"); return -1; }
|
if (!PyUnicode_Check(val)) { PyErr_SetString(PyExc_TypeError, "Must use unicode to set metadata"); return -1; }
|
||||||
PdfDictionary *info_dict;
|
PdfDictionary *info_dict = get_or_create_info(self);
|
||||||
get_or_create_info(self, &info_dict);
|
|
||||||
const char *raw; Py_ssize_t sz;
|
const char *raw; Py_ssize_t sz;
|
||||||
raw = PyUnicode_AsUTF8AndSize(val, &sz);
|
raw = PyUnicode_AsUTF8AndSize(val, &sz);
|
||||||
if (sz == 0) info_dict->RemoveKey(name);
|
if (sz == 0) info_dict->RemoveKey(name);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user