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

Can CsConsoleFormat support output chinese words? #27

Open
wangshuai-007 opened this issue Jul 13, 2020 · 4 comments
Open

Can CsConsoleFormat support output chinese words? #27

wangshuai-007 opened this issue Jul 13, 2020 · 4 comments
Assignees
Labels
type:improvement Improvement

Comments

@wangshuai-007
Copy link

If I output chinese words,the grid will broken,I think the problem is in the character calculation of Chinese characters.
My code is like this:

static void Main(string[] args)
        {
            var headerThickness = new LineThickness(LineWidth.Double, LineWidth.Single);

            Console.WriteLine("outpt Chinese");
            var doc = new Document(
                new Span("Order #") { Color = Yellow }, "Id", "\n",
                new Span("Customer: ") { Color = Yellow }, "Name",
                new Grid
                {
                    Color = Gray,
                    Columns = { GridLength.Char(10), GridLength.Char(20), GridLength.Char(70) },
                    Children = {
                        new Cell("Id") { Stroke = headerThickness ,TextAlign =TextAlign.Center},
                        new Cell("Name") { Stroke = headerThickness ,TextAlign =TextAlign.Center},
                        new Cell("Assembly") { Stroke = headerThickness,TextAlign =TextAlign.Center },
                        new[] {
                            new Cell("AAAAA"),
                            new Cell("这里是中文,有错误!"),
                            new Cell("BBBBBBBBBBBBBBBBBBBBBBBB"),
                        }
                    }
                }
            );

            ConsoleRenderer.RenderDocument(doc);
            Console.WriteLine("outpt English");
            doc = new Document(
                new Span("Order #") { Color = Yellow }, "Id", "\n",
                new Span("Customer: ") { Color = Yellow }, "Name",
                new Grid
                {
                    Color = Gray,
                    Columns = { GridLength.Char(10), GridLength.Char(20), GridLength.Char(70) },
                    Children = {
                        new Cell("Id") { Stroke = headerThickness ,TextAlign =TextAlign.Center},
                        new Cell("Name") { Stroke = headerThickness ,TextAlign =TextAlign.Center},
                        new Cell("Assembly") { Stroke = headerThickness,TextAlign =TextAlign.Center },
                        new[] {
                            new Cell("AAAAA"),
                            new Cell("This is English,And it is OK"),
                            new Cell("BBBBBBBBBBBBBBBBBBBBBBBB"),
                        }
                    }
                }
            );

            ConsoleRenderer.RenderDocument(doc);
        }

the output is the follow.
image

@Athari
Copy link
Owner

Athari commented Jul 13, 2020

@wangshuai-007
The library assumes a monospace font as it's the only way to reliably align text using spaces. The font on your screen is kinda-monospace-but-not-really, with Chinese characters being exactly twice the width of English characters. In theory, the library can be extended to support this case, as all paragraph alignment is in one place, but there will be many hidden problems like trying to align text within 1-character cell or using wide characters as a grid line... Word wrapping is hacky too. While it supports all sorts of Unicode spaces, it relies on very specific characters instead of implementing true Unicode word wrapping support.

However, the main problem is that .NET doesn't provide any API which can help with measuring these lines of text. No API for getting the current console font, no API to get printed console text length.

Are all console "monspace" fonts with support for Chinese characters like this, with hieroglyphs being exactly twice in width compared to the rest of the characters? Maybe something can be done with this assumption...

Note to self. Related:

@Athari Athari self-assigned this Jul 13, 2020
@Athari Athari added the type:improvement Improvement label Jul 13, 2020
@wangshuai-007
Copy link
Author

@Athari
Can CsConsoleFormat support a custom event to calculate the width of output?
Then regardless of the font, the grid will be aligned correctly.

@Athari
Copy link
Owner

Athari commented Jul 13, 2020

@wangshuai-007
That's the same feature. I can't implement this without running code and doing tests, so I'd rather test it on something practical that I can see with my own eyes.

Also, how much broken word wrapping will affect you? If the library doesn't find any good old spaces and hyphens, it'll fallback to breaking on the last character in a line, whatever it is.

@wangshuai-007
Copy link
Author

What does it mean?

If the library doesn't find any good old spaces and hyphens, it'll fallback to breaking on the last character in a line, whatever it is.

It is not necessary to output multiple line words for me.But may be needed for others.
I think,If font width and broken word is both a problem,the custom event can deal with them together.
The name of the event can be CustomCellWitdth,It calculate the width and height for a cell.

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

No branches or pull requests

2 participants