3 pygments.formatters.img |
3 pygments.formatters.img |
4 ~~~~~~~~~~~~~~~~~~~~~~~ |
4 ~~~~~~~~~~~~~~~~~~~~~~~ |
5 |
5 |
6 Formatter for Pixmap output. |
6 Formatter for Pixmap output. |
7 |
7 |
8 :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
11 |
11 |
12 import sys |
12 import sys |
13 from subprocess import getstatusoutput |
13 from subprocess import getstatusoutput |
14 |
14 |
15 from pygments.formatter import Formatter |
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 # Import this carefully |
19 # Import this carefully |
19 try: |
20 try: |
20 import Image, ImageDraw, ImageFont |
21 import Image, ImageDraw, ImageFont |
21 pil_available = True |
22 pil_available = True |
248 `line_number_pad` |
254 `line_number_pad` |
249 The horizontal padding (in pixels) between the line number margin, and |
255 The horizontal padding (in pixels) between the line number margin, and |
250 the source code area. |
256 the source code area. |
251 |
257 |
252 Default: 6 |
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 |
253 """ |
269 """ |
254 |
270 |
255 # Required by the pygments mapper |
271 # Required by the pygments mapper |
256 name = 'img' |
272 name = 'img' |
257 aliases = ['img', 'IMG', 'png'] |
273 aliases = ['img', 'IMG', 'png'] |
297 self.line_number_pad = get_int_opt(options, 'line_number_pad', 6) |
313 self.line_number_pad = get_int_opt(options, 'line_number_pad', 6) |
298 self.line_numbers = get_bool_opt(options, 'line_numbers', True) |
314 self.line_numbers = get_bool_opt(options, 'line_numbers', True) |
299 self.line_number_separator = get_bool_opt(options, |
315 self.line_number_separator = get_bool_opt(options, |
300 'line_number_separator', True) |
316 'line_number_separator', True) |
301 self.line_number_step = get_int_opt(options, 'line_number_step', 1) |
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) |
302 if self.line_numbers: |
319 if self.line_numbers: |
303 self.line_number_width = (self.fontw * self.line_number_chars + |
320 self.line_number_width = (self.fontw * self.line_number_chars + |
304 self.line_number_pad * 2) |
321 self.line_number_pad * 2) |
305 else: |
322 else: |
306 self.line_number_width = 0 |
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' |
307 self.drawables = [] |
333 self.drawables = [] |
308 |
334 |
309 def get_style_defs(self, arg=''): |
335 def get_style_defs(self, arg=''): |
310 raise NotImplementedError('The -S option is meaningless for the image ' |
336 raise NotImplementedError('The -S option is meaningless for the image ' |
311 'formatter. Use -O style=<stylename> instead.') |
337 'formatter. Use -O style=<stylename> instead.') |
367 Get the required image size. |
393 Get the required image size. |
368 """ |
394 """ |
369 return (self._get_char_x(maxcharno) + self.image_pad, |
395 return (self._get_char_x(maxcharno) + self.image_pad, |
370 self._get_line_y(maxlineno + 0) + self.image_pad) |
396 self._get_line_y(maxlineno + 0) + self.image_pad) |
371 |
397 |
372 def _draw_linenumber(self, lineno): |
398 def _draw_linenumber(self, posno, lineno): |
373 """ |
399 """ |
374 Remember a line number drawable to paint later. |
400 Remember a line number drawable to paint later. |
375 """ |
401 """ |
376 self._draw_text( |
402 self._draw_text( |
377 self._get_linenumber_pos(lineno), |
403 self._get_linenumber_pos(posno), |
378 str(lineno + 1).rjust(self.line_number_chars), |
404 str(lineno).rjust(self.line_number_chars), |
379 font=self.fonts.get_font(self.line_number_bold, |
405 font=self.fonts.get_font(self.line_number_bold, |
380 self.line_number_italic), |
406 self.line_number_italic), |
381 fill=self.line_number_fg, |
407 fill=self.line_number_fg, |
382 ) |
408 ) |
383 |
409 |
424 """ |
450 """ |
425 Create drawables for the line numbers. |
451 Create drawables for the line numbers. |
426 """ |
452 """ |
427 if not self.line_numbers: |
453 if not self.line_numbers: |
428 return |
454 return |
429 for i in range(self.maxlineno): |
455 for p in range(self.maxlineno): |
430 if ((i + 1) % self.line_number_step) == 0: |
456 n = p + self.line_number_start |
431 self._draw_linenumber(i) |
457 if (n % self.line_number_step) == 0: |
|
458 self._draw_linenumber(p, n) |
432 |
459 |
433 def _paint_line_number_bg(self, im): |
460 def _paint_line_number_bg(self, im): |
434 """ |
461 """ |
435 Paint the line number background on the image. |
462 Paint the line number background on the image. |
436 """ |
463 """ |
462 self._get_image_size(self.maxcharno, self.maxlineno), |
489 self._get_image_size(self.maxcharno, self.maxlineno), |
463 self.background_color |
490 self.background_color |
464 ) |
491 ) |
465 self._paint_line_number_bg(im) |
492 self._paint_line_number_bg(im) |
466 draw = ImageDraw.Draw(im) |
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) |
467 for pos, value, font, kw in self.drawables: |
503 for pos, value, font, kw in self.drawables: |
468 draw.text(pos, value, font=font, **kw) |
504 draw.text(pos, value, font=font, **kw) |
469 im.save(outfile, self.image_format.upper()) |
505 im.save(outfile, self.image_format.upper()) |
470 |
506 |
471 |
507 |