arklumpus/VectSharp

How to use Chinese font in latest version (2.2.1) repo.

Closed this issue · 6 comments

Hello:

I have to use some Chinese font in my program for the repo.
I install some Chinese font in Windows 10, at the following location:
C:\Windows\Fonts\Chinese.ttf

In old version (1.8.1) I can write the statement to use Chinese font:
FontFamily fnt = (new FontFamily(@"C:\Windows\Fonts\Chinese.ttf"), 20);

But in current version (2.2.1), I can’t find how to use ResolveFontFamily to specify the font location.
Please advise!
Thanks,

If the font is in a file, you can just create the FontFamily using the ResolveFontFamily method:

FontFamily family = FontFamily.ResolveFontFamily("/path/to/font.ttf");

See here for more details.

If you have the font file in a Stream (e.g., an embedded resource), it is slightly more complicated because you need to create a font library object (detailed instructions here), but this is not necessary if the font is a file on disk.

Hello:
Thanks for your reply. But I have yet another issue:
The following code works for English fonts:

        Font titleFont =
            new(ResolveFontFamily(StandardFontFamilies.HelveticaBold), 16);
        Font regularFont =
            new(ResolveFontFamily(StandardFontFamilies.Helvetica), 14);

        double rowHeight = RenderRow(table[i], columnWidths, titleFont, regularFont, pag.Graphics, i < table.Count - 1);

But if I use Chinese font family, like the code:
FontFamily chinese_font = ResolveFontFamily("/path/to/font.ttf");
Then how I can rewrite this statement?
double rowHeight = RenderRow(table[i], columnWidths, titleFont, regularFont, pag.Graphics, i < table.Count - 1);
Thanks,

You should probably replace titleFont or regularFont in the method call with chinese_font, like:

double rowHeight = RenderRow(table[i], columnWidths, titleFont, chinese_font, pag.Graphics, i < table.Count - 1);

// or

double rowHeight = RenderRow(table[i], columnWidths, chinese_font, chinese_font, pag.Graphics, i < table.Count - 1);

Depending on whether the titles or the contents of the table are written in Chinese characterd.

Hello:
I got error message:
Error CS1503 Argument 4: cannot convert from 'VectSharp.FontFamily' to 'VectSharp.Font'
The following is my code:
FontFamily chinese_font = ResolveFontFamily(Chinese_Font_Path);
double rowHeight = RenderRow(table[i], columnWidths, titleFont, chinese_font, pag.Graphics, i < table.Count - 1);
Usually the title font is in English, but the text font is in Chinese.
How I can covert from FontFamily to Font? I am using Visual Studio 2022, and my program is targeting .NET 6.0.

That's because ResolveFontFamily returns a FontFamily and not a Font. You need to create the font:

FontFamily chineseFontFamily = FontFamily.ResolveFontFamily(Chinese_Font_Path);
double fontSize = 20;
Font chineseFont = new Font(chineseFontFamily, fontSize);

Or

Font chineseFont = new Font(FontFamily.ResolveFontFamily(Chinese_Font_Path), 20);

It works!
Thanks,