@@ -452,6 +452,16 @@ def _get_text_color(self, style):
452452 fill = '#000'
453453 return fill
454454
455+ def _get_text_bg_color (self , style ):
456+ """
457+ Get the correct background color for the token from the style.
458+ """
459+ if style ['bgcolor' ] is not None :
460+ bg_color = '#' + style ['bgcolor' ]
461+ else :
462+ bg_color = None
463+ return bg_color
464+
455465 def _get_style_font (self , style ):
456466 """
457467 Get the correct font for the style.
@@ -474,14 +484,15 @@ def _draw_linenumber(self, posno, lineno):
474484 str (lineno ).rjust (self .line_number_chars ),
475485 font = self .fonts .get_font (self .line_number_bold ,
476486 self .line_number_italic ),
477- fill = self .line_number_fg ,
487+ text_fg = self .line_number_fg ,
488+ text_bg = None ,
478489 )
479490
480- def _draw_text (self , pos , text , font , ** kw ):
491+ def _draw_text (self , pos , text , font , text_fg , text_bg ):
481492 """
482493 Remember a single drawable tuple to paint later.
483494 """
484- self .drawables .append ((pos , text , font , kw ))
495+ self .drawables .append ((pos , text , font , text_fg , text_bg ))
485496
486497 def _create_drawables (self , tokensource ):
487498 """
@@ -506,7 +517,8 @@ def _create_drawables(self, tokensource):
506517 self ._get_text_pos (linelength , lineno ),
507518 temp ,
508519 font = self ._get_style_font (style ),
509- fill = self ._get_text_color (style )
520+ text_fg = self ._get_text_color (style ),
521+ text_bg = self ._get_text_bg_color (style ),
510522 )
511523 temp_width , temp_hight = self .fonts .get_text_size (temp )
512524 linelength += temp_width
@@ -576,8 +588,11 @@ def format(self, tokensource, outfile):
576588 y = self ._get_line_y (linenumber - 1 )
577589 draw .rectangle ([(x , y ), (x + rectw , y + recth )],
578590 fill = self .hl_color )
579- for pos , value , font , kw in self .drawables :
580- draw .text (pos , value , font = font , ** kw )
591+ for pos , value , font , text_fg , text_bg in self .drawables :
592+ if text_bg :
593+ text_size = draw .textsize (text = value , font = font )
594+ draw .rectangle ([pos [0 ], pos [1 ], pos [0 ] + text_size [0 ], pos [1 ] + text_size [1 ]], fill = text_bg )
595+ draw .text (pos , value , font = font , fill = text_fg )
581596 im .save (outfile , self .image_format .upper ())
582597
583598
0 commit comments