fonttools 4.60.2__cp311-cp311-win32.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fontTools/__init__.py +8 -0
- fontTools/__main__.py +35 -0
- fontTools/afmLib.py +439 -0
- fontTools/agl.py +5233 -0
- fontTools/annotations.py +30 -0
- fontTools/cffLib/CFF2ToCFF.py +258 -0
- fontTools/cffLib/CFFToCFF2.py +305 -0
- fontTools/cffLib/__init__.py +3694 -0
- fontTools/cffLib/specializer.py +927 -0
- fontTools/cffLib/transforms.py +495 -0
- fontTools/cffLib/width.py +210 -0
- fontTools/colorLib/__init__.py +0 -0
- fontTools/colorLib/builder.py +664 -0
- fontTools/colorLib/errors.py +2 -0
- fontTools/colorLib/geometry.py +143 -0
- fontTools/colorLib/table_builder.py +223 -0
- fontTools/colorLib/unbuilder.py +81 -0
- fontTools/config/__init__.py +90 -0
- fontTools/cu2qu/__init__.py +15 -0
- fontTools/cu2qu/__main__.py +6 -0
- fontTools/cu2qu/benchmark.py +54 -0
- fontTools/cu2qu/cli.py +198 -0
- fontTools/cu2qu/cu2qu.c +15817 -0
- fontTools/cu2qu/cu2qu.cp311-win32.pyd +0 -0
- fontTools/cu2qu/cu2qu.py +563 -0
- fontTools/cu2qu/errors.py +77 -0
- fontTools/cu2qu/ufo.py +363 -0
- fontTools/designspaceLib/__init__.py +3343 -0
- fontTools/designspaceLib/__main__.py +6 -0
- fontTools/designspaceLib/split.py +475 -0
- fontTools/designspaceLib/statNames.py +260 -0
- fontTools/designspaceLib/types.py +147 -0
- fontTools/encodings/MacRoman.py +258 -0
- fontTools/encodings/StandardEncoding.py +258 -0
- fontTools/encodings/__init__.py +1 -0
- fontTools/encodings/codecs.py +135 -0
- fontTools/feaLib/__init__.py +4 -0
- fontTools/feaLib/__main__.py +78 -0
- fontTools/feaLib/ast.py +2143 -0
- fontTools/feaLib/builder.py +1814 -0
- fontTools/feaLib/error.py +22 -0
- fontTools/feaLib/lexer.c +17029 -0
- fontTools/feaLib/lexer.cp311-win32.pyd +0 -0
- fontTools/feaLib/lexer.py +287 -0
- fontTools/feaLib/location.py +12 -0
- fontTools/feaLib/lookupDebugInfo.py +12 -0
- fontTools/feaLib/parser.py +2394 -0
- fontTools/feaLib/variableScalar.py +118 -0
- fontTools/fontBuilder.py +1014 -0
- fontTools/help.py +36 -0
- fontTools/merge/__init__.py +248 -0
- fontTools/merge/__main__.py +6 -0
- fontTools/merge/base.py +81 -0
- fontTools/merge/cmap.py +173 -0
- fontTools/merge/layout.py +526 -0
- fontTools/merge/options.py +85 -0
- fontTools/merge/tables.py +352 -0
- fontTools/merge/unicode.py +78 -0
- fontTools/merge/util.py +143 -0
- fontTools/misc/__init__.py +1 -0
- fontTools/misc/arrayTools.py +424 -0
- fontTools/misc/bezierTools.c +39731 -0
- fontTools/misc/bezierTools.cp311-win32.pyd +0 -0
- fontTools/misc/bezierTools.py +1500 -0
- fontTools/misc/classifyTools.py +170 -0
- fontTools/misc/cliTools.py +53 -0
- fontTools/misc/configTools.py +349 -0
- fontTools/misc/cython.py +27 -0
- fontTools/misc/dictTools.py +83 -0
- fontTools/misc/eexec.py +119 -0
- fontTools/misc/encodingTools.py +72 -0
- fontTools/misc/enumTools.py +23 -0
- fontTools/misc/etree.py +456 -0
- fontTools/misc/filenames.py +245 -0
- fontTools/misc/filesystem/__init__.py +68 -0
- fontTools/misc/filesystem/_base.py +134 -0
- fontTools/misc/filesystem/_copy.py +45 -0
- fontTools/misc/filesystem/_errors.py +54 -0
- fontTools/misc/filesystem/_info.py +75 -0
- fontTools/misc/filesystem/_osfs.py +164 -0
- fontTools/misc/filesystem/_path.py +67 -0
- fontTools/misc/filesystem/_subfs.py +92 -0
- fontTools/misc/filesystem/_tempfs.py +34 -0
- fontTools/misc/filesystem/_tools.py +34 -0
- fontTools/misc/filesystem/_walk.py +55 -0
- fontTools/misc/filesystem/_zipfs.py +204 -0
- fontTools/misc/fixedTools.py +253 -0
- fontTools/misc/intTools.py +25 -0
- fontTools/misc/iterTools.py +12 -0
- fontTools/misc/lazyTools.py +42 -0
- fontTools/misc/loggingTools.py +543 -0
- fontTools/misc/macCreatorType.py +56 -0
- fontTools/misc/macRes.py +261 -0
- fontTools/misc/plistlib/__init__.py +681 -0
- fontTools/misc/plistlib/py.typed +0 -0
- fontTools/misc/psCharStrings.py +1511 -0
- fontTools/misc/psLib.py +398 -0
- fontTools/misc/psOperators.py +572 -0
- fontTools/misc/py23.py +96 -0
- fontTools/misc/roundTools.py +110 -0
- fontTools/misc/sstruct.py +227 -0
- fontTools/misc/symfont.py +242 -0
- fontTools/misc/testTools.py +233 -0
- fontTools/misc/textTools.py +156 -0
- fontTools/misc/timeTools.py +88 -0
- fontTools/misc/transform.py +516 -0
- fontTools/misc/treeTools.py +45 -0
- fontTools/misc/vector.py +147 -0
- fontTools/misc/visitor.py +158 -0
- fontTools/misc/xmlReader.py +188 -0
- fontTools/misc/xmlWriter.py +231 -0
- fontTools/mtiLib/__init__.py +1400 -0
- fontTools/mtiLib/__main__.py +5 -0
- fontTools/otlLib/__init__.py +1 -0
- fontTools/otlLib/builder.py +3465 -0
- fontTools/otlLib/error.py +11 -0
- fontTools/otlLib/maxContextCalc.py +96 -0
- fontTools/otlLib/optimize/__init__.py +53 -0
- fontTools/otlLib/optimize/__main__.py +6 -0
- fontTools/otlLib/optimize/gpos.py +439 -0
- fontTools/pens/__init__.py +1 -0
- fontTools/pens/areaPen.py +52 -0
- fontTools/pens/basePen.py +475 -0
- fontTools/pens/boundsPen.py +98 -0
- fontTools/pens/cairoPen.py +26 -0
- fontTools/pens/cocoaPen.py +26 -0
- fontTools/pens/cu2quPen.py +325 -0
- fontTools/pens/explicitClosingLinePen.py +101 -0
- fontTools/pens/filterPen.py +433 -0
- fontTools/pens/freetypePen.py +462 -0
- fontTools/pens/hashPointPen.py +89 -0
- fontTools/pens/momentsPen.c +13378 -0
- fontTools/pens/momentsPen.cp311-win32.pyd +0 -0
- fontTools/pens/momentsPen.py +879 -0
- fontTools/pens/perimeterPen.py +69 -0
- fontTools/pens/pointInsidePen.py +192 -0
- fontTools/pens/pointPen.py +643 -0
- fontTools/pens/qtPen.py +29 -0
- fontTools/pens/qu2cuPen.py +105 -0
- fontTools/pens/quartzPen.py +43 -0
- fontTools/pens/recordingPen.py +335 -0
- fontTools/pens/reportLabPen.py +79 -0
- fontTools/pens/reverseContourPen.py +96 -0
- fontTools/pens/roundingPen.py +130 -0
- fontTools/pens/statisticsPen.py +312 -0
- fontTools/pens/svgPathPen.py +310 -0
- fontTools/pens/t2CharStringPen.py +88 -0
- fontTools/pens/teePen.py +55 -0
- fontTools/pens/transformPen.py +115 -0
- fontTools/pens/ttGlyphPen.py +335 -0
- fontTools/pens/wxPen.py +29 -0
- fontTools/qu2cu/__init__.py +15 -0
- fontTools/qu2cu/__main__.py +7 -0
- fontTools/qu2cu/benchmark.py +56 -0
- fontTools/qu2cu/cli.py +125 -0
- fontTools/qu2cu/qu2cu.c +16682 -0
- fontTools/qu2cu/qu2cu.cp311-win32.pyd +0 -0
- fontTools/qu2cu/qu2cu.py +405 -0
- fontTools/subset/__init__.py +4096 -0
- fontTools/subset/__main__.py +6 -0
- fontTools/subset/cff.py +184 -0
- fontTools/subset/svg.py +253 -0
- fontTools/subset/util.py +25 -0
- fontTools/svgLib/__init__.py +3 -0
- fontTools/svgLib/path/__init__.py +65 -0
- fontTools/svgLib/path/arc.py +154 -0
- fontTools/svgLib/path/parser.py +322 -0
- fontTools/svgLib/path/shapes.py +183 -0
- fontTools/t1Lib/__init__.py +648 -0
- fontTools/tfmLib.py +460 -0
- fontTools/ttLib/__init__.py +30 -0
- fontTools/ttLib/__main__.py +148 -0
- fontTools/ttLib/macUtils.py +54 -0
- fontTools/ttLib/removeOverlaps.py +395 -0
- fontTools/ttLib/reorderGlyphs.py +285 -0
- fontTools/ttLib/scaleUpem.py +436 -0
- fontTools/ttLib/sfnt.py +661 -0
- fontTools/ttLib/standardGlyphOrder.py +271 -0
- fontTools/ttLib/tables/B_A_S_E_.py +14 -0
- fontTools/ttLib/tables/BitmapGlyphMetrics.py +64 -0
- fontTools/ttLib/tables/C_B_D_T_.py +113 -0
- fontTools/ttLib/tables/C_B_L_C_.py +19 -0
- fontTools/ttLib/tables/C_F_F_.py +61 -0
- fontTools/ttLib/tables/C_F_F__2.py +26 -0
- fontTools/ttLib/tables/C_O_L_R_.py +165 -0
- fontTools/ttLib/tables/C_P_A_L_.py +305 -0
- fontTools/ttLib/tables/D_S_I_G_.py +158 -0
- fontTools/ttLib/tables/D__e_b_g.py +35 -0
- fontTools/ttLib/tables/DefaultTable.py +49 -0
- fontTools/ttLib/tables/E_B_D_T_.py +835 -0
- fontTools/ttLib/tables/E_B_L_C_.py +718 -0
- fontTools/ttLib/tables/F_F_T_M_.py +52 -0
- fontTools/ttLib/tables/F__e_a_t.py +149 -0
- fontTools/ttLib/tables/G_D_E_F_.py +13 -0
- fontTools/ttLib/tables/G_M_A_P_.py +148 -0
- fontTools/ttLib/tables/G_P_K_G_.py +133 -0
- fontTools/ttLib/tables/G_P_O_S_.py +14 -0
- fontTools/ttLib/tables/G_S_U_B_.py +13 -0
- fontTools/ttLib/tables/G_V_A_R_.py +5 -0
- fontTools/ttLib/tables/G__l_a_t.py +235 -0
- fontTools/ttLib/tables/G__l_o_c.py +85 -0
- fontTools/ttLib/tables/H_V_A_R_.py +13 -0
- fontTools/ttLib/tables/J_S_T_F_.py +13 -0
- fontTools/ttLib/tables/L_T_S_H_.py +58 -0
- fontTools/ttLib/tables/M_A_T_H_.py +13 -0
- fontTools/ttLib/tables/M_E_T_A_.py +352 -0
- fontTools/ttLib/tables/M_V_A_R_.py +13 -0
- fontTools/ttLib/tables/O_S_2f_2.py +752 -0
- fontTools/ttLib/tables/S_I_N_G_.py +99 -0
- fontTools/ttLib/tables/S_T_A_T_.py +15 -0
- fontTools/ttLib/tables/S_V_G_.py +223 -0
- fontTools/ttLib/tables/S__i_l_f.py +1040 -0
- fontTools/ttLib/tables/S__i_l_l.py +92 -0
- fontTools/ttLib/tables/T_S_I_B_.py +13 -0
- fontTools/ttLib/tables/T_S_I_C_.py +14 -0
- fontTools/ttLib/tables/T_S_I_D_.py +13 -0
- fontTools/ttLib/tables/T_S_I_J_.py +13 -0
- fontTools/ttLib/tables/T_S_I_P_.py +13 -0
- fontTools/ttLib/tables/T_S_I_S_.py +13 -0
- fontTools/ttLib/tables/T_S_I_V_.py +26 -0
- fontTools/ttLib/tables/T_S_I__0.py +70 -0
- fontTools/ttLib/tables/T_S_I__1.py +163 -0
- fontTools/ttLib/tables/T_S_I__2.py +17 -0
- fontTools/ttLib/tables/T_S_I__3.py +22 -0
- fontTools/ttLib/tables/T_S_I__5.py +60 -0
- fontTools/ttLib/tables/T_T_F_A_.py +14 -0
- fontTools/ttLib/tables/TupleVariation.py +884 -0
- fontTools/ttLib/tables/V_A_R_C_.py +12 -0
- fontTools/ttLib/tables/V_D_M_X_.py +249 -0
- fontTools/ttLib/tables/V_O_R_G_.py +165 -0
- fontTools/ttLib/tables/V_V_A_R_.py +13 -0
- fontTools/ttLib/tables/__init__.py +98 -0
- fontTools/ttLib/tables/_a_n_k_r.py +15 -0
- fontTools/ttLib/tables/_a_v_a_r.py +193 -0
- fontTools/ttLib/tables/_b_s_l_n.py +15 -0
- fontTools/ttLib/tables/_c_i_d_g.py +24 -0
- fontTools/ttLib/tables/_c_m_a_p.py +1591 -0
- fontTools/ttLib/tables/_c_v_a_r.py +94 -0
- fontTools/ttLib/tables/_c_v_t.py +56 -0
- fontTools/ttLib/tables/_f_e_a_t.py +15 -0
- fontTools/ttLib/tables/_f_p_g_m.py +62 -0
- fontTools/ttLib/tables/_f_v_a_r.py +261 -0
- fontTools/ttLib/tables/_g_a_s_p.py +63 -0
- fontTools/ttLib/tables/_g_c_i_d.py +13 -0
- fontTools/ttLib/tables/_g_l_y_f.py +2311 -0
- fontTools/ttLib/tables/_g_v_a_r.py +340 -0
- fontTools/ttLib/tables/_h_d_m_x.py +127 -0
- fontTools/ttLib/tables/_h_e_a_d.py +130 -0
- fontTools/ttLib/tables/_h_h_e_a.py +147 -0
- fontTools/ttLib/tables/_h_m_t_x.py +164 -0
- fontTools/ttLib/tables/_k_e_r_n.py +289 -0
- fontTools/ttLib/tables/_l_c_a_r.py +13 -0
- fontTools/ttLib/tables/_l_o_c_a.py +70 -0
- fontTools/ttLib/tables/_l_t_a_g.py +72 -0
- fontTools/ttLib/tables/_m_a_x_p.py +147 -0
- fontTools/ttLib/tables/_m_e_t_a.py +112 -0
- fontTools/ttLib/tables/_m_o_r_t.py +14 -0
- fontTools/ttLib/tables/_m_o_r_x.py +15 -0
- fontTools/ttLib/tables/_n_a_m_e.py +1242 -0
- fontTools/ttLib/tables/_o_p_b_d.py +14 -0
- fontTools/ttLib/tables/_p_o_s_t.py +319 -0
- fontTools/ttLib/tables/_p_r_e_p.py +16 -0
- fontTools/ttLib/tables/_p_r_o_p.py +12 -0
- fontTools/ttLib/tables/_s_b_i_x.py +129 -0
- fontTools/ttLib/tables/_t_r_a_k.py +332 -0
- fontTools/ttLib/tables/_v_h_e_a.py +139 -0
- fontTools/ttLib/tables/_v_m_t_x.py +19 -0
- fontTools/ttLib/tables/asciiTable.py +20 -0
- fontTools/ttLib/tables/grUtils.py +92 -0
- fontTools/ttLib/tables/otBase.py +1458 -0
- fontTools/ttLib/tables/otConverters.py +2068 -0
- fontTools/ttLib/tables/otData.py +6400 -0
- fontTools/ttLib/tables/otTables.py +2703 -0
- fontTools/ttLib/tables/otTraverse.py +163 -0
- fontTools/ttLib/tables/sbixGlyph.py +149 -0
- fontTools/ttLib/tables/sbixStrike.py +177 -0
- fontTools/ttLib/tables/table_API_readme.txt +91 -0
- fontTools/ttLib/tables/ttProgram.py +594 -0
- fontTools/ttLib/ttCollection.py +125 -0
- fontTools/ttLib/ttFont.py +1148 -0
- fontTools/ttLib/ttGlyphSet.py +490 -0
- fontTools/ttLib/ttVisitor.py +32 -0
- fontTools/ttLib/woff2.py +1680 -0
- fontTools/ttx.py +479 -0
- fontTools/ufoLib/__init__.py +2575 -0
- fontTools/ufoLib/converters.py +407 -0
- fontTools/ufoLib/errors.py +30 -0
- fontTools/ufoLib/etree.py +6 -0
- fontTools/ufoLib/filenames.py +356 -0
- fontTools/ufoLib/glifLib.py +2120 -0
- fontTools/ufoLib/kerning.py +141 -0
- fontTools/ufoLib/plistlib.py +47 -0
- fontTools/ufoLib/pointPen.py +6 -0
- fontTools/ufoLib/utils.py +107 -0
- fontTools/ufoLib/validators.py +1208 -0
- fontTools/unicode.py +50 -0
- fontTools/unicodedata/Blocks.py +817 -0
- fontTools/unicodedata/Mirrored.py +446 -0
- fontTools/unicodedata/OTTags.py +50 -0
- fontTools/unicodedata/ScriptExtensions.py +832 -0
- fontTools/unicodedata/Scripts.py +3639 -0
- fontTools/unicodedata/__init__.py +306 -0
- fontTools/varLib/__init__.py +1600 -0
- fontTools/varLib/__main__.py +6 -0
- fontTools/varLib/avar/__init__.py +0 -0
- fontTools/varLib/avar/__main__.py +72 -0
- fontTools/varLib/avar/build.py +79 -0
- fontTools/varLib/avar/map.py +108 -0
- fontTools/varLib/avar/plan.py +1004 -0
- fontTools/varLib/avar/unbuild.py +271 -0
- fontTools/varLib/avarPlanner.py +8 -0
- fontTools/varLib/builder.py +215 -0
- fontTools/varLib/cff.py +631 -0
- fontTools/varLib/errors.py +219 -0
- fontTools/varLib/featureVars.py +703 -0
- fontTools/varLib/hvar.py +113 -0
- fontTools/varLib/instancer/__init__.py +2052 -0
- fontTools/varLib/instancer/__main__.py +5 -0
- fontTools/varLib/instancer/featureVars.py +190 -0
- fontTools/varLib/instancer/names.py +388 -0
- fontTools/varLib/instancer/solver.py +309 -0
- fontTools/varLib/interpolatable.py +1209 -0
- fontTools/varLib/interpolatableHelpers.py +399 -0
- fontTools/varLib/interpolatablePlot.py +1269 -0
- fontTools/varLib/interpolatableTestContourOrder.py +82 -0
- fontTools/varLib/interpolatableTestStartingPoint.py +107 -0
- fontTools/varLib/interpolate_layout.py +124 -0
- fontTools/varLib/iup.c +19815 -0
- fontTools/varLib/iup.cp311-win32.pyd +0 -0
- fontTools/varLib/iup.py +490 -0
- fontTools/varLib/merger.py +1717 -0
- fontTools/varLib/models.py +642 -0
- fontTools/varLib/multiVarStore.py +253 -0
- fontTools/varLib/mutator.py +529 -0
- fontTools/varLib/mvar.py +40 -0
- fontTools/varLib/plot.py +238 -0
- fontTools/varLib/stat.py +149 -0
- fontTools/varLib/varStore.py +739 -0
- fontTools/voltLib/__init__.py +5 -0
- fontTools/voltLib/__main__.py +206 -0
- fontTools/voltLib/ast.py +452 -0
- fontTools/voltLib/error.py +12 -0
- fontTools/voltLib/lexer.py +99 -0
- fontTools/voltLib/parser.py +664 -0
- fontTools/voltLib/voltToFea.py +911 -0
- fonttools-4.60.2.data/data/share/man/man1/ttx.1 +225 -0
- fonttools-4.60.2.dist-info/METADATA +2250 -0
- fonttools-4.60.2.dist-info/RECORD +353 -0
- fonttools-4.60.2.dist-info/WHEEL +5 -0
- fonttools-4.60.2.dist-info/entry_points.txt +5 -0
- fonttools-4.60.2.dist-info/licenses/LICENSE +21 -0
- fonttools-4.60.2.dist-info/licenses/LICENSE.external +388 -0
- fonttools-4.60.2.dist-info/top_level.txt +1 -0
fontTools/misc/eexec.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""
|
|
2
|
+
PostScript Type 1 fonts make use of two types of encryption: charstring
|
|
3
|
+
encryption and ``eexec`` encryption. Charstring encryption is used for
|
|
4
|
+
the charstrings themselves, while ``eexec`` is used to encrypt larger
|
|
5
|
+
sections of the font program, such as the ``Private`` and ``CharStrings``
|
|
6
|
+
dictionaries. Despite the different names, the algorithm is the same,
|
|
7
|
+
although ``eexec`` encryption uses a fixed initial key R=55665.
|
|
8
|
+
|
|
9
|
+
The algorithm uses cipher feedback, meaning that the ciphertext is used
|
|
10
|
+
to modify the key. Because of this, the routines in this module return
|
|
11
|
+
the new key at the end of the operation.
|
|
12
|
+
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from fontTools.misc.textTools import bytechr, bytesjoin, byteord
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _decryptChar(cipher, R):
|
|
19
|
+
cipher = byteord(cipher)
|
|
20
|
+
plain = ((cipher ^ (R >> 8))) & 0xFF
|
|
21
|
+
R = ((cipher + R) * 52845 + 22719) & 0xFFFF
|
|
22
|
+
return bytechr(plain), R
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _encryptChar(plain, R):
|
|
26
|
+
plain = byteord(plain)
|
|
27
|
+
cipher = ((plain ^ (R >> 8))) & 0xFF
|
|
28
|
+
R = ((cipher + R) * 52845 + 22719) & 0xFFFF
|
|
29
|
+
return bytechr(cipher), R
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def decrypt(cipherstring, R):
|
|
33
|
+
r"""
|
|
34
|
+
Decrypts a string using the Type 1 encryption algorithm.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
cipherstring: String of ciphertext.
|
|
38
|
+
R: Initial key.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
decryptedStr: Plaintext string.
|
|
42
|
+
R: Output key for subsequent decryptions.
|
|
43
|
+
|
|
44
|
+
Examples::
|
|
45
|
+
|
|
46
|
+
>>> testStr = b"\0\0asdadads asds\265"
|
|
47
|
+
>>> decryptedStr, R = decrypt(testStr, 12321)
|
|
48
|
+
>>> decryptedStr == b'0d\nh\x15\xe8\xc4\xb2\x15\x1d\x108\x1a<6\xa1'
|
|
49
|
+
True
|
|
50
|
+
>>> R == 36142
|
|
51
|
+
True
|
|
52
|
+
"""
|
|
53
|
+
plainList = []
|
|
54
|
+
for cipher in cipherstring:
|
|
55
|
+
plain, R = _decryptChar(cipher, R)
|
|
56
|
+
plainList.append(plain)
|
|
57
|
+
plainstring = bytesjoin(plainList)
|
|
58
|
+
return plainstring, int(R)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def encrypt(plainstring, R):
|
|
62
|
+
r"""
|
|
63
|
+
Encrypts a string using the Type 1 encryption algorithm.
|
|
64
|
+
|
|
65
|
+
Note that the algorithm as described in the Type 1 specification requires the
|
|
66
|
+
plaintext to be prefixed with a number of random bytes. (For ``eexec`` the
|
|
67
|
+
number of random bytes is set to 4.) This routine does *not* add the random
|
|
68
|
+
prefix to its input.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
plainstring: String of plaintext.
|
|
72
|
+
R: Initial key.
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
cipherstring: Ciphertext string.
|
|
76
|
+
R: Output key for subsequent encryptions.
|
|
77
|
+
|
|
78
|
+
Examples::
|
|
79
|
+
|
|
80
|
+
>>> testStr = b"\0\0asdadads asds\265"
|
|
81
|
+
>>> decryptedStr, R = decrypt(testStr, 12321)
|
|
82
|
+
>>> decryptedStr == b'0d\nh\x15\xe8\xc4\xb2\x15\x1d\x108\x1a<6\xa1'
|
|
83
|
+
True
|
|
84
|
+
>>> R == 36142
|
|
85
|
+
True
|
|
86
|
+
|
|
87
|
+
>>> testStr = b'0d\nh\x15\xe8\xc4\xb2\x15\x1d\x108\x1a<6\xa1'
|
|
88
|
+
>>> encryptedStr, R = encrypt(testStr, 12321)
|
|
89
|
+
>>> encryptedStr == b"\0\0asdadads asds\265"
|
|
90
|
+
True
|
|
91
|
+
>>> R == 36142
|
|
92
|
+
True
|
|
93
|
+
"""
|
|
94
|
+
cipherList = []
|
|
95
|
+
for plain in plainstring:
|
|
96
|
+
cipher, R = _encryptChar(plain, R)
|
|
97
|
+
cipherList.append(cipher)
|
|
98
|
+
cipherstring = bytesjoin(cipherList)
|
|
99
|
+
return cipherstring, int(R)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def hexString(s):
|
|
103
|
+
import binascii
|
|
104
|
+
|
|
105
|
+
return binascii.hexlify(s)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def deHexString(h):
|
|
109
|
+
import binascii
|
|
110
|
+
|
|
111
|
+
h = bytesjoin(h.split())
|
|
112
|
+
return binascii.unhexlify(h)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
if __name__ == "__main__":
|
|
116
|
+
import sys
|
|
117
|
+
import doctest
|
|
118
|
+
|
|
119
|
+
sys.exit(doctest.testmod().failed)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""fontTools.misc.encodingTools.py -- tools for working with OpenType encodings.
|
|
2
|
+
"""
|
|
3
|
+
|
|
4
|
+
import fontTools.encodings.codecs
|
|
5
|
+
|
|
6
|
+
# Map keyed by platformID, then platEncID, then possibly langID
|
|
7
|
+
_encodingMap = {
|
|
8
|
+
0: { # Unicode
|
|
9
|
+
0: "utf_16_be",
|
|
10
|
+
1: "utf_16_be",
|
|
11
|
+
2: "utf_16_be",
|
|
12
|
+
3: "utf_16_be",
|
|
13
|
+
4: "utf_16_be",
|
|
14
|
+
5: "utf_16_be",
|
|
15
|
+
6: "utf_16_be",
|
|
16
|
+
},
|
|
17
|
+
1: { # Macintosh
|
|
18
|
+
# See
|
|
19
|
+
# https://github.com/fonttools/fonttools/issues/236
|
|
20
|
+
0: { # Macintosh, platEncID==0, keyed by langID
|
|
21
|
+
15: "mac_iceland",
|
|
22
|
+
17: "mac_turkish",
|
|
23
|
+
18: "mac_croatian",
|
|
24
|
+
24: "mac_latin2",
|
|
25
|
+
25: "mac_latin2",
|
|
26
|
+
26: "mac_latin2",
|
|
27
|
+
27: "mac_latin2",
|
|
28
|
+
28: "mac_latin2",
|
|
29
|
+
36: "mac_latin2",
|
|
30
|
+
37: "mac_romanian",
|
|
31
|
+
38: "mac_latin2",
|
|
32
|
+
39: "mac_latin2",
|
|
33
|
+
40: "mac_latin2",
|
|
34
|
+
Ellipsis: "mac_roman", # Other
|
|
35
|
+
},
|
|
36
|
+
1: "x_mac_japanese_ttx",
|
|
37
|
+
2: "x_mac_trad_chinese_ttx",
|
|
38
|
+
3: "x_mac_korean_ttx",
|
|
39
|
+
6: "mac_greek",
|
|
40
|
+
7: "mac_cyrillic",
|
|
41
|
+
25: "x_mac_simp_chinese_ttx",
|
|
42
|
+
29: "mac_latin2",
|
|
43
|
+
35: "mac_turkish",
|
|
44
|
+
37: "mac_iceland",
|
|
45
|
+
},
|
|
46
|
+
2: { # ISO
|
|
47
|
+
0: "ascii",
|
|
48
|
+
1: "utf_16_be",
|
|
49
|
+
2: "latin1",
|
|
50
|
+
},
|
|
51
|
+
3: { # Microsoft
|
|
52
|
+
0: "utf_16_be",
|
|
53
|
+
1: "utf_16_be",
|
|
54
|
+
2: "shift_jis",
|
|
55
|
+
3: "gb2312",
|
|
56
|
+
4: "big5",
|
|
57
|
+
5: "euc_kr",
|
|
58
|
+
6: "johab",
|
|
59
|
+
10: "utf_16_be",
|
|
60
|
+
},
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def getEncoding(platformID, platEncID, langID, default=None):
|
|
65
|
+
"""Returns the Python encoding name for OpenType platformID/encodingID/langID
|
|
66
|
+
triplet. If encoding for these values is not known, by default None is
|
|
67
|
+
returned. That can be overriden by passing a value to the default argument.
|
|
68
|
+
"""
|
|
69
|
+
encoding = _encodingMap.get(platformID, {}).get(platEncID, default)
|
|
70
|
+
if isinstance(encoding, dict):
|
|
71
|
+
encoding = encoding.get(langID, encoding[Ellipsis])
|
|
72
|
+
return encoding
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Enum-related utilities, including backports for older Python versions."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from enum import Enum
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
__all__ = ["StrEnum"]
|
|
9
|
+
|
|
10
|
+
# StrEnum is only available in Python 3.11+
|
|
11
|
+
try:
|
|
12
|
+
from enum import StrEnum
|
|
13
|
+
except ImportError:
|
|
14
|
+
|
|
15
|
+
class StrEnum(str, Enum):
|
|
16
|
+
"""
|
|
17
|
+
Minimal backport of Python 3.11's StrEnum for older versions.
|
|
18
|
+
|
|
19
|
+
An Enum where all members are also strings.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __str__(self) -> str:
|
|
23
|
+
return self.value
|
fontTools/misc/etree.py
ADDED
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
"""Shim module exporting the same ElementTree API for lxml and
|
|
2
|
+
xml.etree backends.
|
|
3
|
+
|
|
4
|
+
When lxml is installed, it is automatically preferred over the built-in
|
|
5
|
+
xml.etree module.
|
|
6
|
+
On Python 2.7, the cElementTree module is preferred over the pure-python
|
|
7
|
+
ElementTree module.
|
|
8
|
+
|
|
9
|
+
Besides exporting a unified interface, this also defines extra functions
|
|
10
|
+
or subclasses built-in ElementTree classes to add features that are
|
|
11
|
+
only availble in lxml, like OrderedDict for attributes, pretty_print and
|
|
12
|
+
iterwalk.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from fontTools.misc.textTools import tostr
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
XML_DECLARATION = """<?xml version='1.0' encoding='%s'?>"""
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
# public symbols
|
|
22
|
+
"Comment",
|
|
23
|
+
"dump",
|
|
24
|
+
"Element",
|
|
25
|
+
"ElementTree",
|
|
26
|
+
"fromstring",
|
|
27
|
+
"fromstringlist",
|
|
28
|
+
"iselement",
|
|
29
|
+
"iterparse",
|
|
30
|
+
"parse",
|
|
31
|
+
"ParseError",
|
|
32
|
+
"PI",
|
|
33
|
+
"ProcessingInstruction",
|
|
34
|
+
"QName",
|
|
35
|
+
"SubElement",
|
|
36
|
+
"tostring",
|
|
37
|
+
"tostringlist",
|
|
38
|
+
"TreeBuilder",
|
|
39
|
+
"XML",
|
|
40
|
+
"XMLParser",
|
|
41
|
+
"register_namespace",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
try:
|
|
45
|
+
from lxml.etree import *
|
|
46
|
+
|
|
47
|
+
_have_lxml = True
|
|
48
|
+
except ImportError:
|
|
49
|
+
try:
|
|
50
|
+
from xml.etree.cElementTree import *
|
|
51
|
+
|
|
52
|
+
# the cElementTree version of XML function doesn't support
|
|
53
|
+
# the optional 'parser' keyword argument
|
|
54
|
+
from xml.etree.ElementTree import XML
|
|
55
|
+
except ImportError: # pragma: no cover
|
|
56
|
+
from xml.etree.ElementTree import *
|
|
57
|
+
_have_lxml = False
|
|
58
|
+
|
|
59
|
+
_Attrib = dict
|
|
60
|
+
|
|
61
|
+
if isinstance(Element, type):
|
|
62
|
+
_Element = Element
|
|
63
|
+
else:
|
|
64
|
+
# in py27, cElementTree.Element cannot be subclassed, so
|
|
65
|
+
# we need to import the pure-python class
|
|
66
|
+
from xml.etree.ElementTree import Element as _Element
|
|
67
|
+
|
|
68
|
+
class Element(_Element):
|
|
69
|
+
"""Element subclass that keeps the order of attributes."""
|
|
70
|
+
|
|
71
|
+
def __init__(self, tag, attrib=_Attrib(), **extra):
|
|
72
|
+
super(Element, self).__init__(tag)
|
|
73
|
+
self.attrib = _Attrib()
|
|
74
|
+
if attrib:
|
|
75
|
+
self.attrib.update(attrib)
|
|
76
|
+
if extra:
|
|
77
|
+
self.attrib.update(extra)
|
|
78
|
+
|
|
79
|
+
def SubElement(parent, tag, attrib=_Attrib(), **extra):
|
|
80
|
+
"""Must override SubElement as well otherwise _elementtree.SubElement
|
|
81
|
+
fails if 'parent' is a subclass of Element object.
|
|
82
|
+
"""
|
|
83
|
+
element = parent.__class__(tag, attrib, **extra)
|
|
84
|
+
parent.append(element)
|
|
85
|
+
return element
|
|
86
|
+
|
|
87
|
+
def _iterwalk(element, events, tag):
|
|
88
|
+
include = tag is None or element.tag == tag
|
|
89
|
+
if include and "start" in events:
|
|
90
|
+
yield ("start", element)
|
|
91
|
+
for e in element:
|
|
92
|
+
for item in _iterwalk(e, events, tag):
|
|
93
|
+
yield item
|
|
94
|
+
if include:
|
|
95
|
+
yield ("end", element)
|
|
96
|
+
|
|
97
|
+
def iterwalk(element_or_tree, events=("end",), tag=None):
|
|
98
|
+
"""A tree walker that generates events from an existing tree as
|
|
99
|
+
if it was parsing XML data with iterparse().
|
|
100
|
+
Drop-in replacement for lxml.etree.iterwalk.
|
|
101
|
+
"""
|
|
102
|
+
if iselement(element_or_tree):
|
|
103
|
+
element = element_or_tree
|
|
104
|
+
else:
|
|
105
|
+
element = element_or_tree.getroot()
|
|
106
|
+
if tag == "*":
|
|
107
|
+
tag = None
|
|
108
|
+
for item in _iterwalk(element, events, tag):
|
|
109
|
+
yield item
|
|
110
|
+
|
|
111
|
+
_ElementTree = ElementTree
|
|
112
|
+
|
|
113
|
+
class ElementTree(_ElementTree):
|
|
114
|
+
"""ElementTree subclass that adds 'pretty_print' and 'doctype'
|
|
115
|
+
arguments to the 'write' method.
|
|
116
|
+
Currently these are only supported for the default XML serialization
|
|
117
|
+
'method', and not also for "html" or "text", for these are delegated
|
|
118
|
+
to the base class.
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
def write(
|
|
122
|
+
self,
|
|
123
|
+
file_or_filename,
|
|
124
|
+
encoding=None,
|
|
125
|
+
xml_declaration=False,
|
|
126
|
+
method=None,
|
|
127
|
+
doctype=None,
|
|
128
|
+
pretty_print=False,
|
|
129
|
+
):
|
|
130
|
+
if method and method != "xml":
|
|
131
|
+
# delegate to super-class
|
|
132
|
+
super(ElementTree, self).write(
|
|
133
|
+
file_or_filename,
|
|
134
|
+
encoding=encoding,
|
|
135
|
+
xml_declaration=xml_declaration,
|
|
136
|
+
method=method,
|
|
137
|
+
)
|
|
138
|
+
return
|
|
139
|
+
|
|
140
|
+
if encoding is not None and encoding.lower() == "unicode":
|
|
141
|
+
if xml_declaration:
|
|
142
|
+
raise ValueError(
|
|
143
|
+
"Serialisation to unicode must not request an XML declaration"
|
|
144
|
+
)
|
|
145
|
+
write_declaration = False
|
|
146
|
+
encoding = "unicode"
|
|
147
|
+
elif xml_declaration is None:
|
|
148
|
+
# by default, write an XML declaration only for non-standard encodings
|
|
149
|
+
write_declaration = encoding is not None and encoding.upper() not in (
|
|
150
|
+
"ASCII",
|
|
151
|
+
"UTF-8",
|
|
152
|
+
"UTF8",
|
|
153
|
+
"US-ASCII",
|
|
154
|
+
)
|
|
155
|
+
else:
|
|
156
|
+
write_declaration = xml_declaration
|
|
157
|
+
|
|
158
|
+
if encoding is None:
|
|
159
|
+
encoding = "ASCII"
|
|
160
|
+
|
|
161
|
+
if pretty_print:
|
|
162
|
+
# NOTE this will modify the tree in-place
|
|
163
|
+
_indent(self._root)
|
|
164
|
+
|
|
165
|
+
with _get_writer(file_or_filename, encoding) as write:
|
|
166
|
+
if write_declaration:
|
|
167
|
+
write(XML_DECLARATION % encoding.upper())
|
|
168
|
+
if pretty_print:
|
|
169
|
+
write("\n")
|
|
170
|
+
if doctype:
|
|
171
|
+
write(_tounicode(doctype))
|
|
172
|
+
if pretty_print:
|
|
173
|
+
write("\n")
|
|
174
|
+
|
|
175
|
+
qnames, namespaces = _namespaces(self._root)
|
|
176
|
+
_serialize_xml(write, self._root, qnames, namespaces)
|
|
177
|
+
|
|
178
|
+
import io
|
|
179
|
+
|
|
180
|
+
def tostring(
|
|
181
|
+
element,
|
|
182
|
+
encoding=None,
|
|
183
|
+
xml_declaration=None,
|
|
184
|
+
method=None,
|
|
185
|
+
doctype=None,
|
|
186
|
+
pretty_print=False,
|
|
187
|
+
):
|
|
188
|
+
"""Custom 'tostring' function that uses our ElementTree subclass, with
|
|
189
|
+
pretty_print support.
|
|
190
|
+
"""
|
|
191
|
+
stream = io.StringIO() if encoding == "unicode" else io.BytesIO()
|
|
192
|
+
ElementTree(element).write(
|
|
193
|
+
stream,
|
|
194
|
+
encoding=encoding,
|
|
195
|
+
xml_declaration=xml_declaration,
|
|
196
|
+
method=method,
|
|
197
|
+
doctype=doctype,
|
|
198
|
+
pretty_print=pretty_print,
|
|
199
|
+
)
|
|
200
|
+
return stream.getvalue()
|
|
201
|
+
|
|
202
|
+
# serialization support
|
|
203
|
+
|
|
204
|
+
import re
|
|
205
|
+
|
|
206
|
+
# Valid XML strings can include any Unicode character, excluding control
|
|
207
|
+
# characters, the surrogate blocks, FFFE, and FFFF:
|
|
208
|
+
# Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
|
|
209
|
+
# Here we reversed the pattern to match only the invalid characters.
|
|
210
|
+
_invalid_xml_string = re.compile(
|
|
211
|
+
"[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE-\uFFFF]"
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
def _tounicode(s):
|
|
215
|
+
"""Test if a string is valid user input and decode it to unicode string
|
|
216
|
+
using ASCII encoding if it's a bytes string.
|
|
217
|
+
Reject all bytes/unicode input that contains non-XML characters.
|
|
218
|
+
Reject all bytes input that contains non-ASCII characters.
|
|
219
|
+
"""
|
|
220
|
+
try:
|
|
221
|
+
s = tostr(s, encoding="ascii", errors="strict")
|
|
222
|
+
except UnicodeDecodeError:
|
|
223
|
+
raise ValueError(
|
|
224
|
+
"Bytes strings can only contain ASCII characters. "
|
|
225
|
+
"Use unicode strings for non-ASCII characters."
|
|
226
|
+
)
|
|
227
|
+
except AttributeError:
|
|
228
|
+
_raise_serialization_error(s)
|
|
229
|
+
if s and _invalid_xml_string.search(s):
|
|
230
|
+
raise ValueError(
|
|
231
|
+
"All strings must be XML compatible: Unicode or ASCII, "
|
|
232
|
+
"no NULL bytes or control characters"
|
|
233
|
+
)
|
|
234
|
+
return s
|
|
235
|
+
|
|
236
|
+
import contextlib
|
|
237
|
+
|
|
238
|
+
@contextlib.contextmanager
|
|
239
|
+
def _get_writer(file_or_filename, encoding):
|
|
240
|
+
# returns text write method and release all resources after using
|
|
241
|
+
try:
|
|
242
|
+
write = file_or_filename.write
|
|
243
|
+
except AttributeError:
|
|
244
|
+
# file_or_filename is a file name
|
|
245
|
+
f = open(
|
|
246
|
+
file_or_filename,
|
|
247
|
+
"w",
|
|
248
|
+
encoding="utf-8" if encoding == "unicode" else encoding,
|
|
249
|
+
errors="xmlcharrefreplace",
|
|
250
|
+
)
|
|
251
|
+
with f:
|
|
252
|
+
yield f.write
|
|
253
|
+
else:
|
|
254
|
+
# file_or_filename is a file-like object
|
|
255
|
+
# encoding determines if it is a text or binary writer
|
|
256
|
+
if encoding == "unicode":
|
|
257
|
+
# use a text writer as is
|
|
258
|
+
yield write
|
|
259
|
+
else:
|
|
260
|
+
# wrap a binary writer with TextIOWrapper
|
|
261
|
+
detach_buffer = False
|
|
262
|
+
if isinstance(file_or_filename, io.BufferedIOBase):
|
|
263
|
+
buf = file_or_filename
|
|
264
|
+
elif isinstance(file_or_filename, io.RawIOBase):
|
|
265
|
+
buf = io.BufferedWriter(file_or_filename)
|
|
266
|
+
detach_buffer = True
|
|
267
|
+
else:
|
|
268
|
+
# This is to handle passed objects that aren't in the
|
|
269
|
+
# IOBase hierarchy, but just have a write method
|
|
270
|
+
buf = io.BufferedIOBase()
|
|
271
|
+
buf.writable = lambda: True
|
|
272
|
+
buf.write = write
|
|
273
|
+
try:
|
|
274
|
+
# TextIOWrapper uses this methods to determine
|
|
275
|
+
# if BOM (for UTF-16, etc) should be added
|
|
276
|
+
buf.seekable = file_or_filename.seekable
|
|
277
|
+
buf.tell = file_or_filename.tell
|
|
278
|
+
except AttributeError:
|
|
279
|
+
pass
|
|
280
|
+
wrapper = io.TextIOWrapper(
|
|
281
|
+
buf,
|
|
282
|
+
encoding=encoding,
|
|
283
|
+
errors="xmlcharrefreplace",
|
|
284
|
+
newline="\n",
|
|
285
|
+
)
|
|
286
|
+
try:
|
|
287
|
+
yield wrapper.write
|
|
288
|
+
finally:
|
|
289
|
+
# Keep the original file open when the TextIOWrapper and
|
|
290
|
+
# the BufferedWriter are destroyed
|
|
291
|
+
wrapper.detach()
|
|
292
|
+
if detach_buffer:
|
|
293
|
+
buf.detach()
|
|
294
|
+
|
|
295
|
+
from xml.etree.ElementTree import _namespace_map
|
|
296
|
+
|
|
297
|
+
def _namespaces(elem):
|
|
298
|
+
# identify namespaces used in this tree
|
|
299
|
+
|
|
300
|
+
# maps qnames to *encoded* prefix:local names
|
|
301
|
+
qnames = {None: None}
|
|
302
|
+
|
|
303
|
+
# maps uri:s to prefixes
|
|
304
|
+
namespaces = {}
|
|
305
|
+
|
|
306
|
+
def add_qname(qname):
|
|
307
|
+
# calculate serialized qname representation
|
|
308
|
+
try:
|
|
309
|
+
qname = _tounicode(qname)
|
|
310
|
+
if qname[:1] == "{":
|
|
311
|
+
uri, tag = qname[1:].rsplit("}", 1)
|
|
312
|
+
prefix = namespaces.get(uri)
|
|
313
|
+
if prefix is None:
|
|
314
|
+
prefix = _namespace_map.get(uri)
|
|
315
|
+
if prefix is None:
|
|
316
|
+
prefix = "ns%d" % len(namespaces)
|
|
317
|
+
else:
|
|
318
|
+
prefix = _tounicode(prefix)
|
|
319
|
+
if prefix != "xml":
|
|
320
|
+
namespaces[uri] = prefix
|
|
321
|
+
if prefix:
|
|
322
|
+
qnames[qname] = "%s:%s" % (prefix, tag)
|
|
323
|
+
else:
|
|
324
|
+
qnames[qname] = tag # default element
|
|
325
|
+
else:
|
|
326
|
+
qnames[qname] = qname
|
|
327
|
+
except TypeError:
|
|
328
|
+
_raise_serialization_error(qname)
|
|
329
|
+
|
|
330
|
+
# populate qname and namespaces table
|
|
331
|
+
for elem in elem.iter():
|
|
332
|
+
tag = elem.tag
|
|
333
|
+
if isinstance(tag, QName):
|
|
334
|
+
if tag.text not in qnames:
|
|
335
|
+
add_qname(tag.text)
|
|
336
|
+
elif isinstance(tag, str):
|
|
337
|
+
if tag not in qnames:
|
|
338
|
+
add_qname(tag)
|
|
339
|
+
elif tag is not None and tag is not Comment and tag is not PI:
|
|
340
|
+
_raise_serialization_error(tag)
|
|
341
|
+
for key, value in elem.items():
|
|
342
|
+
if isinstance(key, QName):
|
|
343
|
+
key = key.text
|
|
344
|
+
if key not in qnames:
|
|
345
|
+
add_qname(key)
|
|
346
|
+
if isinstance(value, QName) and value.text not in qnames:
|
|
347
|
+
add_qname(value.text)
|
|
348
|
+
text = elem.text
|
|
349
|
+
if isinstance(text, QName) and text.text not in qnames:
|
|
350
|
+
add_qname(text.text)
|
|
351
|
+
return qnames, namespaces
|
|
352
|
+
|
|
353
|
+
def _serialize_xml(write, elem, qnames, namespaces, **kwargs):
|
|
354
|
+
tag = elem.tag
|
|
355
|
+
text = elem.text
|
|
356
|
+
if tag is Comment:
|
|
357
|
+
write("<!--%s-->" % _tounicode(text))
|
|
358
|
+
elif tag is ProcessingInstruction:
|
|
359
|
+
write("<?%s?>" % _tounicode(text))
|
|
360
|
+
else:
|
|
361
|
+
tag = qnames[_tounicode(tag) if tag is not None else None]
|
|
362
|
+
if tag is None:
|
|
363
|
+
if text:
|
|
364
|
+
write(_escape_cdata(text))
|
|
365
|
+
for e in elem:
|
|
366
|
+
_serialize_xml(write, e, qnames, None)
|
|
367
|
+
else:
|
|
368
|
+
write("<" + tag)
|
|
369
|
+
if namespaces:
|
|
370
|
+
for uri, prefix in sorted(
|
|
371
|
+
namespaces.items(), key=lambda x: x[1]
|
|
372
|
+
): # sort on prefix
|
|
373
|
+
if prefix:
|
|
374
|
+
prefix = ":" + prefix
|
|
375
|
+
write(' xmlns%s="%s"' % (prefix, _escape_attrib(uri)))
|
|
376
|
+
attrs = elem.attrib
|
|
377
|
+
if attrs:
|
|
378
|
+
# try to keep existing attrib order
|
|
379
|
+
if len(attrs) <= 1 or type(attrs) is _Attrib:
|
|
380
|
+
items = attrs.items()
|
|
381
|
+
else:
|
|
382
|
+
# if plain dict, use lexical order
|
|
383
|
+
items = sorted(attrs.items())
|
|
384
|
+
for k, v in items:
|
|
385
|
+
if isinstance(k, QName):
|
|
386
|
+
k = _tounicode(k.text)
|
|
387
|
+
else:
|
|
388
|
+
k = _tounicode(k)
|
|
389
|
+
if isinstance(v, QName):
|
|
390
|
+
v = qnames[_tounicode(v.text)]
|
|
391
|
+
else:
|
|
392
|
+
v = _escape_attrib(v)
|
|
393
|
+
write(' %s="%s"' % (qnames[k], v))
|
|
394
|
+
if text is not None or len(elem):
|
|
395
|
+
write(">")
|
|
396
|
+
if text:
|
|
397
|
+
write(_escape_cdata(text))
|
|
398
|
+
for e in elem:
|
|
399
|
+
_serialize_xml(write, e, qnames, None)
|
|
400
|
+
write("</" + tag + ">")
|
|
401
|
+
else:
|
|
402
|
+
write("/>")
|
|
403
|
+
if elem.tail:
|
|
404
|
+
write(_escape_cdata(elem.tail))
|
|
405
|
+
|
|
406
|
+
def _raise_serialization_error(text):
|
|
407
|
+
raise TypeError("cannot serialize %r (type %s)" % (text, type(text).__name__))
|
|
408
|
+
|
|
409
|
+
def _escape_cdata(text):
|
|
410
|
+
# escape character data
|
|
411
|
+
try:
|
|
412
|
+
text = _tounicode(text)
|
|
413
|
+
# it's worth avoiding do-nothing calls for short strings
|
|
414
|
+
if "&" in text:
|
|
415
|
+
text = text.replace("&", "&")
|
|
416
|
+
if "<" in text:
|
|
417
|
+
text = text.replace("<", "<")
|
|
418
|
+
if ">" in text:
|
|
419
|
+
text = text.replace(">", ">")
|
|
420
|
+
return text
|
|
421
|
+
except (TypeError, AttributeError):
|
|
422
|
+
_raise_serialization_error(text)
|
|
423
|
+
|
|
424
|
+
def _escape_attrib(text):
|
|
425
|
+
# escape attribute value
|
|
426
|
+
try:
|
|
427
|
+
text = _tounicode(text)
|
|
428
|
+
if "&" in text:
|
|
429
|
+
text = text.replace("&", "&")
|
|
430
|
+
if "<" in text:
|
|
431
|
+
text = text.replace("<", "<")
|
|
432
|
+
if ">" in text:
|
|
433
|
+
text = text.replace(">", ">")
|
|
434
|
+
if '"' in text:
|
|
435
|
+
text = text.replace('"', """)
|
|
436
|
+
if "\n" in text:
|
|
437
|
+
text = text.replace("\n", " ")
|
|
438
|
+
return text
|
|
439
|
+
except (TypeError, AttributeError):
|
|
440
|
+
_raise_serialization_error(text)
|
|
441
|
+
|
|
442
|
+
def _indent(elem, level=0):
|
|
443
|
+
# From http://effbot.org/zone/element-lib.htm#prettyprint
|
|
444
|
+
i = "\n" + level * " "
|
|
445
|
+
if len(elem):
|
|
446
|
+
if not elem.text or not elem.text.strip():
|
|
447
|
+
elem.text = i + " "
|
|
448
|
+
if not elem.tail or not elem.tail.strip():
|
|
449
|
+
elem.tail = i
|
|
450
|
+
for elem in elem:
|
|
451
|
+
_indent(elem, level + 1)
|
|
452
|
+
if not elem.tail or not elem.tail.strip():
|
|
453
|
+
elem.tail = i
|
|
454
|
+
else:
|
|
455
|
+
if level and (not elem.tail or not elem.tail.strip()):
|
|
456
|
+
elem.tail = i
|