本文整理汇总了Python中reportlab.lib.utils.isUnicode函数的典型用法代码示例。如果您正苦于以下问题:Python isUnicode函数的具体用法?Python isUnicode怎么用?Python isUnicode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isUnicode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wordSplit
def wordSplit(word, maxWidths, fontName, fontSize, encoding='utf8'):
"""Attempts to break a word which lacks spaces into two parts, the first of which
fits in the remaining space. It is allowed to add hyphens or whatever it wishes.
This is intended as a wrapper for some language- and user-choice-specific splitting
algorithms. It should only be called after line breaking on spaces, which covers western
languages and is highly optimised already. It works on the 'last unsplit word'.
Presumably with further study one could write a Unicode splitting algorithm for text
fragments whick was much faster.
Courier characters should be 6 points wide.
>>> wordSplit('HelloWorld', 30, 'Courier', 10)
[[0.0, 'Hello'], [0.0, 'World']]
>>> wordSplit('HelloWorld', 31, 'Courier', 10)
[[1.0, 'Hello'], [1.0, 'World']]
"""
if not isUnicode(word):
uword = word.decode(encoding)
else:
uword = word
charWidths = getCharWidths(uword, fontName, fontSize)
lines = dumbSplit(uword, charWidths, maxWidths)
if not isUnicode(word):
lines2 = []
#convert back
for (extraSpace, text) in lines:
lines2.append([extraSpace, text.encode(encoding)])
lines = lines2
return lines
示例2: unicode2T1
def unicode2T1(utext,fonts):
'''return a list of (font,string) pairs representing the unicode text'''
R = []
font, fonts = fonts[0], fonts[1:]
enc = font.encName
if 'UCS-2' in enc:
enc = 'UTF16'
while utext:
try:
if isUnicode(utext):
s = utext.encode(enc)
else:
s = utext
R.append((font,s))
break
except UnicodeEncodeError as e:
i0, il = e.args[2:4]
if i0:
R.append((font,utext[:i0].encode(enc)))
if fonts:
R.extend(unicode2T1(utext[i0:il],fonts))
else:
R.append((font._notdefFont,font._notdefChar*(il-i0)))
utext = utext[il:]
return R
示例3: _issueT1String
def _issueT1String(self,fontObj,x,y,s):
fc = fontObj
code_append = self.code_append
fontSize = self._fontSize
fontsUsed = self._fontsUsed
escape = self._escape
if not isUnicode(s):
try:
s = s.decode('utf8')
except UnicodeDecodeError as e:
i,j = e.args[2:4]
raise UnicodeDecodeError(*(e.args[:4]+('%s\n%s-->%s<--%s' % (e.args[4],s[i-10:i],s[i:j],s[j:j+10]),)))
for f, t in unicode2T1(s,[fontObj]+fontObj.substitutionFonts):
if f!=fc:
psName = asNative(f.face.name)
code_append('(%s) findfont %s scalefont setfont' % (psName,fp_str(fontSize)))
if psName not in fontsUsed:
fontsUsed.append(psName)
fc = f
code_append('%s m (%s) show ' % (fp_str(x,y),escape(t)))
x += f.stringWidth(t.decode(f.encName),fontSize)
if fontObj!=fc:
self._font = None
self.setFont(fontObj.face.name,fontSize)
示例4: reset
def reset(self):
"""restore the cipher to it's start state"""
# Initialize private key, k With the values of the key mod 256.
# and sbox With numbers 0 - 255. Then compute sbox
key = self._key
if isUnicode(key):
key = key.encode("utf8")
sbox = list(range(256))
k = list(range(256))
lk = len(key)
if isPy3:
for i in sbox:
k[i] = key[i % lk] % 256
else:
for i in sbox:
k[i] = ord(key[i % lk]) % 256
# Re-order sbox using the private key, k.
# Iterating each element of sbox re-calculate the counter j
# Then interchange the elements sbox[a] & sbox[b]
j = 0
for i in range(256):
j = (j + sbox[i] + k[i]) % 256
sbox[i], sbox[j] = sbox[j], sbox[i]
self._sbox, self._i, self._j = sbox, 0, 0
示例5: instanceStringWidthTTF
def instanceStringWidthTTF(self, text, size, encoding='utf-8'):
"Calculate text width"
if not isUnicode(text):
text = text.decode(encoding or 'utf-8')
g = self.face.charWidths.get
dw = self.face.defaultWidth
return 0.001*size*sum([g(ord(u),dw) for u in text])
示例6: writeXML
def writeXML(tree):
"Convert to a string. No auto-indenting provided yet"
if isUnicode(tree):
return tree
else:
(tagName, attrs, children, spare) = tree
chunks = []
chunks.append(u'<%s ' % tree)
示例7: asUnicode
def asUnicode(self, markup):
"""convert to unicode"""
#TODO
if not isUnicode(markup):
try:
markup = markup.decode('utf8', 'strict')
except UnicodeDecodeError:
#assume windows encoding
markup = markup.decode('cp1252', 'replace')
return markup
示例8: resolve
def resolve(self, text, enc='utf8'):
self._output = []
self.reset()
if not isUnicode(text):
text = text.decode(enc)
else:
enc = None
self.feed(nakedAmpFix(text).replace(u'<br/>',u'<br />'))
v = u''.join(self._output)
return v.encode(enc) if enc else v
示例9: write
def write(self,u):
if isBytes(u):
try:
u = u.decode('utf-8')
except:
et, ev, tb = sys.exc_info()
ev = str(ev)
del et, tb
raise ValueError("String %r not encoded as 'utf-8'\nerror=%s" % (u,ev))
elif not isUnicode(u):
raise ValueError("EncodedWriter.write(%s) argument should be 'utf-8' bytes or str" % ascii(u))
self.append(u)
示例10: _AsciiHexEncode
def _AsciiHexEncode(input):
"""Encodes input using ASCII-Hex coding.
This is a verbose encoding used for binary data within
a PDF file. One byte binary becomes two bytes of ASCII.
Helper function used by images."""
if isUnicode(input):
input = input.encode('utf-8')
output = getBytesIO()
output.write(binascii.b2a_hex(input))
output.write(b'>')
return output.getvalue()
示例11: __init__
def __init__(self, value='Hello World', **kw):
self.value = isUnicodeOrQRList.normalize(value)
for k, v in kw.items():
setattr(self, k, v)
ec_level = getattr(qrencoder.QRErrorCorrectLevel, self.barLevel)
self.__dict__['qr'] = qrencoder.QRCode(self.qrVersion, ec_level)
if isUnicode(self.value):
self.addData(self.value)
elif self.value:
for v in self.value:
self.addData(v)
示例12: _AsciiHexDecode
def _AsciiHexDecode(input):
"""Decodes input using ASCII-Hex coding.
Not used except to provide a test of the inverse function."""
#strip out all whitespace
if not isUnicode(input):
input = input.decode('utf-8')
stripped = ''.join(input.split())
assert stripped[-1] == '>', 'Invalid terminator for Ascii Hex Stream'
stripped = stripped[:-1] #chop off terminator
assert len(stripped) % 2 == 0, 'Ascii Hex stream has odd number of bytes'
return ''.join([chr(int(stripped[i:i+2],16)) for i in range(0,len(stripped),2)])
示例13: splitString
def splitString(self, text, doc, encoding='utf-8'):
"""Splits text into a number of chunks, each of which belongs to a
single subset. Returns a list of tuples (subset, string). Use subset
numbers with getSubsetInternalName. Doc is needed for distinguishing
subsets when building different documents at the same time."""
asciiReadable = self._asciiReadable
try: state = self.state[doc]
except KeyError: state = self.state[doc] = TTFont.State(asciiReadable)
curSet = -1
cur = []
results = []
if not isUnicode(text):
text = text.decode('utf-8') # encoding defaults to utf-8
assignments = state.assignments
subsets = state.subsets
for code in map(ord,text):
if code in assignments:
n = assignments[code]
else:
if state.frozen:
raise pdfdoc.PDFError("Font %s is already frozen, cannot add new character U+%04X" % (self.fontName, code))
n = state.nextCode
if n&0xFF==32:
# make code 32 always be a space character
if n!=32: subsets[n >> 8].append(32)
state.nextCode += 1
n = state.nextCode
state.nextCode += 1
assignments[code] = n
if n>32:
if not(n&0xFF): subsets.append([])
subsets[n >> 8].append(code)
else:
subsets[0][n] = code
if (n >> 8) != curSet:
if cur:
results.append((curSet,bytes(cur) if isPy3 else ''.join(chr(c) for c in cur)))
curSet = (n >> 8)
cur = []
cur.append(n & 0xFF)
if cur:
results.append((curSet,bytes(cur) if isPy3 else ''.join(chr(c) for c in cur)))
return results
示例14: drawString
def drawString(self, x, y, text, _fontInfo=None):
gs = self._gs
if _fontInfo:
fontName, fontSize = _fontInfo
else:
fontSize = gs.fontSize
fontName = gs.fontName
try:
gfont = getFont(gs.fontName)
except:
gfont = None
font = getFont(fontName)
if font._dynamicFont:
gs.drawString(x, y, text)
else:
fc = font
if not isUnicode(text):
try:
text = text.decode("utf8")
except UnicodeDecodeError as e:
i, j = e.args[2:4]
raise UnicodeDecodeError(
*(
e.args[:4]
+ ("%s\n%s-->%s<--%s" % (e.args[4], text[i - 10 : i], text[i:j], text[j : j + 10]),)
)
)
FT = unicode2T1(text, [font] + font.substitutionFonts)
n = len(FT)
nm1 = n - 1
for i in range(n):
f, t = FT[i]
if f != fc:
_setFont(gs, f.fontName, fontSize)
fc = f
gs.drawString(x, y, t)
if i != nm1:
x += f.stringWidth(t.decode(f.encName), fontSize)
if font != fc:
_setFont(gs, fontName, fontSize)
示例15: tt2xml
def tt2xml(tt):
'''convert tuple tree form to unicode xml'''
if tt is None: return ''
if isBytes(tt):
return tt2xml(tt.decode('utf8'))
if isUnicode(tt):
return escape(tt)
if isinstance(tt,list):
return ''.join(tt2xml(x) for x in tt)
if isinstance(tt,tuple):
tag = tt[0].decode('utf8')
L=['<'+tag].append
C = tt[2]
if tt[1]:
for k,v in tt[1].items():
L((' %s=%s' % (k,quoteattr(v))).decode('utf8'))
if C is not None:
L('>')
L(tt2xml(C))
L('</'+tag+'>')
else:
L('/>')
return ''.join(L.__self__)
raise ValueError('Invalid value %r passed to tt2xml' % tt)