Merge pull request #341 from dotnet/strokedashoffset

Include StrokeDashOffset property
This commit is contained in:
jonlipsky 2022-03-10 08:44:30 -08:00 коммит произвёл GitHub
Родитель 79084b0c51 53d75ef828
Коммит 8c22f8e8d1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
24 изменённых файлов: 86 добавлений и 35 удалений

Просмотреть файл

@ -93,6 +93,7 @@ namespace GraphicsTester.Scenarios
{
canvas.StrokeSize = i;
canvas.StrokeDashPattern = DASHED;
canvas.StrokeDashOffset = 2;
canvas.DrawArc(50f, 200f + i * 30, 150, 20, 0, 180, false, false);
canvas.DrawArc(250.5f, 200.5f + i * 30, 150, 20, 0, 180, false, false);
}

Просмотреть файл

@ -69,6 +69,7 @@ namespace GraphicsTester.Scenarios
{
canvas.StrokeSize = i;
canvas.StrokeDashPattern = DASHED;
canvas.StrokeDashOffset = 2;
canvas.DrawLine(50, 100 + i * 10, 200, 100 + i * 10);
canvas.DrawLine(250, 100.5f + i * 10, 400, 100.5f + i * 10);
}

Просмотреть файл

@ -43,6 +43,7 @@ namespace GraphicsTester.Scenarios
{
canvas.StrokeSize = i;
canvas.StrokeDashPattern = DASHED;
canvas.StrokeDashOffset = 2;
canvas.DrawEllipse(50f, 200f + i * 30, 150, 20);
canvas.DrawEllipse(250.5f, 200.5f + i * 30, 150, 20);
}

Просмотреть файл

@ -49,6 +49,7 @@ namespace GraphicsTester.Scenarios
{
canvas.StrokeSize = i;
canvas.StrokeDashPattern = DASHED;
canvas.StrokeDashOffset = 2;
canvas.DrawRectangle(50f, 200f + i * 30, 150, 20);
canvas.DrawRectangle(250.5f, 200.5f + i * 30, 150, 20);
}

Просмотреть файл

@ -45,6 +45,7 @@ namespace GraphicsTester.Scenarios
{
canvas.StrokeSize = i;
canvas.StrokeDashPattern = DASHED;
canvas.StrokeDashOffset = 2;
canvas.DrawRoundedRectangle(50f, 200f + i * 30, 150, 20, 5);
canvas.DrawRoundedRectangle(250.5f, 200.5f + i * 30, 150, 20, 5);
}

Просмотреть файл

@ -306,14 +306,14 @@ namespace Microsoft.Maui.Graphics.Blazor
_context.Scale(fx, fy);
}
protected override void PlatformSetStrokeDashPattern(float[] pattern, float strokeSize)
protected override void PlatformSetStrokeDashPattern(float[] strokePattern, float strokeDashOffset, float strokeSize)
{
float[] finalPattern = null;
if (pattern != null)
if (strokePattern != null)
{
finalPattern = new float[pattern.Length];
for (int i = 0; i < pattern.Length; i++)
finalPattern[i] = pattern[i] * strokeSize;
finalPattern = new float[strokePattern.Length];
for (int i = 0; i < strokePattern.Length; i++)
finalPattern[i] = strokePattern[i] * strokeSize;
}
CurrentState.BlazorDashPattern = finalPattern;

Просмотреть файл

@ -480,7 +480,7 @@ namespace Microsoft.Maui.Graphics.GDI
set => CurrentState.StrokeWidth = value;
}
protected override void PlatformSetStrokeDashPattern(float[] pattern, float strokeSize)
protected override void PlatformSetStrokeDashPattern(float[] strokePattern, float strokeDashOffset, float strokeSize)
{
}

Просмотреть файл

@ -107,9 +107,9 @@ namespace Microsoft.Maui.Graphics.Platform.Gtk
set => CurrentState.BlendMode = value;
}
protected override void PlatformSetStrokeDashPattern(float[] pattern, float strokeSize)
protected override void PlatformSetStrokeDashPattern(float[] strokePattern, float strokeDashOffset, float strokeSize)
{
CurrentState.StrokeDashPattern = pattern;
CurrentState.StrokeDashPattern = strokePattern;
}
private void Draw(bool preserve = false)

Просмотреть файл

@ -787,9 +787,9 @@ namespace Microsoft.Maui.Graphics.SharpDX
CurrentState.SetShadow(offset, blur, color);
}
protected override void PlatformSetStrokeDashPattern(float[] pattern, float strokeSize)
protected override void PlatformSetStrokeDashPattern(float[] strokePattern, float strokeDashOffset, float strokeSize)
{
CurrentState.SetStrokeDashPattern(pattern, strokeSize);
CurrentState.SetStrokeDashPattern(strokePattern, strokeDashOffset, strokeSize);
}
protected override float PlatformStrokeSize

