1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: latin-1 -*-
|
---|
3 |
|
---|
4 | from .py3k import PY3K, basestring, unicode
|
---|
5 |
|
---|
6 | # fpdf php helpers:
|
---|
7 |
|
---|
8 | def substr(s, start, length=-1):
|
---|
9 | if length < 0:
|
---|
10 | length=len(s)-start
|
---|
11 | return s[start:start+length]
|
---|
12 |
|
---|
13 | def sprintf(fmt, *args): return fmt % args
|
---|
14 |
|
---|
15 | def print_r(array):
|
---|
16 | if not isinstance(array, dict):
|
---|
17 | array = dict([(k, k) for k in array])
|
---|
18 | for k, v in array.items():
|
---|
19 | print("[%s] => %s " % (k, v))
|
---|
20 |
|
---|
21 | def UTF8ToUTF16BE(instr, setbom=True):
|
---|
22 | "Converts UTF-8 strings to UTF16-BE."
|
---|
23 | outstr = "".encode()
|
---|
24 | if (setbom):
|
---|
25 | outstr += "\xFE\xFF".encode("latin1")
|
---|
26 | if not isinstance(instr, unicode):
|
---|
27 | instr = instr.decode('UTF-8')
|
---|
28 | outstr += instr.encode('UTF-16BE')
|
---|
29 | # convert bytes back to fake unicode string until PEP461-like is implemented
|
---|
30 | if PY3K:
|
---|
31 | outstr = outstr.decode("latin1")
|
---|
32 | return outstr
|
---|
33 |
|
---|
34 | def UTF8StringToArray(instr):
|
---|
35 | "Converts UTF-8 strings to codepoints array"
|
---|
36 | return [ord(c) for c in instr]
|
---|
37 |
|
---|
38 | # ttfints php helpers:
|
---|
39 |
|
---|
40 | def die(msg):
|
---|
41 | raise RuntimeError(msg)
|
---|
42 |
|
---|
43 | def str_repeat(s, count):
|
---|
44 | return s * count
|
---|
45 |
|
---|
46 | def str_pad(s, pad_length=0, pad_char= " ", pad_type= +1 ):
|
---|
47 | if pad_type<0: # pad left
|
---|
48 | return s.rjust(pad_length, pad_char)
|
---|
49 | elif pad_type>0: # pad right
|
---|
50 | return s.ljust(pad_length, pad_char)
|
---|
51 | else: # pad both
|
---|
52 | return s.center(pad_length, pad_char)
|
---|
53 |
|
---|
54 | strlen = count = lambda s: len(s)
|
---|