Processor for the content of a PDF page Reference: PDF Reference, Appendix A, Operator Summary
| 49 | |
| 50 | |
| 51 | class PDFPageInterpreterEx(PDFPageInterpreter): |
| 52 | """Processor for the content of a PDF page |
| 53 | |
| 54 | Reference: PDF Reference, Appendix A, Operator Summary |
| 55 | """ |
| 56 | |
| 57 | def __init__( |
| 58 | self, rsrcmgr: PDFResourceManager, device: PDFDevice, obj_patch |
| 59 | ) -> None: |
| 60 | self.rsrcmgr = rsrcmgr |
| 61 | self.device = device |
| 62 | self.obj_patch = obj_patch |
| 63 | |
| 64 | def dup(self) -> "PDFPageInterpreterEx": |
| 65 | return self.__class__(self.rsrcmgr, self.device, self.obj_patch) |
| 66 | |
| 67 | def init_resources(self, resources: Dict[object, object]) -> None: |
| 68 | # 重载设置 fontid 和 descent |
| 69 | """Prepare the fonts and XObjects listed in the Resource attribute.""" |
| 70 | self.resources = resources |
| 71 | self.fontmap: Dict[object, PDFFont] = {} |
| 72 | self.fontid: Dict[PDFFont, object] = {} |
| 73 | self.xobjmap = {} |
| 74 | self.csmap: Dict[str, PDFColorSpace] = PREDEFINED_COLORSPACE.copy() |
| 75 | if not resources: |
| 76 | return |
| 77 | |
| 78 | def get_colorspace(spec: object) -> Optional[PDFColorSpace]: |
| 79 | if isinstance(spec, list): |
| 80 | name = literal_name(spec[0]) |
| 81 | else: |
| 82 | name = literal_name(spec) |
| 83 | if name == "ICCBased" and isinstance(spec, list) and len(spec) >= 2: |
| 84 | return PDFColorSpace(name, stream_value(spec[1])["N"]) |
| 85 | elif name == "DeviceN" and isinstance(spec, list) and len(spec) >= 2: |
| 86 | return PDFColorSpace(name, len(list_value(spec[1]))) |
| 87 | else: |
| 88 | return PREDEFINED_COLORSPACE.get(name) |
| 89 | |
| 90 | for k, v in dict_value(resources).items(): |
| 91 | # log.debug("Resource: %r: %r", k, v) |
| 92 | if k == "Font": |
| 93 | for fontid, spec in dict_value(v).items(): |
| 94 | objid = None |
| 95 | if isinstance(spec, PDFObjRef): |
| 96 | objid = spec.objid |
| 97 | spec = dict_value(spec) |
| 98 | self.fontmap[fontid] = self.rsrcmgr.get_font(objid, spec) |
| 99 | self.fontmap[fontid].descent = 0 # hack fix descent |
| 100 | self.fontid[self.fontmap[fontid]] = fontid |
| 101 | elif k == "ColorSpace": |
| 102 | for csid, spec in dict_value(v).items(): |
| 103 | colorspace = get_colorspace(resolve1(spec)) |
| 104 | if colorspace is not None: |
| 105 | self.csmap[csid] = colorspace |
| 106 | elif k == "ProcSet": |
| 107 | self.rsrcmgr.get_procset(list_value(v)) |
| 108 | elif k == "XObject": |