Просмотреть файл

@ -513,9 +513,9 @@ namespace Microsoft.Maui.Graphics.SharpDX
BlurRadius = 0;
}
public void SetStrokeDashPattern(float[] pattern, float strokeSize)
public void SetStrokeDashPattern(float[] strokePattern, float strokeDashOffset, float strokeSize)
{
if (pattern == null || pattern.Length == 0)
if (strokePattern == null || strokePattern.Length == 0)
{
if (_needsStrokeStyle == false) return;
_strokeStyleProperties.DashStyle = DashStyle.Solid;
@ -524,7 +524,7 @@ namespace Microsoft.Maui.Graphics.SharpDX
else
{
_strokeStyleProperties.DashStyle = DashStyle.Custom;
_dashes = pattern;
_dashes = strokePattern;
}
InvalidateStrokeStyle();

Просмотреть файл

@ -215,10 +215,11 @@ namespace Microsoft.Maui.Graphics.Skia
}
protected override void PlatformSetStrokeDashPattern(
float[] pattern,
float[] strokePattern,
float strokeDashOffset,
float strokeSize)
{
CurrentState.SetStrokeDashPattern(pattern, strokeSize);
CurrentState.SetStrokeDashPattern(strokePattern, strokeDashOffset, strokeSize);
}
public override void SetFillPaint(Paint paint, RectF rectangle)

Просмотреть файл

@ -119,7 +119,7 @@ namespace Microsoft.Maui.Graphics.Skia
set => StrokePaint.StrokeMiter = value;
}
public void SetStrokeDashPattern(float[] pattern, float strokeSize)
public void SetStrokeDashPattern(float[] pattern, float strokeDashOffset, float strokeSize)
{
if (pattern == null || pattern.Length == 0 || strokeSize == 0)
{

Просмотреть файл

@ -84,9 +84,9 @@ After:
set => CurrentState.StrokeLineJoin = value;
}
protected override void PlatformSetStrokeDashPattern(float[] pattern, float strokeSize)
protected override void PlatformSetStrokeDashPattern(float[] strokePattern, float strokeDashOffset, float strokeSize)
{
CurrentState.SetStrokeDashPattern(pattern, strokeSize);
CurrentState.SetStrokeDashPattern(strokePattern, strokeDashOffset, strokeSize);
}
public override Color FillColor

Просмотреть файл

@ -21,6 +21,7 @@ namespace Microsoft.Maui.Graphics.Win2D
private float _alpha = 1;
private float[] _dashes;
private float _dashOffset;
private ICanvasBrush _fillBrush;
private bool _fillBrushValid;
@ -97,6 +98,7 @@ namespace Microsoft.Maui.Graphics.Win2D
_shadowColorValid = prototype._shadowColorValid;
_dashes = prototype._dashes;
_dashOffset = prototype._dashOffset;
_strokeStyle = prototype._strokeStyle;
_lineJoin = prototype._lineJoin;
_lineCap = prototype._lineCap;
@ -127,6 +129,7 @@ namespace Microsoft.Maui.Graphics.Win2D
_strokeStyle = null;
_dashes = null;
_dashOffset = 1;
_miterLimit = CanvasDefaults.DefaultMiterLimit;
_lineCap = CanvasCapStyle.Flat;
_lineJoin = CanvasLineJoin.Miter;
@ -242,7 +245,7 @@ namespace Microsoft.Maui.Graphics.Win2D
}
}
public void SetStrokeDashPattern(float[] pattern, float strokeSize)
public void SetStrokeDashPattern(float[] pattern, float strokeDashOffset, float strokeSize)
{
if (pattern == null || pattern.Length == 0)
{
@ -254,6 +257,8 @@ namespace Microsoft.Maui.Graphics.Win2D
_dashes = pattern;
}
_dashOffset = strokeDashOffset;
InvalidateStrokeStyle();
_needsStrokeStyle = true;
}
@ -574,6 +579,7 @@ After:
_strokeStyle.CustomDashStyle = _emptyFloatArray;
}
_strokeStyle.DashOffset = _dashOffset;
_strokeStyle.MiterLimit = _miterLimit;
_strokeStyle.StartCap = _lineCap;
_strokeStyle.EndCap = _lineCap;

Просмотреть файл

