Exemplo n.º 1
0
class PDFSimpleFont(PDFFont):
    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

    def to_unicode(self, cid):
        if not self.ucs2_cmap:
            try:
                return self.encoding[cid]
            except KeyError:
                raise PDFUnicodeNotDefined(None, cid)
        code = self.ucs2_cmap.tocode(cid)
        if not code:
            raise PDFUnicodeNotDefined(None, cid)
        chars = unpack(">%dH" % (len(code) / 2), code)
        return "".join(unichr(c) for c in chars)
Exemplo n.º 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