Skip to content

Commit

Permalink
ImgFormatter: Use the start position based on the length of text (#1611)
Browse files Browse the repository at this point in the history
  • Loading branch information
15b3 committed Nov 28, 2020
1 parent d004c05 commit 1d51df1
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions pygments/formatters/img.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1d51df1

Please sign in to comment.