mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #2103084 [Private bug](https://bugs.launchpad.net/calibre/+bug/2103084)
This commit is contained in:
parent
d422c4b7f4
commit
3eb3484cc0
@ -242,6 +242,8 @@ def test_podofo():
|
||||
try:
|
||||
p = podofo.PDFDoc()
|
||||
p.open(f.name)
|
||||
print(1111111111111111111)
|
||||
print(22222222, repr(p.title))
|
||||
if (p.title, p.author, p.keywords) != ('info title', 'info author', 'a, b'):
|
||||
raise ValueError('podofo failed to set title and author in Info dict {} != {}'.format(
|
||||
(p.title, p.author, p.keywords), ('info title', 'info author', 'a, b')))
|
||||
|
@ -731,19 +731,21 @@ PDFDoc_version_getter(PDFDoc *self, void *closure) {
|
||||
return PyUnicode_FromString("");
|
||||
}
|
||||
|
||||
static PdfDictionary&
|
||||
get_or_create_info(PDFDoc *self) {
|
||||
static void
|
||||
get_or_create_info(PDFDoc *self, PdfDictionary **dict) {
|
||||
PdfObject *info = self->doc->GetTrailer().GetDictionary().FindKey("Info");
|
||||
if (info && info->IsDictionary()) return info->GetDictionary();
|
||||
if (info && info->TryGetDictionary(*dict)) return;
|
||||
info = &self->doc->GetObjects().CreateDictionaryObject();
|
||||
self->doc->GetTrailer().GetDictionary().AddKeyIndirect("Info", *info);
|
||||
return info->GetDictionary();
|
||||
info->TryGetDictionary(*dict);
|
||||
}
|
||||
|
||||
|
||||
static inline PyObject*
|
||||
string_metadata_getter(PDFDoc *self, const std::string_view name) {
|
||||
auto info = get_or_create_info(self);
|
||||
auto obj = info.FindKey(name);
|
||||
PdfDictionary *info_dict;
|
||||
get_or_create_info(self, &info_dict);
|
||||
auto obj = info_dict->FindKey(name);
|
||||
const PdfString* str;
|
||||
return (obj == nullptr || !obj->TryGetString(str)) ? PyUnicode_FromString("") : podofo_convert_pdfstring(*str);
|
||||
}
|
||||
@ -782,11 +784,12 @@ PDFDoc_producer_getter(PDFDoc *self, void *closure) {
|
||||
static inline int
|
||||
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; }
|
||||
auto& info = get_or_create_info(self);
|
||||
PdfDictionary *info_dict;
|
||||
get_or_create_info(self, &info_dict);
|
||||
const char *raw; Py_ssize_t sz;
|
||||
raw = PyUnicode_AsUTF8AndSize(val, &sz);
|
||||
if (sz == 0) info.RemoveKey(name);
|
||||
else info.AddKey(name, PdfString(std::string_view(raw, sz)));
|
||||
if (sz == 0) info_dict->RemoveKey(name);
|
||||
else info_dict->AddKey(name, PdfString(std::string_view(raw, sz)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user