|eLZdZddlZddlZddlmZddlmZmZmZm Z ddl Z ddl m Z m Z mZdZ ddlZgdZgd d d gd gd dgdZdZdZdZGddeZGddeZGddZGddeZGddeZGddeZGdd eZy#e$rdZYswxYw#e$r ddlZn #e$rdZYnwxYwYwxYw)!z pygments.formatters.img ~~~~~~~~~~~~~~~~~~~~~~~ Formatter for Pixmap output. :copyright: Copyright 2006-2023 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 NewMenloceZdZdZy)PilNotAvailablez,When Python imaging library is not availableN__name__ __module__ __qualname____doc__9/usr/lib/python3/dist-packages/pygments/formatters/img.pyrr4s6r%rceZdZdZy) FontNotFoundz(When there are no usable fonts specifiedNrr$r%r&r(r(8s2r%r(cVeZdZdZddZdZdZdZdZddZ dZ d Z d Z d Z d Zy ) FontManagerz> Manages a set of fonts: normal, italic, bold, etc... cR||_||_i|_d|_d|_t |dst jj|rBtj||j}d|_tD]}||j|<ytjjdr|s t|_|j!ytjjdr|s t"|_|j%y|s t&|_|j)y)NFreadTwindarwin) font_name font_sizefontsencodingvariablehasattrospathisfiler truetypeSTYLESsysplatform startswithDEFAULT_FONT_NAME_WIN _create_winDEFAULT_FONT_NAME_MAC _create_macDEFAULT_FONT_NAME_NIX _create_nix)selfr/r0fontstyles r&__init__zFontManager.__init__As""   9f % )B%%i@D DM )$( 5! )  << " "5 )!6     \\ $ $X .!6    !6    r%c^tjd|d|dgtjd}|j\}}|jdk(r]|j }|D]G}|j dr|jjjd}|sE|cSyy)Nzfc-listz:style=file)stdoutstderrrsFontconfig warning::) subprocessPopenPIPE communicate returncode splitlinesr<decodestrip) rCnamerEprocrI_linesliner6s r&_get_nix_font_pathzFontManager._get_nix_font_path\sT5,I6 R'1tE$$&  ??a %%'E ??#9:{{}**,2237K   r%c*tdD]O}|j|j|}|"tj||j |j d<ntd|jzdD]}t|D]O}|j|j|}|"tj||j |j |<]|dk(r|j d|j |<|j d|j |<y)NrNo usable fonts named: "%s"rrrrr)r9rYr/r r8r0r1r()rCrTr6rE stylenames r&rBzFontManager._create_nixjs 8$ /D**4>>4@D'0'9'9$'O 8$  / <#~~ ./ /5 =E#E] = ..t~~yI#(1(:(:4(PDJJu% = L((, 6(:DJJu%(, 8(>@AAr%ci}tjjtjddddfD]/|j fdtj D1t dD]P}|j||j|}|#tj||j|jd<ntd|jzdD]}t |D]P}|j||j|}|#tj||j|j|<^|d k(r|jd |j|<|jd|j|<y) NHOMEzLibrary/Fonts/z/Library/Fonts/z/System/Library/Fonts/c3K|]s}|jjdrRtjj |djtjj |fuyw))ttfttcrN)raendswithr5r6splitextjoin).0ffont_dirs r& z*FontManager._create_mac..s^7779%%n5!!!$Q'--/h1JK7sA9A<rr[r\rr)r5r6rkgetenvupdatelistdirr9rcr/r r8r0r1r()rCrbrTr6rEr]rns @r&r@zFontManager._create_macslbii&79IJ*,DF 7H OO7H-7 7 78$ /D**8T^^TJD'0'9'9$'O 8$  / <#~~ ./ /5 =E#E] = ..xS#(1(:(:4(PDJJu% = L((, 6(:DJJu%(, 8(zFontManager._create_wins //1fg//1cd002gh002deh  HG oow/*++CAQSWXD+4+=+=dDNN+SDJJx(!AI#//T^^VE]S090B0B40XDJJu-$ 448JJv4F 5 148JJx4H 5 1I$$S)' H:!!FG G$&"%K&$$S)G$$S))  sOG#C+E6G6 F ?FF%F  F%G%F<<G G Gc$|jdS)z) Get the character size. M) get_text_sizerCs r& get_char_sizezFontManager.get_char_sizes!!#&&r%c|jd}t|dr|j|ddS|j|S)z4 Get the text size (width, height). rgetbbox)r1r4rgetsize)rCtextrDs r&rzFontManager.get_text_sizesAzz(# 4 #<<%a* *<<% %r%cr|r.|r,|jr|jdS|jdS|r,|jr|jdS|jdS|r,|jr|jdS|jdS|jr|jdS|jdS)z> Get the font based on bold and italic flags. rrrr)r3 get_styler1)rCboldobliques r&get_fontzFontManager.get_fonts G}}~~l33::l+ + }}~~f--::f% % }}~~h//::h' '}}~~h//::h' 'r%c|j|}t|D]} |j||cS|S#t$rY't$r|cYcSwxYw)z} Get the specified style of the font if it is a variable font. If not found, return the normal font. )r1r9set_variation_by_name ValueErrorrv)rCrErD style_names r&rzFontManager.get_stylese zz%  - J **:6       s5 A AAN))F)r r!r"r#rFrYrBrcr@r~r>rrrrr$r%r&r*r*<sA6 =*B=: %HN' &(2r%r*ceZdZdZdZgdZdgZdZdZdZ ddZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZy)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. If a filename or a file-like object is specified, the user must provide different styles of the font. 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 Dts tdtj|fi|d|_t |j |_|j jd|_n|j j|_t|dgd|jd|_ t|d d |_ t|d d |_t|d d}t|j!dd||_|j"j%\|_|_|j!dd|_|j!dd|_t|dd |_t1|dd|_t1|dd|_t|dd|_t1|dd|_t1|dd|_t|dd|_t|dd|_|j8r/|j&|j.z|j6d zz|_ nd |_ g|_!tE|d!g}|D]'} |jBjGtI|)|j!d"|j jLxsd#|_'g|_(y#tJ$rYlwxYw)$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_padrr0rr/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_availablerrrFr2dictrErybackground_colorrdefault_image_formatrrrrr*r`r1rfontwfonthrrrrrrrrrrrline_number_widthrrappendintrhighlight_colorr drawables)rCoptionsfontsize hl_lines_strrXs r&rFzImageFormatter.__init__sy!GI I4+7+  4::& :: & & .$*D !$(JJ$?$?D !* ^%B  % %6%Wk2>#GZ; w R8 ["!=xH !%!9!9!; DJ%kk*:FC%kk*:FC!,W-@!"E ,W-?!H".w/CU#L*74EqI(.$G%1'2I4&Q" +G5G K!,W6I1!M   &*jj43I3I&I&*&:&:Q&>'?D "&'D " #GZ<   D  $$SY/   J$(JJ$>$>@JCI    s6$J JJctd)NzWThe -S option is meaningless for the image formatter. Use -O style= instead.)NotImplementedError)rCargs r&get_style_defszImageFormatter.get_style_defss!#QR Rr%c4|j|jzS)z+ Get the height of a line. )rrrs r&_get_line_heightzImageFormatter._get_line_heightszzDMM))r%cB||jz|jzS)z8 Get the Y coordinate of a line number. )rrrClinenos r& _get_line_yzImageFormatter._get_line_ys!--//$..@@r%c|jS)z/ Get the width of a character. )rrs r&_get_char_widthzImageFormatter._get_char_widthszzr%c:||jz|jzS)z? Get the X coordinate of a character position. )rr)rC linelengths r& _get_char_xzImageFormatter._get_char_xsDNN*T-C-CCCr%cF|j||j|fS)zL Get the actual position for a character and line position. )rr)rCrrs r& _get_text_poszImageFormatter._get_text_poss% +T-=-=f-EEEr%c<|j|j|fS)zI Get the actual position for the start of a line number. )rrrs r&_get_linenumber_posz"ImageFormatter._get_linenumber_poss 0 0 899r%c(|d d|dz}|Sd}|S)zE Get the correct color for the token from the style. color#z#000r$)rCrEfills r&_get_text_colorzImageFormatter._get_text_colors/ > %w'D D r%c(|d d|dz}|Sd}|S)zP Get the correct background color for the token from the style. bgcolorNrr$)rCrEbg_colors r&_get_text_bg_colorz!ImageFormatter._get_text_bg_colors1   'U9--HHr%cF|jj|d|dS)z5 Get the correct font for the style. ritalic)r1r)rCrEs r&_get_style_fontzImageFormatter._get_style_fonts#zz""5=%/BBr%c|j||jz|j|dz|jzfS)z. Get the required image size. r)rrr)rC maxlinelength maxlinenos r&_get_image_sizezImageFormatter._get_image_sizesB  /$..@  Q/$..@B Br%c|j|j|t|j|j|j j |j|j|jdy)zA Remember a line number drawable to paint later. NrDtext_fgtext_bg) _draw_textrstrrjustrr1rrrr)rCposnors r&_draw_linenumberzImageFormatter._draw_linenumber sj   $ $U + K  d44 5$$T%:%:%)%<%<>''  r%cD|jj|||||fy)zB Remember a single drawable tuple to paint later. N)rr)rCposrrDrrs r&rzImageFormatter._draw_texts! sD$ABr%c dx}x}}dx}}|D]=\}}||jvr|j}||jvr|j|} |jd}|jd} t | D]\} } | j d} | r|j |j||| |j| |j| |j| |jj| \}}||z }t||}|t| z }t||}| jdsd}d}|dz }@||_||_||_y)z9 Create drawables for the token content. rrT rrN)ryparent expandtabsrQ enumeraterstriprrrrrr1rmaxlenrir maxcharnor)rC tokensourcercharnorrrttypevaluerErWirXtemp temp_widthrVs r&_create_drawablesz ImageFormatter._create_drawabless{'('')%&& ' LE5t{{* t{{*KK&E$$Q'E$$T*E$U+ 4{{4(OO**:v>#33E:"&"6"6u"="&"9"9%"@ $%)JJ$<$29L9LI M r%c |j||jtjd|j |j |j |j}|j|tj|}|jr|j|jz|jz dz}|j}|j d|z }|jD]?}|j#|dz } |j%|| f||z| |zfg|j&A|j(D]a\} } } } }|rB|j+| | }|j%| d| d| d|dz| d|dzg||j-| | | | c|j/||j0j3y)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 )rrD)rDrN)rr rnewrrrrrr rrrrrrrrrrrtextsizersaverupper)rCroutfilerrxrr linenumberyrrrDrr text_sizes r&formatzImageFormatter.format`s {+ ! YY   !3!3T^^ D  ! !  ""2&~~b! ==!7!77$:N:NNQRRA))+EGGAJNE"mm 3 $$Z!^4AUAI(>?$(MM3 337.. ; .Cgw MMu4M@ AAA10Es1vPYZ[P\G\]dkl IIc5t'I :  ; **0023r%N)r)r r!r"r#rTaliases filenames unicodeoutputrrFrrrrrrrrrrrrrrr rr!r$r%r&r r sl^ D#G IM 6pR* A  D F : C B  C & P ,"4r%r c$eZdZdZdZdgZdgZdZy)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*.gifNr r!r"r#rTr"r#rr$r%r&r r s" DgG I r%r c&eZdZdZdZddgZdgZdZy)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*.jpgNr'r$r%r&r r s$ DfoG I!r%r c&eZdZdZdZddgZdgZdZy)rz 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*.bmpNr'r$r%r&rrs% DhG I r%r) r#r5r:pygments.formatterr pygments.utilrrrrrLPILrr r r ImportErrorrtwinreg__all__r9rAr=r?r Exceptionr(r*r r r rr$r%r&r5s (//M  Gh'(!=1  +%7k7393QQho4Yo4j ! ! " " ! !UM  sF BB%B"!B"%C+B0/C0B:7C9B::C?C