mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
When replacing font references do it in XObjects as well
This commit is contained in:
parent
227679985f
commit
c842a82a0f
@ -46,26 +46,34 @@ ref_as_integer(const PdfReference &ref) { return ref_as_integer(ref.ObjectNumber
|
|||||||
|
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
replace_font_references(PDFDoc *self, std::unordered_map<uint64_t, uint64_t> &ref_map) {
|
replace_font_references_in_resources(PdfDictionary &resources, const std::unordered_map<uint64_t, uint64_t> &ref_map) {
|
||||||
int num_pages = self->doc->GetPageCount();
|
const PdfObject* f = resources.GetKey("Font");
|
||||||
for (int i = 0; i < num_pages; i++) {
|
if (f && f->IsDictionary()) {
|
||||||
PdfPage *page = self->doc->GetPage(i);
|
const PdfDictionary &font = f->GetDictionary();
|
||||||
PdfDictionary &resources = page->GetResources()->GetDictionary();
|
PdfDictionary new_font = PdfDictionary(font);
|
||||||
PdfObject* f = resources.GetKey("Font");
|
for (auto &k : font.GetKeys()) {
|
||||||
if (f && f->IsDictionary()) {
|
if (k.second->IsReference()) {
|
||||||
const PdfDictionary &font = f->GetDictionary();
|
uint64_t key = ref_as_integer(k.second->GetReference()), r;
|
||||||
PdfDictionary new_font = PdfDictionary(font);
|
try {
|
||||||
for (auto &k : font.GetKeys()) {
|
r = ref_map.at(key);
|
||||||
if (k.second->IsReference()) {
|
} catch (const std::out_of_range &err) { continue; }
|
||||||
uint64_t key = ref_as_integer(k.second->GetReference()), r;
|
PdfReference new_ref(static_cast<uint32_t>(r & 0xffffffff), r >> 32);
|
||||||
try {
|
new_font.AddKey(k.first.GetName(), new_ref);
|
||||||
r = ref_map.at(key);
|
|
||||||
} catch (const std::out_of_range &err) { continue; }
|
|
||||||
PdfReference new_ref(static_cast<uint32_t>(r & 0xffffffff), r >> 32);
|
|
||||||
new_font.AddKey(k.first.GetName(), new_ref);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
resources.AddKey("Font", new_font);
|
}
|
||||||
|
resources.AddKey("Font", new_font);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void
|
||||||
|
replace_font_references(PDFDoc *self, const std::unordered_map<uint64_t, uint64_t> &ref_map) {
|
||||||
|
const PdfVecObjects &objects = self->doc->GetObjects();
|
||||||
|
for (auto &k : objects) {
|
||||||
|
if (!k->IsDictionary()) continue;
|
||||||
|
PdfDictionary &dict = k->GetDictionary();
|
||||||
|
if (dict.HasKey("Resources") && dict.GetKey("Resources")->IsDictionary()) {
|
||||||
|
PdfDictionary &res = dict.GetKey("Resources")->GetDictionary();
|
||||||
|
replace_font_references_in_resources(res, ref_map);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user