Ejemplo n.º 1
0
 def do_Do(self, xobjid):
     xobjid = literal_name(xobjid)
     try:
         xobj = stream_value(self.xobjmap[xobjid])
     except KeyError:
         if STRICT:
             raise PDFInterpreterError('Undefined xobject id: %r' % xobjid)
         return
     if 1 <= self.debug:
         print(('Processing xobj: %r' % xobj))
     subtype = xobj.get('Subtype')
     if subtype is LITERAL_FORM and 'BBox' in xobj:
         interpreter = self.dup()
         bbox = list_value(xobj['BBox'])
         matrix = list_value(xobj.get('Matrix', MATRIX_IDENTITY))
         # According to PDF reference 1.7 section 4.9.1, XObjects in 
         # earlier PDFs (prior to v1.2) use the page's Resources entry
         # instead of having their own Resources entry.
         resources = dict_value(xobj.get('Resources')) or self.resources.copy()
         self.device.begin_figure(xobjid, bbox, matrix)
         interpreter.render_contents(resources, [xobj], ctm=mult_matrix(matrix, self.ctm))
         self.device.end_figure(xobjid)
     elif subtype is LITERAL_IMAGE and 'Width' in xobj and 'Height' in xobj:
         self.device.begin_figure(xobjid, (0,0,1,1), MATRIX_IDENTITY)
         self.device.render_image(xobjid, xobj)
         self.device.end_figure(xobjid)
     else:
         # unsupported xobject type.
         pass
     return
Ejemplo n.º 2
0
 def lookup(d):
     if 'Limits' in d:
         (k1,k2) = list_value(d['Limits'])
         if key < k1 or k2 < key: return None
         if 'Names' in d:
             objs = list_value(d['Names'])
             names = dict(choplist(2, objs))
             return names[key]
     if 'Kids' in d:
         for c in list_value(d['Kids']):
             v = lookup(dict_value(c))
             if v: return v
     raise KeyError((cat,key))
Ejemplo n.º 3
0
 def __init__(self, rsrcmgr, spec):
     firstchar = int_value(spec.get("FirstChar", 0))
     lastchar = int_value(spec.get("LastChar", 0))
     widths = list_value(spec.get("Widths", [0] * 256))
     widths = dict((i + firstchar, w) for (i, w) in enumerate(widths))
     if "FontDescriptor" in spec:
         descriptor = dict_value(spec["FontDescriptor"])
     else:
         descriptor = {"Ascent": 0, "Descent": 0, "FontBBox": spec["FontBBox"]}
     PDFSimpleFont.__init__(self, descriptor, widths, spec)
     self.matrix = tuple(list_value(spec.get("FontMatrix")))
     (_, self.descent, _, self.ascent) = self.bbox
     (self.hscale, self.vscale) = apply_matrix_norm(self.matrix, (1, 1))
     return
Ejemplo n.º 4
0
 def set_parser(self, parser):
     "Set the document to use a given PDFParser object."
     if self._parser: return
     self._parser = parser
     # Retrieve the information of each header that was appended
     # (maybe multiple times) at the end of the document.
     self.xrefs = parser.read_xref()
     for xref in self.xrefs:
         trailer = xref.get_trailer()
         if not trailer: continue
         # If there's an encryption info, remember it.
         if 'Encrypt' in trailer:
             #assert not self.encryption
             self.encryption = (list_value(trailer['ID']),
                                dict_value(trailer['Encrypt']))
         if 'Info' in trailer:
             self.info.append(dict_value(trailer['Info']))
         if 'Root' in trailer:
             #  Every PDF file must have exactly one /Root dictionary.
             self.catalog = dict_value(trailer['Root'])
             break
     else:
         raise PDFSyntaxError('No /Root object! - Is this really a PDF?')
     if self.catalog.get('Type') is not LITERAL_CATALOG:
         if STRICT:
             raise PDFSyntaxError('Catalog not found!')
     return
Ejemplo n.º 5
0
 def render_contents(self, resources, streams, ctm=MATRIX_IDENTITY):
     if 1 <= self.debug:
         print(('render_contents: resources=%r, streams=%r, ctm=%r' %
                          (resources, streams, ctm)))
     self.init_resources(resources)
     self.init_state(ctm)
     self.execute(list_value(streams))
     return
Ejemplo n.º 6
0
 def get_colorspace(spec):
     if isinstance(spec, list):
         name = literal_name(spec[0])
     else:
         name = literal_name(spec)
     if name == 'ICCBased' and isinstance(spec, list) and 2 <= len(spec):
         return PDFColorSpace(name, stream_value(spec[1])['N'])
     elif name == 'DeviceN' and isinstance(spec, list) and 2 <= len(spec):
         return PDFColorSpace(name, len(list_value(spec[1])))
     else:
         return PREDEFINED_COLORSPACE[name]
