Skip to content
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

[REQUEST] Add support for more characters for SVG formatting #2526

Closed
onlyacat opened this issue Sep 19, 2022 · 5 comments · Fixed by #2539
Closed

[REQUEST] Add support for more characters for SVG formatting #2526

onlyacat opened this issue Sep 19, 2022 · 5 comments · Fixed by #2539

Comments

@onlyacat
Copy link
Contributor

Current status
For now, Rich uses the the Fora Code as the font for exporting and saving the SVG.

Currently, this font does not support some kind of characters like Chinese or Japanese, which causes tofu blocks after exporting. The issue can be referred to here: tonsky/FiraCode#620

A workaround is to specify the font that supports the Chinese in the CONSOLE_SVG_FORMAT.


However, another problem comes. Every character's width in FiraCode is 0.6, thus the tables or other formatting in the SVGs are in a mess. Because most of the fonts have width of 0.5.

Therefore, a solution is changing the hardcode width from 0.61 to 0.5.

rich/rich/console.py

Lines 2309 to 2311 in eaba862

width = self.width
char_height = 20
char_width = char_height * 0.61

How would you improve Rich?

we could change the font in the parameter but it would be hard to change the variable inside the lib. Will it be possible to help export the variable, or find other methods to solve it? Thanks in advance.

@onlyacat
Copy link
Contributor Author

1. Export directly

from rich.console import Console
from rich.table import Table

table = Table(title="测试")
table.add_column("Title 测试")
table.add_column("Title 2 测试")
table.add_row("测试","test")
table.add_row("test","测试")

console = Console(record=True)
console.print(table)
console.save_svg('output.svg')

image

2. Export with specifying font in CONSOLE_SVG_FORMAT

Here I use Sarasa Mono SC which supports the Chinese characters

...
console.save_svg('output.svg',code_format=CONSOLE_SVG_FORMAT)
...

image

3. Export with specifying font in CONSOLE_SVG_FORMAT and change the char_width from 0.61 to 0.5

image

@willmcgugan
Copy link
Collaborator

Yes, we would like to support CJK at all times. Will look in to that.

@darrenburns darrenburns linked a pull request Sep 23, 2022 that will close this issue
9 tasks
@github-actions
Copy link

Did I solve your problem?

Why not buy the devs a coffee to say thanks?

@darrenburns
Copy link
Member

darrenburns commented Sep 23, 2022

Hey @onlyacat, I've added a font_aspect_ratio to export_svg and save_svg. If you use a custom font, you can use this new parameter to modify the default 0.61 value. That'll be available in the next release.

@onlyacat
Copy link
Contributor Author

Hey @onlyacat, I've added a font_aspect_ratio to export_svg and save_svg. If you use a custom font, you can use this new parameter to modify the default 0.61 value. That'll be available in the next release.

help me a lot, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants