This file is indexed.

/usr/lib/python2.7/dist-packages/pychart/font.py is in python-pychart 1.39-7.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#
# Copyright (C) 2000-2005 by Yasushi Saito (yasushi.saito@gmail.com)
# 
# Jockey is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# Jockey is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
import color
import string
import pychart_util
import re
import theme
import afm.dir

__doc__ = """The module for manipulating texts and their attributes.

Pychart supports extensive sets of attributes in texts. All attributes
are specified via "escape sequences", starting from letter "/". For
example, the below examples draws string "Hello" using a 12-point font
at 60-degree angle:

/12/a60{}Hello

List of attributes:

/hA
    Specifies horizontal alignment of the text.  A is one of L (left
    alignment), R (right alignment), or C (center alignment).
/vA
    Specifies vertical alignment of the text.  A is one of "B"
    (bottom), "T" (top), " M" (middle).

/F{FONT}
    Switch to FONT font family.
/T
    Shorthand of /F{Times-Roman}.
/H
    Shorthand of /F{Helvetica}.
/C
    Shorthand of /F{Courier}.
/B
    Shorthand of /F{Bookman-Demi}.
/A
    Shorthand of /F{AvantGarde-Book}.
/P
    Shorthand of /F{Palatino}.
/S
    Shorthand of /F{Symbol}.
/b
    Switch to bold typeface.
/i
    Switch to italic typeface.
/o
    Switch to oblique typeface.
/DD
    Set font size to DD points.

    /20{}2001 space odyssey!

/cDD
    Set gray-scale to 0.DD. Gray-scale of 00 means black, 99 means white.

//, /{, /}
    Display `/', `{', or `}'.
    
{ ... }
    Limit the effect of escape sequences. For example, the below
    example draws "Foo" at 12pt, "Bar" at 8pt, and "Baz" at 12pt.

    /12Foo{/8Bar}Baz
\n
    Break the line.
"""

# List of fonts for which their absence have been already warned.
_undefined_font_warned = {}

class FontException(Exception):
    def __init__(self, msg, str):
        self.__msg = msg
        self.__str = str
    def __str__(self):
        return """%s (got "%s"). Write "//". "/{", "/}" to display "/", "{", "}", respectively."""  % \
               (self.__msg, self.__str)

def _intern_afm(font, text):
    global _undefined_font_warned

    r = afm.dir.afm.get(font, None)
    if r: return r

    font2 = _font_aliases.get(font, None)
    if font2:
        r = afm.dir.afm.get(font2, None)
        if r: return r

    try:        
        exec("import pychart.afm.%s" % re.sub("-", "_", font))
        return afm.dir.afm[font]
    except:
        if not font2 and not _undefined_font_warned.has_key(font):
            pychart_util.warn('Warning: unknown font "%s" while parsing "%s"'
                              % (font, text))
            _undefined_font_warned[font] = 1
    
    if font2:
        try:
            exec('import pychart.afm.%s' % re.sub('-', '_', font2))
            return afm.dir.afm[font2]
        except:
            if not _undefined_font_warned.has_key(font):
                pychart_util.warn('Warning: unknown font "%s" while parsing "%s"' % (font, text))
                _undefined_font_warned[font] = 1
    return None    
def line_width(font, size, text):
    table = _intern_afm(font, text)
    if not table:
        return 0

    width = 0
    for ch in text:
        code = ord(ch)
        if code < len(table):
            width += table[code]
        else:
            # Invalid char. Make up a number.
            width += table[0]
            
    width = float(width) * size / 1000.0
    return width

_font_family_map = {'T': 'Times',
                    'H': 'Helvetica',
                    'C': 'Courier',
                    'N': 'Helvetica-Narrow',
                    'B': 'Bookman-Demi', 
                    'A': 'AvantGarde-Book',
                    'P': 'Palatino',
                    'S': 'Symbol'}

# Aliases for ghostscript font names.

