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