Ejemplo n.º 7
0
 def __init__(self, descriptor, widths, default_width=None):
     self.descriptor = descriptor
     self.widths = widths
     self.fontname = resolve1(descriptor.get("FontName", "unknown"))
     if isinstance(self.fontname, PSLiteral):
         self.fontname = literal_name(self.fontname)
     self.flags = int_value(descriptor.get("Flags", 0))
     self.ascent = num_value(descriptor.get("Ascent", 0))
     self.descent = num_value(descriptor.get("Descent", 0))
     self.italic_angle = num_value(descriptor.get("ItalicAngle", 0))
     self.default_width = default_width or num_value(descriptor.get("MissingWidth", 0))
     self.leading = num_value(descriptor.get("Leading", 0))
     self.bbox = list_value(descriptor.get("FontBBox", (0, 0, 0, 0)))
     self.hscale = self.vscale = 0.001
     return
Ejemplo n.º 8
0
 def get_font(self, objid, spec):
     if objid and objid in self._cached_fonts:
         font = self._cached_fonts[objid]
     else:
         if 2 <= self.debug:
             print(('get_font: create: objid=%r, spec=%r' % (objid, spec)))
         if STRICT:
             if spec['Type'] is not LITERAL_FONT:
                 raise PDFFontError('Type is not /Font')
         # Create a Font object.
         if 'Subtype' in spec:
             subtype = literal_name(spec['Subtype'])
         else:
             if STRICT:
                 raise PDFFontError('Font Subtype is not specified.')
             subtype = 'Type1'
         if subtype in ('Type1', 'MMType1'):
             # Type1 Font
             font = PDFType1Font(self, spec)
         elif subtype == 'TrueType':
             # TrueType Font
             font = PDFTrueTypeFont(self, spec)
         elif subtype == 'Type3':
             # Type3 Font
             font = PDFType3Font(self, spec)
         elif subtype in ('CIDFontType0', 'CIDFontType2'):
             # CID Font
             font = PDFCIDFont(self, spec)
         elif subtype == 'Type0':
             # Type0 Font
             dfonts = list_value(spec['DescendantFonts'])
             assert dfonts
             subspec = dict_value(dfonts[0]).copy()
             for k in ('Encoding', 'ToUnicode'):
                 if k in spec:
                     subspec[k] = resolve1(spec[k])
             font = self.get_font(None, subspec)
         else:
             if STRICT:
                 raise PDFFontError('Invalid Font spec: %r' % spec)
             font = PDFType1Font(self, spec) # this is so wrong!
         if objid and self.caching:
             self._cached_fonts[objid] = font
     return font
Ejemplo n.º 9
0
 def search(obj, parent):
     if isinstance(obj, int):
         objid = obj
         tree = dict_value(self.getobj(objid)).copy()
     else:
         objid = obj.objid
         tree = dict_value(obj).copy()
     for (k,v) in list(parent.items()):
         if k in self.INHERITABLE_ATTRS and k not in tree:
             tree[k] = v
     if tree.get('Type') is LITERAL_PAGES and 'Kids' in tree:
         if 1 <= self.debug:
             print(('Pages: Kids=%r' % tree['Kids']))
         for c in list_value(tree['Kids']):
             for x in search(c, tree):
                 yield x
     elif tree.get('Type') is LITERAL_PAGE:
         if 1 <= self.debug:
             print(('Page: %r' % tree))
         yield (objid, tree)
Ejemplo n.º 10
0
 def __init__(self, descriptor, widths, spec):
     # Font encoding is specified either by a name of
     # built-in encoding or a dictionary that describes
     # the differences.
     if "Encoding" in spec:
         encoding = resolve1(spec["Encoding"])
     else:
         encoding = LITERAL_STANDARD_ENCODING
     if isinstance(encoding, dict):
         name = literal_name(encoding.get("BaseEncoding", LITERAL_STANDARD_ENCODING))
         diff = list_value(encoding.get("Differences", None))
         self.cid2unicode = EncodingDB.get_encoding(name, diff)
     else:
         self.cid2unicode = EncodingDB.get_encoding(literal_name(encoding))
     self.unicode_map = None
     if "ToUnicode" in spec:
         strm = stream_value(spec["ToUnicode"])
         self.unicode_map = FileUnicodeMap()
         CMapParser(self.unicode_map, StringIO(strm.get_data())).run()
     PDFFont.__init__(self, descriptor, widths)
     return