_font_aliases = {
    'Bookman-Demi': 'URWBookmanL-DemiBold%I',
    'Bookman-DemiItalic':	'URWBookmanL-DemiBoldItal',
    'Bookman-Demi-Italic':	'URWBookmanL-DemiBoldItal',
    'Bookman-Light':		'URWBookmanL-Ligh',
    'Bookman-LightItalic':		'URWBookmanL-LighItal',
    'Bookman-Light-Italic':		'URWBookmanL-LighItal',
    'Courier':		'NimbusMonL-Regu',
    'Courier-Oblique':	'NimbusMonL-ReguObli',
    'Courier-Bold':		'NimbusMonL-Bold',
    'Courier-BoldOblique':	'NimbusMonL-BoldObli',
    'AvantGarde-Book':	'URWGothicL-Book',
    'AvantGarde-BookOblique':	'URWGothicL-BookObli',
    'AvantGarde-Book-Oblique':	'URWGothicL-BookObli',
    'AvantGarde-Demi':	'URWGothicL-Demi',
    'AvantGarde-DemiOblique':	'URWGothicL-DemiObli',
    'AvantGarde-Demi-Oblique':	'URWGothicL-DemiObli',
    'Helvetica':		'NimbusSanL-Regu',
    'Helvetica-Oblique':	'NimbusSanL-ReguItal',
    'Helvetica-Bold':		'NimbusSanL-Bold',
    'Helvetica-BoldOblique':	'NimbusSanL-BoldItal',
    'Helvetica-Narrow': 'NimbusSanL-ReguCond',
    'Helvetica-Narrow-Oblique': 'NimbusSanL-ReguCondItal',
    'Helvetica-Narrow-Bold':		'NimbusSanL-BoldCond',
    'Helvetica-Narrow-BoldOblique':	'NimbusSanL-BoldCondItal',
    'Palatino-Roman':			'URWPalladioL-Roma',
    'Palatino':			'URWPalladioL-Roma',
    'Palatino-Italic':		'URWPalladioL-Ital',
    'Palatino-Bold':			'URWPalladioL-Bold',
    'Palatino-BoldItalic':		'URWPalladioL-BoldItal',
    'NewCenturySchlbk-Roman':		'CenturySchL-Roma',
    'NewCenturySchlbk':		'CenturySchL-Roma',
    'NewCenturySchlbk-Italic':	'CenturySchL-Ital',
    'NewCenturySchlbk-Bold':		'CenturySchL-Bold',
    'NewCenturySchlbk-BoldItalic':	'CenturySchL-BoldItal',
    'Times-Roman':			'NimbusRomNo9L-Regu',
    'Times':			'NimbusRomNo9L-Regu',
    'Times-Italic':			'NimbusRomNo9L-ReguItal',
    'Times-Bold':			'NimbusRomNo9L-Medi',
    'Times-BoldItalic':		'NimbusRomNo9L-MediItal',
    'Symbol':				'StandardSymL',
    'ZapfChancery-MediumItalic':	'URWChanceryL-MediItal',
    'ZapfChancery-Medium-Italic':	'URWChanceryL-MediItal',
    'ZapfDingbats':			'Dingbats'
}


class text_state:
    def copy(self):
        ts = text_state()
        ts.family = self.family
        ts.modifiers = list(self.modifiers)
        ts.size = self.size
        ts.line_height = self.line_height
        ts.color = self.color
        ts.halign = self.halign
        ts.valign = self.valign
        ts.angle = self.angle
        return ts
    def __init__(self):
        self.family = theme.default_font_family
        self.modifiers = [] # 'b' for bold, 'i' for italic, 'o' for oblique.
        self.size = theme.default_font_size
        self.line_height = theme.default_line_height or theme.default_font_size
        self.color = color.default
        self.halign = theme.default_font_halign
        self.valign = theme.default_font_valign
        self.angle = theme.default_font_angle
        
