要导出带有非ASCII ISO-8859-1字符的Matlab图形,在Windows上没有问题,但在具有UTF-8语言环境的Linux上存在Matlab漏洞和解决方法。这里的问题涉及ISO-8859-1中没有的字符,这更加棘手。以下是我在相关问题上发布的解决方案。
如果所需字符数少于256(8位格式),并且最好使用标准编码集,则一种解决方案是:
- 将八进制代码转换为Unicode字符;
- 将文件保存到目标编码标准中(以8位格式);
- 添加目标编码集的编码向量。
例如,如果您想导出波兰文本,需要将文件转换为ISO-8859-2。以下是使用Python(多平台)的实现:
import sys,codecs
input = sys.argv[1]
fo = codecs.open(input[:-4]+'_latin2.eps','w','latin2')
with codecs.open(input,'r','string_escape') as fi:
data = fi.readlines()
with open('ISOLatin2Encoding.ps') as fenc:
for line in data:
fo.write(line.decode('utf-8').replace('ISOLatin1Encoding','MyEncoding'))
if line.startswith('%%EndPageSetup'):
fo.write(fenc.read())
fo.close()
将文件保存为eps_lat2.py;然后运行命令python eps_lat2.py file.eps
,其中file.eps是由Matlab创建的eps文件,将创建带有Latin-2编码的file_latin2.eps文件。文件ISOLatin2Encoding.ps包含编码向量:
/MyEncoding
% The first 144 entries are the same as the ISO Latin-1 encoding.
ISOLatin1Encoding 0 144 getinterval aload pop
% \22x
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
% \24x
/nbspace /Aogonek /breve /Lslash /currency /Lcaron /Sacute /section
/dieresis /Scaron /Scedilla /Tcaron /Zacute /hyphen /Zcaron /Zdotaccent
/degree /aogonek /ogonek /lslash /acute /lcaron /sacute /caron
/cedilla /scaron /scedilla /tcaron /zacute /hungarumlaut /zcaron /zdotaccent
% \30x
/Racute /Aacute /Acircumflex /Abreve /Adieresis /Lacute /Cacute /Ccedilla
/Ccaron /Eacute /Eogonek /Edieresis /Ecaron /Iacute /Icircumflex /Dcaron
/Dcroat /Nacute /Ncaron /Oacute /Ocircumflex /Ohungarumlaut /Odieresis /multiply
/Rcaron /Uring /Uacute /Uhungarumlaut /Udieresis /Yacute /Tcedilla /germandbls
% \34x
/racute /aacute /acircumflex /abreve /adieresis /lacute /cacute /ccedilla
/ccaron /eacute /eogonek /edieresis /ecaron /iacute /icircumflex /dcaron
/dcroat /nacute /ncaron /oacute /ocircumflex /ohungarumlaut /odieresis /divide
/rcaron /uring /uacute /uhungarumlaut /udieresis /yacute /tcedilla /dotaccent
256 packedarray def
以下是在Linux上使用Bash的另一种实现:
#!/bin/bash
name=$(basename "$1" .eps)
ascii2uni -a K "$1" > /tmp/eps_uni.eps
iconv -t ISO-8859-2 /tmp/eps_uni.eps -o "$name"_latin2.eps
sed -i -e '/%EndPageSetup/ r ISOLatin2Encoding.ps' -e 's/ISOLatin1Encoding/MyEncoding/' "$name"_latin2.eps
将文件保存为eps_lat2;然后运行命令sh eps_lat2 file.eps
,创建使用Latin-2编码的file_latin2.eps。
通过更改脚本中的编码向量和iconv(或codecs.open)参数,可以轻松地适应其他8位编码标准。