|
Packit |
0f19cf |
import sys
|
|
Packit |
0f19cf |
import os
|
|
Packit |
0f19cf |
import codecs
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
from dbtexmf.dblatex.texcodec import LatexCodec
|
|
Packit Service |
7080c1 |
from fsencoder import FontSpecEncoder
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
class XetexCodec(LatexCodec):
|
|
Packit |
0f19cf |
def __init__(self, fontconfig="", pre="", post=""):
|
|
Packit |
0f19cf |
# Use the default TeX escapes, and encoding method
|
|
Packit |
0f19cf |
LatexCodec.__init__(self, input_encoding="utf8",
|
|
Packit |
0f19cf |
output_encoding="utf8")
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
# XeTeX font manager
|
|
Packit |
0f19cf |
if not(fontconfig):
|
|
Packit |
0f19cf |
fontconfig = os.getenv("DBLATEX_FONTSPEC_FILE", "xefont.xml")
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
# If not proper fontconfig, fallback to default behaviour
|
|
Packit |
0f19cf |
try:
|
|
Packit |
0f19cf |
self._fontmgr = FontSpecEncoder(fontconfig)
|
|
Packit |
0f19cf |
except:
|
|
Packit |
0f19cf |
self._fontmgr = None
|
|
Packit |
0f19cf |
return
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
# Ignore the special characters \1 and \2 used as specific
|
|
Packit |
0f19cf |
# substitution characters
|
|
Packit |
0f19cf |
self._fontmgr.ignorechars("\1\2\r")
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
def clear_errors(self):
|
|
Packit |
0f19cf |
pass
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
def get_errors(self):
|
|
Packit |
0f19cf |
pass
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
def decode(self, text):
|
|
Packit |
0f19cf |
return self._decode(text)[0]
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
def encode(self, text):
|
|
Packit |
0f19cf |
# If no font manager, behaves as the default latex codec
|
|
Packit |
0f19cf |
if not(self._fontmgr):
|
|
Packit |
0f19cf |
return LatexCodec.encode(self, text)
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
# Preliminary backslash substitution
|
|
Packit |
0f19cf |
text = text.replace("\\", "\2")
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
# Consider that each text sequence is in his own tex group
|
|
Packit |
0f19cf |
self._fontmgr.reset()
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
# Handle fonts for this Unicode string. We build a list of
|
|
Packit |
0f19cf |
# strings, where each string is handled by a new font
|
|
Packit |
0f19cf |
switchfonts = []
|
|
Packit |
0f19cf |
for c in text:
|
|
Packit |
0f19cf |
font, char = self._fontmgr.encode(c)
|
|
Packit |
0f19cf |
# A new font, or empty switchfont list
|
|
Packit |
0f19cf |
if font or not(switchfonts):
|
|
Packit |
0f19cf |
sf = [font, char]
|
|
Packit |
0f19cf |
switchfonts.append(sf)
|
|
Packit |
0f19cf |
else:
|
|
Packit |
0f19cf |
# No new font, so extend the current string
|
|
Packit |
0f19cf |
sf[1] += char
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
# Merge each part, after escaping each string
|
|
Packit |
0f19cf |
text = ""
|
|
Packit |
0f19cf |
for sf in switchfonts:
|
|
Packit |
0f19cf |
sf[1] = self._texescape(sf[1])
|
|
Packit |
0f19cf |
text += "".join(sf)
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
# Encode for string output
|
|
Packit |
0f19cf |
text = self._encode(text)[0]
|
|
Packit |
0f19cf |
|
|
Packit |
0f19cf |
# Things are done, substitute the '\'
|
|
Packit |
0f19cf |
text = text.replace("\2", r"\textbackslash{}")
|
|
Packit |
0f19cf |
return "{" + text + "}"
|
|
Packit |
0f19cf |
|