Ejemplo n.º 11
0
 def init_resources(self, resources):
     self.resources = resources
     self.fontmap = {}
     self.xobjmap = {}
     self.csmap = PREDEFINED_COLORSPACE.copy()
     if not resources: return
     def get_colorspace(spec):
         if isinstance(spec, list):
             name = literal_name(spec[0])
         else:
             name = literal_name(spec)
         if name == 'ICCBased' and isinstance(spec, list) and 2 <= len(spec):
             return PDFColorSpace(name, stream_value(spec[1])['N'])
         elif name == 'DeviceN' and isinstance(spec, list) and 2 <= len(spec):
             return PDFColorSpace(name, len(list_value(spec[1])))
         else:
             return PREDEFINED_COLORSPACE[name]
     for (k,v) in list(dict_value(resources).items()):
         if 2 <= self.debug:
             print(('Resource: %r: %r' % (k,v)))
         if k == 'Font':
             for (fontid,spec) in list(dict_value(v).items()):
                 objid = None
                 if isinstance(spec, PDFObjRef):
                     objid = spec.objid
                 spec = dict_value(spec)
                 self.fontmap[fontid] = self.rsrcmgr.get_font(objid, spec)
         elif k == 'ColorSpace':
             for (csid,spec) in list(dict_value(v).items()):
                 self.csmap[csid] = get_colorspace(resolve1(spec))
         elif k == 'ProcSet':
             self.rsrcmgr.get_procset(list_value(v))
         elif k == 'XObject':
             for (xobjid,xobjstrm) in list(dict_value(v).items()):
                 self.xobjmap[xobjid] = xobjstrm
     return
Ejemplo n.º 12
0
 def __init__(self, rsrcmgr, spec):
     try:
         self.basefont = literal_name(spec["BaseFont"])
     except KeyError:
         if STRICT:
             raise PDFFontError("BaseFont is missing")
         self.basefont = "unknown"
     try:
         (descriptor, widths) = FontMetricsDB.get_metrics(self.basefont)
     except KeyError:
         descriptor = dict_value(spec.get("FontDescriptor", {}))
         firstchar = int_value(spec.get("FirstChar", 0))
         lastchar = int_value(spec.get("LastChar", 255))
         widths = list_value(spec.get("Widths", [0] * 256))
         widths = dict((i + firstchar, w) for (i, w) in enumerate(widths))
     PDFSimpleFont.__init__(self, descriptor, widths, spec)
     if "Encoding" not in spec and "FontFile" in descriptor:
         # try to recover the missing encoding info from the font file.
         self.fontfile = stream_value(descriptor.get("FontFile"))
         length1 = int_value(self.fontfile["Length1"])
         data = self.fontfile.get_data()[:length1]
         parser = Type1FontHeaderParser(StringIO(data))
         self.cid2unicode = parser.get_encoding()
     return
Ejemplo n.º 13
0
    def __init__(self, rsrcmgr, spec):
        try:
            self.basefont = literal_name(spec["BaseFont"])
        except KeyError:
            if STRICT:
                raise PDFFontError("BaseFont is missing")
            self.basefont = "unknown"
        self.cidsysteminfo = dict_value(spec.get("CIDSystemInfo", {}))
        self.cidcoding = "%s-%s" % (
            self.cidsysteminfo.get("Registry", "unknown"),
            self.cidsysteminfo.get("Ordering", "unknown"),
        )
        try:
            name = literal_name(spec["Encoding"])
        except KeyError:
            if STRICT:
                raise PDFFontError("Encoding is unspecified")
            name = "unknown"
        try:
            self.cmap = CMapDB.get_cmap(name)
        except CMapDB.CMapNotFound as e:
            if STRICT:
                raise PDFFontError(e)
            self.cmap = CMap()
        try:
            descriptor = dict_value(spec["FontDescriptor"])
        except KeyError:
            if STRICT:
                raise PDFFontError("FontDescriptor is missing")
            descriptor = {}
        ttf = None
        if "FontFile2" in descriptor:
            self.fontfile = stream_value(descriptor.get("FontFile2"))
            ttf = TrueTypeFont(self.basefont, StringIO(self.fontfile.get_data()))
        self.unicode_map = None
        if "ToUnicode" in spec:
            strm = stream_value(spec["ToUnicode"])
            self.unicode_map = FileUnicodeMap()
            CMapParser(self.unicode_map, StringIO(strm.get_data())).run()
        elif self.cidcoding == "Adobe-Identity":
            if ttf:
                try:
                    self.unicode_map = ttf.create_unicode_map()
                except TrueTypeFont.CMapNotFound:
                    pass
        else:
            try:
                self.unicode_map = CMapDB.get_unicode_map(self.cidcoding, self.cmap.is_vertical())
            except CMapDB.CMapNotFound as e:
                pass

        self.vertical = self.cmap.is_vertical()
        if self.vertical:
            # writing mode: vertical
            widths = get_widths2(list_value(spec.get("W2", [])))
            self.disps = dict((cid, (vx, vy)) for (cid, (_, (vx, vy))) in list(widths.items()))
            (vy, w) = spec.get("DW2", [880, -1000])
            self.default_disp = (None, vy)
            widths = dict((cid, w) for (cid, (w, _)) in list(widths.items()))
            default_width = w
        else:
            # writing mode: horizontal
            self.disps = {}
            self.default_disp = 0
            widths = get_widths(list_value(spec.get("W", [])))
            default_width = spec.get("DW", 1000)
        PDFFont.__init__(self, descriptor, widths, default_width=default_width)
        return