Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Source/SVGImage/DotNetProjects.SVGImage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
<DefineConstants Condition=" '$(TargetFramework)' == 'net40' ">$(DefineConstants);DOTNET40;NETFULL</DefineConstants>
<DefineConstants Condition="$(TargetFramework.StartsWith('net45'))">$(DefineConstants);DOTNET45;NETFULL</DefineConstants>
<DefineConstants Condition="$(TargetFramework.StartsWith('net46'))">$(DefineConstants);DOTNET46;NETFULL</DefineConstants>
<DefineConstants Condition="$(TargetFramework.StartsWith('net47'))">$(DefineConstants);DOTNET47;NETFULL</DefineConstants>
<DefineConstants Condition="$(TargetFramework.StartsWith('net48'))">$(DefineConstants);DOTNET48;NETFULL</DefineConstants>
<DefineConstants Condition="$(TargetFramework.StartsWith('netcore'))">$(DefineConstants);NETCORE</DefineConstants>
<DefineConstants Condition="$(TargetFramework.StartsWith('net6'))">$(DefineConstants);NETCORE;NETNEXT</DefineConstants>
<DefineConstants Condition="$(TargetFramework.StartsWith('net7'))">$(DefineConstants);NETCORE;NETNEXT</DefineConstants>
<DefineConstants Condition="$(TargetFramework.StartsWith('net8'))">$(DefineConstants);NETCORE;NETNEXT</DefineConstants>
<DefineConstants Condition="$(TargetFramework.StartsWith('net47'))">$(DefineConstants);DOTNET47;DPI_AWARE;NETFULL</DefineConstants>
<DefineConstants Condition="$(TargetFramework.StartsWith('net48'))">$(DefineConstants);DOTNET48;DPI_AWARE;NETFULL</DefineConstants>
<DefineConstants Condition="$(TargetFramework.StartsWith('netcore'))">$(DefineConstants);NETCORE;DPI_AWARE</DefineConstants>
<DefineConstants Condition="$(TargetFramework.StartsWith('net6'))">$(DefineConstants);NETCORE;DPI_AWARE;NETNEXT</DefineConstants>
<DefineConstants Condition="$(TargetFramework.StartsWith('net7'))">$(DefineConstants);NETCORE;DPI_AWARE;NETNEXT</DefineConstants>
<DefineConstants Condition="$(TargetFramework.StartsWith('net8'))">$(DefineConstants);NETCORE;DPI_AWARE;NETNEXT</DefineConstants>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>SVGImage.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
Expand Down
21 changes: 18 additions & 3 deletions Source/SVGImage/SVG/SVGImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public enum eSizeType
DependencyProperty.Register("UriSource", typeof(Uri), typeof(SVGImage),
new FrameworkPropertyMetadata(null, OnUriSourceChanged));

public static DependencyProperty SizeTypeProperty = DependencyProperty.Register("SizeType",
public static readonly DependencyProperty SizeTypeProperty = DependencyProperty.Register("SizeType",
typeof(eSizeType), typeof(SVGImage), new FrameworkPropertyMetadata(eSizeType.ContentToSizeNoStretch,
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback(OnSizeTypeChanged)));
Expand All @@ -84,7 +84,7 @@ public enum eSizeType
public static readonly DependencyProperty FileSourceProperty = DependencyProperty.Register("FileSource",
typeof(string), typeof(SVGImage), new PropertyMetadata(OnFileSourceChanged));

public static DependencyProperty ImageSourcePoperty = DependencyProperty.Register("ImageSource",
public static readonly DependencyProperty ImageSourcePoperty = DependencyProperty.Register("ImageSource",
typeof(Drawing), typeof(SVGImage), new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback(OnImageSourceChanged)));
Expand Down Expand Up @@ -247,6 +247,11 @@ public Uri BaseUri
}
}



/// <summary>
/// Rerenders if CustomBrushesPropertyChanged, OverrideStrokeWidthPropertyChanged, OverrideStrokeColorPropertyChanged, OverrideFillColorPropertyChanged, OverrideColorPropertyChanged
/// </summary>
public void ReRenderSvg()
{
if (_render != null)
Expand Down Expand Up @@ -364,7 +369,17 @@ protected override void OnInitialized(EventArgs e)
_render.OverrideStrokeWidth = OverrideStrokeWidth;
_render.UseAnimations = this.UseAnimations;

_loadImage(_render);
//This is to prevent double rendering because setting CustomBrushes = brushesFromSVG; invokes ReRenderSvg
//Not sure if it has any side effects
if (!String.IsNullOrEmpty(FileSource))
{
_render.LoadDrawingWithoutRender(FileSource);
}
else
{
_loadImage(_render);
}
//_loadImage(_render);
_loadImage = null;
var brushesFromSVG = new Dictionary<string, Brush>();
foreach (var server in _render.SVG.PaintServers.GetServers())
Expand Down
15 changes: 12 additions & 3 deletions Source/SVGImage/SVG/SVGRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public DrawingGroup LoadDrawing(string filename)
return this.CreateDrawing(this.SVG);
}