@ -273,6 +273,7 @@ namespace Microsoft.Maui.Graphics.Xaml
element.Stroke = CurrentState.XamlStrokeBrush;
element.StrokeThickness = CurrentState.StrokeSize;
element.StrokeDashArray = CurrentState.XamlDashArray;
element.StrokeDashOffset = CurrentState.StrokeDashOffset;
element.StrokeEndLineCap = CurrentState.XamlLineCap;
element.StrokeStartLineCap = CurrentState.XamlLineCap;
element.StrokeMiterLimit = CurrentState.MiterLimit;
@ -295,6 +296,7 @@ namespace Microsoft.Maui.Graphics.Xaml
element.Stroke = CurrentState.XamlStrokeBrush;
element.StrokeThickness = CurrentState.StrokeSize;
element.StrokeDashArray = CurrentState.XamlDashArray;
element.StrokeDashOffset = CurrentState.StrokeDashOffset;
element.StrokeEndLineCap = CurrentState.XamlLineCap;
element.StrokeStartLineCap = CurrentState.XamlLineCap;
element.StrokeMiterLimit = CurrentState.MiterLimit;
@ -320,6 +322,7 @@ namespace Microsoft.Maui.Graphics.Xaml
element.Stroke = CurrentState.XamlStrokeBrush;
element.StrokeThickness = CurrentState.StrokeSize;
element.StrokeDashArray = CurrentState.XamlDashArray;
element.StrokeDashOffset = CurrentState.StrokeDashOffset;
element.StrokeEndLineCap = CurrentState.XamlLineCap;
element.StrokeStartLineCap = CurrentState.XamlLineCap;
element.StrokeMiterLimit = CurrentState.MiterLimit;
@ -345,6 +348,7 @@ namespace Microsoft.Maui.Graphics.Xaml
element.Stroke = CurrentState.XamlStrokeBrush;
element.StrokeThickness = CurrentState.StrokeSize;
element.StrokeDashArray = CurrentState.XamlDashArray;
element.StrokeDashOffset = CurrentState.StrokeDashOffset;
element.StrokeEndLineCap = CurrentState.XamlLineCap;
element.StrokeStartLineCap = CurrentState.XamlLineCap;
element.StrokeMiterLimit = CurrentState.MiterLimit;
@ -412,6 +416,7 @@ namespace Microsoft.Maui.Graphics.Xaml
element.Stroke = CurrentState.XamlStrokeBrush;
element.StrokeThickness = CurrentState.StrokeSize;
element.StrokeDashArray = CurrentState.XamlDashArray;
element.StrokeDashOffset = CurrentState.StrokeDashOffset;
element.StrokeEndLineCap = CurrentState.XamlLineCap;
element.StrokeStartLineCap = CurrentState.XamlLineCap;
element.StrokeMiterLimit = CurrentState.MiterLimit;
@ -444,13 +449,14 @@ namespace Microsoft.Maui.Graphics.Xaml
{
System.Diagnostics.Debug.WriteLine(exc);
}
element.StrokeDashOffset = CurrentState.StrokeDashOffset;
element.StrokeEndLineCap = CurrentState.XamlLineCap;
element.StrokeStartLineCap = CurrentState.XamlLineCap;
element.StrokeMiterLimit = CurrentState.MiterLimit;
element.Opacity = CurrentState.Alpha;
}
protected override void PlatformSetStrokeDashPattern(float[] pattern, float strokeSize)
protected override void PlatformSetStrokeDashPattern(float[] strokePattern, float strokeDashOffset, float strokeSize)
{
CurrentState.XamlDashArray = null;
}

Просмотреть файл

