Skip to content

Commit

Permalink
WPF path rendering improvements, gradient stop fix
Browse files Browse the repository at this point in the history
Improved the rendering of paths with transformations.
Added support for gradient stop with missing offset value, will default to 0.
  • Loading branch information
paulushub committed Nov 8, 2018
1 parent 4601700 commit 1f3edee
Show file tree
Hide file tree
Showing 23 changed files with 452 additions and 37 deletions.
Binary file not shown.
Binary file modified Applications/ConvertersWpf/Output/SharpVectors.Core.dll
Binary file not shown.
Binary file modified Applications/ConvertersWpf/Output/SharpVectors.Css.dll
Binary file not shown.
Binary file modified Applications/ConvertersWpf/Output/SharpVectors.Dom.dll
Binary file not shown.
Binary file modified Applications/ConvertersWpf/Output/SharpVectors.Model.dll
Binary file not shown.
Binary file not shown.
Binary file modified Applications/ConvertersWpf/Output/SharpVectors.Runtime.Wpf.dll
Binary file not shown.
Binary file modified Applications/ConvertersWpf/Output/SharpVectors.exe
Binary file not shown.
Binary file modified Output/SharpVectors.Converters.Wpf.dll
Binary file not shown.
Binary file modified Output/SharpVectors.Core.dll
Binary file not shown.
Binary file modified Output/SharpVectors.Css.dll
Binary file not shown.
Binary file modified Output/SharpVectors.Dom.dll
Binary file not shown.
Binary file modified Output/SharpVectors.Model.dll
Binary file not shown.
Binary file modified Output/SharpVectors.Rendering.Gdi.dll
Binary file not shown.
Binary file modified Output/SharpVectors.Rendering.Wpf.dll
Binary file not shown.
Binary file modified Output/SharpVectors.Runtime.Wpf.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<Compile Include="Shapes\WpfShapeHelper.cs" />
<Compile Include="Shapes\WpfShapeRenderer.cs" />
<Compile Include="StreamSvgConverter.cs" />
<Compile Include="SvgControl.cs" />
<Compile Include="Utils\DirectoryCopier.cs" />
<Compile Include="Utils\DirectoryUtils.cs" />
<Compile Include="FileSvgConverter.cs" />
Expand Down Expand Up @@ -127,6 +128,12 @@
<Private>False</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Page Include="Themes\Generic.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
54 changes: 54 additions & 0 deletions Source/SharpVectorConvertersWpf/SvgControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SharpVectors.Converters
{
/// <summary>
/// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
///
/// Step 1a) Using this custom control in a XAML file that exists in the current project.
/// Add this XmlNamespace attribute to the root element of the markup file where it is
/// to be used:
///
/// xmlns:MyNamespace="clr-namespace:SharpVectors.Converters"
///
///
/// Step 1b) Using this custom control in a XAML file that exists in a different project.
/// Add this XmlNamespace attribute to the root element of the markup file where it is
/// to be used:
///
/// xmlns:MyNamespace="clr-namespace:SharpVectors.Converters;assembly=SharpVectors.Converters"
///
/// You will also need to add a project reference from the project where the XAML file lives
/// to this project and Rebuild to avoid compilation errors:
///
/// Right click on the target project in the Solution Explorer and
/// "Add Reference"->"Projects"->[Browse to and select this project]
///
///
/// Step 2)
/// Go ahead and use your control in the XAML file.
///
/// <MyNamespace:SvgControl/>
///
/// </summary>
public class SvgControl : Control
{
static SvgControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(SvgControl),
new FrameworkPropertyMetadata(typeof(SvgControl)));
}
}
}
19 changes: 19 additions & 0 deletions Source/SharpVectorConvertersWpf/Themes/Generic.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SharpVectors.Converters">


