/usr/share/pyshared/blockdiag/utils/fontmap.py is in python-blockdiag 1.3.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | # -*- coding: utf-8 -*-
# Copyright 2011 Takeshi KOMIYA
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
import os
import sys
import copy
from collections import namedtuple
from blockdiag.utils.config import ConfigParser
from blockdiag.utils.compat import u
def parse_fontpath(path):
if path is None:
return (None, None)
match = re.search('^(.*):(\d)$', path)
if match:
return (match.group(1), int(match.group(2)))
else:
return (path, None)
class FontInfo(object):
def __init__(self, family, path, size):
self.path = path
self.size = int(size)
family = self._parse(family)
self.name = family[0]
self.generic_family = family[1]
self.weight = family[2]
self.style = family[3]
@property
def familyname(self):
if self.name:
name = self.name + "-"
else:
name = ''
if self.weight == 'bold':
return "%s%s-%s" % (name, self.generic_family, self.weight)
else:
return "%s%s-%s" % (name, self.generic_family, self.style)
def _parse(self, familyname):
pattern = '^(?:(.*)-)?' + \
'(serif|sansserif|monospace|fantasy|cursive)' + \
'(?:-(normal|bold|italic|oblique))?$'
match = re.search(pattern, familyname or '')
if match is None:
msg = 'Unknown font family: %s' % familyname
raise AttributeError(msg)
name = match.group(1) or ''
generic_family = match.group(2)
style = match.group(3) or ''
if style == 'bold':
weight = 'bold'
style = 'normal'
elif style in ('italic', 'oblique'):
weight = 'normal'
style = style
else:
weight = 'normal'
style = 'normal'
return [name, generic_family, weight, style]
def duplicate(self):
return copy.copy(self)
class FontMap(object):
BASE_FONTSIZE = 11
fontsize = BASE_FONTSIZE
default_fontfamily = 'sansserif'
def __init__(self, filename=None):
self.fonts = {}
self.aliases = {}
if filename:
self._parse_config(filename)
self.set_default_font(None)
def set_default_fontfamily(self, fontfamily):
self.default_fontfamily = fontfamily
self.set_default_font(None)
def _parse_config(self, conffile):
config = ConfigParser()
if hasattr(conffile, 'read'):
config.readfp(conffile)
elif os.path.isfile(conffile):
config.read(conffile)
else:
msg = "fontmap file is not found: %s" % conffile
raise RuntimeError(msg)
if config.has_section('fontmap'):
for name, path in config.items('fontmap'):
self.append_font(name, path)
if config.has_section('fontalias'):
for name, family in config.items('fontalias'):
self.aliases[name] = family
def set_default_font(self, path):
if path is None and self.find() is not None:
return
self.append_font(self.default_fontfamily, path)
def append_font(self, fontfamily, path):
_path, _ = parse_fontpath(path)
if path is None or os.path.isfile(_path):
font = FontInfo(fontfamily, path, self.fontsize)
self.fonts[font.familyname] = font
else:
msg = 'fontfile `%s` is not found: %s' % (fontfamily, path)
sys.stderr.write("WARNING: %s\n" % msg)
def _regulate_familyname(self, name):
return FontInfo(name, None, self.BASE_FONTSIZE).familyname
def find(self, element=None):
fontfamily = getattr(element, 'fontfamily', None) or \
self.default_fontfamily
fontfamily = self.aliases.get(fontfamily, fontfamily)
fontsize = getattr(element, 'fontsize', None) or self.fontsize
name = self._regulate_familyname(fontfamily)
if name in self.fonts:
font = self.fonts[name].duplicate()
font.size = fontsize
elif element is not None:
msg = "Unknown fontfamily: %s" % fontfamily
sys.stderr.write(u("WARNING: %s\n") % msg)
elem = namedtuple('Font', 'fontsize')(fontsize)
font = self.find(elem)
else:
font = None
return font
|