gpt4 book ai didi

c# - WPF : Glyph right-to-left text rendering

转载 作者:行者123 更新时间:2023-11-30 16:42:00 25 4
gpt4 key购买 nike

我想使用 Glyph 呈现从右到左的语言文本(例如阿拉伯语或混合英语和阿拉伯语)。

我正在使用这段代码:

    Typeface typeface = new Typeface(new FontFamily("Arial"),
FontStyles.Italic,
FontWeights.Normal,
FontStretches.Normal);

GlyphTypeface glyphTypeface;
if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
throw new InvalidOperationException("No glyphtypeface found");

string text = "Hello , سلام";
double size = 40;

ushort[] glyphIndexes = new ushort[text.Length];
double[] advanceWidths = new double[text.Length];

double totalWidth = 0;

for (int n = 0; n < text.Length; n++)
{
ushort glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];
glyphIndexes[n] = glyphIndex;

double width = glyphTypeface.AdvanceWidths[glyphIndex] * size;
advanceWidths[n] = width;

totalWidth += width;
}

Point origin = new Point(50, 50);

GlyphRun glyphRun = new GlyphRun(glyphTypeface, 0, false, size,
glyphIndexes, origin, advanceWidths, null, null, null, null,
null, null);

dc.DrawGlyphRun(Brushes.Black, glyphRun);

但问题是阿拉伯字符是分开显示的,像这样:

 Hello  ,  س ل ا م

请指导我

-------------------------------------------- ----------

更新:

这是混淆解决方案的结果: image1

问题是阿拉伯字母是分开渲染的。

但这就是我想要的: image2

最佳答案

尝试用不同的 CultureInfo 格式化两个字符串

string x = string.Format(new CultureInfo("en-US"), "{0}" ,text1);
string y = string.Format(new CultureInfo("ar-SA"), "{0}", text2);

已更新解决方案...xaml 有一个 Canvas 。

<Window x:Class="StackOverflowCS.MainWindow"
Title="MainWindow" Height="600" Width="800">
<Grid>
<Canvas Name="MyCanvas" Height="600" Width="800"/>
</Grid>
</Window>

主窗口 (WPF)

   public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

Size s = new Size(200,50);
GlyphElement gex = new GlyphElement(
string.Format(new CultureInfo("en-US"), "{0}", "Hello"), Brushes.Red, s);
MyCanvas.Children.Add(gex);
Canvas.SetTop(gex, 10);
Canvas.SetLeft(gex, 10);

GlyphElement gey = new GlyphElement(
string.Format(new CultureInfo("ar-SA"), "{0}", "سلام"), Brushes.Black, s);
MyCanvas.Children.Add(gey);
Canvas.SetTop(gey, 100);
Canvas.SetLeft(gey, 10);
}
}

将 GlyphElement 包装在 FrameworkElement 中

    class GlyphElement : FrameworkElement
{
private GlyphRunner gr;
public GlyphElement(string text, Brush br, Size size) { gr = new GlyphRunner(text, br, size); }
protected override Visual GetVisualChild(int index) { return gr; }
protected override int VisualChildrenCount { get { return 1; } }
}

在 DrawingVisual 中包装 GlyphRunner

    public class GlyphRunner : DrawingVisual
{
public GlyphRunner(string text, Brush bc, Size size)
{
DrawingImage di = CreateGlyph(text, new Point(0, 0), bc);
using (DrawingContext dc = RenderOpen())
{
dc.DrawImage(di,new Rect(size));
}
}

// small updates to your code follow
public DrawingImage CreateGlyph(string text, Point origin, Brush bc)
{
Typeface typeface = new Typeface(new FontFamily("Arial"),
FontStyles.Normal,
FontWeights.Normal,
FontStretches.Normal);

GlyphTypeface glyphTypeface;
if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
throw new InvalidOperationException("No glyphtypeface found");

ushort[] glyphIndexes = new ushort[text.Length];
double[] advanceWidths = new double[text.Length];

double totalWidth = 0;

for (int n = 0; n < text.Length; n++)
{
ushort glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];
glyphIndexes[n] = glyphIndex;

double width = glyphTypeface.AdvanceWidths[glyphIndex];
advanceWidths[n] = width;

totalWidth += width;
}

float ppd = (float)VisualTreeHelper.GetDpi(this).PixelsPerDip;
GlyphRun gr = new GlyphRun(glyphTypeface, 0, false, 1.0, ppd, glyphIndexes, origin, advanceWidths, null, null, null, null, null, null);
GlyphRunDrawing glyphRunDrawing = new GlyphRunDrawing(bc, gr);
return new DrawingImage(glyphRunDrawing);
}

}

关于c# - WPF : Glyph right-to-left text rendering,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47490542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com