@ -270,6 +270,7 @@ namespace Microsoft.Maui.Graphics.Xaml
element.Stroke = CurrentState.XamlStrokeBrush;
element.StrokeThickness = CurrentState.StrokeSize;
element.StrokeDashArray = CurrentState.XamlDashArray;
element.StrokeDashOffset = CurrentState.StrokeDashOffset;
element.StrokeEndLineCap = CurrentState.XamlLineCap;
element.StrokeStartLineCap = CurrentState.XamlLineCap;
element.StrokeMiterLimit = CurrentState.MiterLimit;
@ -293,6 +294,7 @@ namespace Microsoft.Maui.Graphics.Xaml
element.Stroke = CurrentState.XamlStrokeBrush;
element.StrokeThickness = CurrentState.StrokeSize;
element.StrokeDashArray = CurrentState.XamlDashArray;
element.StrokeDashOffset = CurrentState.StrokeDashOffset;
element.StrokeEndLineCap = CurrentState.XamlLineCap;
element.StrokeStartLineCap = CurrentState.XamlLineCap;
element.StrokeMiterLimit = CurrentState.MiterLimit;
@ -319,6 +321,7 @@ namespace Microsoft.Maui.Graphics.Xaml
element.Stroke = CurrentState.XamlStrokeBrush;
element.StrokeThickness = CurrentState.StrokeSize;
element.StrokeDashArray = CurrentState.XamlDashArray;
element.StrokeDashOffset = CurrentState.StrokeDashOffset;
element.StrokeEndLineCap = CurrentState.XamlLineCap;
element.StrokeStartLineCap = CurrentState.XamlLineCap;
element.StrokeMiterLimit = CurrentState.MiterLimit;
@ -345,6 +348,7 @@ namespace Microsoft.Maui.Graphics.Xaml
element.Stroke = CurrentState.XamlStrokeBrush;
element.StrokeThickness = CurrentState.StrokeSize;
element.StrokeDashArray = CurrentState.XamlDashArray;
element.StrokeDashOffset = CurrentState.StrokeDashOffset;
element.StrokeEndLineCap = CurrentState.XamlLineCap;
element.StrokeStartLineCap = CurrentState.XamlLineCap;
element.StrokeMiterLimit = CurrentState.MiterLimit;
@ -415,6 +419,7 @@ namespace Microsoft.Maui.Graphics.Xaml
element.Stroke = CurrentState.XamlStrokeBrush;
element.StrokeThickness = CurrentState.StrokeSize;
element.StrokeDashArray = CurrentState.XamlDashArray;
element.StrokeDashOffset = CurrentState.StrokeDashOffset;
element.StrokeEndLineCap = CurrentState.XamlLineCap;
element.StrokeStartLineCap = CurrentState.XamlLineCap;
element.StrokeMiterLimit = CurrentState.MiterLimit;
@ -441,6 +446,7 @@ namespace Microsoft.Maui.Graphics.Xaml
element.Stroke = CurrentState.XamlStrokeBrush;
element.StrokeThickness = CurrentState.StrokeSize;
element.StrokeDashArray = CurrentState.XamlDashArray;
element.StrokeDashOffset = CurrentState.StrokeDashOffset;
element.StrokeEndLineCap = CurrentState.XamlLineCap;
element.StrokeStartLineCap = CurrentState.XamlLineCap;
element.StrokeMiterLimit = CurrentState.MiterLimit;
@ -448,7 +454,7 @@ namespace Microsoft.Maui.Graphics.Xaml
element.Effect = CurrentState.XamlEffect;
}
protected override void NativeSetStrokeDashPattern(float[] pattern, float strokeSize)
protected override void NativeSetStrokeDashPattern(float[] strokePattern, float strokeSize)
{
CurrentState.XamlDashArray = null;
}

Просмотреть файл

@ -19,7 +19,8 @@ namespace Microsoft.Maui.Graphics
private bool _strokeDashPatternDirty;
protected abstract float PlatformStrokeSize { set; }
protected abstract void PlatformSetStrokeDashPattern(float[] pattern, float strokeSize);
protected abstract void PlatformSetStrokeDashPattern(float[] strokePattern, float strokeDashOffset, float strokeSize);
protected abstract void PlatformDrawLine(float x1, float y1, float x2, float y2);
protected abstract void PlatformDrawArc(float x, float y, float width, float height, float startAngle, float endAngle, bool clockwise, bool closed);
protected abstract void PlatformDrawRectangle(float x, float y, float width, float height);
@ -113,12 +114,21 @@ namespace Microsoft.Maui.Graphics
}
}
}
public float StrokeDashOffset
{
set
{
var dashOffset = value;
_currentState.StrokeDashOffset = dashOffset;
}
}
private void EnsureStrokePatternSet()
{
if (_strokeDashPatternDirty)
{
PlatformSetStrokeDashPattern(_currentState.StrokeDashPattern, _currentState.StrokeSize);
PlatformSetStrokeDashPattern(_currentState.StrokeDashPattern, _currentState.StrokeDashOffset, _currentState.StrokeSize);
_strokeDashPatternDirty = false;
}
}

Просмотреть файл

@ -199,9 +199,9 @@ namespace Microsoft.Maui.Graphics.Platform
}
}
protected override void PlatformSetStrokeDashPattern(float[] patter, float linewidth)
protected override void PlatformSetStrokeDashPattern(float[] strokePattern, float strokeDashOffset, float strokeSize)
{
CurrentState.SetStrokeDashPattern(patter, linewidth);
CurrentState.SetStrokeDashPattern(strokePattern, strokeDashOffset, strokeSize);
}
public override void SetFillPaint(Paint paint, RectF rectangle)

Просмотреть файл

