retext-project / retext

ReText: Simple but powerful editor for Markdown and reStructuredText

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problems of table editing mode about Chinese characters

theopathyreg opened this issue · comments

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

|      |     |     |
|------|-----|-----|
|你好    |     |     |
|你们好   |     |     |
|中文不能对齐|     |     |

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:

tablemode.adjustTableToChanges(self.document(), pos, added - removed, markupClass)

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 :(