class text_iterator:
    def __init__(self, s):
        self.str = unicode(s)
        self.i = 0
        self.ts = text_state()
        self.stack = []
    def reset(self, s):
	self.str = unicode(s)
	self.i = 0

    def __return_state(self, ts, str):
	font_name = ts.family

        if ts.modifiers != []:
            is_bold = 0
            if 'b' in ts.modifiers:
                is_bold = 1
                font_name += '-Bold'
            if 'o' in ts.modifiers:
                if not is_bold:
                    font_name += '-'
                font_name += 'Oblique'
            elif 'i' in ts.modifiers:
                if not is_bold:
                    font_name += '-'
                font_name += 'Italic'
        elif font_name in ('Palatino', 'Times', 'NewCenturySchlbk'):
            font_name += '-Roman'
                
	return (font_name, ts.size, ts.line_height, ts.color,
                ts.halign, ts.valign, ts.angle, str)
    def __parse_float(self):
        istart = self.i
        while self.i < len(self.str) and self.str[self.i] in string.digits or self.str[self.i] == '.':
            self.i += 1
        return float(self.str[istart:self.i])
            
    def __parse_int(self):
        istart = self.i
        while self.i < len(self.str) and \
              (self.str[self.i] in string.digits or
               self.str[self.i] == '-'):
            self.i += 1
        return int(self.str[istart:self.i])
    def next(self):
        "Get the next text segment. Return an 8-element array: (FONTNAME, SIZE, LINEHEIGHT, COLOR, H_ALIGN, V_ALIGN, ANGLE, STR."
        l = []
        changed = 0
	self.old_state = self.ts.copy()
        
        while self.i < len(self.str):
            if self.str[self.i] == '/':
                self.i = self.i+1
                ch = self.str[self.i]
                self.i = self.i+1
		self.old_state = self.ts.copy()
                if ch == '/' or ch == '{' or ch == '}':
                    l.append(ch)
                elif _font_family_map.has_key(ch):
                    self.ts.family = _font_family_map[ch]
                    changed = 1
                elif ch == 'F':
                    # /F{font-family}
                    if self.str[self.i] != '{':
                        raise FontException('"{" must follow /F', self.str)
                    self.i += 1
                    istart = self.i
                    while self.str[self.i] != '}':
                        self.i += 1
                        if self.i >= len(self.str):
                            raise FontException('Expecting "/F{...}"', self.str)
                    self.ts.family = self.str[istart:self.i]
                    self.i += 1
                    changed = 1
                    
                elif ch in string.digits:
                    self.i -= 1
                    self.ts.size = self.__parse_int()
                    self.ts.line_height = self.ts.size
                    changed = 1
                elif ch == 'l':
                    self.ts.line_height = self.__parse_int()
                    changed = 1
                elif ch == 'b':
                    self.ts.modifiers.append('b')
                    changed = 1
                elif ch == 'i':
                    self.ts.modifiers.append('i')
                    changed = 1
                elif ch == 'o':
                    self.ts.modifiers.append('q')
                    changed = 1
                elif ch == 'c':
                    self.ts.color = color.gray_scale(self.__parse_float())
                elif ch == 'v':
                    if self.str[self.i] not in 'BTM':
                        raise FontException('Undefined escape sequence "/v%c"' %
                                            self.str[self.i], self.str)
                    self.ts.valign = self.str[self.i]
                    self.i += 1
                    changed = 1
                elif ch == 'h':
                    if self.str[self.i] not in 'LRC':
                        raise FontException('Undefined escape sequence "/h%c"' %
                                            self.str[self.i], self.str)
                    self.ts.halign = self.str[self.i]
                    self.i += 1
                    changed = 1
                elif ch == 'a':
                    self.ts.angle = self.__parse_int()
                    changed = 1
                else:
                    raise FontException('Undefined escape sequence: "/%c"' % ch,
                                        self.str)
            elif self.str[self.i] == '{':
                self.stack.append(self.ts.copy())
                self.i += 1
            elif self.str[self.i] == '}':
                if len(self.stack) == 0:
                    raise FontError('Unmatched "}"', self.str)
                self.ts = self.stack[-1]
                del self.stack[-1]
                self.i += 1
		changed = 1
            else:
                l.append(self.str[self.i])
                self.i += 1

            if changed and len(l) > 0:
                return self.__return_state(self.old_state, ''.join(l))
            else:
                # font change in the beginning of the sequence doesn't count.
                self.old_state = self.ts.copy()
                changed = 0
        if len(l) > 0:
	    return self.__return_state(self.old_state, ''.join(l))
        else:
            return None

