Also delete DescendantFonts of Type0 fonts

This commit is contained in:
Kovid Goyal 2019-07-24 12:04:58 +05:30
parent 7f3bd476d3
commit 6057bcbb5a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -251,6 +251,8 @@ typedef std::unordered_map<PdfReference, unsigned long, PdfReferenceHasher> char
PyObject*
remove_unused_fonts(PDFDoc *self, PyObject *args) {
unsigned long count = 0;
try {
unordered_reference_set used_fonts;
for (int i = 0; i < self->doc->GetPageCount(); i++) {
PdfPage *page = self->doc->GetPage(i);
@ -280,7 +282,6 @@ remove_unused_fonts(PDFDoc *self, PyObject *args) {
}
}
unsigned long count = 0;
for (auto &ref : all_fonts) {
if (used_fonts.find(ref) == used_fonts.end()) {
PdfObject *font = objects.GetObject(ref);
@ -290,6 +291,11 @@ remove_unused_fonts(PDFDoc *self, PyObject *args) {
for (auto &x : font->GetIndirectKey("CharProcs")->GetDictionary().GetKeys()) {
charprocs_usage[x.second->GetReference()] -= 1;
}
} else {
for (auto &x : font->GetIndirectKey("DescendantFonts")->GetArray()) {
PdfObject *dfont = objects.GetObject(x.GetReference());
if (dfont) remove_font(objects, dfont);
}
}
remove_font(objects, font);
}
@ -301,6 +307,8 @@ remove_unused_fonts(PDFDoc *self, PyObject *args) {
delete objects.RemoveObject(x.first);
}
}
} catch(const PdfError &err) { podofo_set_exception(err); return NULL; }
return Py_BuildValue("k", count);
}