o a#U @sZdZddlZddlZddlmZddlmZmZmZm Z ddl Z zddl m Z m Z mZdZWn ey9dZYnwzddlZWney[zddlZWn eyXdZYnwYnwgdZgd d d gd gd dgdZdZdZdZGdddeZGdddeZGdddZGdddeZGdddeZGdddeZGdd d eZdS)!z pygments.formatters.img ~~~~~~~~~~~~~~~~~~~~~~~ Formatter for Pixmap output. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. N) Formatter) get_bool_opt get_int_opt get_list_optget_choice_opt)Image ImageDraw ImageFontTF)ImageFormatterGifImageFormatterJpgImageFormatterBmpImageFormatter)RomanBookNormalRegularMediumObliqueItalicBoldz Bold Obliquez Bold Italic)NORMALITALICBOLD BOLDITALICzDejaVu Sans Monoz Courier NewMenloc@eZdZdZdS)PilNotAvailablez,When Python imaging library is not availableN__name__ __module__ __qualname____doc__r#r#9/usr/lib/python3/dist-packages/pygments/formatters/img.pyr5rc@r) FontNotFoundz(When there are no usable fonts specifiedNrr#r#r#r$r&9r%r&c@sdeZdZdZdddZddZddZd d Zd d ZdddZ ddZ ddZ ddZ ddZ dS) FontManagerz> Manages a set of fonts: normal, italic, bold, etc... cCsr||_||_i|_d|_tjdr|st|_|dStjdr.|s(t |_| dS|s3t |_| dS)Nwindarwin) font_name font_sizefontsencodingsysplatform startswithDEFAULT_FONT_NAME_WIN _create_winDEFAULT_FONT_NAME_MAC _create_macDEFAULT_FONT_NAME_NIX _create_nix)selfr+r,r#r#r$__init__Bs     zFontManager.__init__c Csztjdd||fdgtjdd}|\}}|jdkr;|}|D]}|dr)q!|d}|r8|Sq!dSdS)Nzfc-listz %s:style=%sfile)stdoutstderrrsFontconfig warning::) subprocessPopenPIPE communicate returncode splitlinesr1decodestrip) r8namestyleprocr;_lineslinepathr#r#r$_get_nix_font_pathTs   zFontManager._get_nix_font_pathcCstdD]}||j|}|durt||j|jd<nqtd|jdD]5}t|D]}||j|}|durFt||j|j|<nq-|dkrT|jd|j|<q'|jd|j|<q'dS)NrNo usable fonts named: "%s"rrrrr)STYLESrMr+r truetyper,r-r&)r8rFrLrG stylenamer#r#r$r7bs*  zFontManager._create_nixcCs||d|S)N )getrElower)r8font_maprFrGr#r#r$_get_mac_font_pathwszFontManager._get_mac_font_pathcsi}tjtddddfD]|fddtDqtdD]}|||j|}|dur?t ||j |j d<nq%t d|jd D]6}t|D]}|||j|}|durit ||j |j |<nqO|d krw|j d |j |<qI|j d|j |<qIdS) NHOMEzLibrary/Fonts/z/Library/Fonts/z/System/Library/Fonts/c3s@|]}|drtj|dtj|fVqdS))ttfttcrN)rUendswithosrLsplitextjoin).0ffont_dirr#r$ ~s   z*FontManager._create_mac..rrNrOrr)r\rLr^getenvupdatelistdirrPrWr+r rQr,r-r&)r8rVrFrLrGrRr#rar$r5zs8  zFontManager._create_macFc CsvdD]*}|D]%}zd||od||f}t||\}} |WSty+Yqwq|r9td||dfdS)N)rz (TrueType)z%s%s%srSz"Font %s (%s) not found in registryr)_winreg QueryValueExOSErrorr&) r8keybasenamestylesfailsuffixrGvalnamevalrIr#r#r$ _lookup_wins  zFontManager._lookup_winc CsJd}tjdftjdftjdftjdfg}|D]}z{tj|}zmzQ|||jtdd}t||j |j d<dD].}|||jt|}|rPt||j |j |<q7|dkr]|j d|j |<q7|j d|j |<q7WWt |WdSt y}z|}WYd}~nd}~wwWt |nt |wWqt yYqw|r|t d) Nz2Software\Microsoft\Windows NT\CurrentVersion\Fontsz/Software\Microsoft\Windows\CurrentVersion\FontsrTrOrrz$Can't open Windows font registry key)rgHKEY_CURRENT_USERHKEY_LOCAL_MACHINEOpenKeyrqr+rPr rQr,r-CloseKeyr&ri)r8 lookuperrorkeynameskeynamerjrLrGerrr#r#r$r3sB   zFontManager._create_wincCs|jddS)z) Get the character size. rMr-getsizer8r#r#r$ get_char_sizezFontManager.get_char_sizecCs|jd|S)z3 Get the text size(width, height). rr{)r8textr#r#r$ get_text_sizerzFontManager.get_text_sizecCs8|r |r |jdS|r|jdS|r|jdS|jdS)z> Get the font based on bold and italic flags. rrrr)r-)r8boldobliquer#r#r$get_fonts    zFontManager.get_fontN)r()F)rr r!r"r9rMr7rWr5rqr3r~rrr#r#r#r$r'=s  ' r'c@seZdZdZdZgdZdgZdZdZddZ d-d d Z d d Z ddZ ddZ ddZddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,S).r a Create a PNG image from source code. This uses the Python Imaging Library to generate a pixmap from the source code. .. versionadded:: 0.10 Additional options accepted: `image_format` An image format to output to that is recognised by PIL, these include: * "PNG" (default) * "JPEG" * "BMP" * "GIF" `line_pad` The extra spacing (in pixels) between each line of text. Default: 2 `font_name` The font name to be used as the base font from which others, such as bold and italic fonts will be generated. This really should be a monospace font to look sane. Default: "Courier New" on Windows, "Menlo" on Mac OS, and "DejaVu Sans Mono" on \*nix `font_size` The font size in points to be used. Default: 14 `image_pad` The padding, in pixels to be used at each edge of the resulting image. Default: 10 `line_numbers` Whether line numbers should be shown: True/False Default: True `line_number_start` The line number of the first line. Default: 1 `line_number_step` The step used when printing line numbers. Default: 1 `line_number_bg` The background colour (in "#123456" format) of the line number bar, or None to use the style background color. Default: "#eed" `line_number_fg` The text color of the line numbers (in "#123456"-like format). Default: "#886" `line_number_chars` The number of columns of line numbers allowable in the line number margin. Default: 2 `line_number_bold` Whether line numbers will be bold: True/False Default: False `line_number_italic` Whether line numbers will be italicized: True/False Default: False `line_number_separator` Whether a line will be drawn between the line number area and the source code area: True/False Default: True `line_number_pad` The horizontal padding (in pixels) between the line number margin, and the source code area. Default: 6 `hl_lines` Specify a list of lines to be highlighted. .. versionadded:: 1.2 Default: empty list `hl_color` Specify the color for highlighting lines. .. versionadded:: 1.2 Default: highlight color of the selected style img)rIMGpngz*.pngFrc Kststdtj|fi|d|_t|j|_|jjdur"d|_n|jj|_t |dgd|j dd|_ t |d d |_ t |d d |_t |d d}t|dd||_|j\|_|_|dd|_|dd|_t |dd |_t|dd|_t|dd|_t |dd|_t|dd|_t|dd|_t |dd|_t |dd|_|jr|j|j|jd |_ nd |_ g|_!t"|d!g}|D]}z |j!#t$|Wqt%yYqw|d"|jj&pd#|_'g|_(dS)$zE See the class docstring for explanation of options. z5Python Imaging Library is required for this formatterlatin1Nz#fff image_format)rjpeggifbmpT)normcase image_pad line_padr,r(r+rline_number_fgz#886line_number_bgz#eedline_number_charsline_number_boldFline_number_italicline_number_pad line_numbersline_number_separatorline_number_stepline_number_startrhl_lineshl_colorz#f90)) pil_availablerrr9r.dictrGrlbackground_colorrdefault_image_formatrrrrr'rTr-r~fontwfonthrrrrrrrrrrrline_number_widthrrappendint ValueErrorhighlight_colorr drawables)r8optionsfontsize hl_lines_strrKr#r#r$r9]sp         zImageFormatter.__init__rcCstd)NzWThe -S option is meaningless for the image formatter. Use -O style= instead.)NotImplementedError)r8argr#r#r$get_style_defsszImageFormatter.get_style_defscCs |j|jS)z+ Get the height of a line. )rrr}r#r#r$_get_line_heights zImageFormatter._get_line_heightcCs|||jS)z8 Get the Y coordinate of a line number. )rrr8linenor#r#r$ _get_line_yszImageFormatter._get_line_ycCs|jS)z/ Get the width of a character. )rr}r#r#r$_get_char_widthszImageFormatter._get_char_widthcCs||j|jS)z? Get the X coordinate of a character position. )rr)r8 linelengthr#r#r$ _get_char_xrzImageFormatter._get_char_xcCs||||fS)zL Get the actual position for a character and line position. )rr)r8rrr#r#r$ _get_text_posszImageFormatter._get_text_poscCs|j||fS)zI Get the actual position for the start of a line number. )rrrr#r#r$_get_linenumber_posrz"ImageFormatter._get_linenumber_poscCs$|ddurd|d}|Sd}|S)zE Get the correct color for the token from the style. colorN#z#000r#)r8rGfillr#r#r$_get_text_color  zImageFormatter._get_text_colorcCs$|ddurd|d}|Sd}|S)zP Get the correct background color for the token from the style. bgcolorNrr#)r8rGbg_colorr#r#r$_get_text_bg_colorrz!ImageFormatter._get_text_bg_colorcCs|j|d|dS)z5 Get the correct font for the style. ritalic)r-r)r8rGr#r#r$_get_style_fontszImageFormatter._get_style_fontcCs$|||j||d|jfS)z. Get the required image size. r)rrr)r8 maxlinelength maxlinenor#r#r$_get_image_sizeszImageFormatter._get_image_sizecCs:|j||t||j|j|j|j|j dddS)zA Remember a line number drawable to paint later. Nfonttext_fgtext_bg) _draw_textrstrrjustrr-rrrr)r8posnorr#r#r$_draw_linenumbers  zImageFormatter._draw_linenumbercCs|j|||||fdS)zB Remember a single drawable tuple to paint later. N)rr)r8posrrrrr#r#r$rszImageFormatter._draw_textc Csd}}}d}}|D]p\}}||jvr|j}||jvs|j|} |d}|d} t| D]K\} } | d} | rn|j|||| || | | | | d|j | \}}||7}t ||}|t| 7}t ||}| dr{d}d}|d7}q0q ||_||_||_dS)z9 Create drawables for the token content. rT rrN)rlparent expandtabsrC enumeraterstriprrrrrr-rmaxlenr[r maxcharnor)r8 tokensourcercharnorrrttypevaluerGrJirKtemp temp_width temp_hightr#r#r$_create_drawablessB             z ImageFormatter._create_drawablescCsB|jsdSt|jD]}||j}||jdkr|||q dS)z8 Create drawables for the line numbers. Nr)rrangerrrr)r8pnr#r#r$_draw_line_numberss  z!ImageFormatter._draw_line_numberscCs~|jsdS|jdur dSt|}|jd}|j|j|j}|jd||fg|j d|j r<|j |df||fg|jd~dS)z@ Paint the line number background on the image. N)rrrr) rrrDrawsizerrr rectanglerrrK)r8imdrawrecthrectwr#r#r$_paint_line_number_bg$s   z$ImageFormatter._paint_line_number_bgcCs8|||td||j|j|j}||t |}|j rY|j |j |jd}|}|jd|}|j D]}||d} |j|| f||| |fg|jdq=|jD]4\} } } } }|r|j| | d}|j| d| d| d|d| d|dg|d|j| | | | dq\|||jdS)a  Format ``tokensource``, an iterable of ``(tokentype, tokenstring)`` tuples and write it into ``outfile``. This implementation calculates where it should draw each token on the pixmap, then calculates the required pixmap size and draws the items. RGBrrr)rr)rrN)rrrnewrrrrrrrrrrrrrrrrrtextsizersaverupper)r8routfilerrxrr linenumberyrrrrr text_sizer#r#r$format5s0    6zImageFormatter.formatN)r)rr r!r"rFaliases filenames unicodeoutputrr9rrrrrrrrrrrrrrrrrr#r#r#r$r s2m 8   ( r c@s$eZdZdZdZdgZdgZdZdS)r z Create a GIF image from source code. This uses the Python Imaging Library to generate a pixmap from the source code. .. versionadded:: 1.0 img_gifrz*.gifNrr r!r"rFrrrr#r#r#r$r Zs r c@s&eZdZdZdZddgZdgZdZdS)r z Create a JPEG image from source code. This uses the Python Imaging Library to generate a pixmap from the source code. .. versionadded:: 1.0 img_jpgjpgrz*.jpgNrr#r#r#r$r h r c@s&eZdZdZdZddgZdgZdZdS)r z Create a bitmap image from source code. This uses the Python Imaging Library to generate a pixmap from the source code. .. versionadded:: 1.0 img_bmprbitmapz*.bmpNrr#r#r#r$r vrr ) r"r\r/pygments.formatterr pygments.utilrrrrr>PILrrr r ImportErrorrgwinreg__all__rPr6r2r4r Exceptionr&r'r r r r r#r#r#r$sR       +u