If your chinese like this:
You may set fonts for your render method ;
You can download ttf file and include in your project.
Then init fonts before use it.
/// <summary>
/// init fonts file in linux and return ttf file path
/// </summary>
/// <param name="resourcePath"></param>
/// <param name="_logger"></param>
/// <returns></returns>
public static List<string> InitFonts(string resourcePath,ILogger _logger)
{
_logger.LogTrace("start init fonts");
const string fontsPath = "/usr/share/fonts";
if (!Directory.Exists(fontsPath))
{
_logger.LogWarning("the path of:/usr/share/fonts is not exist ignore init");
return new List<string>();
}
var groupBuyingPath = Path.Combine(fontsPath, "groupbuying");
var pfRegular = Path.Combine(groupBuyingPath, "PingFang-SC-Regular.ttf");
var pfMedium = Path.Combine(groupBuyingPath, "PingFang-SC-Medium.ttf");
var alibaba = Path.Combine(groupBuyingPath, "Alibaba-PuHuiTi-M.ttf");
if (File.Exists(pfRegular))
{
_logger.LogTrace("the file:{path} is exist ignore init", pfRegular);
return new List<string>();
}
if (!Directory.Exists(groupBuyingPath))
{
Directory.CreateDirectory(groupBuyingPath);
_logger.LogTrace("auto create fonts folder in :{path}", fontsPath);
}
File.Copy(Path.Combine(resourcePath, "Fonts", "PingFang-SC-Regular.ttf"), pfRegular);
File.Copy(Path.Combine(resourcePath, "Fonts", "PingFang-SC-Medium.ttf"), pfMedium);
File.Copy(Path.Combine(resourcePath, "Fonts","Alibaba-PuHuiTi-M.ttf"),alibaba);
_logger.LogTrace("ttf file copy success");
var psi = new ProcessStartInfo
{
FileName = "mkfontscale",
WorkingDirectory = groupBuyingPath,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
};
var proc = new Process
{
StartInfo = psi
};
proc.Start();
var psi1 = new ProcessStartInfo
{
FileName = "mkfontdir",
WorkingDirectory = groupBuyingPath,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
};
var proc1 = new Process
{
StartInfo = psi1
};
proc1.Start();
var psi2 = new ProcessStartInfo
{
FileName = "fc-cache",
WorkingDirectory = groupBuyingPath,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
};
var proc2 = new Process
{
StartInfo = psi2
};
proc2.Start();
_logger.LogTrace("run command success");
return new List<string>() {pfRegular, pfMedium, alibaba};
}
my ttf file is in resourcePath/Fonts
.
After run InitFonts
method ,you can ls /usr/share/fonts/groupbuying/
in container (by docker exec -it xxx bash
) and see fonts.dir fonts.scale
Or use fc-list
check fonts is correct.
If you are not sure your fonts name,You can add this API to test fonts.
[HttpGet("[action]")]
public string GetFont(string fontName)
{
var wwwrootPath = "wwwroot";
var directory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, wwwrootPath);
var rootPath = $"resource";
var resourcePath = Path.Combine(directory, rootPath);
InitFonts(resourcePath, _log);
var font = new Font(fontName, 36, FontStyle.Bold, GraphicsUnit.Pixel);
return font.Name;
}
If the api return font name as your param,the InitFonts
works well.
Maybe,you will run your images as root by use docker run --user root
apt-cache search fonts | grep -iE '^fonts.*(chinese|arabic)'
– Pianola