Пример #1
0
 def get_font(self, objid, spec):
     if objid and objid in self.fonts:
         font = self.fonts[objid]
     else:
         if STRICT:
             if spec["Type"] != 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(spec)
         elif subtype == "TrueType":
             # TrueType Font
             font = PDFTrueTypeFont(spec)
         elif subtype == "Type3":
             # Type3 Font
             font = PDFType3Font(spec)
         elif subtype in ("CIDFontType0", "CIDFontType2"):
             # CID Font
             font = PDFCIDFont(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: %r" % spec)
             font = PDFType1Font(spec)  # this is so wrong!
         if objid:
             self.fonts[objid] = font
     return font
Пример #2
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.encoding = EncodingDB.get_encoding(name, diff)
     else:
         self.encoding = EncodingDB.get_encoding(literal_name(encoding))
     self.ucs2_cmap = None
     if "ToUnicode" in spec:
         strm = stream_value(spec["ToUnicode"])
         self.ucs2_cmap = CMap()
         CMapParser(self.ucs2_cmap, StringIO(strm.get_data())).run()
     PDFFont.__init__(self, descriptor, widths)
     return
Пример #3
0
    def init_resources(self, resources):
        self.fontmap = {}
        self.xobjmap = {}
        self.csmap = PREDEFINED_COLORSPACE.copy()
        # Handle resource declarations.
        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 ColorSpace(name, stream_value(spec[1]).dic["N"])
            elif name == "DeviceN" and isinstance(spec, list) and 2 <= len(spec):
                return ColorSpace(name, len(list_value(spec[1])))
            else:
                return PREDEFINED_COLORSPACE[name]

        if resources:
            for (k, v) in dict_value(resources).iteritems():
                if 1 <= self.debug:
                    print >> stderr, "Resource: %r: %r" % (k, v)
                if k == "Font":
                    for (fontid, spec) in dict_value(v).iteritems():
                        objid = None
                        if isinstance(spec, PDFObjRef):
                            objid = spec.objid
                        spec = dict_value(spec)
                        self.fontmap[fontid] = self.rsrc.get_font(objid, spec)
                elif k == "ColorSpace":
                    for (csid, spec) in dict_value(v).iteritems():
                        self.csmap[csid] = get_colorspace(resolve1(spec))
                elif k == "ProcSet":
                    self.rsrc.get_procset(list_value(v))
                elif k == "XObject":
                    for (xobjid, xobjstrm) in dict_value(v).iteritems():
                        self.xobjmap[xobjid] = xobjstrm
        return