【问题标题】:How to implement the outline effect on text with WindowsAPICodePack?如何使用 WindowsAPICodePack 实现对文本的轮廓效果?
【发布时间】:2023-03-18 08:33:01
【问题描述】:

在我们完成其他要求后,我仍在努力解决这个问题。我发现我们可以使用 GeometrySink 类似的类来实现轮廓效果;但我不熟悉c++;见这篇文章: http://msdn.microsoft.com/en-us/library/dd317121.aspx

可以使用 ID2D1GeometrySink 创建更复杂的形状 接口指定一系列由直线、曲线、 和弧线。 ID2D1GeometrySink 被传递给 Open 方法 ID2D1PathGeometry 生成复杂的几何图形。 ID2D1SimplifiedGeometrySink 也可以与 DirectWrite API 一起使用 提取格式化文本的路径轮廓以进行艺术渲染。

如果您有任何建议或想法,请告诉我。

最好的问候, 霍华德

【问题讨论】:

    标签: text outline direct2d windows-api-code-pack


    【解决方案1】:

    我使用SharpDX。这是代码片段(这是正在进行的工作,我刚刚得到它的渲染,但它可能是您正在寻找的东西)。

       public class OutlineTextRender : SharpDX.DirectWrite.TextRenderer
        {
            readonly Factory _factory;
            readonly RenderTarget _surface;
            readonly Brush _brush;
    
            public OutlineTextRender(RenderTarget surface, Brush brush)
            {
                _factory = surface.Factory;
                _surface = surface;
                _brush = brush;
            }
    
            public Result DrawGlyphRun(object clientDrawingContext, float baselineOriginX, float baselineOriginY, MeasuringMode measuringMode, SharpDX.DirectWrite.GlyphRun glyphRun, SharpDX.DirectWrite.GlyphRunDescription glyphRunDescription, ComObject clientDrawingEffect)
            {
                using (PathGeometry path = new PathGeometry(_factory))
                {
                    using (GeometrySink sink = path.Open())
                    {
                        glyphRun.FontFace.GetGlyphRunOutline(glyphRun.FontSize, glyphRun.Indices, glyphRun.Advances, glyphRun.Offsets, glyphRun.IsSideways, (glyphRun.BidiLevel % 2) > 0, sink);
    
                        sink.Close();
                    }
    
                    Matrix matrix = Matrix.Identity;
                    matrix = matrix * Matrix.Translation(baselineOriginX, baselineOriginY, 0);
    
                    TransformedGeometry transformedGeometry = new TransformedGeometry(_factory, path, matrix);
    
                    _surface.DrawGeometry(transformedGeometry, _brush);
    
                }
                return new Result();
            }
    
            public Result DrawInlineObject(object clientDrawingContext, float originX, float originY, SharpDX.DirectWrite.InlineObject inlineObject, bool isSideways, bool isRightToLeft, ComObject clientDrawingEffect)
            {
                return new Result();
            }
    
            public Result DrawStrikethrough(object clientDrawingContext, float baselineOriginX, float baselineOriginY, ref SharpDX.DirectWrite.Strikethrough strikethrough, ComObject clientDrawingEffect)
            {
                return new Result();
            }
    
            public Result DrawUnderline(object clientDrawingContext, float baselineOriginX, float baselineOriginY, ref SharpDX.DirectWrite.Underline underline, ComObject clientDrawingEffect)
            {
                return new Result();
            }
    
            public SharpDX.DirectWrite.Matrix GetCurrentTransform(object clientDrawingContext)
            {
                return new SharpDX.DirectWrite.Matrix();
            }
    
            public float GetPixelsPerDip(object clientDrawingContext)
            {
                return 0;
            }
    
            public bool IsPixelSnappingDisabled(object clientDrawingContext)
            {
                return true; ;
            }
    
            public IDisposable Shadow
            {
                get
                {
                    return null;
                }
                set
                {
                   // throw new NotImplementedException();
                }
            }
    
            public void Dispose()
            {
    
            }
        }
    
        public void strokeText(string text, float x, float y, float maxWidth)
        {
            // http://msdn.microsoft.com/en-us/library/windows/desktop/dd756692(v=vs.85).aspx
    
            // FIXME make field
            SharpDX.DirectWrite.Factory factory = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared);
    
            TextFormat format = new TextFormat(factory, "Verdana", 50);
            SharpDX.DirectWrite.TextLayout layout = new SharpDX.DirectWrite.TextLayout(factory, text, format, 50, 1000);
    
    
    
    
    
            using (var brush = new SolidColorBrush(_surface, CurrentColor))
            {
                var render = new OutlineTextRender(_surface, brush);
    
                layout.Draw(render, x, y);
    
                //_surface.DrawTextLayout(new DrawingPointF(x, y), layout, brush);
    
             //   _surface.DrawGlyphRun(new DrawingPointF(x, y), run, brush, MeasuringMode.Natural);
            }
    
        }
    

    【讨论】:

      【解决方案2】:

      我把它变成了一个通用的 XAML 大纲文本渲染器。感谢发帖。

      https://blogs.msdn.microsoft.com/theuxblog/2016/07/09/outline-text-from-a-windows-10-store-xaml-app-using-sharpdx/

      干杯, 保罗

      【讨论】:

        猜你喜欢
        • 2011-06-22
        • 1970-01-01
        • 2010-10-01
        • 1970-01-01
        • 2015-04-02
        • 2012-12-11
        • 1970-01-01
        • 2014-09-17
        相关资源
        最近更新 更多