internal void LoadDrawingWithoutRender(string filename)
{
this.SVG = new SVG(filename, ExternalFileLoader);
}
internal void LoadDrawingWithoutRender(Stream stream)
{
this.SVG = new SVG(stream, ExternalFileLoader);
}

public DrawingGroup LoadXmlDrawing(string fileXml)
{
this.SVG = new SVG(this.ExternalFileLoader);
Expand Down Expand Up @@ -430,13 +439,13 @@ internal DrawingGroup LoadGroup(IList<Shape> elements, Rect? viewBox, bool isSwi
}
if (shape is TextShape textShape)
{
GeometryGroup gp = TextRender.BuildTextGeometry(textShape);
TextRender textRender2 = new TextRender();
GeometryGroup gp = textRender2.BuildTextGeometry(textShape);
if (gp != null)
{
foreach (Geometry gm in gp.Children)
{
TextSpan tspan = TextRender.GetElement(gm);
if (tspan != null)
if (TextRenderBase.GetElement(gm) is TextShapeBase tspan)
{
var di = this.NewDrawingItem(tspan, gm);
AddDrawingToGroup(grp, shape, di);
Expand Down
48 changes: 48 additions & 0 deletions Source/SVGImage/SVG/Shapes/CharacterLayout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;

namespace SVGImage.SVG.Shapes
{
/// <summary>
/// Represents a per-character layout result.
/// </summary>
public class CharacterLayout
{
private CharacterLayout()
{
// Default constructor for array creation
}
public CharacterLayout(char character)
{
Character = character;
}
public char Character { get; set; } = '\0';
public int GlobalIndex { get; set; }
public double X { get; set; } = 0;
public double Y { get; set; } = 0;
public double DX { get; set; } = Double.NaN;
public double DY { get; set; } = Double.NaN;
public double Rotation { get; set; } = Double.NaN;
public bool Hidden { get; set; } = false;
public bool Addressable { get; set; } = true;
public bool Middle { get; set; } = false;
public bool AnchoredChunk { get; set; } = false;
/// <summary>
/// Not used, part of the SVG 2.0 spec.
/// </summary>
internal bool FirstCharacterInResolvedDescendant { get; set; }
/// <summary>
/// The character redefines the X position for anteceding characters.
/// </summary>
public bool DoesPositionX { get; internal set; }
/// <summary>
/// The character redefines the Y position for anteceding characters.
/// </summary>
public bool DoesPositionY { get; internal set; }


}




}
16 changes: 2 additions & 14 deletions Source/SVGImage/SVG/Shapes/CircleShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@ namespace SVGImage.SVG.Shapes

public sealed class CircleShape : Shape
{
private static Fill DefaultFill = null;

public CircleShape(SVG svg, XmlNode node) : base(svg, node)
{
if (DefaultFill == null)
{
DefaultFill = Fill.CreateDefault(svg, "black");
}

Rect? box = svg.ViewBox;

this.CX = XmlUtil.AttrValue(node, "cx", 0, box.HasValue ? box.Value.Width : svg.Size.Width);
Expand All @@ -27,14 +20,9 @@ public CircleShape(SVG svg, XmlNode node) : base(svg, node)
diagRef = Math.Sqrt(box.Value.Width * box.Value.Width + box.Value.Height * box.Value.Height) / Math.Sqrt(2);
this.R = XmlUtil.AttrValue(node, "r", 0, diagRef);
}

public override Fill Fill
protected override Fill DefaultFill()
{
get {
Fill f = base.Fill;
if (f == null) f = DefaultFill;
return f;
}
return Fill.CreateDefault(Svg, "black");
}

public double CX { get; set; }
Expand Down
16 changes: 3 additions & 13 deletions Source/SVGImage/SVG/Shapes/EllipseShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@ namespace SVGImage.SVG.Shapes

public sealed class EllipseShape : Shape
{
private static Fill DefaultFill = null;

public EllipseShape(SVG svg, XmlNode node)
: base(svg, node)
{
if (DefaultFill == null)
{
DefaultFill = Fill.CreateDefault(svg, "black");
}
{

Rect? box = svg.ViewBox;

Expand All @@ -30,13 +24,9 @@ public EllipseShape(SVG svg, XmlNode node)
this.RY = XmlUtil.AttrValue(node, "ry", 0, diagRef);
}

public override Fill Fill
protected override Fill DefaultFill()
{
get {
Fill f = base.Fill;
if (f == null) f = DefaultFill;
return f;
}
return Fill.CreateDefault(Svg, "black");
}

public double CX { get; set; }
Expand Down
1 change: 1 addition & 0 deletions Source/SVGImage/SVG/Shapes/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private Shape AddToList(SVG svg, XmlNode childnode, Shape parent, bool isDefinit
else if (nodeName == SVGTags.sAnimateTransform)
retVal = new AnimateTransform(svg, childnode, parent);
else if (nodeName == SVGTags.sText)
//retVal = new TextShape(svg, childnode, parent);
retVal = new TextShape(svg, childnode, parent);
else if (nodeName == SVGTags.sLinearGradient)
{
Expand Down
11 changes: 11 additions & 0 deletions Source/SVGImage/SVG/Shapes/ITextChild.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace SVGImage.SVG.Shapes
{
public interface ITextChild : ITextNode
{
Shape Parent { get; set; }
}




}
15 changes: 15 additions & 0 deletions Source/SVGImage/SVG/Shapes/ITextNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace SVGImage.SVG.Shapes
{
public interface ITextNode
{
CharacterLayout GetFirstCharacter();
CharacterLayout GetLastCharacter();
string GetText();
int GetLength();
CharacterLayout[] GetCharacters();
}




}
19 changes: 19 additions & 0 deletions Source/SVGImage/SVG/Shapes/LengthAdjustment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace SVGImage.SVG.Shapes
{
public enum LengthAdjustment
{
None,
/// <summary>
/// Indicates that only the advance values are adjusted. The glyphs themselves are not stretched or compressed.
/// </summary>
Spacing,
/// <summary>
/// Indicates that the advance values are adjusted and the glyphs themselves stretched or compressed in one axis (i.e., a direction parallel to the inline-base direction).
/// </summary>
SpacingAndGlyphs
}




}
65 changes: 65 additions & 0 deletions Source/SVGImage/SVG/Shapes/LengthContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;

namespace SVGImage.SVG.Shapes
{
public class LengthContext
{
public Shape Owner { get; set; }
public LengthUnit Unit { get; set; }
private static readonly Dictionary<string, LengthUnit> _unitMap = new Dictionary<string, LengthUnit>(StringComparer.OrdinalIgnoreCase)
{
{"em", LengthUnit.em},
{"ex", LengthUnit.ex},
{"ch", LengthUnit.ch},
{"rem", LengthUnit.rem},
{"vw", LengthUnit.vw},
{"vh", LengthUnit.vh},
{"vmin", LengthUnit.vmin},
{"vmax", LengthUnit.vmax},
{"cm", LengthUnit.cm},
{"mm", LengthUnit.mm},
{"Q", LengthUnit.Q},
{"in", LengthUnit.Inches},
{"pc", LengthUnit.pc},
{"pt", LengthUnit.pt},
{"px", LengthUnit.px},
};

public LengthContext(Shape owner, LengthUnit unit)
{
Owner = owner;
Unit = unit;
}

public static LengthUnit Parse(string text, LengthOrientation orientation = LengthOrientation.None)
{
if (String.IsNullOrEmpty(text))
{
return LengthUnit.Number;
}
string trimmed = text.Trim();
if(trimmed == "%")
{
switch (orientation)
{
case LengthOrientation.Horizontal:
return LengthUnit.PercentWidth;
case LengthOrientation.Vertical:
return LengthUnit.PercentHeight;
default:
return LengthUnit.PercentDiagonal;
}
}
if(_unitMap.TryGetValue(trimmed, out LengthUnit unit))
{
return unit;
}
return LengthUnit.Unknown;
}
}




}
13 changes: 13 additions & 0 deletions Source/SVGImage/SVG/Shapes/LengthOrientation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace SVGImage.SVG.Shapes
{
public enum LengthOrientation
{
None,
Horizontal,
Vertical,
}




}
Loading