<Style TargetType="{x:Type local:SvgControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SvgControl}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
72 changes: 56 additions & 16 deletions Source/SharpVectorRenderingWpf/Wpf/WpfDrawingSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@ namespace SharpVectors.Renderers.Wpf
/// <summary>
/// This provides the options for the drawing/rendering engine of the WPF.
/// </summary>
[Serializable]
public sealed class WpfDrawingSettings : DependencyObject, ICloneable
{
#region Private Fields

private bool _ensureViewboxSize;
private bool _textAsGeometry;
private bool _includeRuntime;
private bool _optimizePath;

private long _pixelWidth;
private long _pixelHeight;
private int _pixelWidth;
private int _pixelHeight;
private bool _ensureViewboxPosition;
private bool _ensureViewboxSize;

// Formating properties
private CultureInfo _culture;
private CultureInfo _neutralCulture;

// Text rendering fonts properties
private string _defaultFontName;
private static FontFamily _defaultFontFamily;
private static FontFamily _genericSerif;
Expand All @@ -43,15 +47,18 @@ public sealed class WpfDrawingSettings : DependencyObject, ICloneable
/// </summary>
public WpfDrawingSettings()
{
_defaultFontName = "Arial Unicode MS";
_textAsGeometry = false;
_optimizePath = true;
_includeRuntime = true;
_neutralCulture = CultureInfo.GetCultureInfo("en-us");
_culture = CultureInfo.GetCultureInfo("en-us");

_pixelWidth = -1;
_pixelHeight = -1;
_defaultFontName = "Arial Unicode MS";
_textAsGeometry = false;
_optimizePath = true;
_includeRuntime = true;
_neutralCulture = CultureInfo.GetCultureInfo("en-us");
_culture = CultureInfo.GetCultureInfo("en-us");

_pixelWidth = -1;
_pixelHeight = -1;

_ensureViewboxSize = false;
_ensureViewboxPosition = true;
}

/// <summary>
Expand All @@ -63,22 +70,31 @@ public WpfDrawingSettings()
/// </param>
public WpfDrawingSettings(WpfDrawingSettings settings)
{
if (settings == null)
{
return;
}

_defaultFontName = settings._defaultFontName;
_textAsGeometry = settings._textAsGeometry;
_optimizePath = settings._optimizePath;
_includeRuntime = settings._includeRuntime;

_neutralCulture = settings._neutralCulture;
_culture = settings._culture;

_pixelWidth = settings._pixelWidth;
_pixelHeight = settings._pixelHeight;

_ensureViewboxSize = settings._ensureViewboxSize;
_ensureViewboxPosition = settings._ensureViewboxPosition;
}

#endregion

#region Public Properties

public long PixelWidth
public int PixelWidth
{
get {
return _pixelWidth;
Expand All @@ -88,7 +104,7 @@ public long PixelWidth
}
}

public long PixelHeight
public int PixelHeight
{
get {
return _pixelHeight;
Expand All @@ -106,9 +122,10 @@ public bool HasPixelSize
}

/// <summary>
/// Gets or sets a value to indicate saving the original viewbox size when saving images.
/// Gets or sets a value to indicate preserving the original viewbox size when saving images.
/// </summary>
/// <value>For image outputs, this will force the original size to be saved.
/// <value>
/// For image outputs, this will force the original size to be saved.
/// <para>
/// The default value is <see langword="false"/>. However, the ImageSvgConverter converted
/// sets this to <see langword="true"/> by default.
Expand All @@ -129,6 +146,29 @@ public bool EnsureViewboxSize
}
}

/// <summary>
/// Gets or sets a value to indicate applying a translate transform to the viewbox to ensure
/// it is visible when rendered.
/// </summary>
/// <value>
/// This determines whether a transformation is applied to the rendered drawing. For drawings
/// where the top-left position of the viewbox is off the screen, due to negative values, this
/// will ensure the drawing is visible.
/// <para>
/// The default value is <see langword="true"/>. Set this value to <see langword="false"/> if
/// you wish to apply your own transformations to the drawings.
/// </para>
/// </value>
public bool EnsureViewboxPosition
{
get {
return _ensureViewboxPosition;
}
set {
_ensureViewboxPosition = value;
}
}

/// <summary>
/// Gets or sets a value indicating whether the path geometry is
/// optimized using the <see cref="StreamGeometry"/>.
Expand Down
4 changes: 2 additions & 2 deletions Source/SharpVectorRenderingWpf/Wpf/WpfGradientFill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ private GradientStopCollection GetGradientStops(XmlNodeList stops)
for (int i = 0; i < itemCount; i++)
{
SvgStopElement stop = (SvgStopElement)stops.Item(i);
if (stop == null || stop.Offset == null)
if (stop == null)
{
continue;
}
Expand Down Expand Up @@ -369,7 +369,7 @@ private GradientStopCollection GetGradientStops(XmlNodeList stops)
color = Color.FromArgb((byte)Convert.ToInt32(alpha), color.R, color.G, color.B);
}

double offset = stop.Offset.AnimVal;
double offset = (stop.Offset == null) ? 0 : stop.Offset.AnimVal;

offset /= 100;
offset = Math.Max(lastOffset, offset);
Expand Down
Loading

0 comments on commit 1f3edee

Please sign in to comment.