@ -111,7 +111,7 @@ namespace Microsoft.Maui.Graphics.Platform
set => StrokePaint.StrokeMiter = value;
}
public void SetStrokeDashPattern(float[] pattern, float strokeSize)
public void SetStrokeDashPattern(float[] pattern, float strokeDashOffset, float strokeSize)
{
if (pattern == null || pattern.Length == 0 || strokeSize == 0)
{
@ -123,14 +123,16 @@ namespace Microsoft.Maui.Graphics.Platform
if (scaledStrokeSize == 1)
{
StrokePaint.SetPathEffect(new DashPathEffect(pattern, 0));
StrokePaint.SetPathEffect(new DashPathEffect(pattern, strokeDashOffset));
}
else
{
var scaledPattern = new float[pattern.Length];
for (int i = 0; i < pattern.Length; i++)
scaledPattern[i] = pattern[i] * scaledStrokeSize;
StrokePaint.SetPathEffect(new DashPathEffect(scaledPattern, 0));
var scaledStrokeDashOffset = strokeDashOffset * scaledStrokeSize;
StrokePaint.SetPathEffect(new DashPathEffect(scaledPattern, scaledStrokeDashOffset));
}
}
}

Просмотреть файл

@ -6,6 +6,7 @@ namespace Microsoft.Maui.Graphics
public class CanvasState : IDisposable
{
public float[] StrokeDashPattern { get; set; }
public float StrokeDashOffset { get; set; } = 1;
public float StrokeSize { get; set; } = 1;
private Matrix3x2 _transform = Matrix3x2.Identity;
@ -37,6 +38,7 @@ namespace Microsoft.Maui.Graphics
protected CanvasState(CanvasState prototype)
{
StrokeDashPattern = prototype.StrokeDashPattern;
StrokeDashOffset = prototype.StrokeDashOffset;
StrokeSize = prototype.StrokeSize;
this._transform = prototype._transform;

Просмотреть файл

@ -1,5 +1,4 @@
using System.Numerics;
using Microsoft.Maui.Graphics.Text;
namespace Microsoft.Maui.Graphics
@ -15,6 +14,7 @@ namespace Microsoft.Maui.Graphics
public LineCap StrokeLineCap { set; }
public LineJoin StrokeLineJoin { set; }
public float[] StrokeDashPattern { set; }
public float StrokeDashOffset { set; }
public Color FillColor { set; }
public Color FontColor { set; }
public IFont Font { set; }
@ -26,6 +26,7 @@ namespace Microsoft.Maui.Graphics
public void DrawPath(PathF path);
public void FillPath(PathF path, WindingMode windingMode);
public void SubtractFromClip(float x, float y, float width, float height);
public void ClipPath(PathF path, WindingMode windingMode = WindingMode.NonZero);

Просмотреть файл

@ -274,9 +274,9 @@ namespace Microsoft.Maui.Graphics.Platform
}
}
protected override void PlatformSetStrokeDashPattern(float[] pattern, float strokeSize)
protected override void PlatformSetStrokeDashPattern(float[] strokePattern, float strokeDashOffset, float strokeSize)
{
if (pattern == null)
if (strokePattern == null)
{
_context.SetLineDash(0, EmptyNFloatArray);
}
@ -293,13 +293,15 @@ namespace Microsoft.Maui.Graphics.Platform
actualStrokeSize = strokeLimit / scale;
}
var actualDashPattern = new nfloat[pattern.Length];
for (var i = 0; i < pattern.Length; i++)
var actualDashPattern = new nfloat[strokePattern.Length];
for (var i = 0; i < strokePattern.Length; i++)
{
actualDashPattern[i] = pattern[i] * actualStrokeSize;
actualDashPattern[i] = strokePattern[i] * actualStrokeSize;
}
_context.SetLineDash(0, actualDashPattern, actualDashPattern.Length);
var actualDashOffset = strokeDashOffset * actualStrokeSize;
_context.SetLineDash(actualDashOffset, actualDashPattern, actualDashPattern.Length);
}
}

Просмотреть файл

@ -71,6 +71,11 @@ namespace Microsoft.Maui.Graphics
set { _commands.Add(canvas => canvas.StrokeDashPattern = value); }
}
public float StrokeDashOffset
{
set { _commands.Add(canvas => canvas.StrokeDashOffset = value); }
}
public Color FillColor
{
set { _commands.Add(canvas => canvas.FillColor = value); }

Просмотреть файл

@ -72,6 +72,11 @@ namespace Microsoft.Maui.Graphics
set => _canvas.StrokeDashPattern = value;
}
public float StrokeDashOffset
{
set => _canvas.StrokeDashOffset = value;
}
public Color FillColor
{
set => _canvas.FillColor = value;