波浪下划线作为 TextDecoration:我做错了什么?

发布于 2024-07-15 01:26:56 字数 1176 浏览 4 评论 0原文

我想使用 TextDecoration (在类似于 RichTextBox 的控件中)创建 波浪下划线

我做了以下事情:

private static Pen CreateErrorPen() {
    var geometry = new StreamGeometry();
    using (var context = geometry.Open()) {
        context.BeginFigure(new Point(0.0, 0.0), false, false);
        context.PolyLineTo(new[] {
            new Point(0.75, 0.75),
            new Point(1.5, 0.0),
            new Point(2.25, 0.75),
            new Point(3.0, 0.0)
        }, true, true);
    }

    var brushPattern = new GeometryDrawing {
        Pen = new Pen(Brushes.Red, 0.2),
        Geometry = geometry
    };

    var brush = new DrawingBrush(brushPattern) {
        TileMode = TileMode.Tile,
        Viewport = new Rect(0.0, 1.5, 9.0, 3.0),
        ViewportUnits = BrushMappingMode.Absolute
    };

    var pen = new Pen(brush, 3.0);
    pen.Freeze();

    return pen;
}

这几乎有效,但根据文本中带下划线的单词位置,下划线通常显示为几个叠加的波浪的图案。 而且,即使下划线是正确的,下划线也有点模糊(我想,wpf 在像素之间绘图存在问题)。

我的解决方案是一种反复试验,所以我可能走错了路,尤其是使用 Viewport/ViewportUnits 时。

我做错了什么,有没有办法获得清晰的下划线?

提前致谢。

I want to create wavy underlines using TextDecoration (in a control similar to RichTextBox).

I did the following thing:

private static Pen CreateErrorPen() {
    var geometry = new StreamGeometry();
    using (var context = geometry.Open()) {
        context.BeginFigure(new Point(0.0, 0.0), false, false);
        context.PolyLineTo(new[] {
            new Point(0.75, 0.75),
            new Point(1.5, 0.0),
            new Point(2.25, 0.75),
            new Point(3.0, 0.0)
        }, true, true);
    }

    var brushPattern = new GeometryDrawing {
        Pen = new Pen(Brushes.Red, 0.2),
        Geometry = geometry
    };

    var brush = new DrawingBrush(brushPattern) {
        TileMode = TileMode.Tile,
        Viewport = new Rect(0.0, 1.5, 9.0, 3.0),
        ViewportUnits = BrushMappingMode.Absolute
    };

    var pen = new Pen(brush, 3.0);
    pen.Freeze();

    return pen;
}

This almost works, but depending on the underlined word position in text, underlines often show up as a pattern of several superimposed waves. Also the underlines are a bit blurry even when they are correct (wpf problem with drawing between pixels, I suppose).

My solution was kind of a trial-and-error, so I might have gone a wrong way, especially with Viewport/ViewportUnits.

What am I doing wrong and is there a way to get crisp underlines?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长亭外,古道边 2024-07-22 01:26:56

bstoney此处。 关键似乎是设置 Viewbox 以及 Viewport 以使波浪垂直分开,因此下划线中只能得到 1。

波浪中存在一些中断,可以通过将其向右延伸并更改 Viewbox 来消除,使其从 X=1 而不是 0 开始:

<VisualBrush x:Key="WavyBrush" TileMode="Tile" Viewbox="1,0,3,3" ViewboxUnits="Absolute" Viewport="0,-1,6,4" ViewportUnits="Absolute">
    <VisualBrush.Visual>
        <Path Data="M 0,1 C 1,0 2,2 3,1 4,0 5,2 6,1" Stroke="Red" StrokeThickness="0.2"/>
    </VisualBrush.Visual>
</VisualBrush>

bstoney had a solution to this problem here. The key seems to be setting the Viewbox as well as the Viewport such that the waves are seperated vertically, so you only get 1 in the underline.

There are some breaks in the wave that can be eliminated by extending it to the right and changing the Viewbox from so it starts from X=1 instead of 0:

<VisualBrush x:Key="WavyBrush" TileMode="Tile" Viewbox="1,0,3,3" ViewboxUnits="Absolute" Viewport="0,-1,6,4" ViewportUnits="Absolute">
    <VisualBrush.Visual>
        <Path Data="M 0,1 C 1,0 2,2 3,1 4,0 5,2 6,1" Stroke="Red" StrokeThickness="0.2"/>
    </VisualBrush.Visual>
</VisualBrush>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文