img.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. """
  2. pygments.formatters.img
  3. ~~~~~~~~~~~~~~~~~~~~~~~
  4. Formatter for Pixmap output.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import os
  9. import sys
  10. from pygments.formatter import Formatter
  11. from pygments.util import get_bool_opt, get_int_opt, get_list_opt, \
  12. get_choice_opt
  13. import subprocess
  14. # Import this carefully
  15. try:
  16. from PIL import Image, ImageDraw, ImageFont
  17. pil_available = True
  18. except ImportError:
  19. pil_available = False
  20. try:
  21. import _winreg
  22. except ImportError:
  23. try:
  24. import winreg as _winreg
  25. except ImportError:
  26. _winreg = None
  27. __all__ = ['ImageFormatter', 'GifImageFormatter', 'JpgImageFormatter',
  28. 'BmpImageFormatter']
  29. # For some unknown reason every font calls it something different
  30. STYLES = {
  31. 'NORMAL': ['', 'Roman', 'Book', 'Normal', 'Regular', 'Medium'],
  32. 'ITALIC': ['Oblique', 'Italic'],
  33. 'BOLD': ['Bold'],
  34. 'BOLDITALIC': ['Bold Oblique', 'Bold Italic'],
  35. }
  36. # A sane default for modern systems
  37. DEFAULT_FONT_NAME_NIX = 'DejaVu Sans Mono'
  38. DEFAULT_FONT_NAME_WIN = 'Courier New'
  39. DEFAULT_FONT_NAME_MAC = 'Menlo'
  40. class PilNotAvailable(ImportError):
  41. """When Python imaging library is not available"""
  42. class FontNotFound(Exception):
  43. """When there are no usable fonts specified"""
  44. class FontManager:
  45. """
  46. Manages a set of fonts: normal, italic, bold, etc...
  47. """
  48. def __init__(self, font_name, font_size=14):
  49. self.font_name = font_name
  50. self.font_size = font_size
  51. self.fonts = {}
  52. self.encoding = None
  53. self.variable = False
  54. if hasattr(font_name, 'read') or os.path.isfile(font_name):
  55. font = ImageFont.truetype(font_name, self.font_size)
  56. self.variable = True
  57. for style in STYLES:
  58. self.fonts[style] = font
  59. return
  60. if sys.platform.startswith('win'):
  61. if not font_name:
  62. self.font_name = DEFAULT_FONT_NAME_WIN
  63. self._create_win()
  64. elif sys.platform.startswith('darwin'):
  65. if not font_name:
  66. self.font_name = DEFAULT_FONT_NAME_MAC
  67. self._create_mac()
  68. else:
  69. if not font_name:
  70. self.font_name = DEFAULT_FONT_NAME_NIX
  71. self._create_nix()
  72. def _get_nix_font_path(self, name, style):
  73. proc = subprocess.Popen(['fc-list', f"{name}:style={style}", 'file'],
  74. stdout=subprocess.PIPE, stderr=None)
  75. stdout, _ = proc.communicate()
  76. if proc.returncode == 0:
  77. lines = stdout.splitlines()
  78. for line in lines:
  79. if line.startswith(b'Fontconfig warning:'):
  80. continue
  81. path = line.decode().strip().strip(':')
  82. if path:
  83. return path
  84. return None
  85. def _create_nix(self):
  86. for name in STYLES['NORMAL']:
  87. path = self._get_nix_font_path(self.font_name, name)
  88. if path is not None:
  89. self.fonts['NORMAL'] = ImageFont.truetype(path, self.font_size)
  90. break
  91. else:
  92. raise FontNotFound(f'No usable fonts named: "{self.font_name}"')
  93. for style in ('ITALIC', 'BOLD', 'BOLDITALIC'):
  94. for stylename in STYLES[style]:
  95. path = self._get_nix_font_path(self.font_name, stylename)
  96. if path is not None:
  97. self.fonts[style] = ImageFont.truetype(path, self.font_size)
  98. break
  99. else:
  100. if style == 'BOLDITALIC':
  101. self.fonts[style] = self.fonts['BOLD']
  102. else:
  103. self.fonts[style] = self.fonts['NORMAL']
  104. def _get_mac_font_path(self, font_map, name, style):
  105. return font_map.get((name + ' ' + style).strip().lower())
  106. def _create_mac(self):
  107. font_map = {}
  108. for font_dir in (os.path.join(os.getenv("HOME"), 'Library/Fonts/'),
  109. '/Library/Fonts/', '/System/Library/Fonts/'):
  110. font_map.update(
  111. (os.path.splitext(f)[0].lower(), os.path.join(font_dir, f))
  112. for f in os.listdir(font_dir)
  113. if f.lower().endswith(('ttf', 'ttc')))
  114. for name in STYLES['NORMAL']:
  115. path = self._get_mac_font_path(font_map, self.font_name, name)
  116. if path is not None:
  117. self.fonts['NORMAL'] = ImageFont.truetype(path, self.font_size)
  118. break
  119. else:
  120. raise FontNotFound(f'No usable fonts named: "{self.font_name}"')
  121. for style in ('ITALIC', 'BOLD', 'BOLDITALIC'):
  122. for stylename in STYLES[style]:
  123. path = self._get_mac_font_path(font_map, self.font_name, stylename)
  124. if path is not None:
  125. self.fonts[style] = ImageFont.truetype(path, self.font_size)
  126. break
  127. else:
  128. if style == 'BOLDITALIC':
  129. self.fonts[style] = self.fonts['BOLD']
  130. else:
  131. self.fonts[style] = self.fonts['NORMAL']
  132. def _lookup_win(self, key, basename, styles, fail=False):
  133. for suffix in ('', ' (TrueType)'):
  134. for style in styles:
  135. try:
  136. valname = '{}{}{}'.format(basename, style and ' '+style, suffix)
  137. val, _ = _winreg.QueryValueEx(key, valname)
  138. return val
  139. except OSError:
  140. continue
  141. else:
  142. if fail:
  143. raise FontNotFound(f'Font {basename} ({styles[0]}) not found in registry')
  144. return None
  145. def _create_win(self):
  146. lookuperror = None
  147. keynames = [ (_winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows NT\CurrentVersion\Fonts'),
  148. (_winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Fonts'),
  149. (_winreg.HKEY_LOCAL_MACHINE, r'Software\Microsoft\Windows NT\CurrentVersion\Fonts'),
  150. (_winreg.HKEY_LOCAL_MACHINE, r'Software\Microsoft\Windows\CurrentVersion\Fonts') ]
  151. for keyname in keynames:
  152. try:
  153. key = _winreg.OpenKey(*keyname)
  154. try:
  155. path = self._lookup_win(key, self.font_name, STYLES['NORMAL'], True)
  156. self.fonts['NORMAL'] = ImageFont.truetype(path, self.font_size)
  157. for style in ('ITALIC', 'BOLD', 'BOLDITALIC'):
  158. path = self._lookup_win(key, self.font_name, STYLES[style])
  159. if path:
  160. self.fonts[style] = ImageFont.truetype(path, self.font_size)
  161. else:
  162. if style == 'BOLDITALIC':
  163. self.fonts[style] = self.fonts['BOLD']
  164. else:
  165. self.fonts[style] = self.fonts['NORMAL']
  166. return
  167. except FontNotFound as err:
  168. lookuperror = err
  169. finally:
  170. _winreg.CloseKey(key)
  171. except OSError:
  172. pass
  173. else:
  174. # If we get here, we checked all registry keys and had no luck
  175. # We can be in one of two situations now:
  176. # * All key lookups failed. In this case lookuperror is None and we
  177. # will raise a generic error
  178. # * At least one lookup failed with a FontNotFound error. In this
  179. # case, we will raise that as a more specific error
  180. if lookuperror:
  181. raise lookuperror
  182. raise FontNotFound('Can\'t open Windows font registry key')
  183. def get_char_size(self):
  184. """
  185. Get the character size.
  186. """
  187. return self.get_text_size('M')
  188. def get_text_size(self, text):
  189. """
  190. Get the text size (width, height).
  191. """
  192. font = self.fonts['NORMAL']
  193. if hasattr(font, 'getbbox'): # Pillow >= 9.2.0
  194. return font.getbbox(text)[2:4]
  195. else:
  196. return font.getsize(text)
  197. def get_font(self, bold, oblique):
  198. """
  199. Get the font based on bold and italic flags.
  200. """
  201. if bold and oblique:
  202. if self.variable:
  203. return self.get_style('BOLDITALIC')
  204. return self.fonts['BOLDITALIC']
  205. elif bold:
  206. if self.variable:
  207. return self.get_style('BOLD')
  208. return self.fonts['BOLD']
  209. elif oblique:
  210. if self.variable:
  211. return self.get_style('ITALIC')
  212. return self.fonts['ITALIC']
  213. else:
  214. if self.variable:
  215. return self.get_style('NORMAL')
  216. return self.fonts['NORMAL']
  217. def get_style(self, style):
  218. """
  219. Get the specified style of the font if it is a variable font.
  220. If not found, return the normal font.
  221. """
  222. font = self.fonts[style]
  223. for style_name in STYLES[style]:
  224. try:
  225. font.set_variation_by_name(style_name)
  226. return font
  227. except ValueError:
  228. pass
  229. except OSError:
  230. return font
  231. return font
  232. class ImageFormatter(Formatter):
  233. """
  234. Create a PNG image from source code. This uses the Python Imaging Library to
  235. generate a pixmap from the source code.
  236. .. versionadded:: 0.10
  237. Additional options accepted:
  238. `image_format`
  239. An image format to output to that is recognised by PIL, these include:
  240. * "PNG" (default)
  241. * "JPEG"
  242. * "BMP"
  243. * "GIF"
  244. `line_pad`
  245. The extra spacing (in pixels) between each line of text.
  246. Default: 2
  247. `font_name`
  248. The font name to be used as the base font from which others, such as
  249. bold and italic fonts will be generated. This really should be a
  250. monospace font to look sane.
  251. If a filename or a file-like object is specified, the user must
  252. provide different styles of the font.
  253. Default: "Courier New" on Windows, "Menlo" on Mac OS, and
  254. "DejaVu Sans Mono" on \\*nix
  255. `font_size`
  256. The font size in points to be used.
  257. Default: 14
  258. `image_pad`
  259. The padding, in pixels to be used at each edge of the resulting image.
  260. Default: 10
  261. `line_numbers`
  262. Whether line numbers should be shown: True/False
  263. Default: True
  264. `line_number_start`
  265. The line number of the first line.
  266. Default: 1
  267. `line_number_step`
  268. The step used when printing line numbers.
  269. Default: 1
  270. `line_number_bg`
  271. The background colour (in "#123456" format) of the line number bar, or
  272. None to use the style background color.
  273. Default: "#eed"
  274. `line_number_fg`
  275. The text color of the line numbers (in "#123456"-like format).
  276. Default: "#886"
  277. `line_number_chars`
  278. The number of columns of line numbers allowable in the line number
  279. margin.
  280. Default: 2
  281. `line_number_bold`
  282. Whether line numbers will be bold: True/False
  283. Default: False
  284. `line_number_italic`
  285. Whether line numbers will be italicized: True/False
  286. Default: False
  287. `line_number_separator`
  288. Whether a line will be drawn between the line number area and the
  289. source code area: True/False
  290. Default: True
  291. `line_number_pad`
  292. The horizontal padding (in pixels) between the line number margin, and
  293. the source code area.
  294. Default: 6
  295. `hl_lines`
  296. Specify a list of lines to be highlighted.
  297. .. versionadded:: 1.2
  298. Default: empty list
  299. `hl_color`
  300. Specify the color for highlighting lines.
  301. .. versionadded:: 1.2
  302. Default: highlight color of the selected style
  303. """
  304. # Required by the pygments mapper
  305. name = 'img'
  306. aliases = ['img', 'IMG', 'png']
  307. filenames = ['*.png']
  308. unicodeoutput = False
  309. default_image_format = 'png'
  310. def __init__(self, **options):
  311. """
  312. See the class docstring for explanation of options.
  313. """
  314. if not pil_available:
  315. raise PilNotAvailable(
  316. 'Python Imaging Library is required for this formatter')
  317. Formatter.__init__(self, **options)
  318. self.encoding = 'latin1' # let pygments.format() do the right thing
  319. # Read the style
  320. self.styles = dict(self.style)
  321. if self.style.background_color is None:
  322. self.background_color = '#fff'
  323. else:
  324. self.background_color = self.style.background_color
  325. # Image options
  326. self.image_format = get_choice_opt(
  327. options, 'image_format', ['png', 'jpeg', 'gif', 'bmp'],
  328. self.default_image_format, normcase=True)
  329. self.image_pad = get_int_opt(options, 'image_pad', 10)
  330. self.line_pad = get_int_opt(options, 'line_pad', 2)
  331. # The fonts
  332. fontsize = get_int_opt(options, 'font_size', 14)
  333. self.fonts = FontManager(options.get('font_name', ''), fontsize)
  334. self.fontw, self.fonth = self.fonts.get_char_size()
  335. # Line number options
  336. self.line_number_fg = options.get('line_number_fg', '#886')
  337. self.line_number_bg = options.get('line_number_bg', '#eed')
  338. self.line_number_chars = get_int_opt(options,
  339. 'line_number_chars', 2)
  340. self.line_number_bold = get_bool_opt(options,
  341. 'line_number_bold', False)
  342. self.line_number_italic = get_bool_opt(options,
  343. 'line_number_italic', False)
  344. self.line_number_pad = get_int_opt(options, 'line_number_pad', 6)
  345. self.line_numbers = get_bool_opt(options, 'line_numbers', True)
  346. self.line_number_separator = get_bool_opt(options,
  347. 'line_number_separator', True)
  348. self.line_number_step = get_int_opt(options, 'line_number_step', 1)
  349. self.line_number_start = get_int_opt(options, 'line_number_start', 1)
  350. if self.line_numbers:
  351. self.line_number_width = (self.fontw * self.line_number_chars +
  352. self.line_number_pad * 2)
  353. else:
  354. self.line_number_width = 0
  355. self.hl_lines = []
  356. hl_lines_str = get_list_opt(options, 'hl_lines', [])
  357. for line in hl_lines_str:
  358. try:
  359. self.hl_lines.append(int(line))
  360. except ValueError:
  361. pass
  362. self.hl_color = options.get('hl_color',
  363. self.style.highlight_color) or '#f90'
  364. self.drawables = []
  365. def get_style_defs(self, arg=''):
  366. raise NotImplementedError('The -S option is meaningless for the image '
  367. 'formatter. Use -O style=<stylename> instead.')
  368. def _get_line_height(self):
  369. """
  370. Get the height of a line.
  371. """
  372. return self.fonth + self.line_pad
  373. def _get_line_y(self, lineno):
  374. """
  375. Get the Y coordinate of a line number.
  376. """
  377. return lineno * self._get_line_height() + self.image_pad
  378. def _get_char_width(self):
  379. """
  380. Get the width of a character.
  381. """
  382. return self.fontw
  383. def _get_char_x(self, linelength):
  384. """
  385. Get the X coordinate of a character position.
  386. """
  387. return linelength + self.image_pad + self.line_number_width
  388. def _get_text_pos(self, linelength, lineno):
  389. """
  390. Get the actual position for a character and line position.
  391. """
  392. return self._get_char_x(linelength), self._get_line_y(lineno)
  393. def _get_linenumber_pos(self, lineno):
  394. """
  395. Get the actual position for the start of a line number.
  396. """
  397. return (self.image_pad, self._get_line_y(lineno))
  398. def _get_text_color(self, style):
  399. """
  400. Get the correct color for the token from the style.
  401. """
  402. if style['color'] is not None:
  403. fill = '#' + style['color']
  404. else:
  405. fill = '#000'
  406. return fill
  407. def _get_text_bg_color(self, style):
  408. """
  409. Get the correct background color for the token from the style.
  410. """
  411. if style['bgcolor'] is not None:
  412. bg_color = '#' + style['bgcolor']
  413. else:
  414. bg_color = None
  415. return bg_color
  416. def _get_style_font(self, style):
  417. """
  418. Get the correct font for the style.
  419. """
  420. return self.fonts.get_font(style['bold'], style['italic'])
  421. def _get_image_size(self, maxlinelength, maxlineno):
  422. """
  423. Get the required image size.
  424. """
  425. return (self._get_char_x(maxlinelength) + self.image_pad,
  426. self._get_line_y(maxlineno + 0) + self.image_pad)
  427. def _draw_linenumber(self, posno, lineno):
  428. """
  429. Remember a line number drawable to paint later.
  430. """
  431. self._draw_text(
  432. self._get_linenumber_pos(posno),
  433. str(lineno).rjust(self.line_number_chars),
  434. font=self.fonts.get_font(self.line_number_bold,
  435. self.line_number_italic),
  436. text_fg=self.line_number_fg,
  437. text_bg=None,
  438. )
  439. def _draw_text(self, pos, text, font, text_fg, text_bg):
  440. """
  441. Remember a single drawable tuple to paint later.
  442. """
  443. self.drawables.append((pos, text, font, text_fg, text_bg))
  444. def _create_drawables(self, tokensource):
  445. """
  446. Create drawables for the token content.
  447. """
  448. lineno = charno = maxcharno = 0
  449. maxlinelength = linelength = 0
  450. for ttype, value in tokensource:
  451. while ttype not in self.styles:
  452. ttype = ttype.parent
  453. style = self.styles[ttype]
  454. # TODO: make sure tab expansion happens earlier in the chain. It
  455. # really ought to be done on the input, as to do it right here is
  456. # quite complex.
  457. value = value.expandtabs(4)
  458. lines = value.splitlines(True)
  459. # print lines
  460. for i, line in enumerate(lines):
  461. temp = line.rstrip('\n')
  462. if temp:
  463. self._draw_text(
  464. self._get_text_pos(linelength, lineno),
  465. temp,
  466. font = self._get_style_font(style),
  467. text_fg = self._get_text_color(style),
  468. text_bg = self._get_text_bg_color(style),
  469. )
  470. temp_width, _ = self.fonts.get_text_size(temp)
  471. linelength += temp_width
  472. maxlinelength = max(maxlinelength, linelength)
  473. charno += len(temp)
  474. maxcharno = max(maxcharno, charno)
  475. if line.endswith('\n'):
  476. # add a line for each extra line in the value
  477. linelength = 0
  478. charno = 0
  479. lineno += 1
  480. self.maxlinelength = maxlinelength
  481. self.maxcharno = maxcharno
  482. self.maxlineno = lineno
  483. def _draw_line_numbers(self):
  484. """
  485. Create drawables for the line numbers.
  486. """
  487. if not self.line_numbers:
  488. return
  489. for p in range(self.maxlineno):
  490. n = p + self.line_number_start
  491. if (n % self.line_number_step) == 0:
  492. self._draw_linenumber(p, n)
  493. def _paint_line_number_bg(self, im):
  494. """
  495. Paint the line number background on the image.
  496. """
  497. if not self.line_numbers:
  498. return
  499. if self.line_number_fg is None:
  500. return
  501. draw = ImageDraw.Draw(im)
  502. recth = im.size[-1]
  503. rectw = self.image_pad + self.line_number_width - self.line_number_pad
  504. draw.rectangle([(0, 0), (rectw, recth)],
  505. fill=self.line_number_bg)
  506. if self.line_number_separator:
  507. draw.line([(rectw, 0), (rectw, recth)], fill=self.line_number_fg)
  508. del draw
  509. def format(self, tokensource, outfile):
  510. """
  511. Format ``tokensource``, an iterable of ``(tokentype, tokenstring)``
  512. tuples and write it into ``outfile``.
  513. This implementation calculates where it should draw each token on the
  514. pixmap, then calculates the required pixmap size and draws the items.
  515. """
  516. self._create_drawables(tokensource)
  517. self._draw_line_numbers()
  518. im = Image.new(
  519. 'RGB',
  520. self._get_image_size(self.maxlinelength, self.maxlineno),
  521. self.background_color
  522. )
  523. self._paint_line_number_bg(im)
  524. draw = ImageDraw.Draw(im)
  525. # Highlight
  526. if self.hl_lines:
  527. x = self.image_pad + self.line_number_width - self.line_number_pad + 1
  528. recth = self._get_line_height()
  529. rectw = im.size[0] - x
  530. for linenumber in self.hl_lines:
  531. y = self._get_line_y(linenumber - 1)
  532. draw.rectangle([(x, y), (x + rectw, y + recth)],
  533. fill=self.hl_color)
  534. for pos, value, font, text_fg, text_bg in self.drawables:
  535. if text_bg:
  536. # see deprecations https://pillow.readthedocs.io/en/stable/releasenotes/9.2.0.html#font-size-and-offset-methods
  537. if hasattr(draw, 'textsize'):
  538. text_size = draw.textsize(text=value, font=font)
  539. else:
  540. text_size = font.getbbox(value)[2:]
  541. draw.rectangle([pos[0], pos[1], pos[0] + text_size[0], pos[1] + text_size[1]], fill=text_bg)
  542. draw.text(pos, value, font=font, fill=text_fg)
  543. im.save(outfile, self.image_format.upper())
  544. # Add one formatter per format, so that the "-f gif" option gives the correct result
  545. # when used in pygmentize.
  546. class GifImageFormatter(ImageFormatter):
  547. """
  548. Create a GIF image from source code. This uses the Python Imaging Library to
  549. generate a pixmap from the source code.
  550. .. versionadded:: 1.0
  551. """
  552. name = 'img_gif'
  553. aliases = ['gif']
  554. filenames = ['*.gif']
  555. default_image_format = 'gif'
  556. class JpgImageFormatter(ImageFormatter):
  557. """
  558. Create a JPEG image from source code. This uses the Python Imaging Library to
  559. generate a pixmap from the source code.
  560. .. versionadded:: 1.0
  561. """
  562. name = 'img_jpg'
  563. aliases = ['jpg', 'jpeg']
  564. filenames = ['*.jpg']
  565. default_image_format = 'jpeg'
  566. class BmpImageFormatter(ImageFormatter):
  567. """
  568. Create a bitmap image from source code. This uses the Python Imaging Library to
  569. generate a pixmap from the source code.
  570. .. versionadded:: 1.0
  571. """
  572. name = 'img_bmp'
  573. aliases = ['bmp', 'bitmap']
  574. filenames = ['*.bmp']
  575. default_image_format = 'bmp'