retext-project / retext

ReText: Simple but powerful editor for Markdown and reStructuredText
GNU General Public License v2.0
1.88k stars 196 forks source link

Problems of table editing mode about Chinese characters #598

Open theopathyreg opened 2 years ago

theopathyreg commented 2 years ago

When use table edit mode, there are some problems when input Chinese characters. because one Chinese character takes two letters space. Can't make the line fix together in edit window. I think that Japanese and some other characters should have the same problems. Really hope it can be fixed. Thank you. example

|      |     |     |
|------|-----|-----|
|你好    |     |     |
|你们好   |     |     |
|中文不能对齐|     |     |
mitya57 commented 1 year ago

I took a look at this today. Here are some notes:

This problem happens not only with East Asian languages, but also with emojis, zero-width characters and more.

How much width a character takes depends on a font. Some fonts try to fit all characters into a single width, some fonts are duospaced.

To get a character width in a font, one can use QFontMetrics class (we are already using it for some other purposes). Also, Python has an east_asian_width function in the standard library which returns a code from this classifier.

The problem with the current table editing mode code (written by @Griffon26) is that Qt gives us only the number of added or removed characters, not their values or widths: https://github.com/retext-project/retext/blob/0987c28226714633a470b8b8a7ee89de9c7f6bc9/ReText/editor.py#L441

So to fix this issue one would need to rewrite this code to align cells without relying on information about added/removed lengths.

Pull requests for this are welcome. I am not sure if I will ever have time to rewrite this :(