From 1d51df138f6aa8b4b2eed53ca8505dcfd225b562 Mon Sep 17 00:00:00 2001 From: strawberry beach sandals <30496251+15b3@users.noreply.github.com> Date: Sat, 28 Nov 2020 18:18:59 +0900 Subject: [PATCH] ImgFormatter: Use the start position based on the length of text (#1611) --- pygments/formatters/img.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/pygments/formatters/img.py b/pygments/formatters/img.py index 6a9a1eb83c..ef973617f6 100644 --- a/pygments/formatters/img.py +++ b/pygments/formatters/img.py @@ -209,6 +209,12 @@ def get_char_size(self): """ return self.fonts['NORMAL'].getsize('M') + def get_text_size(self, text): + """ + Get the text size(width, height). + """ + return self.fonts['NORMAL'].getsize(text) + def get_font(self, bold, oblique): """ Get the font based on bold and italic flags. @@ -419,17 +425,17 @@ def _get_char_width(self): """ return self.fontw - def _get_char_x(self, charno): + def _get_char_x(self, linelength): """ Get the X coordinate of a character position. """ - return charno * self.fontw + self.image_pad + self.line_number_width + return linelength + self.image_pad + self.line_number_width - def _get_text_pos(self, charno, lineno): + def _get_text_pos(self, linelength, lineno): """ Get the actual position for a character and line position. """ - return self._get_char_x(charno), self._get_line_y(lineno) + return self._get_char_x(linelength), self._get_line_y(lineno) def _get_linenumber_pos(self, lineno): """ @@ -453,11 +459,11 @@ def _get_style_font(self, style): """ return self.fonts.get_font(style['bold'], style['italic']) - def _get_image_size(self, maxcharno, maxlineno): + def _get_image_size(self, maxlinelength, maxlineno): """ Get the required image size. """ - return (self._get_char_x(maxcharno) + self.image_pad, + return (self._get_char_x(maxlinelength) + self.image_pad, self._get_line_y(maxlineno + 0) + self.image_pad) def _draw_linenumber(self, posno, lineno): @@ -483,6 +489,7 @@ def _create_drawables(self, tokensource): Create drawables for the token content. """ lineno = charno = maxcharno = 0 + maxlinelength = linelength = 0 for ttype, value in tokensource: while ttype not in self.styles: ttype = ttype.parent @@ -497,17 +504,22 @@ def _create_drawables(self, tokensource): temp = line.rstrip('\n') if temp: self._draw_text( - self._get_text_pos(charno, lineno), + self._get_text_pos(linelength, lineno), temp, font = self._get_style_font(style), fill = self._get_text_color(style) ) + temp_width, temp_hight = self.fonts.get_text_size(temp) + linelength += temp_width + maxlinelength = max(maxlinelength, linelength) charno += len(temp) maxcharno = max(maxcharno, charno) if line.endswith('\n'): # add a line for each extra line in the value + linelength = 0 charno = 0 lineno += 1 + self.maxlinelength = maxlinelength self.maxcharno = maxcharno self.maxlineno = lineno @@ -551,7 +563,7 @@ def format(self, tokensource, outfile): self._draw_line_numbers() im = Image.new( 'RGB', - self._get_image_size(self.maxcharno, self.maxlineno), + self._get_image_size(self.maxlinelength, self.maxlineno), self.background_color ) self._paint_line_number_bg(im)