3

Now I'm using SkiaSharp on Xamarin Android.

I wanna draw Japanese text with SKCanvas.DrawText ,but japanese characters were garbled.

I saw this similar question,so I tried to convert text to UTF-8, but result is same.

I tried like below.

var s = "abcあいう123壱弐参";
var buf = System.Text.Encoding.UTF8.GetBytes(s);
var utf8s = Encoding.UTF8.GetString(buf);
skcanvas.DrawText(utf8s, 50, 50, paint);

another one.

var s = "abcあいう123壱弐参";
var dest = Encoding.UTF8;
var src = Encoding.Default;
var buf = src.GetBytes(s);
var buf2 = Encoding.Convert(src,dest, buf);
var utf8s = dest.GetString(buf2);
skcanvas.DrawText(utf8s, 50, 50, paint);

Both result are same. "abc" and "123" is drawn well,but Japanese character are garbled.

Any idea?

4

2 回答 2

5

假设您正在加载自定义字体,例如Uzumasa Honmaru Gothic,您可以将其添加到您的每个本机平台项目中,然后通过该本机项目或Xamarin.Forms基于 - 的项目使用它:

安卓:

string fontName = "UzumasaFontMini.otf";
string fontPath = Path.Combine (CacheDir.AbsolutePath, fontName);
using (var asset = Assets.Open (fontName))
using (var dest = File.Open (fontPath, FileMode.Create)) {
    asset.CopyTo (dest);
}
string customFontPath = fontPath;

IOS:

string fontName = "UzumasaFontMini.otf";
string customFontPath = NSBundle.MainBundle.PathForResource (Path.GetFileNameWithoutExtension (fontName), Path.GetExtension (fontName));

绘图文本:

string text = "abcあいう123";
using (var paint = new SKPaint ()) {
    canvas.Clear (SKColors.Black);
    paint.IsAntialias = true;

    using (var tf = SKTypeface.FromFile (customFontPath)) {
        paint.Color = SKColors.White;
        paint.TextSize = 20;
        paint.Typeface = tf;

        canvas.DrawText (text, 50, 50, paint);
    }
}

安卓:

在此处输入图像描述

IOS:

在此处输入图像描述

于 2016-06-09T15:09:34.657 回答
1

您可以使用“matchCharacter()”,输入参数可能只是一个日文字符,如“あ”

var japanese = fontManager.MatchCharacter("Courier New", '年')
paint.Typeface = japanese;
canvas.DrawText("abcあいう123", x, 300, paint);

参考链接:https ://github.com/mono/SkiaSharp/blob/master/samples/SkiaSharpSample.Shared/Samples/UnicodeTextSample.cs

于 2018-01-15T17:25:55.210 回答