-
Notifications
You must be signed in to change notification settings - Fork 695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
img formatter renders text at wrong position (on Windows?) #1213
Comments
(Original comment by hhsprings on 2019-04-26T06:06:48.733712+00:00) Sorry to attach the modified source code directly. I got lost about how to do PR with BitBucket + Mercurial. |
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("こんにちは、世界!") Outputed Image: 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 ) |
That does sound reasonable -- can you please file a PR for it? Does this have significant drawbacks in terms of performance, as it needs one call per line now? |
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):
|
Thanks a lot! That's a 20% slowdown, but given I don't see a better option for handling this, and it's not a quick process to start with, let's go with that for now. |
(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:
used versions:
The text was updated successfully, but these errors were encountered: