img formatter renders text at wrong position (on Windows?) #1213
(Original issue 1509 created by hhsprings on 2019-04-26T01:11:55.667733+00:00)
Note that I have reproduced this problem only on Windows (Japanese edition).
In the case where the lexer is other than "text", the drawing position of tokens other than the first one appearing on the line is wrong.
If you change the passing text to getsize in the "get_char_size" method of class FontManager from "M" to "a", it will draw in the correct position, but I can't explain what's happened. Apart from this mysterious fix method, I think that in the first place the approach adopted does not work when it contains kanji characters.
Maybe I think this is the conclusion:
The text was updated successfully, but these errors were encountered:
I faced a similar problem.
I outputed the image by using ImgFormatter from this sample code containing Japanese characters:
# sample.py print("Hello, World!") print("こんにちは、世界！")
As hhsprings suggests, this may be due to calculating the start position as the number of characters multiplied by the number of widths of the letter "M", especially when the character widths are different.
This can be solved by using the
How about a fix like this one? ( 15b3/pygments@2bdcf50 )
PR is OK.
The calculation cost will be greater.
So I measured the time it took to converting 1000 lines of code on my iMac.
import time from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters import ImageFormatter if __name__ == "__main__": # large code preparation max_lines = 1000 line_sample = 'print("あああ", "いいい", "ううう", "えええ", "おおお", "かかか", "ききき", "くくく")' code = "\n".join([line_sample for i in range(max_lines)]) # start start = time.time() highlight(code, PythonLexer(), ImageFormatter(font_name="ipag")) # end print print(time.time() - start, "[sec]")
Result (twice each):