~ubuntu-branches/ubuntu/quantal/wxwidgets2.8/quantal

« back to all changes in this revision

Viewing changes to wxPython/wx/tools/Editra/src/extern/pygments/formatters/img.py

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2012-01-07 13:59:25 UTC
  • mfrom: (1.1.9) (5.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20120107135925-2601miy9ullcon9j
Tags: 2.8.12.1-6ubuntu1
* Resync from Debian, changes that were kept:
  - debian/rules: re-enable mediactrl. This allows libwx_gtk2u_media-2.8 to be
    built, as this is required by some applications (LP: #632984)
  - debian/control: Build-dep on libxt-dev for mediactrl.
  - Patches
    + fix-bashism-in-example
* Add conflict on python-wxgtk2.8 (<< 2.8.12.1-6ubuntu1~) to python-wxversion
  to guarantee upgrade ordering when moving from pycentral to dh_python2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
    Formatter for Pixmap output.
7
7
 
8
 
    :copyright: 2007 by Ali Afshar.
9
 
    :license: BSD, see LICENSE for more details.
 
8
    :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
 
9
    :license: BSD, see LICENSE for details.
10
10
"""
11
11
 
12
12
import sys
13
13
from commands import getstatusoutput
14
14
 
15
15
from pygments.formatter import Formatter
16
 
from pygments.util import get_bool_opt, get_int_opt, get_choice_opt
 
16
from pygments.util import get_bool_opt, get_int_opt, \
 
17
     get_list_opt, get_choice_opt
17
18
 
18
19
# Import this carefully
19
20
try:
20
 
    import Image, ImageDraw, ImageFont
 
21
    from PIL import Image, ImageDraw, ImageFont
21
22
    pil_available = True
22
23
except ImportError:
23
24
    pil_available = False
61
62
        self.font_name = font_name
62
63
        self.font_size = font_size
63
64
        self.fonts = {}
 
65
        self.encoding = None
64
66
        if sys.platform.startswith('win'):
65
67
            if not font_name:
66
68
                self.font_name = DEFAULT_FONT_NAME_WIN
206
208
 
207
209
        Default: True
208
210
 
 
211
    `line_number_start`
 
212
        The line number of the first line.
 
213
 
 
214
        Default: 1
 
215
 
209
216
    `line_number_step`
210
217
        The step used when printing line numbers.
211
218
 
249
256
        the source code area.
250
257
 
251
258
        Default: 6
 
259
 
 
260
    `hl_lines`
 
261
        Specify a list of lines to be highlighted.  *New in Pygments 1.2.*
 
262
 
 
263
        Default: empty list
 
264
 
 
265
    `hl_color`
 
266
        Specify the color for highlighting lines.  *New in Pygments 1.2.*
 
267
 
 
268
        Default: highlight color of the selected style
252
269
    """
253
270
 
254
271
    # Required by the pygments mapper
298
315
        self.line_number_separator = get_bool_opt(options,
299
316
                                        'line_number_separator', True)
300
317
        self.line_number_step = get_int_opt(options, 'line_number_step', 1)
 
318
        self.line_number_start = get_int_opt(options, 'line_number_start', 1)
301
319
        if self.line_numbers:
302
320
            self.line_number_width = (self.fontw * self.line_number_chars +
303
321
                                   self.line_number_pad * 2)
304
322
        else:
305
323
            self.line_number_width = 0
 
324
        self.hl_lines = []
 
325
        hl_lines_str = get_list_opt(options, 'hl_lines', [])
 
326
        for line in hl_lines_str:
 
327
            try:
 
328
                self.hl_lines.append(int(line))
 
329
            except ValueError:
 
330
                pass
 
331
        self.hl_color = options.get('hl_color',
 
332
                                    self.style.highlight_color) or '#f90'
306
333
        self.drawables = []
307
334
 
308
335
    def get_style_defs(self, arg=''):
368
395
        return (self._get_char_x(maxcharno) + self.image_pad,
369
396
                self._get_line_y(maxlineno + 0) + self.image_pad)
370
397
 
371
 
    def _draw_linenumber(self, lineno):
 
398
    def _draw_linenumber(self, posno, lineno):
372
399
        """
373
400
        Remember a line number drawable to paint later.
374
401
        """
375
402
        self._draw_text(
376
 
            self._get_linenumber_pos(lineno),
377
 
            str(lineno + 1).rjust(self.line_number_chars),
 
403
            self._get_linenumber_pos(posno),
 
404
            str(lineno).rjust(self.line_number_chars),
378
405
            font=self.fonts.get_font(self.line_number_bold,
379
406
                                     self.line_number_italic),
380
407
            fill=self.line_number_fg,
395
422
            while ttype not in self.styles:
396
423
                ttype = ttype.parent
397
424
            style = self.styles[ttype]
 
425
            # TODO: make sure tab expansion happens earlier in the chain.  It
 
426
            # really ought to be done on the input, as to do it right here is
 
427
            # quite complex.
398
428
            value = value.expandtabs(4)
399
 
            lines = value.splitlines()
 
429
            lines = value.splitlines(True)
400
430
            #print lines
401
431
            for i, line in enumerate(lines):
402
 
                if not line:
403
 
                    lineno += 1
404
 
                    charno = 0
405
 
                else:
406
 
                    # add a line for each extra line in the value
407
 
                    if i:
408
 
                        lineno += 1
409
 
                        charno = 0
 
432
                temp = line.rstrip('\n')
 
433
                if temp:
410
434
                    self._draw_text(
411
435
                        self._get_text_pos(charno, lineno),
412
 
                        line,
 
436
                        temp,
413
437
                        font = self._get_style_font(style),
414
438
                        fill = self._get_text_color(style)
415
439
                    )
416
 
                    charno += len(value)
 
440
                    charno += len(temp)
417
441
                    maxcharno = max(maxcharno, charno)
 
442
                if line.endswith('\n'):
 
443
                    # add a line for each extra line in the value
 
444
                    charno = 0
 
445
                    lineno += 1
418
446
        self.maxcharno = maxcharno
419
447
        self.maxlineno = lineno
420
448
 
424
452
        """
425
453
        if not self.line_numbers:
426
454
            return
427
 
        for i in xrange(self.maxlineno):
428
 
            if ((i + 1) % self.line_number_step) == 0:
429
 
                self._draw_linenumber(i)
 
455
        for p in xrange(self.maxlineno):
 
456
            n = p + self.line_number_start
 
457
            if (n % self.line_number_step) == 0:
 
458
                self._draw_linenumber(p, n)
430
459
 
431
460
    def _paint_line_number_bg(self, im):
432
461
        """
462
491
        )
463
492
        self._paint_line_number_bg(im)
464
493
        draw = ImageDraw.Draw(im)
 
494
        # Highlight
 
495
        if self.hl_lines:
 
496
            x = self.image_pad + self.line_number_width - self.line_number_pad + 1
 
497
            recth = self._get_line_height()
 
498
            rectw = im.size[0] - x
 
499
            for linenumber in self.hl_lines:
 
500
                y = self._get_line_y(linenumber - 1)
 
501
                draw.rectangle([(x, y), (x + rectw, y + recth)],
 
502
                               fill=self.hl_color)
465
503
        for pos, value, font, kw in self.drawables:
466
504
            draw.text(pos, value, font=font, **kw)
467
505
        im.save(outfile, self.image_format.upper())