|
Packit Service |
12699a |
#!/usr/bin/python3
|
|
Packit Service |
76cb02 |
# -*- coding: utf-8 -*-
|
|
Packit Service |
76cb02 |
# Time-stamp: <2008-06-23 22:21:26 ah>
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
Provide an encoder for a font specification configuration: the encoder is fed
|
|
Packit Service |
76cb02 |
with Unicode characters one by one and determines the needed font switches
|
|
Packit Service |
76cb02 |
between the preceding and the current character.
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
import re
|
|
Packit Service |
76cb02 |
import xml.dom.minidom
|
|
Packit Service |
76cb02 |
import logging
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
cd7d79 |
from dbtexmf.dblatex.xetex.fcfallback import FcFallbackFontSpec, DefaultFontSpec
|
|
Packit Service |
cd7d79 |
from dbtexmf.dblatex.xetex.fontspec import FontSpec, _indent
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
class FontSpecConfig:
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
This object parses an XML fontspec configuration file and build the
|
|
Packit Service |
76cb02 |
resulting fontspec tree, the root fontspec being the default font
|
|
Packit Service |
76cb02 |
to apply.
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
The fontspec configuration file defines the fonts to apply in order
|
|
Packit Service |
76cb02 |
of precedence (and for some Unicode ranges) and the font levels (or
|
|
Packit Service |
76cb02 |
subfonts) thanks to the 'refmode' attribute that links a font to its
|
|
Packit Service |
76cb02 |
parent.
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
def __init__(self, conf_file):
|
|
Packit Service |
76cb02 |
"""Create a font specification configuration from the specified file
|
|
Packit Service |
76cb02 |
(file name or file-like object).
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
self.log = logging.getLogger("dblatex")
|
|
Packit Service |
76cb02 |
self.fontspecs = []
|
|
Packit Service |
76cb02 |
self.fontnames = {}
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
dom_document = xml.dom.minidom.parse(conf_file)
|
|
Packit Service |
76cb02 |
for dom_fontspec in dom_document.getElementsByTagName('fontspec'):
|
|
Packit Service |
76cb02 |
default = dom_fontspec.getAttribute('default')
|
|
Packit Service |
76cb02 |
if default:
|
|
Packit Service |
76cb02 |
self.log.debug("has default")
|
|
Packit Service |
76cb02 |
fallback = dom_fontspec.getAttribute('fallback')
|
|
Packit Service |
76cb02 |
if fallback == "fontconfig":
|
|
Packit Service |
76cb02 |
self.default_fontspec = FcFallbackFontSpec()
|
|
Packit Service |
76cb02 |
else:
|
|
Packit Service |
76cb02 |
self.default_fontspec = DefaultFontSpec()
|
|
Packit Service |
76cb02 |
fontspec = self.default_fontspec
|
|
Packit Service |
76cb02 |
else:
|
|
Packit Service |
76cb02 |
fontspec = FontSpec()
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
fontspec.fromnode(dom_fontspec)
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
if fontspec != self.default_fontspec:
|
|
Packit Service |
76cb02 |
self.fontspecs.append(fontspec)
|
|
Packit Service |
76cb02 |
if fontspec.id:
|
|
Packit Service |
76cb02 |
self.fontnames[fontspec.id] = fontspec
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
dom_document.unlink()
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
if not(self.default_fontspec):
|
|
Packit Service |
76cb02 |
self.default_fontspec = DefaultFontSpec()
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
self.build_tree()
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
def build_tree(self):
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
Build the fontspec tree, the root node being the default font
|
|
Packit Service |
76cb02 |
to apply. The fontspecs without a refmode (i.e. not being
|
|
Packit Service |
76cb02 |
explicitely a subfont) are direct children of the default font.
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
to_ignore = []
|
|
Packit Service |
76cb02 |
for fontspec in self.fontspecs:
|
|
Packit Service |
76cb02 |
if fontspec.type == "ignore":
|
|
Packit Service |
76cb02 |
to_ignore.append(fontspec)
|
|
Packit Service |
76cb02 |
continue
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
if not(fontspec.refmode):
|
|
Packit Service |
76cb02 |
f = self.default_fontspec
|
|
Packit Service |
76cb02 |
else:
|
|
Packit Service |
76cb02 |
f = self.fontnames.get(fontspec.refmode, None)
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
if (f):
|
|
Packit Service |
76cb02 |
f.add_subfont(fontspec)
|
|
Packit Service |
76cb02 |
else:
|
|
Packit Service |
76cb02 |
raise ValueError("wrong fontspec tree")
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
# Insert the characters to ignore in fontspecs
|
|
Packit Service |
76cb02 |
for f in to_ignore:
|
|
Packit Service |
76cb02 |
self.default_fontspec.add_ignored(f.get_uranges())
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
def __str__(self):
|
|
Packit Service |
76cb02 |
"""Dump the instance's data attributes."""
|
|
Packit Service |
76cb02 |
string = 'FontSpecConfig:'
|
|
Packit Service |
76cb02 |
string += '\n Fontspec list:'
|
|
Packit Service |
76cb02 |
for fontspec in self.fontspecs:
|
|
Packit Service |
76cb02 |
string += '\n' + _indent(str(fontspec), 4)
|
|
Packit Service |
76cb02 |
return string
|
|
Packit Service |
76cb02 |
|