#
#

def unaligned_get_dimension(text):
    """Return the bounding box of the text, assuming that the left-bottom corner
    of the first letter of the text is at (0, 0). This procedure ignores
    /h, /v, and /a directives when calculating the BB; it just returns the
    alignment specifiers as a part of the return value. The return value is a
    tuple (width, height, halign, valign, angle)."""

    xmax = 0
    ymax = 0
    ymax = 0
    angle = None
    halign = None
    valign = None
    itr = text_iterator(None)
    for line in unicode(text).split('\n'):
        cur_height = 0
        cur_width = 0
	itr.reset(line)
        while 1:
            elem = itr.next()
            if not elem:
                break
            (font, size, line_height, color, new_h, new_v, new_a, chunk) = elem
            if halign != None and new_h != halign:
                raise FontException('Only one "/h" can appear in a string.',
                                    unicode(text))
            if valign != None and new_v != valign:
                raise FontException('Only one "/v" can appear in a string.',
                                    unicode(text))
            if angle != None and new_a != angle:
                raise FontException('Only one "/a" can appear in a string.',
                                    unicode(text))
            halign = new_h
            valign = new_v
            angle = new_a
            cur_width += line_width(font, size, chunk)
            cur_height = max(cur_height, line_height)
        xmax = max(cur_width, xmax)
        ymax += cur_height
    return (xmax, ymax,
            halign or theme.default_font_halign,
            valign or theme.default_font_valign,
            angle or theme.default_font_angle)

def get_dimension(text):
    """Return the bounding box of the <text>,
    assuming that the left-bottom corner
    of the first letter of the text is at (0, 0). This procedure ignores
    /h, /v, and /a directives when calculating the boundingbox; it just returns the
    alignment specifiers as a part of the return value. The return value is a
    tuple (width, height, halign, valign, angle)."""
    (xmax, ymax, halign, valign, angle) = unaligned_get_dimension(text)
    xmin = ymin = 0
    if halign == 'C':
        xmin = -xmax / 2.0
        xmax = xmax / 2.0
    elif halign == 'R':
        xmin = -xmax
        xmax = 0
    if valign == 'M':
        ymin = -ymax / 2.0
        ymax = ymax / 2.0
    elif valign == 'T':
        ymin = -ymax
        ymax = 0
    if angle != 0:
        (x0, y0) = pychart_util.rotate(xmin, ymin, angle)
        (x1, y1) = pychart_util.rotate(xmax, ymin, angle)
        (x2, y2) = pychart_util.rotate(xmin, ymax, angle)
        (x3, y3) = pychart_util.rotate(xmax, ymax, angle)
        xmax = max(x0, x1, x2, x3)
        xmin = min(x0, x1, x2, x3)
        ymax = max(y0, y1, y2, y3)
        ymin = min(y0, y1, y2, y3)
        return (xmin, xmax, ymin, ymax)
    return (xmin, xmax, ymin, ymax)

def unaligned_text_width(text):
    x = unaligned_get_dimension(text)
    return x[0]

def text_width(text):
    """Return the width of the <text> in points."""
    (xmin, xmax, d1, d2) = get_dimension(text)
    return xmax-xmin

def unaligned_text_height(text):
    x = unaligned_get_dimension(text)
    return x[1]

def text_height(text):
    """Return the total height of the <text> and the length from the
    base point to the top of the text box."""
    (d1, d2, ymin, ymax) = get_dimension(text)
    return (ymax-ymin, ymax)

def get_align(text):
    "Return (halign, valign, angle) of the <text>."
    (x1, x2, h, v, a) = unaligned_get_dimension(text)
    return (h, v, a)

def quotemeta(text):
    """Quote letters with special meanings in pychart so that <text> will display
    as-is when passed to canvas.show(). 

>>> font.quotemeta("foo/bar")
"foo//bar"
"""
    text = re.sub(r'/', '//', text)
    text = re.sub(r'\\{', '/{', text)
    text = re.sub(r'\\}', '/}', text)
    return text